time-armada-370-xp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/clk.h>
  21. #include <linux/timer.h>
  22. #include <linux/clockchips.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/of.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_address.h>
  27. #include <linux/irq.h>
  28. #include <linux/module.h>
  29. #include <asm/sched_clock.h>
  30. /*
  31. * Timer block registers.
  32. */
  33. #define TIMER_CTRL_OFF 0x0000
  34. #define TIMER0_EN 0x0001
  35. #define TIMER0_RELOAD_EN 0x0002
  36. #define TIMER0_25MHZ 0x0800
  37. #define TIMER0_DIV(div) ((div) << 19)
  38. #define TIMER1_EN 0x0004
  39. #define TIMER1_RELOAD_EN 0x0008
  40. #define TIMER1_25MHZ 0x1000
  41. #define TIMER1_DIV(div) ((div) << 22)
  42. #define TIMER_EVENTS_STATUS 0x0004
  43. #define TIMER0_CLR_MASK (~0x1)
  44. #define TIMER1_CLR_MASK (~0x100)
  45. #define TIMER0_RELOAD_OFF 0x0010
  46. #define TIMER0_VAL_OFF 0x0014
  47. #define TIMER1_RELOAD_OFF 0x0018
  48. #define TIMER1_VAL_OFF 0x001c
  49. /* Global timers are connected to the coherency fabric clock, and the
  50. below divider reduces their incrementing frequency. */
  51. #define TIMER_DIVIDER_SHIFT 5
  52. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  53. /*
  54. * SoC-specific data.
  55. */
  56. static void __iomem *timer_base;
  57. static int timer_irq;
  58. /*
  59. * Number of timer ticks per jiffy.
  60. */
  61. static u32 ticks_per_jiffy;
  62. static u32 notrace armada_370_xp_read_sched_clock(void)
  63. {
  64. return ~readl(timer_base + TIMER0_VAL_OFF);
  65. }
  66. /*
  67. * Clockevent handling.
  68. */
  69. static int
  70. armada_370_xp_clkevt_next_event(unsigned long delta,
  71. struct clock_event_device *dev)
  72. {
  73. u32 u;
  74. /*
  75. * Clear clockevent timer interrupt.
  76. */
  77. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  78. /*
  79. * Setup new clockevent timer value.
  80. */
  81. writel(delta, timer_base + TIMER1_VAL_OFF);
  82. /*
  83. * Enable the timer.
  84. */
  85. u = readl(timer_base + TIMER_CTRL_OFF);
  86. u = ((u & ~TIMER1_RELOAD_EN) | TIMER1_EN |
  87. TIMER1_DIV(TIMER_DIVIDER_SHIFT));
  88. writel(u, timer_base + TIMER_CTRL_OFF);
  89. return 0;
  90. }
  91. static void
  92. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  93. struct clock_event_device *dev)
  94. {
  95. u32 u;
  96. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  97. /*
  98. * Setup timer to fire at 1/HZ intervals.
  99. */
  100. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  101. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  102. /*
  103. * Enable timer.
  104. */
  105. u = readl(timer_base + TIMER_CTRL_OFF);
  106. writel((u | TIMER1_EN | TIMER1_RELOAD_EN |
  107. TIMER1_DIV(TIMER_DIVIDER_SHIFT)),
  108. timer_base + TIMER_CTRL_OFF);
  109. } else {
  110. /*
  111. * Disable timer.
  112. */
  113. u = readl(timer_base + TIMER_CTRL_OFF);
  114. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  115. /*
  116. * ACK pending timer interrupt.
  117. */
  118. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  119. }
  120. }
  121. static struct clock_event_device armada_370_xp_clkevt = {
  122. .name = "armada_370_xp_tick",
  123. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  124. .shift = 32,
  125. .rating = 300,
  126. .set_next_event = armada_370_xp_clkevt_next_event,
  127. .set_mode = armada_370_xp_clkevt_mode,
  128. };
  129. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  130. {
  131. /*
  132. * ACK timer interrupt and call event handler.
  133. */
  134. writel(TIMER1_CLR_MASK, timer_base + TIMER_EVENTS_STATUS);
  135. armada_370_xp_clkevt.event_handler(&armada_370_xp_clkevt);
  136. return IRQ_HANDLED;
  137. }
  138. static struct irqaction armada_370_xp_timer_irq = {
  139. .name = "armada_370_xp_tick",
  140. .flags = IRQF_DISABLED | IRQF_TIMER,
  141. .handler = armada_370_xp_timer_interrupt
  142. };
  143. void __init armada_370_xp_timer_init(void)
  144. {
  145. u32 u;
  146. struct device_node *np;
  147. unsigned int timer_clk;
  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. unsigned long rate = 0;
  159. struct clk *clk = of_clk_get(np, 0);
  160. WARN_ON(IS_ERR(clk));
  161. rate = clk_get_rate(clk);
  162. u = readl(timer_base + TIMER_CTRL_OFF);
  163. writel(u & ~(TIMER0_25MHZ | TIMER1_25MHZ),
  164. timer_base + TIMER_CTRL_OFF);
  165. timer_clk = rate / TIMER_DIVIDER;
  166. }
  167. /* We use timer 0 as clocksource, and timer 1 for
  168. clockevents */
  169. timer_irq = irq_of_parse_and_map(np, 1);
  170. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  171. /*
  172. * Set scale and timer for sched_clock.
  173. */
  174. setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
  175. /*
  176. * Setup free-running clocksource timer (interrupts
  177. * disabled).
  178. */
  179. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  180. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  181. u = readl(timer_base + TIMER_CTRL_OFF);
  182. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  183. TIMER0_DIV(TIMER_DIVIDER_SHIFT)), timer_base + TIMER_CTRL_OFF);
  184. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  185. "armada_370_xp_clocksource",
  186. timer_clk, 300, 32, clocksource_mmio_readl_down);
  187. /*
  188. * Setup clockevent timer (interrupt-driven).
  189. */
  190. setup_irq(timer_irq, &armada_370_xp_timer_irq);
  191. armada_370_xp_clkevt.cpumask = cpumask_of(0);
  192. clockevents_config_and_register(&armada_370_xp_clkevt,
  193. timer_clk, 1, 0xfffffffe);
  194. }