timer-prima2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #define PRIMA2_CLOCK_FREQ 1000000
  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. /* timer0 interrupt handler */
  49. static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
  50. {
  51. struct clock_event_device *ce = dev_id;
  52. WARN_ON(!(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_STATUS) &
  53. 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 notrace sirfsoc_timer_read(struct clocksource *cs)
  61. {
  62. u64 cycles;
  63. /* latch the 64-bit timer counter */
  64. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  65. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  66. cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI);
  67. cycles = (cycles << 32) |
  68. readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  69. return cycles;
  70. }
  71. static int sirfsoc_timer_set_next_event(unsigned long delta,
  72. struct clock_event_device *ce)
  73. {
  74. unsigned long now, next;
  75. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  76. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  77. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  78. next = now + delta;
  79. writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0);
  80. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  81. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  82. now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
  83. return next - now > delta ? -ETIME : 0;
  84. }
  85. static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
  86. {
  87. u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  88. writel_relaxed(val & ~BIT(0),
  89. sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  90. return 0;
  91. }
  92. static int sirfsoc_timer_set_oneshot(struct clock_event_device *evt)
  93. {
  94. u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  95. writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
  96. return 0;
  97. }
  98. static void sirfsoc_clocksource_suspend(struct clocksource *cs)
  99. {
  100. int i;
  101. writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
  102. sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
  103. for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
  104. sirfsoc_timer_reg_val[i] =
  105. readl_relaxed(sirfsoc_timer_base +
  106. 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],
  113. sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
  114. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
  115. sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  116. writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
  117. sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  118. }
  119. static struct clock_event_device sirfsoc_clockevent = {
  120. .name = "sirfsoc_clockevent",
  121. .rating = 200,
  122. .features = CLOCK_EVT_FEAT_ONESHOT,
  123. .set_state_shutdown = sirfsoc_timer_shutdown,
  124. .set_state_oneshot = sirfsoc_timer_set_oneshot,
  125. .set_next_event = sirfsoc_timer_set_next_event,
  126. };
  127. static struct clocksource sirfsoc_clocksource = {
  128. .name = "sirfsoc_clocksource",
  129. .rating = 200,
  130. .mask = CLOCKSOURCE_MASK(64),
  131. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  132. .read = sirfsoc_timer_read,
  133. .suspend = sirfsoc_clocksource_suspend,
  134. .resume = sirfsoc_clocksource_resume,
  135. };
  136. static struct irqaction sirfsoc_timer_irq = {
  137. .name = "sirfsoc_timer0",
  138. .flags = IRQF_TIMER,
  139. .irq = 0,
  140. .handler = sirfsoc_timer_interrupt,
  141. .dev_id = &sirfsoc_clockevent,
  142. };
  143. /* Overwrite weak default sched_clock with more precise one */
  144. static u64 notrace sirfsoc_read_sched_clock(void)
  145. {
  146. return sirfsoc_timer_read(NULL);
  147. }
  148. static void __init sirfsoc_clockevent_init(void)
  149. {
  150. sirfsoc_clockevent.cpumask = cpumask_of(0);
  151. clockevents_config_and_register(&sirfsoc_clockevent, PRIMA2_CLOCK_FREQ,
  152. 2, -2);
  153. }
  154. /* initialize the kernel jiffy timer source */
  155. static int __init sirfsoc_prima2_timer_init(struct device_node *np)
  156. {
  157. unsigned long rate;
  158. struct clk *clk;
  159. int ret;
  160. clk = of_clk_get(np, 0);
  161. if (IS_ERR(clk)) {
  162. pr_err("Failed to get clock");
  163. return PTR_ERR(clk);
  164. }
  165. ret = clk_prepare_enable(clk);
  166. if (ret) {
  167. pr_err("Failed to enable clock");
  168. return ret;
  169. }
  170. rate = clk_get_rate(clk);
  171. if (rate < PRIMA2_CLOCK_FREQ || rate % PRIMA2_CLOCK_FREQ) {
  172. pr_err("Invalid clock rate");
  173. return -EINVAL;
  174. }
  175. sirfsoc_timer_base = of_iomap(np, 0);
  176. if (!sirfsoc_timer_base) {
  177. pr_err("unable to map timer cpu registers\n");
  178. return -ENXIO;
  179. }
  180. sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
  181. writel_relaxed(rate / PRIMA2_CLOCK_FREQ / 2 - 1,
  182. sirfsoc_timer_base + SIRFSOC_TIMER_DIV);
  183. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
  184. writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
  185. writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
  186. ret = clocksource_register_hz(&sirfsoc_clocksource, PRIMA2_CLOCK_FREQ);
  187. if (ret) {
  188. pr_err("Failed to register clocksource");
  189. return ret;
  190. }
  191. sched_clock_register(sirfsoc_read_sched_clock, 64, PRIMA2_CLOCK_FREQ);
  192. ret = setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq);
  193. if (ret) {
  194. pr_err("Failed to setup irq");
  195. return ret;
  196. }
  197. sirfsoc_clockevent_init();
  198. return 0;
  199. }
  200. CLOCKSOURCE_OF_DECLARE(sirfsoc_prima2_timer,
  201. "sirf,prima2-tick", sirfsoc_prima2_timer_init);