time-armada-370-xp.c 8.2 KB

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