timer-atlas7.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * System timer for CSR SiRFprimaII
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/cpu.h>
  13. #include <linux/bitops.h>
  14. #include <linux/irq.h>
  15. #include <linux/clk.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/sched_clock.h>
  21. #define SIRFSOC_TIMER_32COUNTER_0_CTRL 0x0000
  22. #define SIRFSOC_TIMER_32COUNTER_1_CTRL 0x0004
  23. #define SIRFSOC_TIMER_MATCH_0 0x0018
  24. #define SIRFSOC_TIMER_MATCH_1 0x001c
  25. #define SIRFSOC_TIMER_COUNTER_0 0x0048
  26. #define SIRFSOC_TIMER_COUNTER_1 0x004c
  27. #define SIRFSOC_TIMER_INTR_STATUS 0x0060
  28. #define SIRFSOC_TIMER_WATCHDOG_EN 0x0064
  29. #define SIRFSOC_TIMER_64COUNTER_CTRL 0x0068
  30. #define SIRFSOC_TIMER_64COUNTER_LO 0x006c
  31. #define SIRFSOC_TIMER_64COUNTER_HI 0x0070
  32. #define SIRFSOC_TIMER_64COUNTER_LOAD_LO 0x0074
  33. #define SIRFSOC_TIMER_64COUNTER_LOAD_HI 0x0078
  34. #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO 0x007c
  35. #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI 0x0080
  36. #define SIRFSOC_TIMER_REG_CNT 6
  37. static unsigned long atlas7_timer_rate;
  38. static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
  39. SIRFSOC_TIMER_WATCHDOG_EN,
  40. SIRFSOC_TIMER_32COUNTER_0_CTRL,
  41. SIRFSOC_TIMER_32COUNTER_1_CTRL,
  42. SIRFSOC_TIMER_64COUNTER_CTRL,
  43. SIRFSOC_TIMER_64COUNTER_RLATCHED_LO,
  44. SIRFSOC_TIMER_64COUNTER_RLATCHED_HI,
  45. };
  46. static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
  47. static void __iomem *sirfsoc_timer_base;
  48. /* disable count and interrupt */
  49. static inline void sirfsoc_timer_count_disable(int idx)
  50. {
  51. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7,
  52. sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
  53. }
  54. /* enable count and interrupt */
  55. static inline void sirfsoc_timer_count_enable(int idx)
  56. {
  57. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3,
  58. sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
  59. }
  60. /* timer interrupt handler */
  61. static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
  62. {
  63. struct clock_event_device *ce = dev_id;
  64. int cpu = smp_processor_id();
  65. /* clear timer interrupt */
  66. writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
  67. if (clockevent_state_oneshot(ce))
  68. sirfsoc_timer_count_disable(cpu);
  69. ce->event_handler(ce);
  70. return IRQ_HANDLED;
  71. }
  72. /* read 64-bit timer counter */
  73. static cycle_t sirfsoc_timer_read(struct clocksource *cs)
  74. {
  75. u64 cycles;
  76. writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  77. BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  78. cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI);
  79. cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO);
  80. return cycles;
  81. }
  82. static int sirfsoc_timer_set_next_event(unsigned long delta,
  83. struct clock_event_device *ce)
  84. {
  85. int cpu = smp_processor_id();
  86. /* disable timer first, then modify the related registers */
  87. sirfsoc_timer_count_disable(cpu);
  88. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
  89. 4 * cpu);
  90. writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
  91. 4 * cpu);
  92. /* enable the tick */
  93. sirfsoc_timer_count_enable(cpu);
  94. return 0;
  95. }
  96. /* Oneshot is enabled in set_next_event */
  97. static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
  98. {
  99. sirfsoc_timer_count_disable(smp_processor_id());
  100. return 0;
  101. }
  102. static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  103. {
  104. int i;
  105. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  106. sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  107. }
  108. static void sirfsoc_clocksource_resume(struct clocksource *cs)
  109. {
  110. int i;
  111. for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
  112. writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  113. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
  114. sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  115. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
  116. sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  117. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  118. BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  119. }
  120. static struct clock_event_device __percpu *sirfsoc_clockevent;
  121. static struct clocksource sirfsoc_clocksource = {
  122. .name = "sirfsoc_clocksource",
  123. .rating = 200,
  124. .mask = CLOCKSOURCE_MASK(64),
  125. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  126. .read = sirfsoc_timer_read,
  127. .suspend = sirfsoc_clocksource_suspend,
  128. .resume = sirfsoc_clocksource_resume,
  129. };
  130. static struct irqaction sirfsoc_timer_irq = {
  131. .name = "sirfsoc_timer0",
  132. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  133. .handler = sirfsoc_timer_interrupt,
  134. };
  135. static struct irqaction sirfsoc_timer1_irq = {
  136. .name = "sirfsoc_timer1",
  137. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  138. .handler = sirfsoc_timer_interrupt,
  139. };
  140. static int sirfsoc_local_timer_setup(struct clock_event_device *ce)
  141. {
  142. int cpu = smp_processor_id();
  143. struct irqaction *action;
  144. if (cpu == 0)
  145. action = &sirfsoc_timer_irq;
  146. else
  147. action = &sirfsoc_timer1_irq;
  148. ce->irq = action->irq;
  149. ce->name = "local_timer";
  150. ce->features = CLOCK_EVT_FEAT_ONESHOT;
  151. ce->rating = 200;
  152. ce->set_state_shutdown = sirfsoc_timer_shutdown;
  153. ce->set_state_oneshot = sirfsoc_timer_shutdown;
  154. ce->tick_resume = sirfsoc_timer_shutdown;
  155. ce->set_next_event = sirfsoc_timer_set_next_event;
  156. clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60);
  157. ce->max_delta_ns = clockevent_delta2ns(-2, ce);
  158. ce->min_delta_ns = clockevent_delta2ns(2, ce);
  159. ce->cpumask = cpumask_of(cpu);
  160. action->dev_id = ce;
  161. BUG_ON(setup_irq(ce->irq, action));
  162. irq_force_affinity(action->irq, cpumask_of(cpu));
  163. clockevents_register_device(ce);
  164. return 0;
  165. }
  166. static void sirfsoc_local_timer_stop(struct clock_event_device *ce)
  167. {
  168. int cpu = smp_processor_id();
  169. sirfsoc_timer_count_disable(1);
  170. if (cpu == 0)
  171. remove_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq);
  172. else
  173. remove_irq(sirfsoc_timer1_irq.irq, &sirfsoc_timer1_irq);
  174. }
  175. static int sirfsoc_cpu_notify(struct notifier_block *self,
  176. unsigned long action, void *hcpu)
  177. {
  178. /*
  179. * Grab cpu pointer in each case to avoid spurious
  180. * preemptible warnings
  181. */
  182. switch (action & ~CPU_TASKS_FROZEN) {
  183. case CPU_STARTING:
  184. sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent));
  185. break;
  186. case CPU_DYING:
  187. sirfsoc_local_timer_stop(this_cpu_ptr(sirfsoc_clockevent));
  188. break;
  189. }
  190. return NOTIFY_OK;
  191. }
  192. static struct notifier_block sirfsoc_cpu_nb = {
  193. .notifier_call = sirfsoc_cpu_notify,
  194. };
  195. static void __init sirfsoc_clockevent_init(void)
  196. {
  197. sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
  198. BUG_ON(!sirfsoc_clockevent);
  199. BUG_ON(register_cpu_notifier(&sirfsoc_cpu_nb));
  200. /* Immediately configure the timer on the boot CPU */
  201. sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent));
  202. }
  203. /* initialize the kernel jiffy timer source */
  204. static void __init sirfsoc_atlas7_timer_init(struct device_node *np)
  205. {
  206. struct clk *clk;
  207. clk = of_clk_get(np, 0);
  208. BUG_ON(IS_ERR(clk));
  209. BUG_ON(clk_prepare_enable(clk));
  210. atlas7_timer_rate = clk_get_rate(clk);
  211. /* timer dividers: 0, not divided */
  212. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  213. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
  214. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
  215. /* Initialize timer counters to 0 */
  216. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  217. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  218. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  219. BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  220. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
  221. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
  222. /* Clear all interrupts */
  223. writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
  224. BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
  225. sirfsoc_clockevent_init();
  226. }
  227. static void __init sirfsoc_of_timer_init(struct device_node *np)
  228. {
  229. sirfsoc_timer_base = of_iomap(np, 0);
  230. if (!sirfsoc_timer_base)
  231. panic("unable to map timer cpu registers\n");
  232. sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  233. if (!sirfsoc_timer_irq.irq)
  234. panic("No irq passed for timer0 via DT\n");
  235. sirfsoc_timer1_irq.irq = irq_of_parse_and_map(np, 1);
  236. if (!sirfsoc_timer1_irq.irq)
  237. panic("No irq passed for timer1 via DT\n");
  238. sirfsoc_atlas7_timer_init(np);
  239. }
  240. CLOCKSOURCE_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);