sh_cmt.c 28 KB

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