bcm_kona_timer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <asm/mach/time.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #define KONA_GPTIMER_STCS_OFFSET 0x00000000
  26. #define KONA_GPTIMER_STCLO_OFFSET 0x00000004
  27. #define KONA_GPTIMER_STCHI_OFFSET 0x00000008
  28. #define KONA_GPTIMER_STCM0_OFFSET 0x0000000C
  29. #define KONA_GPTIMER_STCS_TIMER_MATCH_SHIFT 0
  30. #define KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT 4
  31. struct kona_bcm_timers {
  32. int tmr_irq;
  33. void __iomem *tmr_regs;
  34. };
  35. static struct kona_bcm_timers timers;
  36. static u32 arch_timer_rate;
  37. /*
  38. * We use the peripheral timers for system tick, the cpu global timer for
  39. * profile tick
  40. */
  41. static void kona_timer_disable_and_clear(void __iomem *base)
  42. {
  43. uint32_t reg;
  44. /*
  45. * clear and disable interrupts
  46. * We are using compare/match register 0 for our system interrupts
  47. */
  48. reg = readl(base + KONA_GPTIMER_STCS_OFFSET);
  49. /* Clear compare (0) interrupt */
  50. reg |= 1 << KONA_GPTIMER_STCS_TIMER_MATCH_SHIFT;
  51. /* disable compare */
  52. reg &= ~(1 << KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT);
  53. writel(reg, base + KONA_GPTIMER_STCS_OFFSET);
  54. }
  55. static void
  56. kona_timer_get_counter(void __iomem *timer_base, uint32_t *msw, uint32_t *lsw)
  57. {
  58. int loop_limit = 4;
  59. /*
  60. * Read 64-bit free running counter
  61. * 1. Read hi-word
  62. * 2. Read low-word
  63. * 3. Read hi-word again
  64. * 4.1
  65. * if new hi-word is not equal to previously read hi-word, then
  66. * start from #1
  67. * 4.2
  68. * if new hi-word is equal to previously read hi-word then stop.
  69. */
  70. while (--loop_limit) {
  71. *msw = readl(timer_base + KONA_GPTIMER_STCHI_OFFSET);
  72. *lsw = readl(timer_base + KONA_GPTIMER_STCLO_OFFSET);
  73. if (*msw == readl(timer_base + KONA_GPTIMER_STCHI_OFFSET))
  74. break;
  75. }
  76. if (!loop_limit) {
  77. pr_err("bcm_kona_timer: getting counter failed.\n");
  78. pr_err(" Timer will be impacted\n");
  79. }
  80. return;
  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. kona_timer_get_counter(timers.tmr_regs, &msw, &lsw);
  97. /* Load the "next" event tick value */
  98. writel(lsw + clc, timers.tmr_regs + KONA_GPTIMER_STCM0_OFFSET);
  99. /* Enable compare */
  100. reg = readl(timers.tmr_regs + KONA_GPTIMER_STCS_OFFSET);
  101. reg |= (1 << KONA_GPTIMER_STCS_COMPARE_ENABLE_SHIFT);
  102. writel(reg, timers.tmr_regs + KONA_GPTIMER_STCS_OFFSET);
  103. return 0;
  104. }
  105. static int kona_timer_shutdown(struct clock_event_device *evt)
  106. {
  107. kona_timer_disable_and_clear(timers.tmr_regs);
  108. return 0;
  109. }
  110. static struct clock_event_device kona_clockevent_timer = {
  111. .name = "timer 1",
  112. .features = CLOCK_EVT_FEAT_ONESHOT,
  113. .set_next_event = kona_timer_set_next_event,
  114. .set_state_shutdown = kona_timer_shutdown,
  115. .tick_resume = kona_timer_shutdown,
  116. };
  117. static void __init kona_timer_clockevents_init(void)
  118. {
  119. kona_clockevent_timer.cpumask = cpumask_of(0);
  120. clockevents_config_and_register(&kona_clockevent_timer,
  121. arch_timer_rate, 6, 0xffffffff);
  122. }
  123. static irqreturn_t kona_timer_interrupt(int irq, void *dev_id)
  124. {
  125. struct clock_event_device *evt = &kona_clockevent_timer;
  126. kona_timer_disable_and_clear(timers.tmr_regs);
  127. evt->event_handler(evt);
  128. return IRQ_HANDLED;
  129. }
  130. static struct irqaction kona_timer_irq = {
  131. .name = "Kona Timer Tick",
  132. .flags = IRQF_TIMER,
  133. .handler = kona_timer_interrupt,
  134. };
  135. static void __init kona_timer_init(struct device_node *node)
  136. {
  137. u32 freq;
  138. struct clk *external_clk;
  139. if (!of_device_is_available(node)) {
  140. pr_info("Kona Timer v1 marked as disabled in device tree\n");
  141. return;
  142. }
  143. external_clk = of_clk_get_by_name(node, NULL);
  144. if (!IS_ERR(external_clk)) {
  145. arch_timer_rate = clk_get_rate(external_clk);
  146. clk_prepare_enable(external_clk);
  147. } else if (!of_property_read_u32(node, "clock-frequency", &freq)) {
  148. arch_timer_rate = freq;
  149. } else {
  150. pr_err("Kona Timer v1 unable to determine clock-frequency");
  151. return;
  152. }
  153. /* Setup IRQ numbers */
  154. timers.tmr_irq = irq_of_parse_and_map(node, 0);
  155. /* Setup IO addresses */
  156. timers.tmr_regs = of_iomap(node, 0);
  157. kona_timer_disable_and_clear(timers.tmr_regs);
  158. kona_timer_clockevents_init();
  159. setup_irq(timers.tmr_irq, &kona_timer_irq);
  160. kona_timer_set_next_event((arch_timer_rate / HZ), NULL);
  161. }
  162. CLOCKSOURCE_OF_DECLARE(brcm_kona, "brcm,kona-timer", kona_timer_init);
  163. /*
  164. * bcm,kona-timer is deprecated by brcm,kona-timer
  165. * being kept here for driver compatibility
  166. */
  167. CLOCKSOURCE_OF_DECLARE(bcm_kona, "bcm,kona-timer", kona_timer_init);