2 * Copyright (c) 2014 Zhang, Keguang <keguang.zhang@gmail.com>
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.
10 #include <linux/clk.h>
11 #include <linux/interrupt.h>
12 #include <linux/sizes.h>
15 #include <loongson1.h>
18 #ifdef CONFIG_CEVT_CSRC_LS1X
20 #if defined(CONFIG_TIMER_USE_PWM1)
21 #define LS1X_TIMER_BASE LS1X_PWM1_BASE
22 #define LS1X_TIMER_IRQ LS1X_PWM1_IRQ
24 #elif defined(CONFIG_TIMER_USE_PWM2)
25 #define LS1X_TIMER_BASE LS1X_PWM2_BASE
26 #define LS1X_TIMER_IRQ LS1X_PWM2_IRQ
28 #elif defined(CONFIG_TIMER_USE_PWM3)
29 #define LS1X_TIMER_BASE LS1X_PWM3_BASE
30 #define LS1X_TIMER_IRQ LS1X_PWM3_IRQ
33 #define LS1X_TIMER_BASE LS1X_PWM0_BASE
34 #define LS1X_TIMER_IRQ LS1X_PWM0_IRQ
37 DEFINE_RAW_SPINLOCK(ls1x_timer_lock
);
39 static void __iomem
*timer_reg_base
;
40 static uint32_t ls1x_jiffies_per_tick
;
42 static inline void ls1x_pwmtimer_set_period(uint32_t period
)
44 __raw_writel(period
, timer_reg_base
+ PWM_HRC
);
45 __raw_writel(period
, timer_reg_base
+ PWM_LRC
);
48 static inline void ls1x_pwmtimer_restart(void)
50 __raw_writel(0x0, timer_reg_base
+ PWM_CNT
);
51 __raw_writel(INT_EN
| CNT_EN
, timer_reg_base
+ PWM_CTRL
);
54 void __init
ls1x_pwmtimer_init(void)
56 timer_reg_base
= ioremap_nocache(LS1X_TIMER_BASE
, SZ_16
);
58 panic("Failed to remap timer registers");
60 ls1x_jiffies_per_tick
= DIV_ROUND_CLOSEST(mips_hpt_frequency
, HZ
);
62 ls1x_pwmtimer_set_period(ls1x_jiffies_per_tick
);
63 ls1x_pwmtimer_restart();
66 static u64
ls1x_clocksource_read(struct clocksource
*cs
)
74 raw_spin_lock_irqsave(&ls1x_timer_lock
, flags
);
76 * Although our caller may have the read side of xtime_lock,
77 * this is now a seqlock, and we are cheating in this routine
78 * by having side effects on state that we cannot undo if
79 * there is a collision on the seqlock and our caller has to
80 * retry. (Namely, old_jifs and old_count.) So we must treat
81 * jiffies as volatile despite the lock. We read jiffies
82 * before latching the timer count to guarantee that although
83 * the jiffies value might be older than the count (that is,
84 * the counter may underflow between the last point where
85 * jiffies was incremented and the point where we latch the
86 * count), it cannot be newer.
90 count
= __raw_readl(timer_reg_base
+ PWM_CNT
);
93 * It's possible for count to appear to go the wrong way for this
96 * The timer counter underflows, but we haven't handled the resulting
97 * interrupt and incremented jiffies yet.
99 * Previous attempts to handle these cases intelligently were buggy, so
100 * we just do the simple thing now.
102 if (count
< old_count
&& jifs
== old_jifs
)
108 raw_spin_unlock_irqrestore(&ls1x_timer_lock
, flags
);
110 return (u64
) (jifs
* ls1x_jiffies_per_tick
) + count
;
113 static struct clocksource ls1x_clocksource
= {
114 .name
= "ls1x-pwmtimer",
115 .read
= ls1x_clocksource_read
,
116 .mask
= CLOCKSOURCE_MASK(24),
117 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
120 static irqreturn_t
ls1x_clockevent_isr(int irq
, void *devid
)
122 struct clock_event_device
*cd
= devid
;
124 ls1x_pwmtimer_restart();
125 cd
->event_handler(cd
);
130 static int ls1x_clockevent_set_state_periodic(struct clock_event_device
*cd
)
132 raw_spin_lock(&ls1x_timer_lock
);
133 ls1x_pwmtimer_set_period(ls1x_jiffies_per_tick
);
134 ls1x_pwmtimer_restart();
135 __raw_writel(INT_EN
| CNT_EN
, timer_reg_base
+ PWM_CTRL
);
136 raw_spin_unlock(&ls1x_timer_lock
);
141 static int ls1x_clockevent_tick_resume(struct clock_event_device
*cd
)
143 raw_spin_lock(&ls1x_timer_lock
);
144 __raw_writel(INT_EN
| CNT_EN
, timer_reg_base
+ PWM_CTRL
);
145 raw_spin_unlock(&ls1x_timer_lock
);
150 static int ls1x_clockevent_set_state_shutdown(struct clock_event_device
*cd
)
152 raw_spin_lock(&ls1x_timer_lock
);
153 __raw_writel(__raw_readl(timer_reg_base
+ PWM_CTRL
) & ~CNT_EN
,
154 timer_reg_base
+ PWM_CTRL
);
155 raw_spin_unlock(&ls1x_timer_lock
);
160 static int ls1x_clockevent_set_next(unsigned long evt
,
161 struct clock_event_device
*cd
)
163 raw_spin_lock(&ls1x_timer_lock
);
164 ls1x_pwmtimer_set_period(evt
);
165 ls1x_pwmtimer_restart();
166 raw_spin_unlock(&ls1x_timer_lock
);
171 static struct clock_event_device ls1x_clockevent
= {
172 .name
= "ls1x-pwmtimer",
173 .features
= CLOCK_EVT_FEAT_PERIODIC
,
175 .irq
= LS1X_TIMER_IRQ
,
176 .set_next_event
= ls1x_clockevent_set_next
,
177 .set_state_shutdown
= ls1x_clockevent_set_state_shutdown
,
178 .set_state_periodic
= ls1x_clockevent_set_state_periodic
,
179 .set_state_oneshot
= ls1x_clockevent_set_state_shutdown
,
180 .tick_resume
= ls1x_clockevent_tick_resume
,
183 static struct irqaction ls1x_pwmtimer_irqaction
= {
184 .name
= "ls1x-pwmtimer",
185 .handler
= ls1x_clockevent_isr
,
186 .dev_id
= &ls1x_clockevent
,
187 .flags
= IRQF_PERCPU
| IRQF_TIMER
,
190 static void __init
ls1x_time_init(void)
192 struct clock_event_device
*cd
= &ls1x_clockevent
;
195 if (!mips_hpt_frequency
)
196 panic("Invalid timer clock rate");
198 ls1x_pwmtimer_init();
200 clockevent_set_clock(cd
, mips_hpt_frequency
);
201 cd
->max_delta_ns
= clockevent_delta2ns(0xffffff, cd
);
202 cd
->max_delta_ticks
= 0xffffff;
203 cd
->min_delta_ns
= clockevent_delta2ns(0x000300, cd
);
204 cd
->min_delta_ticks
= 0x000300;
205 cd
->cpumask
= cpumask_of(smp_processor_id());
206 clockevents_register_device(cd
);
208 ls1x_clocksource
.rating
= 200 + mips_hpt_frequency
/ 10000000;
209 ret
= clocksource_register_hz(&ls1x_clocksource
, mips_hpt_frequency
);
211 panic(KERN_ERR
"Failed to register clocksource: %d\n", ret
);
213 setup_irq(LS1X_TIMER_IRQ
, &ls1x_pwmtimer_irqaction
);
215 #endif /* CONFIG_CEVT_CSRC_LS1X */
217 void __init
plat_time_init(void)
219 struct clk
*clk
= NULL
;
221 /* initialize LS1X clocks */
224 #ifdef CONFIG_CEVT_CSRC_LS1X
225 /* setup LS1X PWM timer */
226 clk
= clk_get(NULL
, "ls1x-pwmtimer");
228 panic("unable to get timer clock, err=%ld", PTR_ERR(clk
));
230 mips_hpt_frequency
= clk_get_rate(clk
);
233 /* setup mips r4k timer */
234 clk
= clk_get(NULL
, "cpu_clk");
236 panic("unable to get cpu clock, err=%ld", PTR_ERR(clk
));
238 mips_hpt_frequency
= clk_get_rate(clk
) / 2;
239 #endif /* CONFIG_CEVT_CSRC_LS1X */