PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/clocksource/timer-atlas7.c

https://github.com/tklauser/linux-nios2
C | 286 lines | 210 code | 56 blank | 20 comment | 10 complexity | 3a906d48f9f311a67d0e781710c0d18f MD5 | raw file
  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 u64 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_starting_cpu(unsigned int cpu)
  141. {
  142. struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
  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->max_delta_ticks = (unsigned long)-2;
  159. ce->min_delta_ns = clockevent_delta2ns(2, ce);
  160. ce->min_delta_ticks = 2;
  161. ce->cpumask = cpumask_of(cpu);
  162. action->dev_id = ce;
  163. BUG_ON(setup_irq(ce->irq, action));
  164. irq_force_affinity(action->irq, cpumask_of(cpu));
  165. clockevents_register_device(ce);
  166. return 0;
  167. }
  168. static int sirfsoc_local_timer_dying_cpu(unsigned int cpu)
  169. {
  170. sirfsoc_timer_count_disable(1);
  171. if (cpu == 0)
  172. remove_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq);
  173. else
  174. remove_irq(sirfsoc_timer1_irq.irq, &sirfsoc_timer1_irq);
  175. return 0;
  176. }
  177. static int __init sirfsoc_clockevent_init(void)
  178. {
  179. sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
  180. BUG_ON(!sirfsoc_clockevent);
  181. /* Install and invoke hotplug callbacks */
  182. return cpuhp_setup_state(CPUHP_AP_MARCO_TIMER_STARTING,
  183. "clockevents/marco:starting",
  184. sirfsoc_local_timer_starting_cpu,
  185. sirfsoc_local_timer_dying_cpu);
  186. }
  187. /* initialize the kernel jiffy timer source */
  188. static int __init sirfsoc_atlas7_timer_init(struct device_node *np)
  189. {
  190. struct clk *clk;
  191. clk = of_clk_get(np, 0);
  192. BUG_ON(IS_ERR(clk));
  193. BUG_ON(clk_prepare_enable(clk));
  194. atlas7_timer_rate = clk_get_rate(clk);
  195. /* timer dividers: 0, not divided */
  196. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  197. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
  198. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
  199. /* Initialize timer counters to 0 */
  200. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  201. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  202. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  203. BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  204. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
  205. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
  206. /* Clear all interrupts */
  207. writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
  208. BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
  209. return sirfsoc_clockevent_init();
  210. }
  211. static int __init sirfsoc_of_timer_init(struct device_node *np)
  212. {
  213. sirfsoc_timer_base = of_iomap(np, 0);
  214. if (!sirfsoc_timer_base) {
  215. pr_err("unable to map timer cpu registers\n");
  216. return -ENXIO;
  217. }
  218. sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  219. if (!sirfsoc_timer_irq.irq) {
  220. pr_err("No irq passed for timer0 via DT\n");
  221. return -EINVAL;
  222. }
  223. sirfsoc_timer1_irq.irq = irq_of_parse_and_map(np, 1);
  224. if (!sirfsoc_timer1_irq.irq) {
  225. pr_err("No irq passed for timer1 via DT\n");
  226. return -EINVAL;
  227. }
  228. return sirfsoc_atlas7_timer_init(np);
  229. }
  230. CLOCKSOURCE_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);