time-armada-370-xp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. static u32 enable_mask;
  76. /*
  77. * Number of timer ticks per jiffy.
  78. */
  79. static u32 ticks_per_jiffy;
  80. static struct clock_event_device __percpu *armada_370_xp_evt;
  81. static void local_timer_ctrl_clrset(u32 clr, u32 set)
  82. {
  83. writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,
  84. local_base + TIMER_CTRL_OFF);
  85. }
  86. static u64 notrace armada_370_xp_read_sched_clock(void)
  87. {
  88. return ~readl(timer_base + TIMER0_VAL_OFF);
  89. }
  90. /*
  91. * Clockevent handling.
  92. */
  93. static int
  94. armada_370_xp_clkevt_next_event(unsigned long delta,
  95. struct clock_event_device *dev)
  96. {
  97. /*
  98. * Clear clockevent timer interrupt.
  99. */
  100. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  101. /*
  102. * Setup new clockevent timer value.
  103. */
  104. writel(delta, local_base + TIMER0_VAL_OFF);
  105. /*
  106. * Enable the timer.
  107. */
  108. local_timer_ctrl_clrset(TIMER0_RELOAD_EN, enable_mask);
  109. return 0;
  110. }
  111. static void
  112. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  113. struct clock_event_device *dev)
  114. {
  115. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  116. /*
  117. * Setup timer to fire at 1/HZ intervals.
  118. */
  119. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  120. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  121. /*
  122. * Enable timer.
  123. */
  124. local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | enable_mask);
  125. } else {
  126. /*
  127. * Disable timer.
  128. */
  129. local_timer_ctrl_clrset(TIMER0_EN, 0);
  130. /*
  131. * ACK pending timer interrupt.
  132. */
  133. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  134. }
  135. }
  136. static int armada_370_xp_clkevt_irq;
  137. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  138. {
  139. /*
  140. * ACK timer interrupt and call event handler.
  141. */
  142. struct clock_event_device *evt = dev_id;
  143. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  144. evt->event_handler(evt);
  145. return IRQ_HANDLED;
  146. }
  147. /*
  148. * Setup the local clock events for a CPU.
  149. */
  150. static int armada_370_xp_timer_setup(struct clock_event_device *evt)
  151. {
  152. u32 clr = 0, set = 0;
  153. int cpu = smp_processor_id();
  154. if (timer25Mhz)
  155. set = TIMER0_25MHZ;
  156. else
  157. clr = TIMER0_25MHZ;
  158. local_timer_ctrl_clrset(clr, set);
  159. evt->name = "armada_370_xp_per_cpu_tick",
  160. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  161. CLOCK_EVT_FEAT_PERIODIC;
  162. evt->shift = 32,
  163. evt->rating = 300,
  164. evt->set_next_event = armada_370_xp_clkevt_next_event,
  165. evt->set_mode = armada_370_xp_clkevt_mode,
  166. evt->irq = armada_370_xp_clkevt_irq;
  167. evt->cpumask = cpumask_of(cpu);
  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 int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  178. unsigned long action, void *hcpu)
  179. {
  180. /*
  181. * Grab cpu pointer in each case to avoid spurious
  182. * preemptible warnings
  183. */
  184. switch (action & ~CPU_TASKS_FROZEN) {
  185. case CPU_STARTING:
  186. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  187. break;
  188. case CPU_DYING:
  189. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  190. break;
  191. }
  192. return NOTIFY_OK;
  193. }
  194. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  195. .notifier_call = armada_370_xp_timer_cpu_notify,
  196. };
  197. static void __init armada_370_xp_timer_common_init(struct device_node *np)
  198. {
  199. u32 clr = 0, set = 0;
  200. int res;
  201. timer_base = of_iomap(np, 0);
  202. WARN_ON(!timer_base);
  203. local_base = of_iomap(np, 1);
  204. if (timer25Mhz) {
  205. set = TIMER0_25MHZ;
  206. enable_mask = TIMER0_EN;
  207. } else {
  208. clr = TIMER0_25MHZ;
  209. enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);
  210. }
  211. atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);
  212. local_timer_ctrl_clrset(clr, set);
  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. * Setup free-running clocksource timer (interrupts
  221. * disabled).
  222. */
  223. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  224. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  225. atomic_io_modify(timer_base + TIMER_CTRL_OFF,
  226. TIMER0_RELOAD_EN | enable_mask,
  227. TIMER0_RELOAD_EN | enable_mask);
  228. /*
  229. * Set scale and timer for sched_clock.
  230. */
  231. sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);
  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_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  236. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  237. /*
  238. * Setup clockevent timer (interrupt-driven).
  239. */
  240. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  241. armada_370_xp_timer_interrupt,
  242. "armada_370_xp_per_cpu_tick",
  243. armada_370_xp_evt);
  244. /* Immediately configure the timer on the boot CPU */
  245. if (!res)
  246. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  247. }
  248. static void __init armada_xp_timer_init(struct device_node *np)
  249. {
  250. struct clk *clk = of_clk_get_by_name(np, "fixed");
  251. /* The 25Mhz fixed clock is mandatory, and must always be available */
  252. BUG_ON(IS_ERR(clk));
  253. timer_clk = clk_get_rate(clk);
  254. armada_370_xp_timer_common_init(np);
  255. }
  256. CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  257. armada_xp_timer_init);
  258. static void __init armada_370_timer_init(struct device_node *np)
  259. {
  260. struct clk *clk = of_clk_get(np, 0);
  261. BUG_ON(IS_ERR(clk));
  262. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  263. timer25Mhz = false;
  264. armada_370_xp_timer_common_init(np);
  265. }
  266. CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  267. armada_370_timer_init);