timer-orion.c 4.5 KB

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