pxa_timer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * arch/arm/mach-pxa/time.c
  3. *
  4. * PXA clocksource, clockevents, and OST interrupt handlers.
  5. * Copyright (c) 2007 by Bill Gatliff <bgat@billgatliff.com>.
  6. *
  7. * Derived from Nicolas Pitre's PXA timer handler Copyright (c) 2001
  8. * by MontaVista Software, Inc. (Nico, your code rocks!)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/clk.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/sched_clock.h>
  22. #include <asm/div64.h>
  23. #define OSMR0 0x00 /* OS Timer 0 Match Register */
  24. #define OSMR1 0x04 /* OS Timer 1 Match Register */
  25. #define OSMR2 0x08 /* OS Timer 2 Match Register */
  26. #define OSMR3 0x0C /* OS Timer 3 Match Register */
  27. #define OSCR 0x10 /* OS Timer Counter Register */
  28. #define OSSR 0x14 /* OS Timer Status Register */
  29. #define OWER 0x18 /* OS Timer Watchdog Enable Register */
  30. #define OIER 0x1C /* OS Timer Interrupt Enable Register */
  31. #define OSSR_M3 (1 << 3) /* Match status channel 3 */
  32. #define OSSR_M2 (1 << 2) /* Match status channel 2 */
  33. #define OSSR_M1 (1 << 1) /* Match status channel 1 */
  34. #define OSSR_M0 (1 << 0) /* Match status channel 0 */
  35. #define OIER_E0 (1 << 0) /* Interrupt enable channel 0 */
  36. /*
  37. * This is PXA's sched_clock implementation. This has a resolution
  38. * of at least 308 ns and a maximum value of 208 days.
  39. *
  40. * The return value is guaranteed to be monotonic in that range as
  41. * long as there is always less than 582 seconds between successive
  42. * calls to sched_clock() which should always be the case in practice.
  43. */
  44. #define timer_readl(reg) readl_relaxed(timer_base + (reg))
  45. #define timer_writel(val, reg) writel_relaxed((val), timer_base + (reg))
  46. static void __iomem *timer_base;
  47. static u64 notrace pxa_read_sched_clock(void)
  48. {
  49. return timer_readl(OSCR);
  50. }
  51. #define MIN_OSCR_DELTA 16
  52. static irqreturn_t
  53. pxa_ost0_interrupt(int irq, void *dev_id)
  54. {
  55. struct clock_event_device *c = dev_id;
  56. /* Disarm the compare/match, signal the event. */
  57. timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
  58. timer_writel(OSSR_M0, OSSR);
  59. c->event_handler(c);
  60. return IRQ_HANDLED;
  61. }
  62. static int
  63. pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev)
  64. {
  65. unsigned long next, oscr;
  66. timer_writel(timer_readl(OIER) | OIER_E0, OIER);
  67. next = timer_readl(OSCR) + delta;
  68. timer_writel(next, OSMR0);
  69. oscr = timer_readl(OSCR);
  70. return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
  71. }
  72. static void
  73. pxa_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  74. {
  75. switch (mode) {
  76. case CLOCK_EVT_MODE_ONESHOT:
  77. timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
  78. timer_writel(OSSR_M0, OSSR);
  79. break;
  80. case CLOCK_EVT_MODE_UNUSED:
  81. case CLOCK_EVT_MODE_SHUTDOWN:
  82. /* initializing, released, or preparing for suspend */
  83. timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
  84. timer_writel(OSSR_M0, OSSR);
  85. break;
  86. case CLOCK_EVT_MODE_RESUME:
  87. case CLOCK_EVT_MODE_PERIODIC:
  88. break;
  89. }
  90. }
  91. #ifdef CONFIG_PM
  92. static unsigned long osmr[4], oier, oscr;
  93. static void pxa_timer_suspend(struct clock_event_device *cedev)
  94. {
  95. osmr[0] = timer_readl(OSMR0);
  96. osmr[1] = timer_readl(OSMR1);
  97. osmr[2] = timer_readl(OSMR2);
  98. osmr[3] = timer_readl(OSMR3);
  99. oier = timer_readl(OIER);
  100. oscr = timer_readl(OSCR);
  101. }
  102. static void pxa_timer_resume(struct clock_event_device *cedev)
  103. {
  104. /*
  105. * Ensure that we have at least MIN_OSCR_DELTA between match
  106. * register 0 and the OSCR, to guarantee that we will receive
  107. * the one-shot timer interrupt. We adjust OSMR0 in preference
  108. * to OSCR to guarantee that OSCR is monotonically incrementing.
  109. */
  110. if (osmr[0] - oscr < MIN_OSCR_DELTA)
  111. osmr[0] += MIN_OSCR_DELTA;
  112. timer_writel(osmr[0], OSMR0);
  113. timer_writel(osmr[1], OSMR1);
  114. timer_writel(osmr[2], OSMR2);
  115. timer_writel(osmr[3], OSMR3);
  116. timer_writel(oier, OIER);
  117. timer_writel(oscr, OSCR);
  118. }
  119. #else
  120. #define pxa_timer_suspend NULL
  121. #define pxa_timer_resume NULL
  122. #endif
  123. static struct clock_event_device ckevt_pxa_osmr0 = {
  124. .name = "osmr0",
  125. .features = CLOCK_EVT_FEAT_ONESHOT,
  126. .rating = 200,
  127. .set_next_event = pxa_osmr0_set_next_event,
  128. .set_mode = pxa_osmr0_set_mode,
  129. .suspend = pxa_timer_suspend,
  130. .resume = pxa_timer_resume,
  131. };
  132. static struct irqaction pxa_ost0_irq = {
  133. .name = "ost0",
  134. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  135. .handler = pxa_ost0_interrupt,
  136. .dev_id = &ckevt_pxa_osmr0,
  137. };
  138. static void __init pxa_timer_common_init(int irq, unsigned long clock_tick_rate)
  139. {
  140. timer_writel(0, OIER);
  141. timer_writel(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR);
  142. sched_clock_register(pxa_read_sched_clock, 32, clock_tick_rate);
  143. ckevt_pxa_osmr0.cpumask = cpumask_of(0);
  144. setup_irq(irq, &pxa_ost0_irq);
  145. clocksource_mmio_init(timer_base + OSCR, "oscr0", clock_tick_rate, 200,
  146. 32, clocksource_mmio_readl_up);
  147. clockevents_config_and_register(&ckevt_pxa_osmr0, clock_tick_rate,
  148. MIN_OSCR_DELTA * 2, 0x7fffffff);
  149. }
  150. static void __init pxa_timer_dt_init(struct device_node *np)
  151. {
  152. struct clk *clk;
  153. int irq;
  154. /* timer registers are shared with watchdog timer */
  155. timer_base = of_iomap(np, 0);
  156. if (!timer_base)
  157. panic("%s: unable to map resource\n", np->name);
  158. clk = of_clk_get(np, 0);
  159. if (IS_ERR(clk)) {
  160. pr_crit("%s: unable to get clk\n", np->name);
  161. return;
  162. }
  163. clk_prepare_enable(clk);
  164. /* we are only interested in OS-timer0 irq */
  165. irq = irq_of_parse_and_map(np, 0);
  166. if (irq <= 0) {
  167. pr_crit("%s: unable to parse OS-timer0 irq\n", np->name);
  168. return;
  169. }
  170. pxa_timer_common_init(irq, clk_get_rate(clk));
  171. }
  172. CLOCKSOURCE_OF_DECLARE(pxa_timer, "marvell,pxa-timer", pxa_timer_dt_init);
  173. /*
  174. * Legacy timer init for non device-tree boards.
  175. */
  176. void __init pxa_timer_nodt_init(int irq, void __iomem *base,
  177. unsigned long clock_tick_rate)
  178. {
  179. struct clk *clk;
  180. timer_base = base;
  181. clk = clk_get(NULL, "OSTIMER0");
  182. if (clk && !IS_ERR(clk))
  183. clk_prepare_enable(clk);
  184. else
  185. pr_crit("%s: unable to get clk\n", __func__);
  186. pxa_timer_common_init(irq, clock_tick_rate);
  187. }