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