blob: c24756489612e0e129ec66c46da5e1a2b8e96e06 [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>
21#include <linux/bootmem.h>
22#include <linux/platform_device.h>
23#include <linux/spinlock.h>
24#include <linux/interrupt.h>
25#include <linux/ioport.h>
26#include <linux/io.h>
27#include <linux/clk.h>
28#include <linux/irq.h>
29#include <linux/err.h>
30#include <linux/clocksource.h>
31#include <linux/clockchips.h>
32#include <linux/sh_cmt.h>
33
34struct sh_cmt_priv {
35 void __iomem *mapbase;
36 struct clk *clk;
37 unsigned long width; /* 16 or 32 bit version of hardware block */
38 unsigned long overflow_bit;
39 unsigned long clear_bits;
40 struct irqaction irqaction;
41 struct platform_device *pdev;
42
43 unsigned long flags;
44 unsigned long match_value;
45 unsigned long next_match_value;
46 unsigned long max_match_value;
47 unsigned long rate;
48 spinlock_t lock;
49 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +000050 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000051 unsigned long total_cycles;
52};
53
54static DEFINE_SPINLOCK(sh_cmt_lock);
55
56#define CMSTR -1 /* shared register */
57#define CMCSR 0 /* channel register */
58#define CMCNT 1 /* channel register */
59#define CMCOR 2 /* channel register */
60
61static inline unsigned long sh_cmt_read(struct sh_cmt_priv *p, int reg_nr)
62{
63 struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
64 void __iomem *base = p->mapbase;
65 unsigned long offs;
66
67 if (reg_nr == CMSTR) {
68 offs = 0;
69 base -= cfg->channel_offset;
70 } else
71 offs = reg_nr;
72
73 if (p->width == 16)
74 offs <<= 1;
75 else {
76 offs <<= 2;
77 if ((reg_nr == CMCNT) || (reg_nr == CMCOR))
78 return ioread32(base + offs);
79 }
80
81 return ioread16(base + offs);
82}
83
84static inline void sh_cmt_write(struct sh_cmt_priv *p, int reg_nr,
85 unsigned long value)
86{
87 struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
88 void __iomem *base = p->mapbase;
89 unsigned long offs;
90
91 if (reg_nr == CMSTR) {
92 offs = 0;
93 base -= cfg->channel_offset;
94 } else
95 offs = reg_nr;
96
97 if (p->width == 16)
98 offs <<= 1;
99 else {
100 offs <<= 2;
101 if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) {
102 iowrite32(value, base + offs);
103 return;
104 }
105 }
106
107 iowrite16(value, base + offs);
108}
109
110static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
111 int *has_wrapped)
112{
113 unsigned long v1, v2, v3;
114
115 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
116 do {
117 v1 = sh_cmt_read(p, CMCNT);
118 v2 = sh_cmt_read(p, CMCNT);
119 v3 = sh_cmt_read(p, CMCNT);
120 } while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
121 || (v3 > v1 && v3 < v2)));
122
123 *has_wrapped = sh_cmt_read(p, CMCSR) & p->overflow_bit;
124 return v2;
125}
126
127
128static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
129{
130 struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
131 unsigned long flags, value;
132
133 /* start stop register shared by multiple timer channels */
134 spin_lock_irqsave(&sh_cmt_lock, flags);
135 value = sh_cmt_read(p, CMSTR);
136
137 if (start)
138 value |= 1 << cfg->timer_bit;
139 else
140 value &= ~(1 << cfg->timer_bit);
141
142 sh_cmt_write(p, CMSTR, value);
143 spin_unlock_irqrestore(&sh_cmt_lock, flags);
144}
145
146static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
147{
148 struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
149 int ret;
150
151 /* enable clock */
152 ret = clk_enable(p->clk);
153 if (ret) {
154 pr_err("sh_cmt: cannot enable clock \"%s\"\n", cfg->clk);
155 return ret;
156 }
157 *rate = clk_get_rate(p->clk) / 8;
158
159 /* make sure channel is disabled */
160 sh_cmt_start_stop_ch(p, 0);
161
162 /* configure channel, periodic mode and maximum timeout */
163 if (p->width == 16)
164 sh_cmt_write(p, CMCSR, 0);
165 else
166 sh_cmt_write(p, CMCSR, 0x01a4);
167
168 sh_cmt_write(p, CMCOR, 0xffffffff);
169 sh_cmt_write(p, CMCNT, 0);
170
171 /* enable channel */
172 sh_cmt_start_stop_ch(p, 1);
173 return 0;
174}
175
176static void sh_cmt_disable(struct sh_cmt_priv *p)
177{
178 /* disable channel */
179 sh_cmt_start_stop_ch(p, 0);
180
181 /* stop clock */
182 clk_disable(p->clk);
183}
184
185/* private flags */
186#define FLAG_CLOCKEVENT (1 << 0)
187#define FLAG_CLOCKSOURCE (1 << 1)
188#define FLAG_REPROGRAM (1 << 2)
189#define FLAG_SKIPEVENT (1 << 3)
190#define FLAG_IRQCONTEXT (1 << 4)
191
192static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
193 int absolute)
194{
195 unsigned long new_match;
196 unsigned long value = p->next_match_value;
197 unsigned long delay = 0;
198 unsigned long now = 0;
199 int has_wrapped;
200
201 now = sh_cmt_get_counter(p, &has_wrapped);
202 p->flags |= FLAG_REPROGRAM; /* force reprogram */
203
204 if (has_wrapped) {
205 /* we're competing with the interrupt handler.
206 * -> let the interrupt handler reprogram the timer.
207 * -> interrupt number two handles the event.
208 */
209 p->flags |= FLAG_SKIPEVENT;
210 return;
211 }
212
213 if (absolute)
214 now = 0;
215
216 do {
217 /* reprogram the timer hardware,
218 * but don't save the new match value yet.
219 */
220 new_match = now + value + delay;
221 if (new_match > p->max_match_value)
222 new_match = p->max_match_value;
223
224 sh_cmt_write(p, CMCOR, new_match);
225
226 now = sh_cmt_get_counter(p, &has_wrapped);
227 if (has_wrapped && (new_match > p->match_value)) {
228 /* we are changing to a greater match value,
229 * so this wrap must be caused by the counter
230 * matching the old value.
231 * -> first interrupt reprograms the timer.
232 * -> interrupt number two handles the event.
233 */
234 p->flags |= FLAG_SKIPEVENT;
235 break;
236 }
237
238 if (has_wrapped) {
239 /* we are changing to a smaller match value,
240 * so the wrap must be caused by the counter
241 * matching the new value.
242 * -> save programmed match value.
243 * -> let isr handle the event.
244 */
245 p->match_value = new_match;
246 break;
247 }
248
249 /* be safe: verify hardware settings */
250 if (now < new_match) {
251 /* timer value is below match value, all good.
252 * this makes sure we won't miss any match events.
253 * -> save programmed match value.
254 * -> let isr handle the event.
255 */
256 p->match_value = new_match;
257 break;
258 }
259
260 /* the counter has reached a value greater
261 * than our new match value. and since the
262 * has_wrapped flag isn't set we must have
263 * programmed a too close event.
264 * -> increase delay and retry.
265 */
266 if (delay)
267 delay <<= 1;
268 else
269 delay = 1;
270
271 if (!delay)
272 pr_warning("sh_cmt: too long delay\n");
273
274 } while (delay);
275}
276
277static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
278{
279 unsigned long flags;
280
281 if (delta > p->max_match_value)
282 pr_warning("sh_cmt: delta out of range\n");
283
284 spin_lock_irqsave(&p->lock, flags);
285 p->next_match_value = delta;
286 sh_cmt_clock_event_program_verify(p, 0);
287 spin_unlock_irqrestore(&p->lock, flags);
288}
289
290static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
291{
292 struct sh_cmt_priv *p = dev_id;
293
294 /* clear flags */
295 sh_cmt_write(p, CMCSR, sh_cmt_read(p, CMCSR) & p->clear_bits);
296
297 /* update clock source counter to begin with if enabled
298 * the wrap flag should be cleared by the timer specific
299 * isr before we end up here.
300 */
301 if (p->flags & FLAG_CLOCKSOURCE)
302 p->total_cycles += p->match_value;
303
304 if (!(p->flags & FLAG_REPROGRAM))
305 p->next_match_value = p->max_match_value;
306
307 p->flags |= FLAG_IRQCONTEXT;
308
309 if (p->flags & FLAG_CLOCKEVENT) {
310 if (!(p->flags & FLAG_SKIPEVENT)) {
311 if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
312 p->next_match_value = p->max_match_value;
313 p->flags |= FLAG_REPROGRAM;
314 }
315
316 p->ced.event_handler(&p->ced);
317 }
318 }
319
320 p->flags &= ~FLAG_SKIPEVENT;
321
322 if (p->flags & FLAG_REPROGRAM) {
323 p->flags &= ~FLAG_REPROGRAM;
324 sh_cmt_clock_event_program_verify(p, 1);
325
326 if (p->flags & FLAG_CLOCKEVENT)
327 if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
328 || (p->match_value == p->next_match_value))
329 p->flags &= ~FLAG_REPROGRAM;
330 }
331
332 p->flags &= ~FLAG_IRQCONTEXT;
333
334 return IRQ_HANDLED;
335}
336
337static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
338{
339 int ret = 0;
340 unsigned long flags;
341
342 spin_lock_irqsave(&p->lock, flags);
343
344 if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
345 ret = sh_cmt_enable(p, &p->rate);
346
347 if (ret)
348 goto out;
349 p->flags |= flag;
350
351 /* setup timeout if no clockevent */
352 if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
353 sh_cmt_set_next(p, p->max_match_value);
354 out:
355 spin_unlock_irqrestore(&p->lock, flags);
356
357 return ret;
358}
359
360static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
361{
362 unsigned long flags;
363 unsigned long f;
364
365 spin_lock_irqsave(&p->lock, flags);
366
367 f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
368 p->flags &= ~flag;
369
370 if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
371 sh_cmt_disable(p);
372
373 /* adjust the timeout to maximum if only clocksource left */
374 if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
375 sh_cmt_set_next(p, p->max_match_value);
376
377 spin_unlock_irqrestore(&p->lock, flags);
378}
379
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000380static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs)
381{
382 return container_of(cs, struct sh_cmt_priv, cs);
383}
384
385static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
386{
387 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
388 unsigned long flags, raw;
389 unsigned long value;
390 int has_wrapped;
391
392 spin_lock_irqsave(&p->lock, flags);
393 value = p->total_cycles;
394 raw = sh_cmt_get_counter(p, &has_wrapped);
395
396 if (unlikely(has_wrapped))
397 raw = p->match_value;
398 spin_unlock_irqrestore(&p->lock, flags);
399
400 return value + raw;
401}
402
403static int sh_cmt_clocksource_enable(struct clocksource *cs)
404{
405 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
406 int ret;
407
408 p->total_cycles = 0;
409
410 ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
411 if (ret)
412 return ret;
413
414 /* TODO: calculate good shift from rate and counter bit width */
415 cs->shift = 0;
416 cs->mult = clocksource_hz2mult(p->rate, cs->shift);
417 return 0;
418}
419
420static void sh_cmt_clocksource_disable(struct clocksource *cs)
421{
422 sh_cmt_stop(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
423}
424
425static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
426 char *name, unsigned long rating)
427{
428 struct clocksource *cs = &p->cs;
429
430 cs->name = name;
431 cs->rating = rating;
432 cs->read = sh_cmt_clocksource_read;
433 cs->enable = sh_cmt_clocksource_enable;
434 cs->disable = sh_cmt_clocksource_disable;
435 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
436 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
437 pr_info("sh_cmt: %s used as clock source\n", cs->name);
438 clocksource_register(cs);
439 return 0;
440}
441
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000442static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
443{
444 return container_of(ced, struct sh_cmt_priv, ced);
445}
446
447static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
448{
449 struct clock_event_device *ced = &p->ced;
450
451 sh_cmt_start(p, FLAG_CLOCKEVENT);
452
453 /* TODO: calculate good shift from rate and counter bit width */
454
455 ced->shift = 32;
456 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
457 ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
458 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
459
460 if (periodic)
461 sh_cmt_set_next(p, (p->rate + HZ/2) / HZ);
462 else
463 sh_cmt_set_next(p, p->max_match_value);
464}
465
466static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
467 struct clock_event_device *ced)
468{
469 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
470
471 /* deal with old setting first */
472 switch (ced->mode) {
473 case CLOCK_EVT_MODE_PERIODIC:
474 case CLOCK_EVT_MODE_ONESHOT:
475 sh_cmt_stop(p, FLAG_CLOCKEVENT);
476 break;
477 default:
478 break;
479 }
480
481 switch (mode) {
482 case CLOCK_EVT_MODE_PERIODIC:
483 pr_info("sh_cmt: %s used for periodic clock events\n",
484 ced->name);
485 sh_cmt_clock_event_start(p, 1);
486 break;
487 case CLOCK_EVT_MODE_ONESHOT:
488 pr_info("sh_cmt: %s used for oneshot clock events\n",
489 ced->name);
490 sh_cmt_clock_event_start(p, 0);
491 break;
492 case CLOCK_EVT_MODE_SHUTDOWN:
493 case CLOCK_EVT_MODE_UNUSED:
494 sh_cmt_stop(p, FLAG_CLOCKEVENT);
495 break;
496 default:
497 break;
498 }
499}
500
501static int sh_cmt_clock_event_next(unsigned long delta,
502 struct clock_event_device *ced)
503{
504 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
505
506 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
507 if (likely(p->flags & FLAG_IRQCONTEXT))
508 p->next_match_value = delta;
509 else
510 sh_cmt_set_next(p, delta);
511
512 return 0;
513}
514
515static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
516 char *name, unsigned long rating)
517{
518 struct clock_event_device *ced = &p->ced;
519
520 memset(ced, 0, sizeof(*ced));
521
522 ced->name = name;
523 ced->features = CLOCK_EVT_FEAT_PERIODIC;
524 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
525 ced->rating = rating;
526 ced->cpumask = cpumask_of(0);
527 ced->set_next_event = sh_cmt_clock_event_next;
528 ced->set_mode = sh_cmt_clock_event_mode;
529
530 pr_info("sh_cmt: %s used for clock events\n", ced->name);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000531 clockevents_register_device(ced);
532}
533
534int sh_cmt_register(struct sh_cmt_priv *p, char *name,
535 unsigned long clockevent_rating,
536 unsigned long clocksource_rating)
537{
538 if (p->width == (sizeof(p->max_match_value) * 8))
539 p->max_match_value = ~0;
540 else
541 p->max_match_value = (1 << p->width) - 1;
542
543 p->match_value = p->max_match_value;
544 spin_lock_init(&p->lock);
545
546 if (clockevent_rating)
547 sh_cmt_register_clockevent(p, name, clockevent_rating);
548
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000549 if (clocksource_rating)
550 sh_cmt_register_clocksource(p, name, clocksource_rating);
551
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000552 return 0;
553}
554
555static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
556{
557 struct sh_cmt_config *cfg = pdev->dev.platform_data;
558 struct resource *res;
559 int irq, ret;
560 ret = -ENXIO;
561
562 memset(p, 0, sizeof(*p));
563 p->pdev = pdev;
564
565 if (!cfg) {
566 dev_err(&p->pdev->dev, "missing platform data\n");
567 goto err0;
568 }
569
570 platform_set_drvdata(pdev, p);
571
572 res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
573 if (!res) {
574 dev_err(&p->pdev->dev, "failed to get I/O memory\n");
575 goto err0;
576 }
577
578 irq = platform_get_irq(p->pdev, 0);
579 if (irq < 0) {
580 dev_err(&p->pdev->dev, "failed to get irq\n");
581 goto err0;
582 }
583
584 /* map memory, let mapbase point to our channel */
585 p->mapbase = ioremap_nocache(res->start, resource_size(res));
586 if (p->mapbase == NULL) {
587 pr_err("sh_cmt: failed to remap I/O memory\n");
588 goto err0;
589 }
590
591 /* request irq using setup_irq() (too early for request_irq()) */
592 p->irqaction.name = cfg->name;
593 p->irqaction.handler = sh_cmt_interrupt;
594 p->irqaction.dev_id = p;
595 p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL;
596 p->irqaction.mask = CPU_MASK_NONE;
597 ret = setup_irq(irq, &p->irqaction);
598 if (ret) {
599 pr_err("sh_cmt: failed to request irq %d\n", irq);
600 goto err1;
601 }
602
603 /* get hold of clock */
604 p->clk = clk_get(&p->pdev->dev, cfg->clk);
605 if (IS_ERR(p->clk)) {
606 pr_err("sh_cmt: cannot get clock \"%s\"\n", cfg->clk);
607 ret = PTR_ERR(p->clk);
608 goto err2;
609 }
610
611 if (resource_size(res) == 6) {
612 p->width = 16;
613 p->overflow_bit = 0x80;
614 p->clear_bits = ~0xc0;
615 } else {
616 p->width = 32;
617 p->overflow_bit = 0x8000;
618 p->clear_bits = ~0xc000;
619 }
620
621 return sh_cmt_register(p, cfg->name,
622 cfg->clockevent_rating,
623 cfg->clocksource_rating);
624 err2:
Magnus Damm3093e782009-04-01 14:11:07 +0000625 remove_irq(irq, &p->irqaction);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000626 err1:
627 iounmap(p->mapbase);
628 err0:
629 return ret;
630}
631
632static int __devinit sh_cmt_probe(struct platform_device *pdev)
633{
634 struct sh_cmt_priv *p = platform_get_drvdata(pdev);
Magnus Damme475eed2009-04-15 10:50:04 +0000635 struct sh_cmt_config *cfg = pdev->dev.platform_data;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000636 int ret;
637
Magnus Damme475eed2009-04-15 10:50:04 +0000638 if (p) {
639 pr_info("sh_cmt: %s kept as earlytimer\n", cfg->name);
640 return 0;
641 }
642
643 if (is_early_platform_device(pdev))
644 p = alloc_bootmem(sizeof(*p));
645 else
646 p = kmalloc(sizeof(*p), GFP_KERNEL);
647
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000648 if (p == NULL) {
649 dev_err(&pdev->dev, "failed to allocate driver data\n");
650 return -ENOMEM;
651 }
652
653 ret = sh_cmt_setup(p, pdev);
654 if (ret) {
Magnus Damme475eed2009-04-15 10:50:04 +0000655 if (is_early_platform_device(pdev))
656 free_bootmem(__pa(p), sizeof(*p));
657 else
658 kfree(p);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000659
660 platform_set_drvdata(pdev, NULL);
661 }
662 return ret;
663}
664
665static int __devexit sh_cmt_remove(struct platform_device *pdev)
666{
667 return -EBUSY; /* cannot unregister clockevent and clocksource */
668}
669
670static struct platform_driver sh_cmt_device_driver = {
671 .probe = sh_cmt_probe,
672 .remove = __devexit_p(sh_cmt_remove),
673 .driver = {
674 .name = "sh_cmt",
675 }
676};
677
678static int __init sh_cmt_init(void)
679{
680 return platform_driver_register(&sh_cmt_device_driver);
681}
682
683static void __exit sh_cmt_exit(void)
684{
685 platform_driver_unregister(&sh_cmt_device_driver);
686}
687
Magnus Damme475eed2009-04-15 10:50:04 +0000688early_platform_init("earlytimer", &sh_cmt_device_driver);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000689module_init(sh_cmt_init);
690module_exit(sh_cmt_exit);
691
692MODULE_AUTHOR("Magnus Damm");
693MODULE_DESCRIPTION("SuperH CMT Timer Driver");
694MODULE_LICENSE("GPL v2");