blob: 1ba453b474785b301b48acd6b58c008229cde8aa [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;
Daniel Lezcano75160512015-11-08 22:55:12 +010033 void __iomem *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
Yoshinori Sato618b9022015-01-28 02:52:42 +090039static irqreturn_t timer8_interrupt(int irq, void *dev_id)
40{
41 struct timer8_priv *p = dev_id;
42
Daniel Lezcano7053fda2015-11-08 18:07:38 +010043 if (clockevent_state_oneshot(&p->ced))
Daniel Lezcano75160512015-11-08 22:55:12 +010044 writew(0x0000, p->mapbase + _8TCR);
Daniel Lezcano7053fda2015-11-08 18:07:38 +010045
46 p->ced.event_handler(&p->ced);
Yoshinori Sato618b9022015-01-28 02:52:42 +090047
Yoshinori Satof37632d2015-12-05 02:48:16 +090048 writeb(readb(p->mapbase + _8TCSR) & ~0x40,
49 p->mapbase + _8TCSR);
50
Yoshinori Sato618b9022015-01-28 02:52:42 +090051 return IRQ_HANDLED;
52}
53
54static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
55{
Yoshinori Sato618b9022015-01-28 02:52:42 +090056 if (delta >= 0x10000)
Daniel Lezcano8c09b7d2015-11-09 09:02:38 +010057 pr_warn("delta out of range\n");
Yoshinori Satof37632d2015-12-05 02:48:16 +090058 writeb(readb(p->mapbase + _8TCR) & ~0x40, p->mapbase + _8TCR);
59 writew(0, p->mapbase + _8TCNT);
60 writew(delta, p->mapbase + TCORA);
Daniel Lezcano75160512015-11-08 22:55:12 +010061 writeb(readb(p->mapbase + _8TCR) | 0x40, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090062}
63
64static int timer8_enable(struct timer8_priv *p)
65{
Daniel Lezcano75160512015-11-08 22:55:12 +010066 writew(0xffff, p->mapbase + TCORA);
67 writew(0x0000, p->mapbase + _8TCNT);
68 writew(0x0c02, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090069
70 return 0;
71}
72
73static int timer8_start(struct timer8_priv *p)
74{
Daniel Lezcanocce483e2015-11-08 23:24:28 +010075 int ret;
Yoshinori Sato618b9022015-01-28 02:52:42 +090076
Daniel Lezcanocce483e2015-11-08 23:24:28 +010077 if ((p->flags & FLAG_STARTED))
78 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +090079
Daniel Lezcanocce483e2015-11-08 23:24:28 +010080 ret = timer8_enable(p);
81 if (!ret)
82 p->flags |= FLAG_STARTED;
Yoshinori Sato618b9022015-01-28 02:52:42 +090083
Yoshinori Sato618b9022015-01-28 02:52:42 +090084 return ret;
85}
86
87static void timer8_stop(struct timer8_priv *p)
88{
Daniel Lezcano75160512015-11-08 22:55:12 +010089 writew(0x0000, p->mapbase + _8TCR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090090}
91
92static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
93{
94 return container_of(ced, struct timer8_priv, ced);
95}
96
Daniel Lezcano1f058d52015-11-08 17:46:54 +010097static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
Yoshinori Sato618b9022015-01-28 02:52:42 +090098{
99 struct clock_event_device *ced = &p->ced;
100
101 timer8_start(p);
102
103 ced->shift = 32;
104 ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
105 ced->max_delta_ns = clockevent_delta2ns(0xffff, ced);
106 ced->min_delta_ns = clockevent_delta2ns(0x0001, ced);
107
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100108 timer8_set_next(p, delta);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900109}
110
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530111static int timer8_clock_event_shutdown(struct clock_event_device *ced)
112{
113 timer8_stop(ced_to_priv(ced));
114 return 0;
115}
116
117static int timer8_clock_event_periodic(struct clock_event_device *ced)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900118{
119 struct timer8_priv *p = ced_to_priv(ced);
120
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900121 pr_info("%s: used for periodic clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530122 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100123 timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530124
125 return 0;
126}
127
128static int timer8_clock_event_oneshot(struct clock_event_device *ced)
129{
130 struct timer8_priv *p = ced_to_priv(ced);
131
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900132 pr_info("%s: used for oneshot clock events\n", ced->name);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530133 timer8_stop(p);
Daniel Lezcano1f058d52015-11-08 17:46:54 +0100134 timer8_clock_event_start(p, 0x10000);
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530135
136 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900137}
138
139static int timer8_clock_event_next(unsigned long delta,
140 struct clock_event_device *ced)
141{
142 struct timer8_priv *p = ced_to_priv(ced);
143
Viresh Kumarfc2b2f52015-07-27 15:02:00 +0530144 BUG_ON(!clockevent_state_oneshot(ced));
Yoshinori Sato618b9022015-01-28 02:52:42 +0900145 timer8_set_next(p, delta - 1);
146
147 return 0;
148}
149
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900150static struct timer8_priv timer8_priv = {
151 .ced = {
152 .name = "h8300_8timer",
153 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
154 .rating = 200,
155 .set_next_event = timer8_clock_event_next,
156 .set_state_shutdown = timer8_clock_event_shutdown,
157 .set_state_periodic = timer8_clock_event_periodic,
158 .set_state_oneshot = timer8_clock_event_oneshot,
159 },
160};
161
162static void __init h8300_8timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900163{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900164 void __iomem *base;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900165 int irq;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900166 int ret = 0;
167 int rate;
168 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900169
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900170 clk = of_clk_get(node, 0);
171 if (IS_ERR(clk)) {
172 pr_err("failed to get clock for clockevent\n");
173 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900174 }
175
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900176 base = of_iomap(node, 0);
177 if (!base) {
178 pr_err("failed to map registers for clockevent\n");
179 goto free_clk;
180 }
181
182 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano54a0cd52015-11-08 17:56:18 +0100183 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900184 pr_err("failed to get irq for clockevent\n");
185 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900186 }
187
Daniel Lezcano75160512015-11-08 22:55:12 +0100188 timer8_priv.mapbase = base;
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100189
190 rate = clk_get_rate(clk) / SCALE;
191 if (!rate) {
192 pr_err("Failed to get rate for the clocksource\n");
193 goto unmap_reg;
194 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900195
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900196 ret = request_irq(irq, timer8_interrupt,
197 IRQF_TIMER, timer8_priv.ced.name, &timer8_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900198 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900199 pr_err("failed to request irq %d for clockevent\n", irq);
200 goto unmap_reg;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900201 }
Yoshinori Sato618b9022015-01-28 02:52:42 +0900202
Daniel Lezcanocce483e2015-11-08 23:24:28 +0100203 clockevents_config_and_register(&timer8_priv.ced, rate, 1, 0x0000ffff);
204
205 return;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900206unmap_reg:
207 iounmap(base);
208free_clk:
209 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900210}
211
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900212CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);