timer-sun5i.c 5.0 KB

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