sh_cmt.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH Timer Support - CMT
  4. *
  5. * Copyright (C) 2008 Magnus Damm
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clockchips.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/sh_timer.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. struct sh_cmt_device;
  27. /*
  28. * The CMT comes in 5 different identified flavours, depending not only on the
  29. * SoC but also on the particular instance. The following table lists the main
  30. * characteristics of those flavours.
  31. *
  32. * 16B 32B 32B-F 48B R-Car Gen2
  33. * -----------------------------------------------------------------------------
  34. * Channels 2 1/4 1 6 2/8
  35. * Control Width 16 16 16 16 32
  36. * Counter Width 16 32 32 32/48 32/48
  37. * Shared Start/Stop Y Y Y Y N
  38. *
  39. * The r8a73a4 / R-Car Gen2 version has a per-channel start/stop register
  40. * located in the channel registers block. All other versions have a shared
  41. * start/stop register located in the global space.
  42. *
  43. * Channels are indexed from 0 to N-1 in the documentation. The channel index
  44. * infers the start/stop bit position in the control register and the channel
  45. * registers block address. Some CMT instances have a subset of channels
  46. * available, in which case the index in the documentation doesn't match the
  47. * "real" index as implemented in hardware. This is for instance the case with
  48. * CMT0 on r8a7740, which is a 32-bit variant with a single channel numbered 0
  49. * in the documentation but using start/stop bit 5 and having its registers
  50. * block at 0x60.
  51. *
  52. * Similarly CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
  53. * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
  54. */
  55. enum sh_cmt_model {
  56. SH_CMT_16BIT,
  57. SH_CMT_32BIT,
  58. SH_CMT_48BIT,
  59. SH_CMT0_RCAR_GEN2,
  60. SH_CMT1_RCAR_GEN2,
  61. };
  62. struct sh_cmt_info {
  63. enum sh_cmt_model model;
  64. unsigned int channels_mask;
  65. unsigned long width; /* 16 or 32 bit version of hardware block */
  66. u32 overflow_bit;
  67. u32 clear_bits;
  68. /* callbacks for CMSTR and CMCSR access */
  69. u32 (*read_control)(void __iomem *base, unsigned long offs);
  70. void (*write_control)(void __iomem *base, unsigned long offs,
  71. u32 value);
  72. /* callbacks for CMCNT and CMCOR access */
  73. u32 (*read_count)(void __iomem *base, unsigned long offs);
  74. void (*write_count)(void __iomem *base, unsigned long offs, u32 value);
  75. };
  76. struct sh_cmt_channel {
  77. struct sh_cmt_device *cmt;
  78. unsigned int index; /* Index in the documentation */
  79. unsigned int hwidx; /* Real hardware index */
  80. void __iomem *iostart;
  81. void __iomem *ioctrl;
  82. unsigned int timer_bit;
  83. unsigned long flags;
  84. u32 match_value;
  85. u32 next_match_value;
  86. u32 max_match_value;
  87. raw_spinlock_t lock;
  88. struct clock_event_device ced;
  89. struct clocksource cs;
  90. u64 total_cycles;
  91. bool cs_enabled;
  92. };
  93. struct sh_cmt_device {
  94. struct platform_device *pdev;
  95. const struct sh_cmt_info *info;
  96. void __iomem *mapbase;
  97. struct clk *clk;
  98. unsigned long rate;
  99. raw_spinlock_t lock; /* Protect the shared start/stop register */
  100. struct sh_cmt_channel *channels;
  101. unsigned int num_channels;
  102. unsigned int hw_channels;
  103. bool has_clockevent;
  104. bool has_clocksource;
  105. };
  106. #define SH_CMT16_CMCSR_CMF (1 << 7)
  107. #define SH_CMT16_CMCSR_CMIE (1 << 6)
  108. #define SH_CMT16_CMCSR_CKS8 (0 << 0)
  109. #define SH_CMT16_CMCSR_CKS32 (1 << 0)
  110. #define SH_CMT16_CMCSR_CKS128 (2 << 0)
  111. #define SH_CMT16_CMCSR_CKS512 (3 << 0)
  112. #define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
  113. #define SH_CMT32_CMCSR_CMF (1 << 15)
  114. #define SH_CMT32_CMCSR_OVF (1 << 14)
  115. #define SH_CMT32_CMCSR_WRFLG (1 << 13)
  116. #define SH_CMT32_CMCSR_STTF (1 << 12)
  117. #define SH_CMT32_CMCSR_STPF (1 << 11)
  118. #define SH_CMT32_CMCSR_SSIE (1 << 10)
  119. #define SH_CMT32_CMCSR_CMS (1 << 9)
  120. #define SH_CMT32_CMCSR_CMM (1 << 8)
  121. #define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
  122. #define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
  123. #define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
  124. #define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
  125. #define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
  126. #define SH_CMT32_CMCSR_DBGIVD (1 << 3)
  127. #define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
  128. #define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
  129. #define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
  130. #define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
  131. #define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
  132. static u32 sh_cmt_read16(void __iomem *base, unsigned long offs)
  133. {
  134. return ioread16(base + (offs << 1));
  135. }
  136. static u32 sh_cmt_read32(void __iomem *base, unsigned long offs)
  137. {
  138. return ioread32(base + (offs << 2));
  139. }
  140. static void sh_cmt_write16(void __iomem *base, unsigned long offs, u32 value)
  141. {
  142. iowrite16(value, base + (offs << 1));
  143. }
  144. static void sh_cmt_write32(void __iomem *base, unsigned long offs, u32 value)
  145. {
  146. iowrite32(value, base + (offs << 2));
  147. }
  148. static const struct sh_cmt_info sh_cmt_info[] = {
  149. [SH_CMT_16BIT] = {
  150. .model = SH_CMT_16BIT,
  151. .width = 16,
  152. .overflow_bit = SH_CMT16_CMCSR_CMF,
  153. .clear_bits = ~SH_CMT16_CMCSR_CMF,
  154. .read_control = sh_cmt_read16,
  155. .write_control = sh_cmt_write16,
  156. .read_count = sh_cmt_read16,
  157. .write_count = sh_cmt_write16,
  158. },
  159. [SH_CMT_32BIT] = {
  160. .model = SH_CMT_32BIT,
  161. .width = 32,
  162. .overflow_bit = SH_CMT32_CMCSR_CMF,
  163. .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
  164. .read_control = sh_cmt_read16,
  165. .write_control = sh_cmt_write16,
  166. .read_count = sh_cmt_read32,
  167. .write_count = sh_cmt_write32,
  168. },
  169. [SH_CMT_48BIT] = {
  170. .model = SH_CMT_48BIT,
  171. .channels_mask = 0x3f,
  172. .width = 32,
  173. .overflow_bit = SH_CMT32_CMCSR_CMF,
  174. .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
  175. .read_control = sh_cmt_read32,
  176. .write_control = sh_cmt_write32,
  177. .read_count = sh_cmt_read32,
  178. .write_count = sh_cmt_write32,
  179. },
  180. [SH_CMT0_RCAR_GEN2] = {
  181. .model = SH_CMT0_RCAR_GEN2,
  182. .channels_mask = 0x60,
  183. .width = 32,
  184. .overflow_bit = SH_CMT32_CMCSR_CMF,
  185. .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
  186. .read_control = sh_cmt_read32,
  187. .write_control = sh_cmt_write32,
  188. .read_count = sh_cmt_read32,
  189. .write_count = sh_cmt_write32,
  190. },
  191. [SH_CMT1_RCAR_GEN2] = {
  192. .model = SH_CMT1_RCAR_GEN2,
  193. .channels_mask = 0xff,
  194. .width = 32,
  195. .overflow_bit = SH_CMT32_CMCSR_CMF,
  196. .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
  197. .read_control = sh_cmt_read32,
  198. .write_control = sh_cmt_write32,
  199. .read_count = sh_cmt_read32,
  200. .write_count = sh_cmt_write32,
  201. },
  202. };
  203. #define CMCSR 0 /* channel register */
  204. #define CMCNT 1 /* channel register */
  205. #define CMCOR 2 /* channel register */
  206. static inline u32 sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
  207. {
  208. if (ch->iostart)
  209. return ch->cmt->info->read_control(ch->iostart, 0);
  210. else
  211. return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
  212. }
  213. static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch, u32 value)
  214. {
  215. if (ch->iostart)
  216. ch->cmt->info->write_control(ch->iostart, 0, value);
  217. else
  218. ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
  219. }
  220. static inline u32 sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
  221. {
  222. return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
  223. }
  224. static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch, u32 value)
  225. {
  226. ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
  227. }
  228. static inline u32 sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
  229. {
  230. return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
  231. }
  232. static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch, u32 value)
  233. {
  234. ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
  235. }
  236. static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch, u32 value)
  237. {
  238. ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
  239. }
  240. static u32 sh_cmt_get_counter(struct sh_cmt_channel *ch, u32 *has_wrapped)
  241. {
  242. u32 v1, v2, v3;
  243. u32 o1, o2;
  244. o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
  245. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  246. do {
  247. o2 = o1;
  248. v1 = sh_cmt_read_cmcnt(ch);
  249. v2 = sh_cmt_read_cmcnt(ch);
  250. v3 = sh_cmt_read_cmcnt(ch);
  251. o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
  252. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  253. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  254. *has_wrapped = o1;
  255. return v2;
  256. }
  257. static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
  258. {
  259. unsigned long flags;
  260. u32 value;
  261. /* start stop register shared by multiple timer channels */
  262. raw_spin_lock_irqsave(&ch->cmt->lock, flags);
  263. value = sh_cmt_read_cmstr(ch);
  264. if (start)
  265. value |= 1 << ch->timer_bit;
  266. else
  267. value &= ~(1 << ch->timer_bit);
  268. sh_cmt_write_cmstr(ch, value);
  269. raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
  270. }
  271. static int sh_cmt_enable(struct sh_cmt_channel *ch)
  272. {
  273. int k, ret;
  274. pm_runtime_get_sync(&ch->cmt->pdev->dev);
  275. dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
  276. /* enable clock */
  277. ret = clk_enable(ch->cmt->clk);
  278. if (ret) {
  279. dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
  280. ch->index);
  281. goto err0;
  282. }
  283. /* make sure channel is disabled */
  284. sh_cmt_start_stop_ch(ch, 0);
  285. /* configure channel, periodic mode and maximum timeout */
  286. if (ch->cmt->info->width == 16) {
  287. sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
  288. SH_CMT16_CMCSR_CKS512);
  289. } else {
  290. sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
  291. SH_CMT32_CMCSR_CMTOUT_IE |
  292. SH_CMT32_CMCSR_CMR_IRQ |
  293. SH_CMT32_CMCSR_CKS_RCLK8);
  294. }
  295. sh_cmt_write_cmcor(ch, 0xffffffff);
  296. sh_cmt_write_cmcnt(ch, 0);
  297. /*
  298. * According to the sh73a0 user's manual, as CMCNT can be operated
  299. * only by the RCLK (Pseudo 32 KHz), there's one restriction on
  300. * modifying CMCNT register; two RCLK cycles are necessary before
  301. * this register is either read or any modification of the value
  302. * it holds is reflected in the LSI's actual operation.
  303. *
  304. * While at it, we're supposed to clear out the CMCNT as of this
  305. * moment, so make sure it's processed properly here. This will
  306. * take RCLKx2 at maximum.
  307. */
  308. for (k = 0; k < 100; k++) {
  309. if (!sh_cmt_read_cmcnt(ch))
  310. break;
  311. udelay(1);
  312. }
  313. if (sh_cmt_read_cmcnt(ch)) {
  314. dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
  315. ch->index);
  316. ret = -ETIMEDOUT;
  317. goto err1;
  318. }
  319. /* enable channel */
  320. sh_cmt_start_stop_ch(ch, 1);
  321. return 0;
  322. err1:
  323. /* stop clock */
  324. clk_disable(ch->cmt->clk);
  325. err0:
  326. return ret;
  327. }
  328. static void sh_cmt_disable(struct sh_cmt_channel *ch)
  329. {
  330. /* disable channel */
  331. sh_cmt_start_stop_ch(ch, 0);
  332. /* disable interrupts in CMT block */
  333. sh_cmt_write_cmcsr(ch, 0);
  334. /* stop clock */
  335. clk_disable(ch->cmt->clk);
  336. dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
  337. pm_runtime_put(&ch->cmt->pdev->dev);
  338. }
  339. /* private flags */
  340. #define FLAG_CLOCKEVENT (1 << 0)
  341. #define FLAG_CLOCKSOURCE (1 << 1)
  342. #define FLAG_REPROGRAM (1 << 2)
  343. #define FLAG_SKIPEVENT (1 << 3)
  344. #define FLAG_IRQCONTEXT (1 << 4)
  345. static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
  346. int absolute)
  347. {
  348. u32 value = ch->next_match_value;
  349. u32 new_match;
  350. u32 delay = 0;
  351. u32 now = 0;
  352. u32 has_wrapped;
  353. now = sh_cmt_get_counter(ch, &has_wrapped);
  354. ch->flags |= FLAG_REPROGRAM; /* force reprogram */
  355. if (has_wrapped) {
  356. /* we're competing with the interrupt handler.
  357. * -> let the interrupt handler reprogram the timer.
  358. * -> interrupt number two handles the event.
  359. */
  360. ch->flags |= FLAG_SKIPEVENT;
  361. return;
  362. }
  363. if (absolute)
  364. now = 0;
  365. do {
  366. /* reprogram the timer hardware,
  367. * but don't save the new match value yet.
  368. */
  369. new_match = now + value + delay;
  370. if (new_match > ch->max_match_value)
  371. new_match = ch->max_match_value;
  372. sh_cmt_write_cmcor(ch, new_match);
  373. now = sh_cmt_get_counter(ch, &has_wrapped);
  374. if (has_wrapped && (new_match > ch->match_value)) {
  375. /* we are changing to a greater match value,
  376. * so this wrap must be caused by the counter
  377. * matching the old value.
  378. * -> first interrupt reprograms the timer.
  379. * -> interrupt number two handles the event.
  380. */
  381. ch->flags |= FLAG_SKIPEVENT;
  382. break;
  383. }
  384. if (has_wrapped) {
  385. /* we are changing to a smaller match value,
  386. * so the wrap must be caused by the counter
  387. * matching the new value.
  388. * -> save programmed match value.
  389. * -> let isr handle the event.
  390. */
  391. ch->match_value = new_match;
  392. break;
  393. }
  394. /* be safe: verify hardware settings */
  395. if (now < new_match) {
  396. /* timer value is below match value, all good.
  397. * this makes sure we won't miss any match events.
  398. * -> save programmed match value.
  399. * -> let isr handle the event.
  400. */
  401. ch->match_value = new_match;
  402. break;
  403. }
  404. /* the counter has reached a value greater
  405. * than our new match value. and since the
  406. * has_wrapped flag isn't set we must have
  407. * programmed a too close event.
  408. * -> increase delay and retry.
  409. */
  410. if (delay)
  411. delay <<= 1;
  412. else
  413. delay = 1;
  414. if (!delay)
  415. dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
  416. ch->index);
  417. } while (delay);
  418. }
  419. static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  420. {
  421. if (delta > ch->max_match_value)
  422. dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
  423. ch->index);
  424. ch->next_match_value = delta;
  425. sh_cmt_clock_event_program_verify(ch, 0);
  426. }
  427. static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  428. {
  429. unsigned long flags;
  430. raw_spin_lock_irqsave(&ch->lock, flags);
  431. __sh_cmt_set_next(ch, delta);
  432. raw_spin_unlock_irqrestore(&ch->lock, flags);
  433. }
  434. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  435. {
  436. struct sh_cmt_channel *ch = dev_id;
  437. /* clear flags */
  438. sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
  439. ch->cmt->info->clear_bits);
  440. /* update clock source counter to begin with if enabled
  441. * the wrap flag should be cleared by the timer specific
  442. * isr before we end up here.
  443. */
  444. if (ch->flags & FLAG_CLOCKSOURCE)
  445. ch->total_cycles += ch->match_value + 1;
  446. if (!(ch->flags & FLAG_REPROGRAM))
  447. ch->next_match_value = ch->max_match_value;
  448. ch->flags |= FLAG_IRQCONTEXT;
  449. if (ch->flags & FLAG_CLOCKEVENT) {
  450. if (!(ch->flags & FLAG_SKIPEVENT)) {
  451. if (clockevent_state_oneshot(&ch->ced)) {
  452. ch->next_match_value = ch->max_match_value;
  453. ch->flags |= FLAG_REPROGRAM;
  454. }
  455. ch->ced.event_handler(&ch->ced);
  456. }
  457. }
  458. ch->flags &= ~FLAG_SKIPEVENT;
  459. if (ch->flags & FLAG_REPROGRAM) {
  460. ch->flags &= ~FLAG_REPROGRAM;
  461. sh_cmt_clock_event_program_verify(ch, 1);
  462. if (ch->flags & FLAG_CLOCKEVENT)
  463. if ((clockevent_state_shutdown(&ch->ced))
  464. || (ch->match_value == ch->next_match_value))
  465. ch->flags &= ~FLAG_REPROGRAM;
  466. }
  467. ch->flags &= ~FLAG_IRQCONTEXT;
  468. return IRQ_HANDLED;
  469. }
  470. static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
  471. {
  472. int ret = 0;
  473. unsigned long flags;
  474. raw_spin_lock_irqsave(&ch->lock, flags);
  475. if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  476. ret = sh_cmt_enable(ch);
  477. if (ret)
  478. goto out;
  479. ch->flags |= flag;
  480. /* setup timeout if no clockevent */
  481. if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
  482. __sh_cmt_set_next(ch, ch->max_match_value);
  483. out:
  484. raw_spin_unlock_irqrestore(&ch->lock, flags);
  485. return ret;
  486. }
  487. static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
  488. {
  489. unsigned long flags;
  490. unsigned long f;
  491. raw_spin_lock_irqsave(&ch->lock, flags);
  492. f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  493. ch->flags &= ~flag;
  494. if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  495. sh_cmt_disable(ch);
  496. /* adjust the timeout to maximum if only clocksource left */
  497. if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
  498. __sh_cmt_set_next(ch, ch->max_match_value);
  499. raw_spin_unlock_irqrestore(&ch->lock, flags);
  500. }
  501. static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
  502. {
  503. return container_of(cs, struct sh_cmt_channel, cs);
  504. }
  505. static u64 sh_cmt_clocksource_read(struct clocksource *cs)
  506. {
  507. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  508. unsigned long flags;
  509. u32 has_wrapped;
  510. u64 value;
  511. u32 raw;
  512. raw_spin_lock_irqsave(&ch->lock, flags);
  513. value = ch->total_cycles;
  514. raw = sh_cmt_get_counter(ch, &has_wrapped);
  515. if (unlikely(has_wrapped))
  516. raw += ch->match_value + 1;
  517. raw_spin_unlock_irqrestore(&ch->lock, flags);
  518. return value + raw;
  519. }
  520. static int sh_cmt_clocksource_enable(struct clocksource *cs)
  521. {
  522. int ret;
  523. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  524. WARN_ON(ch->cs_enabled);
  525. ch->total_cycles = 0;
  526. ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  527. if (!ret)
  528. ch->cs_enabled = true;
  529. return ret;
  530. }
  531. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  532. {
  533. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  534. WARN_ON(!ch->cs_enabled);
  535. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  536. ch->cs_enabled = false;
  537. }
  538. static void sh_cmt_clocksource_suspend(struct clocksource *cs)
  539. {
  540. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  541. if (!ch->cs_enabled)
  542. return;
  543. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  544. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  545. }
  546. static void sh_cmt_clocksource_resume(struct clocksource *cs)
  547. {
  548. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  549. if (!ch->cs_enabled)
  550. return;
  551. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  552. sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  553. }
  554. static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
  555. const char *name)
  556. {
  557. struct clocksource *cs = &ch->cs;
  558. cs->name = name;
  559. cs->rating = 125;
  560. cs->read = sh_cmt_clocksource_read;
  561. cs->enable = sh_cmt_clocksource_enable;
  562. cs->disable = sh_cmt_clocksource_disable;
  563. cs->suspend = sh_cmt_clocksource_suspend;
  564. cs->resume = sh_cmt_clocksource_resume;
  565. cs->mask = CLOCKSOURCE_MASK(sizeof(u64) * 8);
  566. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  567. dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
  568. ch->index);
  569. clocksource_register_hz(cs, ch->cmt->rate);
  570. return 0;
  571. }
  572. static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
  573. {
  574. return container_of(ced, struct sh_cmt_channel, ced);
  575. }
  576. static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
  577. {
  578. sh_cmt_start(ch, FLAG_CLOCKEVENT);
  579. if (periodic)
  580. sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
  581. else
  582. sh_cmt_set_next(ch, ch->max_match_value);
  583. }
  584. static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
  585. {
  586. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  587. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  588. return 0;
  589. }
  590. static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
  591. int periodic)
  592. {
  593. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  594. /* deal with old setting first */
  595. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  596. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  597. dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
  598. ch->index, periodic ? "periodic" : "oneshot");
  599. sh_cmt_clock_event_start(ch, periodic);
  600. return 0;
  601. }
  602. static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
  603. {
  604. return sh_cmt_clock_event_set_state(ced, 0);
  605. }
  606. static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
  607. {
  608. return sh_cmt_clock_event_set_state(ced, 1);
  609. }
  610. static int sh_cmt_clock_event_next(unsigned long delta,
  611. struct clock_event_device *ced)
  612. {
  613. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  614. BUG_ON(!clockevent_state_oneshot(ced));
  615. if (likely(ch->flags & FLAG_IRQCONTEXT))
  616. ch->next_match_value = delta - 1;
  617. else
  618. sh_cmt_set_next(ch, delta - 1);
  619. return 0;
  620. }
  621. static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
  622. {
  623. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  624. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  625. clk_unprepare(ch->cmt->clk);
  626. }
  627. static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
  628. {
  629. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  630. clk_prepare(ch->cmt->clk);
  631. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  632. }
  633. static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
  634. const char *name)
  635. {
  636. struct clock_event_device *ced = &ch->ced;
  637. int irq;
  638. int ret;
  639. irq = platform_get_irq(ch->cmt->pdev, ch->index);
  640. if (irq < 0) {
  641. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
  642. ch->index);
  643. return irq;
  644. }
  645. ret = request_irq(irq, sh_cmt_interrupt,
  646. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  647. dev_name(&ch->cmt->pdev->dev), ch);
  648. if (ret) {
  649. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
  650. ch->index, irq);
  651. return ret;
  652. }
  653. ced->name = name;
  654. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  655. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  656. ced->rating = 125;
  657. ced->cpumask = cpu_possible_mask;
  658. ced->set_next_event = sh_cmt_clock_event_next;
  659. ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
  660. ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
  661. ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
  662. ced->suspend = sh_cmt_clock_event_suspend;
  663. ced->resume = sh_cmt_clock_event_resume;
  664. /* TODO: calculate good shift from rate and counter bit width */
  665. ced->shift = 32;
  666. ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
  667. ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
  668. ced->max_delta_ticks = ch->max_match_value;
  669. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  670. ced->min_delta_ticks = 0x1f;
  671. dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
  672. ch->index);
  673. clockevents_register_device(ced);
  674. return 0;
  675. }
  676. static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
  677. bool clockevent, bool clocksource)
  678. {
  679. int ret;
  680. if (clockevent) {
  681. ch->cmt->has_clockevent = true;
  682. ret = sh_cmt_register_clockevent(ch, name);
  683. if (ret < 0)
  684. return ret;
  685. }
  686. if (clocksource) {
  687. ch->cmt->has_clocksource = true;
  688. sh_cmt_register_clocksource(ch, name);
  689. }
  690. return 0;
  691. }
  692. static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
  693. unsigned int hwidx, bool clockevent,
  694. bool clocksource, struct sh_cmt_device *cmt)
  695. {
  696. int ret;
  697. /* Skip unused channels. */
  698. if (!clockevent && !clocksource)
  699. return 0;
  700. ch->cmt = cmt;
  701. ch->index = index;
  702. ch->hwidx = hwidx;
  703. ch->timer_bit = hwidx;
  704. /*
  705. * Compute the address of the channel control register block. For the
  706. * timers with a per-channel start/stop register, compute its address
  707. * as well.
  708. */
  709. switch (cmt->info->model) {
  710. case SH_CMT_16BIT:
  711. ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
  712. break;
  713. case SH_CMT_32BIT:
  714. case SH_CMT_48BIT:
  715. ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
  716. break;
  717. case SH_CMT0_RCAR_GEN2:
  718. case SH_CMT1_RCAR_GEN2:
  719. ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
  720. ch->ioctrl = ch->iostart + 0x10;
  721. ch->timer_bit = 0;
  722. break;
  723. }
  724. if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
  725. ch->max_match_value = ~0;
  726. else
  727. ch->max_match_value = (1 << cmt->info->width) - 1;
  728. ch->match_value = ch->max_match_value;
  729. raw_spin_lock_init(&ch->lock);
  730. ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
  731. clockevent, clocksource);
  732. if (ret) {
  733. dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
  734. ch->index);
  735. return ret;
  736. }
  737. ch->cs_enabled = false;
  738. return 0;
  739. }
  740. static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
  741. {
  742. struct resource *mem;
  743. mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
  744. if (!mem) {
  745. dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
  746. return -ENXIO;
  747. }
  748. cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
  749. if (cmt->mapbase == NULL) {
  750. dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
  751. return -ENXIO;
  752. }
  753. return 0;
  754. }
  755. static const struct platform_device_id sh_cmt_id_table[] = {
  756. { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
  757. { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
  758. { }
  759. };
  760. MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
  761. static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
  762. { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
  763. {
  764. /* deprecated, preserved for backward compatibility */
  765. .compatible = "renesas,cmt-48-gen2",
  766. .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
  767. },
  768. {
  769. .compatible = "renesas,rcar-gen2-cmt0",
  770. .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
  771. },
  772. {
  773. .compatible = "renesas,rcar-gen2-cmt1",
  774. .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2]
  775. },
  776. {
  777. .compatible = "renesas,rcar-gen3-cmt0",
  778. .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
  779. },
  780. {
  781. .compatible = "renesas,rcar-gen3-cmt1",
  782. .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2]
  783. },
  784. { }
  785. };
  786. MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
  787. static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
  788. {
  789. unsigned int mask;
  790. unsigned int i;
  791. int ret;
  792. cmt->pdev = pdev;
  793. raw_spin_lock_init(&cmt->lock);
  794. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  795. cmt->info = of_device_get_match_data(&pdev->dev);
  796. cmt->hw_channels = cmt->info->channels_mask;
  797. } else if (pdev->dev.platform_data) {
  798. struct sh_timer_config *cfg = pdev->dev.platform_data;
  799. const struct platform_device_id *id = pdev->id_entry;
  800. cmt->info = (const struct sh_cmt_info *)id->driver_data;
  801. cmt->hw_channels = cfg->channels_mask;
  802. } else {
  803. dev_err(&cmt->pdev->dev, "missing platform data\n");
  804. return -ENXIO;
  805. }
  806. /* Get hold of clock. */
  807. cmt->clk = clk_get(&cmt->pdev->dev, "fck");
  808. if (IS_ERR(cmt->clk)) {
  809. dev_err(&cmt->pdev->dev, "cannot get clock\n");
  810. return PTR_ERR(cmt->clk);
  811. }
  812. ret = clk_prepare(cmt->clk);
  813. if (ret < 0)
  814. goto err_clk_put;
  815. /* Determine clock rate. */
  816. ret = clk_enable(cmt->clk);
  817. if (ret < 0)
  818. goto err_clk_unprepare;
  819. if (cmt->info->width == 16)
  820. cmt->rate = clk_get_rate(cmt->clk) / 512;
  821. else
  822. cmt->rate = clk_get_rate(cmt->clk) / 8;
  823. clk_disable(cmt->clk);
  824. /* Map the memory resource(s). */
  825. ret = sh_cmt_map_memory(cmt);
  826. if (ret < 0)
  827. goto err_clk_unprepare;
  828. /* Allocate and setup the channels. */
  829. cmt->num_channels = hweight8(cmt->hw_channels);
  830. cmt->channels = kcalloc(cmt->num_channels, sizeof(*cmt->channels),
  831. GFP_KERNEL);
  832. if (cmt->channels == NULL) {
  833. ret = -ENOMEM;
  834. goto err_unmap;
  835. }
  836. /*
  837. * Use the first channel as a clock event device and the second channel
  838. * as a clock source. If only one channel is available use it for both.
  839. */
  840. for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
  841. unsigned int hwidx = ffs(mask) - 1;
  842. bool clocksource = i == 1 || cmt->num_channels == 1;
  843. bool clockevent = i == 0;
  844. ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
  845. clockevent, clocksource, cmt);
  846. if (ret < 0)
  847. goto err_unmap;
  848. mask &= ~(1 << hwidx);
  849. }
  850. platform_set_drvdata(pdev, cmt);
  851. return 0;
  852. err_unmap:
  853. kfree(cmt->channels);
  854. iounmap(cmt->mapbase);
  855. err_clk_unprepare:
  856. clk_unprepare(cmt->clk);
  857. err_clk_put:
  858. clk_put(cmt->clk);
  859. return ret;
  860. }
  861. static int sh_cmt_probe(struct platform_device *pdev)
  862. {
  863. struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
  864. int ret;
  865. if (!is_early_platform_device(pdev)) {
  866. pm_runtime_set_active(&pdev->dev);
  867. pm_runtime_enable(&pdev->dev);
  868. }
  869. if (cmt) {
  870. dev_info(&pdev->dev, "kept as earlytimer\n");
  871. goto out;
  872. }
  873. cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
  874. if (cmt == NULL)
  875. return -ENOMEM;
  876. ret = sh_cmt_setup(cmt, pdev);
  877. if (ret) {
  878. kfree(cmt);
  879. pm_runtime_idle(&pdev->dev);
  880. return ret;
  881. }
  882. if (is_early_platform_device(pdev))
  883. return 0;
  884. out:
  885. if (cmt->has_clockevent || cmt->has_clocksource)
  886. pm_runtime_irq_safe(&pdev->dev);
  887. else
  888. pm_runtime_idle(&pdev->dev);
  889. return 0;
  890. }
  891. static int sh_cmt_remove(struct platform_device *pdev)
  892. {
  893. return -EBUSY; /* cannot unregister clockevent and clocksource */
  894. }
  895. static struct platform_driver sh_cmt_device_driver = {
  896. .probe = sh_cmt_probe,
  897. .remove = sh_cmt_remove,
  898. .driver = {
  899. .name = "sh_cmt",
  900. .of_match_table = of_match_ptr(sh_cmt_of_table),
  901. },
  902. .id_table = sh_cmt_id_table,
  903. };
  904. static int __init sh_cmt_init(void)
  905. {
  906. return platform_driver_register(&sh_cmt_device_driver);
  907. }
  908. static void __exit sh_cmt_exit(void)
  909. {
  910. platform_driver_unregister(&sh_cmt_device_driver);
  911. }
  912. early_platform_init("earlytimer", &sh_cmt_device_driver);
  913. subsys_initcall(sh_cmt_init);
  914. module_exit(sh_cmt_exit);
  915. MODULE_AUTHOR("Magnus Damm");
  916. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  917. MODULE_LICENSE("GPL v2");