timer-armada-370-xp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Marvell Armada 370/XP SoC timer handling.
  4. *
  5. * Copyright (C) 2012 Marvell
  6. *
  7. * Lior Amsalem <[email protected]>
  8. * Gregory CLEMENT <[email protected]>
  9. * Thomas Petazzoni <[email protected]>
  10. *
  11. * Timer 0 is used as free-running clocksource, while timer 1 is
  12. * used as clock_event_device.
  13. *
  14. * ---
  15. * Clocksource driver for Armada 370 and Armada XP SoC.
  16. * This driver implements one compatible string for each SoC, given
  17. * each has its own characteristics:
  18. *
  19. * * Armada 370 has no 25 MHz fixed timer.
  20. *
  21. * * Armada XP cannot work properly without such 25 MHz fixed timer as
  22. * doing otherwise leads to using a clocksource whose frequency varies
  23. * when doing cpufreq frequency changes.
  24. *
  25. * See Documentation/devicetree/bindings/timer/marvell,armada-370-xp-timer.txt
  26. */
  27. #include <linux/init.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/kernel.h>
  30. #include <linux/clk.h>
  31. #include <linux/cpu.h>
  32. #include <linux/timer.h>
  33. #include <linux/clockchips.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/of.h>
  36. #include <linux/of_irq.h>
  37. #include <linux/of_address.h>
  38. #include <linux/irq.h>
  39. #include <linux/module.h>
  40. #include <linux/sched_clock.h>
  41. #include <linux/percpu.h>
  42. #include <linux/syscore_ops.h>
  43. #include <asm/delay.h>
  44. /*
  45. * Timer block registers.
  46. */
  47. #define TIMER_CTRL_OFF 0x0000
  48. #define TIMER0_EN BIT(0)
  49. #define TIMER0_RELOAD_EN BIT(1)
  50. #define TIMER0_25MHZ BIT(11)
  51. #define TIMER0_DIV(div) ((div) << 19)
  52. #define TIMER1_EN BIT(2)
  53. #define TIMER1_RELOAD_EN BIT(3)
  54. #define TIMER1_25MHZ BIT(12)
  55. #define TIMER1_DIV(div) ((div) << 22)
  56. #define TIMER_EVENTS_STATUS 0x0004
  57. #define TIMER0_CLR_MASK (~0x1)
  58. #define TIMER1_CLR_MASK (~0x100)
  59. #define TIMER0_RELOAD_OFF 0x0010
  60. #define TIMER0_VAL_OFF 0x0014
  61. #define TIMER1_RELOAD_OFF 0x0018
  62. #define TIMER1_VAL_OFF 0x001c
  63. #define LCL_TIMER_EVENTS_STATUS 0x0028
  64. /* Global timers are connected to the coherency fabric clock, and the
  65. below divider reduces their incrementing frequency. */
  66. #define TIMER_DIVIDER_SHIFT 5
  67. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  68. /*
  69. * SoC-specific data.
  70. */
  71. static void __iomem *timer_base, *local_base;
  72. static unsigned int timer_clk;
  73. static bool timer25Mhz = true;
  74. static u32 enable_mask;
  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 local_timer_ctrl_clrset(u32 clr, u32 set)
  81. {
  82. writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,
  83. local_base + TIMER_CTRL_OFF);
  84. }
  85. static u64 notrace armada_370_xp_read_sched_clock(void)
  86. {
  87. return ~readl(timer_base + TIMER0_VAL_OFF);
  88. }
  89. /*
  90. * Clockevent handling.
  91. */
  92. static int
  93. armada_370_xp_clkevt_next_event(unsigned long delta,
  94. struct clock_event_device *dev)
  95. {
  96. /*
  97. * Clear clockevent timer interrupt.
  98. */
  99. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  100. /*
  101. * Setup new clockevent timer value.
  102. */
  103. writel(delta, local_base + TIMER0_VAL_OFF);
  104. /*
  105. * Enable the timer.
  106. */
  107. local_timer_ctrl_clrset(TIMER0_RELOAD_EN, enable_mask);
  108. return 0;
  109. }
  110. static int armada_370_xp_clkevt_shutdown(struct clock_event_device *evt)
  111. {
  112. /*
  113. * Disable timer.
  114. */
  115. local_timer_ctrl_clrset(TIMER0_EN, 0);
  116. /*
  117. * ACK pending timer interrupt.
  118. */
  119. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  120. return 0;
  121. }
  122. static int armada_370_xp_clkevt_set_periodic(struct clock_event_device *evt)
  123. {
  124. /*
  125. * Setup timer to fire at 1/HZ intervals.
  126. */
  127. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  128. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  129. /*
  130. * Enable timer.
  131. */
  132. local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | enable_mask);
  133. return 0;
  134. }
  135. static int armada_370_xp_clkevt_irq;
  136. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  137. {
  138. /*
  139. * ACK timer interrupt and call event handler.
  140. */
  141. struct clock_event_device *evt = dev_id;
  142. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  143. evt->event_handler(evt);
  144. return IRQ_HANDLED;
  145. }
  146. /*
  147. * Setup the local clock events for a CPU.
  148. */
  149. static int armada_370_xp_timer_starting_cpu(unsigned int cpu)
  150. {
  151. struct clock_event_device *evt = per_cpu_ptr(armada_370_xp_evt, cpu);
  152. u32 clr = 0, set = 0;
  153. if (timer25Mhz)
  154. set = TIMER0_25MHZ;
  155. else
  156. clr = TIMER0_25MHZ;
  157. local_timer_ctrl_clrset(clr, set);
  158. evt->name = "armada_370_xp_per_cpu_tick";
  159. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  160. CLOCK_EVT_FEAT_PERIODIC;
  161. evt->shift = 32;
  162. evt->rating = 300;
  163. evt->set_next_event = armada_370_xp_clkevt_next_event;
  164. evt->set_state_shutdown = armada_370_xp_clkevt_shutdown;
  165. evt->set_state_periodic = armada_370_xp_clkevt_set_periodic;
  166. evt->set_state_oneshot = armada_370_xp_clkevt_shutdown;
  167. evt->tick_resume = armada_370_xp_clkevt_shutdown;
  168. evt->irq = armada_370_xp_clkevt_irq;
  169. evt->cpumask = cpumask_of(cpu);
  170. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  171. enable_percpu_irq(evt->irq, 0);
  172. return 0;
  173. }
  174. static int armada_370_xp_timer_dying_cpu(unsigned int cpu)
  175. {
  176. struct clock_event_device *evt = per_cpu_ptr(armada_370_xp_evt, cpu);
  177. evt->set_state_shutdown(evt);
  178. disable_percpu_irq(evt->irq);
  179. return 0;
  180. }
  181. static u32 timer0_ctrl_reg, timer0_local_ctrl_reg;
  182. static int armada_370_xp_timer_suspend(void)
  183. {
  184. timer0_ctrl_reg = readl(timer_base + TIMER_CTRL_OFF);
  185. timer0_local_ctrl_reg = readl(local_base + TIMER_CTRL_OFF);
  186. return 0;
  187. }
  188. static void armada_370_xp_timer_resume(void)
  189. {
  190. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  191. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  192. writel(timer0_ctrl_reg, timer_base + TIMER_CTRL_OFF);
  193. writel(timer0_local_ctrl_reg, local_base + TIMER_CTRL_OFF);
  194. }
  195. static struct syscore_ops armada_370_xp_timer_syscore_ops = {
  196. .suspend = armada_370_xp_timer_suspend,
  197. .resume = armada_370_xp_timer_resume,
  198. };
  199. static unsigned long armada_370_delay_timer_read(void)
  200. {
  201. return ~readl(timer_base + TIMER0_VAL_OFF);
  202. }
  203. static struct delay_timer armada_370_delay_timer = {
  204. .read_current_timer = armada_370_delay_timer_read,
  205. };
  206. static int __init armada_370_xp_timer_common_init(struct device_node *np)
  207. {
  208. u32 clr = 0, set = 0;
  209. int res;
  210. timer_base = of_iomap(np, 0);
  211. if (!timer_base) {
  212. pr_err("Failed to iomap\n");
  213. return -ENXIO;
  214. }
  215. local_base = of_iomap(np, 1);
  216. if (!local_base) {
  217. pr_err("Failed to iomap\n");
  218. return -ENXIO;
  219. }
  220. if (timer25Mhz) {
  221. set = TIMER0_25MHZ;
  222. enable_mask = TIMER0_EN;
  223. } else {
  224. clr = TIMER0_25MHZ;
  225. enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);
  226. }
  227. atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);
  228. local_timer_ctrl_clrset(clr, set);
  229. /*
  230. * We use timer 0 as clocksource, and private(local) timer 0
  231. * for clockevents
  232. */
  233. armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);
  234. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  235. /*
  236. * Setup free-running clocksource timer (interrupts
  237. * disabled).
  238. */
  239. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  240. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  241. atomic_io_modify(timer_base + TIMER_CTRL_OFF,
  242. TIMER0_RELOAD_EN | enable_mask,
  243. TIMER0_RELOAD_EN | enable_mask);
  244. armada_370_delay_timer.freq = timer_clk;
  245. register_current_timer_delay(&armada_370_delay_timer);
  246. /*
  247. * Set scale and timer for sched_clock.
  248. */
  249. sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);
  250. res = clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  251. "armada_370_xp_clocksource",
  252. timer_clk, 300, 32, clocksource_mmio_readl_down);
  253. if (res) {
  254. pr_err("Failed to initialize clocksource mmio\n");
  255. return res;
  256. }
  257. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  258. if (!armada_370_xp_evt)
  259. return -ENOMEM;
  260. /*
  261. * Setup clockevent timer (interrupt-driven).
  262. */
  263. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  264. armada_370_xp_timer_interrupt,
  265. "armada_370_xp_per_cpu_tick",
  266. armada_370_xp_evt);
  267. /* Immediately configure the timer on the boot CPU */
  268. if (res) {
  269. pr_err("Failed to request percpu irq\n");
  270. return res;
  271. }
  272. res = cpuhp_setup_state(CPUHP_AP_ARMADA_TIMER_STARTING,
  273. "clockevents/armada:starting",
  274. armada_370_xp_timer_starting_cpu,
  275. armada_370_xp_timer_dying_cpu);
  276. if (res) {
  277. pr_err("Failed to setup hotplug state and timer\n");
  278. return res;
  279. }
  280. register_syscore_ops(&armada_370_xp_timer_syscore_ops);
  281. return 0;
  282. }
  283. static int __init armada_xp_timer_init(struct device_node *np)
  284. {
  285. struct clk *clk = of_clk_get_by_name(np, "fixed");
  286. int ret;
  287. if (IS_ERR(clk)) {
  288. pr_err("Failed to get clock\n");
  289. return PTR_ERR(clk);
  290. }
  291. ret = clk_prepare_enable(clk);
  292. if (ret)
  293. return ret;
  294. timer_clk = clk_get_rate(clk);
  295. return armada_370_xp_timer_common_init(np);
  296. }
  297. TIMER_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  298. armada_xp_timer_init);
  299. static int __init armada_375_timer_init(struct device_node *np)
  300. {
  301. struct clk *clk;
  302. int ret;
  303. clk = of_clk_get_by_name(np, "fixed");
  304. if (!IS_ERR(clk)) {
  305. ret = clk_prepare_enable(clk);
  306. if (ret)
  307. return ret;
  308. timer_clk = clk_get_rate(clk);
  309. } else {
  310. /*
  311. * This fallback is required in order to retain proper
  312. * devicetree backwards compatibility.
  313. */
  314. clk = of_clk_get(np, 0);
  315. /* Must have at least a clock */
  316. if (IS_ERR(clk)) {
  317. pr_err("Failed to get clock\n");
  318. return PTR_ERR(clk);
  319. }
  320. ret = clk_prepare_enable(clk);
  321. if (ret)
  322. return ret;
  323. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  324. timer25Mhz = false;
  325. }
  326. return armada_370_xp_timer_common_init(np);
  327. }
  328. TIMER_OF_DECLARE(armada_375, "marvell,armada-375-timer",
  329. armada_375_timer_init);
  330. static int __init armada_370_timer_init(struct device_node *np)
  331. {
  332. struct clk *clk;
  333. int ret;
  334. clk = of_clk_get(np, 0);
  335. if (IS_ERR(clk)) {
  336. pr_err("Failed to get clock\n");
  337. return PTR_ERR(clk);
  338. }
  339. ret = clk_prepare_enable(clk);
  340. if (ret)
  341. return ret;
  342. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  343. timer25Mhz = false;
  344. return armada_370_xp_timer_common_init(np);
  345. }
  346. TIMER_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  347. armada_370_timer_init);