time-armada-370-xp.c 10 KB

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