timer-orion.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Marvell Orion SoC timer handling.
  3. *
  4. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. *
  10. * Timer 0 is used as free-running clocksource, while timer 1 is
  11. * used as clock_event_device.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/sched_clock.h>
  23. #define TIMER_CTRL 0x00
  24. #define TIMER0_EN BIT(0)
  25. #define TIMER0_RELOAD_EN BIT(1)
  26. #define TIMER1_EN BIT(2)
  27. #define TIMER1_RELOAD_EN BIT(3)
  28. #define TIMER0_RELOAD 0x10
  29. #define TIMER0_VAL 0x14
  30. #define TIMER1_RELOAD 0x18
  31. #define TIMER1_VAL 0x1c
  32. #define ORION_ONESHOT_MIN 1
  33. #define ORION_ONESHOT_MAX 0xfffffffe
  34. static void __iomem *timer_base;
  35. static unsigned long notrace orion_read_timer(void)
  36. {
  37. return ~readl(timer_base + TIMER0_VAL);
  38. }
  39. static struct delay_timer orion_delay_timer = {
  40. .read_current_timer = orion_read_timer,
  41. };
  42. static void orion_delay_timer_init(unsigned long rate)
  43. {
  44. orion_delay_timer.freq = rate;
  45. register_current_timer_delay(&orion_delay_timer);
  46. }
  47. /*
  48. * Free-running clocksource handling.
  49. */
  50. static u64 notrace orion_read_sched_clock(void)
  51. {
  52. return ~readl(timer_base + TIMER0_VAL);
  53. }
  54. /*
  55. * Clockevent handling.
  56. */
  57. static u32 ticks_per_jiffy;
  58. static int orion_clkevt_next_event(unsigned long delta,
  59. struct clock_event_device *dev)
  60. {
  61. /* setup and enable one-shot timer */
  62. writel(delta, timer_base + TIMER1_VAL);
  63. atomic_io_modify(timer_base + TIMER_CTRL,
  64. TIMER1_RELOAD_EN | TIMER1_EN, TIMER1_EN);
  65. return 0;
  66. }
  67. static int orion_clkevt_shutdown(struct clock_event_device *dev)
  68. {
  69. /* disable timer */
  70. atomic_io_modify(timer_base + TIMER_CTRL,
  71. TIMER1_RELOAD_EN | TIMER1_EN, 0);
  72. return 0;
  73. }
  74. static int orion_clkevt_set_periodic(struct clock_event_device *dev)
  75. {
  76. /* setup and enable periodic timer at 1/HZ intervals */
  77. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD);
  78. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL);
  79. atomic_io_modify(timer_base + TIMER_CTRL,
  80. TIMER1_RELOAD_EN | TIMER1_EN,
  81. TIMER1_RELOAD_EN | TIMER1_EN);
  82. return 0;
  83. }
  84. static struct clock_event_device orion_clkevt = {
  85. .name = "orion_event",
  86. .features = CLOCK_EVT_FEAT_ONESHOT |
  87. CLOCK_EVT_FEAT_PERIODIC,
  88. .shift = 32,
  89. .rating = 300,
  90. .set_next_event = orion_clkevt_next_event,
  91. .set_state_shutdown = orion_clkevt_shutdown,
  92. .set_state_periodic = orion_clkevt_set_periodic,
  93. .set_state_oneshot = orion_clkevt_shutdown,
  94. .tick_resume = orion_clkevt_shutdown,
  95. };
  96. static irqreturn_t orion_clkevt_irq_handler(int irq, void *dev_id)
  97. {
  98. orion_clkevt.event_handler(&orion_clkevt);
  99. return IRQ_HANDLED;
  100. }
  101. static int __init orion_timer_init(struct device_node *np)
  102. {
  103. unsigned long rate;
  104. struct clk *clk;
  105. int irq, ret;
  106. /* timer registers are shared with watchdog timer */
  107. timer_base = of_iomap(np, 0);
  108. if (!timer_base) {
  109. pr_err("%pOFn: unable to map resource\n", np);
  110. return -ENXIO;
  111. }
  112. clk = of_clk_get(np, 0);
  113. if (IS_ERR(clk)) {
  114. pr_err("%pOFn: unable to get clk\n", np);
  115. return PTR_ERR(clk);
  116. }
  117. ret = clk_prepare_enable(clk);
  118. if (ret) {
  119. pr_err("Failed to prepare clock\n");
  120. return ret;
  121. }
  122. /* we are only interested in timer1 irq */
  123. irq = irq_of_parse_and_map(np, 1);
  124. if (irq <= 0) {
  125. pr_err("%pOFn: unable to parse timer1 irq\n", np);
  126. ret = -EINVAL;
  127. goto out_unprep_clk;
  128. }
  129. rate = clk_get_rate(clk);
  130. /* setup timer0 as free-running clocksource */
  131. writel(~0, timer_base + TIMER0_VAL);
  132. writel(~0, timer_base + TIMER0_RELOAD);
  133. atomic_io_modify(timer_base + TIMER_CTRL,
  134. TIMER0_RELOAD_EN | TIMER0_EN,
  135. TIMER0_RELOAD_EN | TIMER0_EN);
  136. ret = clocksource_mmio_init(timer_base + TIMER0_VAL,
  137. "orion_clocksource", rate, 300, 32,
  138. clocksource_mmio_readl_down);
  139. if (ret) {
  140. pr_err("Failed to initialize mmio timer\n");
  141. goto out_unprep_clk;
  142. }
  143. sched_clock_register(orion_read_sched_clock, 32, rate);
  144. /* setup timer1 as clockevent timer */
  145. ret = request_irq(irq, orion_clkevt_irq_handler, IRQF_TIMER,
  146. "orion_event", NULL);
  147. if (ret) {
  148. pr_err("%pOFn: unable to setup irq\n", np);
  149. goto out_unprep_clk;
  150. }
  151. ticks_per_jiffy = (clk_get_rate(clk) + HZ/2) / HZ;
  152. orion_clkevt.cpumask = cpumask_of(0);
  153. orion_clkevt.irq = irq;
  154. clockevents_config_and_register(&orion_clkevt, rate,
  155. ORION_ONESHOT_MIN, ORION_ONESHOT_MAX);
  156. orion_delay_timer_init(rate);
  157. return 0;
  158. out_unprep_clk:
  159. clk_disable_unprepare(clk);
  160. return ret;
  161. }
  162. TIMER_OF_DECLARE(orion_timer, "marvell,orion-timer", orion_timer_init);