1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
6 * samsung - Common hr-timer support (s3c and s5p)
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/err.h>
12 #include <linux/clk.h>
13 #include <linux/clockchips.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/sched_clock.h>
23 #include <clocksource/samsung_pwm.h>
29 #define REG_TCFG0 0x00
30 #define REG_TCFG1 0x04
32 #define REG_TINT_CSTAT 0x44
34 #define REG_TCNTB(chan) (0x0c + 12 * (chan))
35 #define REG_TCMPB(chan) (0x10 + 12 * (chan))
37 #define TCFG0_PRESCALER_MASK 0xff
38 #define TCFG0_PRESCALER1_SHIFT 8
40 #define TCFG1_SHIFT(x) ((x) * 4)
41 #define TCFG1_MUX_MASK 0xf
44 * Each channel occupies 4 bits in TCON register, but there is a gap of 4
45 * bits (one channel) after channel 0, so channels have different numbering
46 * when accessing TCON register.
48 * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
49 * in its set of bits is 2 as opposed to 3 for other channels.
51 #define TCON_START(chan) (1 << (4 * (chan) + 0))
52 #define TCON_MANUALUPDATE(chan) (1 << (4 * (chan) + 1))
53 #define TCON_INVERT(chan) (1 << (4 * (chan) + 2))
54 #define _TCON_AUTORELOAD(chan) (1 << (4 * (chan) + 3))
55 #define _TCON_AUTORELOAD4(chan) (1 << (4 * (chan) + 2))
56 #define TCON_AUTORELOAD(chan) \
57 ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
59 DEFINE_SPINLOCK(samsung_pwm_lock);
60 EXPORT_SYMBOL(samsung_pwm_lock);
62 struct samsung_pwm_clocksource {
64 const void __iomem *source_reg;
65 unsigned int irq[SAMSUNG_PWM_NUM];
66 struct samsung_pwm_variant variant;
70 unsigned int event_id;
71 unsigned int source_id;
72 unsigned int tcnt_max;
73 unsigned int tscaler_div;
76 unsigned long clock_count_per_tick;
79 static struct samsung_pwm_clocksource pwm;
81 static void samsung_timer_set_prescale(unsigned int channel, u16 prescale)
88 shift = TCFG0_PRESCALER1_SHIFT;
90 spin_lock_irqsave(&samsung_pwm_lock, flags);
92 reg = readl(pwm.base + REG_TCFG0);
93 reg &= ~(TCFG0_PRESCALER_MASK << shift);
94 reg |= (prescale - 1) << shift;
95 writel(reg, pwm.base + REG_TCFG0);
97 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
100 static void samsung_timer_set_divisor(unsigned int channel, u8 divisor)
102 u8 shift = TCFG1_SHIFT(channel);
107 bits = (fls(divisor) - 1) - pwm.variant.div_base;
109 spin_lock_irqsave(&samsung_pwm_lock, flags);
111 reg = readl(pwm.base + REG_TCFG1);
112 reg &= ~(TCFG1_MUX_MASK << shift);
113 reg |= bits << shift;
114 writel(reg, pwm.base + REG_TCFG1);
116 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
119 static void samsung_time_stop(unsigned int channel)
127 spin_lock_irqsave(&samsung_pwm_lock, flags);
129 tcon = readl_relaxed(pwm.base + REG_TCON);
130 tcon &= ~TCON_START(channel);
131 writel_relaxed(tcon, pwm.base + REG_TCON);
133 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
136 static void samsung_time_setup(unsigned int channel, unsigned long tcnt)
140 unsigned int tcon_chan = channel;
145 spin_lock_irqsave(&samsung_pwm_lock, flags);
147 tcon = readl_relaxed(pwm.base + REG_TCON);
149 tcon &= ~(TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan));
150 tcon |= TCON_MANUALUPDATE(tcon_chan);
152 writel_relaxed(tcnt, pwm.base + REG_TCNTB(channel));
153 writel_relaxed(tcnt, pwm.base + REG_TCMPB(channel));
154 writel_relaxed(tcon, pwm.base + REG_TCON);
156 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
159 static void samsung_time_start(unsigned int channel, bool periodic)
167 spin_lock_irqsave(&samsung_pwm_lock, flags);
169 tcon = readl_relaxed(pwm.base + REG_TCON);
171 tcon &= ~TCON_MANUALUPDATE(channel);
172 tcon |= TCON_START(channel);
175 tcon |= TCON_AUTORELOAD(channel);
177 tcon &= ~TCON_AUTORELOAD(channel);
179 writel_relaxed(tcon, pwm.base + REG_TCON);
181 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
184 static int samsung_set_next_event(unsigned long cycles,
185 struct clock_event_device *evt)
188 * This check is needed to account for internal rounding
189 * errors inside clockevents core, which might result in
190 * passing cycles = 0, which in turn would not generate any
191 * timer interrupt and hang the system.
193 * Another solution would be to set up the clockevent device
194 * with min_delta = 2, but this would unnecessarily increase
195 * the minimum sleep period.
200 samsung_time_setup(pwm.event_id, cycles);
201 samsung_time_start(pwm.event_id, false);
206 static int samsung_shutdown(struct clock_event_device *evt)
208 samsung_time_stop(pwm.event_id);
212 static int samsung_set_periodic(struct clock_event_device *evt)
214 samsung_time_stop(pwm.event_id);
215 samsung_time_setup(pwm.event_id, pwm.clock_count_per_tick - 1);
216 samsung_time_start(pwm.event_id, true);
220 static void samsung_clockevent_resume(struct clock_event_device *cev)
222 samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div);
223 samsung_timer_set_divisor(pwm.event_id, pwm.tdiv);
225 if (pwm.variant.has_tint_cstat) {
226 u32 mask = (1 << pwm.event_id);
228 writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
232 static struct clock_event_device time_event_device = {
233 .name = "samsung_event_timer",
234 .features = CLOCK_EVT_FEAT_PERIODIC |
235 CLOCK_EVT_FEAT_ONESHOT,
237 .set_next_event = samsung_set_next_event,
238 .set_state_shutdown = samsung_shutdown,
239 .set_state_periodic = samsung_set_periodic,
240 .set_state_oneshot = samsung_shutdown,
241 .tick_resume = samsung_shutdown,
242 .resume = samsung_clockevent_resume,
245 static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id)
247 struct clock_event_device *evt = dev_id;
249 if (pwm.variant.has_tint_cstat) {
250 u32 mask = (1 << pwm.event_id);
252 writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
255 evt->event_handler(evt);
260 static void __init samsung_clockevent_init(void)
263 unsigned long clock_rate;
264 unsigned int irq_number;
266 pclk = clk_get_rate(pwm.timerclk);
268 samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div);
269 samsung_timer_set_divisor(pwm.event_id, pwm.tdiv);
271 clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
272 pwm.clock_count_per_tick = clock_rate / HZ;
274 time_event_device.cpumask = cpumask_of(0);
275 clockevents_config_and_register(&time_event_device,
276 clock_rate, 1, pwm.tcnt_max);
278 irq_number = pwm.irq[pwm.event_id];
279 if (request_irq(irq_number, samsung_clock_event_isr,
280 IRQF_TIMER | IRQF_IRQPOLL, "samsung_time_irq",
282 pr_err("%s: request_irq() failed\n", "samsung_time_irq");
284 if (pwm.variant.has_tint_cstat) {
285 u32 mask = (1 << pwm.event_id);
287 writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
291 static void samsung_clocksource_suspend(struct clocksource *cs)
293 samsung_time_stop(pwm.source_id);
296 static void samsung_clocksource_resume(struct clocksource *cs)
298 samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div);
299 samsung_timer_set_divisor(pwm.source_id, pwm.tdiv);
301 samsung_time_setup(pwm.source_id, pwm.tcnt_max);
302 samsung_time_start(pwm.source_id, true);
305 static u64 notrace samsung_clocksource_read(struct clocksource *c)
307 return ~readl_relaxed(pwm.source_reg);
310 static struct clocksource samsung_clocksource = {
311 .name = "samsung_clocksource_timer",
313 .read = samsung_clocksource_read,
314 .suspend = samsung_clocksource_suspend,
315 .resume = samsung_clocksource_resume,
316 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
320 * Override the global weak sched_clock symbol with this
321 * local implementation which uses the clocksource to get some
322 * better resolution when scheduling the kernel. We accept that
323 * this wraps around for now, since it is just a relative time
324 * stamp. (Inspired by U300 implementation.)
326 static u64 notrace samsung_read_sched_clock(void)
328 return samsung_clocksource_read(NULL);
331 static int __init samsung_clocksource_init(void)
334 unsigned long clock_rate;
336 pclk = clk_get_rate(pwm.timerclk);
338 samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div);
339 samsung_timer_set_divisor(pwm.source_id, pwm.tdiv);
341 clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
343 samsung_time_setup(pwm.source_id, pwm.tcnt_max);
344 samsung_time_start(pwm.source_id, true);
346 if (pwm.source_id == 4)
347 pwm.source_reg = pwm.base + 0x40;
349 pwm.source_reg = pwm.base + pwm.source_id * 0x0c + 0x14;
351 sched_clock_register(samsung_read_sched_clock,
352 pwm.variant.bits, clock_rate);
354 samsung_clocksource.mask = CLOCKSOURCE_MASK(pwm.variant.bits);
355 return clocksource_register_hz(&samsung_clocksource, clock_rate);
358 static void __init samsung_timer_resources(void)
360 clk_prepare_enable(pwm.timerclk);
362 pwm.tcnt_max = (1UL << pwm.variant.bits) - 1;
363 if (pwm.variant.bits == 16) {
364 pwm.tscaler_div = 25;
375 static int __init _samsung_pwm_clocksource_init(void)
380 mask = ~pwm.variant.output_mask & ((1 << SAMSUNG_PWM_NUM) - 1);
381 channel = fls(mask) - 1;
383 pr_crit("failed to find PWM channel for clocksource\n");
386 pwm.source_id = channel;
388 mask &= ~(1 << channel);
389 channel = fls(mask) - 1;
391 pr_crit("failed to find PWM channel for clock event\n");
394 pwm.event_id = channel;
396 samsung_timer_resources();
397 samsung_clockevent_init();
399 return samsung_clocksource_init();
402 void __init samsung_pwm_clocksource_init(void __iomem *base,
404 const struct samsung_pwm_variant *variant)
407 memcpy(&pwm.variant, variant, sizeof(pwm.variant));
408 memcpy(pwm.irq, irqs, SAMSUNG_PWM_NUM * sizeof(*irqs));
410 pwm.timerclk = clk_get(NULL, "timers");
411 if (IS_ERR(pwm.timerclk))
412 panic("failed to get timers clock for timer");
414 _samsung_pwm_clocksource_init();
417 #ifdef CONFIG_TIMER_OF
418 static int __init samsung_pwm_alloc(struct device_node *np,
419 const struct samsung_pwm_variant *variant)
421 struct property *prop;
426 memcpy(&pwm.variant, variant, sizeof(pwm.variant));
427 for (i = 0; i < SAMSUNG_PWM_NUM; ++i)
428 pwm.irq[i] = irq_of_parse_and_map(np, i);
430 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
431 if (val >= SAMSUNG_PWM_NUM) {
432 pr_warn("%s: invalid channel index in samsung,pwm-outputs property\n", __func__);
435 pwm.variant.output_mask |= 1 << val;
438 pwm.base = of_iomap(np, 0);
440 pr_err("%s: failed to map PWM registers\n", __func__);
444 pwm.timerclk = of_clk_get_by_name(np, "timers");
445 if (IS_ERR(pwm.timerclk)) {
446 pr_crit("failed to get timers clock for timer\n");
447 ret = PTR_ERR(pwm.timerclk);
451 ret = _samsung_pwm_clocksource_init();
453 goto err_clocksource;
458 clk_put(pwm.timerclk);
467 static const struct samsung_pwm_variant s3c24xx_variant = {
470 .has_tint_cstat = false,
471 .tclk_mask = (1 << 4),
474 static int __init s3c2410_pwm_clocksource_init(struct device_node *np)
476 return samsung_pwm_alloc(np, &s3c24xx_variant);
478 TIMER_OF_DECLARE(s3c2410_pwm, "samsung,s3c2410-pwm", s3c2410_pwm_clocksource_init);
480 static const struct samsung_pwm_variant s3c64xx_variant = {
483 .has_tint_cstat = true,
484 .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5),
487 static int __init s3c64xx_pwm_clocksource_init(struct device_node *np)
489 return samsung_pwm_alloc(np, &s3c64xx_variant);
491 TIMER_OF_DECLARE(s3c6400_pwm, "samsung,s3c6400-pwm", s3c64xx_pwm_clocksource_init);
493 static const struct samsung_pwm_variant s5p64x0_variant = {
496 .has_tint_cstat = true,
500 static int __init s5p64x0_pwm_clocksource_init(struct device_node *np)
502 return samsung_pwm_alloc(np, &s5p64x0_variant);
504 TIMER_OF_DECLARE(s5p6440_pwm, "samsung,s5p6440-pwm", s5p64x0_pwm_clocksource_init);
506 static const struct samsung_pwm_variant s5p_variant = {
509 .has_tint_cstat = true,
510 .tclk_mask = (1 << 5),
513 static int __init s5p_pwm_clocksource_init(struct device_node *np)
515 return samsung_pwm_alloc(np, &s5p_variant);
517 TIMER_OF_DECLARE(s5pc100_pwm, "samsung,s5pc100-pwm", s5p_pwm_clocksource_init);