]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/clocksource/samsung_pwm_timer.c
clocksource: samsung_pwm_timer: Correct programming of clock events
[mirror_ubuntu-artful-kernel.git] / drivers / clocksource / samsung_pwm_timer.c
CommitLineData
f1189989
TF
1/*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com/
4 *
5 * samsung - Common hr-timer support (s3c and s5p)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/interrupt.h>
13#include <linux/irq.h>
14#include <linux/err.h>
15#include <linux/clk.h>
16#include <linux/clockchips.h>
17#include <linux/list.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_irq.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24
25#include <clocksource/samsung_pwm.h>
26
27#include <asm/sched_clock.h>
28
29/*
30 * Clocksource driver
31 */
32
33#define REG_TCFG0 0x00
34#define REG_TCFG1 0x04
35#define REG_TCON 0x08
36#define REG_TINT_CSTAT 0x44
37
38#define REG_TCNTB(chan) (0x0c + 12 * (chan))
39#define REG_TCMPB(chan) (0x10 + 12 * (chan))
40
41#define TCFG0_PRESCALER_MASK 0xff
42#define TCFG0_PRESCALER1_SHIFT 8
43
44#define TCFG1_SHIFT(x) ((x) * 4)
45#define TCFG1_MUX_MASK 0xf
46
47#define TCON_START(chan) (1 << (4 * (chan) + 0))
48#define TCON_MANUALUPDATE(chan) (1 << (4 * (chan) + 1))
49#define TCON_INVERT(chan) (1 << (4 * (chan) + 2))
50#define TCON_AUTORELOAD(chan) (1 << (4 * (chan) + 3))
51
7aac482e
TF
52DEFINE_SPINLOCK(samsung_pwm_lock);
53EXPORT_SYMBOL(samsung_pwm_lock);
54
030c2a1e
TF
55struct samsung_pwm_clocksource {
56 void __iomem *base;
57 unsigned int irq[SAMSUNG_PWM_NUM];
58 struct samsung_pwm_variant variant;
59
60 struct clk *timerclk;
61
f1189989
TF
62 unsigned int event_id;
63 unsigned int source_id;
64 unsigned int tcnt_max;
65 unsigned int tscaler_div;
66 unsigned int tdiv;
030c2a1e
TF
67
68 unsigned long clock_count_per_tick;
f1189989
TF
69};
70
030c2a1e 71static struct samsung_pwm_clocksource pwm;
f1189989 72
030c2a1e 73static void samsung_timer_set_prescale(unsigned int channel, u16 prescale)
f1189989
TF
74{
75 unsigned long flags;
76 u8 shift = 0;
77 u32 reg;
78
79 if (channel >= 2)
80 shift = TCFG0_PRESCALER1_SHIFT;
81
7aac482e 82 spin_lock_irqsave(&samsung_pwm_lock, flags);
f1189989 83
030c2a1e 84 reg = readl(pwm.base + REG_TCFG0);
f1189989
TF
85 reg &= ~(TCFG0_PRESCALER_MASK << shift);
86 reg |= (prescale - 1) << shift;
030c2a1e 87 writel(reg, pwm.base + REG_TCFG0);
f1189989 88
7aac482e 89 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
f1189989
TF
90}
91
030c2a1e 92static void samsung_timer_set_divisor(unsigned int channel, u8 divisor)
f1189989
TF
93{
94 u8 shift = TCFG1_SHIFT(channel);
95 unsigned long flags;
96 u32 reg;
97 u8 bits;
98
030c2a1e 99 bits = (fls(divisor) - 1) - pwm.variant.div_base;
f1189989 100
7aac482e 101 spin_lock_irqsave(&samsung_pwm_lock, flags);
f1189989 102
030c2a1e 103 reg = readl(pwm.base + REG_TCFG1);
f1189989
TF
104 reg &= ~(TCFG1_MUX_MASK << shift);
105 reg |= bits << shift;
030c2a1e 106 writel(reg, pwm.base + REG_TCFG1);
f1189989 107
7aac482e 108 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
f1189989
TF
109}
110
111static void samsung_time_stop(unsigned int channel)
112{
113 unsigned long tcon;
114 unsigned long flags;
115
116 if (channel > 0)
117 ++channel;
118
7aac482e 119 spin_lock_irqsave(&samsung_pwm_lock, flags);
f1189989 120
030c2a1e 121 tcon = __raw_readl(pwm.base + REG_TCON);
f1189989 122 tcon &= ~TCON_START(channel);
030c2a1e 123 __raw_writel(tcon, pwm.base + REG_TCON);
f1189989 124
7aac482e 125 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
f1189989
TF
126}
127
128static void samsung_time_setup(unsigned int channel, unsigned long tcnt)
129{
130 unsigned long tcon;
131 unsigned long flags;
132 unsigned int tcon_chan = channel;
133
134 if (tcon_chan > 0)
135 ++tcon_chan;
136
7aac482e 137 spin_lock_irqsave(&samsung_pwm_lock, flags);
f1189989 138
030c2a1e 139 tcon = __raw_readl(pwm.base + REG_TCON);
f1189989 140
f1189989
TF
141 tcon &= ~(TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan));
142 tcon |= TCON_MANUALUPDATE(tcon_chan);
143
030c2a1e
TF
144 __raw_writel(tcnt, pwm.base + REG_TCNTB(channel));
145 __raw_writel(tcnt, pwm.base + REG_TCMPB(channel));
146 __raw_writel(tcon, pwm.base + REG_TCON);
f1189989 147
7aac482e 148 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
f1189989
TF
149}
150
151static void samsung_time_start(unsigned int channel, bool periodic)
152{
153 unsigned long tcon;
154 unsigned long flags;
155
156 if (channel > 0)
157 ++channel;
158
7aac482e 159 spin_lock_irqsave(&samsung_pwm_lock, flags);
f1189989 160
030c2a1e 161 tcon = __raw_readl(pwm.base + REG_TCON);
f1189989
TF
162
163 tcon &= ~TCON_MANUALUPDATE(channel);
164 tcon |= TCON_START(channel);
165
166 if (periodic)
167 tcon |= TCON_AUTORELOAD(channel);
168 else
169 tcon &= ~TCON_AUTORELOAD(channel);
170
030c2a1e 171 __raw_writel(tcon, pwm.base + REG_TCON);
f1189989 172
7aac482e 173 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
f1189989
TF
174}
175
176static int samsung_set_next_event(unsigned long cycles,
177 struct clock_event_device *evt)
178{
030c2a1e
TF
179 samsung_time_setup(pwm.event_id, cycles);
180 samsung_time_start(pwm.event_id, false);
f1189989
TF
181
182 return 0;
183}
184
185static void samsung_timer_resume(void)
186{
187 /* event timer restart */
6fe4dfd0 188 samsung_time_setup(pwm.event_id, pwm.clock_count_per_tick - 1);
030c2a1e 189 samsung_time_start(pwm.event_id, true);
f1189989
TF
190
191 /* source timer restart */
030c2a1e
TF
192 samsung_time_setup(pwm.source_id, pwm.tcnt_max);
193 samsung_time_start(pwm.source_id, true);
f1189989
TF
194}
195
196static void samsung_set_mode(enum clock_event_mode mode,
197 struct clock_event_device *evt)
198{
030c2a1e 199 samsung_time_stop(pwm.event_id);
f1189989
TF
200
201 switch (mode) {
202 case CLOCK_EVT_MODE_PERIODIC:
6fe4dfd0 203 samsung_time_setup(pwm.event_id, pwm.clock_count_per_tick - 1);
030c2a1e 204 samsung_time_start(pwm.event_id, true);
f1189989
TF
205 break;
206
207 case CLOCK_EVT_MODE_ONESHOT:
208 break;
209
210 case CLOCK_EVT_MODE_UNUSED:
211 case CLOCK_EVT_MODE_SHUTDOWN:
212 break;
213
214 case CLOCK_EVT_MODE_RESUME:
215 samsung_timer_resume();
216 break;
217 }
218}
219
220static struct clock_event_device time_event_device = {
221 .name = "samsung_event_timer",
222 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
223 .rating = 200,
224 .set_next_event = samsung_set_next_event,
225 .set_mode = samsung_set_mode,
226};
227
228static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id)
229{
230 struct clock_event_device *evt = dev_id;
231
030c2a1e
TF
232 if (pwm.variant.has_tint_cstat) {
233 u32 mask = (1 << pwm.event_id);
234 writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
f1189989
TF
235 }
236
237 evt->event_handler(evt);
238
239 return IRQ_HANDLED;
240}
241
242static struct irqaction samsung_clock_event_irq = {
243 .name = "samsung_time_irq",
244 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
245 .handler = samsung_clock_event_isr,
246 .dev_id = &time_event_device,
247};
248
249static void __init samsung_clockevent_init(void)
250{
251 unsigned long pclk;
252 unsigned long clock_rate;
253 unsigned int irq_number;
254
030c2a1e 255 pclk = clk_get_rate(pwm.timerclk);
f1189989 256
030c2a1e
TF
257 samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div);
258 samsung_timer_set_divisor(pwm.event_id, pwm.tdiv);
f1189989 259
030c2a1e
TF
260 clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
261 pwm.clock_count_per_tick = clock_rate / HZ;
f1189989
TF
262
263 time_event_device.cpumask = cpumask_of(0);
e9b852b8
TF
264 clockevents_config_and_register(&time_event_device,
265 clock_rate, 1, pwm.tcnt_max);
f1189989 266
030c2a1e 267 irq_number = pwm.irq[pwm.event_id];
f1189989
TF
268 setup_irq(irq_number, &samsung_clock_event_irq);
269
030c2a1e
TF
270 if (pwm.variant.has_tint_cstat) {
271 u32 mask = (1 << pwm.event_id);
272 writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
f1189989
TF
273 }
274}
275
276static void __iomem *samsung_timer_reg(void)
277{
030c2a1e 278 switch (pwm.source_id) {
f1189989
TF
279 case 0:
280 case 1:
281 case 2:
282 case 3:
030c2a1e 283 return pwm.base + pwm.source_id * 0x0c + 0x14;
f1189989
TF
284
285 case 4:
030c2a1e 286 return pwm.base + 0x40;
f1189989
TF
287
288 default:
289 BUG();
290 }
291}
292
293/*
294 * Override the global weak sched_clock symbol with this
295 * local implementation which uses the clocksource to get some
296 * better resolution when scheduling the kernel. We accept that
297 * this wraps around for now, since it is just a relative time
298 * stamp. (Inspired by U300 implementation.)
299 */
300static u32 notrace samsung_read_sched_clock(void)
301{
302 void __iomem *reg = samsung_timer_reg();
303
304 if (!reg)
305 return 0;
306
307 return ~__raw_readl(reg);
308}
309
310static void __init samsung_clocksource_init(void)
311{
312 void __iomem *reg = samsung_timer_reg();
313 unsigned long pclk;
314 unsigned long clock_rate;
315 int ret;
316
030c2a1e 317 pclk = clk_get_rate(pwm.timerclk);
f1189989 318
030c2a1e
TF
319 samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div);
320 samsung_timer_set_divisor(pwm.source_id, pwm.tdiv);
f1189989 321
030c2a1e 322 clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
f1189989 323
030c2a1e
TF
324 samsung_time_setup(pwm.source_id, pwm.tcnt_max);
325 samsung_time_start(pwm.source_id, true);
f1189989
TF
326
327 setup_sched_clock(samsung_read_sched_clock,
030c2a1e 328 pwm.variant.bits, clock_rate);
f1189989
TF
329
330 ret = clocksource_mmio_init(reg, "samsung_clocksource_timer",
030c2a1e 331 clock_rate, 250, pwm.variant.bits,
f1189989
TF
332 clocksource_mmio_readl_down);
333 if (ret)
334 panic("samsung_clocksource_timer: can't register clocksource\n");
335}
336
337static void __init samsung_timer_resources(void)
338{
030c2a1e
TF
339 pwm.timerclk = clk_get(NULL, "timers");
340 if (IS_ERR(pwm.timerclk))
f1189989
TF
341 panic("failed to get timers clock for timer");
342
030c2a1e 343 clk_prepare_enable(pwm.timerclk);
f1189989 344
030c2a1e
TF
345 pwm.tcnt_max = (1UL << pwm.variant.bits) - 1;
346 if (pwm.variant.bits == 16) {
347 pwm.tscaler_div = 25;
348 pwm.tdiv = 2;
f1189989 349 } else {
030c2a1e
TF
350 pwm.tscaler_div = 2;
351 pwm.tdiv = 1;
f1189989
TF
352 }
353}
354
355/*
356 * PWM master driver
357 */
f9bb48a2 358static void __init _samsung_pwm_clocksource_init(void)
f1189989
TF
359{
360 u8 mask;
361 int channel;
362
030c2a1e 363 mask = ~pwm.variant.output_mask & ((1 << SAMSUNG_PWM_NUM) - 1);
f1189989
TF
364 channel = fls(mask) - 1;
365 if (channel < 0)
366 panic("failed to find PWM channel for clocksource");
030c2a1e 367 pwm.source_id = channel;
f1189989
TF
368
369 mask &= ~(1 << channel);
370 channel = fls(mask) - 1;
371 if (channel < 0)
372 panic("failed to find PWM channel for clock event");
030c2a1e 373 pwm.event_id = channel;
f1189989
TF
374
375 samsung_timer_resources();
376 samsung_clockevent_init();
377 samsung_clocksource_init();
378}
379
f9bb48a2
TF
380void __init samsung_pwm_clocksource_init(void __iomem *base,
381 unsigned int *irqs, struct samsung_pwm_variant *variant)
382{
383 pwm.base = base;
384 memcpy(&pwm.variant, variant, sizeof(pwm.variant));
385 memcpy(pwm.irq, irqs, SAMSUNG_PWM_NUM * sizeof(*irqs));
386
387 _samsung_pwm_clocksource_init();
388}
389
390#ifdef CONFIG_CLKSRC_OF
f1189989
TF
391static void __init samsung_pwm_alloc(struct device_node *np,
392 const struct samsung_pwm_variant *variant)
393{
394 struct resource res;
395 struct property *prop;
396 const __be32 *cur;
397 u32 val;
398 int i;
399
030c2a1e 400 memcpy(&pwm.variant, variant, sizeof(pwm.variant));
f1189989 401 for (i = 0; i < SAMSUNG_PWM_NUM; ++i)
030c2a1e 402 pwm.irq[i] = irq_of_parse_and_map(np, i);
f1189989
TF
403
404 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
405 if (val >= SAMSUNG_PWM_NUM) {
406 pr_warning("%s: invalid channel index in samsung,pwm-outputs property\n",
407 __func__);
408 continue;
409 }
030c2a1e 410 pwm.variant.output_mask |= 1 << val;
f1189989
TF
411 }
412
413 of_address_to_resource(np, 0, &res);
414 if (!request_mem_region(res.start,
415 resource_size(&res), "samsung-pwm")) {
416 pr_err("%s: failed to request IO mem region\n", __func__);
417 return;
418 }
419
030c2a1e
TF
420 pwm.base = ioremap(res.start, resource_size(&res));
421 if (!pwm.base) {
f1189989
TF
422 pr_err("%s: failed to map PWM registers\n", __func__);
423 release_mem_region(res.start, resource_size(&res));
424 return;
425 }
426
f9bb48a2 427 _samsung_pwm_clocksource_init();
f1189989
TF
428}
429
430static const struct samsung_pwm_variant s3c24xx_variant = {
431 .bits = 16,
432 .div_base = 1,
433 .has_tint_cstat = false,
434 .tclk_mask = (1 << 4),
435};
436
437static void __init s3c2410_pwm_clocksource_init(struct device_node *np)
438{
439 samsung_pwm_alloc(np, &s3c24xx_variant);
440}
441CLOCKSOURCE_OF_DECLARE(s3c2410_pwm, "samsung,s3c2410-pwm", s3c2410_pwm_clocksource_init);
442
443static const struct samsung_pwm_variant s3c64xx_variant = {
444 .bits = 32,
445 .div_base = 0,
446 .has_tint_cstat = true,
447 .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5),
448};
449
450static void __init s3c64xx_pwm_clocksource_init(struct device_node *np)
451{
452 samsung_pwm_alloc(np, &s3c64xx_variant);
453}
454CLOCKSOURCE_OF_DECLARE(s3c6400_pwm, "samsung,s3c6400-pwm", s3c64xx_pwm_clocksource_init);
455
456static const struct samsung_pwm_variant s5p64x0_variant = {
457 .bits = 32,
458 .div_base = 0,
459 .has_tint_cstat = true,
460 .tclk_mask = 0,
461};
462
463static void __init s5p64x0_pwm_clocksource_init(struct device_node *np)
464{
465 samsung_pwm_alloc(np, &s5p64x0_variant);
466}
467CLOCKSOURCE_OF_DECLARE(s5p6440_pwm, "samsung,s5p6440-pwm", s5p64x0_pwm_clocksource_init);
468
469static const struct samsung_pwm_variant s5p_variant = {
470 .bits = 32,
471 .div_base = 0,
472 .has_tint_cstat = true,
473 .tclk_mask = (1 << 5),
474};
475
476static void __init s5p_pwm_clocksource_init(struct device_node *np)
477{
478 samsung_pwm_alloc(np, &s5p_variant);
479}
480CLOCKSOURCE_OF_DECLARE(s5pc100_pwm, "samsung,s5pc100-pwm", s5p_pwm_clocksource_init);
f9bb48a2 481#endif