tcb_clksrc.c 10 KB

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