time-armada-370-xp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include <linux/syscore_ops.h>
  46. /*
  47. * Timer block registers.
  48. */
  49. #define TIMER_CTRL_OFF 0x0000
  50. #define TIMER0_EN BIT(0)
  51. #define TIMER0_RELOAD_EN BIT(1)
  52. #define TIMER0_25MHZ BIT(11)
  53. #define TIMER0_DIV(div) ((div) << 19)
  54. #define TIMER1_EN BIT(2)
  55. #define TIMER1_RELOAD_EN BIT(3)
  56. #define TIMER1_25MHZ BIT(12)
  57. #define TIMER1_DIV(div) ((div) << 22)
  58. #define TIMER_EVENTS_STATUS 0x0004
  59. #define TIMER0_CLR_MASK (~0x1)
  60. #define TIMER1_CLR_MASK (~0x100)
  61. #define TIMER0_RELOAD_OFF 0x0010
  62. #define TIMER0_VAL_OFF 0x0014
  63. #define TIMER1_RELOAD_OFF 0x0018
  64. #define TIMER1_VAL_OFF 0x001c
  65. #define LCL_TIMER_EVENTS_STATUS 0x0028
  66. /* Global timers are connected to the coherency fabric clock, and the
  67. below divider reduces their incrementing frequency. */
  68. #define TIMER_DIVIDER_SHIFT 5
  69. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  70. /*
  71. * SoC-specific data.
  72. */
  73. static void __iomem *timer_base, *local_base;
  74. static unsigned int timer_clk;
  75. static bool timer25Mhz = true;
  76. static u32 enable_mask;
  77. /*
  78. * Number of timer ticks per jiffy.
  79. */
  80. static u32 ticks_per_jiffy;
  81. static struct clock_event_device __percpu *armada_370_xp_evt;
  82. static void local_timer_ctrl_clrset(u32 clr, u32 set)
  83. {
  84. writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,
  85. local_base + TIMER_CTRL_OFF);
  86. }
  87. static u64 notrace armada_370_xp_read_sched_clock(void)
  88. {
  89. return ~readl(timer_base + TIMER0_VAL_OFF);
  90. }
  91. /*
  92. * Clockevent handling.
  93. */
  94. static int
  95. armada_370_xp_clkevt_next_event(unsigned long delta,
  96. struct clock_event_device *dev)
  97. {
  98. /*
  99. * Clear clockevent timer interrupt.
  100. */
  101. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  102. /*
  103. * Setup new clockevent timer value.
  104. */
  105. writel(delta, local_base + TIMER0_VAL_OFF);
  106. /*
  107. * Enable the timer.
  108. */
  109. local_timer_ctrl_clrset(TIMER0_RELOAD_EN, enable_mask);
  110. return 0;
  111. }
  112. static int armada_370_xp_clkevt_shutdown(struct clock_event_device *evt)
  113. {
  114. /*
  115. * Disable timer.
  116. */
  117. local_timer_ctrl_clrset(TIMER0_EN, 0);
  118. /*
  119. * ACK pending timer interrupt.
  120. */
  121. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  122. return 0;
  123. }
  124. static int armada_370_xp_clkevt_set_periodic(struct clock_event_device *evt)
  125. {
  126. /*
  127. * Setup timer to fire at 1/HZ intervals.
  128. */
  129. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  130. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  131. /*
  132. * Enable timer.
  133. */
  134. local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | enable_mask);
  135. return 0;
  136. }
  137. static int armada_370_xp_clkevt_irq;
  138. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  139. {
  140. /*
  141. * ACK timer interrupt and call event handler.
  142. */
  143. struct clock_event_device *evt = dev_id;
  144. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  145. evt->event_handler(evt);
  146. return IRQ_HANDLED;
  147. }
  148. /*
  149. * Setup the local clock events for a CPU.
  150. */
  151. static int armada_370_xp_timer_setup(struct clock_event_device *evt)
  152. {
  153. u32 clr = 0, set = 0;
  154. int cpu = smp_processor_id();
  155. if (timer25Mhz)
  156. set = TIMER0_25MHZ;
  157. else
  158. clr = TIMER0_25MHZ;
  159. local_timer_ctrl_clrset(clr, set);
  160. evt->name = "armada_370_xp_per_cpu_tick",
  161. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  162. CLOCK_EVT_FEAT_PERIODIC;
  163. evt->shift = 32,
  164. evt->rating = 300,
  165. evt->set_next_event = armada_370_xp_clkevt_next_event,
  166. evt->set_state_shutdown = armada_370_xp_clkevt_shutdown;
  167. evt->set_state_periodic = armada_370_xp_clkevt_set_periodic;
  168. evt->set_state_oneshot = armada_370_xp_clkevt_shutdown;
  169. evt->tick_resume = armada_370_xp_clkevt_shutdown;
  170. evt->irq = armada_370_xp_clkevt_irq;
  171. evt->cpumask = cpumask_of(cpu);
  172. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  173. enable_percpu_irq(evt->irq, 0);
  174. return 0;
  175. }
  176. static void armada_370_xp_timer_stop(struct clock_event_device *evt)
  177. {
  178. evt->set_state_shutdown(evt);
  179. disable_percpu_irq(evt->irq);
  180. }
  181. static int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  182. unsigned long action, void *hcpu)
  183. {
  184. /*
  185. * Grab cpu pointer in each case to avoid spurious
  186. * preemptible warnings
  187. */
  188. switch (action & ~CPU_TASKS_FROZEN) {
  189. case CPU_STARTING:
  190. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  191. break;
  192. case CPU_DYING:
  193. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  194. break;
  195. }
  196. return NOTIFY_OK;
  197. }
  198. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  199. .notifier_call = armada_370_xp_timer_cpu_notify,
  200. };
  201. static u32 timer0_ctrl_reg, timer0_local_ctrl_reg;
  202. static int armada_370_xp_timer_suspend(void)
  203. {
  204. timer0_ctrl_reg = readl(timer_base + TIMER_CTRL_OFF);
  205. timer0_local_ctrl_reg = readl(local_base + TIMER_CTRL_OFF);
  206. return 0;
  207. }
  208. static void armada_370_xp_timer_resume(void)
  209. {
  210. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  211. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  212. writel(timer0_ctrl_reg, timer_base + TIMER_CTRL_OFF);
  213. writel(timer0_local_ctrl_reg, local_base + TIMER_CTRL_OFF);
  214. }
  215. struct syscore_ops armada_370_xp_timer_syscore_ops = {
  216. .suspend = armada_370_xp_timer_suspend,
  217. .resume = armada_370_xp_timer_resume,
  218. };
  219. static void __init armada_370_xp_timer_common_init(struct device_node *np)
  220. {
  221. u32 clr = 0, set = 0;
  222. int res;
  223. timer_base = of_iomap(np, 0);
  224. WARN_ON(!timer_base);
  225. local_base = of_iomap(np, 1);
  226. if (timer25Mhz) {
  227. set = TIMER0_25MHZ;
  228. enable_mask = TIMER0_EN;
  229. } else {
  230. clr = TIMER0_25MHZ;
  231. enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);
  232. }
  233. atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);
  234. local_timer_ctrl_clrset(clr, set);
  235. /*
  236. * We use timer 0 as clocksource, and private(local) timer 0
  237. * for clockevents
  238. */
  239. armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);
  240. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  241. /*
  242. * Setup free-running clocksource timer (interrupts
  243. * disabled).
  244. */
  245. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  246. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  247. atomic_io_modify(timer_base + TIMER_CTRL_OFF,
  248. TIMER0_RELOAD_EN | enable_mask,
  249. TIMER0_RELOAD_EN | enable_mask);
  250. /*
  251. * Set scale and timer for sched_clock.
  252. */
  253. sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);
  254. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  255. "armada_370_xp_clocksource",
  256. timer_clk, 300, 32, clocksource_mmio_readl_down);
  257. register_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  258. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  259. /*
  260. * Setup clockevent timer (interrupt-driven).
  261. */
  262. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  263. armada_370_xp_timer_interrupt,
  264. "armada_370_xp_per_cpu_tick",
  265. armada_370_xp_evt);
  266. /* Immediately configure the timer on the boot CPU */
  267. if (!res)
  268. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  269. register_syscore_ops(&armada_370_xp_timer_syscore_ops);
  270. }
  271. static void __init armada_xp_timer_init(struct device_node *np)
  272. {
  273. struct clk *clk = of_clk_get_by_name(np, "fixed");
  274. /* The 25Mhz fixed clock is mandatory, and must always be available */
  275. BUG_ON(IS_ERR(clk));
  276. clk_prepare_enable(clk);
  277. timer_clk = clk_get_rate(clk);
  278. armada_370_xp_timer_common_init(np);
  279. }
  280. CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  281. armada_xp_timer_init);
  282. static void __init armada_375_timer_init(struct device_node *np)
  283. {
  284. struct clk *clk;
  285. clk = of_clk_get_by_name(np, "fixed");
  286. if (!IS_ERR(clk)) {
  287. clk_prepare_enable(clk);
  288. timer_clk = clk_get_rate(clk);
  289. } else {
  290. /*
  291. * This fallback is required in order to retain proper
  292. * devicetree backwards compatibility.
  293. */
  294. clk = of_clk_get(np, 0);
  295. /* Must have at least a clock */
  296. BUG_ON(IS_ERR(clk));
  297. clk_prepare_enable(clk);
  298. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  299. timer25Mhz = false;
  300. }
  301. armada_370_xp_timer_common_init(np);
  302. }
  303. CLOCKSOURCE_OF_DECLARE(armada_375, "marvell,armada-375-timer",
  304. armada_375_timer_init);
  305. static void __init armada_370_timer_init(struct device_node *np)
  306. {
  307. struct clk *clk = of_clk_get(np, 0);
  308. BUG_ON(IS_ERR(clk));
  309. clk_prepare_enable(clk);
  310. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  311. timer25Mhz = false;
  312. armada_370_xp_timer_common_init(np);
  313. }
  314. CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  315. armada_370_timer_init);