time-armada-370-xp.c 7.7 KB

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