blob: 75c44079b3454513b2f6a788872b4c579c7f27b8 [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
Yoshinori Satod33f2502015-12-05 02:48:18 +090022#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
23#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
24
Yoshinori Sato618b9022015-01-28 02:52:42 +090025struct timer16_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090026 struct clocksource cs;
Yoshinori Sato618b9022015-01-28 02:52:42 +090027 unsigned long total_cycles;
Daniel Lezcano75160512015-11-08 22:55:12 +010028 void __iomem *mapbase;
29 void __iomem *mapcommon;
Yoshinori Sato618b9022015-01-28 02:52:42 +090030 unsigned short cs_enabled;
31 unsigned char enb;
Yoshinori Sato618b9022015-01-28 02:52:42 +090032 unsigned char ovf;
Yoshinori Sato2a0ff872015-12-05 02:48:14 +090033 unsigned char ovie;
Yoshinori Sato618b9022015-01-28 02:52:42 +090034};
35
36static unsigned long timer16_get_counter(struct timer16_priv *p)
37{
Yoshinori Satod33f2502015-12-05 02:48:18 +090038 unsigned short v1, v2, v3;
39 unsigned char o1, o2;
Yoshinori Sato618b9022015-01-28 02:52:42 +090040
Yoshinori Satod33f2502015-12-05 02:48:18 +090041 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090042
43 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
44 do {
45 o2 = o1;
Yoshinori Satod33f2502015-12-05 02:48:18 +090046 v1 = ioread16be(p->mapbase + TCNT);
47 v2 = ioread16be(p->mapbase + TCNT);
48 v3 = ioread16be(p->mapbase + TCNT);
49 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090050 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
51 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
52
Yoshinori Sato2f445e02015-12-05 02:48:15 +090053 if (likely(!o1))
54 return v2;
55 else
56 return v2 + 0x10000;
Yoshinori Sato618b9022015-01-28 02:52:42 +090057}
58
59
60static irqreturn_t timer16_interrupt(int irq, void *dev_id)
61{
62 struct timer16_priv *p = (struct timer16_priv *)dev_id;
63
Yoshinori Satod33f2502015-12-05 02:48:18 +090064 bclr(p->ovf, p->mapcommon + TISRC);
Yoshinori Sato618b9022015-01-28 02:52:42 +090065 p->total_cycles += 0x10000;
66
67 return IRQ_HANDLED;
68}
69
70static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
71{
72 return container_of(cs, struct timer16_priv, cs);
73}
74
75static cycle_t timer16_clocksource_read(struct clocksource *cs)
76{
77 struct timer16_priv *p = cs_to_priv(cs);
Daniel Lezcano05de7ed2015-11-09 10:55:30 +010078 unsigned long raw, value;
Yoshinori Sato618b9022015-01-28 02:52:42 +090079
Yoshinori Sato618b9022015-01-28 02:52:42 +090080 value = p->total_cycles;
81 raw = timer16_get_counter(p);
Yoshinori Sato618b9022015-01-28 02:52:42 +090082
83 return value + raw;
84}
85
86static int timer16_enable(struct clocksource *cs)
87{
88 struct timer16_priv *p = cs_to_priv(cs);
89
90 WARN_ON(p->cs_enabled);
91
92 p->total_cycles = 0;
Yoshinori Satod33f2502015-12-05 02:48:18 +090093 iowrite16be(0x0000, p->mapbase + TCNT);
94 iowrite8(0x83, p->mapbase + TCR);
95 bset(p->ovie, p->mapcommon + TISRC);
96 bset(p->enb, p->mapcommon + TSTR);
Yoshinori Sato618b9022015-01-28 02:52:42 +090097
98 p->cs_enabled = true;
99 return 0;
100}
101
102static void timer16_disable(struct clocksource *cs)
103{
104 struct timer16_priv *p = cs_to_priv(cs);
105
106 WARN_ON(!p->cs_enabled);
107
Yoshinori Satod33f2502015-12-05 02:48:18 +0900108 bclr(p->ovie, p->mapcommon + TISRC);
109 bclr(p->enb, p->mapcommon + TSTR);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900110
111 p->cs_enabled = false;
112}
113
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900114static struct timer16_priv timer16_priv = {
115 .cs = {
116 .name = "h8300_16timer",
117 .rating = 200,
118 .read = timer16_clocksource_read,
119 .enable = timer16_enable,
120 .disable = timer16_disable,
121 .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
122 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
123 },
124};
125
Yoshinori Sato618b9022015-01-28 02:52:42 +0900126#define REG_CH 0
127#define REG_COMM 1
128
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900129static void __init h8300_16timer_init(struct device_node *node)
Yoshinori Sato618b9022015-01-28 02:52:42 +0900130{
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900131 void __iomem *base[2];
Yoshinori Sato618b9022015-01-28 02:52:42 +0900132 int ret, irq;
133 unsigned int ch;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900134 struct clk *clk;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900135
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900136 clk = of_clk_get(node, 0);
137 if (IS_ERR(clk)) {
138 pr_err("failed to get clock for clocksource\n");
139 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900140 }
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900141
142 base[REG_CH] = of_iomap(node, 0);
143 if (!base[REG_CH]) {
144 pr_err("failed to map registers for clocksource\n");
145 goto free_clk;
146 }
147
148 base[REG_COMM] = of_iomap(node, 1);
149 if (!base[REG_COMM]) {
150 pr_err("failed to map registers for clocksource\n");
151 goto unmap_ch;
152 }
153
154 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano5019c902015-11-09 10:52:35 +0100155 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900156 pr_err("failed to get irq for clockevent\n");
157 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900158 }
159
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900160 of_property_read_u32(node, "renesas,channel", &ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900161
Daniel Lezcano75160512015-11-08 22:55:12 +0100162 timer16_priv.mapbase = base[REG_CH];
163 timer16_priv.mapcommon = base[REG_COMM];
Yoshinori Satod33f2502015-12-05 02:48:18 +0900164 timer16_priv.enb = ch;
165 timer16_priv.ovf = ch;
166 timer16_priv.ovie = 4 + ch;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900167
168 ret = request_irq(irq, timer16_interrupt,
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900169 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900170 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900171 pr_err("failed to request irq %d of clocksource\n", irq);
172 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900173 }
174
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900175 clocksource_register_hz(&timer16_priv.cs,
Yoshinori Satod33f2502015-12-05 02:48:18 +0900176 clk_get_rate(clk) / 8);
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900177 return;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900178
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900179unmap_comm:
180 iounmap(base[REG_COMM]);
181unmap_ch:
182 iounmap(base[REG_CH]);
183free_clk:
184 clk_put(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900185}
186
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900187CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer", h8300_16timer_init);