time-armada-370-xp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/cpu.h>
  35. #include <linux/timer.h>
  36. #include <linux/clockchips.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/of.h>
  39. #include <linux/of_irq.h>
  40. #include <linux/of_address.h>
  41. #include <linux/irq.h>
  42. #include <linux/module.h>
  43. #include <linux/sched_clock.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 *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 int armada_370_xp_clkevt_irq;
  144. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  145. {
  146. /*
  147. * ACK timer interrupt and call event handler.
  148. */
  149. struct clock_event_device *evt = dev_id;
  150. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  151. evt->event_handler(evt);
  152. return IRQ_HANDLED;
  153. }
  154. /*
  155. * Setup the local clock events for a CPU.
  156. */
  157. static int armada_370_xp_timer_setup(struct clock_event_device *evt)
  158. {
  159. u32 clr = 0, set = 0;
  160. int cpu = smp_processor_id();
  161. if (timer25Mhz)
  162. set = TIMER0_25MHZ;
  163. else
  164. clr = TIMER0_25MHZ;
  165. local_timer_ctrl_clrset(clr, set);
  166. evt->name = "armada_370_xp_per_cpu_tick",
  167. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  168. CLOCK_EVT_FEAT_PERIODIC;
  169. evt->shift = 32,
  170. evt->rating = 300,
  171. evt->set_next_event = armada_370_xp_clkevt_next_event,
  172. evt->set_mode = armada_370_xp_clkevt_mode,
  173. evt->irq = armada_370_xp_clkevt_irq;
  174. evt->cpumask = cpumask_of(cpu);
  175. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  176. enable_percpu_irq(evt->irq, 0);
  177. return 0;
  178. }
  179. static void armada_370_xp_timer_stop(struct clock_event_device *evt)
  180. {
  181. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  182. disable_percpu_irq(evt->irq);
  183. }
  184. static int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  185. unsigned long action, void *hcpu)
  186. {
  187. /*
  188. * Grab cpu pointer in each case to avoid spurious
  189. * preemptible warnings
  190. */
  191. switch (action & ~CPU_TASKS_FROZEN) {
  192. case CPU_STARTING:
  193. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  194. break;
  195. case CPU_DYING:
  196. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  197. break;
  198. }
  199. return NOTIFY_OK;
  200. }
  201. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  202. .notifier_call = armada_370_xp_timer_cpu_notify,
  203. };
  204. static void __init armada_370_xp_timer_common_init(struct device_node *np)
  205. {
  206. u32 clr = 0, set = 0;
  207. int res;
  208. timer_base = of_iomap(np, 0);
  209. WARN_ON(!timer_base);
  210. local_base = of_iomap(np, 1);
  211. if (timer25Mhz)
  212. set = TIMER0_25MHZ;
  213. else
  214. clr = TIMER0_25MHZ;
  215. timer_ctrl_clrset(clr, set);
  216. local_timer_ctrl_clrset(clr, set);
  217. /*
  218. * We use timer 0 as clocksource, and private(local) timer 0
  219. * for clockevents
  220. */
  221. armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);
  222. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  223. /*
  224. * Set scale and timer for sched_clock.
  225. */
  226. setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
  227. /*
  228. * Setup free-running clocksource timer (interrupts
  229. * disabled).
  230. */
  231. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  232. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  233. timer_ctrl_clrset(0, TIMER0_EN | TIMER0_RELOAD_EN |
  234. TIMER0_DIV(TIMER_DIVIDER_SHIFT));
  235. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  236. "armada_370_xp_clocksource",
  237. timer_clk, 300, 32, clocksource_mmio_readl_down);
  238. register_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  239. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  240. /*
  241. * Setup clockevent timer (interrupt-driven).
  242. */
  243. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  244. armada_370_xp_timer_interrupt,
  245. "armada_370_xp_per_cpu_tick",
  246. armada_370_xp_evt);
  247. /* Immediately configure the timer on the boot CPU */
  248. if (!res)
  249. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  250. }
  251. static void __init armada_xp_timer_init(struct device_node *np)
  252. {
  253. struct clk *clk = of_clk_get_by_name(np, "fixed");
  254. /* The 25Mhz fixed clock is mandatory, and must always be available */
  255. BUG_ON(IS_ERR(clk));
  256. timer_clk = clk_get_rate(clk);
  257. armada_370_xp_timer_common_init(np);
  258. }
  259. CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  260. armada_xp_timer_init);
  261. static void __init armada_370_timer_init(struct device_node *np)
  262. {
  263. struct clk *clk = of_clk_get(np, 0);
  264. BUG_ON(IS_ERR(clk));
  265. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  266. timer25Mhz = false;
  267. armada_370_xp_timer_common_init(np);
  268. }
  269. CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  270. armada_370_timer_init);