pxa_timer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 int pxa_osmr0_shutdown(struct clock_event_device *evt)
  73. {
  74. /* initializing, released, or preparing for suspend */
  75. timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
  76. timer_writel(OSSR_M0, OSSR);
  77. return 0;
  78. }
  79. #ifdef CONFIG_PM
  80. static unsigned long osmr[4], oier, oscr;
  81. static void pxa_timer_suspend(struct clock_event_device *cedev)
  82. {
  83. osmr[0] = timer_readl(OSMR0);
  84. osmr[1] = timer_readl(OSMR1);
  85. osmr[2] = timer_readl(OSMR2);
  86. osmr[3] = timer_readl(OSMR3);
  87. oier = timer_readl(OIER);
  88. oscr = timer_readl(OSCR);
  89. }
  90. static void pxa_timer_resume(struct clock_event_device *cedev)
  91. {
  92. /*
  93. * Ensure that we have at least MIN_OSCR_DELTA between match
  94. * register 0 and the OSCR, to guarantee that we will receive
  95. * the one-shot timer interrupt. We adjust OSMR0 in preference
  96. * to OSCR to guarantee that OSCR is monotonically incrementing.
  97. */
  98. if (osmr[0] - oscr < MIN_OSCR_DELTA)
  99. osmr[0] += MIN_OSCR_DELTA;
  100. timer_writel(osmr[0], OSMR0);
  101. timer_writel(osmr[1], OSMR1);
  102. timer_writel(osmr[2], OSMR2);
  103. timer_writel(osmr[3], OSMR3);
  104. timer_writel(oier, OIER);
  105. timer_writel(oscr, OSCR);
  106. }
  107. #else
  108. #define pxa_timer_suspend NULL
  109. #define pxa_timer_resume NULL
  110. #endif
  111. static struct clock_event_device ckevt_pxa_osmr0 = {
  112. .name = "osmr0",
  113. .features = CLOCK_EVT_FEAT_ONESHOT,
  114. .rating = 200,
  115. .set_next_event = pxa_osmr0_set_next_event,
  116. .set_state_shutdown = pxa_osmr0_shutdown,
  117. .set_state_oneshot = pxa_osmr0_shutdown,
  118. .suspend = pxa_timer_suspend,
  119. .resume = pxa_timer_resume,
  120. };
  121. static struct irqaction pxa_ost0_irq = {
  122. .name = "ost0",
  123. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  124. .handler = pxa_ost0_interrupt,
  125. .dev_id = &ckevt_pxa_osmr0,
  126. };
  127. static void __init pxa_timer_common_init(int irq, unsigned long clock_tick_rate)
  128. {
  129. timer_writel(0, OIER);
  130. timer_writel(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR);
  131. sched_clock_register(pxa_read_sched_clock, 32, clock_tick_rate);
  132. ckevt_pxa_osmr0.cpumask = cpumask_of(0);
  133. setup_irq(irq, &pxa_ost0_irq);
  134. clocksource_mmio_init(timer_base + OSCR, "oscr0", clock_tick_rate, 200,
  135. 32, clocksource_mmio_readl_up);
  136. clockevents_config_and_register(&ckevt_pxa_osmr0, clock_tick_rate,
  137. MIN_OSCR_DELTA * 2, 0x7fffffff);
  138. }
  139. static void __init pxa_timer_dt_init(struct device_node *np)
  140. {
  141. struct clk *clk;
  142. int irq;
  143. /* timer registers are shared with watchdog timer */
  144. timer_base = of_iomap(np, 0);
  145. if (!timer_base)
  146. panic("%s: unable to map resource\n", np->name);
  147. clk = of_clk_get(np, 0);
  148. if (IS_ERR(clk)) {
  149. pr_crit("%s: unable to get clk\n", np->name);
  150. return;
  151. }
  152. clk_prepare_enable(clk);
  153. /* we are only interested in OS-timer0 irq */
  154. irq = irq_of_parse_and_map(np, 0);
  155. if (irq <= 0) {
  156. pr_crit("%s: unable to parse OS-timer0 irq\n", np->name);
  157. return;
  158. }
  159. pxa_timer_common_init(irq, clk_get_rate(clk));
  160. }
  161. CLOCKSOURCE_OF_DECLARE(pxa_timer, "marvell,pxa-timer", pxa_timer_dt_init);
  162. /*
  163. * Legacy timer init for non device-tree boards.
  164. */
  165. void __init pxa_timer_nodt_init(int irq, void __iomem *base,
  166. unsigned long clock_tick_rate)
  167. {
  168. struct clk *clk;
  169. timer_base = base;
  170. clk = clk_get(NULL, "OSTIMER0");
  171. if (clk && !IS_ERR(clk))
  172. clk_prepare_enable(clk);
  173. else
  174. pr_crit("%s: unable to get clk\n", __func__);
  175. pxa_timer_common_init(irq, clock_tick_rate);
  176. }