time-armada-370-xp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <linux/sched_clock.h>
  30. #include <linux/percpu.h>
  31. #include <linux/time-armada-370-xp.h>
  32. #include <asm/localtimer.h>
  33. /*
  34. * Timer block registers.
  35. */
  36. #define TIMER_CTRL_OFF 0x0000
  37. #define TIMER0_EN 0x0001
  38. #define TIMER0_RELOAD_EN 0x0002
  39. #define TIMER0_25MHZ 0x0800
  40. #define TIMER0_DIV(div) ((div) << 19)
  41. #define TIMER1_EN 0x0004
  42. #define TIMER1_RELOAD_EN 0x0008
  43. #define TIMER1_25MHZ 0x1000
  44. #define TIMER1_DIV(div) ((div) << 22)
  45. #define TIMER_EVENTS_STATUS 0x0004
  46. #define TIMER0_CLR_MASK (~0x1)
  47. #define TIMER1_CLR_MASK (~0x100)
  48. #define TIMER0_RELOAD_OFF 0x0010
  49. #define TIMER0_VAL_OFF 0x0014
  50. #define TIMER1_RELOAD_OFF 0x0018
  51. #define TIMER1_VAL_OFF 0x001c
  52. #define LCL_TIMER_EVENTS_STATUS 0x0028
  53. /* Global timers are connected to the coherency fabric clock, and the
  54. below divider reduces their incrementing frequency. */
  55. #define TIMER_DIVIDER_SHIFT 5
  56. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  57. /*
  58. * SoC-specific data.
  59. */
  60. static void __iomem *timer_base, *local_base;
  61. static unsigned int timer_clk;
  62. static bool timer25Mhz = true;
  63. /*
  64. * Number of timer ticks per jiffy.
  65. */
  66. static u32 ticks_per_jiffy;
  67. static struct clock_event_device __percpu **percpu_armada_370_xp_evt;
  68. static u32 notrace armada_370_xp_read_sched_clock(void)
  69. {
  70. return ~readl(timer_base + TIMER0_VAL_OFF);
  71. }
  72. /*
  73. * Clockevent handling.
  74. */
  75. static int
  76. armada_370_xp_clkevt_next_event(unsigned long delta,
  77. struct clock_event_device *dev)
  78. {
  79. u32 u;
  80. /*
  81. * Clear clockevent timer interrupt.
  82. */
  83. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  84. /*
  85. * Setup new clockevent timer value.
  86. */
  87. writel(delta, local_base + TIMER0_VAL_OFF);
  88. /*
  89. * Enable the timer.
  90. */
  91. u = readl(local_base + TIMER_CTRL_OFF);
  92. u = ((u & ~TIMER0_RELOAD_EN) | TIMER0_EN |
  93. TIMER0_DIV(TIMER_DIVIDER_SHIFT));
  94. writel(u, local_base + TIMER_CTRL_OFF);
  95. return 0;
  96. }
  97. static void
  98. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  99. struct clock_event_device *dev)
  100. {
  101. u32 u;
  102. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  103. /*
  104. * Setup timer to fire at 1/HZ intervals.
  105. */
  106. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  107. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  108. /*
  109. * Enable timer.
  110. */
  111. u = readl(local_base + TIMER_CTRL_OFF);
  112. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  113. TIMER0_DIV(TIMER_DIVIDER_SHIFT)),
  114. local_base + TIMER_CTRL_OFF);
  115. } else {
  116. /*
  117. * Disable timer.
  118. */
  119. u = readl(local_base + TIMER_CTRL_OFF);
  120. writel(u & ~TIMER0_EN, local_base + TIMER_CTRL_OFF);
  121. /*
  122. * ACK pending timer interrupt.
  123. */
  124. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  125. }
  126. }
  127. static struct clock_event_device armada_370_xp_clkevt = {
  128. .name = "armada_370_xp_per_cpu_tick",
  129. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  130. .shift = 32,
  131. .rating = 300,
  132. .set_next_event = armada_370_xp_clkevt_next_event,
  133. .set_mode = armada_370_xp_clkevt_mode,
  134. };
  135. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  136. {
  137. /*
  138. * ACK timer interrupt and call event handler.
  139. */
  140. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  141. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  142. evt->event_handler(evt);
  143. return IRQ_HANDLED;
  144. }
  145. /*
  146. * Setup the local clock events for a CPU.
  147. */
  148. static int __cpuinit armada_370_xp_timer_setup(struct clock_event_device *evt)
  149. {
  150. u32 u;
  151. int cpu = smp_processor_id();
  152. /* Use existing clock_event for cpu 0 */
  153. if (!smp_processor_id())
  154. return 0;
  155. u = readl(local_base + TIMER_CTRL_OFF);
  156. if (timer25Mhz)
  157. writel(u | TIMER0_25MHZ, local_base + TIMER_CTRL_OFF);
  158. else
  159. writel(u & ~TIMER0_25MHZ, local_base + TIMER_CTRL_OFF);
  160. evt->name = armada_370_xp_clkevt.name;
  161. evt->irq = armada_370_xp_clkevt.irq;
  162. evt->features = armada_370_xp_clkevt.features;
  163. evt->shift = armada_370_xp_clkevt.shift;
  164. evt->rating = armada_370_xp_clkevt.rating,
  165. evt->set_next_event = armada_370_xp_clkevt_next_event,
  166. evt->set_mode = armada_370_xp_clkevt_mode,
  167. evt->cpumask = cpumask_of(cpu);
  168. *__this_cpu_ptr(percpu_armada_370_xp_evt) = evt;
  169. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  170. enable_percpu_irq(evt->irq, 0);
  171. return 0;
  172. }
  173. static void armada_370_xp_timer_stop(struct clock_event_device *evt)
  174. {
  175. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  176. disable_percpu_irq(evt->irq);
  177. }
  178. static struct local_timer_ops armada_370_xp_local_timer_ops __cpuinitdata = {
  179. .setup = armada_370_xp_timer_setup,
  180. .stop = armada_370_xp_timer_stop,
  181. };
  182. void __init armada_370_xp_timer_init(void)
  183. {
  184. u32 u;
  185. struct device_node *np;
  186. int res;
  187. np = of_find_compatible_node(NULL, NULL, "marvell,armada-370-xp-timer");
  188. timer_base = of_iomap(np, 0);
  189. WARN_ON(!timer_base);
  190. local_base = of_iomap(np, 1);
  191. if (of_find_property(np, "marvell,timer-25Mhz", NULL)) {
  192. /* The fixed 25MHz timer is available so let's use it */
  193. u = readl(local_base + TIMER_CTRL_OFF);
  194. writel(u | TIMER0_25MHZ,
  195. local_base + TIMER_CTRL_OFF);
  196. u = readl(timer_base + TIMER_CTRL_OFF);
  197. writel(u | TIMER0_25MHZ,
  198. timer_base + TIMER_CTRL_OFF);
  199. timer_clk = 25000000;
  200. } else {
  201. unsigned long rate = 0;
  202. struct clk *clk = of_clk_get(np, 0);
  203. WARN_ON(IS_ERR(clk));
  204. rate = clk_get_rate(clk);
  205. u = readl(local_base + TIMER_CTRL_OFF);
  206. writel(u & ~(TIMER0_25MHZ),
  207. local_base + TIMER_CTRL_OFF);
  208. u = readl(timer_base + TIMER_CTRL_OFF);
  209. writel(u & ~(TIMER0_25MHZ),
  210. timer_base + TIMER_CTRL_OFF);
  211. timer_clk = rate / TIMER_DIVIDER;
  212. timer25Mhz = false;
  213. }
  214. /*
  215. * We use timer 0 as clocksource, and private(local) timer 0
  216. * for clockevents
  217. */
  218. armada_370_xp_clkevt.irq = irq_of_parse_and_map(np, 4);
  219. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  220. /*
  221. * Set scale and timer for sched_clock.
  222. */
  223. setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
  224. /*
  225. * Setup free-running clocksource timer (interrupts
  226. * disabled).
  227. */
  228. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  229. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  230. u = readl(timer_base + TIMER_CTRL_OFF);
  231. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  232. TIMER0_DIV(TIMER_DIVIDER_SHIFT)), timer_base + TIMER_CTRL_OFF);
  233. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  234. "armada_370_xp_clocksource",
  235. timer_clk, 300, 32, clocksource_mmio_readl_down);
  236. /* Register the clockevent on the private timer of CPU 0 */
  237. armada_370_xp_clkevt.cpumask = cpumask_of(0);
  238. clockevents_config_and_register(&armada_370_xp_clkevt,
  239. timer_clk, 1, 0xfffffffe);
  240. percpu_armada_370_xp_evt = alloc_percpu(struct clock_event_device *);
  241. /*
  242. * Setup clockevent timer (interrupt-driven).
  243. */
  244. *__this_cpu_ptr(percpu_armada_370_xp_evt) = &armada_370_xp_clkevt;
  245. res = request_percpu_irq(armada_370_xp_clkevt.irq,
  246. armada_370_xp_timer_interrupt,
  247. armada_370_xp_clkevt.name,
  248. percpu_armada_370_xp_evt);
  249. if (!res) {
  250. enable_percpu_irq(armada_370_xp_clkevt.irq, 0);
  251. #ifdef CONFIG_LOCAL_TIMERS
  252. local_timer_register(&armada_370_xp_local_timer_ops);
  253. #endif
  254. }
  255. }