sh_cmt.c 28 KB

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