timer-sun5i.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Allwinner SoCs hstimer driver.
  3. *
  4. * Copyright (C) 2013 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqreturn.h>
  18. #include <linux/reset.h>
  19. #include <linux/sched_clock.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #define TIMER_IRQ_EN_REG 0x00
  24. #define TIMER_IRQ_EN(val) BIT(val)
  25. #define TIMER_IRQ_ST_REG 0x04
  26. #define TIMER_CTL_REG(val) (0x20 * (val) + 0x10)
  27. #define TIMER_CTL_ENABLE BIT(0)
  28. #define TIMER_CTL_RELOAD BIT(1)
  29. #define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4)
  30. #define TIMER_CTL_ONESHOT BIT(7)
  31. #define TIMER_INTVAL_LO_REG(val) (0x20 * (val) + 0x14)
  32. #define TIMER_INTVAL_HI_REG(val) (0x20 * (val) + 0x18)
  33. #define TIMER_CNTVAL_LO_REG(val) (0x20 * (val) + 0x1c)
  34. #define TIMER_CNTVAL_HI_REG(val) (0x20 * (val) + 0x20)
  35. #define TIMER_SYNC_TICKS 3
  36. static void __iomem *timer_base;
  37. static u32 ticks_per_jiffy;
  38. /*
  39. * When we disable a timer, we need to wait at least for 2 cycles of
  40. * the timer source clock. We will use for that the clocksource timer
  41. * that is already setup and runs at the same frequency than the other
  42. * timers, and we never will be disabled.
  43. */
  44. static void sun5i_clkevt_sync(void)
  45. {
  46. u32 old = readl(timer_base + TIMER_CNTVAL_LO_REG(1));
  47. while ((old - readl(timer_base + TIMER_CNTVAL_LO_REG(1))) < TIMER_SYNC_TICKS)
  48. cpu_relax();
  49. }
  50. static void sun5i_clkevt_time_stop(u8 timer)
  51. {
  52. u32 val = readl(timer_base + TIMER_CTL_REG(timer));
  53. writel(val & ~TIMER_CTL_ENABLE, timer_base + TIMER_CTL_REG(timer));
  54. sun5i_clkevt_sync();
  55. }
  56. static void sun5i_clkevt_time_setup(u8 timer, u32 delay)
  57. {
  58. writel(delay, timer_base + TIMER_INTVAL_LO_REG(timer));
  59. }
  60. static void sun5i_clkevt_time_start(u8 timer, bool periodic)
  61. {
  62. u32 val = readl(timer_base + TIMER_CTL_REG(timer));
  63. if (periodic)
  64. val &= ~TIMER_CTL_ONESHOT;
  65. else
  66. val |= TIMER_CTL_ONESHOT;
  67. writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  68. timer_base + TIMER_CTL_REG(timer));
  69. }
  70. static void sun5i_clkevt_mode(enum clock_event_mode mode,
  71. struct clock_event_device *clk)
  72. {
  73. switch (mode) {
  74. case CLOCK_EVT_MODE_PERIODIC:
  75. sun5i_clkevt_time_stop(0);
  76. sun5i_clkevt_time_setup(0, ticks_per_jiffy);
  77. sun5i_clkevt_time_start(0, true);
  78. break;
  79. case CLOCK_EVT_MODE_ONESHOT:
  80. sun5i_clkevt_time_stop(0);
  81. sun5i_clkevt_time_start(0, false);
  82. break;
  83. case CLOCK_EVT_MODE_UNUSED:
  84. case CLOCK_EVT_MODE_SHUTDOWN:
  85. default:
  86. sun5i_clkevt_time_stop(0);
  87. break;
  88. }
  89. }
  90. static int sun5i_clkevt_next_event(unsigned long evt,
  91. struct clock_event_device *unused)
  92. {
  93. sun5i_clkevt_time_stop(0);
  94. sun5i_clkevt_time_setup(0, evt - TIMER_SYNC_TICKS);
  95. sun5i_clkevt_time_start(0, false);
  96. return 0;
  97. }
  98. static struct clock_event_device sun5i_clockevent = {
  99. .name = "sun5i_tick",
  100. .rating = 340,
  101. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  102. .set_mode = sun5i_clkevt_mode,
  103. .set_next_event = sun5i_clkevt_next_event,
  104. };
  105. static irqreturn_t sun5i_timer_interrupt(int irq, void *dev_id)
  106. {
  107. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  108. writel(0x1, timer_base + TIMER_IRQ_ST_REG);
  109. evt->event_handler(evt);
  110. return IRQ_HANDLED;
  111. }
  112. static struct irqaction sun5i_timer_irq = {
  113. .name = "sun5i_timer0",
  114. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  115. .handler = sun5i_timer_interrupt,
  116. .dev_id = &sun5i_clockevent,
  117. };
  118. static u64 sun5i_timer_sched_read(void)
  119. {
  120. return ~readl(timer_base + TIMER_CNTVAL_LO_REG(1));
  121. }
  122. static void __init sun5i_timer_init(struct device_node *node)
  123. {
  124. struct reset_control *rstc;
  125. unsigned long rate;
  126. struct clk *clk;
  127. int ret, irq;
  128. u32 val;
  129. timer_base = of_iomap(node, 0);
  130. if (!timer_base)
  131. panic("Can't map registers");
  132. irq = irq_of_parse_and_map(node, 0);
  133. if (irq <= 0)
  134. panic("Can't parse IRQ");
  135. clk = of_clk_get(node, 0);
  136. if (IS_ERR(clk))
  137. panic("Can't get timer clock");
  138. clk_prepare_enable(clk);
  139. rate = clk_get_rate(clk);
  140. rstc = of_reset_control_get(node, NULL);
  141. if (!IS_ERR(rstc))
  142. reset_control_deassert(rstc);
  143. writel(~0, timer_base + TIMER_INTVAL_LO_REG(1));
  144. writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  145. timer_base + TIMER_CTL_REG(1));
  146. sched_clock_register(sun5i_timer_sched_read, 32, rate);
  147. clocksource_mmio_init(timer_base + TIMER_CNTVAL_LO_REG(1), node->name,
  148. rate, 340, 32, clocksource_mmio_readl_down);
  149. ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  150. ret = setup_irq(irq, &sun5i_timer_irq);
  151. if (ret)
  152. pr_warn("failed to setup irq %d\n", irq);
  153. /* Enable timer0 interrupt */
  154. val = readl(timer_base + TIMER_IRQ_EN_REG);
  155. writel(val | TIMER_IRQ_EN(0), timer_base + TIMER_IRQ_EN_REG);
  156. sun5i_clockevent.cpumask = cpu_possible_mask;
  157. sun5i_clockevent.irq = irq;
  158. clockevents_config_and_register(&sun5i_clockevent, rate,
  159. TIMER_SYNC_TICKS, 0xffffffff);
  160. }
  161. CLOCKSOURCE_OF_DECLARE(sun5i_a13, "allwinner,sun5i-a13-hstimer",
  162. sun5i_timer_init);
  163. CLOCKSOURCE_OF_DECLARE(sun7i_a20, "allwinner,sun7i-a20-hstimer",
  164. sun5i_timer_init);