xref: /openbmc/linux/drivers/clocksource/timer-loongson1-pwm.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e738521aSKeguang Zhang // SPDX-License-Identifier: GPL-2.0-or-later
2e738521aSKeguang Zhang /*
3e738521aSKeguang Zhang  * Clocksource driver for Loongson-1 SoC
4e738521aSKeguang Zhang  *
5e738521aSKeguang Zhang  * Copyright (c) 2023 Keguang Zhang <keguang.zhang@gmail.com>
6e738521aSKeguang Zhang  */
7e738521aSKeguang Zhang 
8e738521aSKeguang Zhang #include <linux/clockchips.h>
9e738521aSKeguang Zhang #include <linux/interrupt.h>
10e738521aSKeguang Zhang #include <linux/sizes.h>
11e738521aSKeguang Zhang #include "timer-of.h"
12e738521aSKeguang Zhang 
13e738521aSKeguang Zhang /* Loongson-1 PWM Timer Register Definitions */
14e738521aSKeguang Zhang #define PWM_CNTR		0x0
15e738521aSKeguang Zhang #define PWM_HRC			0x4
16e738521aSKeguang Zhang #define PWM_LRC			0x8
17e738521aSKeguang Zhang #define PWM_CTRL		0xc
18e738521aSKeguang Zhang 
19e738521aSKeguang Zhang /* PWM Control Register Bits */
20e738521aSKeguang Zhang #define INT_LRC_EN		BIT(11)
21e738521aSKeguang Zhang #define INT_HRC_EN		BIT(10)
22e738521aSKeguang Zhang #define CNTR_RST		BIT(7)
23e738521aSKeguang Zhang #define INT_SR			BIT(6)
24e738521aSKeguang Zhang #define INT_EN			BIT(5)
25e738521aSKeguang Zhang #define PWM_SINGLE		BIT(4)
26e738521aSKeguang Zhang #define PWM_OE			BIT(3)
27e738521aSKeguang Zhang #define CNT_EN			BIT(0)
28e738521aSKeguang Zhang 
29e738521aSKeguang Zhang #define CNTR_WIDTH		24
30e738521aSKeguang Zhang 
31*bd0f3aacSTom Rix static DEFINE_RAW_SPINLOCK(ls1x_timer_lock);
32e738521aSKeguang Zhang 
33e738521aSKeguang Zhang struct ls1x_clocksource {
34e738521aSKeguang Zhang 	void __iomem *reg_base;
35e738521aSKeguang Zhang 	unsigned long ticks_per_jiffy;
36e738521aSKeguang Zhang 	struct clocksource clksrc;
37e738521aSKeguang Zhang };
38e738521aSKeguang Zhang 
to_ls1x_clksrc(struct clocksource * c)39e738521aSKeguang Zhang static inline struct ls1x_clocksource *to_ls1x_clksrc(struct clocksource *c)
40e738521aSKeguang Zhang {
41e738521aSKeguang Zhang 	return container_of(c, struct ls1x_clocksource, clksrc);
42e738521aSKeguang Zhang }
43e738521aSKeguang Zhang 
ls1x_pwmtimer_set_period(unsigned int period,struct timer_of * to)44e738521aSKeguang Zhang static inline void ls1x_pwmtimer_set_period(unsigned int period,
45e738521aSKeguang Zhang 					    struct timer_of *to)
46e738521aSKeguang Zhang {
47e738521aSKeguang Zhang 	writel(period, timer_of_base(to) + PWM_LRC);
48e738521aSKeguang Zhang 	writel(period, timer_of_base(to) + PWM_HRC);
49e738521aSKeguang Zhang }
50e738521aSKeguang Zhang 
ls1x_pwmtimer_clear(struct timer_of * to)51e738521aSKeguang Zhang static inline void ls1x_pwmtimer_clear(struct timer_of *to)
52e738521aSKeguang Zhang {
53e738521aSKeguang Zhang 	writel(0, timer_of_base(to) + PWM_CNTR);
54e738521aSKeguang Zhang }
55e738521aSKeguang Zhang 
ls1x_pwmtimer_start(struct timer_of * to)56e738521aSKeguang Zhang static inline void ls1x_pwmtimer_start(struct timer_of *to)
57e738521aSKeguang Zhang {
58e738521aSKeguang Zhang 	writel((INT_EN | PWM_OE | CNT_EN), timer_of_base(to) + PWM_CTRL);
59e738521aSKeguang Zhang }
60e738521aSKeguang Zhang 
ls1x_pwmtimer_stop(struct timer_of * to)61e738521aSKeguang Zhang static inline void ls1x_pwmtimer_stop(struct timer_of *to)
62e738521aSKeguang Zhang {
63e738521aSKeguang Zhang 	writel(0, timer_of_base(to) + PWM_CTRL);
64e738521aSKeguang Zhang }
65e738521aSKeguang Zhang 
ls1x_pwmtimer_irq_ack(struct timer_of * to)66e738521aSKeguang Zhang static inline void ls1x_pwmtimer_irq_ack(struct timer_of *to)
67e738521aSKeguang Zhang {
68e738521aSKeguang Zhang 	int val;
69e738521aSKeguang Zhang 
70e738521aSKeguang Zhang 	val = readl(timer_of_base(to) + PWM_CTRL);
71e738521aSKeguang Zhang 	val |= INT_SR;
72e738521aSKeguang Zhang 	writel(val, timer_of_base(to) + PWM_CTRL);
73e738521aSKeguang Zhang }
74e738521aSKeguang Zhang 
ls1x_clockevent_isr(int irq,void * dev_id)75e738521aSKeguang Zhang static irqreturn_t ls1x_clockevent_isr(int irq, void *dev_id)
76e738521aSKeguang Zhang {
77e738521aSKeguang Zhang 	struct clock_event_device *clkevt = dev_id;
78e738521aSKeguang Zhang 	struct timer_of *to = to_timer_of(clkevt);
79e738521aSKeguang Zhang 
80e738521aSKeguang Zhang 	ls1x_pwmtimer_irq_ack(to);
81e738521aSKeguang Zhang 	ls1x_pwmtimer_clear(to);
82e738521aSKeguang Zhang 	ls1x_pwmtimer_start(to);
83e738521aSKeguang Zhang 
84e738521aSKeguang Zhang 	clkevt->event_handler(clkevt);
85e738521aSKeguang Zhang 
86e738521aSKeguang Zhang 	return IRQ_HANDLED;
87e738521aSKeguang Zhang }
88e738521aSKeguang Zhang 
ls1x_clockevent_set_state_periodic(struct clock_event_device * clkevt)89e738521aSKeguang Zhang static int ls1x_clockevent_set_state_periodic(struct clock_event_device *clkevt)
90e738521aSKeguang Zhang {
91e738521aSKeguang Zhang 	struct timer_of *to = to_timer_of(clkevt);
92e738521aSKeguang Zhang 
93e738521aSKeguang Zhang 	raw_spin_lock(&ls1x_timer_lock);
94e738521aSKeguang Zhang 	ls1x_pwmtimer_set_period(timer_of_period(to), to);
95e738521aSKeguang Zhang 	ls1x_pwmtimer_clear(to);
96e738521aSKeguang Zhang 	ls1x_pwmtimer_start(to);
97e738521aSKeguang Zhang 	raw_spin_unlock(&ls1x_timer_lock);
98e738521aSKeguang Zhang 
99e738521aSKeguang Zhang 	return 0;
100e738521aSKeguang Zhang }
101e738521aSKeguang Zhang 
ls1x_clockevent_tick_resume(struct clock_event_device * clkevt)102e738521aSKeguang Zhang static int ls1x_clockevent_tick_resume(struct clock_event_device *clkevt)
103e738521aSKeguang Zhang {
104e738521aSKeguang Zhang 	raw_spin_lock(&ls1x_timer_lock);
105e738521aSKeguang Zhang 	ls1x_pwmtimer_start(to_timer_of(clkevt));
106e738521aSKeguang Zhang 	raw_spin_unlock(&ls1x_timer_lock);
107e738521aSKeguang Zhang 
108e738521aSKeguang Zhang 	return 0;
109e738521aSKeguang Zhang }
110e738521aSKeguang Zhang 
ls1x_clockevent_set_state_shutdown(struct clock_event_device * clkevt)111e738521aSKeguang Zhang static int ls1x_clockevent_set_state_shutdown(struct clock_event_device *clkevt)
112e738521aSKeguang Zhang {
113e738521aSKeguang Zhang 	raw_spin_lock(&ls1x_timer_lock);
114e738521aSKeguang Zhang 	ls1x_pwmtimer_stop(to_timer_of(clkevt));
115e738521aSKeguang Zhang 	raw_spin_unlock(&ls1x_timer_lock);
116e738521aSKeguang Zhang 
117e738521aSKeguang Zhang 	return 0;
118e738521aSKeguang Zhang }
119e738521aSKeguang Zhang 
ls1x_clockevent_set_next(unsigned long evt,struct clock_event_device * clkevt)120e738521aSKeguang Zhang static int ls1x_clockevent_set_next(unsigned long evt,
121e738521aSKeguang Zhang 				    struct clock_event_device *clkevt)
122e738521aSKeguang Zhang {
123e738521aSKeguang Zhang 	struct timer_of *to = to_timer_of(clkevt);
124e738521aSKeguang Zhang 
125e738521aSKeguang Zhang 	raw_spin_lock(&ls1x_timer_lock);
126e738521aSKeguang Zhang 	ls1x_pwmtimer_set_period(evt, to);
127e738521aSKeguang Zhang 	ls1x_pwmtimer_clear(to);
128e738521aSKeguang Zhang 	ls1x_pwmtimer_start(to);
129e738521aSKeguang Zhang 	raw_spin_unlock(&ls1x_timer_lock);
130e738521aSKeguang Zhang 
131e738521aSKeguang Zhang 	return 0;
132e738521aSKeguang Zhang }
133e738521aSKeguang Zhang 
134e738521aSKeguang Zhang static struct timer_of ls1x_to = {
135e738521aSKeguang Zhang 	.flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
136e738521aSKeguang Zhang 	.clkevt = {
137e738521aSKeguang Zhang 		.name			= "ls1x-pwmtimer",
138e738521aSKeguang Zhang 		.features		= CLOCK_EVT_FEAT_PERIODIC |
139e738521aSKeguang Zhang 					  CLOCK_EVT_FEAT_ONESHOT,
140e738521aSKeguang Zhang 		.rating			= 300,
141e738521aSKeguang Zhang 		.set_next_event		= ls1x_clockevent_set_next,
142e738521aSKeguang Zhang 		.set_state_periodic	= ls1x_clockevent_set_state_periodic,
143e738521aSKeguang Zhang 		.set_state_oneshot	= ls1x_clockevent_set_state_shutdown,
144e738521aSKeguang Zhang 		.set_state_shutdown	= ls1x_clockevent_set_state_shutdown,
145e738521aSKeguang Zhang 		.tick_resume		= ls1x_clockevent_tick_resume,
146e738521aSKeguang Zhang 	},
147e738521aSKeguang Zhang 	.of_irq = {
148e738521aSKeguang Zhang 		.handler		= ls1x_clockevent_isr,
149e738521aSKeguang Zhang 		.flags			= IRQF_TIMER,
150e738521aSKeguang Zhang 	},
151e738521aSKeguang Zhang };
152e738521aSKeguang Zhang 
153e738521aSKeguang Zhang /*
154e738521aSKeguang Zhang  * Since the PWM timer overflows every two ticks, its not very useful
155e738521aSKeguang Zhang  * to just read by itself. So use jiffies to emulate a free
156e738521aSKeguang Zhang  * running counter:
157e738521aSKeguang Zhang  */
ls1x_clocksource_read(struct clocksource * cs)158e738521aSKeguang Zhang static u64 ls1x_clocksource_read(struct clocksource *cs)
159e738521aSKeguang Zhang {
160e738521aSKeguang Zhang 	struct ls1x_clocksource *ls1x_cs = to_ls1x_clksrc(cs);
161e738521aSKeguang Zhang 	unsigned long flags;
162e738521aSKeguang Zhang 	int count;
163e738521aSKeguang Zhang 	u32 jifs;
164e738521aSKeguang Zhang 	static int old_count;
165e738521aSKeguang Zhang 	static u32 old_jifs;
166e738521aSKeguang Zhang 
167e738521aSKeguang Zhang 	raw_spin_lock_irqsave(&ls1x_timer_lock, flags);
168e738521aSKeguang Zhang 	/*
169e738521aSKeguang Zhang 	 * Although our caller may have the read side of xtime_lock,
170e738521aSKeguang Zhang 	 * this is now a seqlock, and we are cheating in this routine
171e738521aSKeguang Zhang 	 * by having side effects on state that we cannot undo if
172e738521aSKeguang Zhang 	 * there is a collision on the seqlock and our caller has to
173e738521aSKeguang Zhang 	 * retry.  (Namely, old_jifs and old_count.)  So we must treat
174e738521aSKeguang Zhang 	 * jiffies as volatile despite the lock.  We read jiffies
175e738521aSKeguang Zhang 	 * before latching the timer count to guarantee that although
176e738521aSKeguang Zhang 	 * the jiffies value might be older than the count (that is,
177e738521aSKeguang Zhang 	 * the counter may underflow between the last point where
178e738521aSKeguang Zhang 	 * jiffies was incremented and the point where we latch the
179e738521aSKeguang Zhang 	 * count), it cannot be newer.
180e738521aSKeguang Zhang 	 */
181e738521aSKeguang Zhang 	jifs = jiffies;
182e738521aSKeguang Zhang 	/* read the count */
183e738521aSKeguang Zhang 	count = readl(ls1x_cs->reg_base + PWM_CNTR);
184e738521aSKeguang Zhang 
185e738521aSKeguang Zhang 	/*
186e738521aSKeguang Zhang 	 * It's possible for count to appear to go the wrong way for this
187e738521aSKeguang Zhang 	 * reason:
188e738521aSKeguang Zhang 	 *
189e738521aSKeguang Zhang 	 *  The timer counter underflows, but we haven't handled the resulting
190e738521aSKeguang Zhang 	 *  interrupt and incremented jiffies yet.
191e738521aSKeguang Zhang 	 *
192e738521aSKeguang Zhang 	 * Previous attempts to handle these cases intelligently were buggy, so
193e738521aSKeguang Zhang 	 * we just do the simple thing now.
194e738521aSKeguang Zhang 	 */
195e738521aSKeguang Zhang 	if (count < old_count && jifs == old_jifs)
196e738521aSKeguang Zhang 		count = old_count;
197e738521aSKeguang Zhang 
198e738521aSKeguang Zhang 	old_count = count;
199e738521aSKeguang Zhang 	old_jifs = jifs;
200e738521aSKeguang Zhang 
201e738521aSKeguang Zhang 	raw_spin_unlock_irqrestore(&ls1x_timer_lock, flags);
202e738521aSKeguang Zhang 
203e738521aSKeguang Zhang 	return (u64)(jifs * ls1x_cs->ticks_per_jiffy) + count;
204e738521aSKeguang Zhang }
205e738521aSKeguang Zhang 
206e738521aSKeguang Zhang static struct ls1x_clocksource ls1x_clocksource = {
207e738521aSKeguang Zhang 	.clksrc = {
208e738521aSKeguang Zhang 		.name           = "ls1x-pwmtimer",
209e738521aSKeguang Zhang 		.rating		= 300,
210e738521aSKeguang Zhang 		.read           = ls1x_clocksource_read,
211e738521aSKeguang Zhang 		.mask           = CLOCKSOURCE_MASK(CNTR_WIDTH),
212e738521aSKeguang Zhang 		.flags          = CLOCK_SOURCE_IS_CONTINUOUS,
213e738521aSKeguang Zhang 	},
214e738521aSKeguang Zhang };
215e738521aSKeguang Zhang 
ls1x_pwm_clocksource_init(struct device_node * np)216e738521aSKeguang Zhang static int __init ls1x_pwm_clocksource_init(struct device_node *np)
217e738521aSKeguang Zhang {
218e738521aSKeguang Zhang 	struct timer_of *to = &ls1x_to;
219e738521aSKeguang Zhang 	int ret;
220e738521aSKeguang Zhang 
221e738521aSKeguang Zhang 	ret = timer_of_init(np, to);
222e738521aSKeguang Zhang 	if (ret)
223e738521aSKeguang Zhang 		return ret;
224e738521aSKeguang Zhang 
225e738521aSKeguang Zhang 	clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
226e738521aSKeguang Zhang 					0x1, GENMASK(CNTR_WIDTH - 1, 0));
227e738521aSKeguang Zhang 
228e738521aSKeguang Zhang 	ls1x_clocksource.reg_base = timer_of_base(to);
229e738521aSKeguang Zhang 	ls1x_clocksource.ticks_per_jiffy = timer_of_period(to);
230e738521aSKeguang Zhang 
231e738521aSKeguang Zhang 	return clocksource_register_hz(&ls1x_clocksource.clksrc,
232e738521aSKeguang Zhang 				       timer_of_rate(to));
233e738521aSKeguang Zhang }
234e738521aSKeguang Zhang 
235e738521aSKeguang Zhang TIMER_OF_DECLARE(ls1x_pwm_clocksource, "loongson,ls1b-pwmtimer",
236e738521aSKeguang Zhang 		 ls1x_pwm_clocksource_init);
237