time.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014 Zhang, Keguang <[email protected]>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/sizes.h>
  8. #include <asm/time.h>
  9. #include <loongson1.h>
  10. #include <platform.h>
  11. #ifdef CONFIG_CEVT_CSRC_LS1X
  12. #if defined(CONFIG_TIMER_USE_PWM1)
  13. #define LS1X_TIMER_BASE LS1X_PWM1_BASE
  14. #define LS1X_TIMER_IRQ LS1X_PWM1_IRQ
  15. #elif defined(CONFIG_TIMER_USE_PWM2)
  16. #define LS1X_TIMER_BASE LS1X_PWM2_BASE
  17. #define LS1X_TIMER_IRQ LS1X_PWM2_IRQ
  18. #elif defined(CONFIG_TIMER_USE_PWM3)
  19. #define LS1X_TIMER_BASE LS1X_PWM3_BASE
  20. #define LS1X_TIMER_IRQ LS1X_PWM3_IRQ
  21. #else
  22. #define LS1X_TIMER_BASE LS1X_PWM0_BASE
  23. #define LS1X_TIMER_IRQ LS1X_PWM0_IRQ
  24. #endif
  25. DEFINE_RAW_SPINLOCK(ls1x_timer_lock);
  26. static void __iomem *timer_reg_base;
  27. static uint32_t ls1x_jiffies_per_tick;
  28. static inline void ls1x_pwmtimer_set_period(uint32_t period)
  29. {
  30. __raw_writel(period, timer_reg_base + PWM_HRC);
  31. __raw_writel(period, timer_reg_base + PWM_LRC);
  32. }
  33. static inline void ls1x_pwmtimer_restart(void)
  34. {
  35. __raw_writel(0x0, timer_reg_base + PWM_CNT);
  36. __raw_writel(INT_EN | CNT_EN, timer_reg_base + PWM_CTRL);
  37. }
  38. void __init ls1x_pwmtimer_init(void)
  39. {
  40. timer_reg_base = ioremap(LS1X_TIMER_BASE, SZ_16);
  41. if (!timer_reg_base)
  42. panic("Failed to remap timer registers");
  43. ls1x_jiffies_per_tick = DIV_ROUND_CLOSEST(mips_hpt_frequency, HZ);
  44. ls1x_pwmtimer_set_period(ls1x_jiffies_per_tick);
  45. ls1x_pwmtimer_restart();
  46. }
  47. static u64 ls1x_clocksource_read(struct clocksource *cs)
  48. {
  49. unsigned long flags;
  50. int count;
  51. u32 jifs;
  52. static int old_count;
  53. static u32 old_jifs;
  54. raw_spin_lock_irqsave(&ls1x_timer_lock, flags);
  55. /*
  56. * Although our caller may have the read side of xtime_lock,
  57. * this is now a seqlock, and we are cheating in this routine
  58. * by having side effects on state that we cannot undo if
  59. * there is a collision on the seqlock and our caller has to
  60. * retry. (Namely, old_jifs and old_count.) So we must treat
  61. * jiffies as volatile despite the lock. We read jiffies
  62. * before latching the timer count to guarantee that although
  63. * the jiffies value might be older than the count (that is,
  64. * the counter may underflow between the last point where
  65. * jiffies was incremented and the point where we latch the
  66. * count), it cannot be newer.
  67. */
  68. jifs = jiffies;
  69. /* read the count */
  70. count = __raw_readl(timer_reg_base + PWM_CNT);
  71. /*
  72. * It's possible for count to appear to go the wrong way for this
  73. * reason:
  74. *
  75. * The timer counter underflows, but we haven't handled the resulting
  76. * interrupt and incremented jiffies yet.
  77. *
  78. * Previous attempts to handle these cases intelligently were buggy, so
  79. * we just do the simple thing now.
  80. */
  81. if (count < old_count && jifs == old_jifs)
  82. count = old_count;
  83. old_count = count;
  84. old_jifs = jifs;
  85. raw_spin_unlock_irqrestore(&ls1x_timer_lock, flags);
  86. return (u64) (jifs * ls1x_jiffies_per_tick) + count;
  87. }
  88. static struct clocksource ls1x_clocksource = {
  89. .name = "ls1x-pwmtimer",
  90. .read = ls1x_clocksource_read,
  91. .mask = CLOCKSOURCE_MASK(24),
  92. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  93. };
  94. static irqreturn_t ls1x_clockevent_isr(int irq, void *devid)
  95. {
  96. struct clock_event_device *cd = devid;
  97. ls1x_pwmtimer_restart();
  98. cd->event_handler(cd);
  99. return IRQ_HANDLED;
  100. }
  101. static int ls1x_clockevent_set_state_periodic(struct clock_event_device *cd)
  102. {
  103. raw_spin_lock(&ls1x_timer_lock);
  104. ls1x_pwmtimer_set_period(ls1x_jiffies_per_tick);
  105. ls1x_pwmtimer_restart();
  106. __raw_writel(INT_EN | CNT_EN, timer_reg_base + PWM_CTRL);
  107. raw_spin_unlock(&ls1x_timer_lock);
  108. return 0;
  109. }
  110. static int ls1x_clockevent_tick_resume(struct clock_event_device *cd)
  111. {
  112. raw_spin_lock(&ls1x_timer_lock);
  113. __raw_writel(INT_EN | CNT_EN, timer_reg_base + PWM_CTRL);
  114. raw_spin_unlock(&ls1x_timer_lock);
  115. return 0;
  116. }
  117. static int ls1x_clockevent_set_state_shutdown(struct clock_event_device *cd)
  118. {
  119. raw_spin_lock(&ls1x_timer_lock);
  120. __raw_writel(__raw_readl(timer_reg_base + PWM_CTRL) & ~CNT_EN,
  121. timer_reg_base + PWM_CTRL);
  122. raw_spin_unlock(&ls1x_timer_lock);
  123. return 0;
  124. }
  125. static int ls1x_clockevent_set_next(unsigned long evt,
  126. struct clock_event_device *cd)
  127. {
  128. raw_spin_lock(&ls1x_timer_lock);
  129. ls1x_pwmtimer_set_period(evt);
  130. ls1x_pwmtimer_restart();
  131. raw_spin_unlock(&ls1x_timer_lock);
  132. return 0;
  133. }
  134. static struct clock_event_device ls1x_clockevent = {
  135. .name = "ls1x-pwmtimer",
  136. .features = CLOCK_EVT_FEAT_PERIODIC,
  137. .rating = 300,
  138. .irq = LS1X_TIMER_IRQ,
  139. .set_next_event = ls1x_clockevent_set_next,
  140. .set_state_shutdown = ls1x_clockevent_set_state_shutdown,
  141. .set_state_periodic = ls1x_clockevent_set_state_periodic,
  142. .set_state_oneshot = ls1x_clockevent_set_state_shutdown,
  143. .tick_resume = ls1x_clockevent_tick_resume,
  144. };
  145. static void __init ls1x_time_init(void)
  146. {
  147. struct clock_event_device *cd = &ls1x_clockevent;
  148. int ret;
  149. if (!mips_hpt_frequency)
  150. panic("Invalid timer clock rate");
  151. ls1x_pwmtimer_init();
  152. clockevent_set_clock(cd, mips_hpt_frequency);
  153. cd->max_delta_ns = clockevent_delta2ns(0xffffff, cd);
  154. cd->max_delta_ticks = 0xffffff;
  155. cd->min_delta_ns = clockevent_delta2ns(0x000300, cd);
  156. cd->min_delta_ticks = 0x000300;
  157. cd->cpumask = cpumask_of(smp_processor_id());
  158. clockevents_register_device(cd);
  159. ls1x_clocksource.rating = 200 + mips_hpt_frequency / 10000000;
  160. ret = clocksource_register_hz(&ls1x_clocksource, mips_hpt_frequency);
  161. if (ret)
  162. panic(KERN_ERR "Failed to register clocksource: %d\n", ret);
  163. if (request_irq(LS1X_TIMER_IRQ, ls1x_clockevent_isr,
  164. IRQF_PERCPU | IRQF_TIMER, "ls1x-pwmtimer",
  165. &ls1x_clockevent))
  166. pr_err("Failed to register ls1x-pwmtimer interrupt\n");
  167. }
  168. #endif /* CONFIG_CEVT_CSRC_LS1X */
  169. void __init plat_time_init(void)
  170. {
  171. struct clk *clk = NULL;
  172. /* initialize LS1X clocks */
  173. ls1x_clk_init();
  174. #ifdef CONFIG_CEVT_CSRC_LS1X
  175. /* setup LS1X PWM timer */
  176. clk = clk_get(NULL, "ls1x-pwmtimer");
  177. if (IS_ERR(clk))
  178. panic("unable to get timer clock, err=%ld", PTR_ERR(clk));
  179. mips_hpt_frequency = clk_get_rate(clk);
  180. ls1x_time_init();
  181. #else
  182. /* setup mips r4k timer */
  183. clk = clk_get(NULL, "cpu_clk");
  184. if (IS_ERR(clk))
  185. panic("unable to get cpu clock, err=%ld", PTR_ERR(clk));
  186. mips_hpt_frequency = clk_get_rate(clk) / 2;
  187. #endif /* CONFIG_CEVT_CSRC_LS1X */
  188. }