bcm_kona_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2012 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/types.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #define KONA_GPTIMER_STCS_OFFSET 0x00000000
  25. #define KONA_GPTIMER_STCLO_OFFSET 0x00000004
  26. #define KONA_GPTIMER_STCHI_OFFSET 0x00000008
  27. #define KONA_GPTIMER_STCM0_OFFSET 0x0000000C
  28. #define KONA_GPTIMER_STCS_TIMER_MATCH_SHIFT 0
  29. #define KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT 4
  30. struct kona_bcm_timers {
  31. int tmr_irq;
  32. void __iomem *tmr_regs;
  33. };
  34. static struct kona_bcm_timers timers;
  35. static u32 arch_timer_rate;
  36. /*
  37. * We use the peripheral timers for system tick, the cpu global timer for
  38. * profile tick
  39. */
  40. static void kona_timer_disable_and_clear(void __iomem *base)
  41. {
  42. uint32_t reg;
  43. /*
  44. * clear and disable interrupts
  45. * We are using compare/match register 0 for our system interrupts
  46. */
  47. reg = readl(base + KONA_GPTIMER_STCS_OFFSET);
  48. /* Clear compare (0) interrupt */
  49. reg |= 1 << KONA_GPTIMER_STCS_TIMER_MATCH_SHIFT;
  50. /* disable compare */
  51. reg &= ~(1 << KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT);
  52. writel(reg, base + KONA_GPTIMER_STCS_OFFSET);
  53. }
  54. static int
  55. kona_timer_get_counter(void __iomem *timer_base, uint32_t *msw, uint32_t *lsw)
  56. {
  57. int loop_limit = 3;
  58. /*
  59. * Read 64-bit free running counter
  60. * 1. Read hi-word
  61. * 2. Read low-word
  62. * 3. Read hi-word again
  63. * 4.1
  64. * if new hi-word is not equal to previously read hi-word, then
  65. * start from #1
  66. * 4.2
  67. * if new hi-word is equal to previously read hi-word then stop.
  68. */
  69. do {
  70. *msw = readl(timer_base + KONA_GPTIMER_STCHI_OFFSET);
  71. *lsw = readl(timer_base + KONA_GPTIMER_STCLO_OFFSET);
  72. if (*msw == readl(timer_base + KONA_GPTIMER_STCHI_OFFSET))
  73. break;
  74. } while (--loop_limit);
  75. if (!loop_limit) {
  76. pr_err("bcm_kona_timer: getting counter failed.\n");
  77. pr_err(" Timer will be impacted\n");
  78. return -ETIMEDOUT;
  79. }
  80. return 0;
  81. }
  82. static int kona_timer_set_next_event(unsigned long clc,
  83. struct clock_event_device *unused)
  84. {
  85. /*
  86. * timer (0) is disabled by the timer interrupt already
  87. * so, here we reload the next event value and re-enable
  88. * the timer.
  89. *
  90. * This way, we are potentially losing the time between
  91. * timer-interrupt->set_next_event. CPU local timers, when
  92. * they come in should get rid of skew.
  93. */
  94. uint32_t lsw, msw;
  95. uint32_t reg;
  96. int ret;
  97. ret = kona_timer_get_counter(timers.tmr_regs, &msw, &lsw);
  98. if (ret)
  99. return ret;
  100. /* Load the "next" event tick value */
  101. writel(lsw + clc, timers.tmr_regs + KONA_GPTIMER_STCM0_OFFSET);
  102. /* Enable compare */
  103. reg = readl(timers.tmr_regs + KONA_GPTIMER_STCS_OFFSET);
  104. reg |= (1 << KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT);
  105. writel(reg, timers.tmr_regs + KONA_GPTIMER_STCS_OFFSET);
  106. return 0;
  107. }
  108. static int kona_timer_shutdown(struct clock_event_device *evt)
  109. {
  110. kona_timer_disable_and_clear(timers.tmr_regs);
  111. return 0;
  112. }
  113. static struct clock_event_device kona_clockevent_timer = {
  114. .name = "timer 1",
  115. .features = CLOCK_EVT_FEAT_ONESHOT,
  116. .set_next_event = kona_timer_set_next_event,
  117. .set_state_shutdown = kona_timer_shutdown,
  118. .tick_resume = kona_timer_shutdown,
  119. };
  120. static void __init kona_timer_clockevents_init(void)
  121. {
  122. kona_clockevent_timer.cpumask = cpumask_of(0);
  123. clockevents_config_and_register(&kona_clockevent_timer,
  124. arch_timer_rate, 6, 0xffffffff);
  125. }
  126. static irqreturn_t kona_timer_interrupt(int irq, void *dev_id)
  127. {
  128. struct clock_event_device *evt = &kona_clockevent_timer;
  129. kona_timer_disable_and_clear(timers.tmr_regs);
  130. evt->event_handler(evt);
  131. return IRQ_HANDLED;
  132. }
  133. static int __init kona_timer_init(struct device_node *node)
  134. {
  135. u32 freq;
  136. struct clk *external_clk;
  137. external_clk = of_clk_get_by_name(node, NULL);
  138. if (!IS_ERR(external_clk)) {
  139. arch_timer_rate = clk_get_rate(external_clk);
  140. clk_prepare_enable(external_clk);
  141. } else if (!of_property_read_u32(node, "clock-frequency", &freq)) {
  142. arch_timer_rate = freq;
  143. } else {
  144. pr_err("Kona Timer v1 unable to determine clock-frequency\n");
  145. return -EINVAL;
  146. }
  147. /* Setup IRQ numbers */
  148. timers.tmr_irq = irq_of_parse_and_map(node, 0);
  149. /* Setup IO addresses */
  150. timers.tmr_regs = of_iomap(node, 0);
  151. kona_timer_disable_and_clear(timers.tmr_regs);
  152. kona_timer_clockevents_init();
  153. if (request_irq(timers.tmr_irq, kona_timer_interrupt, IRQF_TIMER,
  154. "Kona Timer Tick", NULL))
  155. pr_err("%s: request_irq() failed\n", "Kona Timer Tick");
  156. kona_timer_set_next_event((arch_timer_rate / HZ), NULL);
  157. return 0;
  158. }
  159. TIMER_OF_DECLARE(brcm_kona, "brcm,kona-timer", kona_timer_init);
  160. /*
  161. * bcm,kona-timer is deprecated by brcm,kona-timer
  162. * being kept here for driver compatibility
  163. */
  164. TIMER_OF_DECLARE(bcm_kona, "bcm,kona-timer", kona_timer_init);