time-armada-370-xp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_setup(struct clock_event_device *evt)
  153. {
  154. u32 clr = 0, set = 0;
  155. int cpu = smp_processor_id();
  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 void armada_370_xp_timer_stop(struct clock_event_device *evt)
  178. {
  179. evt->set_state_shutdown(evt);
  180. disable_percpu_irq(evt->irq);
  181. }
  182. static int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  183. unsigned long action, void *hcpu)
  184. {
  185. /*
  186. * Grab cpu pointer in each case to avoid spurious
  187. * preemptible warnings
  188. */
  189. switch (action & ~CPU_TASKS_FROZEN) {
  190. case CPU_STARTING:
  191. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  192. break;
  193. case CPU_DYING:
  194. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  195. break;
  196. }
  197. return NOTIFY_OK;
  198. }
  199. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  200. .notifier_call = armada_370_xp_timer_cpu_notify,
  201. };
  202. static u32 timer0_ctrl_reg, timer0_local_ctrl_reg;
  203. static int armada_370_xp_timer_suspend(void)
  204. {
  205. timer0_ctrl_reg = readl(timer_base + TIMER_CTRL_OFF);
  206. timer0_local_ctrl_reg = readl(local_base + TIMER_CTRL_OFF);
  207. return 0;
  208. }
  209. static void armada_370_xp_timer_resume(void)
  210. {
  211. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  212. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  213. writel(timer0_ctrl_reg, timer_base + TIMER_CTRL_OFF);
  214. writel(timer0_local_ctrl_reg, local_base + TIMER_CTRL_OFF);
  215. }
  216. struct syscore_ops armada_370_xp_timer_syscore_ops = {
  217. .suspend = armada_370_xp_timer_suspend,
  218. .resume = armada_370_xp_timer_resume,
  219. };
  220. static unsigned long armada_370_delay_timer_read(void)
  221. {
  222. return ~readl(timer_base + TIMER0_VAL_OFF);
  223. }
  224. static struct delay_timer armada_370_delay_timer = {
  225. .read_current_timer = armada_370_delay_timer_read,
  226. };
  227. static void __init armada_370_xp_timer_common_init(struct device_node *np)
  228. {
  229. u32 clr = 0, set = 0;
  230. int res;
  231. timer_base = of_iomap(np, 0);
  232. WARN_ON(!timer_base);
  233. local_base = of_iomap(np, 1);
  234. if (timer25Mhz) {
  235. set = TIMER0_25MHZ;
  236. enable_mask = TIMER0_EN;
  237. } else {
  238. clr = TIMER0_25MHZ;
  239. enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);
  240. }
  241. atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);
  242. local_timer_ctrl_clrset(clr, set);
  243. /*
  244. * We use timer 0 as clocksource, and private(local) timer 0
  245. * for clockevents
  246. */
  247. armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);
  248. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  249. /*
  250. * Setup free-running clocksource timer (interrupts
  251. * disabled).
  252. */
  253. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  254. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  255. atomic_io_modify(timer_base + TIMER_CTRL_OFF,
  256. TIMER0_RELOAD_EN | enable_mask,
  257. TIMER0_RELOAD_EN | enable_mask);
  258. armada_370_delay_timer.freq = timer_clk;
  259. register_current_timer_delay(&armada_370_delay_timer);
  260. /*
  261. * Set scale and timer for sched_clock.
  262. */
  263. sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);
  264. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  265. "armada_370_xp_clocksource",
  266. timer_clk, 300, 32, clocksource_mmio_readl_down);
  267. register_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  268. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  269. /*
  270. * Setup clockevent timer (interrupt-driven).
  271. */
  272. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  273. armada_370_xp_timer_interrupt,
  274. "armada_370_xp_per_cpu_tick",
  275. armada_370_xp_evt);
  276. /* Immediately configure the timer on the boot CPU */
  277. if (!res)
  278. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  279. register_syscore_ops(&armada_370_xp_timer_syscore_ops);
  280. }
  281. static void __init armada_xp_timer_init(struct device_node *np)
  282. {
  283. struct clk *clk = of_clk_get_by_name(np, "fixed");
  284. /* The 25Mhz fixed clock is mandatory, and must always be available */
  285. BUG_ON(IS_ERR(clk));
  286. clk_prepare_enable(clk);
  287. timer_clk = clk_get_rate(clk);
  288. armada_370_xp_timer_common_init(np);
  289. }
  290. CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  291. armada_xp_timer_init);
  292. static void __init armada_375_timer_init(struct device_node *np)
  293. {
  294. struct clk *clk;
  295. clk = of_clk_get_by_name(np, "fixed");
  296. if (!IS_ERR(clk)) {
  297. clk_prepare_enable(clk);
  298. timer_clk = clk_get_rate(clk);
  299. } else {
  300. /*
  301. * This fallback is required in order to retain proper
  302. * devicetree backwards compatibility.
  303. */
  304. clk = of_clk_get(np, 0);
  305. /* Must have at least a clock */
  306. BUG_ON(IS_ERR(clk));
  307. clk_prepare_enable(clk);
  308. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  309. timer25Mhz = false;
  310. }
  311. armada_370_xp_timer_common_init(np);
  312. }
  313. CLOCKSOURCE_OF_DECLARE(armada_375, "marvell,armada-375-timer",
  314. armada_375_timer_init);
  315. static void __init armada_370_timer_init(struct device_node *np)
  316. {
  317. struct clk *clk = of_clk_get(np, 0);
  318. BUG_ON(IS_ERR(clk));
  319. clk_prepare_enable(clk);
  320. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  321. timer25Mhz = false;
  322. armada_370_xp_timer_common_init(np);
  323. }
  324. CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  325. armada_370_timer_init);