sh_cmt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. raw_spinlock_t lock;
  95. struct clock_event_device ced;
  96. struct clocksource cs;
  97. unsigned long total_cycles;
  98. bool cs_enabled;
  99. };
  100. struct sh_cmt_device {
  101. struct platform_device *pdev;
  102. const struct sh_cmt_info *info;
  103. void __iomem *mapbase;
  104. struct clk *clk;
  105. unsigned long rate;
  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)
  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. sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
  298. SH_CMT16_CMCSR_CKS512);
  299. } else {
  300. sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
  301. SH_CMT32_CMCSR_CMTOUT_IE |
  302. SH_CMT32_CMCSR_CMR_IRQ |
  303. SH_CMT32_CMCSR_CKS_RCLK8);
  304. }
  305. sh_cmt_write_cmcor(ch, 0xffffffff);
  306. sh_cmt_write_cmcnt(ch, 0);
  307. /*
  308. * According to the sh73a0 user's manual, as CMCNT can be operated
  309. * only by the RCLK (Pseudo 32 KHz), there's one restriction on
  310. * modifying CMCNT register; two RCLK cycles are necessary before
  311. * this register is either read or any modification of the value
  312. * it holds is reflected in the LSI's actual operation.
  313. *
  314. * While at it, we're supposed to clear out the CMCNT as of this
  315. * moment, so make sure it's processed properly here. This will
  316. * take RCLKx2 at maximum.
  317. */
  318. for (k = 0; k < 100; k++) {
  319. if (!sh_cmt_read_cmcnt(ch))
  320. break;
  321. udelay(1);
  322. }
  323. if (sh_cmt_read_cmcnt(ch)) {
  324. dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
  325. ch->index);
  326. ret = -ETIMEDOUT;
  327. goto err1;
  328. }
  329. /* enable channel */
  330. sh_cmt_start_stop_ch(ch, 1);
  331. return 0;
  332. err1:
  333. /* stop clock */
  334. clk_disable(ch->cmt->clk);
  335. err0:
  336. return ret;
  337. }
  338. static void sh_cmt_disable(struct sh_cmt_channel *ch)
  339. {
  340. /* disable channel */
  341. sh_cmt_start_stop_ch(ch, 0);
  342. /* disable interrupts in CMT block */
  343. sh_cmt_write_cmcsr(ch, 0);
  344. /* stop clock */
  345. clk_disable(ch->cmt->clk);
  346. dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
  347. pm_runtime_put(&ch->cmt->pdev->dev);
  348. }
  349. /* private flags */
  350. #define FLAG_CLOCKEVENT (1 << 0)
  351. #define FLAG_CLOCKSOURCE (1 << 1)
  352. #define FLAG_REPROGRAM (1 << 2)
  353. #define FLAG_SKIPEVENT (1 << 3)
  354. #define FLAG_IRQCONTEXT (1 << 4)
  355. static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
  356. int absolute)
  357. {
  358. unsigned long new_match;
  359. unsigned long value = ch->next_match_value;
  360. unsigned long delay = 0;
  361. unsigned long now = 0;
  362. int has_wrapped;
  363. now = sh_cmt_get_counter(ch, &has_wrapped);
  364. ch->flags |= FLAG_REPROGRAM; /* force reprogram */
  365. if (has_wrapped) {
  366. /* we're competing with the interrupt handler.
  367. * -> let the interrupt handler reprogram the timer.
  368. * -> interrupt number two handles the event.
  369. */
  370. ch->flags |= FLAG_SKIPEVENT;
  371. return;
  372. }
  373. if (absolute)
  374. now = 0;
  375. do {
  376. /* reprogram the timer hardware,
  377. * but don't save the new match value yet.
  378. */
  379. new_match = now + value + delay;
  380. if (new_match > ch->max_match_value)
  381. new_match = ch->max_match_value;
  382. sh_cmt_write_cmcor(ch, new_match);
  383. now = sh_cmt_get_counter(ch, &has_wrapped);
  384. if (has_wrapped && (new_match > ch->match_value)) {
  385. /* we are changing to a greater match value,
  386. * so this wrap must be caused by the counter
  387. * matching the old value.
  388. * -> first interrupt reprograms the timer.
  389. * -> interrupt number two handles the event.
  390. */
  391. ch->flags |= FLAG_SKIPEVENT;
  392. break;
  393. }
  394. if (has_wrapped) {
  395. /* we are changing to a smaller match value,
  396. * so the wrap must be caused by the counter
  397. * matching the new value.
  398. * -> save programmed match value.
  399. * -> let isr handle the event.
  400. */
  401. ch->match_value = new_match;
  402. break;
  403. }
  404. /* be safe: verify hardware settings */
  405. if (now < new_match) {
  406. /* timer value is below match value, all good.
  407. * this makes sure we won't miss any match events.
  408. * -> save programmed match value.
  409. * -> let isr handle the event.
  410. */
  411. ch->match_value = new_match;
  412. break;
  413. }
  414. /* the counter has reached a value greater
  415. * than our new match value. and since the
  416. * has_wrapped flag isn't set we must have
  417. * programmed a too close event.
  418. * -> increase delay and retry.
  419. */
  420. if (delay)
  421. delay <<= 1;
  422. else
  423. delay = 1;
  424. if (!delay)
  425. dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
  426. ch->index);
  427. } while (delay);
  428. }
  429. static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  430. {
  431. if (delta > ch->max_match_value)
  432. dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
  433. ch->index);
  434. ch->next_match_value = delta;
  435. sh_cmt_clock_event_program_verify(ch, 0);
  436. }
  437. static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
  438. {
  439. unsigned long flags;
  440. raw_spin_lock_irqsave(&ch->lock, flags);
  441. __sh_cmt_set_next(ch, delta);
  442. raw_spin_unlock_irqrestore(&ch->lock, flags);
  443. }
  444. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  445. {
  446. struct sh_cmt_channel *ch = dev_id;
  447. /* clear flags */
  448. sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
  449. ch->cmt->info->clear_bits);
  450. /* update clock source counter to begin with if enabled
  451. * the wrap flag should be cleared by the timer specific
  452. * isr before we end up here.
  453. */
  454. if (ch->flags & FLAG_CLOCKSOURCE)
  455. ch->total_cycles += ch->match_value + 1;
  456. if (!(ch->flags & FLAG_REPROGRAM))
  457. ch->next_match_value = ch->max_match_value;
  458. ch->flags |= FLAG_IRQCONTEXT;
  459. if (ch->flags & FLAG_CLOCKEVENT) {
  460. if (!(ch->flags & FLAG_SKIPEVENT)) {
  461. if (clockevent_state_oneshot(&ch->ced)) {
  462. ch->next_match_value = ch->max_match_value;
  463. ch->flags |= FLAG_REPROGRAM;
  464. }
  465. ch->ced.event_handler(&ch->ced);
  466. }
  467. }
  468. ch->flags &= ~FLAG_SKIPEVENT;
  469. if (ch->flags & FLAG_REPROGRAM) {
  470. ch->flags &= ~FLAG_REPROGRAM;
  471. sh_cmt_clock_event_program_verify(ch, 1);
  472. if (ch->flags & FLAG_CLOCKEVENT)
  473. if ((clockevent_state_shutdown(&ch->ced))
  474. || (ch->match_value == ch->next_match_value))
  475. ch->flags &= ~FLAG_REPROGRAM;
  476. }
  477. ch->flags &= ~FLAG_IRQCONTEXT;
  478. return IRQ_HANDLED;
  479. }
  480. static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
  481. {
  482. int ret = 0;
  483. unsigned long flags;
  484. raw_spin_lock_irqsave(&ch->lock, flags);
  485. if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  486. ret = sh_cmt_enable(ch);
  487. if (ret)
  488. goto out;
  489. ch->flags |= flag;
  490. /* setup timeout if no clockevent */
  491. if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
  492. __sh_cmt_set_next(ch, ch->max_match_value);
  493. out:
  494. raw_spin_unlock_irqrestore(&ch->lock, flags);
  495. return ret;
  496. }
  497. static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
  498. {
  499. unsigned long flags;
  500. unsigned long f;
  501. raw_spin_lock_irqsave(&ch->lock, flags);
  502. f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  503. ch->flags &= ~flag;
  504. if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  505. sh_cmt_disable(ch);
  506. /* adjust the timeout to maximum if only clocksource left */
  507. if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
  508. __sh_cmt_set_next(ch, ch->max_match_value);
  509. raw_spin_unlock_irqrestore(&ch->lock, flags);
  510. }
  511. static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
  512. {
  513. return container_of(cs, struct sh_cmt_channel, cs);
  514. }
  515. static u64 sh_cmt_clocksource_read(struct clocksource *cs)
  516. {
  517. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  518. unsigned long flags, raw;
  519. unsigned long value;
  520. int has_wrapped;
  521. raw_spin_lock_irqsave(&ch->lock, flags);
  522. value = ch->total_cycles;
  523. raw = sh_cmt_get_counter(ch, &has_wrapped);
  524. if (unlikely(has_wrapped))
  525. raw += ch->match_value + 1;
  526. raw_spin_unlock_irqrestore(&ch->lock, flags);
  527. return value + raw;
  528. }
  529. static int sh_cmt_clocksource_enable(struct clocksource *cs)
  530. {
  531. int ret;
  532. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  533. WARN_ON(ch->cs_enabled);
  534. ch->total_cycles = 0;
  535. ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  536. if (!ret)
  537. ch->cs_enabled = true;
  538. return ret;
  539. }
  540. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  541. {
  542. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  543. WARN_ON(!ch->cs_enabled);
  544. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  545. ch->cs_enabled = false;
  546. }
  547. static void sh_cmt_clocksource_suspend(struct clocksource *cs)
  548. {
  549. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  550. if (!ch->cs_enabled)
  551. return;
  552. sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
  553. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  554. }
  555. static void sh_cmt_clocksource_resume(struct clocksource *cs)
  556. {
  557. struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
  558. if (!ch->cs_enabled)
  559. return;
  560. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  561. sh_cmt_start(ch, FLAG_CLOCKSOURCE);
  562. }
  563. static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
  564. const char *name)
  565. {
  566. struct clocksource *cs = &ch->cs;
  567. cs->name = name;
  568. cs->rating = 125;
  569. cs->read = sh_cmt_clocksource_read;
  570. cs->enable = sh_cmt_clocksource_enable;
  571. cs->disable = sh_cmt_clocksource_disable;
  572. cs->suspend = sh_cmt_clocksource_suspend;
  573. cs->resume = sh_cmt_clocksource_resume;
  574. cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  575. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  576. dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
  577. ch->index);
  578. clocksource_register_hz(cs, ch->cmt->rate);
  579. return 0;
  580. }
  581. static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
  582. {
  583. return container_of(ced, struct sh_cmt_channel, ced);
  584. }
  585. static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
  586. {
  587. sh_cmt_start(ch, FLAG_CLOCKEVENT);
  588. if (periodic)
  589. sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
  590. else
  591. sh_cmt_set_next(ch, ch->max_match_value);
  592. }
  593. static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
  594. {
  595. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  596. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  597. return 0;
  598. }
  599. static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
  600. int periodic)
  601. {
  602. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  603. /* deal with old setting first */
  604. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  605. sh_cmt_stop(ch, FLAG_CLOCKEVENT);
  606. dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
  607. ch->index, periodic ? "periodic" : "oneshot");
  608. sh_cmt_clock_event_start(ch, periodic);
  609. return 0;
  610. }
  611. static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
  612. {
  613. return sh_cmt_clock_event_set_state(ced, 0);
  614. }
  615. static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
  616. {
  617. return sh_cmt_clock_event_set_state(ced, 1);
  618. }
  619. static int sh_cmt_clock_event_next(unsigned long delta,
  620. struct clock_event_device *ced)
  621. {
  622. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  623. BUG_ON(!clockevent_state_oneshot(ced));
  624. if (likely(ch->flags & FLAG_IRQCONTEXT))
  625. ch->next_match_value = delta - 1;
  626. else
  627. sh_cmt_set_next(ch, delta - 1);
  628. return 0;
  629. }
  630. static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
  631. {
  632. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  633. pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
  634. clk_unprepare(ch->cmt->clk);
  635. }
  636. static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
  637. {
  638. struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
  639. clk_prepare(ch->cmt->clk);
  640. pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
  641. }
  642. static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
  643. const char *name)
  644. {
  645. struct clock_event_device *ced = &ch->ced;
  646. int irq;
  647. int ret;
  648. irq = platform_get_irq(ch->cmt->pdev, ch->index);
  649. if (irq < 0) {
  650. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
  651. ch->index);
  652. return irq;
  653. }
  654. ret = request_irq(irq, sh_cmt_interrupt,
  655. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  656. dev_name(&ch->cmt->pdev->dev), ch);
  657. if (ret) {
  658. dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
  659. ch->index, irq);
  660. return ret;
  661. }
  662. ced->name = name;
  663. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  664. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  665. ced->rating = 125;
  666. ced->cpumask = cpu_possible_mask;
  667. ced->set_next_event = sh_cmt_clock_event_next;
  668. ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
  669. ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
  670. ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
  671. ced->suspend = sh_cmt_clock_event_suspend;
  672. ced->resume = sh_cmt_clock_event_resume;
  673. /* TODO: calculate good shift from rate and counter bit width */
  674. ced->shift = 32;
  675. ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
  676. ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
  677. ced->max_delta_ticks = ch->max_match_value;
  678. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  679. ced->min_delta_ticks = 0x1f;
  680. dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
  681. ch->index);
  682. clockevents_register_device(ced);
  683. return 0;
  684. }
  685. static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
  686. bool clockevent, bool clocksource)
  687. {
  688. int ret;
  689. if (clockevent) {
  690. ch->cmt->has_clockevent = true;
  691. ret = sh_cmt_register_clockevent(ch, name);
  692. if (ret < 0)
  693. return ret;
  694. }
  695. if (clocksource) {
  696. ch->cmt->has_clocksource = true;
  697. sh_cmt_register_clocksource(ch, name);
  698. }
  699. return 0;
  700. }
  701. static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
  702. unsigned int hwidx, bool clockevent,
  703. bool clocksource, struct sh_cmt_device *cmt)
  704. {
  705. int ret;
  706. /* Skip unused channels. */
  707. if (!clockevent && !clocksource)
  708. return 0;
  709. ch->cmt = cmt;
  710. ch->index = index;
  711. ch->hwidx = 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_CMT_32BIT_FAST:
  726. /*
  727. * The 32-bit "fast" timer has a single channel at hwidx 5 but
  728. * is located at offset 0x40 instead of 0x60 for some reason.
  729. */
  730. ch->ioctrl = cmt->mapbase + 0x40;
  731. break;
  732. case SH_CMT_48BIT_GEN2:
  733. ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
  734. ch->ioctrl = ch->iostart + 0x10;
  735. break;
  736. }
  737. if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
  738. ch->max_match_value = ~0;
  739. else
  740. ch->max_match_value = (1 << cmt->info->width) - 1;
  741. ch->match_value = ch->max_match_value;
  742. raw_spin_lock_init(&ch->lock);
  743. ch->timer_bit = cmt->info->model == SH_CMT_48BIT_GEN2 ? 0 : ch->hwidx;
  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-32", .data = &sh_cmt_info[SH_CMT_32BIT] },
  777. { .compatible = "renesas,cmt-32-fast", .data = &sh_cmt_info[SH_CMT_32BIT_FAST] },
  778. { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
  779. { .compatible = "renesas,cmt-48-gen2", .data = &sh_cmt_info[SH_CMT_48BIT_GEN2] },
  780. { }
  781. };
  782. MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
  783. static int sh_cmt_parse_dt(struct sh_cmt_device *cmt)
  784. {
  785. struct device_node *np = cmt->pdev->dev.of_node;
  786. return of_property_read_u32(np, "renesas,channels-mask",
  787. &cmt->hw_channels);
  788. }
  789. static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
  790. {
  791. unsigned int mask;
  792. unsigned int i;
  793. int ret;
  794. cmt->pdev = pdev;
  795. raw_spin_lock_init(&cmt->lock);
  796. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  797. const struct of_device_id *id;
  798. id = of_match_node(sh_cmt_of_table, pdev->dev.of_node);
  799. cmt->info = id->data;
  800. ret = sh_cmt_parse_dt(cmt);
  801. if (ret < 0)
  802. return ret;
  803. } else if (pdev->dev.platform_data) {
  804. struct sh_timer_config *cfg = pdev->dev.platform_data;
  805. const struct platform_device_id *id = pdev->id_entry;
  806. cmt->info = (const struct sh_cmt_info *)id->driver_data;
  807. cmt->hw_channels = cfg->channels_mask;
  808. } else {
  809. dev_err(&cmt->pdev->dev, "missing platform data\n");
  810. return -ENXIO;
  811. }
  812. /* Get hold of clock. */
  813. cmt->clk = clk_get(&cmt->pdev->dev, "fck");
  814. if (IS_ERR(cmt->clk)) {
  815. dev_err(&cmt->pdev->dev, "cannot get clock\n");
  816. return PTR_ERR(cmt->clk);
  817. }
  818. ret = clk_prepare(cmt->clk);
  819. if (ret < 0)
  820. goto err_clk_put;
  821. /* Determine clock rate. */
  822. ret = clk_enable(cmt->clk);
  823. if (ret < 0)
  824. goto err_clk_unprepare;
  825. if (cmt->info->width == 16)
  826. cmt->rate = clk_get_rate(cmt->clk) / 512;
  827. else
  828. cmt->rate = clk_get_rate(cmt->clk) / 8;
  829. clk_disable(cmt->clk);
  830. /* Map the memory resource(s). */
  831. ret = sh_cmt_map_memory(cmt);
  832. if (ret < 0)
  833. goto err_clk_unprepare;
  834. /* Allocate and setup the channels. */
  835. cmt->num_channels = hweight8(cmt->hw_channels);
  836. cmt->channels = kzalloc(cmt->num_channels * sizeof(*cmt->channels),
  837. GFP_KERNEL);
  838. if (cmt->channels == NULL) {
  839. ret = -ENOMEM;
  840. goto err_unmap;
  841. }
  842. /*
  843. * Use the first channel as a clock event device and the second channel
  844. * as a clock source. If only one channel is available use it for both.
  845. */
  846. for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
  847. unsigned int hwidx = ffs(mask) - 1;
  848. bool clocksource = i == 1 || cmt->num_channels == 1;
  849. bool clockevent = i == 0;
  850. ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
  851. clockevent, clocksource, cmt);
  852. if (ret < 0)
  853. goto err_unmap;
  854. mask &= ~(1 << hwidx);
  855. }
  856. platform_set_drvdata(pdev, cmt);
  857. return 0;
  858. err_unmap:
  859. kfree(cmt->channels);
  860. iounmap(cmt->mapbase);
  861. err_clk_unprepare:
  862. clk_unprepare(cmt->clk);
  863. err_clk_put:
  864. clk_put(cmt->clk);
  865. return ret;
  866. }
  867. static int sh_cmt_probe(struct platform_device *pdev)
  868. {
  869. struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
  870. int ret;
  871. if (!is_early_platform_device(pdev)) {
  872. pm_runtime_set_active(&pdev->dev);
  873. pm_runtime_enable(&pdev->dev);
  874. }
  875. if (cmt) {
  876. dev_info(&pdev->dev, "kept as earlytimer\n");
  877. goto out;
  878. }
  879. cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
  880. if (cmt == NULL)
  881. return -ENOMEM;
  882. ret = sh_cmt_setup(cmt, pdev);
  883. if (ret) {
  884. kfree(cmt);
  885. pm_runtime_idle(&pdev->dev);
  886. return ret;
  887. }
  888. if (is_early_platform_device(pdev))
  889. return 0;
  890. out:
  891. if (cmt->has_clockevent || cmt->has_clocksource)
  892. pm_runtime_irq_safe(&pdev->dev);
  893. else
  894. pm_runtime_idle(&pdev->dev);
  895. return 0;
  896. }
  897. static int sh_cmt_remove(struct platform_device *pdev)
  898. {
  899. return -EBUSY; /* cannot unregister clockevent and clocksource */
  900. }
  901. static struct platform_driver sh_cmt_device_driver = {
  902. .probe = sh_cmt_probe,
  903. .remove = sh_cmt_remove,
  904. .driver = {
  905. .name = "sh_cmt",
  906. .of_match_table = of_match_ptr(sh_cmt_of_table),
  907. },
  908. .id_table = sh_cmt_id_table,
  909. };
  910. static int __init sh_cmt_init(void)
  911. {
  912. return platform_driver_register(&sh_cmt_device_driver);
  913. }
  914. static void __exit sh_cmt_exit(void)
  915. {
  916. platform_driver_unregister(&sh_cmt_device_driver);
  917. }
  918. early_platform_init("earlytimer", &sh_cmt_device_driver);
  919. subsys_initcall(sh_cmt_init);
  920. module_exit(sh_cmt_exit);
  921. MODULE_AUTHOR("Magnus Damm");
  922. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  923. MODULE_LICENSE("GPL v2");