time-armada-370-xp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Marvell Armada 370/XP SoC timer handling.
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Lior Amsalem <alior@marvell.com>
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * Timer 0 is used as free-running clocksource, while timer 1 is
  15. * used as clock_event_device.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/timer.h>
  21. #include <linux/clockchips.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/of.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_address.h>
  26. #include <linux/irq.h>
  27. #include <linux/module.h>
  28. #include <asm/sched_clock.h>
  29. /*
  30. * Timer block registers.
  31. */
  32. #define TIMER_CTRL_OFF 0x0000
  33. #define TIMER0_EN 0x0001
  34. #define TIMER0_RELOAD_EN 0x0002
  35. #define TIMER0_25MHZ 0x0800
  36. #define TIMER0_DIV(div) ((div) << 19)
  37. #define TIMER1_EN 0x0004
  38. #define TIMER1_RELOAD_EN 0x0008
  39. #define TIMER1_25MHZ 0x1000
  40. #define TIMER1_DIV(div) ((div) << 22)
  41. #define TIMER_EVENTS_STATUS 0x0004
  42. #define TIMER0_CLR_MASK (~0x1)
  43. #define TIMER1_CLR_MASK (~0x100)
  44. #define TIMER0_RELOAD_OFF 0x0010
  45. #define TIMER0_VAL_OFF 0x0014
  46. #define TIMER1_RELOAD_OFF 0x0018
  47. #define TIMER1_VAL_OFF 0x001c
  48. /* Global timers are connected to the coherency fabric clock, and the
  49. below divider reduces their incrementing frequency. */
  50. #define TIMER_DIVIDER_SHIFT 5
  51. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  52. /*
  53. * SoC-specific data.
  54. */
  55. static void __iomem *timer_base;
  56. static int timer_irq;
  57. /*
  58. * Number of timer ticks per jiffy.
  59. */
  60. static u32 ticks_per_jiffy;
  61. static u32 notrace armada_370_xp_read_sched_clock(void)
  62. {
  63. return ~readl(timer_base + TIMER0_VAL_OFF);
  64. }
  65. /*
  66. * Clockevent handling.
  67. */
  68. static int
  69. armada_370_xp_clkevt_next_event(unsigned long delta,
  70. struct clock_event_device *dev)
  71. {
  72. u32 u;
  73. /*
  74. * Clear clockevent timer interrupt.
  75. */
  76. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  77. /*
  78. * Setup new clockevent timer value.
  79. */
  80. writel(delta, timer_base + TIMER1_VAL_OFF);
  81. /*
  82. * Enable the timer.
  83. */
  84. u = readl(timer_base + TIMER_CTRL_OFF);
  85. u = ((u & ~TIMER1_RELOAD_EN) | TIMER1_EN |
  86. TIMER1_DIV(TIMER_DIVIDER_SHIFT));
  87. writel(u, timer_base + TIMER_CTRL_OFF);
  88. return 0;
  89. }
  90. static void
  91. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  92. struct clock_event_device *dev)
  93. {
  94. u32 u;
  95. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  96. /*
  97. * Setup timer to fire at 1/HZ intervals.
  98. */
  99. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  100. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  101. /*
  102. * Enable timer.
  103. */
  104. u = readl(timer_base + TIMER_CTRL_OFF);
  105. writel((u | TIMER1_EN | TIMER1_RELOAD_EN |
  106. TIMER1_DIV(TIMER_DIVIDER_SHIFT)),
  107. timer_base + TIMER_CTRL_OFF);
  108. } else {
  109. /*
  110. * Disable timer.
  111. */
  112. u = readl(timer_base + TIMER_CTRL_OFF);
  113. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  114. /*
  115. * ACK pending timer interrupt.
  116. */
  117. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  118. }
  119. }
  120. static struct clock_event_device armada_370_xp_clkevt = {
  121. .name = "armada_370_xp_tick",
  122. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  123. .shift = 32,
  124. .rating = 300,
  125. .set_next_event = armada_370_xp_clkevt_next_event,
  126. .set_mode = armada_370_xp_clkevt_mode,
  127. };
  128. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  129. {
  130. /*
  131. * ACK timer interrupt and call event handler.
  132. */
  133. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  134. armada_370_xp_clkevt.event_handler(&armada_370_xp_clkevt);
  135. return IRQ_HANDLED;
  136. }
  137. static struct irqaction armada_370_xp_timer_irq = {
  138. .name = "armada_370_xp_tick",
  139. .flags = IRQF_DISABLED | IRQF_TIMER,
  140. .handler = armada_370_xp_timer_interrupt
  141. };
  142. void __init armada_370_xp_timer_init(void)
  143. {
  144. u32 u;
  145. struct device_node *np;
  146. unsigned int timer_clk;
  147. int ret;
  148. np = of_find_compatible_node(NULL, NULL, "marvell,armada-370-xp-timer");
  149. timer_base = of_iomap(np, 0);
  150. WARN_ON(!timer_base);
  151. if (of_find_property(np, "marvell,timer-25Mhz", NULL)) {
  152. /* The fixed 25MHz timer is available so let's use it */
  153. u = readl(timer_base + TIMER_CTRL_OFF);
  154. writel(u | TIMER0_25MHZ | TIMER1_25MHZ,
  155. timer_base + TIMER_CTRL_OFF);
  156. timer_clk = 25000000;
  157. } else {
  158. u32 clk = 0;
  159. ret = of_property_read_u32(np, "clock-frequency", &clk);
  160. WARN_ON(!clk || ret < 0);
  161. u = readl(timer_base + TIMER_CTRL_OFF);
  162. writel(u & ~(TIMER0_25MHZ | TIMER1_25MHZ),
  163. timer_base + TIMER_CTRL_OFF);
  164. timer_clk = clk / TIMER_DIVIDER;
  165. }
  166. /* We use timer 0 as clocksource, and timer 1 for
  167. clockevents */
  168. timer_irq = irq_of_parse_and_map(np, 1);
  169. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  170. /*
  171. * Set scale and timer for sched_clock.
  172. */
  173. setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
  174. /*
  175. * Setup free-running clocksource timer (interrupts
  176. * disabled).
  177. */
  178. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  179. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  180. u = readl(timer_base + TIMER_CTRL_OFF);
  181. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  182. TIMER0_DIV(TIMER_DIVIDER_SHIFT)), timer_base + TIMER_CTRL_OFF);
  183. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  184. "armada_370_xp_clocksource",
  185. timer_clk, 300, 32, clocksource_mmio_readl_down);
  186. /*
  187. * Setup clockevent timer (interrupt-driven).
  188. */
  189. setup_irq(timer_irq, &armada_370_xp_timer_irq);
  190. armada_370_xp_clkevt.cpumask = cpumask_of(0);
  191. clockevents_config_and_register(&armada_370_xp_clkevt,
  192. timer_clk, 1, 0xfffffffe);
  193. }