timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <mach/map.h>
  20. #include <asm/sched_clock.h>
  21. #include <asm/mach/time.h>
  22. #define SIRFSOC_TIMER_COUNTER_LO 0x0000
  23. #define SIRFSOC_TIMER_COUNTER_HI 0x0004
  24. #define SIRFSOC_TIMER_MATCH_0 0x0008
  25. #define SIRFSOC_TIMER_MATCH_1 0x000C
  26. #define SIRFSOC_TIMER_MATCH_2 0x0010
  27. #define SIRFSOC_TIMER_MATCH_3 0x0014
  28. #define SIRFSOC_TIMER_MATCH_4 0x0018
  29. #define SIRFSOC_TIMER_MATCH_5 0x001C
  30. #define SIRFSOC_TIMER_STATUS 0x0020
  31. #define SIRFSOC_TIMER_INT_EN 0x0024
  32. #define SIRFSOC_TIMER_WATCHDOG_EN 0x0028
  33. #define SIRFSOC_TIMER_DIV 0x002C
  34. #define SIRFSOC_TIMER_LATCH 0x0030
  35. #define SIRFSOC_TIMER_LATCHED_LO 0x0034
  36. #define SIRFSOC_TIMER_LATCHED_HI 0x0038
  37. #define SIRFSOC_TIMER_WDT_INDEX 5
  38. #define SIRFSOC_TIMER_LATCH_BIT BIT(0)
  39. #define SIRFSOC_TIMER_REG_CNT 11
  40. static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
  41. SIRFSOC_TIMER_MATCH_0, SIRFSOC_TIMER_MATCH_1, SIRFSOC_TIMER_MATCH_2,
  42. SIRFSOC_TIMER_MATCH_3, SIRFSOC_TIMER_MATCH_4, SIRFSOC_TIMER_MATCH_5,
  43. SIRFSOC_TIMER_INT_EN, SIRFSOC_TIMER_WATCHDOG_EN, SIRFSOC_TIMER_DIV,
  44. SIRFSOC_TIMER_LATCHED_LO, SIRFSOC_TIMER_LATCHED_HI,
  45. };
  46. static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
  47. static void __iomem *sirfsoc_timer_base;
  48. static void __init sirfsoc_of_timer_map(void);
  49. /* timer0 interrupt handler */
  50. static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
  51. {
  52. struct clock_event_device *ce = dev_id;
  53. WARN_ON(!(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_STATUS) & BIT(0)));
  54. /* clear timer0 interrupt */
  55. writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
  56. ce->event_handler(ce);
  57. return IRQ_HANDLED;
  58. }
  59. /* read 64-bit timer counter */
  60. static cycle_t sirfsoc_timer_read(struct clocksource *cs)
  61. {
  62. u64 cycles;
  63. /* latch the 64-bit timer counter */
  64. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  65. cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI);
  66. cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  67. return cycles;
  68. }
  69. static int sirfsoc_timer_set_next_event(unsigned long delta,
  70. struct clock_event_device *ce)
  71. {
  72. unsigned long now, next;
  73. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  74. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  75. next = now + delta;
  76. writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0);
  77. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  78. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  79. return next - now > delta ? -ETIME : 0;
  80. }
  81. static void sirfsoc_timer_set_mode(enum clock_event_mode mode,
  82. struct clock_event_device *ce)
  83. {
  84. u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  85. switch (mode) {
  86. case CLOCK_EVT_MODE_PERIODIC:
  87. WARN_ON(1);
  88. break;
  89. case CLOCK_EVT_MODE_ONESHOT:
  90. writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  91. break;
  92. case CLOCK_EVT_MODE_SHUTDOWN:
  93. writel_relaxed(val & ~BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  94. break;
  95. case CLOCK_EVT_MODE_UNUSED:
  96. case CLOCK_EVT_MODE_RESUME:
  97. break;
  98. }
  99. }
  100. static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  101. {
  102. int i;
  103. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  104. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  105. sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  106. }
  107. static void sirfsoc_clocksource_resume(struct clocksource *cs)
  108. {
  109. int i;
  110. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  111. writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  112. writel_relaxed(sirfsoc_timer_reg_val[i - 2], sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  113. writel_relaxed(sirfsoc_timer_reg_val[i - 1], sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  114. }
  115. static struct clock_event_device sirfsoc_clockevent = {
  116. .name = "sirfsoc_clockevent",
  117. .rating = 200,
  118. .features = CLOCK_EVT_FEAT_ONESHOT,
  119. .set_mode = sirfsoc_timer_set_mode,
  120. .set_next_event = sirfsoc_timer_set_next_event,
  121. };
  122. static struct clocksource sirfsoc_clocksource = {
  123. .name = "sirfsoc_clocksource",
  124. .rating = 200,
  125. .mask = CLOCKSOURCE_MASK(64),
  126. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  127. .read = sirfsoc_timer_read,
  128. .suspend = sirfsoc_clocksource_suspend,
  129. .resume = sirfsoc_clocksource_resume,
  130. };
  131. static struct irqaction sirfsoc_timer_irq = {
  132. .name = "sirfsoc_timer0",
  133. .flags = IRQF_TIMER,
  134. .irq = 0,
  135. .handler = sirfsoc_timer_interrupt,
  136. .dev_id = &sirfsoc_clockevent,
  137. };
  138. /* Overwrite weak default sched_clock with more precise one */
  139. static u32 notrace sirfsoc_read_sched_clock(void)
  140. {
  141. return (u32)(sirfsoc_timer_read(NULL) & 0xffffffff);
  142. }
  143. static void __init sirfsoc_clockevent_init(void)
  144. {
  145. clockevents_calc_mult_shift(&sirfsoc_clockevent, CLOCK_TICK_RATE, 60);
  146. sirfsoc_clockevent.max_delta_ns =
  147. clockevent_delta2ns(-2, &sirfsoc_clockevent);
  148. sirfsoc_clockevent.min_delta_ns =
  149. clockevent_delta2ns(2, &sirfsoc_clockevent);
  150. sirfsoc_clockevent.cpumask = cpumask_of(0);
  151. clockevents_register_device(&sirfsoc_clockevent);
  152. }
  153. /* initialize the kernel jiffy timer source */
  154. static void __init sirfsoc_timer_init(void)
  155. {
  156. unsigned long rate;
  157. /* timer's input clock is io clock */
  158. struct clk *clk = clk_get_sys("io", NULL);
  159. BUG_ON(IS_ERR(clk));
  160. rate = clk_get_rate(clk);
  161. BUG_ON(rate < CLOCK_TICK_RATE);
  162. BUG_ON(rate % CLOCK_TICK_RATE);
  163. sirfsoc_of_timer_map();
  164. writel_relaxed(rate / CLOCK_TICK_RATE / 2 - 1, sirfsoc_timer_base + SIRFSOC_TIMER_DIV);
  165. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  166. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  167. writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
  168. BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, CLOCK_TICK_RATE));
  169. setup_sched_clock(sirfsoc_read_sched_clock, 32, CLOCK_TICK_RATE);
  170. BUG_ON(setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq));
  171. sirfsoc_clockevent_init();
  172. }
  173. static struct of_device_id timer_ids[] = {
  174. { .compatible = "sirf,prima2-tick" },
  175. {},
  176. };
  177. static void __init sirfsoc_of_timer_map(void)
  178. {
  179. struct device_node *np;
  180. const unsigned int *intspec;
  181. np = of_find_matching_node(NULL, timer_ids);
  182. if (!np)
  183. panic("unable to find compatible timer node in dtb\n");
  184. sirfsoc_timer_base = of_iomap(np, 0);
  185. if (!sirfsoc_timer_base)
  186. panic("unable to map timer cpu registers\n");
  187. /* Get the interrupts property */
  188. intspec = of_get_property(np, "interrupts", NULL);
  189. BUG_ON(!intspec);
  190. sirfsoc_timer_irq.irq = be32_to_cpup(intspec);
  191. of_node_put(np);
  192. }
  193. struct sys_timer sirfsoc_timer = {
  194. .init = sirfsoc_timer_init,
  195. };