blob: 804c489531d692e96a5166c49f443f8aa18f3aca [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 Satod33f2502015-12-05 02:48:18 +090027#define CMIEA 6
28#define CMFA 6
29
Yoshinori Sato618b9022015-01-28 02:52:42 +090030#define FLAG_STARTED (1 << 3)
31
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090032#define SCALE 64
33
Yoshinori Satod33f2502015-12-05 02:48:18 +090034#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
35#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
36
Yoshinori Sato618b9022015-01-28 02:52:42 +090037struct timer8_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090038 struct clock_event_device ced;
Daniel Lezcano75160512015-11-08 22:55:12 +010039 void __iomem *mapbase;
Yoshinori Sato618b9022015-01-28 02:52:42 +090040 unsigned long flags;
41 unsigned int rate;
Yoshinori Sato618b9022015-01-28 02:52:42 +090042};
43
Yoshinori Sato618b9022015-01-28 02:52:42 +090044static irqreturn_t timer8_interrupt(int irq, void *dev_id)
45{
46 struct timer8_priv *p = dev_id;
47
Daniel Lezcano7053fda2015-11-08 18:07:38 +010048 if (clockevent_state_oneshot(&p->ced))
Yoshinori Satod33f2502015-12-05 02:48:18 +090049 iowrite16be(0x0000, p->mapbase + _8TCR);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010050
51 p->ced.event_handler(&p->ced);
Yoshinori Sato618b9022015-01-28 02:52:42 +090052
Yoshinori Satod33f2502015-12-05 02:48:18 +090053 bclr(CMFA, p->mapbase + _8TCSR);
Yoshinori Satof37632d2015-12-05 02:48:16 +090054
Yoshinori Sato618b9022015-01-28 02:52:42 +090055 return IRQ_HANDLED;
56}
57
58static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
59{
Yoshinori Sato618b9022015-01-28 02:52:42 +090060 if (delta >= 0x10000)
Daniel Lezcano8c09b7d2015-11-09 09:02:38 +010061 pr_warn("delta out of range\n");
Yoshinori Satod33f2502015-12-05 02:48:18 +090062 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);
Yoshinori Sato618b9022015-01-28 02:52:42 +090067}
68
69static int timer8_enable(struct timer8_priv *p)
70{
Yoshinori Satod33f2502015-12-05 02:48:18 +090071 iowrite16be(0xffff, p->mapbase + TCORA);
72 iowrite16be(0x0000, p->mapbase + _8TCNT);
73 iowrite16be(0x0c02, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090074
75 return 0;
76}
77
78static int timer8_start(struct timer8_priv *p)
79{
Daniel Lezcanocce483e2015-11-08 23:24:28 +010080 int ret;
Yoshinori Sato618b9022015-01-28 02:52:42 +090081
Daniel Lezcanocce483e2015-11-08 23:24:28 +010082 if ((p->flags & FLAG_STARTED))
83 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +090084
Daniel Lezcanocce483e2015-11-08 23:24:28 +010085 ret = timer8_enable(p);
86 if (!ret)
87 p->flags |= FLAG_STARTED;
Yoshinori Sato618b9022015-01-28 02:52:42 +090088
Yoshinori Sato618b9022015-01-28 02:52:42 +090089 return ret;
90}
91
92static void timer8_stop(struct timer8_priv *p)
93{
Yoshinori Satod33f2502015-12-05 02:48:18 +090094 iowrite16be(0x0000, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090095}
96
97static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
98{
99 return container_of(ced, struct timer8_priv, ced);
100}
101
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100102static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900103{
Yoshinori Sato618b9022015-01-28 02:52:42 +0900104 timer8_start(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100105 timer8_set_next(p, delta);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900106}
107
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530108static int timer8_clock_event_shutdown(struct clock_event_device *ced)
109{
110 timer8_stop(ced_to_priv(ced));
111 return 0;
112}
113
114static int timer8_clock_event_periodic(struct clock_event_device *ced)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900115{
116 struct timer8_priv *p = ced_to_priv(ced);
117
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900118 pr_info("%s: used for periodic clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530119 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100120 timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530121
122 return 0;
123}
124
125static int timer8_clock_event_oneshot(struct clock_event_device *ced)
126{
127 struct timer8_priv *p = ced_to_priv(ced);
128
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900129 pr_info("%s: used for oneshot clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530130 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100131 timer8_clock_event_start(p, 0x10000);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530132
133 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900134}
135
136static int timer8_clock_event_next(unsigned long delta,
137 struct clock_event_device *ced)
138{
139 struct timer8_priv *p = ced_to_priv(ced);
140
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530141 BUG_ON(!clockevent_state_oneshot(ced));
Yoshinori Sato618b9022015-01-28 02:52:42 +0900142 timer8_set_next(p, delta - 1);
143
144 return 0;
145}
146
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900147static struct timer8_priv timer8_priv = {
148 .ced = {
149 .name = "h8300_8timer",
150 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
151 .rating = 200,
152 .set_next_event = timer8_clock_event_next,
153 .set_state_shutdown = timer8_clock_event_shutdown,
154 .set_state_periodic = timer8_clock_event_periodic,
155 .set_state_oneshot = timer8_clock_event_oneshot,
156 },
157};
158
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200159static int __init h8300_8timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900160{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900161 void __iomem *base;
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200162 int irq, ret;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900163 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900164
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900165 clk = of_clk_get(node, 0);
166 if (IS_ERR(clk)) {
167 pr_err("failed to get clock for clockevent\n");
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200168 return PTR_ERR(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900169 }
170
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200171 ret = ENXIO;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900172 base = of_iomap(node, 0);
173 if (!base) {
174 pr_err("failed to map registers for clockevent\n");
175 goto free_clk;
176 }
177
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200178 ret = -EINVAL;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900179 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano54a0cd52015-11-08 17:56:18 +0100180 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900181 pr_err("failed to get irq for clockevent\n");
182 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900183 }
184
Daniel Lezcano75160512015-11-08 22:55:12 +0100185 timer8_priv.mapbase = base;
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100186
Yoshinori Sato6f2b6112015-12-05 02:48:17 +0900187 timer8_priv.rate = clk_get_rate(clk) / SCALE;
188 if (!timer8_priv.rate) {
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100189 pr_err("Failed to get rate for the clocksource\n");
190 goto unmap_reg;
191 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900192
Yoshinori Sato6f2b6112015-12-05 02:48:17 +0900193 if (request_irq(irq, timer8_interrupt, IRQF_TIMER,
194 timer8_priv.ced.name, &timer8_priv) < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900195 pr_err("failed to request irq %d for clockevent\n", irq);
196 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900197 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900198
Yoshinori Sato6f2b6112015-12-05 02:48:17 +0900199 clockevents_config_and_register(&timer8_priv.ced,
200 timer8_priv.rate, 1, 0x0000ffff);
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100201
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200202 return 0;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900203unmap_reg:
204 iounmap(base);
205free_clk:
206 clk_put(clk);
Daniel Lezcano691f8f82016-06-06 17:56:37 +0200207 return ret;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900208}
209
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200210CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);