time-armada-370-xp.c 7.4 KB

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