time.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 void ls1x_clockevent_set_mode(enum clock_event_mode mode,
  105. struct clock_event_device *cd)
  106. {
  107. raw_spin_lock(&ls1x_timer_lock);
  108. switch (mode) {
  109. case CLOCK_EVT_MODE_PERIODIC:
  110. ls1x_pwmtimer_set_period(ls1x_jiffies_per_tick);
  111. ls1x_pwmtimer_restart();
  112. case CLOCK_EVT_MODE_RESUME:
  113. __raw_writel(INT_EN | CNT_EN, timer_base + PWM_CTRL);
  114. break;
  115. case CLOCK_EVT_MODE_ONESHOT:
  116. case CLOCK_EVT_MODE_SHUTDOWN:
  117. __raw_writel(__raw_readl(timer_base + PWM_CTRL) & ~CNT_EN,
  118. timer_base + PWM_CTRL);
  119. break;
  120. default:
  121. break;
  122. }
  123. raw_spin_unlock(&ls1x_timer_lock);
  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_mode = ls1x_clockevent_set_mode,
  141. };
  142. static struct irqaction ls1x_pwmtimer_irqaction = {
  143. .name = "ls1x-pwmtimer",
  144. .handler = ls1x_clockevent_isr,
  145. .dev_id = &ls1x_clockevent,
  146. .flags = IRQF_PERCPU | IRQF_TIMER,
  147. };
  148. static void __init ls1x_time_init(void)
  149. {
  150. struct clock_event_device *cd = &ls1x_clockevent;
  151. int ret;
  152. if (!mips_hpt_frequency)
  153. panic("Invalid timer clock rate");
  154. ls1x_pwmtimer_init();
  155. clockevent_set_clock(cd, mips_hpt_frequency);
  156. cd->max_delta_ns = clockevent_delta2ns(0xffffff, cd);
  157. cd->min_delta_ns = clockevent_delta2ns(0x000300, cd);
  158. cd->cpumask = cpumask_of(smp_processor_id());
  159. clockevents_register_device(cd);
  160. ls1x_clocksource.rating = 200 + mips_hpt_frequency / 10000000;
  161. ret = clocksource_register_hz(&ls1x_clocksource, mips_hpt_frequency);
  162. if (ret)
  163. panic(KERN_ERR "Failed to register clocksource: %d\n", ret);
  164. setup_irq(LS1X_TIMER_IRQ, &ls1x_pwmtimer_irqaction);
  165. }
  166. #endif /* CONFIG_CEVT_CSRC_LS1X */
  167. void __init plat_time_init(void)
  168. {
  169. struct clk *clk = NULL;
  170. /* initialize LS1X clocks */
  171. ls1x_clk_init();
  172. #ifdef CONFIG_CEVT_CSRC_LS1X
  173. /* setup LS1X PWM timer */
  174. clk = clk_get(NULL, "ls1x_pwmtimer");
  175. if (IS_ERR(clk))
  176. panic("unable to get timer clock, err=%ld", PTR_ERR(clk));
  177. mips_hpt_frequency = clk_get_rate(clk);
  178. ls1x_time_init();
  179. #else
  180. /* setup mips r4k timer */
  181. clk = clk_get(NULL, "cpu_clk");
  182. if (IS_ERR(clk))
  183. panic("unable to get cpu clock, err=%ld", PTR_ERR(clk));
  184. mips_hpt_frequency = clk_get_rate(clk) / 2;
  185. #endif /* CONFIG_CEVT_CSRC_LS1X */
  186. }