Merge branch 'akpm'
[linux-2.6/next.git] / drivers / clocksource / sh_cmt.c
blobca09bc421ddbc25345cf3d6d9b0efe237941454c
1 /*
2 * SuperH Timer Support - CMT
4 * Copyright (C) 2008 Magnus Damm
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
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.
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
20 #include <linux/init.h>
21 #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>
29 #include <linux/delay.h>
30 #include <linux/clocksource.h>
31 #include <linux/clockchips.h>
32 #include <linux/sh_timer.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
36 struct sh_cmt_priv {
37 void __iomem *mapbase;
38 struct clk *clk;
39 unsigned long width; /* 16 or 32 bit version of hardware block */
40 unsigned long overflow_bit;
41 unsigned long clear_bits;
42 struct irqaction irqaction;
43 struct platform_device *pdev;
45 unsigned long flags;
46 unsigned long match_value;
47 unsigned long next_match_value;
48 unsigned long max_match_value;
49 unsigned long rate;
50 spinlock_t lock;
51 struct clock_event_device ced;
52 struct clocksource cs;
53 unsigned long total_cycles;
56 static DEFINE_SPINLOCK(sh_cmt_lock);
58 #define CMSTR -1 /* shared register */
59 #define CMCSR 0 /* channel register */
60 #define CMCNT 1 /* channel register */
61 #define CMCOR 2 /* channel register */
63 static inline unsigned long sh_cmt_read(struct sh_cmt_priv *p, int reg_nr)
65 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
66 void __iomem *base = p->mapbase;
67 unsigned long offs;
69 if (reg_nr == CMSTR) {
70 offs = 0;
71 base -= cfg->channel_offset;
72 } else
73 offs = reg_nr;
75 if (p->width == 16)
76 offs <<= 1;
77 else {
78 offs <<= 2;
79 if ((reg_nr == CMCNT) || (reg_nr == CMCOR))
80 return ioread32(base + offs);
83 return ioread16(base + offs);
86 static inline void sh_cmt_write(struct sh_cmt_priv *p, int reg_nr,
87 unsigned long value)
89 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
90 void __iomem *base = p->mapbase;
91 unsigned long offs;
93 if (reg_nr == CMSTR) {
94 offs = 0;
95 base -= cfg->channel_offset;
96 } else
97 offs = reg_nr;
99 if (p->width == 16)
100 offs <<= 1;
101 else {
102 offs <<= 2;
103 if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) {
104 iowrite32(value, base + offs);
105 return;
109 iowrite16(value, base + offs);
112 static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
113 int *has_wrapped)
115 unsigned long v1, v2, v3;
116 int o1, o2;
118 o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit;
120 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
121 do {
122 o2 = o1;
123 v1 = sh_cmt_read(p, CMCNT);
124 v2 = sh_cmt_read(p, CMCNT);
125 v3 = sh_cmt_read(p, CMCNT);
126 o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit;
127 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
128 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
130 *has_wrapped = o1;
131 return v2;
135 static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
137 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
138 unsigned long flags, value;
140 /* start stop register shared by multiple timer channels */
141 spin_lock_irqsave(&sh_cmt_lock, flags);
142 value = sh_cmt_read(p, CMSTR);
144 if (start)
145 value |= 1 << cfg->timer_bit;
146 else
147 value &= ~(1 << cfg->timer_bit);
149 sh_cmt_write(p, CMSTR, value);
150 spin_unlock_irqrestore(&sh_cmt_lock, flags);
153 static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
155 int k, ret;
157 /* enable clock */
158 ret = clk_enable(p->clk);
159 if (ret) {
160 dev_err(&p->pdev->dev, "cannot enable clock\n");
161 goto err0;
164 /* make sure channel is disabled */
165 sh_cmt_start_stop_ch(p, 0);
167 /* configure channel, periodic mode and maximum timeout */
168 if (p->width == 16) {
169 *rate = clk_get_rate(p->clk) / 512;
170 sh_cmt_write(p, CMCSR, 0x43);
171 } else {
172 *rate = clk_get_rate(p->clk) / 8;
173 sh_cmt_write(p, CMCSR, 0x01a4);
176 sh_cmt_write(p, CMCOR, 0xffffffff);
177 sh_cmt_write(p, CMCNT, 0);
180 * According to the sh73a0 user's manual, as CMCNT can be operated
181 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
182 * modifying CMCNT register; two RCLK cycles are necessary before
183 * this register is either read or any modification of the value
184 * it holds is reflected in the LSI's actual operation.
186 * While at it, we're supposed to clear out the CMCNT as of this
187 * moment, so make sure it's processed properly here. This will
188 * take RCLKx2 at maximum.
190 for (k = 0; k < 100; k++) {
191 if (!sh_cmt_read(p, CMCNT))
192 break;
193 udelay(1);
196 if (sh_cmt_read(p, CMCNT)) {
197 dev_err(&p->pdev->dev, "cannot clear CMCNT\n");
198 ret = -ETIMEDOUT;
199 goto err1;
202 /* enable channel */
203 sh_cmt_start_stop_ch(p, 1);
204 return 0;
205 err1:
206 /* stop clock */
207 clk_disable(p->clk);
209 err0:
210 return ret;
213 static void sh_cmt_disable(struct sh_cmt_priv *p)
215 /* disable channel */
216 sh_cmt_start_stop_ch(p, 0);
218 /* disable interrupts in CMT block */
219 sh_cmt_write(p, CMCSR, 0);
221 /* stop clock */
222 clk_disable(p->clk);
225 /* private flags */
226 #define FLAG_CLOCKEVENT (1 << 0)
227 #define FLAG_CLOCKSOURCE (1 << 1)
228 #define FLAG_REPROGRAM (1 << 2)
229 #define FLAG_SKIPEVENT (1 << 3)
230 #define FLAG_IRQCONTEXT (1 << 4)
232 static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
233 int absolute)
235 unsigned long new_match;
236 unsigned long value = p->next_match_value;
237 unsigned long delay = 0;
238 unsigned long now = 0;
239 int has_wrapped;
241 now = sh_cmt_get_counter(p, &has_wrapped);
242 p->flags |= FLAG_REPROGRAM; /* force reprogram */
244 if (has_wrapped) {
245 /* we're competing with the interrupt handler.
246 * -> let the interrupt handler reprogram the timer.
247 * -> interrupt number two handles the event.
249 p->flags |= FLAG_SKIPEVENT;
250 return;
253 if (absolute)
254 now = 0;
256 do {
257 /* reprogram the timer hardware,
258 * but don't save the new match value yet.
260 new_match = now + value + delay;
261 if (new_match > p->max_match_value)
262 new_match = p->max_match_value;
264 sh_cmt_write(p, CMCOR, new_match);
266 now = sh_cmt_get_counter(p, &has_wrapped);
267 if (has_wrapped && (new_match > p->match_value)) {
268 /* we are changing to a greater match value,
269 * so this wrap must be caused by the counter
270 * matching the old value.
271 * -> first interrupt reprograms the timer.
272 * -> interrupt number two handles the event.
274 p->flags |= FLAG_SKIPEVENT;
275 break;
278 if (has_wrapped) {
279 /* we are changing to a smaller match value,
280 * so the wrap must be caused by the counter
281 * matching the new value.
282 * -> save programmed match value.
283 * -> let isr handle the event.
285 p->match_value = new_match;
286 break;
289 /* be safe: verify hardware settings */
290 if (now < new_match) {
291 /* timer value is below match value, all good.
292 * this makes sure we won't miss any match events.
293 * -> save programmed match value.
294 * -> let isr handle the event.
296 p->match_value = new_match;
297 break;
300 /* the counter has reached a value greater
301 * than our new match value. and since the
302 * has_wrapped flag isn't set we must have
303 * programmed a too close event.
304 * -> increase delay and retry.
306 if (delay)
307 delay <<= 1;
308 else
309 delay = 1;
311 if (!delay)
312 dev_warn(&p->pdev->dev, "too long delay\n");
314 } while (delay);
317 static void __sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
319 if (delta > p->max_match_value)
320 dev_warn(&p->pdev->dev, "delta out of range\n");
322 p->next_match_value = delta;
323 sh_cmt_clock_event_program_verify(p, 0);
326 static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
328 unsigned long flags;
330 spin_lock_irqsave(&p->lock, flags);
331 __sh_cmt_set_next(p, delta);
332 spin_unlock_irqrestore(&p->lock, flags);
335 static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
337 struct sh_cmt_priv *p = dev_id;
339 /* clear flags */
340 sh_cmt_write(p, CMCSR, sh_cmt_read(p, CMCSR) & p->clear_bits);
342 /* update clock source counter to begin with if enabled
343 * the wrap flag should be cleared by the timer specific
344 * isr before we end up here.
346 if (p->flags & FLAG_CLOCKSOURCE)
347 p->total_cycles += p->match_value + 1;
349 if (!(p->flags & FLAG_REPROGRAM))
350 p->next_match_value = p->max_match_value;
352 p->flags |= FLAG_IRQCONTEXT;
354 if (p->flags & FLAG_CLOCKEVENT) {
355 if (!(p->flags & FLAG_SKIPEVENT)) {
356 if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
357 p->next_match_value = p->max_match_value;
358 p->flags |= FLAG_REPROGRAM;
361 p->ced.event_handler(&p->ced);
365 p->flags &= ~FLAG_SKIPEVENT;
367 if (p->flags & FLAG_REPROGRAM) {
368 p->flags &= ~FLAG_REPROGRAM;
369 sh_cmt_clock_event_program_verify(p, 1);
371 if (p->flags & FLAG_CLOCKEVENT)
372 if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
373 || (p->match_value == p->next_match_value))
374 p->flags &= ~FLAG_REPROGRAM;
377 p->flags &= ~FLAG_IRQCONTEXT;
379 return IRQ_HANDLED;
382 static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
384 int ret = 0;
385 unsigned long flags;
387 spin_lock_irqsave(&p->lock, flags);
389 if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
390 ret = sh_cmt_enable(p, &p->rate);
392 if (ret)
393 goto out;
394 p->flags |= flag;
396 /* setup timeout if no clockevent */
397 if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
398 __sh_cmt_set_next(p, p->max_match_value);
399 out:
400 spin_unlock_irqrestore(&p->lock, flags);
402 return ret;
405 static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
407 unsigned long flags;
408 unsigned long f;
410 spin_lock_irqsave(&p->lock, flags);
412 f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
413 p->flags &= ~flag;
415 if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
416 sh_cmt_disable(p);
418 /* adjust the timeout to maximum if only clocksource left */
419 if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
420 __sh_cmt_set_next(p, p->max_match_value);
422 spin_unlock_irqrestore(&p->lock, flags);
425 static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs)
427 return container_of(cs, struct sh_cmt_priv, cs);
430 static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
432 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
433 unsigned long flags, raw;
434 unsigned long value;
435 int has_wrapped;
437 spin_lock_irqsave(&p->lock, flags);
438 value = p->total_cycles;
439 raw = sh_cmt_get_counter(p, &has_wrapped);
441 if (unlikely(has_wrapped))
442 raw += p->match_value + 1;
443 spin_unlock_irqrestore(&p->lock, flags);
445 return value + raw;
448 static int sh_cmt_clocksource_enable(struct clocksource *cs)
450 int ret;
451 struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
453 p->total_cycles = 0;
455 ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
456 if (!ret)
457 __clocksource_updatefreq_hz(cs, p->rate);
458 return ret;
461 static void sh_cmt_clocksource_disable(struct clocksource *cs)
463 sh_cmt_stop(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
466 static void sh_cmt_clocksource_resume(struct clocksource *cs)
468 sh_cmt_start(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
471 static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
472 char *name, unsigned long rating)
474 struct clocksource *cs = &p->cs;
476 cs->name = name;
477 cs->rating = rating;
478 cs->read = sh_cmt_clocksource_read;
479 cs->enable = sh_cmt_clocksource_enable;
480 cs->disable = sh_cmt_clocksource_disable;
481 cs->suspend = sh_cmt_clocksource_disable;
482 cs->resume = sh_cmt_clocksource_resume;
483 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
484 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
486 dev_info(&p->pdev->dev, "used as clock source\n");
488 /* Register with dummy 1 Hz value, gets updated in ->enable() */
489 clocksource_register_hz(cs, 1);
490 return 0;
493 static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
495 return container_of(ced, struct sh_cmt_priv, ced);
498 static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
500 struct clock_event_device *ced = &p->ced;
502 sh_cmt_start(p, FLAG_CLOCKEVENT);
504 /* TODO: calculate good shift from rate and counter bit width */
506 ced->shift = 32;
507 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
508 ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
509 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
511 if (periodic)
512 sh_cmt_set_next(p, ((p->rate + HZ/2) / HZ) - 1);
513 else
514 sh_cmt_set_next(p, p->max_match_value);
517 static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
518 struct clock_event_device *ced)
520 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
522 /* deal with old setting first */
523 switch (ced->mode) {
524 case CLOCK_EVT_MODE_PERIODIC:
525 case CLOCK_EVT_MODE_ONESHOT:
526 sh_cmt_stop(p, FLAG_CLOCKEVENT);
527 break;
528 default:
529 break;
532 switch (mode) {
533 case CLOCK_EVT_MODE_PERIODIC:
534 dev_info(&p->pdev->dev, "used for periodic clock events\n");
535 sh_cmt_clock_event_start(p, 1);
536 break;
537 case CLOCK_EVT_MODE_ONESHOT:
538 dev_info(&p->pdev->dev, "used for oneshot clock events\n");
539 sh_cmt_clock_event_start(p, 0);
540 break;
541 case CLOCK_EVT_MODE_SHUTDOWN:
542 case CLOCK_EVT_MODE_UNUSED:
543 sh_cmt_stop(p, FLAG_CLOCKEVENT);
544 break;
545 default:
546 break;
550 static int sh_cmt_clock_event_next(unsigned long delta,
551 struct clock_event_device *ced)
553 struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
555 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
556 if (likely(p->flags & FLAG_IRQCONTEXT))
557 p->next_match_value = delta - 1;
558 else
559 sh_cmt_set_next(p, delta - 1);
561 return 0;
564 static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
565 char *name, unsigned long rating)
567 struct clock_event_device *ced = &p->ced;
569 memset(ced, 0, sizeof(*ced));
571 ced->name = name;
572 ced->features = CLOCK_EVT_FEAT_PERIODIC;
573 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
574 ced->rating = rating;
575 ced->cpumask = cpumask_of(0);
576 ced->set_next_event = sh_cmt_clock_event_next;
577 ced->set_mode = sh_cmt_clock_event_mode;
579 dev_info(&p->pdev->dev, "used for clock events\n");
580 clockevents_register_device(ced);
583 static int sh_cmt_register(struct sh_cmt_priv *p, char *name,
584 unsigned long clockevent_rating,
585 unsigned long clocksource_rating)
587 if (p->width == (sizeof(p->max_match_value) * 8))
588 p->max_match_value = ~0;
589 else
590 p->max_match_value = (1 << p->width) - 1;
592 p->match_value = p->max_match_value;
593 spin_lock_init(&p->lock);
595 if (clockevent_rating)
596 sh_cmt_register_clockevent(p, name, clockevent_rating);
598 if (clocksource_rating)
599 sh_cmt_register_clocksource(p, name, clocksource_rating);
601 return 0;
604 static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
606 struct sh_timer_config *cfg = pdev->dev.platform_data;
607 struct resource *res;
608 int irq, ret;
609 ret = -ENXIO;
611 memset(p, 0, sizeof(*p));
612 p->pdev = pdev;
614 if (!cfg) {
615 dev_err(&p->pdev->dev, "missing platform data\n");
616 goto err0;
619 platform_set_drvdata(pdev, p);
621 res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
622 if (!res) {
623 dev_err(&p->pdev->dev, "failed to get I/O memory\n");
624 goto err0;
627 irq = platform_get_irq(p->pdev, 0);
628 if (irq < 0) {
629 dev_err(&p->pdev->dev, "failed to get irq\n");
630 goto err0;
633 /* map memory, let mapbase point to our channel */
634 p->mapbase = ioremap_nocache(res->start, resource_size(res));
635 if (p->mapbase == NULL) {
636 dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
637 goto err0;
640 /* request irq using setup_irq() (too early for request_irq()) */
641 p->irqaction.name = dev_name(&p->pdev->dev);
642 p->irqaction.handler = sh_cmt_interrupt;
643 p->irqaction.dev_id = p;
644 p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
645 IRQF_IRQPOLL | IRQF_NOBALANCING;
647 /* get hold of clock */
648 p->clk = clk_get(&p->pdev->dev, "cmt_fck");
649 if (IS_ERR(p->clk)) {
650 dev_err(&p->pdev->dev, "cannot get clock\n");
651 ret = PTR_ERR(p->clk);
652 goto err1;
655 if (resource_size(res) == 6) {
656 p->width = 16;
657 p->overflow_bit = 0x80;
658 p->clear_bits = ~0x80;
659 } else {
660 p->width = 32;
661 p->overflow_bit = 0x8000;
662 p->clear_bits = ~0xc000;
665 ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev),
666 cfg->clockevent_rating,
667 cfg->clocksource_rating);
668 if (ret) {
669 dev_err(&p->pdev->dev, "registration failed\n");
670 goto err1;
673 ret = setup_irq(irq, &p->irqaction);
674 if (ret) {
675 dev_err(&p->pdev->dev, "failed to request irq %d\n", irq);
676 goto err1;
679 return 0;
681 err1:
682 iounmap(p->mapbase);
683 err0:
684 return ret;
687 static int __devinit sh_cmt_probe(struct platform_device *pdev)
689 struct sh_cmt_priv *p = platform_get_drvdata(pdev);
690 int ret;
692 if (p) {
693 dev_info(&pdev->dev, "kept as earlytimer\n");
694 return 0;
697 p = kmalloc(sizeof(*p), GFP_KERNEL);
698 if (p == NULL) {
699 dev_err(&pdev->dev, "failed to allocate driver data\n");
700 return -ENOMEM;
703 ret = sh_cmt_setup(p, pdev);
704 if (ret) {
705 kfree(p);
706 platform_set_drvdata(pdev, NULL);
708 return ret;
711 static int __devexit sh_cmt_remove(struct platform_device *pdev)
713 return -EBUSY; /* cannot unregister clockevent and clocksource */
716 static struct platform_driver sh_cmt_device_driver = {
717 .probe = sh_cmt_probe,
718 .remove = __devexit_p(sh_cmt_remove),
719 .driver = {
720 .name = "sh_cmt",
724 static int __init sh_cmt_init(void)
726 return platform_driver_register(&sh_cmt_device_driver);
729 static void __exit sh_cmt_exit(void)
731 platform_driver_unregister(&sh_cmt_device_driver);
734 early_platform_init("earlytimer", &sh_cmt_device_driver);
735 module_init(sh_cmt_init);
736 module_exit(sh_cmt_exit);
738 MODULE_AUTHOR("Magnus Damm");
739 MODULE_DESCRIPTION("SuperH CMT Timer Driver");
740 MODULE_LICENSE("GPL v2");