tcb_clksrc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  81. {
  82. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  83. void __iomem *regs = tcd->regs;
  84. if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC
  85. || tcd->clkevt.mode == CLOCK_EVT_MODE_ONESHOT) {
  86. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  87. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  88. clk_disable(tcd->clk);
  89. }
  90. switch (m) {
  91. /* By not making the gentime core emulate periodic mode on top
  92. * of oneshot, we get lower overhead and improved accuracy.
  93. */
  94. case CLOCK_EVT_MODE_PERIODIC:
  95. clk_enable(tcd->clk);
  96. /* slow clock, count up to RC, then irq and restart */
  97. __raw_writel(timer_clock
  98. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  99. regs + ATMEL_TC_REG(2, CMR));
  100. __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  101. /* Enable clock and interrupts on RC compare */
  102. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  103. /* go go gadget! */
  104. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  105. regs + ATMEL_TC_REG(2, CCR));
  106. break;
  107. case CLOCK_EVT_MODE_ONESHOT:
  108. clk_enable(tcd->clk);
  109. /* slow clock, count up to RC, then irq and stop */
  110. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  111. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  112. regs + ATMEL_TC_REG(2, CMR));
  113. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  114. /* set_next_event() configures and starts the timer */
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. static int tc_next_event(unsigned long delta, struct clock_event_device *d)
  121. {
  122. __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
  123. /* go go gadget! */
  124. __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
  125. tcaddr + ATMEL_TC_REG(2, CCR));
  126. return 0;
  127. }
  128. static struct tc_clkevt_device clkevt = {
  129. .clkevt = {
  130. .name = "tc_clkevt",
  131. .features = CLOCK_EVT_FEAT_PERIODIC
  132. | CLOCK_EVT_FEAT_ONESHOT,
  133. /* Should be lower than at91rm9200's system timer */
  134. .rating = 125,
  135. .set_next_event = tc_next_event,
  136. .set_mode = tc_mode,
  137. },
  138. };
  139. static irqreturn_t ch2_irq(int irq, void *handle)
  140. {
  141. struct tc_clkevt_device *dev = handle;
  142. unsigned int sr;
  143. sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
  144. if (sr & ATMEL_TC_CPCS) {
  145. dev->clkevt.event_handler(&dev->clkevt);
  146. return IRQ_HANDLED;
  147. }
  148. return IRQ_NONE;
  149. }
  150. static struct irqaction tc_irqaction = {
  151. .name = "tc_clkevt",
  152. .flags = IRQF_TIMER | IRQF_DISABLED,
  153. .handler = ch2_irq,
  154. };
  155. static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  156. {
  157. struct clk *t2_clk = tc->clk[2];
  158. int irq = tc->irq[2];
  159. clkevt.regs = tc->regs;
  160. clkevt.clk = t2_clk;
  161. tc_irqaction.dev_id = &clkevt;
  162. timer_clock = clk32k_divisor_idx;
  163. clkevt.clkevt.cpumask = cpumask_of(0);
  164. clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  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 void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx)
  174. {
  175. /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
  176. __raw_writel(mck_divisor_idx /* likely divide-by-8 */
  177. | ATMEL_TC_WAVE
  178. | ATMEL_TC_WAVESEL_UP /* free-run */
  179. | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
  180. | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
  181. tcaddr + ATMEL_TC_REG(0, CMR));
  182. __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
  183. __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
  184. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  185. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  186. /* channel 1: waveform mode, input TIOA0 */
  187. __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
  188. | ATMEL_TC_WAVE
  189. | ATMEL_TC_WAVESEL_UP, /* free-run */
  190. tcaddr + ATMEL_TC_REG(1, CMR));
  191. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
  192. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
  193. /* chain channel 0 to channel 1*/
  194. __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
  195. /* then reset all the timers */
  196. __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  197. }
  198. static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx)
  199. {
  200. /* channel 0: waveform mode, input mclk/8 */
  201. __raw_writel(mck_divisor_idx /* likely divide-by-8 */
  202. | ATMEL_TC_WAVE
  203. | ATMEL_TC_WAVESEL_UP, /* free-run */
  204. tcaddr + ATMEL_TC_REG(0, CMR));
  205. __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
  206. __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
  207. /* then reset all the timers */
  208. __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
  209. }
  210. static int __init tcb_clksrc_init(void)
  211. {
  212. static char bootinfo[] __initdata
  213. = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
  214. struct platform_device *pdev;
  215. struct atmel_tc *tc;
  216. struct clk *t0_clk;
  217. u32 rate, divided_rate = 0;
  218. int best_divisor_idx = -1;
  219. int clk32k_divisor_idx = -1;
  220. int i;
  221. tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
  222. if (!tc) {
  223. pr_debug("can't alloc TC for clocksource\n");
  224. return -ENODEV;
  225. }
  226. tcaddr = tc->regs;
  227. pdev = tc->pdev;
  228. t0_clk = tc->clk[0];
  229. clk_enable(t0_clk);
  230. /* How fast will we be counting? Pick something over 5 MHz. */
  231. rate = (u32) clk_get_rate(t0_clk);
  232. for (i = 0; i < 5; i++) {
  233. unsigned divisor = atmel_tc_divisors[i];
  234. unsigned tmp;
  235. /* remember 32 KiHz clock for later */
  236. if (!divisor) {
  237. clk32k_divisor_idx = i;
  238. continue;
  239. }
  240. tmp = rate / divisor;
  241. pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
  242. if (best_divisor_idx > 0) {
  243. if (tmp < 5 * 1000 * 1000)
  244. continue;
  245. }
  246. divided_rate = tmp;
  247. best_divisor_idx = i;
  248. }
  249. printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
  250. divided_rate / 1000000,
  251. ((divided_rate + 500000) % 1000000) / 1000);
  252. if (tc->tcb_config && tc->tcb_config->counter_width == 32) {
  253. /* use apropriate function to read 32 bit counter */
  254. clksrc.read = tc_get_cycles32;
  255. /* setup ony channel 0 */
  256. tcb_setup_single_chan(tc, best_divisor_idx);
  257. } else {
  258. /* tclib will give us three clocks no matter what the
  259. * underlying platform supports.
  260. */
  261. clk_enable(tc->clk[1]);
  262. /* setup both channel 0 & 1 */
  263. tcb_setup_dual_chan(tc, best_divisor_idx);
  264. }
  265. /* and away we go! */
  266. clocksource_register_hz(&clksrc, divided_rate);
  267. /* channel 2: periodic and oneshot timer support */
  268. setup_clkevents(tc, clk32k_divisor_idx);
  269. return 0;
  270. }
  271. arch_initcall(tcb_clksrc_init);