time.c 6.3 KB

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