time-armada-370-xp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/clk.h>
  21. #include <linux/cpu.h>
  22. #include <linux/timer.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/of.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_address.h>
  28. #include <linux/irq.h>
  29. #include <linux/module.h>
  30. #include <linux/sched_clock.h>
  31. #include <linux/percpu.h>
  32. #include <linux/time-armada-370-xp.h>
  33. /*
  34. * Timer block registers.
  35. */
  36. #define TIMER_CTRL_OFF 0x0000
  37. #define TIMER0_EN 0x0001
  38. #define TIMER0_RELOAD_EN 0x0002
  39. #define TIMER0_25MHZ 0x0800
  40. #define TIMER0_DIV(div) ((div) << 19)
  41. #define TIMER1_EN 0x0004
  42. #define TIMER1_RELOAD_EN 0x0008
  43. #define TIMER1_25MHZ 0x1000
  44. #define TIMER1_DIV(div) ((div) << 22)
  45. #define TIMER_EVENTS_STATUS 0x0004
  46. #define TIMER0_CLR_MASK (~0x1)
  47. #define TIMER1_CLR_MASK (~0x100)
  48. #define TIMER0_RELOAD_OFF 0x0010
  49. #define TIMER0_VAL_OFF 0x0014
  50. #define TIMER1_RELOAD_OFF 0x0018
  51. #define TIMER1_VAL_OFF 0x001c
  52. #define LCL_TIMER_EVENTS_STATUS 0x0028
  53. /* Global timers are connected to the coherency fabric clock, and the
  54. below divider reduces their incrementing frequency. */
  55. #define TIMER_DIVIDER_SHIFT 5
  56. #define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
  57. /*
  58. * SoC-specific data.
  59. */
  60. static void __iomem *timer_base, *local_base;
  61. static unsigned int timer_clk;
  62. static bool timer25Mhz = true;
  63. /*
  64. * Number of timer ticks per jiffy.
  65. */
  66. static u32 ticks_per_jiffy;
  67. static struct clock_event_device __percpu *armada_370_xp_evt;
  68. static u32 notrace armada_370_xp_read_sched_clock(void)
  69. {
  70. return ~readl(timer_base + TIMER0_VAL_OFF);
  71. }
  72. /*
  73. * Clockevent handling.
  74. */
  75. static int
  76. armada_370_xp_clkevt_next_event(unsigned long delta,
  77. struct clock_event_device *dev)
  78. {
  79. u32 u;
  80. /*
  81. * Clear clockevent timer interrupt.
  82. */
  83. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  84. /*
  85. * Setup new clockevent timer value.
  86. */
  87. writel(delta, local_base + TIMER0_VAL_OFF);
  88. /*
  89. * Enable the timer.
  90. */
  91. u = readl(local_base + TIMER_CTRL_OFF);
  92. u = ((u & ~TIMER0_RELOAD_EN) | TIMER0_EN |
  93. TIMER0_DIV(TIMER_DIVIDER_SHIFT));
  94. writel(u, local_base + TIMER_CTRL_OFF);
  95. return 0;
  96. }
  97. static void
  98. armada_370_xp_clkevt_mode(enum clock_event_mode mode,
  99. struct clock_event_device *dev)
  100. {
  101. u32 u;
  102. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  103. /*
  104. * Setup timer to fire at 1/HZ intervals.
  105. */
  106. writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
  107. writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
  108. /*
  109. * Enable timer.
  110. */
  111. u = readl(local_base + TIMER_CTRL_OFF);
  112. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  113. TIMER0_DIV(TIMER_DIVIDER_SHIFT)),
  114. local_base + TIMER_CTRL_OFF);
  115. } else {
  116. /*
  117. * Disable timer.
  118. */
  119. u = readl(local_base + TIMER_CTRL_OFF);
  120. writel(u & ~TIMER0_EN, local_base + TIMER_CTRL_OFF);
  121. /*
  122. * ACK pending timer interrupt.
  123. */
  124. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  125. }
  126. }
  127. static int armada_370_xp_clkevt_irq;
  128. static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
  129. {
  130. /*
  131. * ACK timer interrupt and call event handler.
  132. */
  133. struct clock_event_device *evt = dev_id;
  134. writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
  135. evt->event_handler(evt);
  136. return IRQ_HANDLED;
  137. }
  138. /*
  139. * Setup the local clock events for a CPU.
  140. */
  141. static int armada_370_xp_timer_setup(struct clock_event_device *evt)
  142. {
  143. u32 u;
  144. int cpu = smp_processor_id();
  145. u = readl(local_base + TIMER_CTRL_OFF);
  146. if (timer25Mhz)
  147. writel(u | TIMER0_25MHZ, local_base + TIMER_CTRL_OFF);
  148. else
  149. writel(u & ~TIMER0_25MHZ, local_base + TIMER_CTRL_OFF);
  150. evt->name = "armada_370_xp_per_cpu_tick",
  151. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  152. CLOCK_EVT_FEAT_PERIODIC;
  153. evt->shift = 32,
  154. evt->rating = 300,
  155. evt->set_next_event = armada_370_xp_clkevt_next_event,
  156. evt->set_mode = armada_370_xp_clkevt_mode,
  157. evt->irq = armada_370_xp_clkevt_irq;
  158. evt->cpumask = cpumask_of(cpu);
  159. clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
  160. enable_percpu_irq(evt->irq, 0);
  161. return 0;
  162. }
  163. static void armada_370_xp_timer_stop(struct clock_event_device *evt)
  164. {
  165. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  166. disable_percpu_irq(evt->irq);
  167. }
  168. static int armada_370_xp_timer_cpu_notify(struct notifier_block *self,
  169. unsigned long action, void *hcpu)
  170. {
  171. /*
  172. * Grab cpu pointer in each case to avoid spurious
  173. * preemptible warnings
  174. */
  175. switch (action & ~CPU_TASKS_FROZEN) {
  176. case CPU_STARTING:
  177. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  178. break;
  179. case CPU_DYING:
  180. armada_370_xp_timer_stop(this_cpu_ptr(armada_370_xp_evt));
  181. break;
  182. }
  183. return NOTIFY_OK;
  184. }
  185. static struct notifier_block armada_370_xp_timer_cpu_nb = {
  186. .notifier_call = armada_370_xp_timer_cpu_notify,
  187. };
  188. void __init armada_370_xp_timer_init(void)
  189. {
  190. u32 u;
  191. struct device_node *np;
  192. int res;
  193. np = of_find_compatible_node(NULL, NULL, "marvell,armada-370-xp-timer");
  194. timer_base = of_iomap(np, 0);
  195. WARN_ON(!timer_base);
  196. local_base = of_iomap(np, 1);
  197. if (of_find_property(np, "marvell,timer-25Mhz", NULL)) {
  198. /* The fixed 25MHz timer is available so let's use it */
  199. u = readl(timer_base + TIMER_CTRL_OFF);
  200. writel(u | TIMER0_25MHZ,
  201. timer_base + TIMER_CTRL_OFF);
  202. timer_clk = 25000000;
  203. } else {
  204. unsigned long rate = 0;
  205. struct clk *clk = of_clk_get(np, 0);
  206. WARN_ON(IS_ERR(clk));
  207. rate = clk_get_rate(clk);
  208. u = readl(timer_base + TIMER_CTRL_OFF);
  209. writel(u & ~(TIMER0_25MHZ),
  210. timer_base + TIMER_CTRL_OFF);
  211. timer_clk = rate / TIMER_DIVIDER;
  212. timer25Mhz = false;
  213. }
  214. /*
  215. * We use timer 0 as clocksource, and private(local) timer 0
  216. * for clockevents
  217. */
  218. armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);
  219. ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
  220. /*
  221. * Set scale and timer for sched_clock.
  222. */
  223. setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
  224. /*
  225. * Setup free-running clocksource timer (interrupts
  226. * disabled).
  227. */
  228. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  229. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  230. u = readl(timer_base + TIMER_CTRL_OFF);
  231. writel((u | TIMER0_EN | TIMER0_RELOAD_EN |
  232. TIMER0_DIV(TIMER_DIVIDER_SHIFT)), timer_base + TIMER_CTRL_OFF);
  233. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
  234. "armada_370_xp_clocksource",
  235. timer_clk, 300, 32, clocksource_mmio_readl_down);
  236. register_cpu_notifier(&armada_370_xp_timer_cpu_nb);
  237. armada_370_xp_evt = alloc_percpu(struct clock_event_device);
  238. /*
  239. * Setup clockevent timer (interrupt-driven).
  240. */
  241. res = request_percpu_irq(armada_370_xp_clkevt_irq,
  242. armada_370_xp_timer_interrupt,
  243. "armada_370_xp_per_cpu_tick",
  244. armada_370_xp_evt);
  245. /* Immediately configure the timer on the boot CPU */
  246. if (!res)
  247. armada_370_xp_timer_setup(this_cpu_ptr(armada_370_xp_evt));
  248. }