Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * H8/300 16bit Timer driver
4 *
5 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
6 */
7
8#include <linux/interrupt.h>
9#include <linux/init.h>
10#include <linux/clocksource.h>
11#include <linux/clk.h>
12#include <linux/io.h>
13#include <linux/of.h>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16
17#define TSTR 0
18#define TISRC 6
19
20#define TCR 0
21#define TCNT 2
22
23#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
24#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
25
26struct timer16_priv {
27 struct clocksource cs;
28 unsigned long total_cycles;
29 void __iomem *mapbase;
30 void __iomem *mapcommon;
31 unsigned short cs_enabled;
32 unsigned char enb;
33 unsigned char ovf;
34 unsigned char ovie;
35};
36
37static unsigned long timer16_get_counter(struct timer16_priv *p)
38{
39 unsigned short v1, v2, v3;
40 unsigned char o1, o2;
41
42 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
43
44 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
45 do {
46 o2 = o1;
47 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;
51 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
52 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
53
54 if (likely(!o1))
55 return v2;
56 else
57 return v2 + 0x10000;
58}
59
60
61static irqreturn_t timer16_interrupt(int irq, void *dev_id)
62{
63 struct timer16_priv *p = (struct timer16_priv *)dev_id;
64
65 bclr(p->ovf, p->mapcommon + TISRC);
66 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
76static u64 timer16_clocksource_read(struct clocksource *cs)
77{
78 struct timer16_priv *p = cs_to_priv(cs);
79 unsigned long raw, value;
80
81 value = p->total_cycles;
82 raw = timer16_get_counter(p);
83
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;
94 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);
98
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
109 bclr(p->ovie, p->mapcommon + TISRC);
110 bclr(p->enb, p->mapcommon + TSTR);
111
112 p->cs_enabled = false;
113}
114
115static 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
127#define REG_CH 0
128#define REG_COMM 1
129
130static int __init h8300_16timer_init(struct device_node *node)
131{
132 void __iomem *base[2];
133 int ret, irq;
134 unsigned int ch;
135 struct clk *clk;
136
137 clk = of_clk_get(node, 0);
138 if (IS_ERR(clk)) {
139 pr_err("failed to get clock for clocksource\n");
140 return PTR_ERR(clk);
141 }
142
143 ret = -ENXIO;
144 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
156 ret = -EINVAL;
157 irq = irq_of_parse_and_map(node, 0);
158 if (!irq) {
159 pr_err("failed to get irq for clockevent\n");
160 goto unmap_comm;
161 }
162
163 of_property_read_u32(node, "renesas,channel", &ch);
164
165 timer16_priv.mapbase = base[REG_CH];
166 timer16_priv.mapcommon = base[REG_COMM];
167 timer16_priv.enb = ch;
168 timer16_priv.ovf = ch;
169 timer16_priv.ovie = 4 + ch;
170
171 ret = request_irq(irq, timer16_interrupt,
172 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
173 if (ret < 0) {
174 pr_err("failed to request irq %d of clocksource\n", irq);
175 goto unmap_comm;
176 }
177
178 clocksource_register_hz(&timer16_priv.cs,
179 clk_get_rate(clk) / 8);
180 return 0;
181
182unmap_comm:
183 iounmap(base[REG_COMM]);
184unmap_ch:
185 iounmap(base[REG_CH]);
186free_clk:
187 clk_put(clk);
188 return ret;
189}
190
191TIMER_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",
192 h8300_16timer_init);
1/*
2 * H8/300 16bit Timer driver
3 *
4 * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
5 */
6
7#include <linux/interrupt.h>
8#include <linux/init.h>
9#include <linux/clocksource.h>
10#include <linux/clk.h>
11#include <linux/io.h>
12#include <linux/of.h>
13#include <linux/of_address.h>
14#include <linux/of_irq.h>
15
16#define TSTR 0
17#define TISRC 6
18
19#define TCR 0
20#define TCNT 2
21
22#define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
23#define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
24
25struct timer16_priv {
26 struct clocksource cs;
27 unsigned long total_cycles;
28 void __iomem *mapbase;
29 void __iomem *mapcommon;
30 unsigned short cs_enabled;
31 unsigned char enb;
32 unsigned char ovf;
33 unsigned char ovie;
34};
35
36static unsigned long timer16_get_counter(struct timer16_priv *p)
37{
38 unsigned short v1, v2, v3;
39 unsigned char o1, o2;
40
41 o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
42
43 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
44 do {
45 o2 = o1;
46 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;
50 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
51 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
52
53 if (likely(!o1))
54 return v2;
55 else
56 return v2 + 0x10000;
57}
58
59
60static irqreturn_t timer16_interrupt(int irq, void *dev_id)
61{
62 struct timer16_priv *p = (struct timer16_priv *)dev_id;
63
64 bclr(p->ovf, p->mapcommon + TISRC);
65 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 u64 timer16_clocksource_read(struct clocksource *cs)
76{
77 struct timer16_priv *p = cs_to_priv(cs);
78 unsigned long raw, value;
79
80 value = p->total_cycles;
81 raw = timer16_get_counter(p);
82
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;
93 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);
97
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
108 bclr(p->ovie, p->mapcommon + TISRC);
109 bclr(p->enb, p->mapcommon + TSTR);
110
111 p->cs_enabled = false;
112}
113
114static 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
126#define REG_CH 0
127#define REG_COMM 1
128
129static int __init h8300_16timer_init(struct device_node *node)
130{
131 void __iomem *base[2];
132 int ret, irq;
133 unsigned int ch;
134 struct clk *clk;
135
136 clk = of_clk_get(node, 0);
137 if (IS_ERR(clk)) {
138 pr_err("failed to get clock for clocksource\n");
139 return PTR_ERR(clk);
140 }
141
142 ret = -ENXIO;
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 ret = -EINVAL;
156 irq = irq_of_parse_and_map(node, 0);
157 if (!irq) {
158 pr_err("failed to get irq for clockevent\n");
159 goto unmap_comm;
160 }
161
162 of_property_read_u32(node, "renesas,channel", &ch);
163
164 timer16_priv.mapbase = base[REG_CH];
165 timer16_priv.mapcommon = base[REG_COMM];
166 timer16_priv.enb = ch;
167 timer16_priv.ovf = ch;
168 timer16_priv.ovie = 4 + ch;
169
170 ret = request_irq(irq, timer16_interrupt,
171 IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
172 if (ret < 0) {
173 pr_err("failed to request irq %d of clocksource\n", irq);
174 goto unmap_comm;
175 }
176
177 clocksource_register_hz(&timer16_priv.cs,
178 clk_get_rate(clk) / 8);
179 return 0;
180
181unmap_comm:
182 iounmap(base[REG_COMM]);
183unmap_ch:
184 iounmap(base[REG_CH]);
185free_clk:
186 clk_put(clk);
187 return ret;
188}
189
190CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",
191 h8300_16timer_init);