time.c 6.2 KB

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