2 * linux/arch/h8300/kernel/cpu/timer/timer8.c
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/init.h>
14 #include <linux/clockchips.h>
15 #include <linux/clk.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
30 #define FLAG_STARTED (1 << 3)
34 #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
35 #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
38 struct clock_event_device ced;
39 void __iomem *mapbase;
44 static irqreturn_t timer8_interrupt(int irq, void *dev_id)
46 struct timer8_priv *p = dev_id;
48 if (clockevent_state_oneshot(&p->ced))
49 iowrite16be(0x0000, p->mapbase + _8TCR);
51 p->ced.event_handler(&p->ced);
53 bclr(CMFA, p->mapbase + _8TCSR);
58 static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
61 pr_warn("delta out of range\n");
62 bclr(CMIEA, p->mapbase + _8TCR);
63 iowrite16be(delta, p->mapbase + TCORA);
64 iowrite16be(0x0000, p->mapbase + _8TCNT);
65 bclr(CMFA, p->mapbase + _8TCSR);
66 bset(CMIEA, p->mapbase + _8TCR);
69 static int timer8_enable(struct timer8_priv *p)
71 iowrite16be(0xffff, p->mapbase + TCORA);
72 iowrite16be(0x0000, p->mapbase + _8TCNT);
73 iowrite16be(0x0c02, p->mapbase + _8TCR);
78 static int timer8_start(struct timer8_priv *p)
82 if ((p->flags & FLAG_STARTED))
85 ret = timer8_enable(p);
87 p->flags |= FLAG_STARTED;
92 static void timer8_stop(struct timer8_priv *p)
94 iowrite16be(0x0000, p->mapbase + _8TCR);
97 static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
99 return container_of(ced, struct timer8_priv, ced);
102 static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
104 struct clock_event_device *ced = &p->ced;
109 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
110 ced->max_delta_ns = clockevent_delta2ns(0xffff, ced);
111 ced->min_delta_ns = clockevent_delta2ns(0x0001, ced);
113 timer8_set_next(p, delta);
116 static int timer8_clock_event_shutdown(struct clock_event_device *ced)
118 timer8_stop(ced_to_priv(ced));
122 static int timer8_clock_event_periodic(struct clock_event_device *ced)
124 struct timer8_priv *p = ced_to_priv(ced);
126 pr_info("%s: used for periodic clock events\n", ced->name);
128 timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
133 static int timer8_clock_event_oneshot(struct clock_event_device *ced)
135 struct timer8_priv *p = ced_to_priv(ced);
137 pr_info("%s: used for oneshot clock events\n", ced->name);
139 timer8_clock_event_start(p, 0x10000);
144 static int timer8_clock_event_next(unsigned long delta,
145 struct clock_event_device *ced)
147 struct timer8_priv *p = ced_to_priv(ced);
149 BUG_ON(!clockevent_state_oneshot(ced));
150 timer8_set_next(p, delta - 1);
155 static struct timer8_priv timer8_priv = {
157 .name = "h8300_8timer",
158 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
160 .set_next_event = timer8_clock_event_next,
161 .set_state_shutdown = timer8_clock_event_shutdown,
162 .set_state_periodic = timer8_clock_event_periodic,
163 .set_state_oneshot = timer8_clock_event_oneshot,
167 static int __init h8300_8timer_init(struct device_node *node)
173 clk = of_clk_get(node, 0);
175 pr_err("failed to get clock for clockevent\n");
180 base = of_iomap(node, 0);
182 pr_err("failed to map registers for clockevent\n");
187 irq = irq_of_parse_and_map(node, 0);
189 pr_err("failed to get irq for clockevent\n");
193 timer8_priv.mapbase = base;
195 timer8_priv.rate = clk_get_rate(clk) / SCALE;
196 if (!timer8_priv.rate) {
197 pr_err("Failed to get rate for the clocksource\n");
201 if (request_irq(irq, timer8_interrupt, IRQF_TIMER,
202 timer8_priv.ced.name, &timer8_priv) < 0) {
203 pr_err("failed to request irq %d for clockevent\n", irq);
207 clockevents_config_and_register(&timer8_priv.ced,
208 timer8_priv.rate, 1, 0x0000ffff);
218 CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);