blob: bc48cbf6a7957198d4ecd6bf91840df350843d48 [file] [log] [blame]
Magnus Dammb9dbf952012-05-25 16:03:44 +09001/*
2 * Emma Mobile Timer Support - STI
3 *
4 * Copyright (C) 2012 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/io.h>
26#include <linux/clk.h>
27#include <linux/irq.h>
28#include <linux/err.h>
29#include <linux/delay.h>
30#include <linux/clocksource.h>
31#include <linux/clockchips.h>
32#include <linux/slab.h>
33#include <linux/module.h>
34
35enum { USER_CLOCKSOURCE, USER_CLOCKEVENT, USER_NR };
36
37struct em_sti_priv {
38 void __iomem *base;
39 struct clk *clk;
40 struct platform_device *pdev;
41 unsigned int active[USER_NR];
42 unsigned long rate;
43 raw_spinlock_t lock;
44 struct clock_event_device ced;
45 struct clocksource cs;
46};
47
48#define STI_CONTROL 0x00
49#define STI_COMPA_H 0x10
50#define STI_COMPA_L 0x14
51#define STI_COMPB_H 0x18
52#define STI_COMPB_L 0x1c
53#define STI_COUNT_H 0x20
54#define STI_COUNT_L 0x24
55#define STI_COUNT_RAW_H 0x28
56#define STI_COUNT_RAW_L 0x2c
57#define STI_SET_H 0x30
58#define STI_SET_L 0x34
59#define STI_INTSTATUS 0x40
60#define STI_INTRAWSTATUS 0x44
61#define STI_INTENSET 0x48
62#define STI_INTENCLR 0x4c
63#define STI_INTFFCLR 0x50
64
65static inline unsigned long em_sti_read(struct em_sti_priv *p, int offs)
66{
67 return ioread32(p->base + offs);
68}
69
70static inline void em_sti_write(struct em_sti_priv *p, int offs,
71 unsigned long value)
72{
73 iowrite32(value, p->base + offs);
74}
75
76static int em_sti_enable(struct em_sti_priv *p)
77{
78 int ret;
79
80 /* enable clock */
Nicolai Stange3814ae02017-02-06 22:12:01 +010081 ret = clk_enable(p->clk);
Magnus Dammb9dbf952012-05-25 16:03:44 +090082 if (ret) {
83 dev_err(&p->pdev->dev, "cannot enable clock\n");
84 return ret;
85 }
86
Magnus Dammb9dbf952012-05-25 16:03:44 +090087 /* reset the counter */
88 em_sti_write(p, STI_SET_H, 0x40000000);
89 em_sti_write(p, STI_SET_L, 0x00000000);
90
91 /* mask and clear pending interrupts */
92 em_sti_write(p, STI_INTENCLR, 3);
93 em_sti_write(p, STI_INTFFCLR, 3);
94
95 /* enable updates of counter registers */
96 em_sti_write(p, STI_CONTROL, 1);
97
98 return 0;
99}
100
101static void em_sti_disable(struct em_sti_priv *p)
102{
103 /* mask interrupts */
104 em_sti_write(p, STI_INTENCLR, 3);
105
106 /* stop clock */
Nicolai Stange3814ae02017-02-06 22:12:01 +0100107 clk_disable(p->clk);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900108}
109
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100110static u64 em_sti_count(struct em_sti_priv *p)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900111{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100112 u64 ticks;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900113 unsigned long flags;
114
115 /* the STI hardware buffers the 48-bit count, but to
116 * break it out into two 32-bit access the registers
117 * must be accessed in a certain order.
118 * Always read STI_COUNT_H before STI_COUNT_L.
119 */
120 raw_spin_lock_irqsave(&p->lock, flags);
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100121 ticks = (u64)(em_sti_read(p, STI_COUNT_H) & 0xffff) << 32;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900122 ticks |= em_sti_read(p, STI_COUNT_L);
123 raw_spin_unlock_irqrestore(&p->lock, flags);
124
125 return ticks;
126}
127
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100128static u64 em_sti_set_next(struct em_sti_priv *p, u64 next)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900129{
130 unsigned long flags;
131
132 raw_spin_lock_irqsave(&p->lock, flags);
133
134 /* mask compare A interrupt */
135 em_sti_write(p, STI_INTENCLR, 1);
136
137 /* update compare A value */
138 em_sti_write(p, STI_COMPA_H, next >> 32);
139 em_sti_write(p, STI_COMPA_L, next & 0xffffffff);
140
141 /* clear compare A interrupt source */
142 em_sti_write(p, STI_INTFFCLR, 1);
143
144 /* unmask compare A interrupt */
145 em_sti_write(p, STI_INTENSET, 1);
146
147 raw_spin_unlock_irqrestore(&p->lock, flags);
148
149 return next;
150}
151
152static irqreturn_t em_sti_interrupt(int irq, void *dev_id)
153{
154 struct em_sti_priv *p = dev_id;
155
156 p->ced.event_handler(&p->ced);
157 return IRQ_HANDLED;
158}
159
160static int em_sti_start(struct em_sti_priv *p, unsigned int user)
161{
162 unsigned long flags;
163 int used_before;
164 int ret = 0;
165
166 raw_spin_lock_irqsave(&p->lock, flags);
167 used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
168 if (!used_before)
169 ret = em_sti_enable(p);
170
171 if (!ret)
172 p->active[user] = 1;
173 raw_spin_unlock_irqrestore(&p->lock, flags);
174
175 return ret;
176}
177
178static void em_sti_stop(struct em_sti_priv *p, unsigned int user)
179{
180 unsigned long flags;
181 int used_before, used_after;
182
183 raw_spin_lock_irqsave(&p->lock, flags);
184 used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
185 p->active[user] = 0;
186 used_after = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
187
188 if (used_before && !used_after)
189 em_sti_disable(p);
190 raw_spin_unlock_irqrestore(&p->lock, flags);
191}
192
193static struct em_sti_priv *cs_to_em_sti(struct clocksource *cs)
194{
195 return container_of(cs, struct em_sti_priv, cs);
196}
197
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100198static u64 em_sti_clocksource_read(struct clocksource *cs)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900199{
200 return em_sti_count(cs_to_em_sti(cs));
201}
202
203static int em_sti_clocksource_enable(struct clocksource *cs)
204{
Magnus Dammb9dbf952012-05-25 16:03:44 +0900205 struct em_sti_priv *p = cs_to_em_sti(cs);
206
Nicolai Stange4e53aa22017-02-06 22:12:02 +0100207 return em_sti_start(p, USER_CLOCKSOURCE);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900208}
209
210static void em_sti_clocksource_disable(struct clocksource *cs)
211{
212 em_sti_stop(cs_to_em_sti(cs), USER_CLOCKSOURCE);
213}
214
215static void em_sti_clocksource_resume(struct clocksource *cs)
216{
217 em_sti_clocksource_enable(cs);
218}
219
220static int em_sti_register_clocksource(struct em_sti_priv *p)
221{
222 struct clocksource *cs = &p->cs;
223
Magnus Dammb9dbf952012-05-25 16:03:44 +0900224 cs->name = dev_name(&p->pdev->dev);
225 cs->rating = 200;
226 cs->read = em_sti_clocksource_read;
227 cs->enable = em_sti_clocksource_enable;
228 cs->disable = em_sti_clocksource_disable;
229 cs->suspend = em_sti_clocksource_disable;
230 cs->resume = em_sti_clocksource_resume;
231 cs->mask = CLOCKSOURCE_MASK(48);
232 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
233
234 dev_info(&p->pdev->dev, "used as clock source\n");
235
Nicolai Stange4e53aa22017-02-06 22:12:02 +0100236 clocksource_register_hz(cs, p->rate);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900237 return 0;
238}
239
240static struct em_sti_priv *ced_to_em_sti(struct clock_event_device *ced)
241{
242 return container_of(ced, struct em_sti_priv, ced);
243}
244
Viresh Kumar75f94062015-06-12 13:30:17 +0530245static int em_sti_clock_event_shutdown(struct clock_event_device *ced)
246{
247 struct em_sti_priv *p = ced_to_em_sti(ced);
248 em_sti_stop(p, USER_CLOCKEVENT);
249 return 0;
250}
251
252static int em_sti_clock_event_set_oneshot(struct clock_event_device *ced)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900253{
254 struct em_sti_priv *p = ced_to_em_sti(ced);
255
Viresh Kumar75f94062015-06-12 13:30:17 +0530256 dev_info(&p->pdev->dev, "used for oneshot clock events\n");
257 em_sti_start(p, USER_CLOCKEVENT);
Viresh Kumar75f94062015-06-12 13:30:17 +0530258 return 0;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900259}
260
261static int em_sti_clock_event_next(unsigned long delta,
262 struct clock_event_device *ced)
263{
264 struct em_sti_priv *p = ced_to_em_sti(ced);
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100265 u64 next;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900266 int safe;
267
268 next = em_sti_set_next(p, em_sti_count(p) + delta);
269 safe = em_sti_count(p) < (next - 1);
270
271 return !safe;
272}
273
274static void em_sti_register_clockevent(struct em_sti_priv *p)
275{
276 struct clock_event_device *ced = &p->ced;
277
Magnus Dammb9dbf952012-05-25 16:03:44 +0900278 ced->name = dev_name(&p->pdev->dev);
279 ced->features = CLOCK_EVT_FEAT_ONESHOT;
280 ced->rating = 200;
Magnus Damm2199a552013-09-18 15:01:16 -0500281 ced->cpumask = cpu_possible_mask;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900282 ced->set_next_event = em_sti_clock_event_next;
Viresh Kumar75f94062015-06-12 13:30:17 +0530283 ced->set_state_shutdown = em_sti_clock_event_shutdown;
284 ced->set_state_oneshot = em_sti_clock_event_set_oneshot;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900285
286 dev_info(&p->pdev->dev, "used for clock events\n");
287
Nicolai Stange4e53aa22017-02-06 22:12:02 +0100288 clockevents_config_and_register(ced, p->rate, 2, 0xffffffff);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900289}
290
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800291static int em_sti_probe(struct platform_device *pdev)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900292{
293 struct em_sti_priv *p;
294 struct resource *res;
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200295 int irq;
Nicolai Stange3814ae02017-02-06 22:12:01 +0100296 int ret;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900297
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200298 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
Jingoo Han39dd5672014-05-22 14:05:06 +0200299 if (p == NULL)
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200300 return -ENOMEM;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900301
302 p->pdev = pdev;
303 platform_set_drvdata(pdev, p);
304
Magnus Dammb9dbf952012-05-25 16:03:44 +0900305 irq = platform_get_irq(pdev, 0);
306 if (irq < 0) {
307 dev_err(&pdev->dev, "failed to get irq\n");
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200308 return -EINVAL;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900309 }
310
311 /* map memory, let base point to the STI instance */
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 p->base = devm_ioremap_resource(&pdev->dev, res);
314 if (IS_ERR(p->base))
315 return PTR_ERR(p->base);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900316
Nicolai Stange3814ae02017-02-06 22:12:01 +0100317 if (devm_request_irq(&pdev->dev, irq, em_sti_interrupt,
318 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
319 dev_name(&pdev->dev), p)) {
320 dev_err(&pdev->dev, "failed to request low IRQ\n");
321 return -ENOENT;
322 }
323
Magnus Dammb9dbf952012-05-25 16:03:44 +0900324 /* get hold of clock */
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200325 p->clk = devm_clk_get(&pdev->dev, "sclk");
Magnus Dammb9dbf952012-05-25 16:03:44 +0900326 if (IS_ERR(p->clk)) {
327 dev_err(&pdev->dev, "cannot get clock\n");
Laurent Pinchart1745e6962013-07-30 16:24:37 +0200328 return PTR_ERR(p->clk);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900329 }
330
Nicolai Stange3814ae02017-02-06 22:12:01 +0100331 ret = clk_prepare(p->clk);
332 if (ret < 0) {
333 dev_err(&pdev->dev, "cannot prepare clock\n");
334 return ret;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900335 }
336
Nicolai Stange4e53aa22017-02-06 22:12:02 +0100337 ret = clk_enable(p->clk);
338 if (ret < 0) {
339 dev_err(&p->pdev->dev, "cannot enable clock\n");
340 clk_unprepare(p->clk);
341 return ret;
342 }
343 p->rate = clk_get_rate(p->clk);
344 clk_disable(p->clk);
345
Magnus Dammb9dbf952012-05-25 16:03:44 +0900346 raw_spin_lock_init(&p->lock);
347 em_sti_register_clockevent(p);
348 em_sti_register_clocksource(p);
349 return 0;
Magnus Dammb9dbf952012-05-25 16:03:44 +0900350}
351
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800352static int em_sti_remove(struct platform_device *pdev)
Magnus Dammb9dbf952012-05-25 16:03:44 +0900353{
354 return -EBUSY; /* cannot unregister clockevent and clocksource */
355}
356
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800357static const struct of_device_id em_sti_dt_ids[] = {
Magnus Dammfc0830f2012-05-09 23:39:50 +0900358 { .compatible = "renesas,em-sti", },
359 {},
360};
361MODULE_DEVICE_TABLE(of, em_sti_dt_ids);
362
Magnus Dammb9dbf952012-05-25 16:03:44 +0900363static struct platform_driver em_sti_device_driver = {
364 .probe = em_sti_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -0800365 .remove = em_sti_remove,
Magnus Dammb9dbf952012-05-25 16:03:44 +0900366 .driver = {
367 .name = "em_sti",
Magnus Dammfc0830f2012-05-09 23:39:50 +0900368 .of_match_table = em_sti_dt_ids,
Magnus Dammb9dbf952012-05-25 16:03:44 +0900369 }
370};
371
Simon Horman09acc3a2013-03-05 15:40:42 +0900372static int __init em_sti_init(void)
373{
374 return platform_driver_register(&em_sti_device_driver);
375}
376
377static void __exit em_sti_exit(void)
378{
379 platform_driver_unregister(&em_sti_device_driver);
380}
381
382subsys_initcall(em_sti_init);
383module_exit(em_sti_exit);
Magnus Dammb9dbf952012-05-25 16:03:44 +0900384
385MODULE_AUTHOR("Magnus Damm");
386MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver");
387MODULE_LICENSE("GPL v2");