tcb_clksrc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  55. };
  56. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  57. struct tc_clkevt_device {
  58. struct clock_event_device clkevt;
  59. struct clk *clk;
  60. void __iomem *regs;
  61. };
  62. static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  63. {
  64. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  65. }
  66. /* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  67. * because using one of the divided clocks would usually mean the
  68. * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  69. *
  70. * A divided clock could be good for high resolution timers, since
  71. * 30.5 usec resolution can seem "low".
  72. */
  73. static u32 timer_clock;
  74. static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  75. {
  76. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  77. void __iomem *regs = tcd->regs;
  78. if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC
  79. || tcd->clkevt.mode == CLOCK_EVT_MODE_ONESHOT) {
  80. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  81. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  82. clk_disable(tcd->clk);
  83. }
  84. switch (m) {
  85. /* By not making the gentime core emulate periodic mode on top
  86. * of oneshot, we get lower overhead and improved accuracy.
  87. */
  88. case CLOCK_EVT_MODE_PERIODIC:
  89. clk_enable(tcd->clk);
  90. /* slow clock, count up to RC, then irq and restart */
  91. __raw_writel(timer_clock
  92. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  93. regs + ATMEL_TC_REG(2, CMR));
  94. __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  95. /* Enable clock and interrupts on RC compare */
  96. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  97. /* go go gadget! */
  98. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  99. regs + ATMEL_TC_REG(2, CCR));
  100. break;
  101. case CLOCK_EVT_MODE_ONESHOT:
  102. clk_enable(tcd->clk);
  103. /* slow clock, count up to RC, then irq and stop */
  104. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  105. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  106. regs + ATMEL_TC_REG(2, CMR));
  107. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  108. /* set_next_event() configures and starts the timer */
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. static int tc_next_event(unsigned long delta, struct clock_event_device *d)
  115. {
  116. __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
  117. /* go go gadget! */
  118. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  119. tcaddr + ATMEL_TC_REG(2, CCR));
  120. return 0;
  121. }
  122. static struct tc_clkevt_device clkevt = {
  123. .clkevt = {
  124. .name = "tc_clkevt",
  125. .features = CLOCK_EVT_FEAT_PERIODIC
  126. | CLOCK_EVT_FEAT_ONESHOT,
  127. .shift = 32,
  128. /* Should be lower than at91rm9200's system timer */
  129. .rating = 125,
  130. .set_next_event = tc_next_event,
  131. .set_mode = tc_mode,
  132. },
  133. };
  134. static irqreturn_t ch2_irq(int irq, void *handle)
  135. {
  136. struct tc_clkevt_device *dev = handle;
  137. unsigned int sr;
  138. sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
  139. if (sr & ATMEL_TC_CPCS) {
  140. dev->clkevt.event_handler(&dev->clkevt);
  141. return IRQ_HANDLED;
  142. }
  143. return IRQ_NONE;
  144. }
  145. static struct irqaction tc_irqaction = {
  146. .name = "tc_clkevt",
  147. .flags = IRQF_TIMER | IRQF_DISABLED,
  148. .handler = ch2_irq,
  149. };
  150. static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  151. {
  152. struct clk *t2_clk = tc->clk[2];
  153. int irq = tc->irq[2];
  154. clkevt.regs = tc->regs;
  155. clkevt.clk = t2_clk;
  156. tc_irqaction.dev_id = &clkevt;
  157. timer_clock = clk32k_divisor_idx;
  158. clkevt.clkevt.mult = div_sc(32768, NSEC_PER_SEC, clkevt.clkevt.shift);
  159. clkevt.clkevt.max_delta_ns
  160. = clockevent_delta2ns(0xffff, &clkevt.clkevt);
  161. clkevt.clkevt.min_delta_ns = clockevent_delta2ns(1, &clkevt.clkevt) + 1;
  162. clkevt.clkevt.cpumask = cpumask_of(0);
  163. clockevents_register_device(&clkevt.clkevt);
  164. setup_irq(irq, &tc_irqaction);
  165. }
  166. #else /* !CONFIG_GENERIC_CLOCKEVENTS */
  167. static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  168. {
  169. /* NOTHING */
  170. }
  171. #endif
  172. static int __init tcb_clksrc_init(void)
  173. {
  174. static char bootinfo[] __initdata
  175. = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
  176. struct platform_device *pdev;
  177. struct atmel_tc *tc;
  178. struct clk *t0_clk;
  179. u32 rate, divided_rate = 0;
  180. int best_divisor_idx = -1;
  181. int clk32k_divisor_idx = -1;
  182. int i;
  183. tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
  184. if (!tc) {
  185. pr_debug("can't alloc TC for clocksource\n");
  186. return -ENODEV;
  187. }
  188. tcaddr = tc->regs;
  189. pdev = tc->pdev;
  190. t0_clk = tc->clk[0];
  191. clk_enable(t0_clk);
  192. /* How fast will we be counting? Pick something over 5 MHz. */
  193. rate = (u32) clk_get_rate(t0_clk);
  194. for (i = 0; i < 5; i++) {
  195. unsigned divisor = atmel_tc_divisors[i];
  196. unsigned tmp;
  197. /* remember 32 KiHz clock for later */
  198. if (!divisor) {
  199. clk32k_divisor_idx = i;
  200. continue;
  201. }
  202. tmp = rate / divisor;
  203. pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
  204. if (best_divisor_idx > 0) {
  205. if (tmp < 5 * 1000 * 1000)
  206. continue;
  207. }
  208. divided_rate = tmp;
  209. best_divisor_idx = i;
  210. }
  211. printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
  212. divided_rate / 1000000,
  213. ((divided_rate + 500000) % 1000000) / 1000);
  214. /* tclib will give us three clocks no matter what the
  215. * underlying platform supports.
  216. */
  217. clk_enable(tc->clk[1]);
  218. /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
  219. __raw_writel(best_divisor_idx /* likely divide-by-8 */
  220. | ATMEL_TC_WAVE
  221. | ATMEL_TC_WAVESEL_UP /* free-run */
  222. | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
  223. | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
  224. tcaddr + ATMEL_TC_REG(0, CMR));
  225. __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
  226. __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
  227. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  228. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  229. /* channel 1: waveform mode, input TIOA0 */
  230. __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
  231. | ATMEL_TC_WAVE
  232. | ATMEL_TC_WAVESEL_UP, /* free-run */
  233. tcaddr + ATMEL_TC_REG(1, CMR));
  234. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
  235. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
  236. /* chain channel 0 to channel 1, then reset all the timers */
  237. __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
  238. __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  239. /* and away we go! */
  240. clocksource_register_hz(&clksrc, divided_rate);
  241. /* channel 2: periodic and oneshot timer support */
  242. setup_clkevents(tc, clk32k_divisor_idx);
  243. return 0;
  244. }
  245. arch_initcall(tcb_clksrc_init);