blob: 7108963a6ab81d0751aa13332311825d0e7d461e [file] [log] [blame]
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001/*
2 * SuperH Timer Support - CMT
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000021#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/io.h>
26#include <linux/clk.h>
27#include <linux/irq.h>
28#include <linux/err.h>
Magnus Damm3f7e5e22011-07-13 07:59:48 +000029#include <linux/delay.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000030#include <linux/clocksource.h>
31#include <linux/clockchips.h>
Paul Mundt46a12f72009-05-03 17:57:17 +090032#include <linux/sh_timer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker7deeab52011-07-03 13:36:22 -040034#include <linux/module.h>
Rafael J. Wysocki615a4452012-03-13 22:40:06 +010035#include <linux/pm_domain.h>
Rafael J. Wysockibad81382012-08-06 01:48:57 +020036#include <linux/pm_runtime.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000037
38struct sh_cmt_priv {
39 void __iomem *mapbase;
40 struct clk *clk;
41 unsigned long width; /* 16 or 32 bit version of hardware block */
42 unsigned long overflow_bit;
43 unsigned long clear_bits;
44 struct irqaction irqaction;
45 struct platform_device *pdev;
46
47 unsigned long flags;
48 unsigned long match_value;
49 unsigned long next_match_value;
50 unsigned long max_match_value;
51 unsigned long rate;
Paul Mundt7d0c3992012-05-25 13:36:43 +090052 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000053 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +000054 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000055 unsigned long total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +020056 bool cs_enabled;
Magnus Damma6a912c2012-12-14 14:54:19 +090057
Magnus Dammcccd7042012-12-14 14:54:28 +090058 /* callbacks for CMSTR and CMCSR access */
59 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
60 void (*write_control)(void __iomem *base, unsigned long offs,
61 unsigned long value);
62
Magnus Damma6a912c2012-12-14 14:54:19 +090063 /* callbacks for CMCNT and CMCOR access */
64 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
65 void (*write_count)(void __iomem *base, unsigned long offs,
66 unsigned long value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000067};
68
Magnus Damma6a912c2012-12-14 14:54:19 +090069static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +090070{
71 return ioread16(base + (offs << 1));
72}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000073
Magnus Damma6a912c2012-12-14 14:54:19 +090074static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
75{
76 return ioread32(base + (offs << 2));
77}
78
79static void sh_cmt_write16(void __iomem *base, unsigned long offs,
80 unsigned long value)
Magnus Damm587acb32012-12-14 14:54:10 +090081{
82 iowrite16(value, base + (offs << 1));
83}
84
Magnus Damma6a912c2012-12-14 14:54:19 +090085static void sh_cmt_write32(void __iomem *base, unsigned long offs,
86 unsigned long value)
87{
88 iowrite32(value, base + (offs << 2));
89}
90
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000091#define CMCSR 0 /* channel register */
92#define CMCNT 1 /* channel register */
93#define CMCOR 2 /* channel register */
94
Magnus Damm1b56b962012-12-14 14:54:00 +090095static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_priv *p)
96{
Magnus Damm587acb32012-12-14 14:54:10 +090097 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
98
Magnus Dammcccd7042012-12-14 14:54:28 +090099 return p->read_control(p->mapbase - cfg->channel_offset, 0);
Magnus Damm1b56b962012-12-14 14:54:00 +0900100}
101
102static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_priv *p)
103{
Magnus Dammcccd7042012-12-14 14:54:28 +0900104 return p->read_control(p->mapbase, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900105}
106
107static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_priv *p)
108{
Magnus Damma6a912c2012-12-14 14:54:19 +0900109 return p->read_count(p->mapbase, CMCNT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000110}
111
Magnus Damm1b56b962012-12-14 14:54:00 +0900112static inline void sh_cmt_write_cmstr(struct sh_cmt_priv *p,
113 unsigned long value)
114{
Magnus Damm587acb32012-12-14 14:54:10 +0900115 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
116
Magnus Dammcccd7042012-12-14 14:54:28 +0900117 p->write_control(p->mapbase - cfg->channel_offset, 0, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900118}
119
120static inline void sh_cmt_write_cmcsr(struct sh_cmt_priv *p,
121 unsigned long value)
122{
Magnus Dammcccd7042012-12-14 14:54:28 +0900123 p->write_control(p->mapbase, CMCSR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900124}
125
126static inline void sh_cmt_write_cmcnt(struct sh_cmt_priv *p,
127 unsigned long value)
128{
Magnus Damma6a912c2012-12-14 14:54:19 +0900129 p->write_count(p->mapbase, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900130}
131
132static inline void sh_cmt_write_cmcor(struct sh_cmt_priv *p,
133 unsigned long value)
134{
Magnus Damma6a912c2012-12-14 14:54:19 +0900135 p->write_count(p->mapbase, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900136}
137
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000138static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
139 int *has_wrapped)
140{
141 unsigned long v1, v2, v3;
Magnus Damm5b644c72009-04-28 08:17:54 +0000142 int o1, o2;
143
Magnus Damm1b56b962012-12-14 14:54:00 +0900144 o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000145
146 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
147 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000148 o2 = o1;
Magnus Damm1b56b962012-12-14 14:54:00 +0900149 v1 = sh_cmt_read_cmcnt(p);
150 v2 = sh_cmt_read_cmcnt(p);
151 v3 = sh_cmt_read_cmcnt(p);
152 o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000153 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
154 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000155
Magnus Damm5b644c72009-04-28 08:17:54 +0000156 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000157 return v2;
158}
159
Magnus Damm587acb32012-12-14 14:54:10 +0900160static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000161
162static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
163{
Paul Mundt46a12f72009-05-03 17:57:17 +0900164 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000165 unsigned long flags, value;
166
167 /* start stop register shared by multiple timer channels */
Paul Mundt7d0c3992012-05-25 13:36:43 +0900168 raw_spin_lock_irqsave(&sh_cmt_lock, flags);
Magnus Damm1b56b962012-12-14 14:54:00 +0900169 value = sh_cmt_read_cmstr(p);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000170
171 if (start)
172 value |= 1 << cfg->timer_bit;
173 else
174 value &= ~(1 << cfg->timer_bit);
175
Magnus Damm1b56b962012-12-14 14:54:00 +0900176 sh_cmt_write_cmstr(p, value);
Paul Mundt7d0c3992012-05-25 13:36:43 +0900177 raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000178}
179
180static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
181{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000182 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000183
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200184 pm_runtime_get_sync(&p->pdev->dev);
185 dev_pm_syscore_device(&p->pdev->dev, true);
186
Paul Mundt9436b4a2011-05-31 15:26:42 +0900187 /* enable clock */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000188 ret = clk_enable(p->clk);
189 if (ret) {
Paul Mundt214a6072010-03-10 16:26:25 +0900190 dev_err(&p->pdev->dev, "cannot enable clock\n");
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000191 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000192 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000193
194 /* make sure channel is disabled */
195 sh_cmt_start_stop_ch(p, 0);
196
197 /* configure channel, periodic mode and maximum timeout */
Magnus Damm3014f472009-04-29 14:50:37 +0000198 if (p->width == 16) {
199 *rate = clk_get_rate(p->clk) / 512;
Magnus Damm1b56b962012-12-14 14:54:00 +0900200 sh_cmt_write_cmcsr(p, 0x43);
Magnus Damm3014f472009-04-29 14:50:37 +0000201 } else {
202 *rate = clk_get_rate(p->clk) / 8;
Magnus Damm1b56b962012-12-14 14:54:00 +0900203 sh_cmt_write_cmcsr(p, 0x01a4);
Magnus Damm3014f472009-04-29 14:50:37 +0000204 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000205
Magnus Damm1b56b962012-12-14 14:54:00 +0900206 sh_cmt_write_cmcor(p, 0xffffffff);
207 sh_cmt_write_cmcnt(p, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000208
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000209 /*
210 * According to the sh73a0 user's manual, as CMCNT can be operated
211 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
212 * modifying CMCNT register; two RCLK cycles are necessary before
213 * this register is either read or any modification of the value
214 * it holds is reflected in the LSI's actual operation.
215 *
216 * While at it, we're supposed to clear out the CMCNT as of this
217 * moment, so make sure it's processed properly here. This will
218 * take RCLKx2 at maximum.
219 */
220 for (k = 0; k < 100; k++) {
Magnus Damm1b56b962012-12-14 14:54:00 +0900221 if (!sh_cmt_read_cmcnt(p))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000222 break;
223 udelay(1);
224 }
225
Magnus Damm1b56b962012-12-14 14:54:00 +0900226 if (sh_cmt_read_cmcnt(p)) {
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000227 dev_err(&p->pdev->dev, "cannot clear CMCNT\n");
228 ret = -ETIMEDOUT;
229 goto err1;
230 }
231
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000232 /* enable channel */
233 sh_cmt_start_stop_ch(p, 1);
234 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000235 err1:
236 /* stop clock */
237 clk_disable(p->clk);
238
239 err0:
240 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000241}
242
243static void sh_cmt_disable(struct sh_cmt_priv *p)
244{
245 /* disable channel */
246 sh_cmt_start_stop_ch(p, 0);
247
Magnus Dammbe890a12009-06-17 05:04:04 +0000248 /* disable interrupts in CMT block */
Magnus Damm1b56b962012-12-14 14:54:00 +0900249 sh_cmt_write_cmcsr(p, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000250
Paul Mundt9436b4a2011-05-31 15:26:42 +0900251 /* stop clock */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000252 clk_disable(p->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200253
254 dev_pm_syscore_device(&p->pdev->dev, false);
255 pm_runtime_put(&p->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000256}
257
258/* private flags */
259#define FLAG_CLOCKEVENT (1 << 0)
260#define FLAG_CLOCKSOURCE (1 << 1)
261#define FLAG_REPROGRAM (1 << 2)
262#define FLAG_SKIPEVENT (1 << 3)
263#define FLAG_IRQCONTEXT (1 << 4)
264
265static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
266 int absolute)
267{
268 unsigned long new_match;
269 unsigned long value = p->next_match_value;
270 unsigned long delay = 0;
271 unsigned long now = 0;
272 int has_wrapped;
273
274 now = sh_cmt_get_counter(p, &has_wrapped);
275 p->flags |= FLAG_REPROGRAM; /* force reprogram */
276
277 if (has_wrapped) {
278 /* we're competing with the interrupt handler.
279 * -> let the interrupt handler reprogram the timer.
280 * -> interrupt number two handles the event.
281 */
282 p->flags |= FLAG_SKIPEVENT;
283 return;
284 }
285
286 if (absolute)
287 now = 0;
288
289 do {
290 /* reprogram the timer hardware,
291 * but don't save the new match value yet.
292 */
293 new_match = now + value + delay;
294 if (new_match > p->max_match_value)
295 new_match = p->max_match_value;
296
Magnus Damm1b56b962012-12-14 14:54:00 +0900297 sh_cmt_write_cmcor(p, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000298
299 now = sh_cmt_get_counter(p, &has_wrapped);
300 if (has_wrapped && (new_match > p->match_value)) {
301 /* we are changing to a greater match value,
302 * so this wrap must be caused by the counter
303 * matching the old value.
304 * -> first interrupt reprograms the timer.
305 * -> interrupt number two handles the event.
306 */
307 p->flags |= FLAG_SKIPEVENT;
308 break;
309 }
310
311 if (has_wrapped) {
312 /* we are changing to a smaller match value,
313 * so the wrap must be caused by the counter
314 * matching the new value.
315 * -> save programmed match value.
316 * -> let isr handle the event.
317 */
318 p->match_value = new_match;
319 break;
320 }
321
322 /* be safe: verify hardware settings */
323 if (now < new_match) {
324 /* timer value is below match value, all good.
325 * this makes sure we won't miss any match events.
326 * -> save programmed match value.
327 * -> let isr handle the event.
328 */
329 p->match_value = new_match;
330 break;
331 }
332
333 /* the counter has reached a value greater
334 * than our new match value. and since the
335 * has_wrapped flag isn't set we must have
336 * programmed a too close event.
337 * -> increase delay and retry.
338 */
339 if (delay)
340 delay <<= 1;
341 else
342 delay = 1;
343
344 if (!delay)
Paul Mundt214a6072010-03-10 16:26:25 +0900345 dev_warn(&p->pdev->dev, "too long delay\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000346
347 } while (delay);
348}
349
Takashi YOSHII65ada542010-12-17 07:25:09 +0000350static void __sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
351{
352 if (delta > p->max_match_value)
353 dev_warn(&p->pdev->dev, "delta out of range\n");
354
355 p->next_match_value = delta;
356 sh_cmt_clock_event_program_verify(p, 0);
357}
358
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000359static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
360{
361 unsigned long flags;
362
Paul Mundt7d0c3992012-05-25 13:36:43 +0900363 raw_spin_lock_irqsave(&p->lock, flags);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000364 __sh_cmt_set_next(p, delta);
Paul Mundt7d0c3992012-05-25 13:36:43 +0900365 raw_spin_unlock_irqrestore(&p->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000366}
367
368static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
369{
370 struct sh_cmt_priv *p = dev_id;
371
372 /* clear flags */
Magnus Damm1b56b962012-12-14 14:54:00 +0900373 sh_cmt_write_cmcsr(p, sh_cmt_read_cmcsr(p) & p->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000374
375 /* update clock source counter to begin with if enabled
376 * the wrap flag should be cleared by the timer specific
377 * isr before we end up here.
378 */
379 if (p->flags & FLAG_CLOCKSOURCE)
Magnus Damm43809472010-08-04 04:31:38 +0000380 p->total_cycles += p->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000381
382 if (!(p->flags & FLAG_REPROGRAM))
383 p->next_match_value = p->max_match_value;
384
385 p->flags |= FLAG_IRQCONTEXT;
386
387 if (p->flags & FLAG_CLOCKEVENT) {
388 if (!(p->flags & FLAG_SKIPEVENT)) {
389 if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
390 p->next_match_value = p->max_match_value;
391 p->flags |= FLAG_REPROGRAM;
392 }
393
394 p->ced.event_handler(&p->ced);
395 }
396 }
397
398 p->flags &= ~FLAG_SKIPEVENT;
399
400 if (p->flags & FLAG_REPROGRAM) {
401 p->flags &= ~FLAG_REPROGRAM;
402 sh_cmt_clock_event_program_verify(p, 1);
403
404 if (p->flags & FLAG_CLOCKEVENT)
405 if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
406 || (p->match_value == p->next_match_value))
407 p->flags &= ~FLAG_REPROGRAM;
408 }
409
410 p->flags &= ~FLAG_IRQCONTEXT;
411
412 return IRQ_HANDLED;
413}
414
415static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
416{
417 int ret = 0;
418 unsigned long flags;
419
Paul Mundt7d0c3992012-05-25 13:36:43 +0900420 raw_spin_lock_irqsave(&p->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000421
422 if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
423 ret = sh_cmt_enable(p, &p->rate);
424
425 if (ret)
426 goto out;
427 p->flags |= flag;
428
429 /* setup timeout if no clockevent */
430 if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
Takashi YOSHII65ada542010-12-17 07:25:09 +0000431 __sh_cmt_set_next(p, p->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000432 out:
Paul Mundt7d0c3992012-05-25 13:36:43 +0900433 raw_spin_unlock_irqrestore(&p->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000434
435 return ret;
436}
437
438static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
439{
440 unsigned long flags;
441 unsigned long f;
442
Paul Mundt7d0c3992012-05-25 13:36:43 +0900443 raw_spin_lock_irqsave(&p->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000444
445 f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
446 p->flags &= ~flag;
447
448 if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
449 sh_cmt_disable(p);
450
451 /* adjust the timeout to maximum if only clocksource left */
452 if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
Takashi YOSHII65ada542010-12-17 07:25:09 +0000453 __sh_cmt_set_next(p, p->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000454
Paul Mundt7d0c3992012-05-25 13:36:43 +0900455 raw_spin_unlock_irqrestore(&p->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000456}
457
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000458static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs)
459{
460 return container_of(cs, struct sh_cmt_priv, cs);
461}
462
463static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
464{
465 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
466 unsigned long flags, raw;
467 unsigned long value;
468 int has_wrapped;
469
Paul Mundt7d0c3992012-05-25 13:36:43 +0900470 raw_spin_lock_irqsave(&p->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000471 value = p->total_cycles;
472 raw = sh_cmt_get_counter(p, &has_wrapped);
473
474 if (unlikely(has_wrapped))
Magnus Damm43809472010-08-04 04:31:38 +0000475 raw += p->match_value + 1;
Paul Mundt7d0c3992012-05-25 13:36:43 +0900476 raw_spin_unlock_irqrestore(&p->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000477
478 return value + raw;
479}
480
481static int sh_cmt_clocksource_enable(struct clocksource *cs)
482{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900483 int ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000484 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000485
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200486 WARN_ON(p->cs_enabled);
487
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000488 p->total_cycles = 0;
489
Magnus Damm3593f5f2011-04-25 22:32:11 +0900490 ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200491 if (!ret) {
Magnus Damm3593f5f2011-04-25 22:32:11 +0900492 __clocksource_updatefreq_hz(cs, p->rate);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200493 p->cs_enabled = true;
494 }
Magnus Damm3593f5f2011-04-25 22:32:11 +0900495 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000496}
497
498static void sh_cmt_clocksource_disable(struct clocksource *cs)
499{
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200500 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
501
502 WARN_ON(!p->cs_enabled);
503
504 sh_cmt_stop(p, FLAG_CLOCKSOURCE);
505 p->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000506}
507
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200508static void sh_cmt_clocksource_suspend(struct clocksource *cs)
509{
510 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
511
512 sh_cmt_stop(p, FLAG_CLOCKSOURCE);
513 pm_genpd_syscore_poweroff(&p->pdev->dev);
514}
515
Magnus Dammc8162882010-02-02 14:41:40 -0800516static void sh_cmt_clocksource_resume(struct clocksource *cs)
517{
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200518 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
519
520 pm_genpd_syscore_poweron(&p->pdev->dev);
521 sh_cmt_start(p, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800522}
523
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000524static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
525 char *name, unsigned long rating)
526{
527 struct clocksource *cs = &p->cs;
528
529 cs->name = name;
530 cs->rating = rating;
531 cs->read = sh_cmt_clocksource_read;
532 cs->enable = sh_cmt_clocksource_enable;
533 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200534 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800535 cs->resume = sh_cmt_clocksource_resume;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000536 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
537 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900538
Paul Mundt214a6072010-03-10 16:26:25 +0900539 dev_info(&p->pdev->dev, "used as clock source\n");
Paul Mundtf4d7c352010-06-02 17:10:44 +0900540
Magnus Damm3593f5f2011-04-25 22:32:11 +0900541 /* Register with dummy 1 Hz value, gets updated in ->enable() */
542 clocksource_register_hz(cs, 1);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000543 return 0;
544}
545
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000546static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
547{
548 return container_of(ced, struct sh_cmt_priv, ced);
549}
550
551static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
552{
553 struct clock_event_device *ced = &p->ced;
554
555 sh_cmt_start(p, FLAG_CLOCKEVENT);
556
557 /* TODO: calculate good shift from rate and counter bit width */
558
559 ced->shift = 32;
560 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
561 ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
562 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
563
564 if (periodic)
Magnus Damm43809472010-08-04 04:31:38 +0000565 sh_cmt_set_next(p, ((p->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000566 else
567 sh_cmt_set_next(p, p->max_match_value);
568}
569
570static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
571 struct clock_event_device *ced)
572{
573 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
574
575 /* deal with old setting first */
576 switch (ced->mode) {
577 case CLOCK_EVT_MODE_PERIODIC:
578 case CLOCK_EVT_MODE_ONESHOT:
579 sh_cmt_stop(p, FLAG_CLOCKEVENT);
580 break;
581 default:
582 break;
583 }
584
585 switch (mode) {
586 case CLOCK_EVT_MODE_PERIODIC:
Paul Mundt214a6072010-03-10 16:26:25 +0900587 dev_info(&p->pdev->dev, "used for periodic clock events\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000588 sh_cmt_clock_event_start(p, 1);
589 break;
590 case CLOCK_EVT_MODE_ONESHOT:
Paul Mundt214a6072010-03-10 16:26:25 +0900591 dev_info(&p->pdev->dev, "used for oneshot clock events\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000592 sh_cmt_clock_event_start(p, 0);
593 break;
594 case CLOCK_EVT_MODE_SHUTDOWN:
595 case CLOCK_EVT_MODE_UNUSED:
596 sh_cmt_stop(p, FLAG_CLOCKEVENT);
597 break;
598 default:
599 break;
600 }
601}
602
603static int sh_cmt_clock_event_next(unsigned long delta,
604 struct clock_event_device *ced)
605{
606 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
607
608 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
609 if (likely(p->flags & FLAG_IRQCONTEXT))
Magnus Damm43809472010-08-04 04:31:38 +0000610 p->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000611 else
Magnus Damm43809472010-08-04 04:31:38 +0000612 sh_cmt_set_next(p, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000613
614 return 0;
615}
616
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200617static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
618{
619 pm_genpd_syscore_poweroff(&ced_to_sh_cmt(ced)->pdev->dev);
620}
621
622static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
623{
624 pm_genpd_syscore_poweron(&ced_to_sh_cmt(ced)->pdev->dev);
625}
626
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000627static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
628 char *name, unsigned long rating)
629{
630 struct clock_event_device *ced = &p->ced;
631
632 memset(ced, 0, sizeof(*ced));
633
634 ced->name = name;
635 ced->features = CLOCK_EVT_FEAT_PERIODIC;
636 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
637 ced->rating = rating;
638 ced->cpumask = cpumask_of(0);
639 ced->set_next_event = sh_cmt_clock_event_next;
640 ced->set_mode = sh_cmt_clock_event_mode;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200641 ced->suspend = sh_cmt_clock_event_suspend;
642 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000643
Paul Mundt214a6072010-03-10 16:26:25 +0900644 dev_info(&p->pdev->dev, "used for clock events\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000645 clockevents_register_device(ced);
646}
647
Paul Mundtd1fcc0a2009-05-03 18:05:42 +0900648static int sh_cmt_register(struct sh_cmt_priv *p, char *name,
649 unsigned long clockevent_rating,
650 unsigned long clocksource_rating)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000651{
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000652 if (clockevent_rating)
653 sh_cmt_register_clockevent(p, name, clockevent_rating);
654
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000655 if (clocksource_rating)
656 sh_cmt_register_clocksource(p, name, clocksource_rating);
657
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000658 return 0;
659}
660
661static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
662{
Paul Mundt46a12f72009-05-03 17:57:17 +0900663 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000664 struct resource *res;
665 int irq, ret;
666 ret = -ENXIO;
667
668 memset(p, 0, sizeof(*p));
669 p->pdev = pdev;
670
671 if (!cfg) {
672 dev_err(&p->pdev->dev, "missing platform data\n");
673 goto err0;
674 }
675
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000676 res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
677 if (!res) {
678 dev_err(&p->pdev->dev, "failed to get I/O memory\n");
679 goto err0;
680 }
681
682 irq = platform_get_irq(p->pdev, 0);
683 if (irq < 0) {
684 dev_err(&p->pdev->dev, "failed to get irq\n");
685 goto err0;
686 }
687
688 /* map memory, let mapbase point to our channel */
689 p->mapbase = ioremap_nocache(res->start, resource_size(res));
690 if (p->mapbase == NULL) {
Paul Mundt214a6072010-03-10 16:26:25 +0900691 dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000692 goto err0;
693 }
694
695 /* request irq using setup_irq() (too early for request_irq()) */
Paul Mundt214a6072010-03-10 16:26:25 +0900696 p->irqaction.name = dev_name(&p->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000697 p->irqaction.handler = sh_cmt_interrupt;
698 p->irqaction.dev_id = p;
Paul Mundtfecf0662010-04-15 11:59:28 +0900699 p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
700 IRQF_IRQPOLL | IRQF_NOBALANCING;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000701
702 /* get hold of clock */
Paul Mundtc2a25e82010-03-29 16:55:43 +0900703 p->clk = clk_get(&p->pdev->dev, "cmt_fck");
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000704 if (IS_ERR(p->clk)) {
Magnus Damm03ff8582010-10-13 07:36:38 +0000705 dev_err(&p->pdev->dev, "cannot get clock\n");
706 ret = PTR_ERR(p->clk);
707 goto err1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000708 }
709
Magnus Dammcccd7042012-12-14 14:54:28 +0900710 p->read_control = sh_cmt_read16;
711 p->write_control = sh_cmt_write16;
712
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000713 if (resource_size(res) == 6) {
714 p->width = 16;
Magnus Damma6a912c2012-12-14 14:54:19 +0900715 p->read_count = sh_cmt_read16;
716 p->write_count = sh_cmt_write16;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000717 p->overflow_bit = 0x80;
Magnus Damm3014f472009-04-29 14:50:37 +0000718 p->clear_bits = ~0x80;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000719 } else {
720 p->width = 32;
Magnus Damma6a912c2012-12-14 14:54:19 +0900721 p->read_count = sh_cmt_read32;
722 p->write_count = sh_cmt_write32;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000723 p->overflow_bit = 0x8000;
724 p->clear_bits = ~0xc000;
725 }
726
Magnus Damm44a10f92012-12-14 14:53:41 +0900727 if (p->width == (sizeof(p->max_match_value) * 8))
728 p->max_match_value = ~0;
729 else
730 p->max_match_value = (1 << p->width) - 1;
731
732 p->match_value = p->max_match_value;
733 raw_spin_lock_init(&p->lock);
734
Paul Mundt214a6072010-03-10 16:26:25 +0900735 ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev),
Paul Mundtda64c2a2010-02-25 16:37:46 +0900736 cfg->clockevent_rating,
737 cfg->clocksource_rating);
738 if (ret) {
Paul Mundt214a6072010-03-10 16:26:25 +0900739 dev_err(&p->pdev->dev, "registration failed\n");
Magnus Damm2fd61b32012-12-14 14:53:32 +0900740 goto err2;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900741 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200742 p->cs_enabled = false;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900743
744 ret = setup_irq(irq, &p->irqaction);
745 if (ret) {
Paul Mundt214a6072010-03-10 16:26:25 +0900746 dev_err(&p->pdev->dev, "failed to request irq %d\n", irq);
Magnus Damm2fd61b32012-12-14 14:53:32 +0900747 goto err2;
Paul Mundtda64c2a2010-02-25 16:37:46 +0900748 }
749
Magnus Dammadccc692012-12-14 14:53:51 +0900750 platform_set_drvdata(pdev, p);
751
Paul Mundtda64c2a2010-02-25 16:37:46 +0900752 return 0;
Magnus Damm2fd61b32012-12-14 14:53:32 +0900753err2:
754 clk_put(p->clk);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900755
756err1:
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000757 iounmap(p->mapbase);
Paul Mundtda64c2a2010-02-25 16:37:46 +0900758err0:
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000759 return ret;
760}
761
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800762static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000763{
764 struct sh_cmt_priv *p = platform_get_drvdata(pdev);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200765 struct sh_timer_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000766 int ret;
767
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200768 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200769 pm_runtime_set_active(&pdev->dev);
770 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200771 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +0100772
Magnus Damme475eed2009-04-15 10:50:04 +0000773 if (p) {
Paul Mundt214a6072010-03-10 16:26:25 +0900774 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200775 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +0000776 }
777
Magnus Damm8e0b8422009-04-28 08:19:50 +0000778 p = kmalloc(sizeof(*p), GFP_KERNEL);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000779 if (p == NULL) {
780 dev_err(&pdev->dev, "failed to allocate driver data\n");
781 return -ENOMEM;
782 }
783
784 ret = sh_cmt_setup(p, pdev);
785 if (ret) {
Magnus Damm8e0b8422009-04-28 08:19:50 +0000786 kfree(p);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200787 pm_runtime_idle(&pdev->dev);
788 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000789 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200790 if (is_early_platform_device(pdev))
791 return 0;
792
793 out:
794 if (cfg->clockevent_rating || cfg->clocksource_rating)
795 pm_runtime_irq_safe(&pdev->dev);
796 else
797 pm_runtime_idle(&pdev->dev);
798
799 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000800}
801
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800802static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000803{
804 return -EBUSY; /* cannot unregister clockevent and clocksource */
805}
806
807static struct platform_driver sh_cmt_device_driver = {
808 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800809 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000810 .driver = {
811 .name = "sh_cmt",
812 }
813};
814
815static int __init sh_cmt_init(void)
816{
817 return platform_driver_register(&sh_cmt_device_driver);
818}
819
820static void __exit sh_cmt_exit(void)
821{
822 platform_driver_unregister(&sh_cmt_device_driver);
823}
824
Magnus Damme475eed2009-04-15 10:50:04 +0000825early_platform_init("earlytimer", &sh_cmt_device_driver);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000826module_init(sh_cmt_init);
827module_exit(sh_cmt_exit);
828
829MODULE_AUTHOR("Magnus Damm");
830MODULE_DESCRIPTION("SuperH CMT Timer Driver");
831MODULE_LICENSE("GPL v2");