timer.c 5.9 KB

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