time-armada-370-xp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 void
  113. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  114. struct clock_event_device *dev)
  115. {
  116. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  117. /*
  118. * Setup timer to fire at 1/HZ intervals.
  119. */
  120. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  121. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  122. /*
  123. * Enable timer.
  124. */
  125. local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | enable_mask);
  126. } else {
  127. /*
  128. * Disable timer.
  129. */
  130. local_timer_ctrl_clrset(TIMER0_EN, 0);
  131. /*
  132. * ACK pending timer interrupt.
  133. */
  134. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  135. }
  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_mode = armada_370_xp_clkevt_mode,
  167. evt->irq = armada_370_xp_clkevt_irq;
  168. evt->cpumask = cpumask_of(cpu);
  169. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  170. enable_percpu_irq(evt->irq, 0);
  171. return 0;
  172. }
  173. static void armada_370_xp_timer_stop(struct clock_event_device *evt)
  174. {
  175. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  176. disable_percpu_irq(evt->irq);
  177. }
  178. static int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  179. unsigned long action, void *hcpu)
  180. {
  181. /*
  182. * Grab cpu pointer in each case to avoid spurious
  183. * preemptible warnings
  184. */
  185. switch (action & ~CPU_TASKS_FROZEN) {
  186. case CPU_STARTING:
  187. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  188. break;
  189. case CPU_DYING:
  190. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  191. break;
  192. }
  193. return NOTIFY_OK;
  194. }
  195. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  196. .notifier_call = armada_370_xp_timer_cpu_notify,
  197. };
  198. static u32 timer0_ctrl_reg, timer0_local_ctrl_reg;
  199. static int armada_370_xp_timer_suspend(void)
  200. {
  201. timer0_ctrl_reg = readl(timer_base + TIMER_CTRL_OFF);
  202. timer0_local_ctrl_reg = readl(local_base + TIMER_CTRL_OFF);
  203. return 0;
  204. }
  205. static void armada_370_xp_timer_resume(void)
  206. {
  207. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  208. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  209. writel(timer0_ctrl_reg, timer_base + TIMER_CTRL_OFF);
  210. writel(timer0_local_ctrl_reg, local_base + TIMER_CTRL_OFF);
  211. }
  212. struct syscore_ops armada_370_xp_timer_syscore_ops = {
  213. .suspend = armada_370_xp_timer_suspend,
  214. .resume = armada_370_xp_timer_resume,
  215. };
  216. static void __init armada_370_xp_timer_common_init(struct device_node *np)
  217. {
  218. u32 clr = 0, set = 0;
  219. int res;
  220. timer_base = of_iomap(np, 0);
  221. WARN_ON(!timer_base);
  222. local_base = of_iomap(np, 1);
  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. /*
  248. * Set scale and timer for sched_clock.
  249. */
  250. sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);
  251. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  252. "armada_370_xp_clocksource",
  253. timer_clk, 300, 32, clocksource_mmio_readl_down);
  254. register_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  255. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  256. /*
  257. * Setup clockevent timer (interrupt-driven).
  258. */
  259. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  260. armada_370_xp_timer_interrupt,
  261. "armada_370_xp_per_cpu_tick",
  262. armada_370_xp_evt);
  263. /* Immediately configure the timer on the boot CPU */
  264. if (!res)
  265. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  266. register_syscore_ops(&armada_370_xp_timer_syscore_ops);
  267. }
  268. static void __init armada_xp_timer_init(struct device_node *np)
  269. {
  270. struct clk *clk = of_clk_get_by_name(np, "fixed");
  271. /* The 25Mhz fixed clock is mandatory, and must always be available */
  272. BUG_ON(IS_ERR(clk));
  273. clk_prepare_enable(clk);
  274. timer_clk = clk_get_rate(clk);
  275. armada_370_xp_timer_common_init(np);
  276. }
  277. CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
  278. armada_xp_timer_init);
  279. static void __init armada_375_timer_init(struct device_node *np)
  280. {
  281. struct clk *clk;
  282. clk = of_clk_get_by_name(np, "fixed");
  283. if (!IS_ERR(clk)) {
  284. clk_prepare_enable(clk);
  285. timer_clk = clk_get_rate(clk);
  286. } else {
  287. /*
  288. * This fallback is required in order to retain proper
  289. * devicetree backwards compatibility.
  290. */
  291. clk = of_clk_get(np, 0);
  292. /* Must have at least a clock */
  293. BUG_ON(IS_ERR(clk));
  294. clk_prepare_enable(clk);
  295. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  296. timer25Mhz = false;
  297. }
  298. armada_370_xp_timer_common_init(np);
  299. }
  300. CLOCKSOURCE_OF_DECLARE(armada_375, "marvell,armada-375-timer",
  301. armada_375_timer_init);
  302. static void __init armada_370_timer_init(struct device_node *np)
  303. {
  304. struct clk *clk = of_clk_get(np, 0);
  305. BUG_ON(IS_ERR(clk));
  306. clk_prepare_enable(clk);
  307. timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
  308. timer25Mhz = false;
  309. armada_370_xp_timer_common_init(np);
  310. }
  311. CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
  312. armada_370_timer_init);