]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/clocksource/sh_cmt.c
clocksource/drivers/sh_cmt: Remove support for "renesas,cmt-32*"
[mirror_ubuntu-bionic-kernel.git] / drivers / clocksource / sh_cmt.c
1 /*
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
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>
23 #include <linux/io.h>
24 #include <linux/ioport.h>
25 #include <linux/irq.h>
26 #include <linux/module.h>
27 #include <linux/of.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>
34
35 struct sh_cmt_device;
36
37 /*
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.
41 *
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
48 *
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.
52 *
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
60 * block at 0x60.
61 *
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.
64 */
65
66 enum sh_cmt_model {
67 SH_CMT_16BIT,
68 SH_CMT_32BIT,
69 SH_CMT_48BIT,
70 SH_CMT0_RCAR_GEN2,
71 SH_CMT1_RCAR_GEN2,
72 };
73
74 struct sh_cmt_info {
75 enum sh_cmt_model model;
76
77 unsigned int channels_mask;
78
79 unsigned long width; /* 16 or 32 bit version of hardware block */
80 unsigned long overflow_bit;
81 unsigned long clear_bits;
82
83 /* callbacks for CMSTR and CMCSR access */
84 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
85 void (*write_control)(void __iomem *base, unsigned long offs,
86 unsigned long value);
87
88 /* callbacks for CMCNT and CMCOR access */
89 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
90 void (*write_count)(void __iomem *base, unsigned long offs,
91 unsigned long value);
92 };
93
94 struct sh_cmt_channel {
95 struct sh_cmt_device *cmt;
96
97 unsigned int index; /* Index in the documentation */
98 unsigned int hwidx; /* Real hardware index */
99
100 void __iomem *iostart;
101 void __iomem *ioctrl;
102
103 unsigned int timer_bit;
104 unsigned long flags;
105 unsigned long match_value;
106 unsigned long next_match_value;
107 unsigned long max_match_value;
108 raw_spinlock_t lock;
109 struct clock_event_device ced;
110 struct clocksource cs;
111 unsigned long total_cycles;
112 bool cs_enabled;
113 };
114
115 struct sh_cmt_device {
116 struct platform_device *pdev;
117
118 const struct sh_cmt_info *info;
119
120 void __iomem *mapbase;
121 struct clk *clk;
122 unsigned long rate;
123
124 raw_spinlock_t lock; /* Protect the shared start/stop register */
125
126 struct sh_cmt_channel *channels;
127 unsigned int num_channels;
128 unsigned int hw_channels;
129
130 bool has_clockevent;
131 bool has_clocksource;
132 };
133
134 #define SH_CMT16_CMCSR_CMF (1 << 7)
135 #define SH_CMT16_CMCSR_CMIE (1 << 6)
136 #define SH_CMT16_CMCSR_CKS8 (0 << 0)
137 #define SH_CMT16_CMCSR_CKS32 (1 << 0)
138 #define SH_CMT16_CMCSR_CKS128 (2 << 0)
139 #define SH_CMT16_CMCSR_CKS512 (3 << 0)
140 #define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
141
142 #define SH_CMT32_CMCSR_CMF (1 << 15)
143 #define SH_CMT32_CMCSR_OVF (1 << 14)
144 #define SH_CMT32_CMCSR_WRFLG (1 << 13)
145 #define SH_CMT32_CMCSR_STTF (1 << 12)
146 #define SH_CMT32_CMCSR_STPF (1 << 11)
147 #define SH_CMT32_CMCSR_SSIE (1 << 10)
148 #define SH_CMT32_CMCSR_CMS (1 << 9)
149 #define SH_CMT32_CMCSR_CMM (1 << 8)
150 #define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
151 #define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
152 #define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
153 #define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
154 #define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
155 #define SH_CMT32_CMCSR_DBGIVD (1 << 3)
156 #define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
157 #define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
158 #define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
159 #define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
160 #define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
161
162 static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
163 {
164 return ioread16(base + (offs << 1));
165 }
166
167 static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
168 {
169 return ioread32(base + (offs << 2));
170 }
171
172 static void sh_cmt_write16(void __iomem *base, unsigned long offs,
173 unsigned long value)
174 {
175 iowrite16(value, base + (offs << 1));
176 }
177
178 static void sh_cmt_write32(void __iomem *base, unsigned long offs,
179 unsigned long value)
180 {
181 iowrite32(value, base + (offs << 2));
182 }
183
184 static const struct sh_cmt_info sh_cmt_info[] = {
185 [SH_CMT_16BIT] = {
186 .model = SH_CMT_16BIT,
187 .width = 16,
188 .overflow_bit = SH_CMT16_CMCSR_CMF,
189 .clear_bits = ~SH_CMT16_CMCSR_CMF,
190 .read_control = sh_cmt_read16,
191 .write_control = sh_cmt_write16,
192 .read_count = sh_cmt_read16,
193 .write_count = sh_cmt_write16,
194 },
195 [SH_CMT_32BIT] = {
196 .model = SH_CMT_32BIT,
197 .width = 32,
198 .overflow_bit = SH_CMT32_CMCSR_CMF,
199 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
200 .read_control = sh_cmt_read16,
201 .write_control = sh_cmt_write16,
202 .read_count = sh_cmt_read32,
203 .write_count = sh_cmt_write32,
204 },
205 [SH_CMT_48BIT] = {
206 .model = SH_CMT_48BIT,
207 .channels_mask = 0x3f,
208 .width = 32,
209 .overflow_bit = SH_CMT32_CMCSR_CMF,
210 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
211 .read_control = sh_cmt_read32,
212 .write_control = sh_cmt_write32,
213 .read_count = sh_cmt_read32,
214 .write_count = sh_cmt_write32,
215 },
216 [SH_CMT0_RCAR_GEN2] = {
217 .model = SH_CMT0_RCAR_GEN2,
218 .channels_mask = 0x60,
219 .width = 32,
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,
226 },
227 [SH_CMT1_RCAR_GEN2] = {
228 .model = SH_CMT1_RCAR_GEN2,
229 .channels_mask = 0xff,
230 .width = 32,
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,
237 },
238 };
239
240 #define CMCSR 0 /* channel register */
241 #define CMCNT 1 /* channel register */
242 #define CMCOR 2 /* channel register */
243
244 static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
245 {
246 if (ch->iostart)
247 return ch->cmt->info->read_control(ch->iostart, 0);
248 else
249 return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
250 }
251
252 static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
253 unsigned long value)
254 {
255 if (ch->iostart)
256 ch->cmt->info->write_control(ch->iostart, 0, value);
257 else
258 ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
259 }
260
261 static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
262 {
263 return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
264 }
265
266 static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
267 unsigned long value)
268 {
269 ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
270 }
271
272 static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
273 {
274 return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
275 }
276
277 static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
278 unsigned long value)
279 {
280 ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
281 }
282
283 static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
284 unsigned long value)
285 {
286 ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
287 }
288
289 static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
290 int *has_wrapped)
291 {
292 unsigned long v1, v2, v3;
293 int o1, o2;
294
295 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
296
297 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
298 do {
299 o2 = o1;
300 v1 = sh_cmt_read_cmcnt(ch);
301 v2 = sh_cmt_read_cmcnt(ch);
302 v3 = sh_cmt_read_cmcnt(ch);
303 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
304 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
305 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
306
307 *has_wrapped = o1;
308 return v2;
309 }
310
311 static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
312 {
313 unsigned long flags, value;
314
315 /* start stop register shared by multiple timer channels */
316 raw_spin_lock_irqsave(&ch->cmt->lock, flags);
317 value = sh_cmt_read_cmstr(ch);
318
319 if (start)
320 value |= 1 << ch->timer_bit;
321 else
322 value &= ~(1 << ch->timer_bit);
323
324 sh_cmt_write_cmstr(ch, value);
325 raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
326 }
327
328 static int sh_cmt_enable(struct sh_cmt_channel *ch)
329 {
330 int k, ret;
331
332 pm_runtime_get_sync(&ch->cmt->pdev->dev);
333 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
334
335 /* enable clock */
336 ret = clk_enable(ch->cmt->clk);
337 if (ret) {
338 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
339 ch->index);
340 goto err0;
341 }
342
343 /* make sure channel is disabled */
344 sh_cmt_start_stop_ch(ch, 0);
345
346 /* configure channel, periodic mode and maximum timeout */
347 if (ch->cmt->info->width == 16) {
348 sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
349 SH_CMT16_CMCSR_CKS512);
350 } else {
351 sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
352 SH_CMT32_CMCSR_CMTOUT_IE |
353 SH_CMT32_CMCSR_CMR_IRQ |
354 SH_CMT32_CMCSR_CKS_RCLK8);
355 }
356
357 sh_cmt_write_cmcor(ch, 0xffffffff);
358 sh_cmt_write_cmcnt(ch, 0);
359
360 /*
361 * According to the sh73a0 user's manual, as CMCNT can be operated
362 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
363 * modifying CMCNT register; two RCLK cycles are necessary before
364 * this register is either read or any modification of the value
365 * it holds is reflected in the LSI's actual operation.
366 *
367 * While at it, we're supposed to clear out the CMCNT as of this
368 * moment, so make sure it's processed properly here. This will
369 * take RCLKx2 at maximum.
370 */
371 for (k = 0; k < 100; k++) {
372 if (!sh_cmt_read_cmcnt(ch))
373 break;
374 udelay(1);
375 }
376
377 if (sh_cmt_read_cmcnt(ch)) {
378 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
379 ch->index);
380 ret = -ETIMEDOUT;
381 goto err1;
382 }
383
384 /* enable channel */
385 sh_cmt_start_stop_ch(ch, 1);
386 return 0;
387 err1:
388 /* stop clock */
389 clk_disable(ch->cmt->clk);
390
391 err0:
392 return ret;
393 }
394
395 static void sh_cmt_disable(struct sh_cmt_channel *ch)
396 {
397 /* disable channel */
398 sh_cmt_start_stop_ch(ch, 0);
399
400 /* disable interrupts in CMT block */
401 sh_cmt_write_cmcsr(ch, 0);
402
403 /* stop clock */
404 clk_disable(ch->cmt->clk);
405
406 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
407 pm_runtime_put(&ch->cmt->pdev->dev);
408 }
409
410 /* private flags */
411 #define FLAG_CLOCKEVENT (1 << 0)
412 #define FLAG_CLOCKSOURCE (1 << 1)
413 #define FLAG_REPROGRAM (1 << 2)
414 #define FLAG_SKIPEVENT (1 << 3)
415 #define FLAG_IRQCONTEXT (1 << 4)
416
417 static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
418 int absolute)
419 {
420 unsigned long new_match;
421 unsigned long value = ch->next_match_value;
422 unsigned long delay = 0;
423 unsigned long now = 0;
424 int has_wrapped;
425
426 now = sh_cmt_get_counter(ch, &has_wrapped);
427 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
428
429 if (has_wrapped) {
430 /* we're competing with the interrupt handler.
431 * -> let the interrupt handler reprogram the timer.
432 * -> interrupt number two handles the event.
433 */
434 ch->flags |= FLAG_SKIPEVENT;
435 return;
436 }
437
438 if (absolute)
439 now = 0;
440
441 do {
442 /* reprogram the timer hardware,
443 * but don't save the new match value yet.
444 */
445 new_match = now + value + delay;
446 if (new_match > ch->max_match_value)
447 new_match = ch->max_match_value;
448
449 sh_cmt_write_cmcor(ch, new_match);
450
451 now = sh_cmt_get_counter(ch, &has_wrapped);
452 if (has_wrapped && (new_match > ch->match_value)) {
453 /* we are changing to a greater match value,
454 * so this wrap must be caused by the counter
455 * matching the old value.
456 * -> first interrupt reprograms the timer.
457 * -> interrupt number two handles the event.
458 */
459 ch->flags |= FLAG_SKIPEVENT;
460 break;
461 }
462
463 if (has_wrapped) {
464 /* we are changing to a smaller match value,
465 * so the wrap must be caused by the counter
466 * matching the new value.
467 * -> save programmed match value.
468 * -> let isr handle the event.
469 */
470 ch->match_value = new_match;
471 break;
472 }
473
474 /* be safe: verify hardware settings */
475 if (now < new_match) {
476 /* timer value is below match value, all good.
477 * this makes sure we won't miss any match events.
478 * -> save programmed match value.
479 * -> let isr handle the event.
480 */
481 ch->match_value = new_match;
482 break;
483 }
484
485 /* the counter has reached a value greater
486 * than our new match value. and since the
487 * has_wrapped flag isn't set we must have
488 * programmed a too close event.
489 * -> increase delay and retry.
490 */
491 if (delay)
492 delay <<= 1;
493 else
494 delay = 1;
495
496 if (!delay)
497 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
498 ch->index);
499
500 } while (delay);
501 }
502
503 static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
504 {
505 if (delta > ch->max_match_value)
506 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
507 ch->index);
508
509 ch->next_match_value = delta;
510 sh_cmt_clock_event_program_verify(ch, 0);
511 }
512
513 static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
514 {
515 unsigned long flags;
516
517 raw_spin_lock_irqsave(&ch->lock, flags);
518 __sh_cmt_set_next(ch, delta);
519 raw_spin_unlock_irqrestore(&ch->lock, flags);
520 }
521
522 static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
523 {
524 struct sh_cmt_channel *ch = dev_id;
525
526 /* clear flags */
527 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
528 ch->cmt->info->clear_bits);
529
530 /* update clock source counter to begin with if enabled
531 * the wrap flag should be cleared by the timer specific
532 * isr before we end up here.
533 */
534 if (ch->flags & FLAG_CLOCKSOURCE)
535 ch->total_cycles += ch->match_value + 1;
536
537 if (!(ch->flags & FLAG_REPROGRAM))
538 ch->next_match_value = ch->max_match_value;
539
540 ch->flags |= FLAG_IRQCONTEXT;
541
542 if (ch->flags & FLAG_CLOCKEVENT) {
543 if (!(ch->flags & FLAG_SKIPEVENT)) {
544 if (clockevent_state_oneshot(&ch->ced)) {
545 ch->next_match_value = ch->max_match_value;
546 ch->flags |= FLAG_REPROGRAM;
547 }
548
549 ch->ced.event_handler(&ch->ced);
550 }
551 }
552
553 ch->flags &= ~FLAG_SKIPEVENT;
554
555 if (ch->flags & FLAG_REPROGRAM) {
556 ch->flags &= ~FLAG_REPROGRAM;
557 sh_cmt_clock_event_program_verify(ch, 1);
558
559 if (ch->flags & FLAG_CLOCKEVENT)
560 if ((clockevent_state_shutdown(&ch->ced))
561 || (ch->match_value == ch->next_match_value))
562 ch->flags &= ~FLAG_REPROGRAM;
563 }
564
565 ch->flags &= ~FLAG_IRQCONTEXT;
566
567 return IRQ_HANDLED;
568 }
569
570 static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
571 {
572 int ret = 0;
573 unsigned long flags;
574
575 raw_spin_lock_irqsave(&ch->lock, flags);
576
577 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
578 ret = sh_cmt_enable(ch);
579
580 if (ret)
581 goto out;
582 ch->flags |= flag;
583
584 /* setup timeout if no clockevent */
585 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
586 __sh_cmt_set_next(ch, ch->max_match_value);
587 out:
588 raw_spin_unlock_irqrestore(&ch->lock, flags);
589
590 return ret;
591 }
592
593 static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
594 {
595 unsigned long flags;
596 unsigned long f;
597
598 raw_spin_lock_irqsave(&ch->lock, flags);
599
600 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
601 ch->flags &= ~flag;
602
603 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
604 sh_cmt_disable(ch);
605
606 /* adjust the timeout to maximum if only clocksource left */
607 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
608 __sh_cmt_set_next(ch, ch->max_match_value);
609
610 raw_spin_unlock_irqrestore(&ch->lock, flags);
611 }
612
613 static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
614 {
615 return container_of(cs, struct sh_cmt_channel, cs);
616 }
617
618 static u64 sh_cmt_clocksource_read(struct clocksource *cs)
619 {
620 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
621 unsigned long flags, raw;
622 unsigned long value;
623 int has_wrapped;
624
625 raw_spin_lock_irqsave(&ch->lock, flags);
626 value = ch->total_cycles;
627 raw = sh_cmt_get_counter(ch, &has_wrapped);
628
629 if (unlikely(has_wrapped))
630 raw += ch->match_value + 1;
631 raw_spin_unlock_irqrestore(&ch->lock, flags);
632
633 return value + raw;
634 }
635
636 static int sh_cmt_clocksource_enable(struct clocksource *cs)
637 {
638 int ret;
639 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
640
641 WARN_ON(ch->cs_enabled);
642
643 ch->total_cycles = 0;
644
645 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
646 if (!ret)
647 ch->cs_enabled = true;
648
649 return ret;
650 }
651
652 static void sh_cmt_clocksource_disable(struct clocksource *cs)
653 {
654 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
655
656 WARN_ON(!ch->cs_enabled);
657
658 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
659 ch->cs_enabled = false;
660 }
661
662 static void sh_cmt_clocksource_suspend(struct clocksource *cs)
663 {
664 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
665
666 if (!ch->cs_enabled)
667 return;
668
669 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
670 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
671 }
672
673 static void sh_cmt_clocksource_resume(struct clocksource *cs)
674 {
675 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
676
677 if (!ch->cs_enabled)
678 return;
679
680 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
681 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
682 }
683
684 static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
685 const char *name)
686 {
687 struct clocksource *cs = &ch->cs;
688
689 cs->name = name;
690 cs->rating = 125;
691 cs->read = sh_cmt_clocksource_read;
692 cs->enable = sh_cmt_clocksource_enable;
693 cs->disable = sh_cmt_clocksource_disable;
694 cs->suspend = sh_cmt_clocksource_suspend;
695 cs->resume = sh_cmt_clocksource_resume;
696 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
697 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
698
699 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
700 ch->index);
701
702 clocksource_register_hz(cs, ch->cmt->rate);
703 return 0;
704 }
705
706 static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
707 {
708 return container_of(ced, struct sh_cmt_channel, ced);
709 }
710
711 static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
712 {
713 sh_cmt_start(ch, FLAG_CLOCKEVENT);
714
715 if (periodic)
716 sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
717 else
718 sh_cmt_set_next(ch, ch->max_match_value);
719 }
720
721 static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
722 {
723 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
724
725 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
726 return 0;
727 }
728
729 static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
730 int periodic)
731 {
732 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
733
734 /* deal with old setting first */
735 if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
736 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
737
738 dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
739 ch->index, periodic ? "periodic" : "oneshot");
740 sh_cmt_clock_event_start(ch, periodic);
741 return 0;
742 }
743
744 static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
745 {
746 return sh_cmt_clock_event_set_state(ced, 0);
747 }
748
749 static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
750 {
751 return sh_cmt_clock_event_set_state(ced, 1);
752 }
753
754 static int sh_cmt_clock_event_next(unsigned long delta,
755 struct clock_event_device *ced)
756 {
757 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
758
759 BUG_ON(!clockevent_state_oneshot(ced));
760 if (likely(ch->flags & FLAG_IRQCONTEXT))
761 ch->next_match_value = delta - 1;
762 else
763 sh_cmt_set_next(ch, delta - 1);
764
765 return 0;
766 }
767
768 static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
769 {
770 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
771
772 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
773 clk_unprepare(ch->cmt->clk);
774 }
775
776 static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
777 {
778 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
779
780 clk_prepare(ch->cmt->clk);
781 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
782 }
783
784 static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
785 const char *name)
786 {
787 struct clock_event_device *ced = &ch->ced;
788 int irq;
789 int ret;
790
791 irq = platform_get_irq(ch->cmt->pdev, ch->index);
792 if (irq < 0) {
793 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
794 ch->index);
795 return irq;
796 }
797
798 ret = request_irq(irq, sh_cmt_interrupt,
799 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
800 dev_name(&ch->cmt->pdev->dev), ch);
801 if (ret) {
802 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
803 ch->index, irq);
804 return ret;
805 }
806
807 ced->name = name;
808 ced->features = CLOCK_EVT_FEAT_PERIODIC;
809 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
810 ced->rating = 125;
811 ced->cpumask = cpu_possible_mask;
812 ced->set_next_event = sh_cmt_clock_event_next;
813 ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
814 ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
815 ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
816 ced->suspend = sh_cmt_clock_event_suspend;
817 ced->resume = sh_cmt_clock_event_resume;
818
819 /* TODO: calculate good shift from rate and counter bit width */
820 ced->shift = 32;
821 ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
822 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
823 ced->max_delta_ticks = ch->max_match_value;
824 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
825 ced->min_delta_ticks = 0x1f;
826
827 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
828 ch->index);
829 clockevents_register_device(ced);
830
831 return 0;
832 }
833
834 static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
835 bool clockevent, bool clocksource)
836 {
837 int ret;
838
839 if (clockevent) {
840 ch->cmt->has_clockevent = true;
841 ret = sh_cmt_register_clockevent(ch, name);
842 if (ret < 0)
843 return ret;
844 }
845
846 if (clocksource) {
847 ch->cmt->has_clocksource = true;
848 sh_cmt_register_clocksource(ch, name);
849 }
850
851 return 0;
852 }
853
854 static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
855 unsigned int hwidx, bool clockevent,
856 bool clocksource, struct sh_cmt_device *cmt)
857 {
858 int ret;
859
860 /* Skip unused channels. */
861 if (!clockevent && !clocksource)
862 return 0;
863
864 ch->cmt = cmt;
865 ch->index = index;
866 ch->hwidx = hwidx;
867 ch->timer_bit = hwidx;
868
869 /*
870 * Compute the address of the channel control register block. For the
871 * timers with a per-channel start/stop register, compute its address
872 * as well.
873 */
874 switch (cmt->info->model) {
875 case SH_CMT_16BIT:
876 ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
877 break;
878 case SH_CMT_32BIT:
879 case SH_CMT_48BIT:
880 ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
881 break;
882 case SH_CMT0_RCAR_GEN2:
883 case SH_CMT1_RCAR_GEN2:
884 ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
885 ch->ioctrl = ch->iostart + 0x10;
886 ch->timer_bit = 0;
887 break;
888 }
889
890 if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
891 ch->max_match_value = ~0;
892 else
893 ch->max_match_value = (1 << cmt->info->width) - 1;
894
895 ch->match_value = ch->max_match_value;
896 raw_spin_lock_init(&ch->lock);
897
898 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
899 clockevent, clocksource);
900 if (ret) {
901 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
902 ch->index);
903 return ret;
904 }
905 ch->cs_enabled = false;
906
907 return 0;
908 }
909
910 static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
911 {
912 struct resource *mem;
913
914 mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
915 if (!mem) {
916 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
917 return -ENXIO;
918 }
919
920 cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
921 if (cmt->mapbase == NULL) {
922 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
923 return -ENXIO;
924 }
925
926 return 0;
927 }
928
929 static const struct platform_device_id sh_cmt_id_table[] = {
930 { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
931 { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
932 { }
933 };
934 MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
935
936 static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
937 { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
938 { .compatible = "renesas,cmt-48-gen2", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
939 { .compatible = "renesas,rcar-gen2-cmt0", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
940 { .compatible = "renesas,rcar-gen2-cmt1", .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2] },
941 { }
942 };
943 MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
944
945 static int sh_cmt_parse_dt(struct sh_cmt_device *cmt)
946 {
947 struct device_node *np = cmt->pdev->dev.of_node;
948
949 return of_property_read_u32(np, "renesas,channels-mask",
950 &cmt->hw_channels);
951 }
952
953 static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
954 {
955 unsigned int mask;
956 unsigned int i;
957 int ret;
958
959 cmt->pdev = pdev;
960 raw_spin_lock_init(&cmt->lock);
961
962 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
963 const struct of_device_id *id;
964
965 id = of_match_node(sh_cmt_of_table, pdev->dev.of_node);
966 cmt->info = id->data;
967
968 /* prefer in-driver channel configuration over DT */
969 if (cmt->info->channels_mask) {
970 cmt->hw_channels = cmt->info->channels_mask;
971 } else {
972 ret = sh_cmt_parse_dt(cmt);
973 if (ret < 0)
974 return ret;
975 }
976 } else if (pdev->dev.platform_data) {
977 struct sh_timer_config *cfg = pdev->dev.platform_data;
978 const struct platform_device_id *id = pdev->id_entry;
979
980 cmt->info = (const struct sh_cmt_info *)id->driver_data;
981 cmt->hw_channels = cfg->channels_mask;
982 } else {
983 dev_err(&cmt->pdev->dev, "missing platform data\n");
984 return -ENXIO;
985 }
986
987 /* Get hold of clock. */
988 cmt->clk = clk_get(&cmt->pdev->dev, "fck");
989 if (IS_ERR(cmt->clk)) {
990 dev_err(&cmt->pdev->dev, "cannot get clock\n");
991 return PTR_ERR(cmt->clk);
992 }
993
994 ret = clk_prepare(cmt->clk);
995 if (ret < 0)
996 goto err_clk_put;
997
998 /* Determine clock rate. */
999 ret = clk_enable(cmt->clk);
1000 if (ret < 0)
1001 goto err_clk_unprepare;
1002
1003 if (cmt->info->width == 16)
1004 cmt->rate = clk_get_rate(cmt->clk) / 512;
1005 else
1006 cmt->rate = clk_get_rate(cmt->clk) / 8;
1007
1008 clk_disable(cmt->clk);
1009
1010 /* Map the memory resource(s). */
1011 ret = sh_cmt_map_memory(cmt);
1012 if (ret < 0)
1013 goto err_clk_unprepare;
1014
1015 /* Allocate and setup the channels. */
1016 cmt->num_channels = hweight8(cmt->hw_channels);
1017 cmt->channels = kzalloc(cmt->num_channels * sizeof(*cmt->channels),
1018 GFP_KERNEL);
1019 if (cmt->channels == NULL) {
1020 ret = -ENOMEM;
1021 goto err_unmap;
1022 }
1023
1024 /*
1025 * Use the first channel as a clock event device and the second channel
1026 * as a clock source. If only one channel is available use it for both.
1027 */
1028 for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
1029 unsigned int hwidx = ffs(mask) - 1;
1030 bool clocksource = i == 1 || cmt->num_channels == 1;
1031 bool clockevent = i == 0;
1032
1033 ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
1034 clockevent, clocksource, cmt);
1035 if (ret < 0)
1036 goto err_unmap;
1037
1038 mask &= ~(1 << hwidx);
1039 }
1040
1041 platform_set_drvdata(pdev, cmt);
1042
1043 return 0;
1044
1045 err_unmap:
1046 kfree(cmt->channels);
1047 iounmap(cmt->mapbase);
1048 err_clk_unprepare:
1049 clk_unprepare(cmt->clk);
1050 err_clk_put:
1051 clk_put(cmt->clk);
1052 return ret;
1053 }
1054
1055 static int sh_cmt_probe(struct platform_device *pdev)
1056 {
1057 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
1058 int ret;
1059
1060 if (!is_early_platform_device(pdev)) {
1061 pm_runtime_set_active(&pdev->dev);
1062 pm_runtime_enable(&pdev->dev);
1063 }
1064
1065 if (cmt) {
1066 dev_info(&pdev->dev, "kept as earlytimer\n");
1067 goto out;
1068 }
1069
1070 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
1071 if (cmt == NULL)
1072 return -ENOMEM;
1073
1074 ret = sh_cmt_setup(cmt, pdev);
1075 if (ret) {
1076 kfree(cmt);
1077 pm_runtime_idle(&pdev->dev);
1078 return ret;
1079 }
1080 if (is_early_platform_device(pdev))
1081 return 0;
1082
1083 out:
1084 if (cmt->has_clockevent || cmt->has_clocksource)
1085 pm_runtime_irq_safe(&pdev->dev);
1086 else
1087 pm_runtime_idle(&pdev->dev);
1088
1089 return 0;
1090 }
1091
1092 static int sh_cmt_remove(struct platform_device *pdev)
1093 {
1094 return -EBUSY; /* cannot unregister clockevent and clocksource */
1095 }
1096
1097 static struct platform_driver sh_cmt_device_driver = {
1098 .probe = sh_cmt_probe,
1099 .remove = sh_cmt_remove,
1100 .driver = {
1101 .name = "sh_cmt",
1102 .of_match_table = of_match_ptr(sh_cmt_of_table),
1103 },
1104 .id_table = sh_cmt_id_table,
1105 };
1106
1107 static int __init sh_cmt_init(void)
1108 {
1109 return platform_driver_register(&sh_cmt_device_driver);
1110 }
1111
1112 static void __exit sh_cmt_exit(void)
1113 {
1114 platform_driver_unregister(&sh_cmt_device_driver);
1115 }
1116
1117 early_platform_init("earlytimer", &sh_cmt_device_driver);
1118 subsys_initcall(sh_cmt_init);
1119 module_exit(sh_cmt_exit);
1120
1121 MODULE_AUTHOR("Magnus Damm");
1122 MODULE_DESCRIPTION("SuperH CMT Timer Driver");
1123 MODULE_LICENSE("GPL v2");