timer-marco.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/bitops.h>
  13. #include <linux/irq.h>
  14. #include <linux/clk.h>
  15. #include <linux/slab.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #include <asm/sched_clock.h>
  20. #include <asm/localtimer.h>
  21. #include <asm/mach/time.h>
  22. #define SIRFSOC_TIMER_32COUNTER_0_CTRL 0x0000
  23. #define SIRFSOC_TIMER_32COUNTER_1_CTRL 0x0004
  24. #define SIRFSOC_TIMER_MATCH_0 0x0018
  25. #define SIRFSOC_TIMER_MATCH_1 0x001c
  26. #define SIRFSOC_TIMER_COUNTER_0 0x0048
  27. #define SIRFSOC_TIMER_COUNTER_1 0x004c
  28. #define SIRFSOC_TIMER_INTR_STATUS 0x0060
  29. #define SIRFSOC_TIMER_WATCHDOG_EN 0x0064
  30. #define SIRFSOC_TIMER_64COUNTER_CTRL 0x0068
  31. #define SIRFSOC_TIMER_64COUNTER_LO 0x006c
  32. #define SIRFSOC_TIMER_64COUNTER_HI 0x0070
  33. #define SIRFSOC_TIMER_64COUNTER_LOAD_LO 0x0074
  34. #define SIRFSOC_TIMER_64COUNTER_LOAD_HI 0x0078
  35. #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO 0x007c
  36. #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI 0x0080
  37. #define SIRFSOC_TIMER_REG_CNT 6
  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) | 0x7,
  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 (ce->mode == CLOCK_EVT_MODE_ONESHOT)
  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. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
  87. 4 * cpu);
  88. writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
  89. 4 * cpu);
  90. /* enable the tick */
  91. sirfsoc_timer_count_enable(cpu);
  92. return 0;
  93. }
  94. static void sirfsoc_timer_set_mode(enum clock_event_mode mode,
  95. struct clock_event_device *ce)
  96. {
  97. switch (mode) {
  98. case CLOCK_EVT_MODE_ONESHOT:
  99. /* enable in set_next_event */
  100. break;
  101. default:
  102. break;
  103. }
  104. sirfsoc_timer_count_disable(smp_processor_id());
  105. }
  106. static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  107. {
  108. int i;
  109. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  110. sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  111. }
  112. static void sirfsoc_clocksource_resume(struct clocksource *cs)
  113. {
  114. int i;
  115. for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
  116. writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  117. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
  118. sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  119. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
  120. sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  121. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  122. BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  123. }
  124. static struct clock_event_device sirfsoc_clockevent = {
  125. .name = "sirfsoc_clockevent",
  126. .rating = 200,
  127. .features = CLOCK_EVT_FEAT_ONESHOT,
  128. .set_mode = sirfsoc_timer_set_mode,
  129. .set_next_event = sirfsoc_timer_set_next_event,
  130. };
  131. static struct clocksource sirfsoc_clocksource = {
  132. .name = "sirfsoc_clocksource",
  133. .rating = 200,
  134. .mask = CLOCKSOURCE_MASK(64),
  135. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  136. .read = sirfsoc_timer_read,
  137. .suspend = sirfsoc_clocksource_suspend,
  138. .resume = sirfsoc_clocksource_resume,
  139. };
  140. static struct irqaction sirfsoc_timer_irq = {
  141. .name = "sirfsoc_timer0",
  142. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  143. .handler = sirfsoc_timer_interrupt,
  144. .dev_id = &sirfsoc_clockevent,
  145. };
  146. #ifdef CONFIG_LOCAL_TIMERS
  147. static struct irqaction sirfsoc_timer1_irq = {
  148. .name = "sirfsoc_timer1",
  149. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  150. .handler = sirfsoc_timer_interrupt,
  151. };
  152. static int __cpuinit sirfsoc_local_timer_setup(struct clock_event_device *ce)
  153. {
  154. /* Use existing clock_event for cpu 0 */
  155. if (!smp_processor_id())
  156. return 0;
  157. ce->irq = sirfsoc_timer1_irq.irq;
  158. ce->name = "local_timer";
  159. ce->features = sirfsoc_clockevent.features;
  160. ce->rating = sirfsoc_clockevent.rating;
  161. ce->set_mode = sirfsoc_timer_set_mode;
  162. ce->set_next_event = sirfsoc_timer_set_next_event;
  163. ce->shift = sirfsoc_clockevent.shift;
  164. ce->mult = sirfsoc_clockevent.mult;
  165. ce->max_delta_ns = sirfsoc_clockevent.max_delta_ns;
  166. ce->min_delta_ns = sirfsoc_clockevent.min_delta_ns;
  167. sirfsoc_timer1_irq.dev_id = ce;
  168. BUG_ON(setup_irq(ce->irq, &sirfsoc_timer1_irq));
  169. irq_set_affinity(sirfsoc_timer1_irq.irq, cpumask_of(1));
  170. clockevents_register_device(ce);
  171. return 0;
  172. }
  173. static void sirfsoc_local_timer_stop(struct clock_event_device *ce)
  174. {
  175. sirfsoc_timer_count_disable(1);
  176. remove_irq(sirfsoc_timer1_irq.irq, &sirfsoc_timer1_irq);
  177. }
  178. static struct local_timer_ops sirfsoc_local_timer_ops __cpuinitdata = {
  179. .setup = sirfsoc_local_timer_setup,
  180. .stop = sirfsoc_local_timer_stop,
  181. };
  182. #endif /* CONFIG_LOCAL_TIMERS */
  183. static void __init sirfsoc_clockevent_init(void)
  184. {
  185. clockevents_calc_mult_shift(&sirfsoc_clockevent, CLOCK_TICK_RATE, 60);
  186. sirfsoc_clockevent.max_delta_ns =
  187. clockevent_delta2ns(-2, &sirfsoc_clockevent);
  188. sirfsoc_clockevent.min_delta_ns =
  189. clockevent_delta2ns(2, &sirfsoc_clockevent);
  190. sirfsoc_clockevent.cpumask = cpumask_of(0);
  191. clockevents_register_device(&sirfsoc_clockevent);
  192. #ifdef CONFIG_LOCAL_TIMERS
  193. local_timer_register(&sirfsoc_local_timer_ops);
  194. #endif
  195. }
  196. /* initialize the kernel jiffy timer source */
  197. static void __init sirfsoc_marco_timer_init(void)
  198. {
  199. unsigned long rate;
  200. u32 timer_div;
  201. struct clk *clk;
  202. /* timer's input clock is io clock */
  203. clk = clk_get_sys("io", NULL);
  204. BUG_ON(IS_ERR(clk));
  205. rate = clk_get_rate(clk);
  206. BUG_ON(rate < CLOCK_TICK_RATE);
  207. BUG_ON(rate % CLOCK_TICK_RATE);
  208. /* Initialize the timer dividers */
  209. timer_div = rate / CLOCK_TICK_RATE - 1;
  210. writel_relaxed(timer_div << 16, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  211. writel_relaxed(timer_div << 16, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
  212. writel_relaxed(timer_div << 16, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
  213. /* Initialize timer counters to 0 */
  214. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
  215. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
  216. writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
  217. BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
  218. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
  219. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
  220. /* Clear all interrupts */
  221. writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
  222. BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, CLOCK_TICK_RATE));
  223. BUG_ON(setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq));
  224. sirfsoc_clockevent_init();
  225. }
  226. static void __init sirfsoc_of_timer_init(struct device_node *np)
  227. {
  228. sirfsoc_timer_base = of_iomap(np, 0);
  229. if (!sirfsoc_timer_base)
  230. panic("unable to map timer cpu registers\n");
  231. sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  232. if (!sirfsoc_timer_irq.irq)
  233. panic("No irq passed for timer0 via DT\n");
  234. #ifdef CONFIG_LOCAL_TIMERS
  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. #endif
  239. sirfsoc_marco_timer_init();
  240. }
  241. CLOCKSOURCE_OF_DECLARE(sirfsoc_marco_timer, "sirf,marco-tick", sirfsoc_of_timer_init );