sh_cmt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. unsigned long overflow_bit;
  75. unsigned long clear_bits;
  76. /* callbacks for CMSTR and CMCSR access */
  77. unsigned long (*read_control)(void __iomem *base, unsigned long offs);
  78. void (*write_control)(void __iomem *base, unsigned long offs,
  79. unsigned long value);
  80. /* callbacks for CMCNT and CMCOR access */
  81. unsigned long (*read_count)(void __iomem *base, unsigned long offs);
  82. void (*write_count)(void __iomem *base, unsigned long offs,
  83. unsigned long value);
  84. };
  85. struct sh_cmt_channel {
  86. struct sh_cmt_device *cmt;
  87. unsigned int index; /* Index in the documentation */
  88. unsigned int hwidx; /* Real hardware index */
  89. void __iomem *iostart;
  90. void __iomem *ioctrl;
  91. unsigned int timer_bit;
  92. unsigned long flags;
  93. unsigned long match_value;
  94. unsigned long next_match_value;
  95. unsigned long max_match_value;
  96. raw_spinlock_t lock;
  97. struct clock_event_device ced;
  98. struct clocksource cs;
  99. unsigned long total_cycles;
  100. bool cs_enabled;
  101. };
  102. struct sh_cmt_device {
  103. struct platform_device *pdev;
  104. const struct sh_cmt_info *info;
  105. void __iomem *mapbase;
  106. struct clk *clk;
  107. unsigned long rate;
  108. raw_spinlock_t lock; /* Protect the shared start/stop register */
  109. struct sh_cmt_channel *channels;
  110. unsigned int num_channels;
  111. unsigned int hw_channels;
  112. bool has_clockevent;
  113. bool has_clocksource;
  114. };
  115. #define SH_CMT16_CMCSR_CMF (1 << 7)
  116. #define SH_CMT16_CMCSR_CMIE (1 << 6)
  117. #define SH_CMT16_CMCSR_CKS8 (0 << 0)
  118. #define SH_CMT16_CMCSR_CKS32 (1 << 0)
  119. #define SH_CMT16_CMCSR_CKS128 (2 << 0)
  120. #define SH_CMT16_CMCSR_CKS512 (3 << 0)
  121. #define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
  122. #define SH_CMT32_CMCSR_CMF (1 << 15)
  123. #define SH_CMT32_CMCSR_OVF (1 << 14)
  124. #define SH_CMT32_CMCSR_WRFLG (1 << 13)
  125. #define SH_CMT32_CMCSR_STTF (1 << 12)
  126. #define SH_CMT32_CMCSR_STPF (1 << 11)
  127. #define SH_CMT32_CMCSR_SSIE (1 << 10)
  128. #define SH_CMT32_CMCSR_CMS (1 << 9)
  129. #define SH_CMT32_CMCSR_CMM (1 << 8)
  130. #define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
  131. #define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
  132. #define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
  133. #define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
  134. #define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
  135. #define SH_CMT32_CMCSR_DBGIVD (1 << 3)
  136. #define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
  137. #define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
  138. #define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
  139. #define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
  140. #define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
  141. static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
  142. {
  143. return ioread16(base + (offs << 1));
  144. }
  145. static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
  146. {
  147. return ioread32(base + (offs << 2));
  148. }
  149. static void sh_cmt_write16(void __iomem *base, unsigned long offs,
  150. unsigned long value)
  151. {
  152. iowrite16(value, base + (offs << 1));
  153. }
  154. static void sh_cmt_write32(void __iomem *base, unsigned long offs,
  155. unsigned long value)
  156. {
  157. iowrite32(value, base + (offs << 2));
  158. }
  159. static const struct sh_cmt_info sh_cmt_info[] = {
  160. [SH_CMT_16BIT] = {
  161. .model = SH_CMT_16BIT,
  162. .width = 16,
  163. .overflow_bit = SH_CMT16_CMCSR_CMF,
  164. .clear_bits = ~SH_CMT16_CMCSR_CMF,
  165. .read_control = sh_cmt_read16,
  166. .write_control = sh_cmt_write16,
  167. .read_count = sh_cmt_read16,
  168. .write_count = sh_cmt_write16,
  169. },
  170. [SH_CMT_32BIT] = {
  171. .model = SH_CMT_32BIT,
  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_read16,
  176. .write_control = sh_cmt_write16,
  177. .read_count = sh_cmt_read32,
  178. .write_count = sh_cmt_write32,
  179. },
  180. [SH_CMT_48BIT] = {
  181. .model = SH_CMT_48BIT,
  182. .channels_mask = 0x3f,
  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_CMT0_RCAR_GEN2] = {
  192. .model = SH_CMT0_RCAR_GEN2,
  193. .channels_mask = 0x60,
  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. [SH_CMT1_RCAR_GEN2] = {
  203. .model = SH_CMT1_RCAR_GEN2,
  204. .channels_mask = 0xff,
  205. .width = 32,
  206. .overflow_bit = SH_CMT32_CMCSR_CMF,
  207. .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
  208. .read_control = sh_cmt_read32,
  209. .write_control = sh_cmt_write32,
  210. .read_count = sh_cmt_read32,
  211. .write_count = sh_cmt_write32,
  212. },
  213. };
  214. #define CMCSR 0 /* channel register */
  215. #define CMCNT 1 /* channel register */
  216. #define CMCOR 2 /* channel register */
  217. static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
  218. {
  219. if (ch->iostart)
  220. return ch->cmt->info->read_control(ch->iostart, 0);
  221. else
  222. return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
  223. }
  224. static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
  225. unsigned long value)
  226. {
  227. if (ch->iostart)
  228. ch->cmt->info->write_control(ch->iostart, 0, value);
  229. else
  230. ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
  231. }
  232. static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
  233. {
  234. return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
  235. }
  236. static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
  237. unsigned long value)
  238. {
  239. ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
  240. }
  241. static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
  242. {
  243. return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
  244. }
  245. static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
  246. unsigned long value)
  247. {
  248. ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
  249. }
  250. static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
  251. unsigned long value)
  252. {
  253. ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
  254. }
  255. static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
  256. int *has_wrapped)
  257. {
  258. unsigned long v1, v2, v3;
  259. int o1, o2;
  260. o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
  261. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  262. do {
  263. o2 = o1;
  264. v1 = sh_cmt_read_cmcnt(ch);
  265. v2 = sh_cmt_read_cmcnt(ch);
  266. v3 = sh_cmt_read_cmcnt(ch);
  267. o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
  268. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  269. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  270. *has_wrapped = o1;
  271. return v2;
  272. }
  273. static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
  274. {
  275. unsigned long flags, value;
  276. /* start stop register shared by multiple timer channels */
  277. raw_spin_lock_irqsave(&ch->cmt->lock, flags);
  278. value = sh_cmt_read_cmstr(ch);
  279. if (start)
  280. value |= 1 << ch->timer_bit;
  281. else
  282. value &= ~(1 << ch->timer_bit);
  283. sh_cmt_write_cmstr(ch, value);
  284. raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
  285. }
  286. static int sh_cmt_enable(struct sh_cmt_channel *ch)
  287. {
  288. int k, ret;
  289. pm_runtime_get_sync(&ch->cmt->pdev->dev);
  290. dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
  291. /* enable clock */
  292. ret = clk_enable(ch->cmt->clk);
  293. if (ret) {
  294. dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
  295. ch->index);
  296. goto err0;
  297. }
  298. /* make sure channel is disabled */
  299. sh_cmt_start_stop_ch(ch, 0);
  300. /* configure channel, periodic mode and maximum timeout */
  301. if (ch->cmt->info->width == 16) {
  302. sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
  303. SH_CMT16_CMCSR_CKS512);
  304. } else {
  305. sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
  306. SH_CMT32_CMCSR_CMTOUT_IE |
  307. SH_CMT32_CMCSR_CMR_IRQ |
  308. SH_CMT32_CMCSR_CKS_RCLK8);
  309. }
  310. sh_cmt_write_cmcor(ch, 0xffffffff);
  311. sh_cmt_write_cmcnt(ch, 0);
  312. /*
  313. * According to the sh73a0 user's manual, as CMCNT can be operated
  314. * only by the RCLK (Pseudo 32 KHz), there's one restriction on
  315. * modifying CMCNT register; two RCLK cycles are necessary before
  316. * this register is either read or any modification of the value
  317. * it holds is reflected in the LSI's actual operation.
  318. *
  319. * While at it, we're supposed to clear out the CMCNT as of this
  320. * moment, so make sure it's processed properly here. This will
  321. * take RCLKx2 at maximum.
  322. */
  323. for (k = 0; k < 100; k++) {
  324. if (!sh_cmt_read_cmcnt(ch))
  325. break;
  326. udelay(1);
  327. }
  328. if (sh_cmt_read_cmcnt(ch)) {
  329. dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
  330. ch->index);
  331. ret = -ETIMEDOUT;
  332. goto err1;
  333. }
  334. /* enable channel */
  335. sh_cmt_start_stop_ch(ch, 1);
  336. return 0;
  337. err1:
  338. /* stop clock */
  339. clk_disable(ch->cmt->clk);
  340. err0:
  341. return ret;
  342. }
  343. static void sh_cmt_disable(struct sh_cmt_channel *ch)
  344. {
  345. /* disable channel */
  346. sh_cmt_start_stop_ch(ch, 0);
  347. /* disable interrupts in CMT block */
  348. sh_cmt_write_cmcsr(ch, 0);
  349. /* stop clock */
  350. clk_disable(ch->cmt->clk);
  351. dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
  352. pm_runtime_put(&ch->cmt->pdev->dev);
  353. }
  354. /* private flags */
  355. #define FLAG_CLOCKEVENT (1 << 0)
  356. #define FLAG_CLOCKSOURCE (1 << 1)
  357. #define FLAG_REPROGRAM (1 << 2)
  358. #define FLAG_SKIPEVENT (1 << 3)
  359. #define FLAG_IRQCONTEXT (1 << 4)
  360. static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
  361. int absolute)
  362. {
  363. unsigned long new_match;
  364. unsigned long value = ch->next_match_value;
  365. unsigned long delay = 0;
  366. unsigned long now = 0;
  367. int has_wrapped;
  368. now = sh_cmt_get_counter(ch, &has_wrapped);
  369. ch->flags |= FLAG_REPROGRAM; /* force reprogram */
  370. if (has_wrapped) {
  371. /* we're competing with the interrupt handler.
  372. * -> let the interrupt handler reprogram the timer.
  373. * -> interrupt number two handles the event.
  374. */
  375. ch->flags |= FLAG_SKIPEVENT;
  376. return;
  377. }
  378. if (absolute)
  379. now = 0;
  380. do {
  381. /* reprogram the timer hardware,
  382. * but don't save the new match value yet.
  383. */
  384. new_match = now + value + delay;
  385. if (new_match > ch->max_match_value)
  386. new_match = ch->max_match_value;
  387. sh_cmt_write_cmcor(ch, new_match);
  388. now = sh_cmt_get_counter(ch, &has_wrapped);
  389. if (has_wrapped && (new_match > ch->match_value)) {
  390. /* we are changing to a greater match value,
  391. * so this wrap must be caused by the counter
  392. * matching the old value.
  393. * -> first interrupt reprograms the timer.
  394. * -> interrupt number two handles the event.
  395. */
  396. ch->flags |= FLAG_SKIPEVENT;
  397. break;
  398. }
  399. if (has_wrapped) {
  400. /* we are changing to a smaller match value,
  401. * so the wrap must be caused by the counter
  402. * matching the new value.
  403. * -> save programmed match value.
  404. * -> let isr handle the event.
  405. */
  406. ch->match_value = new_match;
  407. break;
  408. }
  409. /* be safe: verify hardware settings */
  410. if (now < new_match) {
  411. /* timer value is below match value, all good.
  412. * this makes sure we won't miss any match events.
  413. * -> save programmed match value.
  414. * -> let isr handle the event.
  415. */
  416. ch->match_value = new_match;
  417. break;
  418. }
  419. /* the counter has reached a value greater
  420. * than our new match value. and since the
  421. * has_wrapped flag isn't set we must have
  422. * programmed a too close event.
  423. * -> increase delay and retry.
  424. */
  425. if (delay)
  426. delay <<= 1;
  427. else
  428. delay = 1;
  429. if (!delay)
  430. dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
  431. ch->index);
  432. } while (delay);
  433. }
  434. static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  435. {
  436. if (delta > ch->max_match_value)
  437. dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
  438. ch->index);
  439. ch->next_match_value = delta;
  440. sh_cmt_clock_event_program_verify(ch, 0);
  441. }
  442. static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  443. {
  444. unsigned long flags;
  445. raw_spin_lock_irqsave(&ch->lock, flags);
  446. __sh_cmt_set_next(ch, delta);
  447. raw_spin_unlock_irqrestore(&ch->lock, flags);
  448. }
  449. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  450. {
  451. struct sh_cmt_channel *ch = dev_id;
  452. /* clear flags */
  453. sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
  454. ch->cmt->info->clear_bits);
  455. /* update clock source counter to begin with if enabled
  456. * the wrap flag should be cleared by the timer specific
  457. * isr before we end up here.
  458. */
  459. if (ch->flags & FLAG_CLOCKSOURCE)
  460. ch->total_cycles += ch->match_value + 1;
  461. if (!(ch->flags & FLAG_REPROGRAM))
  462. ch->next_match_value = ch->max_match_value;
  463. ch->flags |= FLAG_IRQCONTEXT;
  464. if (ch->flags & FLAG_CLOCKEVENT) {
  465. if (!(ch->flags & FLAG_SKIPEVENT)) {
  466. if (clockevent_state_oneshot(&ch->ced)) {
  467. ch->next_match_value = ch->max_match_value;
  468. ch->flags |= FLAG_REPROGRAM;
  469. }
  470. ch->ced.event_handler(&ch->ced);
  471. }
  472. }
  473. ch->flags &= ~FLAG_SKIPEVENT;
  474. if (ch->flags & FLAG_REPROGRAM) {
  475. ch->flags &= ~FLAG_REPROGRAM;
  476. sh_cmt_clock_event_program_verify(ch, 1);
  477. if (ch->flags & FLAG_CLOCKEVENT)
  478. if ((clockevent_state_shutdown(&ch->ced))
  479. || (ch->match_value == ch->next_match_value))
  480. ch->flags &= ~FLAG_REPROGRAM;
  481. }
  482. ch->flags &= ~FLAG_IRQCONTEXT;
  483. return IRQ_HANDLED;
  484. }
  485. static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
  486. {
  487. int ret = 0;
  488. unsigned long flags;
  489. raw_spin_lock_irqsave(&ch->lock, flags);
  490. if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  491. ret = sh_cmt_enable(ch);
  492. if (ret)
  493. goto out;
  494. ch->flags |= flag;
  495. /* setup timeout if no clockevent */
  496. if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
  497. __sh_cmt_set_next(ch, ch->max_match_value);
  498. out:
  499. raw_spin_unlock_irqrestore(&ch->lock, flags);
  500. return ret;
  501. }
  502. static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
  503. {
  504. unsigned long flags;
  505. unsigned long f;
  506. raw_spin_lock_irqsave(&ch->lock, flags);
  507. f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  508. ch->flags &= ~flag;
  509. if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  510. sh_cmt_disable(ch);
  511. /* adjust the timeout to maximum if only clocksource left */
  512. if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
  513. __sh_cmt_set_next(ch, ch->max_match_value);
  514. raw_spin_unlock_irqrestore(&ch->lock, flags);
  515. }
  516. static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
  517. {
  518. return container_of(cs, struct sh_cmt_channel, cs);
  519. }
  520. static u64 sh_cmt_clocksource_read(struct clocksource *cs)
  521. {
  522. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  523. unsigned long flags, raw;
  524. unsigned long value;
  525. int has_wrapped;
  526. raw_spin_lock_irqsave(&ch->lock, flags);
  527. value = ch->total_cycles;
  528. raw = sh_cmt_get_counter(ch, &has_wrapped);
  529. if (unlikely(has_wrapped))
  530. raw += ch->match_value + 1;
  531. raw_spin_unlock_irqrestore(&ch->lock, flags);
  532. return value + raw;
  533. }
  534. static int sh_cmt_clocksource_enable(struct clocksource *cs)
  535. {
  536. int ret;
  537. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  538. WARN_ON(ch->cs_enabled);
  539. ch->total_cycles = 0;
  540. ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  541. if (!ret)
  542. ch->cs_enabled = true;
  543. return ret;
  544. }
  545. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  546. {
  547. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  548. WARN_ON(!ch->cs_enabled);
  549. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  550. ch->cs_enabled = false;
  551. }
  552. static void sh_cmt_clocksource_suspend(struct clocksource *cs)
  553. {
  554. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  555. if (!ch->cs_enabled)
  556. return;
  557. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  558. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  559. }
  560. static void sh_cmt_clocksource_resume(struct clocksource *cs)
  561. {
  562. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  563. if (!ch->cs_enabled)
  564. return;
  565. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  566. sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  567. }
  568. static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
  569. const char *name)
  570. {
  571. struct clocksource *cs = &ch->cs;
  572. cs->name = name;
  573. cs->rating = 125;
  574. cs->read = sh_cmt_clocksource_read;
  575. cs->enable = sh_cmt_clocksource_enable;
  576. cs->disable = sh_cmt_clocksource_disable;
  577. cs->suspend = sh_cmt_clocksource_suspend;
  578. cs->resume = sh_cmt_clocksource_resume;
  579. cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  580. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  581. dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
  582. ch->index);
  583. clocksource_register_hz(cs, ch->cmt->rate);
  584. return 0;
  585. }
  586. static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
  587. {
  588. return container_of(ced, struct sh_cmt_channel, ced);
  589. }
  590. static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
  591. {
  592. sh_cmt_start(ch, FLAG_CLOCKEVENT);
  593. if (periodic)
  594. sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
  595. else
  596. sh_cmt_set_next(ch, ch->max_match_value);
  597. }
  598. static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
  599. {
  600. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  601. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  602. return 0;
  603. }
  604. static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
  605. int periodic)
  606. {
  607. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  608. /* deal with old setting first */
  609. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  610. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  611. dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
  612. ch->index, periodic ? "periodic" : "oneshot");
  613. sh_cmt_clock_event_start(ch, periodic);
  614. return 0;
  615. }
  616. static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
  617. {
  618. return sh_cmt_clock_event_set_state(ced, 0);
  619. }
  620. static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
  621. {
  622. return sh_cmt_clock_event_set_state(ced, 1);
  623. }
  624. static int sh_cmt_clock_event_next(unsigned long delta,
  625. struct clock_event_device *ced)
  626. {
  627. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  628. BUG_ON(!clockevent_state_oneshot(ced));
  629. if (likely(ch->flags & FLAG_IRQCONTEXT))
  630. ch->next_match_value = delta - 1;
  631. else
  632. sh_cmt_set_next(ch, delta - 1);
  633. return 0;
  634. }
  635. static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
  636. {
  637. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  638. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  639. clk_unprepare(ch->cmt->clk);
  640. }
  641. static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
  642. {
  643. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  644. clk_prepare(ch->cmt->clk);
  645. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  646. }
  647. static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
  648. const char *name)
  649. {
  650. struct clock_event_device *ced = &ch->ced;
  651. int irq;
  652. int ret;
  653. irq = platform_get_irq(ch->cmt->pdev, ch->index);
  654. if (irq < 0) {
  655. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
  656. ch->index);
  657. return irq;
  658. }
  659. ret = request_irq(irq, sh_cmt_interrupt,
  660. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  661. dev_name(&ch->cmt->pdev->dev), ch);
  662. if (ret) {
  663. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
  664. ch->index, irq);
  665. return ret;
  666. }
  667. ced->name = name;
  668. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  669. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  670. ced->rating = 125;
  671. ced->cpumask = cpu_possible_mask;
  672. ced->set_next_event = sh_cmt_clock_event_next;
  673. ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
  674. ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
  675. ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
  676. ced->suspend = sh_cmt_clock_event_suspend;
  677. ced->resume = sh_cmt_clock_event_resume;
  678. /* TODO: calculate good shift from rate and counter bit width */
  679. ced->shift = 32;
  680. ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
  681. ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
  682. ced->max_delta_ticks = ch->max_match_value;
  683. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  684. ced->min_delta_ticks = 0x1f;
  685. dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
  686. ch->index);
  687. clockevents_register_device(ced);
  688. return 0;
  689. }
  690. static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
  691. bool clockevent, bool clocksource)
  692. {
  693. int ret;
  694. if (clockevent) {
  695. ch->cmt->has_clockevent = true;
  696. ret = sh_cmt_register_clockevent(ch, name);
  697. if (ret < 0)
  698. return ret;
  699. }
  700. if (clocksource) {
  701. ch->cmt->has_clocksource = true;
  702. sh_cmt_register_clocksource(ch, name);
  703. }
  704. return 0;
  705. }
  706. static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
  707. unsigned int hwidx, bool clockevent,
  708. bool clocksource, struct sh_cmt_device *cmt)
  709. {
  710. int ret;
  711. /* Skip unused channels. */
  712. if (!clockevent && !clocksource)
  713. return 0;
  714. ch->cmt = cmt;
  715. ch->index = index;
  716. ch->hwidx = hwidx;
  717. ch->timer_bit = hwidx;
  718. /*
  719. * Compute the address of the channel control register block. For the
  720. * timers with a per-channel start/stop register, compute its address
  721. * as well.
  722. */
  723. switch (cmt->info->model) {
  724. case SH_CMT_16BIT:
  725. ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
  726. break;
  727. case SH_CMT_32BIT:
  728. case SH_CMT_48BIT:
  729. ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
  730. break;
  731. case SH_CMT0_RCAR_GEN2:
  732. case SH_CMT1_RCAR_GEN2:
  733. ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
  734. ch->ioctrl = ch->iostart + 0x10;
  735. ch->timer_bit = 0;
  736. break;
  737. }
  738. if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
  739. ch->max_match_value = ~0;
  740. else
  741. ch->max_match_value = (1 << cmt->info->width) - 1;
  742. ch->match_value = ch->max_match_value;
  743. raw_spin_lock_init(&ch->lock);
  744. ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
  745. clockevent, clocksource);
  746. if (ret) {
  747. dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
  748. ch->index);
  749. return ret;
  750. }
  751. ch->cs_enabled = false;
  752. return 0;
  753. }
  754. static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
  755. {
  756. struct resource *mem;
  757. mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
  758. if (!mem) {
  759. dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
  760. return -ENXIO;
  761. }
  762. cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
  763. if (cmt->mapbase == NULL) {
  764. dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
  765. return -ENXIO;
  766. }
  767. return 0;
  768. }
  769. static const struct platform_device_id sh_cmt_id_table[] = {
  770. { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
  771. { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
  772. { }
  773. };
  774. MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
  775. static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
  776. { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
  777. {
  778. /* deprecated, preserved for backward compatibility */
  779. .compatible = "renesas,cmt-48-gen2",
  780. .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
  781. },
  782. { .compatible = "renesas,rcar-gen2-cmt0", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
  783. { .compatible = "renesas,rcar-gen2-cmt1", .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2] },
  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");