tcb_clksrc.c 12 KB

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