tcb_clksrc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. *
  20. * - The third channel may be used to provide a 16-bit clockevent
  21. * source, used in either periodic or oneshot mode. This runs
  22. * at 32 KiHZ, and can handle delays of up to two seconds.
  23. *
  24. * A boot clocksource and clockevent source are also currently needed,
  25. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  26. * this code can be used when init_timers() is called, well before most
  27. * devices are set up. (Some low end AT91 parts, which can run uClinux,
  28. * have only the timers in one TC block... they currently don't support
  29. * the tclib code, because of that initialization issue.)
  30. *
  31. * REVISIT behavior during system suspend states... we should disable
  32. * all clocks and save the power. Easily done for clockevent devices,
  33. * but clocksources won't necessarily get the needed notifications.
  34. * For deeper system sleep states, this will be mandatory...
  35. */
  36. static void __iomem *tcaddr;
  37. static cycle_t tc_get_cycles(struct clocksource *cs)
  38. {
  39. unsigned long flags;
  40. u32 lower, upper;
  41. raw_local_irq_save(flags);
  42. do {
  43. upper = __raw_readl(tcaddr + ATMEL_TC_REG(1, CV));
  44. lower = __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
  45. } while (upper != __raw_readl(tcaddr + ATMEL_TC_REG(1, CV)));
  46. raw_local_irq_restore(flags);
  47. return (upper << 16) | lower;
  48. }
  49. static struct clocksource clksrc = {
  50. .name = "tcb_clksrc",
  51. .rating = 200,
  52. .read = tc_get_cycles,
  53. .mask = CLOCKSOURCE_MASK(32),
  54. .shift = 18,
  55. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  56. };
  57. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  58. struct tc_clkevt_device {
  59. struct clock_event_device clkevt;
  60. struct clk *clk;
  61. void __iomem *regs;
  62. };
  63. static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  64. {
  65. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  66. }
  67. /* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  68. * because using one of the divided clocks would usually mean the
  69. * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  70. *
  71. * A divided clock could be good for high resolution timers, since
  72. * 30.5 usec resolution can seem "low".
  73. */
  74. static u32 timer_clock;
  75. static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  76. {
  77. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  78. void __iomem *regs = tcd->regs;
  79. if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC
  80. || tcd->clkevt.mode == CLOCK_EVT_MODE_ONESHOT) {
  81. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  82. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  83. clk_disable(tcd->clk);
  84. }
  85. switch (m) {
  86. /* By not making the gentime core emulate periodic mode on top
  87. * of oneshot, we get lower overhead and improved accuracy.
  88. */
  89. case CLOCK_EVT_MODE_PERIODIC:
  90. clk_enable(tcd->clk);
  91. /* slow clock, count up to RC, then irq and restart */
  92. __raw_writel(timer_clock
  93. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  94. regs + ATMEL_TC_REG(2, CMR));
  95. __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  96. /* Enable clock and interrupts on RC compare */
  97. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  98. /* go go gadget! */
  99. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  100. regs + ATMEL_TC_REG(2, CCR));
  101. break;
  102. case CLOCK_EVT_MODE_ONESHOT:
  103. clk_enable(tcd->clk);
  104. /* slow clock, count up to RC, then irq and stop */
  105. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  106. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  107. regs + ATMEL_TC_REG(2, CMR));
  108. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  109. /* set_next_event() configures and starts the timer */
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. static int tc_next_event(unsigned long delta, struct clock_event_device *d)
  116. {
  117. __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
  118. /* go go gadget! */
  119. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  120. tcaddr + ATMEL_TC_REG(2, CCR));
  121. return 0;
  122. }
  123. static struct tc_clkevt_device clkevt = {
  124. .clkevt = {
  125. .name = "tc_clkevt",
  126. .features = CLOCK_EVT_FEAT_PERIODIC
  127. | CLOCK_EVT_FEAT_ONESHOT,
  128. .shift = 32,
  129. /* Should be lower than at91rm9200's system timer */
  130. .rating = 125,
  131. .set_next_event = tc_next_event,
  132. .set_mode = tc_mode,
  133. },
  134. };
  135. static irqreturn_t ch2_irq(int irq, void *handle)
  136. {
  137. struct tc_clkevt_device *dev = handle;
  138. unsigned int sr;
  139. sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
  140. if (sr & ATMEL_TC_CPCS) {
  141. dev->clkevt.event_handler(&dev->clkevt);
  142. return IRQ_HANDLED;
  143. }
  144. return IRQ_NONE;
  145. }
  146. static struct irqaction tc_irqaction = {
  147. .name = "tc_clkevt",
  148. .flags = IRQF_TIMER | IRQF_DISABLED,
  149. .handler = ch2_irq,
  150. };
  151. static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  152. {
  153. struct clk *t2_clk = tc->clk[2];
  154. int irq = tc->irq[2];
  155. clkevt.regs = tc->regs;
  156. clkevt.clk = t2_clk;
  157. tc_irqaction.dev_id = &clkevt;
  158. timer_clock = clk32k_divisor_idx;
  159. clkevt.clkevt.mult = div_sc(32768, NSEC_PER_SEC, clkevt.clkevt.shift);
  160. clkevt.clkevt.max_delta_ns
  161. = clockevent_delta2ns(0xffff, &clkevt.clkevt);
  162. clkevt.clkevt.min_delta_ns = clockevent_delta2ns(1, &clkevt.clkevt) + 1;
  163. clkevt.clkevt.cpumask = cpumask_of(0);
  164. clockevents_register_device(&clkevt.clkevt);
  165. setup_irq(irq, &tc_irqaction);
  166. }
  167. #else /* !CONFIG_GENERIC_CLOCKEVENTS */
  168. static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  169. {
  170. /* NOTHING */
  171. }
  172. #endif
  173. static int __init tcb_clksrc_init(void)
  174. {
  175. static char bootinfo[] __initdata
  176. = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
  177. struct platform_device *pdev;
  178. struct atmel_tc *tc;
  179. struct clk *t0_clk;
  180. u32 rate, divided_rate = 0;
  181. int best_divisor_idx = -1;
  182. int clk32k_divisor_idx = -1;
  183. int i;
  184. tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
  185. if (!tc) {
  186. pr_debug("can't alloc TC for clocksource\n");
  187. return -ENODEV;
  188. }
  189. tcaddr = tc->regs;
  190. pdev = tc->pdev;
  191. t0_clk = tc->clk[0];
  192. clk_enable(t0_clk);
  193. /* How fast will we be counting? Pick something over 5 MHz. */
  194. rate = (u32) clk_get_rate(t0_clk);
  195. for (i = 0; i < 5; i++) {
  196. unsigned divisor = atmel_tc_divisors[i];
  197. unsigned tmp;
  198. /* remember 32 KiHz clock for later */
  199. if (!divisor) {
  200. clk32k_divisor_idx = i;
  201. continue;
  202. }
  203. tmp = rate / divisor;
  204. pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
  205. if (best_divisor_idx > 0) {
  206. if (tmp < 5 * 1000 * 1000)
  207. continue;
  208. }
  209. divided_rate = tmp;
  210. best_divisor_idx = i;
  211. }
  212. clksrc.mult = clocksource_hz2mult(divided_rate, clksrc.shift);
  213. printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
  214. divided_rate / 1000000,
  215. ((divided_rate + 500000) % 1000000) / 1000);
  216. /* tclib will give us three clocks no matter what the
  217. * underlying platform supports.
  218. */
  219. clk_enable(tc->clk[1]);
  220. /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
  221. __raw_writel(best_divisor_idx /* likely divide-by-8 */
  222. | ATMEL_TC_WAVE
  223. | ATMEL_TC_WAVESEL_UP /* free-run */
  224. | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
  225. | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
  226. tcaddr + ATMEL_TC_REG(0, CMR));
  227. __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
  228. __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
  229. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  230. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  231. /* channel 1: waveform mode, input TIOA0 */
  232. __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
  233. | ATMEL_TC_WAVE
  234. | ATMEL_TC_WAVESEL_UP, /* free-run */
  235. tcaddr + ATMEL_TC_REG(1, CMR));
  236. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
  237. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
  238. /* chain channel 0 to channel 1, then reset all the timers */
  239. __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
  240. __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  241. /* and away we go! */
  242. clocksource_register(&clksrc);
  243. /* channel 2: periodic and oneshot timer support */
  244. setup_clkevents(tc, clk32k_divisor_idx);
  245. return 0;
  246. }
  247. arch_initcall(tcb_clksrc_init);