blob: 187c41619b13f69f9f616cb8f75ffc8a0bcadfb2 [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;
Yoshinori Sato618b9022015-01-28 02:52:42 +090037};
38
39static unsigned long timer8_get_counter(struct timer8_priv *p)
40{
41 unsigned long v1, v2, v3;
42 int o1, o2;
43
44 o1 = ctrl_inb(p->mapbase + _8TCSR) & 0x20;
45
46 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
47 do {
48 o2 = o1;
49 v1 = ctrl_inw(p->mapbase + _8TCNT);
50 v2 = ctrl_inw(p->mapbase + _8TCNT);
51 v3 = ctrl_inw(p->mapbase + _8TCNT);
52 o1 = ctrl_inb(p->mapbase + _8TCSR) & 0x20;
53 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
54 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
55
56 v2 |= o1 << 10;
57 return v2;
58}
59
60static irqreturn_t timer8_interrupt(int irq, void *dev_id)
61{
62 struct timer8_priv *p = dev_id;
63
64 ctrl_outb(ctrl_inb(p->mapbase + _8TCSR) & ~0x40,
65 p->mapbase + _8TCSR);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010066
Yoshinori Sato618b9022015-01-28 02:52:42 +090067 ctrl_outw(p->tcora, p->mapbase + TCORA);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010068
69 if (clockevent_state_oneshot(&p->ced))
70 ctrl_outw(0x0000, p->mapbase + _8TCR);
71
72 p->ced.event_handler(&p->ced);
Yoshinori Sato618b9022015-01-28 02:52:42 +090073
74 return IRQ_HANDLED;
75}
76
77static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
78{
Yoshinori Sato618b9022015-01-28 02:52:42 +090079 unsigned long now;
80
Yoshinori Sato618b9022015-01-28 02:52:42 +090081 if (delta >= 0x10000)
Daniel Lezcano8c09b7d2015-11-09 09:02:38 +010082 pr_warn("delta out of range\n");
Yoshinori Sato618b9022015-01-28 02:52:42 +090083 now = timer8_get_counter(p);
84 p->tcora = delta;
85 ctrl_outb(ctrl_inb(p->mapbase + _8TCR) | 0x40, p->mapbase + _8TCR);
86 if (delta > now)
87 ctrl_outw(delta, p->mapbase + TCORA);
88 else
89 ctrl_outw(now + 1, p->mapbase + TCORA);
Yoshinori Sato618b9022015-01-28 02:52:42 +090090}
91
92static int timer8_enable(struct timer8_priv *p)
93{
Yoshinori Sato618b9022015-01-28 02:52:42 +090094 ctrl_outw(0xffff, p->mapbase + TCORA);
95 ctrl_outw(0x0000, p->mapbase + _8TCNT);
96 ctrl_outw(0x0c02, p->mapbase + _8TCR);
97
98 return 0;
99}
100
101static int timer8_start(struct timer8_priv *p)
102{
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100103 int ret;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900104
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100105 if ((p->flags & FLAG_STARTED))
106 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900107
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100108 ret = timer8_enable(p);
109 if (!ret)
110 p->flags |= FLAG_STARTED;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900111
Yoshinori Sato618b9022015-01-28 02:52:42 +0900112 return ret;
113}
114
115static void timer8_stop(struct timer8_priv *p)
116{
Yoshinori Sato618b9022015-01-28 02:52:42 +0900117 ctrl_outw(0x0000, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900118}
119
120static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
121{
122 return container_of(ced, struct timer8_priv, ced);
123}
124
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100125static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900126{
127 struct clock_event_device *ced = &p->ced;
128
129 timer8_start(p);
130
131 ced->shift = 32;
132 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
133 ced->max_delta_ns = clockevent_delta2ns(0xffff, ced);
134 ced->min_delta_ns = clockevent_delta2ns(0x0001, ced);
135
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100136 timer8_set_next(p, delta);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900137}
138
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530139static int timer8_clock_event_shutdown(struct clock_event_device *ced)
140{
141 timer8_stop(ced_to_priv(ced));
142 return 0;
143}
144
145static int timer8_clock_event_periodic(struct clock_event_device *ced)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900146{
147 struct timer8_priv *p = ced_to_priv(ced);
148
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900149 pr_info("%s: used for periodic clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530150 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100151 timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530152
153 return 0;
154}
155
156static int timer8_clock_event_oneshot(struct clock_event_device *ced)
157{
158 struct timer8_priv *p = ced_to_priv(ced);
159
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900160 pr_info("%s: used for oneshot clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530161 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100162 timer8_clock_event_start(p, 0x10000);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530163
164 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900165}
166
167static int timer8_clock_event_next(unsigned long delta,
168 struct clock_event_device *ced)
169{
170 struct timer8_priv *p = ced_to_priv(ced);
171
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530172 BUG_ON(!clockevent_state_oneshot(ced));
Yoshinori Sato618b9022015-01-28 02:52:42 +0900173 timer8_set_next(p, delta - 1);
174
175 return 0;
176}
177
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900178static struct timer8_priv timer8_priv = {
179 .ced = {
180 .name = "h8300_8timer",
181 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
182 .rating = 200,
183 .set_next_event = timer8_clock_event_next,
184 .set_state_shutdown = timer8_clock_event_shutdown,
185 .set_state_periodic = timer8_clock_event_periodic,
186 .set_state_oneshot = timer8_clock_event_oneshot,
187 },
188};
189
190static void __init h8300_8timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900191{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900192 void __iomem *base;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900193 int irq;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900194 int ret = 0;
195 int rate;
196 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900197
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900198 clk = of_clk_get(node, 0);
199 if (IS_ERR(clk)) {
200 pr_err("failed to get clock for clockevent\n");
201 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900202 }
203
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900204 base = of_iomap(node, 0);
205 if (!base) {
206 pr_err("failed to map registers for clockevent\n");
207 goto free_clk;
208 }
209
210 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano54a0cd52015-11-08 17:56:18 +0100211 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900212 pr_err("failed to get irq for clockevent\n");
213 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900214 }
215
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900216 timer8_priv.mapbase = (unsigned long)base;
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100217
218 rate = clk_get_rate(clk) / SCALE;
219 if (!rate) {
220 pr_err("Failed to get rate for the clocksource\n");
221 goto unmap_reg;
222 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900223
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900224 ret = request_irq(irq, timer8_interrupt,
225 IRQF_TIMER, timer8_priv.ced.name, &timer8_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900226 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900227 pr_err("failed to request irq %d for clockevent\n", irq);
228 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900229 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900230
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100231 clockevents_config_and_register(&timer8_priv.ced, rate, 1, 0x0000ffff);
232
233 return;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900234unmap_reg:
235 iounmap(base);
236free_clk:
237 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900238}
239
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900240CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);