blob: 86ca91451b2ece3091fe535d940ace7a8bd46ef7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yoshinori Sato618b9022015-01-28 02:52:42 +09002/*
3 * H8/300 16bit Timer driver
4 *
5 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
6 */
7
Yoshinori Sato618b9022015-01-28 02:52:42 +09008#include <linux/interrupt.h>
9#include <linux/init.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090010#include <linux/clocksource.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090011#include <linux/clk.h>
12#include <linux/io.h>
13#include <linux/of.h>
Yoshinori Sato4633f4c2015-11-07 01:31:44 +090014#include <linux/of_address.h>
15#include <linux/of_irq.h>
Yoshinori Sato618b9022015-01-28 02:52:42 +090016
Yoshinori Sato618b9022015-01-28 02:52:42 +090017#define TSTR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090018#define TISRC 6
19
20#define TCR 0
Yoshinori Sato618b9022015-01-28 02:52:42 +090021#define TCNT 2
Yoshinori Sato618b9022015-01-28 02:52:42 +090022
Yoshinori Satod33f2502015-12-05 02:48:18 +090023#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
24#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
25
Yoshinori Sato618b9022015-01-28 02:52:42 +090026struct timer16_priv {
Yoshinori Sato618b9022015-01-28 02:52:42 +090027 struct clocksource cs;
Yoshinori Sato618b9022015-01-28 02:52:42 +090028 unsigned long total_cycles;
Daniel Lezcano75160512015-11-08 22:55:12 +010029 void __iomem *mapbase;
30 void __iomem *mapcommon;
Yoshinori Sato618b9022015-01-28 02:52:42 +090031 unsigned short cs_enabled;
32 unsigned char enb;
Yoshinori Sato618b9022015-01-28 02:52:42 +090033 unsigned char ovf;
Yoshinori Sato2a0ff872015-12-05 02:48:14 +090034 unsigned char ovie;
Yoshinori Sato618b9022015-01-28 02:52:42 +090035};
36
37static unsigned long timer16_get_counter(struct timer16_priv *p)
38{
Yoshinori Satod33f2502015-12-05 02:48:18 +090039 unsigned short v1, v2, v3;
40 unsigned char o1, o2;
Yoshinori Sato618b9022015-01-28 02:52:42 +090041
Yoshinori Satod33f2502015-12-05 02:48:18 +090042 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090043
44 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
45 do {
46 o2 = o1;
Yoshinori Satod33f2502015-12-05 02:48:18 +090047 v1 = ioread16be(p->mapbase + TCNT);
48 v2 = ioread16be(p->mapbase + TCNT);
49 v3 = ioread16be(p->mapbase + TCNT);
50 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
Yoshinori Sato618b9022015-01-28 02:52:42 +090051 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
52 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
53
Yoshinori Sato2f445e02015-12-05 02:48:15 +090054 if (likely(!o1))
55 return v2;
56 else
57 return v2 + 0x10000;
Yoshinori Sato618b9022015-01-28 02:52:42 +090058}
59
60
61static irqreturn_t timer16_interrupt(int irq, void *dev_id)
62{
63 struct timer16_priv *p = (struct timer16_priv *)dev_id;
64
Yoshinori Satod33f2502015-12-05 02:48:18 +090065 bclr(p->ovf, p->mapcommon + TISRC);
Yoshinori Sato618b9022015-01-28 02:52:42 +090066 p->total_cycles += 0x10000;
67
68 return IRQ_HANDLED;
69}
70
71static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
72{
73 return container_of(cs, struct timer16_priv, cs);
74}
75
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010076static u64 timer16_clocksource_read(struct clocksource *cs)
Yoshinori Sato618b9022015-01-28 02:52:42 +090077{
78 struct timer16_priv *p = cs_to_priv(cs);
Daniel Lezcano05de7ed2015-11-09 10:55:30 +010079 unsigned long raw, value;
Yoshinori Sato618b9022015-01-28 02:52:42 +090080
Yoshinori Sato618b9022015-01-28 02:52:42 +090081 value = p->total_cycles;
82 raw = timer16_get_counter(p);
Yoshinori Sato618b9022015-01-28 02:52:42 +090083
84 return value + raw;
85}
86
87static int timer16_enable(struct clocksource *cs)
88{
89 struct timer16_priv *p = cs_to_priv(cs);
90
91 WARN_ON(p->cs_enabled);
92
93 p->total_cycles = 0;
Yoshinori Satod33f2502015-12-05 02:48:18 +090094 iowrite16be(0x0000, p->mapbase + TCNT);
95 iowrite8(0x83, p->mapbase + TCR);
96 bset(p->ovie, p->mapcommon + TISRC);
97 bset(p->enb, 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
Yoshinori Satod33f2502015-12-05 02:48:18 +0900109 bclr(p->ovie, p->mapcommon + TISRC);
110 bclr(p->enb, p->mapcommon + TSTR);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900111
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
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200130static int __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");
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200140 return PTR_ERR(clk);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900141 }
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900142
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200143 ret = -ENXIO;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900144 base[REG_CH] = of_iomap(node, 0);
145 if (!base[REG_CH]) {
146 pr_err("failed to map registers for clocksource\n");
147 goto free_clk;
148 }
149
150 base[REG_COMM] = of_iomap(node, 1);
151 if (!base[REG_COMM]) {
152 pr_err("failed to map registers for clocksource\n");
153 goto unmap_ch;
154 }
155
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200156 ret = -EINVAL;
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900157 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano5019c902015-11-09 10:52:35 +0100158 if (!irq) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900159 pr_err("failed to get irq for clockevent\n");
160 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900161 }
162
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900163 of_property_read_u32(node, "renesas,channel", &ch);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900164
Daniel Lezcano75160512015-11-08 22:55:12 +0100165 timer16_priv.mapbase = base[REG_CH];
166 timer16_priv.mapcommon = base[REG_COMM];
Yoshinori Satod33f2502015-12-05 02:48:18 +0900167 timer16_priv.enb = ch;
168 timer16_priv.ovf = ch;
169 timer16_priv.ovie = 4 + ch;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900170
171 ret = request_irq(irq, timer16_interrupt,
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900172 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
Yoshinori Sato618b9022015-01-28 02:52:42 +0900173 if (ret < 0) {
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900174 pr_err("failed to request irq %d of clocksource\n", irq);
175 goto unmap_comm;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900176 }
177
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900178 clocksource_register_hz(&timer16_priv.cs,
Yoshinori Satod33f2502015-12-05 02:48:18 +0900179 clk_get_rate(clk) / 8);
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200180 return 0;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900181
Yoshinori Sato4633f4c2015-11-07 01:31:44 +0900182unmap_comm:
183 iounmap(base[REG_COMM]);
184unmap_ch:
185 iounmap(base[REG_CH]);
186free_clk:
187 clk_put(clk);
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200188 return ret;
Yoshinori Sato618b9022015-01-28 02:52:42 +0900189}
190
Daniel Lezcano17273392017-05-26 16:56:11 +0200191TIMER_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",
Daniel Lezcanoeacf2092016-06-06 17:56:21 +0200192 h8300_16timer_init);