pxa_timer.c 6.1 KB

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