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/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_domain.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/sh_timer.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
39 * The CMT comes in 5 different identified flavours, depending not only on the
40 * SoC but also on the particular instance. The following table lists the main
41 * characteristics of those flavours.
43 * 16B 32B 32B-F 48B R-Car Gen2
44 * -----------------------------------------------------------------------------
45 * Channels 2 1/4 1 6 2/8
46 * Control Width 16 16 16 16 32
47 * Counter Width 16 32 32 32/48 32/48
48 * Shared Start/Stop Y Y Y Y N
50 * The r8a73a4 / R-Car Gen2 version has a per-channel start/stop register
51 * located in the channel registers block. All other versions have a shared
52 * start/stop register located in the global space.
54 * Channels are indexed from 0 to N-1 in the documentation. The channel index
55 * infers the start/stop bit position in the control register and the channel
56 * registers block address. Some CMT instances have a subset of channels
57 * available, in which case the index in the documentation doesn't match the
58 * "real" index as implemented in hardware. This is for instance the case with
59 * CMT0 on r8a7740, which is a 32-bit variant with a single channel numbered 0
60 * in the documentation but using start/stop bit 5 and having its registers
63 * Similarly CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
64 * 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
,
207 .model
= SH_CMT_48BIT
,
208 .channels_mask
= 0x3f,
210 .overflow_bit
= SH_CMT32_CMCSR_CMF
,
211 .clear_bits
= ~(SH_CMT32_CMCSR_CMF
| SH_CMT32_CMCSR_OVF
),
212 .read_control
= sh_cmt_read32
,
213 .write_control
= sh_cmt_write32
,
214 .read_count
= sh_cmt_read32
,
215 .write_count
= sh_cmt_write32
,
217 [SH_CMT0_RCAR_GEN2
] = {
218 .model
= SH_CMT0_RCAR_GEN2
,
219 .channels_mask
= 0x60,
221 .overflow_bit
= SH_CMT32_CMCSR_CMF
,
222 .clear_bits
= ~(SH_CMT32_CMCSR_CMF
| SH_CMT32_CMCSR_OVF
),
223 .read_control
= sh_cmt_read32
,
224 .write_control
= sh_cmt_write32
,
225 .read_count
= sh_cmt_read32
,
226 .write_count
= sh_cmt_write32
,
228 [SH_CMT1_RCAR_GEN2
] = {
229 .model
= SH_CMT1_RCAR_GEN2
,
230 .channels_mask
= 0xff,
232 .overflow_bit
= SH_CMT32_CMCSR_CMF
,
233 .clear_bits
= ~(SH_CMT32_CMCSR_CMF
| SH_CMT32_CMCSR_OVF
),
234 .read_control
= sh_cmt_read32
,
235 .write_control
= sh_cmt_write32
,
236 .read_count
= sh_cmt_read32
,
237 .write_count
= sh_cmt_write32
,
241 #define CMCSR 0 /* channel register */
242 #define CMCNT 1 /* channel register */
243 #define CMCOR 2 /* channel register */
245 static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel
*ch
)
248 return ch
->cmt
->info
->read_control(ch
->iostart
, 0);
250 return ch
->cmt
->info
->read_control(ch
->cmt
->mapbase
, 0);
253 static inline void sh_cmt_write_cmstr(struct sh_cmt_channel
*ch
,
257 ch
->cmt
->info
->write_control(ch
->iostart
, 0, value
);
259 ch
->cmt
->info
->write_control(ch
->cmt
->mapbase
, 0, value
);
262 static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel
*ch
)
264 return ch
->cmt
->info
->read_control(ch
->ioctrl
, CMCSR
);
267 static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel
*ch
,
270 ch
->cmt
->info
->write_control(ch
->ioctrl
, CMCSR
, value
);
273 static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel
*ch
)
275 return ch
->cmt
->info
->read_count(ch
->ioctrl
, CMCNT
);
278 static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel
*ch
,
281 ch
->cmt
->info
->write_count(ch
->ioctrl
, CMCNT
, value
);
284 static inline void sh_cmt_write_cmcor(struct sh_cmt_channel
*ch
,
287 ch
->cmt
->info
->write_count(ch
->ioctrl
, CMCOR
, value
);
290 static unsigned long sh_cmt_get_counter(struct sh_cmt_channel
*ch
,
293 unsigned long v1
, v2
, v3
;
296 o1
= sh_cmt_read_cmcsr(ch
) & ch
->cmt
->info
->overflow_bit
;
298 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
301 v1
= sh_cmt_read_cmcnt(ch
);
302 v2
= sh_cmt_read_cmcnt(ch
);
303 v3
= sh_cmt_read_cmcnt(ch
);
304 o1
= sh_cmt_read_cmcsr(ch
) & ch
->cmt
->info
->overflow_bit
;
305 } while (unlikely((o1
!= o2
) || (v1
> v2
&& v1
< v3
)
306 || (v2
> v3
&& v2
< v1
) || (v3
> v1
&& v3
< v2
)));
312 static void sh_cmt_start_stop_ch(struct sh_cmt_channel
*ch
, int start
)
314 unsigned long flags
, value
;
316 /* start stop register shared by multiple timer channels */
317 raw_spin_lock_irqsave(&ch
->cmt
->lock
, flags
);
318 value
= sh_cmt_read_cmstr(ch
);
321 value
|= 1 << ch
->timer_bit
;
323 value
&= ~(1 << ch
->timer_bit
);
325 sh_cmt_write_cmstr(ch
, value
);
326 raw_spin_unlock_irqrestore(&ch
->cmt
->lock
, flags
);
329 static int sh_cmt_enable(struct sh_cmt_channel
*ch
)
333 pm_runtime_get_sync(&ch
->cmt
->pdev
->dev
);
334 dev_pm_syscore_device(&ch
->cmt
->pdev
->dev
, true);
337 ret
= clk_enable(ch
->cmt
->clk
);
339 dev_err(&ch
->cmt
->pdev
->dev
, "ch%u: cannot enable clock\n",
344 /* make sure channel is disabled */
345 sh_cmt_start_stop_ch(ch
, 0);
347 /* configure channel, periodic mode and maximum timeout */
348 if (ch
->cmt
->info
->width
== 16) {
349 sh_cmt_write_cmcsr(ch
, SH_CMT16_CMCSR_CMIE
|
350 SH_CMT16_CMCSR_CKS512
);
352 sh_cmt_write_cmcsr(ch
, SH_CMT32_CMCSR_CMM
|
353 SH_CMT32_CMCSR_CMTOUT_IE
|
354 SH_CMT32_CMCSR_CMR_IRQ
|
355 SH_CMT32_CMCSR_CKS_RCLK8
);
358 sh_cmt_write_cmcor(ch
, 0xffffffff);
359 sh_cmt_write_cmcnt(ch
, 0);
362 * According to the sh73a0 user's manual, as CMCNT can be operated
363 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
364 * modifying CMCNT register; two RCLK cycles are necessary before
365 * this register is either read or any modification of the value
366 * it holds is reflected in the LSI's actual operation.
368 * While at it, we're supposed to clear out the CMCNT as of this
369 * moment, so make sure it's processed properly here. This will
370 * take RCLKx2 at maximum.
372 for (k
= 0; k
< 100; k
++) {
373 if (!sh_cmt_read_cmcnt(ch
))
378 if (sh_cmt_read_cmcnt(ch
)) {
379 dev_err(&ch
->cmt
->pdev
->dev
, "ch%u: cannot clear CMCNT\n",
386 sh_cmt_start_stop_ch(ch
, 1);
390 clk_disable(ch
->cmt
->clk
);
396 static void sh_cmt_disable(struct sh_cmt_channel
*ch
)
398 /* disable channel */
399 sh_cmt_start_stop_ch(ch
, 0);
401 /* disable interrupts in CMT block */
402 sh_cmt_write_cmcsr(ch
, 0);
405 clk_disable(ch
->cmt
->clk
);
407 dev_pm_syscore_device(&ch
->cmt
->pdev
->dev
, false);
408 pm_runtime_put(&ch
->cmt
->pdev
->dev
);
412 #define FLAG_CLOCKEVENT (1 << 0)
413 #define FLAG_CLOCKSOURCE (1 << 1)
414 #define FLAG_REPROGRAM (1 << 2)
415 #define FLAG_SKIPEVENT (1 << 3)
416 #define FLAG_IRQCONTEXT (1 << 4)
418 static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel
*ch
,
421 unsigned long new_match
;
422 unsigned long value
= ch
->next_match_value
;
423 unsigned long delay
= 0;
424 unsigned long now
= 0;
427 now
= sh_cmt_get_counter(ch
, &has_wrapped
);
428 ch
->flags
|= FLAG_REPROGRAM
; /* force reprogram */
431 /* we're competing with the interrupt handler.
432 * -> let the interrupt handler reprogram the timer.
433 * -> interrupt number two handles the event.
435 ch
->flags
|= FLAG_SKIPEVENT
;
443 /* reprogram the timer hardware,
444 * but don't save the new match value yet.
446 new_match
= now
+ value
+ delay
;
447 if (new_match
> ch
->max_match_value
)
448 new_match
= ch
->max_match_value
;
450 sh_cmt_write_cmcor(ch
, new_match
);
452 now
= sh_cmt_get_counter(ch
, &has_wrapped
);
453 if (has_wrapped
&& (new_match
> ch
->match_value
)) {
454 /* we are changing to a greater match value,
455 * so this wrap must be caused by the counter
456 * matching the old value.
457 * -> first interrupt reprograms the timer.
458 * -> interrupt number two handles the event.
460 ch
->flags
|= FLAG_SKIPEVENT
;
465 /* we are changing to a smaller match value,
466 * so the wrap must be caused by the counter
467 * matching the new value.
468 * -> save programmed match value.
469 * -> let isr handle the event.
471 ch
->match_value
= new_match
;
475 /* be safe: verify hardware settings */
476 if (now
< new_match
) {
477 /* timer value is below match value, all good.
478 * this makes sure we won't miss any match events.
479 * -> save programmed match value.
480 * -> let isr handle the event.
482 ch
->match_value
= new_match
;
486 /* the counter has reached a value greater
487 * than our new match value. and since the
488 * has_wrapped flag isn't set we must have
489 * programmed a too close event.
490 * -> increase delay and retry.
498 dev_warn(&ch
->cmt
->pdev
->dev
, "ch%u: too long delay\n",
504 static void __sh_cmt_set_next(struct sh_cmt_channel
*ch
, unsigned long delta
)
506 if (delta
> ch
->max_match_value
)
507 dev_warn(&ch
->cmt
->pdev
->dev
, "ch%u: delta out of range\n",
510 ch
->next_match_value
= delta
;
511 sh_cmt_clock_event_program_verify(ch
, 0);
514 static void sh_cmt_set_next(struct sh_cmt_channel
*ch
, unsigned long delta
)
518 raw_spin_lock_irqsave(&ch
->lock
, flags
);
519 __sh_cmt_set_next(ch
, delta
);
520 raw_spin_unlock_irqrestore(&ch
->lock
, flags
);
523 static irqreturn_t
sh_cmt_interrupt(int irq
, void *dev_id
)
525 struct sh_cmt_channel
*ch
= dev_id
;
528 sh_cmt_write_cmcsr(ch
, sh_cmt_read_cmcsr(ch
) &
529 ch
->cmt
->info
->clear_bits
);
531 /* update clock source counter to begin with if enabled
532 * the wrap flag should be cleared by the timer specific
533 * isr before we end up here.
535 if (ch
->flags
& FLAG_CLOCKSOURCE
)
536 ch
->total_cycles
+= ch
->match_value
+ 1;
538 if (!(ch
->flags
& FLAG_REPROGRAM
))
539 ch
->next_match_value
= ch
->max_match_value
;
541 ch
->flags
|= FLAG_IRQCONTEXT
;
543 if (ch
->flags
& FLAG_CLOCKEVENT
) {
544 if (!(ch
->flags
& FLAG_SKIPEVENT
)) {
545 if (clockevent_state_oneshot(&ch
->ced
)) {
546 ch
->next_match_value
= ch
->max_match_value
;
547 ch
->flags
|= FLAG_REPROGRAM
;
550 ch
->ced
.event_handler(&ch
->ced
);
554 ch
->flags
&= ~FLAG_SKIPEVENT
;
556 if (ch
->flags
& FLAG_REPROGRAM
) {
557 ch
->flags
&= ~FLAG_REPROGRAM
;
558 sh_cmt_clock_event_program_verify(ch
, 1);
560 if (ch
->flags
& FLAG_CLOCKEVENT
)
561 if ((clockevent_state_shutdown(&ch
->ced
))
562 || (ch
->match_value
== ch
->next_match_value
))
563 ch
->flags
&= ~FLAG_REPROGRAM
;
566 ch
->flags
&= ~FLAG_IRQCONTEXT
;
571 static int sh_cmt_start(struct sh_cmt_channel
*ch
, unsigned long flag
)
576 raw_spin_lock_irqsave(&ch
->lock
, flags
);
578 if (!(ch
->flags
& (FLAG_CLOCKEVENT
| FLAG_CLOCKSOURCE
)))
579 ret
= sh_cmt_enable(ch
);
585 /* setup timeout if no clockevent */
586 if ((flag
== FLAG_CLOCKSOURCE
) && (!(ch
->flags
& FLAG_CLOCKEVENT
)))
587 __sh_cmt_set_next(ch
, ch
->max_match_value
);
589 raw_spin_unlock_irqrestore(&ch
->lock
, flags
);
594 static void sh_cmt_stop(struct sh_cmt_channel
*ch
, unsigned long flag
)
599 raw_spin_lock_irqsave(&ch
->lock
, flags
);
601 f
= ch
->flags
& (FLAG_CLOCKEVENT
| FLAG_CLOCKSOURCE
);
604 if (f
&& !(ch
->flags
& (FLAG_CLOCKEVENT
| FLAG_CLOCKSOURCE
)))
607 /* adjust the timeout to maximum if only clocksource left */
608 if ((flag
== FLAG_CLOCKEVENT
) && (ch
->flags
& FLAG_CLOCKSOURCE
))
609 __sh_cmt_set_next(ch
, ch
->max_match_value
);
611 raw_spin_unlock_irqrestore(&ch
->lock
, flags
);
614 static struct sh_cmt_channel
*cs_to_sh_cmt(struct clocksource
*cs
)
616 return container_of(cs
, struct sh_cmt_channel
, cs
);
619 static u64
sh_cmt_clocksource_read(struct clocksource
*cs
)
621 struct sh_cmt_channel
*ch
= cs_to_sh_cmt(cs
);
622 unsigned long flags
, raw
;
626 raw_spin_lock_irqsave(&ch
->lock
, flags
);
627 value
= ch
->total_cycles
;
628 raw
= sh_cmt_get_counter(ch
, &has_wrapped
);
630 if (unlikely(has_wrapped
))
631 raw
+= ch
->match_value
+ 1;
632 raw_spin_unlock_irqrestore(&ch
->lock
, flags
);
637 static int sh_cmt_clocksource_enable(struct clocksource
*cs
)
640 struct sh_cmt_channel
*ch
= cs_to_sh_cmt(cs
);
642 WARN_ON(ch
->cs_enabled
);
644 ch
->total_cycles
= 0;
646 ret
= sh_cmt_start(ch
, FLAG_CLOCKSOURCE
);
648 ch
->cs_enabled
= true;
653 static void sh_cmt_clocksource_disable(struct clocksource
*cs
)
655 struct sh_cmt_channel
*ch
= cs_to_sh_cmt(cs
);
657 WARN_ON(!ch
->cs_enabled
);
659 sh_cmt_stop(ch
, FLAG_CLOCKSOURCE
);
660 ch
->cs_enabled
= false;
663 static void sh_cmt_clocksource_suspend(struct clocksource
*cs
)
665 struct sh_cmt_channel
*ch
= cs_to_sh_cmt(cs
);
670 sh_cmt_stop(ch
, FLAG_CLOCKSOURCE
);
671 pm_genpd_syscore_poweroff(&ch
->cmt
->pdev
->dev
);
674 static void sh_cmt_clocksource_resume(struct clocksource
*cs
)
676 struct sh_cmt_channel
*ch
= cs_to_sh_cmt(cs
);
681 pm_genpd_syscore_poweron(&ch
->cmt
->pdev
->dev
);
682 sh_cmt_start(ch
, FLAG_CLOCKSOURCE
);
685 static int sh_cmt_register_clocksource(struct sh_cmt_channel
*ch
,
688 struct clocksource
*cs
= &ch
->cs
;
692 cs
->read
= sh_cmt_clocksource_read
;
693 cs
->enable
= sh_cmt_clocksource_enable
;
694 cs
->disable
= sh_cmt_clocksource_disable
;
695 cs
->suspend
= sh_cmt_clocksource_suspend
;
696 cs
->resume
= sh_cmt_clocksource_resume
;
697 cs
->mask
= CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
698 cs
->flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
700 dev_info(&ch
->cmt
->pdev
->dev
, "ch%u: used as clock source\n",
703 clocksource_register_hz(cs
, ch
->cmt
->rate
);
707 static struct sh_cmt_channel
*ced_to_sh_cmt(struct clock_event_device
*ced
)
709 return container_of(ced
, struct sh_cmt_channel
, ced
);
712 static void sh_cmt_clock_event_start(struct sh_cmt_channel
*ch
, int periodic
)
714 sh_cmt_start(ch
, FLAG_CLOCKEVENT
);
717 sh_cmt_set_next(ch
, ((ch
->cmt
->rate
+ HZ
/2) / HZ
) - 1);
719 sh_cmt_set_next(ch
, ch
->max_match_value
);
722 static int sh_cmt_clock_event_shutdown(struct clock_event_device
*ced
)
724 struct sh_cmt_channel
*ch
= ced_to_sh_cmt(ced
);
726 sh_cmt_stop(ch
, FLAG_CLOCKEVENT
);
730 static int sh_cmt_clock_event_set_state(struct clock_event_device
*ced
,
733 struct sh_cmt_channel
*ch
= ced_to_sh_cmt(ced
);
735 /* deal with old setting first */
736 if (clockevent_state_oneshot(ced
) || clockevent_state_periodic(ced
))
737 sh_cmt_stop(ch
, FLAG_CLOCKEVENT
);
739 dev_info(&ch
->cmt
->pdev
->dev
, "ch%u: used for %s clock events\n",
740 ch
->index
, periodic
? "periodic" : "oneshot");
741 sh_cmt_clock_event_start(ch
, periodic
);
745 static int sh_cmt_clock_event_set_oneshot(struct clock_event_device
*ced
)
747 return sh_cmt_clock_event_set_state(ced
, 0);
750 static int sh_cmt_clock_event_set_periodic(struct clock_event_device
*ced
)
752 return sh_cmt_clock_event_set_state(ced
, 1);
755 static int sh_cmt_clock_event_next(unsigned long delta
,
756 struct clock_event_device
*ced
)
758 struct sh_cmt_channel
*ch
= ced_to_sh_cmt(ced
);
760 BUG_ON(!clockevent_state_oneshot(ced
));
761 if (likely(ch
->flags
& FLAG_IRQCONTEXT
))
762 ch
->next_match_value
= delta
- 1;
764 sh_cmt_set_next(ch
, delta
- 1);
769 static void sh_cmt_clock_event_suspend(struct clock_event_device
*ced
)
771 struct sh_cmt_channel
*ch
= ced_to_sh_cmt(ced
);
773 pm_genpd_syscore_poweroff(&ch
->cmt
->pdev
->dev
);
774 clk_unprepare(ch
->cmt
->clk
);
777 static void sh_cmt_clock_event_resume(struct clock_event_device
*ced
)
779 struct sh_cmt_channel
*ch
= ced_to_sh_cmt(ced
);
781 clk_prepare(ch
->cmt
->clk
);
782 pm_genpd_syscore_poweron(&ch
->cmt
->pdev
->dev
);
785 static int sh_cmt_register_clockevent(struct sh_cmt_channel
*ch
,
788 struct clock_event_device
*ced
= &ch
->ced
;
792 irq
= platform_get_irq(ch
->cmt
->pdev
, ch
->index
);
794 dev_err(&ch
->cmt
->pdev
->dev
, "ch%u: failed to get irq\n",
799 ret
= request_irq(irq
, sh_cmt_interrupt
,
800 IRQF_TIMER
| IRQF_IRQPOLL
| IRQF_NOBALANCING
,
801 dev_name(&ch
->cmt
->pdev
->dev
), ch
);
803 dev_err(&ch
->cmt
->pdev
->dev
, "ch%u: failed to request irq %d\n",
809 ced
->features
= CLOCK_EVT_FEAT_PERIODIC
;
810 ced
->features
|= CLOCK_EVT_FEAT_ONESHOT
;
812 ced
->cpumask
= cpu_possible_mask
;
813 ced
->set_next_event
= sh_cmt_clock_event_next
;
814 ced
->set_state_shutdown
= sh_cmt_clock_event_shutdown
;
815 ced
->set_state_periodic
= sh_cmt_clock_event_set_periodic
;
816 ced
->set_state_oneshot
= sh_cmt_clock_event_set_oneshot
;
817 ced
->suspend
= sh_cmt_clock_event_suspend
;
818 ced
->resume
= sh_cmt_clock_event_resume
;
820 /* TODO: calculate good shift from rate and counter bit width */
822 ced
->mult
= div_sc(ch
->cmt
->rate
, NSEC_PER_SEC
, ced
->shift
);
823 ced
->max_delta_ns
= clockevent_delta2ns(ch
->max_match_value
, ced
);
824 ced
->max_delta_ticks
= ch
->max_match_value
;
825 ced
->min_delta_ns
= clockevent_delta2ns(0x1f, ced
);
826 ced
->min_delta_ticks
= 0x1f;
828 dev_info(&ch
->cmt
->pdev
->dev
, "ch%u: used for clock events\n",
830 clockevents_register_device(ced
);
835 static int sh_cmt_register(struct sh_cmt_channel
*ch
, const char *name
,
836 bool clockevent
, bool clocksource
)
841 ch
->cmt
->has_clockevent
= true;
842 ret
= sh_cmt_register_clockevent(ch
, name
);
848 ch
->cmt
->has_clocksource
= true;
849 sh_cmt_register_clocksource(ch
, name
);
855 static int sh_cmt_setup_channel(struct sh_cmt_channel
*ch
, unsigned int index
,
856 unsigned int hwidx
, bool clockevent
,
857 bool clocksource
, struct sh_cmt_device
*cmt
)
861 /* Skip unused channels. */
862 if (!clockevent
&& !clocksource
)
868 ch
->timer_bit
= hwidx
;
871 * Compute the address of the channel control register block. For the
872 * timers with a per-channel start/stop register, compute its address
875 switch (cmt
->info
->model
) {
877 ch
->ioctrl
= cmt
->mapbase
+ 2 + ch
->hwidx
* 6;
881 ch
->ioctrl
= cmt
->mapbase
+ 0x10 + ch
->hwidx
* 0x10;
883 case SH_CMT0_RCAR_GEN2
:
884 case SH_CMT1_RCAR_GEN2
:
885 ch
->iostart
= cmt
->mapbase
+ ch
->hwidx
* 0x100;
886 ch
->ioctrl
= ch
->iostart
+ 0x10;
891 if (cmt
->info
->width
== (sizeof(ch
->max_match_value
) * 8))
892 ch
->max_match_value
= ~0;
894 ch
->max_match_value
= (1 << cmt
->info
->width
) - 1;
896 ch
->match_value
= ch
->max_match_value
;
897 raw_spin_lock_init(&ch
->lock
);
899 ret
= sh_cmt_register(ch
, dev_name(&cmt
->pdev
->dev
),
900 clockevent
, clocksource
);
902 dev_err(&cmt
->pdev
->dev
, "ch%u: registration failed\n",
906 ch
->cs_enabled
= false;
911 static int sh_cmt_map_memory(struct sh_cmt_device
*cmt
)
913 struct resource
*mem
;
915 mem
= platform_get_resource(cmt
->pdev
, IORESOURCE_MEM
, 0);
917 dev_err(&cmt
->pdev
->dev
, "failed to get I/O memory\n");
921 cmt
->mapbase
= ioremap_nocache(mem
->start
, resource_size(mem
));
922 if (cmt
->mapbase
== NULL
) {
923 dev_err(&cmt
->pdev
->dev
, "failed to remap I/O memory\n");
930 static const struct platform_device_id sh_cmt_id_table
[] = {
931 { "sh-cmt-16", (kernel_ulong_t
)&sh_cmt_info
[SH_CMT_16BIT
] },
932 { "sh-cmt-32", (kernel_ulong_t
)&sh_cmt_info
[SH_CMT_32BIT
] },
935 MODULE_DEVICE_TABLE(platform
, sh_cmt_id_table
);
937 static const struct of_device_id sh_cmt_of_table
[] __maybe_unused
= {
938 { .compatible
= "renesas,cmt-48", .data
= &sh_cmt_info
[SH_CMT_48BIT
] },
940 /* deprecated, preserved for backward compatibility */
941 .compatible
= "renesas,cmt-48-gen2",
942 .data
= &sh_cmt_info
[SH_CMT0_RCAR_GEN2
]
944 { .compatible
= "renesas,rcar-gen2-cmt0", .data
= &sh_cmt_info
[SH_CMT0_RCAR_GEN2
] },
945 { .compatible
= "renesas,rcar-gen2-cmt1", .data
= &sh_cmt_info
[SH_CMT1_RCAR_GEN2
] },
948 MODULE_DEVICE_TABLE(of
, sh_cmt_of_table
);
950 static int sh_cmt_setup(struct sh_cmt_device
*cmt
, struct platform_device
*pdev
)
957 raw_spin_lock_init(&cmt
->lock
);
959 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
960 cmt
->info
= of_device_get_match_data(&pdev
->dev
);
961 cmt
->hw_channels
= cmt
->info
->channels_mask
;
962 } else if (pdev
->dev
.platform_data
) {
963 struct sh_timer_config
*cfg
= pdev
->dev
.platform_data
;
964 const struct platform_device_id
*id
= pdev
->id_entry
;
966 cmt
->info
= (const struct sh_cmt_info
*)id
->driver_data
;
967 cmt
->hw_channels
= cfg
->channels_mask
;
969 dev_err(&cmt
->pdev
->dev
, "missing platform data\n");
973 /* Get hold of clock. */
974 cmt
->clk
= clk_get(&cmt
->pdev
->dev
, "fck");
975 if (IS_ERR(cmt
->clk
)) {
976 dev_err(&cmt
->pdev
->dev
, "cannot get clock\n");
977 return PTR_ERR(cmt
->clk
);
980 ret
= clk_prepare(cmt
->clk
);
984 /* Determine clock rate. */
985 ret
= clk_enable(cmt
->clk
);
987 goto err_clk_unprepare
;
989 if (cmt
->info
->width
== 16)
990 cmt
->rate
= clk_get_rate(cmt
->clk
) / 512;
992 cmt
->rate
= clk_get_rate(cmt
->clk
) / 8;
994 clk_disable(cmt
->clk
);
996 /* Map the memory resource(s). */
997 ret
= sh_cmt_map_memory(cmt
);
999 goto err_clk_unprepare
;
1001 /* Allocate and setup the channels. */
1002 cmt
->num_channels
= hweight8(cmt
->hw_channels
);
1003 cmt
->channels
= kzalloc(cmt
->num_channels
* sizeof(*cmt
->channels
),
1005 if (cmt
->channels
== NULL
) {
1011 * Use the first channel as a clock event device and the second channel
1012 * as a clock source. If only one channel is available use it for both.
1014 for (i
= 0, mask
= cmt
->hw_channels
; i
< cmt
->num_channels
; ++i
) {
1015 unsigned int hwidx
= ffs(mask
) - 1;
1016 bool clocksource
= i
== 1 || cmt
->num_channels
== 1;
1017 bool clockevent
= i
== 0;
1019 ret
= sh_cmt_setup_channel(&cmt
->channels
[i
], i
, hwidx
,
1020 clockevent
, clocksource
, cmt
);
1024 mask
&= ~(1 << hwidx
);
1027 platform_set_drvdata(pdev
, cmt
);
1032 kfree(cmt
->channels
);
1033 iounmap(cmt
->mapbase
);
1035 clk_unprepare(cmt
->clk
);
1041 static int sh_cmt_probe(struct platform_device
*pdev
)
1043 struct sh_cmt_device
*cmt
= platform_get_drvdata(pdev
);
1046 if (!is_early_platform_device(pdev
)) {
1047 pm_runtime_set_active(&pdev
->dev
);
1048 pm_runtime_enable(&pdev
->dev
);
1052 dev_info(&pdev
->dev
, "kept as earlytimer\n");
1056 cmt
= kzalloc(sizeof(*cmt
), GFP_KERNEL
);
1060 ret
= sh_cmt_setup(cmt
, pdev
);
1063 pm_runtime_idle(&pdev
->dev
);
1066 if (is_early_platform_device(pdev
))
1070 if (cmt
->has_clockevent
|| cmt
->has_clocksource
)
1071 pm_runtime_irq_safe(&pdev
->dev
);
1073 pm_runtime_idle(&pdev
->dev
);
1078 static int sh_cmt_remove(struct platform_device
*pdev
)
1080 return -EBUSY
; /* cannot unregister clockevent and clocksource */
1083 static struct platform_driver sh_cmt_device_driver
= {
1084 .probe
= sh_cmt_probe
,
1085 .remove
= sh_cmt_remove
,
1088 .of_match_table
= of_match_ptr(sh_cmt_of_table
),
1090 .id_table
= sh_cmt_id_table
,
1093 static int __init
sh_cmt_init(void)
1095 return platform_driver_register(&sh_cmt_device_driver
);
1098 static void __exit
sh_cmt_exit(void)
1100 platform_driver_unregister(&sh_cmt_device_driver
);
1103 early_platform_init("earlytimer", &sh_cmt_device_driver
);
1104 subsys_initcall(sh_cmt_init
);
1105 module_exit(sh_cmt_exit
);
1107 MODULE_AUTHOR("Magnus Damm");
1108 MODULE_DESCRIPTION("SuperH CMT Timer Driver");
1109 MODULE_LICENSE("GPL v2");