timer-prima2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/sched_clock.h>
  21. #include <asm/mach/time.h>
  22. #define PRIMA2_CLOCK_FREQ 1000000
  23. #define SIRFSOC_TIMER_COUNTER_LO 0x0000
  24. #define SIRFSOC_TIMER_COUNTER_HI 0x0004
  25. #define SIRFSOC_TIMER_MATCH_0 0x0008
  26. #define SIRFSOC_TIMER_MATCH_1 0x000C
  27. #define SIRFSOC_TIMER_MATCH_2 0x0010
  28. #define SIRFSOC_TIMER_MATCH_3 0x0014
  29. #define SIRFSOC_TIMER_MATCH_4 0x0018
  30. #define SIRFSOC_TIMER_MATCH_5 0x001C
  31. #define SIRFSOC_TIMER_STATUS 0x0020
  32. #define SIRFSOC_TIMER_INT_EN 0x0024
  33. #define SIRFSOC_TIMER_WATCHDOG_EN 0x0028
  34. #define SIRFSOC_TIMER_DIV 0x002C
  35. #define SIRFSOC_TIMER_LATCH 0x0030
  36. #define SIRFSOC_TIMER_LATCHED_LO 0x0034
  37. #define SIRFSOC_TIMER_LATCHED_HI 0x0038
  38. #define SIRFSOC_TIMER_WDT_INDEX 5
  39. #define SIRFSOC_TIMER_LATCH_BIT BIT(0)
  40. #define SIRFSOC_TIMER_REG_CNT 11
  41. static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
  42. SIRFSOC_TIMER_MATCH_0, SIRFSOC_TIMER_MATCH_1, SIRFSOC_TIMER_MATCH_2,
  43. SIRFSOC_TIMER_MATCH_3, SIRFSOC_TIMER_MATCH_4, SIRFSOC_TIMER_MATCH_5,
  44. SIRFSOC_TIMER_INT_EN, SIRFSOC_TIMER_WATCHDOG_EN, SIRFSOC_TIMER_DIV,
  45. SIRFSOC_TIMER_LATCHED_LO, SIRFSOC_TIMER_LATCHED_HI,
  46. };
  47. static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
  48. static void __iomem *sirfsoc_timer_base;
  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) &
  54. BIT(0)));
  55. /* clear timer0 interrupt */
  56. writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
  57. ce->event_handler(ce);
  58. return IRQ_HANDLED;
  59. }
  60. /* read 64-bit timer counter */
  61. static cycle_t notrace sirfsoc_timer_read(struct clocksource *cs)
  62. {
  63. u64 cycles;
  64. /* latch the 64-bit timer counter */
  65. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  66. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  67. cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI);
  68. cycles = (cycles << 32) |
  69. readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  70. return cycles;
  71. }
  72. static int sirfsoc_timer_set_next_event(unsigned long delta,
  73. struct clock_event_device *ce)
  74. {
  75. unsigned long now, next;
  76. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  77. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  78. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  79. next = now + delta;
  80. writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0);
  81. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  82. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  83. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  84. return next - now > delta ? -ETIME : 0;
  85. }
  86. static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
  87. {
  88. u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  89. writel_relaxed(val & ~BIT(0),
  90. sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  91. return 0;
  92. }
  93. static int sirfsoc_timer_set_oneshot(struct clock_event_device *evt)
  94. {
  95. u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  96. writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  97. return 0;
  98. }
  99. static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  100. {
  101. int i;
  102. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  103. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  104. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  105. sirfsoc_timer_reg_val[i] =
  106. readl_relaxed(sirfsoc_timer_base +
  107. sirfsoc_timer_reg_list[i]);
  108. }
  109. static void sirfsoc_clocksource_resume(struct clocksource *cs)
  110. {
  111. int i;
  112. for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
  113. writel_relaxed(sirfsoc_timer_reg_val[i],
  114. sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  115. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
  116. sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  117. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
  118. sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  119. }
  120. static struct clock_event_device sirfsoc_clockevent = {
  121. .name = "sirfsoc_clockevent",
  122. .rating = 200,
  123. .features = CLOCK_EVT_FEAT_ONESHOT,
  124. .set_state_shutdown = sirfsoc_timer_shutdown,
  125. .set_state_oneshot = sirfsoc_timer_set_oneshot,
  126. .set_next_event = sirfsoc_timer_set_next_event,
  127. };
  128. static struct clocksource sirfsoc_clocksource = {
  129. .name = "sirfsoc_clocksource",
  130. .rating = 200,
  131. .mask = CLOCKSOURCE_MASK(64),
  132. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  133. .read = sirfsoc_timer_read,
  134. .suspend = sirfsoc_clocksource_suspend,
  135. .resume = sirfsoc_clocksource_resume,
  136. };
  137. static struct irqaction sirfsoc_timer_irq = {
  138. .name = "sirfsoc_timer0",
  139. .flags = IRQF_TIMER,
  140. .irq = 0,
  141. .handler = sirfsoc_timer_interrupt,
  142. .dev_id = &sirfsoc_clockevent,
  143. };
  144. /* Overwrite weak default sched_clock with more precise one */
  145. static u64 notrace sirfsoc_read_sched_clock(void)
  146. {
  147. return sirfsoc_timer_read(NULL);
  148. }
  149. static void __init sirfsoc_clockevent_init(void)
  150. {
  151. sirfsoc_clockevent.cpumask = cpumask_of(0);
  152. clockevents_config_and_register(&sirfsoc_clockevent, PRIMA2_CLOCK_FREQ,
  153. 2, -2);
  154. }
  155. /* initialize the kernel jiffy timer source */
  156. static void __init sirfsoc_prima2_timer_init(struct device_node *np)
  157. {
  158. unsigned long rate;
  159. struct clk *clk;
  160. clk = of_clk_get(np, 0);
  161. BUG_ON(IS_ERR(clk));
  162. BUG_ON(clk_prepare_enable(clk));
  163. rate = clk_get_rate(clk);
  164. BUG_ON(rate < PRIMA2_CLOCK_FREQ);
  165. BUG_ON(rate % PRIMA2_CLOCK_FREQ);
  166. sirfsoc_timer_base = of_iomap(np, 0);
  167. if (!sirfsoc_timer_base)
  168. panic("unable to map timer cpu registers\n");
  169. sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  170. writel_relaxed(rate / PRIMA2_CLOCK_FREQ / 2 - 1,
  171. sirfsoc_timer_base + SIRFSOC_TIMER_DIV);
  172. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  173. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  174. writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
  175. BUG_ON(clocksource_register_hz(&sirfsoc_clocksource,
  176. PRIMA2_CLOCK_FREQ));
  177. sched_clock_register(sirfsoc_read_sched_clock, 64, PRIMA2_CLOCK_FREQ);
  178. BUG_ON(setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq));
  179. sirfsoc_clockevent_init();
  180. }
  181. CLOCKSOURCE_OF_DECLARE(sirfsoc_prima2_timer,
  182. "sirf,prima2-tick", sirfsoc_prima2_timer_init);