1 // SPDX-License-Identifier: GPL-2.0
3 * H8/300 16bit Timer driver
5 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
8 #include <linux/interrupt.h>
9 #include <linux/init.h>
10 #include <linux/clocksource.h>
11 #include <linux/clk.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
23 #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
24 #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
27 struct clocksource cs;
28 unsigned long total_cycles;
29 void __iomem *mapbase;
30 void __iomem *mapcommon;
31 unsigned short cs_enabled;
37 static unsigned long timer16_get_counter(struct timer16_priv *p)
39 unsigned short v1, v2, v3;
42 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
44 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
47 v1 = ioread16be(p->mapbase + TCNT);
48 v2 = ioread16be(p->mapbase + TCNT);
49 v3 = ioread16be(p->mapbase + TCNT);
50 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
51 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
52 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
61 static irqreturn_t timer16_interrupt(int irq, void *dev_id)
63 struct timer16_priv *p = (struct timer16_priv *)dev_id;
65 bclr(p->ovf, p->mapcommon + TISRC);
66 p->total_cycles += 0x10000;
71 static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
73 return container_of(cs, struct timer16_priv, cs);
76 static u64 timer16_clocksource_read(struct clocksource *cs)
78 struct timer16_priv *p = cs_to_priv(cs);
79 unsigned long raw, value;
81 value = p->total_cycles;
82 raw = timer16_get_counter(p);
87 static int timer16_enable(struct clocksource *cs)
89 struct timer16_priv *p = cs_to_priv(cs);
91 WARN_ON(p->cs_enabled);
94 iowrite16be(0x0000, p->mapbase + TCNT);
95 iowrite8(0x83, p->mapbase + TCR);
96 bset(p->ovie, p->mapcommon + TISRC);
97 bset(p->enb, p->mapcommon + TSTR);
103 static void timer16_disable(struct clocksource *cs)
105 struct timer16_priv *p = cs_to_priv(cs);
107 WARN_ON(!p->cs_enabled);
109 bclr(p->ovie, p->mapcommon + TISRC);
110 bclr(p->enb, p->mapcommon + TSTR);
112 p->cs_enabled = false;
115 static struct timer16_priv timer16_priv = {
117 .name = "h8300_16timer",
119 .read = timer16_clocksource_read,
120 .enable = timer16_enable,
121 .disable = timer16_disable,
122 .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
123 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
130 static int __init h8300_16timer_init(struct device_node *node)
132 void __iomem *base[2];
137 clk = of_clk_get(node, 0);
139 pr_err("failed to get clock for clocksource\n");
144 base[REG_CH] = of_iomap(node, 0);
146 pr_err("failed to map registers for clocksource\n");
150 base[REG_COMM] = of_iomap(node, 1);
151 if (!base[REG_COMM]) {
152 pr_err("failed to map registers for clocksource\n");
157 irq = irq_of_parse_and_map(node, 0);
159 pr_err("failed to get irq for clockevent\n");
163 of_property_read_u32(node, "renesas,channel", &ch);
165 timer16_priv.mapbase = base[REG_CH];
166 timer16_priv.mapcommon = base[REG_COMM];
167 timer16_priv.enb = ch;
168 timer16_priv.ovf = ch;
169 timer16_priv.ovie = 4 + ch;
171 ret = request_irq(irq, timer16_interrupt,
172 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
174 pr_err("failed to request irq %d of clocksource\n", irq);
178 clocksource_register_hz(&timer16_priv.cs,
179 clk_get_rate(clk) / 8);
183 iounmap(base[REG_COMM]);
185 iounmap(base[REG_CH]);
191 TIMER_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",