blob: 24d91b1a5d5cc62fa9b3bdf9728fbd7a881ab832 [file] [log] [blame]
Yoshinori Sato618b9022015-01-28 02:52:42 +09001/*
2 * linux/arch/h8300/kernel/cpu/timer/timer8.c
3 *
4 * Yoshinori Sato <ysato@users.sourcefoge.jp>
5 *
6 * 8bit Timer driver
7 *
8 */
9
10#include <linux/errno.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090011#include <linux/kernel.h>
12#include <linux/interrupt.h>
13#include <linux/init.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090014#include <linux/clockchips.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090015#include <linux/clk.h>
16#include <linux/io.h>
17#include <linux/of.h>
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090018#include <linux/of_address.h>
19#include <linux/of_irq.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090020
Yoshinori Sato618b9022015-01-28 02:52:42 +090021#define _8TCR 0
22#define _8TCSR 2
23#define TCORA 4
24#define TCORB 6
25#define _8TCNT 8
26
Yoshinori Sato618b9022015-01-28 02:52:42 +090027#define FLAG_STARTED (1 << 3)
28
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090029#define SCALE 64
30
Yoshinori Sato618b9022015-01-28 02:52:42 +090031struct timer8_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090032 struct clock_event_device ced;
Yoshinori Sato618b9022015-01-28 02:52:42 +090033 unsigned long mapbase;
Yoshinori Sato618b9022015-01-28 02:52:42 +090034 unsigned long flags;
35 unsigned int rate;
36 unsigned int tcora;
37 struct clk *pclk;
38};
39
40static unsigned long timer8_get_counter(struct timer8_priv *p)
41{
42 unsigned long v1, v2, v3;
43 int o1, o2;
44
45 o1 = ctrl_inb(p->mapbase + _8TCSR) & 0x20;
46
47 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
48 do {
49 o2 = o1;
50 v1 = ctrl_inw(p->mapbase + _8TCNT);
51 v2 = ctrl_inw(p->mapbase + _8TCNT);
52 v3 = ctrl_inw(p->mapbase + _8TCNT);
53 o1 = ctrl_inb(p->mapbase + _8TCSR) & 0x20;
54 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
55 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
56
57 v2 |= o1 << 10;
58 return v2;
59}
60
61static irqreturn_t timer8_interrupt(int irq, void *dev_id)
62{
63 struct timer8_priv *p = dev_id;
64
65 ctrl_outb(ctrl_inb(p->mapbase + _8TCSR) & ~0x40,
66 p->mapbase + _8TCSR);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010067
Yoshinori Sato618b9022015-01-28 02:52:42 +090068 ctrl_outw(p->tcora, p->mapbase + TCORA);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010069
70 if (clockevent_state_oneshot(&p->ced))
71 ctrl_outw(0x0000, p->mapbase + _8TCR);
72
73 p->ced.event_handler(&p->ced);
Yoshinori Sato618b9022015-01-28 02:52:42 +090074
75 return IRQ_HANDLED;
76}
77
78static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
79{
Yoshinori Sato618b9022015-01-28 02:52:42 +090080 unsigned long now;
81
Yoshinori Sato618b9022015-01-28 02:52:42 +090082 if (delta >= 0x10000)
Daniel Lezcano8c09b7d2015-11-09 09:02:38 +010083 pr_warn("delta out of range\n");
Yoshinori Sato618b9022015-01-28 02:52:42 +090084 now = timer8_get_counter(p);
85 p->tcora = delta;
86 ctrl_outb(ctrl_inb(p->mapbase + _8TCR) | 0x40, p->mapbase + _8TCR);
87 if (delta > now)
88 ctrl_outw(delta, p->mapbase + TCORA);
89 else
90 ctrl_outw(now + 1, p->mapbase + TCORA);
Yoshinori Sato618b9022015-01-28 02:52:42 +090091}
92
93static int timer8_enable(struct timer8_priv *p)
94{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090095 p->rate = clk_get_rate(p->pclk) / SCALE;
Yoshinori Sato618b9022015-01-28 02:52:42 +090096 ctrl_outw(0xffff, p->mapbase + TCORA);
97 ctrl_outw(0x0000, p->mapbase + _8TCNT);
98 ctrl_outw(0x0c02, p->mapbase + _8TCR);
99
100 return 0;
101}
102
103static int timer8_start(struct timer8_priv *p)
104{
105 int ret = 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900106
107 if (!(p->flags & FLAG_STARTED))
108 ret = timer8_enable(p);
109
110 if (ret)
111 goto out;
112 p->flags |= FLAG_STARTED;
113
114 out:
Yoshinori Sato618b9022015-01-28 02:52:42 +0900115 return ret;
116}
117
118static void timer8_stop(struct timer8_priv *p)
119{
Yoshinori Sato618b9022015-01-28 02:52:42 +0900120 ctrl_outw(0x0000, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900121}
122
123static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
124{
125 return container_of(ced, struct timer8_priv, ced);
126}
127
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100128static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900129{
130 struct clock_event_device *ced = &p->ced;
131
132 timer8_start(p);
133
134 ced->shift = 32;
135 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
136 ced->max_delta_ns = clockevent_delta2ns(0xffff, ced);
137 ced->min_delta_ns = clockevent_delta2ns(0x0001, ced);
138
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100139 timer8_set_next(p, delta);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900140}
141
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530142static int timer8_clock_event_shutdown(struct clock_event_device *ced)
143{
144 timer8_stop(ced_to_priv(ced));
145 return 0;
146}
147
148static int timer8_clock_event_periodic(struct clock_event_device *ced)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900149{
150 struct timer8_priv *p = ced_to_priv(ced);
151
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900152 pr_info("%s: used for periodic clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530153 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100154 timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530155
156 return 0;
157}
158
159static int timer8_clock_event_oneshot(struct clock_event_device *ced)
160{
161 struct timer8_priv *p = ced_to_priv(ced);
162
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900163 pr_info("%s: used for oneshot clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530164 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100165 timer8_clock_event_start(p, 0x10000);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530166
167 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900168}
169
170static int timer8_clock_event_next(unsigned long delta,
171 struct clock_event_device *ced)
172{
173 struct timer8_priv *p = ced_to_priv(ced);
174
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530175 BUG_ON(!clockevent_state_oneshot(ced));
Yoshinori Sato618b9022015-01-28 02:52:42 +0900176 timer8_set_next(p, delta - 1);
177
178 return 0;
179}
180
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900181static struct timer8_priv timer8_priv = {
182 .ced = {
183 .name = "h8300_8timer",
184 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
185 .rating = 200,
186 .set_next_event = timer8_clock_event_next,
187 .set_state_shutdown = timer8_clock_event_shutdown,
188 .set_state_periodic = timer8_clock_event_periodic,
189 .set_state_oneshot = timer8_clock_event_oneshot,
190 },
191};
192
193static void __init h8300_8timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900194{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900195 void __iomem *base;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900196 int irq;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900197 int ret = 0;
198 int rate;
199 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900200
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900201 clk = of_clk_get(node, 0);
202 if (IS_ERR(clk)) {
203 pr_err("failed to get clock for clockevent\n");
204 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900205 }
206
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900207 base = of_iomap(node, 0);
208 if (!base) {
209 pr_err("failed to map registers for clockevent\n");
210 goto free_clk;
211 }
212
213 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano54a0cd52015-11-08 17:56:18 +0100214 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900215 pr_err("failed to get irq for clockevent\n");
216 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900217 }
218
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900219 timer8_priv.mapbase = (unsigned long)base;
220 timer8_priv.pclk = clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900221
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900222 ret = request_irq(irq, timer8_interrupt,
223 IRQF_TIMER, timer8_priv.ced.name, &timer8_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900224 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900225 pr_err("failed to request irq %d for clockevent\n", irq);
226 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900227 }
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900228 rate = clk_get_rate(clk) / SCALE;
229 clockevents_config_and_register(&timer8_priv.ced, rate, 1, 0x0000ffff);
230 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900231
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900232unmap_reg:
233 iounmap(base);
234free_clk:
235 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900236}
237
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900238CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);