tcb_clksrc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/clocksource.h>
  4. #include <linux/clockchips.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/irq.h>
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/ioport.h>
  10. #include <linux/io.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/syscore_ops.h>
  13. #include <linux/atmel_tc.h>
  14. /*
  15. * We're configured to use a specific TC block, one that's not hooked
  16. * up to external hardware, to provide a time solution:
  17. *
  18. * - Two channels combine to create a free-running 32 bit counter
  19. * with a base rate of 5+ MHz, packaged as a clocksource (with
  20. * resolution better than 200 nsec).
  21. * - Some chips support 32 bit counter. A single channel is used for
  22. * this 32 bit free-running counter. the second channel is not used.
  23. *
  24. * - The third channel may be used to provide a 16-bit clockevent
  25. * source, used in either periodic or oneshot mode. This runs
  26. * at 32 KiHZ, and can handle delays of up to two seconds.
  27. *
  28. * A boot clocksource and clockevent source are also currently needed,
  29. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  30. * this code can be used when init_timers() is called, well before most
  31. * devices are set up. (Some low end AT91 parts, which can run uClinux,
  32. * have only the timers in one TC block... they currently don't support
  33. * the tclib code, because of that initialization issue.)
  34. *
  35. * REVISIT behavior during system suspend states... we should disable
  36. * all clocks and save the power. Easily done for clockevent devices,
  37. * but clocksources won't necessarily get the needed notifications.
  38. * For deeper system sleep states, this will be mandatory...
  39. */
  40. static void __iomem *tcaddr;
  41. static struct
  42. {
  43. u32 cmr;
  44. u32 imr;
  45. u32 rc;
  46. bool clken;
  47. } tcb_cache[3];
  48. static u32 bmr_cache;
  49. static u64 tc_get_cycles(struct clocksource *cs)
  50. {
  51. unsigned long flags;
  52. u32 lower, upper;
  53. raw_local_irq_save(flags);
  54. do {
  55. upper = readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV));
  56. lower = readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV));
  57. } while (upper != readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV)));
  58. raw_local_irq_restore(flags);
  59. return (upper << 16) | lower;
  60. }
  61. static u64 tc_get_cycles32(struct clocksource *cs)
  62. {
  63. return readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV));
  64. }
  65. void tc_clksrc_suspend(struct clocksource *cs)
  66. {
  67. int i;
  68. for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) {
  69. tcb_cache[i].cmr = readl(tcaddr + ATMEL_TC_REG(i, CMR));
  70. tcb_cache[i].imr = readl(tcaddr + ATMEL_TC_REG(i, IMR));
  71. tcb_cache[i].rc = readl(tcaddr + ATMEL_TC_REG(i, RC));
  72. tcb_cache[i].clken = !!(readl(tcaddr + ATMEL_TC_REG(i, SR)) &
  73. ATMEL_TC_CLKSTA);
  74. }
  75. bmr_cache = readl(tcaddr + ATMEL_TC_BMR);
  76. }
  77. void tc_clksrc_resume(struct clocksource *cs)
  78. {
  79. int i;
  80. for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) {
  81. /* Restore registers for the channel, RA and RB are not used */
  82. writel(tcb_cache[i].cmr, tcaddr + ATMEL_TC_REG(i, CMR));
  83. writel(tcb_cache[i].rc, tcaddr + ATMEL_TC_REG(i, RC));
  84. writel(0, tcaddr + ATMEL_TC_REG(i, RA));
  85. writel(0, tcaddr + ATMEL_TC_REG(i, RB));
  86. /* Disable all the interrupts */
  87. writel(0xff, tcaddr + ATMEL_TC_REG(i, IDR));
  88. /* Reenable interrupts that were enabled before suspending */
  89. writel(tcb_cache[i].imr, tcaddr + ATMEL_TC_REG(i, IER));
  90. /* Start the clock if it was used */
  91. if (tcb_cache[i].clken)
  92. writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(i, CCR));
  93. }
  94. /* Dual channel, chain channels */
  95. writel(bmr_cache, tcaddr + ATMEL_TC_BMR);
  96. /* Finally, trigger all the channels*/
  97. writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  98. }
  99. static struct clocksource clksrc = {
  100. .name = "tcb_clksrc",
  101. .rating = 200,
  102. .read = tc_get_cycles,
  103. .mask = CLOCKSOURCE_MASK(32),
  104. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  105. .suspend = tc_clksrc_suspend,
  106. .resume = tc_clksrc_resume,
  107. };
  108. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  109. struct tc_clkevt_device {
  110. struct clock_event_device clkevt;
  111. struct clk *clk;
  112. void __iomem *regs;
  113. };
  114. static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  115. {
  116. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  117. }
  118. /* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  119. * because using one of the divided clocks would usually mean the
  120. * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  121. *
  122. * A divided clock could be good for high resolution timers, since
  123. * 30.5 usec resolution can seem "low".
  124. */
  125. static u32 timer_clock;
  126. static int tc_shutdown(struct clock_event_device *d)
  127. {
  128. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  129. void __iomem *regs = tcd->regs;
  130. writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  131. writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  132. if (!clockevent_state_detached(d))
  133. clk_disable(tcd->clk);
  134. return 0;
  135. }
  136. static int tc_set_oneshot(struct clock_event_device *d)
  137. {
  138. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  139. void __iomem *regs = tcd->regs;
  140. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  141. tc_shutdown(d);
  142. clk_enable(tcd->clk);
  143. /* slow clock, count up to RC, then irq and stop */
  144. writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  145. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  146. writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  147. /* set_next_event() configures and starts the timer */
  148. return 0;
  149. }
  150. static int tc_set_periodic(struct clock_event_device *d)
  151. {
  152. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  153. void __iomem *regs = tcd->regs;
  154. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  155. tc_shutdown(d);
  156. /* By not making the gentime core emulate periodic mode on top
  157. * of oneshot, we get lower overhead and improved accuracy.
  158. */
  159. clk_enable(tcd->clk);
  160. /* slow clock, count up to RC, then irq and restart */
  161. writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  162. regs + ATMEL_TC_REG(2, CMR));
  163. writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  164. /* Enable clock and interrupts on RC compare */
  165. writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  166. /* go go gadget! */
  167. writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, regs +
  168. ATMEL_TC_REG(2, CCR));
  169. return 0;
  170. }
  171. static int tc_next_event(unsigned long delta, struct clock_event_device *d)
  172. {
  173. writel_relaxed(delta, tcaddr + ATMEL_TC_REG(2, RC));
  174. /* go go gadget! */
  175. writel_relaxed(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  176. tcaddr + ATMEL_TC_REG(2, CCR));
  177. return 0;
  178. }
  179. static struct tc_clkevt_device clkevt = {
  180. .clkevt = {
  181. .name = "tc_clkevt",
  182. .features = CLOCK_EVT_FEAT_PERIODIC |
  183. CLOCK_EVT_FEAT_ONESHOT,
  184. /* Should be lower than at91rm9200's system timer */
  185. .rating = 125,
  186. .set_next_event = tc_next_event,
  187. .set_state_shutdown = tc_shutdown,
  188. .set_state_periodic = tc_set_periodic,
  189. .set_state_oneshot = tc_set_oneshot,
  190. },
  191. };
  192. static irqreturn_t ch2_irq(int irq, void *handle)
  193. {
  194. struct tc_clkevt_device *dev = handle;
  195. unsigned int sr;
  196. sr = readl_relaxed(dev->regs + ATMEL_TC_REG(2, SR));
  197. if (sr & ATMEL_TC_CPCS) {
  198. dev->clkevt.event_handler(&dev->clkevt);
  199. return IRQ_HANDLED;
  200. }
  201. return IRQ_NONE;
  202. }
  203. static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  204. {
  205. int ret;
  206. struct clk *t2_clk = tc->clk[2];
  207. int irq = tc->irq[2];
  208. ret = clk_prepare_enable(tc->slow_clk);
  209. if (ret)
  210. return ret;
  211. /* try to enable t2 clk to avoid future errors in mode change */
  212. ret = clk_prepare_enable(t2_clk);
  213. if (ret) {
  214. clk_disable_unprepare(tc->slow_clk);
  215. return ret;
  216. }
  217. clk_disable(t2_clk);
  218. clkevt.regs = tc->regs;
  219. clkevt.clk = t2_clk;
  220. timer_clock = clk32k_divisor_idx;
  221. clkevt.clkevt.cpumask = cpumask_of(0);
  222. ret = request_irq(irq, ch2_irq, IRQF_TIMER, "tc_clkevt", &clkevt);
  223. if (ret) {
  224. clk_unprepare(t2_clk);
  225. clk_disable_unprepare(tc->slow_clk);
  226. return ret;
  227. }
  228. clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  229. return ret;
  230. }
  231. #else /* !CONFIG_GENERIC_CLOCKEVENTS */
  232. static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  233. {
  234. /* NOTHING */
  235. return 0;
  236. }
  237. #endif
  238. static void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx)
  239. {
  240. /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
  241. writel(mck_divisor_idx /* likely divide-by-8 */
  242. | ATMEL_TC_WAVE
  243. | ATMEL_TC_WAVESEL_UP /* free-run */
  244. | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
  245. | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
  246. tcaddr + ATMEL_TC_REG(0, CMR));
  247. writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
  248. writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
  249. writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  250. writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  251. /* channel 1: waveform mode, input TIOA0 */
  252. writel(ATMEL_TC_XC1 /* input: TIOA0 */
  253. | ATMEL_TC_WAVE
  254. | ATMEL_TC_WAVESEL_UP, /* free-run */
  255. tcaddr + ATMEL_TC_REG(1, CMR));
  256. writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
  257. writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
  258. /* chain channel 0 to channel 1*/
  259. writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
  260. /* then reset all the timers */
  261. writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  262. }
  263. static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx)
  264. {
  265. /* channel 0: waveform mode, input mclk/8 */
  266. writel(mck_divisor_idx /* likely divide-by-8 */
  267. | ATMEL_TC_WAVE
  268. | ATMEL_TC_WAVESEL_UP, /* free-run */
  269. tcaddr + ATMEL_TC_REG(0, CMR));
  270. writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  271. writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  272. /* then reset all the timers */
  273. writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  274. }
  275. static int __init tcb_clksrc_init(void)
  276. {
  277. static char bootinfo[] __initdata
  278. = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
  279. struct platform_device *pdev;
  280. struct atmel_tc *tc;
  281. struct clk *t0_clk;
  282. u32 rate, divided_rate = 0;
  283. int best_divisor_idx = -1;
  284. int clk32k_divisor_idx = -1;
  285. int i;
  286. int ret;
  287. tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
  288. if (!tc) {
  289. pr_debug("can't alloc TC for clocksource\n");
  290. return -ENODEV;
  291. }
  292. tcaddr = tc->regs;
  293. pdev = tc->pdev;
  294. t0_clk = tc->clk[0];
  295. ret = clk_prepare_enable(t0_clk);
  296. if (ret) {
  297. pr_debug("can't enable T0 clk\n");
  298. goto err_free_tc;
  299. }
  300. /* How fast will we be counting? Pick something over 5 MHz. */
  301. rate = (u32) clk_get_rate(t0_clk);
  302. for (i = 0; i < 5; i++) {
  303. unsigned divisor = atmel_tc_divisors[i];
  304. unsigned tmp;
  305. /* remember 32 KiHz clock for later */
  306. if (!divisor) {
  307. clk32k_divisor_idx = i;
  308. continue;
  309. }
  310. tmp = rate / divisor;
  311. pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
  312. if (best_divisor_idx > 0) {
  313. if (tmp < 5 * 1000 * 1000)
  314. continue;
  315. }
  316. divided_rate = tmp;
  317. best_divisor_idx = i;
  318. }
  319. printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
  320. divided_rate / 1000000,
  321. ((divided_rate % 1000000) + 500) / 1000);
  322. if (tc->tcb_config && tc->tcb_config->counter_width == 32) {
  323. /* use apropriate function to read 32 bit counter */
  324. clksrc.read = tc_get_cycles32;
  325. /* setup ony channel 0 */
  326. tcb_setup_single_chan(tc, best_divisor_idx);
  327. } else {
  328. /* tclib will give us three clocks no matter what the
  329. * underlying platform supports.
  330. */
  331. ret = clk_prepare_enable(tc->clk[1]);
  332. if (ret) {
  333. pr_debug("can't enable T1 clk\n");
  334. goto err_disable_t0;
  335. }
  336. /* setup both channel 0 & 1 */
  337. tcb_setup_dual_chan(tc, best_divisor_idx);
  338. }
  339. /* and away we go! */
  340. ret = clocksource_register_hz(&clksrc, divided_rate);
  341. if (ret)
  342. goto err_disable_t1;
  343. /* channel 2: periodic and oneshot timer support */
  344. ret = setup_clkevents(tc, clk32k_divisor_idx);
  345. if (ret)
  346. goto err_unregister_clksrc;
  347. return 0;
  348. err_unregister_clksrc:
  349. clocksource_unregister(&clksrc);
  350. err_disable_t1:
  351. if (!tc->tcb_config || tc->tcb_config->counter_width != 32)
  352. clk_disable_unprepare(tc->clk[1]);
  353. err_disable_t0:
  354. clk_disable_unprepare(t0_clk);
  355. err_free_tc:
  356. atmel_tc_free(tc);
  357. return ret;
  358. }
  359. arch_initcall(tcb_clksrc_init);