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.
16 #include <linux/clk.h>
17 #include <linux/clockchips.h>
18 #include <linux/clocksource.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/irq.h>
26 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/pm_domain.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/sh_timer.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
38 * The CMT comes in 5 different identified flavours, depending not only on the
39 * SoC but also on the particular instance. The following table lists the main
40 * characteristics of those flavours.
42 * 16B 32B 32B-F 48B R-Car Gen2
43 * -----------------------------------------------------------------------------
44 * Channels 2 1/4 1 6 2/8
45 * Control Width 16 16 16 16 32
46 * Counter Width 16 32 32 32/48 32/48
47 * Shared Start/Stop Y Y Y Y N
49 * The r8a73a4 / R-Car Gen2 version has a per-channel start/stop register
50 * located in the channel registers block. All other versions have a shared
51 * start/stop register located in the global space.
53 * Channels are indexed from 0 to N-1 in the documentation. The channel index
54 * infers the start/stop bit position in the control register and the channel
55 * registers block address. Some CMT instances have a subset of channels
56 * available, in which case the index in the documentation doesn't match the
57 * "real" index as implemented in hardware. This is for instance the case with
58 * CMT0 on r8a7740, which is a 32-bit variant with a single channel numbered 0
59 * in the documentation but using start/stop bit 5 and having its registers
62 * Similarly CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
63 * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
76 enum sh_cmt_model model;
78 unsigned int channels_mask;
80 unsigned long width; /* 16 or 32 bit version of hardware block */
81 unsigned long overflow_bit;
82 unsigned long clear_bits;
84 /* callbacks for CMSTR and CMCSR access */
85 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
86 void (*write_control)(void __iomem *base, unsigned long offs,
89 /* callbacks for CMCNT and CMCOR access */
90 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
91 void (*write_count)(void __iomem *base, unsigned long offs,
95 struct sh_cmt_channel {
96 struct sh_cmt_device *cmt;
98 unsigned int index; /* Index in the documentation */
99 unsigned int hwidx; /* Real hardware index */
101 void __iomem *iostart;
102 void __iomem *ioctrl;
104 unsigned int timer_bit;
106 unsigned long match_value;
107 unsigned long next_match_value;
108 unsigned long max_match_value;
110 struct clock_event_device ced;
111 struct clocksource cs;
112 unsigned long total_cycles;
116 struct sh_cmt_device {
117 struct platform_device *pdev;
119 const struct sh_cmt_info *info;
121 void __iomem *mapbase;
125 raw_spinlock_t lock; /* Protect the shared start/stop register */
127 struct sh_cmt_channel *channels;
128 unsigned int num_channels;
129 unsigned int hw_channels;
132 bool has_clocksource;
135 #define SH_CMT16_CMCSR_CMF (1 << 7)
136 #define SH_CMT16_CMCSR_CMIE (1 << 6)
137 #define SH_CMT16_CMCSR_CKS8 (0 << 0)
138 #define SH_CMT16_CMCSR_CKS32 (1 << 0)
139 #define SH_CMT16_CMCSR_CKS128 (2 << 0)
140 #define SH_CMT16_CMCSR_CKS512 (3 << 0)
141 #define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
143 #define SH_CMT32_CMCSR_CMF (1 << 15)
144 #define SH_CMT32_CMCSR_OVF (1 << 14)
145 #define SH_CMT32_CMCSR_WRFLG (1 << 13)
146 #define SH_CMT32_CMCSR_STTF (1 << 12)
147 #define SH_CMT32_CMCSR_STPF (1 << 11)
148 #define SH_CMT32_CMCSR_SSIE (1 << 10)
149 #define SH_CMT32_CMCSR_CMS (1 << 9)
150 #define SH_CMT32_CMCSR_CMM (1 << 8)
151 #define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
152 #define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
153 #define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
154 #define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
155 #define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
156 #define SH_CMT32_CMCSR_DBGIVD (1 << 3)
157 #define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
158 #define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
159 #define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
160 #define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
161 #define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
163 static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
165 return ioread16(base + (offs << 1));
168 static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
170 return ioread32(base + (offs << 2));
173 static void sh_cmt_write16(void __iomem *base, unsigned long offs,
176 iowrite16(value, base + (offs << 1));
179 static void sh_cmt_write32(void __iomem *base, unsigned long offs,
182 iowrite32(value, base + (offs << 2));
185 static const struct sh_cmt_info sh_cmt_info[] = {
187 .model = SH_CMT_16BIT,
189 .overflow_bit = SH_CMT16_CMCSR_CMF,
190 .clear_bits = ~SH_CMT16_CMCSR_CMF,
191 .read_control = sh_cmt_read16,
192 .write_control = sh_cmt_write16,
193 .read_count = sh_cmt_read16,
194 .write_count = sh_cmt_write16,
197 .model = SH_CMT_32BIT,
199 .overflow_bit = SH_CMT32_CMCSR_CMF,
200 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
201 .read_control = sh_cmt_read16,
202 .write_control = sh_cmt_write16,
203 .read_count = sh_cmt_read32,
204 .write_count = sh_cmt_write32,
206 [SH_CMT_32BIT_FAST] = {
207 .model = SH_CMT_32BIT_FAST,
209 .overflow_bit = SH_CMT32_CMCSR_CMF,
210 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
211 .read_control = sh_cmt_read16,
212 .write_control = sh_cmt_write16,
213 .read_count = sh_cmt_read32,
214 .write_count = sh_cmt_write32,
217 .model = SH_CMT_48BIT,
218 .channels_mask = 0x3f,
220 .overflow_bit = SH_CMT32_CMCSR_CMF,
221 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
222 .read_control = sh_cmt_read32,
223 .write_control = sh_cmt_write32,
224 .read_count = sh_cmt_read32,
225 .write_count = sh_cmt_write32,
227 [SH_CMT0_RCAR_GEN2] = {
228 .model = SH_CMT0_RCAR_GEN2,
229 .channels_mask = 0x60,
231 .overflow_bit = SH_CMT32_CMCSR_CMF,
232 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
233 .read_control = sh_cmt_read32,
234 .write_control = sh_cmt_write32,
235 .read_count = sh_cmt_read32,
236 .write_count = sh_cmt_write32,
238 [SH_CMT1_RCAR_GEN2] = {
239 .model = SH_CMT1_RCAR_GEN2,
240 .channels_mask = 0xff,
242 .overflow_bit = SH_CMT32_CMCSR_CMF,
243 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
244 .read_control = sh_cmt_read32,
245 .write_control = sh_cmt_write32,
246 .read_count = sh_cmt_read32,
247 .write_count = sh_cmt_write32,
251 #define CMCSR 0 /* channel register */
252 #define CMCNT 1 /* channel register */
253 #define CMCOR 2 /* channel register */
255 static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
258 return ch->cmt->info->read_control(ch->iostart, 0);
260 return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
263 static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
267 ch->cmt->info->write_control(ch->iostart, 0, value);
269 ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
272 static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
274 return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
277 static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
280 ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
283 static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
285 return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
288 static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
291 ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
294 static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
297 ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
300 static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
303 unsigned long v1, v2, v3;
306 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
308 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
311 v1 = sh_cmt_read_cmcnt(ch);
312 v2 = sh_cmt_read_cmcnt(ch);
313 v3 = sh_cmt_read_cmcnt(ch);
314 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
315 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
316 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
322 static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
324 unsigned long flags, value;
326 /* start stop register shared by multiple timer channels */
327 raw_spin_lock_irqsave(&ch->cmt->lock, flags);
328 value = sh_cmt_read_cmstr(ch);
331 value |= 1 << ch->timer_bit;
333 value &= ~(1 << ch->timer_bit);
335 sh_cmt_write_cmstr(ch, value);
336 raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
339 static int sh_cmt_enable(struct sh_cmt_channel *ch)
343 pm_runtime_get_sync(&ch->cmt->pdev->dev);
344 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
347 ret = clk_enable(ch->cmt->clk);
349 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
354 /* make sure channel is disabled */
355 sh_cmt_start_stop_ch(ch, 0);
357 /* configure channel, periodic mode and maximum timeout */
358 if (ch->cmt->info->width == 16) {
359 sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
360 SH_CMT16_CMCSR_CKS512);
362 sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
363 SH_CMT32_CMCSR_CMTOUT_IE |
364 SH_CMT32_CMCSR_CMR_IRQ |
365 SH_CMT32_CMCSR_CKS_RCLK8);
368 sh_cmt_write_cmcor(ch, 0xffffffff);
369 sh_cmt_write_cmcnt(ch, 0);
372 * According to the sh73a0 user's manual, as CMCNT can be operated
373 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
374 * modifying CMCNT register; two RCLK cycles are necessary before
375 * this register is either read or any modification of the value
376 * it holds is reflected in the LSI's actual operation.
378 * While at it, we're supposed to clear out the CMCNT as of this
379 * moment, so make sure it's processed properly here. This will
380 * take RCLKx2 at maximum.
382 for (k = 0; k < 100; k++) {
383 if (!sh_cmt_read_cmcnt(ch))
388 if (sh_cmt_read_cmcnt(ch)) {
389 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
396 sh_cmt_start_stop_ch(ch, 1);
400 clk_disable(ch->cmt->clk);
406 static void sh_cmt_disable(struct sh_cmt_channel *ch)
408 /* disable channel */
409 sh_cmt_start_stop_ch(ch, 0);
411 /* disable interrupts in CMT block */
412 sh_cmt_write_cmcsr(ch, 0);
415 clk_disable(ch->cmt->clk);
417 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
418 pm_runtime_put(&ch->cmt->pdev->dev);
422 #define FLAG_CLOCKEVENT (1 << 0)
423 #define FLAG_CLOCKSOURCE (1 << 1)
424 #define FLAG_REPROGRAM (1 << 2)
425 #define FLAG_SKIPEVENT (1 << 3)
426 #define FLAG_IRQCONTEXT (1 << 4)
428 static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
431 unsigned long new_match;
432 unsigned long value = ch->next_match_value;
433 unsigned long delay = 0;
434 unsigned long now = 0;
437 now = sh_cmt_get_counter(ch, &has_wrapped);
438 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
441 /* we're competing with the interrupt handler.
442 * -> let the interrupt handler reprogram the timer.
443 * -> interrupt number two handles the event.
445 ch->flags |= FLAG_SKIPEVENT;
453 /* reprogram the timer hardware,
454 * but don't save the new match value yet.
456 new_match = now + value + delay;
457 if (new_match > ch->max_match_value)
458 new_match = ch->max_match_value;
460 sh_cmt_write_cmcor(ch, new_match);
462 now = sh_cmt_get_counter(ch, &has_wrapped);
463 if (has_wrapped && (new_match > ch->match_value)) {
464 /* we are changing to a greater match value,
465 * so this wrap must be caused by the counter
466 * matching the old value.
467 * -> first interrupt reprograms the timer.
468 * -> interrupt number two handles the event.
470 ch->flags |= FLAG_SKIPEVENT;
475 /* we are changing to a smaller match value,
476 * so the wrap must be caused by the counter
477 * matching the new value.
478 * -> save programmed match value.
479 * -> let isr handle the event.
481 ch->match_value = new_match;
485 /* be safe: verify hardware settings */
486 if (now < new_match) {
487 /* timer value is below match value, all good.
488 * this makes sure we won't miss any match events.
489 * -> save programmed match value.
490 * -> let isr handle the event.
492 ch->match_value = new_match;
496 /* the counter has reached a value greater
497 * than our new match value. and since the
498 * has_wrapped flag isn't set we must have
499 * programmed a too close event.
500 * -> increase delay and retry.
508 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
514 static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
516 if (delta > ch->max_match_value)
517 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
520 ch->next_match_value = delta;
521 sh_cmt_clock_event_program_verify(ch, 0);
524 static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
528 raw_spin_lock_irqsave(&ch->lock, flags);
529 __sh_cmt_set_next(ch, delta);
530 raw_spin_unlock_irqrestore(&ch->lock, flags);
533 static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
535 struct sh_cmt_channel *ch = dev_id;
538 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
539 ch->cmt->info->clear_bits);
541 /* update clock source counter to begin with if enabled
542 * the wrap flag should be cleared by the timer specific
543 * isr before we end up here.
545 if (ch->flags & FLAG_CLOCKSOURCE)
546 ch->total_cycles += ch->match_value + 1;
548 if (!(ch->flags & FLAG_REPROGRAM))
549 ch->next_match_value = ch->max_match_value;
551 ch->flags |= FLAG_IRQCONTEXT;
553 if (ch->flags & FLAG_CLOCKEVENT) {
554 if (!(ch->flags & FLAG_SKIPEVENT)) {
555 if (clockevent_state_oneshot(&ch->ced)) {
556 ch->next_match_value = ch->max_match_value;
557 ch->flags |= FLAG_REPROGRAM;
560 ch->ced.event_handler(&ch->ced);
564 ch->flags &= ~FLAG_SKIPEVENT;
566 if (ch->flags & FLAG_REPROGRAM) {
567 ch->flags &= ~FLAG_REPROGRAM;
568 sh_cmt_clock_event_program_verify(ch, 1);
570 if (ch->flags & FLAG_CLOCKEVENT)
571 if ((clockevent_state_shutdown(&ch->ced))
572 || (ch->match_value == ch->next_match_value))
573 ch->flags &= ~FLAG_REPROGRAM;
576 ch->flags &= ~FLAG_IRQCONTEXT;
581 static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
586 raw_spin_lock_irqsave(&ch->lock, flags);
588 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
589 ret = sh_cmt_enable(ch);
595 /* setup timeout if no clockevent */
596 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
597 __sh_cmt_set_next(ch, ch->max_match_value);
599 raw_spin_unlock_irqrestore(&ch->lock, flags);
604 static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
609 raw_spin_lock_irqsave(&ch->lock, flags);
611 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
614 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
617 /* adjust the timeout to maximum if only clocksource left */
618 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
619 __sh_cmt_set_next(ch, ch->max_match_value);
621 raw_spin_unlock_irqrestore(&ch->lock, flags);
624 static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
626 return container_of(cs, struct sh_cmt_channel, cs);
629 static u64 sh_cmt_clocksource_read(struct clocksource *cs)
631 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
632 unsigned long flags, raw;
636 raw_spin_lock_irqsave(&ch->lock, flags);
637 value = ch->total_cycles;
638 raw = sh_cmt_get_counter(ch, &has_wrapped);
640 if (unlikely(has_wrapped))
641 raw += ch->match_value + 1;
642 raw_spin_unlock_irqrestore(&ch->lock, flags);
647 static int sh_cmt_clocksource_enable(struct clocksource *cs)
650 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
652 WARN_ON(ch->cs_enabled);
654 ch->total_cycles = 0;
656 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
658 ch->cs_enabled = true;
663 static void sh_cmt_clocksource_disable(struct clocksource *cs)
665 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
667 WARN_ON(!ch->cs_enabled);
669 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
670 ch->cs_enabled = false;
673 static void sh_cmt_clocksource_suspend(struct clocksource *cs)
675 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
680 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
681 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
684 static void sh_cmt_clocksource_resume(struct clocksource *cs)
686 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
691 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
692 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
695 static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
698 struct clocksource *cs = &ch->cs;
702 cs->read = sh_cmt_clocksource_read;
703 cs->enable = sh_cmt_clocksource_enable;
704 cs->disable = sh_cmt_clocksource_disable;
705 cs->suspend = sh_cmt_clocksource_suspend;
706 cs->resume = sh_cmt_clocksource_resume;
707 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
708 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
710 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
713 clocksource_register_hz(cs, ch->cmt->rate);
717 static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
719 return container_of(ced, struct sh_cmt_channel, ced);
722 static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
724 sh_cmt_start(ch, FLAG_CLOCKEVENT);
727 sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
729 sh_cmt_set_next(ch, ch->max_match_value);
732 static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
734 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
736 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
740 static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
743 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
745 /* deal with old setting first */
746 if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
747 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
749 dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
750 ch->index, periodic ? "periodic" : "oneshot");
751 sh_cmt_clock_event_start(ch, periodic);
755 static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
757 return sh_cmt_clock_event_set_state(ced, 0);
760 static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
762 return sh_cmt_clock_event_set_state(ced, 1);
765 static int sh_cmt_clock_event_next(unsigned long delta,
766 struct clock_event_device *ced)
768 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
770 BUG_ON(!clockevent_state_oneshot(ced));
771 if (likely(ch->flags & FLAG_IRQCONTEXT))
772 ch->next_match_value = delta - 1;
774 sh_cmt_set_next(ch, delta - 1);
779 static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
781 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
783 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
784 clk_unprepare(ch->cmt->clk);
787 static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
789 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
791 clk_prepare(ch->cmt->clk);
792 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
795 static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
798 struct clock_event_device *ced = &ch->ced;
802 irq = platform_get_irq(ch->cmt->pdev, ch->index);
804 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
809 ret = request_irq(irq, sh_cmt_interrupt,
810 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
811 dev_name(&ch->cmt->pdev->dev), ch);
813 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
819 ced->features = CLOCK_EVT_FEAT_PERIODIC;
820 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
822 ced->cpumask = cpu_possible_mask;
823 ced->set_next_event = sh_cmt_clock_event_next;
824 ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
825 ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
826 ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
827 ced->suspend = sh_cmt_clock_event_suspend;
828 ced->resume = sh_cmt_clock_event_resume;
830 /* TODO: calculate good shift from rate and counter bit width */
832 ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
833 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
834 ced->max_delta_ticks = ch->max_match_value;
835 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
836 ced->min_delta_ticks = 0x1f;
838 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
840 clockevents_register_device(ced);
845 static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
846 bool clockevent, bool clocksource)
851 ch->cmt->has_clockevent = true;
852 ret = sh_cmt_register_clockevent(ch, name);
858 ch->cmt->has_clocksource = true;
859 sh_cmt_register_clocksource(ch, name);
865 static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
866 unsigned int hwidx, bool clockevent,
867 bool clocksource, struct sh_cmt_device *cmt)
871 /* Skip unused channels. */
872 if (!clockevent && !clocksource)
878 ch->timer_bit = hwidx;
881 * Compute the address of the channel control register block. For the
882 * timers with a per-channel start/stop register, compute its address
885 switch (cmt->info->model) {
887 ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
891 ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
893 case SH_CMT_32BIT_FAST:
895 * The 32-bit "fast" timer has a single channel at hwidx 5 but
896 * is located at offset 0x40 instead of 0x60 for some reason.
898 ch->ioctrl = cmt->mapbase + 0x40;
900 case SH_CMT0_RCAR_GEN2:
901 case SH_CMT1_RCAR_GEN2:
902 ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
903 ch->ioctrl = ch->iostart + 0x10;
908 if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
909 ch->max_match_value = ~0;
911 ch->max_match_value = (1 << cmt->info->width) - 1;
913 ch->match_value = ch->max_match_value;
914 raw_spin_lock_init(&ch->lock);
916 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
917 clockevent, clocksource);
919 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
923 ch->cs_enabled = false;
928 static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
930 struct resource *mem;
932 mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
934 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
938 cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
939 if (cmt->mapbase == NULL) {
940 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
947 static const struct platform_device_id sh_cmt_id_table[] = {
948 { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
949 { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
952 MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
954 static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
955 { .compatible = "renesas,cmt-32", .data = &sh_cmt_info[SH_CMT_32BIT] },
956 { .compatible = "renesas,cmt-32-fast", .data = &sh_cmt_info[SH_CMT_32BIT_FAST] },
957 { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
958 { .compatible = "renesas,cmt-48-gen2", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
959 { .compatible = "renesas,rcar-gen2-cmt0", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
960 { .compatible = "renesas,rcar-gen2-cmt1", .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2] },
963 MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
965 static int sh_cmt_parse_dt(struct sh_cmt_device *cmt)
967 struct device_node *np = cmt->pdev->dev.of_node;
969 return of_property_read_u32(np, "renesas,channels-mask",
973 static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
980 raw_spin_lock_init(&cmt->lock);
982 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
983 const struct of_device_id *id;
985 id = of_match_node(sh_cmt_of_table, pdev->dev.of_node);
986 cmt->info = id->data;
988 /* prefer in-driver channel configuration over DT */
989 if (cmt->info->channels_mask) {
990 cmt->hw_channels = cmt->info->channels_mask;
992 ret = sh_cmt_parse_dt(cmt);
996 } else if (pdev->dev.platform_data) {
997 struct sh_timer_config *cfg = pdev->dev.platform_data;
998 const struct platform_device_id *id = pdev->id_entry;
1000 cmt->info = (const struct sh_cmt_info *)id->driver_data;
1001 cmt->hw_channels = cfg->channels_mask;
1003 dev_err(&cmt->pdev->dev, "missing platform data\n");
1007 /* Get hold of clock. */
1008 cmt->clk = clk_get(&cmt->pdev->dev, "fck");
1009 if (IS_ERR(cmt->clk)) {
1010 dev_err(&cmt->pdev->dev, "cannot get clock\n");
1011 return PTR_ERR(cmt->clk);
1014 ret = clk_prepare(cmt->clk);
1018 /* Determine clock rate. */
1019 ret = clk_enable(cmt->clk);
1021 goto err_clk_unprepare;
1023 if (cmt->info->width == 16)
1024 cmt->rate = clk_get_rate(cmt->clk) / 512;
1026 cmt->rate = clk_get_rate(cmt->clk) / 8;
1028 clk_disable(cmt->clk);
1030 /* Map the memory resource(s). */
1031 ret = sh_cmt_map_memory(cmt);
1033 goto err_clk_unprepare;
1035 /* Allocate and setup the channels. */
1036 cmt->num_channels = hweight8(cmt->hw_channels);
1037 cmt->channels = kzalloc(cmt->num_channels * sizeof(*cmt->channels),
1039 if (cmt->channels == NULL) {
1045 * Use the first channel as a clock event device and the second channel
1046 * as a clock source. If only one channel is available use it for both.
1048 for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
1049 unsigned int hwidx = ffs(mask) - 1;
1050 bool clocksource = i == 1 || cmt->num_channels == 1;
1051 bool clockevent = i == 0;
1053 ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
1054 clockevent, clocksource, cmt);
1058 mask &= ~(1 << hwidx);
1061 platform_set_drvdata(pdev, cmt);
1066 kfree(cmt->channels);
1067 iounmap(cmt->mapbase);
1069 clk_unprepare(cmt->clk);
1075 static int sh_cmt_probe(struct platform_device *pdev)
1077 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
1080 if (!is_early_platform_device(pdev)) {
1081 pm_runtime_set_active(&pdev->dev);
1082 pm_runtime_enable(&pdev->dev);
1086 dev_info(&pdev->dev, "kept as earlytimer\n");
1090 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
1094 ret = sh_cmt_setup(cmt, pdev);
1097 pm_runtime_idle(&pdev->dev);
1100 if (is_early_platform_device(pdev))
1104 if (cmt->has_clockevent || cmt->has_clocksource)
1105 pm_runtime_irq_safe(&pdev->dev);
1107 pm_runtime_idle(&pdev->dev);
1112 static int sh_cmt_remove(struct platform_device *pdev)
1114 return -EBUSY; /* cannot unregister clockevent and clocksource */
1117 static struct platform_driver sh_cmt_device_driver = {
1118 .probe = sh_cmt_probe,
1119 .remove = sh_cmt_remove,
1122 .of_match_table = of_match_ptr(sh_cmt_of_table),
1124 .id_table = sh_cmt_id_table,
1127 static int __init sh_cmt_init(void)
1129 return platform_driver_register(&sh_cmt_device_driver);
1132 static void __exit sh_cmt_exit(void)
1134 platform_driver_unregister(&sh_cmt_device_driver);
1137 early_platform_init("earlytimer", &sh_cmt_device_driver);
1138 subsys_initcall(sh_cmt_init);
1139 module_exit(sh_cmt_exit);
1141 MODULE_AUTHOR("Magnus Damm");
1142 MODULE_DESCRIPTION("SuperH CMT Timer Driver");
1143 MODULE_LICENSE("GPL v2");