blob: 934ed0bceec58a24b8e886cf9ec1d665d9eb126d [file] [log] [blame]
Yoshinori Sato618b9022015-01-28 02:52:42 +09001/*
2 * H8/300 16bit Timer driver
3 *
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
5 */
6
Yoshinori Sato618b9022015-01-28 02:52:42 +09007#include <linux/interrupt.h>
8#include <linux/init.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +09009#include <linux/clocksource.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090010#include <linux/clk.h>
11#include <linux/io.h>
12#include <linux/of.h>
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090013#include <linux/of_address.h>
14#include <linux/of_irq.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090015
Yoshinori Sato618b9022015-01-28 02:52:42 +090016#define TSTR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090017#define TISRC 6
18
19#define TCR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090020#define TCNT 2
Yoshinori Sato618b9022015-01-28 02:52:42 +090021
22struct timer16_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090023 struct clocksource cs;
Yoshinori Sato618b9022015-01-28 02:52:42 +090024 unsigned long total_cycles;
Daniel Lezcano75160512015-11-08 22:55:12 +010025 void __iomem *mapbase;
26 void __iomem *mapcommon;
Yoshinori Sato618b9022015-01-28 02:52:42 +090027 unsigned short cs_enabled;
28 unsigned char enb;
Yoshinori Sato618b9022015-01-28 02:52:42 +090029 unsigned char ovf;
Yoshinori Sato2a0ff872015-12-05 02:48:14 +090030 unsigned char ovie;
Yoshinori Sato618b9022015-01-28 02:52:42 +090031 struct clk *clk;
32};
33
34static unsigned long timer16_get_counter(struct timer16_priv *p)
35{
36 unsigned long v1, v2, v3;
37 int o1, o2;
38
Daniel Lezcano75160512015-11-08 22:55:12 +010039 o1 = readb(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090040
41 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
42 do {
43 o2 = o1;
Daniel Lezcano75160512015-11-08 22:55:12 +010044 v1 = readw(p->mapbase + TCNT);
45 v2 = readw(p->mapbase + TCNT);
46 v3 = readw(p->mapbase + TCNT);
47 o1 = readb(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090048 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
49 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
50
Yoshinori Sato2f445e02015-12-05 02:48:15 +090051 if (likely(!o1))
52 return v2;
53 else
54 return v2 + 0x10000;
Yoshinori Sato618b9022015-01-28 02:52:42 +090055}
56
57
58static irqreturn_t timer16_interrupt(int irq, void *dev_id)
59{
60 struct timer16_priv *p = (struct timer16_priv *)dev_id;
61
Yoshinori Sato2a0ff872015-12-05 02:48:14 +090062 writeb(readb(p->mapcommon + TISRC) & ~p->ovf,
63 p->mapcommon + TISRC);
Yoshinori Sato618b9022015-01-28 02:52:42 +090064 p->total_cycles += 0x10000;
65
66 return IRQ_HANDLED;
67}
68
69static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
70{
71 return container_of(cs, struct timer16_priv, cs);
72}
73
74static cycle_t timer16_clocksource_read(struct clocksource *cs)
75{
76 struct timer16_priv *p = cs_to_priv(cs);
Daniel Lezcano05de7ed2015-11-09 10:55:30 +010077 unsigned long raw, value;
Yoshinori Sato618b9022015-01-28 02:52:42 +090078
Yoshinori Sato618b9022015-01-28 02:52:42 +090079 value = p->total_cycles;
80 raw = timer16_get_counter(p);
Yoshinori Sato618b9022015-01-28 02:52:42 +090081
82 return value + raw;
83}
84
85static int timer16_enable(struct clocksource *cs)
86{
87 struct timer16_priv *p = cs_to_priv(cs);
88
89 WARN_ON(p->cs_enabled);
90
91 p->total_cycles = 0;
Daniel Lezcano75160512015-11-08 22:55:12 +010092 writew(0x0000, p->mapbase + TCNT);
93 writeb(0x83, p->mapbase + TCR);
94 writeb(readb(p->mapcommon + TSTR) | p->enb,
Yoshinori Sato618b9022015-01-28 02:52:42 +090095 p->mapcommon + TSTR);
Yoshinori Sato2a0ff872015-12-05 02:48:14 +090096 writeb(readb(p->mapcommon + TISRC) | p->ovie,
97 p->mapcommon + TSTR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090098
99 p->cs_enabled = true;
100 return 0;
101}
102
103static void timer16_disable(struct clocksource *cs)
104{
105 struct timer16_priv *p = cs_to_priv(cs);
106
107 WARN_ON(!p->cs_enabled);
108
Daniel Lezcano75160512015-11-08 22:55:12 +0100109 writeb(readb(p->mapcommon + TSTR) & ~p->enb,
Yoshinori Sato618b9022015-01-28 02:52:42 +0900110 p->mapcommon + TSTR);
111
112 p->cs_enabled = false;
113}
114
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900115static struct timer16_priv timer16_priv = {
116 .cs = {
117 .name = "h8300_16timer",
118 .rating = 200,
119 .read = timer16_clocksource_read,
120 .enable = timer16_enable,
121 .disable = timer16_disable,
122 .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
123 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
124 },
125};
126
Yoshinori Sato618b9022015-01-28 02:52:42 +0900127#define REG_CH 0
128#define REG_COMM 1
129
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900130static void __init h8300_16timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900131{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900132 void __iomem *base[2];
Yoshinori Sato618b9022015-01-28 02:52:42 +0900133 int ret, irq;
134 unsigned int ch;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900135 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900136
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900137 clk = of_clk_get(node, 0);
138 if (IS_ERR(clk)) {
139 pr_err("failed to get clock for clocksource\n");
140 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900141 }
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900142
143 base[REG_CH] = of_iomap(node, 0);
144 if (!base[REG_CH]) {
145 pr_err("failed to map registers for clocksource\n");
146 goto free_clk;
147 }
148
149 base[REG_COMM] = of_iomap(node, 1);
150 if (!base[REG_COMM]) {
151 pr_err("failed to map registers for clocksource\n");
152 goto unmap_ch;
153 }
154
155 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano5019c902015-11-09 10:52:35 +0100156 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900157 pr_err("failed to get irq for clockevent\n");
158 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900159 }
160
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900161 of_property_read_u32(node, "renesas,channel", &ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900162
Daniel Lezcano75160512015-11-08 22:55:12 +0100163 timer16_priv.mapbase = base[REG_CH];
164 timer16_priv.mapcommon = base[REG_COMM];
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900165 timer16_priv.enb = 1 << ch;
Yoshinori Sato2a0ff872015-12-05 02:48:14 +0900166 timer16_priv.ovf = 1 << ch;
167 timer16_priv.ovie = 1 << (4 + ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900168
169 ret = request_irq(irq, timer16_interrupt,
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900170 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900171 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900172 pr_err("failed to request irq %d of clocksource\n", irq);
173 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900174 }
175
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900176 clocksource_register_hz(&timer16_priv.cs,
177 clk_get_rate(timer16_priv.clk) / 8);
178 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900179
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900180unmap_comm:
181 iounmap(base[REG_COMM]);
182unmap_ch:
183 iounmap(base[REG_CH]);
184free_clk:
185 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900186}
187
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900188CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer", h8300_16timer_init);