2 * Emma Mobile Timer Support - STI
4 * Copyright (C) 2012 Magnus Damm
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
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.
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
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>
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>
35 enum { USER_CLOCKSOURCE, USER_CLOCKEVENT, USER_NR };
40 struct platform_device *pdev;
41 unsigned int active[USER_NR];
44 struct clock_event_device ced;
45 struct clocksource cs;
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
65 static inline unsigned long em_sti_read(struct em_sti_priv *p, int offs)
67 return ioread32(p->base + offs);
70 static inline void em_sti_write(struct em_sti_priv *p, int offs,
73 iowrite32(value, p->base + offs);
76 static int em_sti_enable(struct em_sti_priv *p)
81 ret = clk_enable(p->clk);
83 dev_err(&p->pdev->dev, "cannot enable clock\n");
87 /* configure channel, periodic mode and maximum timeout */
88 p->rate = clk_get_rate(p->clk);
90 /* reset the counter */
91 em_sti_write(p, STI_SET_H, 0x40000000);
92 em_sti_write(p, STI_SET_L, 0x00000000);
94 /* mask and clear pending interrupts */
95 em_sti_write(p, STI_INTENCLR, 3);
96 em_sti_write(p, STI_INTFFCLR, 3);
98 /* enable updates of counter registers */
99 em_sti_write(p, STI_CONTROL, 1);
104 static void em_sti_disable(struct em_sti_priv *p)
106 /* mask interrupts */
107 em_sti_write(p, STI_INTENCLR, 3);
113 static cycle_t em_sti_count(struct em_sti_priv *p)
118 /* the STI hardware buffers the 48-bit count, but to
119 * break it out into two 32-bit access the registers
120 * must be accessed in a certain order.
121 * Always read STI_COUNT_H before STI_COUNT_L.
123 raw_spin_lock_irqsave(&p->lock, flags);
124 ticks = (cycle_t)(em_sti_read(p, STI_COUNT_H) & 0xffff) << 32;
125 ticks |= em_sti_read(p, STI_COUNT_L);
126 raw_spin_unlock_irqrestore(&p->lock, flags);
131 static cycle_t em_sti_set_next(struct em_sti_priv *p, cycle_t next)
135 raw_spin_lock_irqsave(&p->lock, flags);
137 /* mask compare A interrupt */
138 em_sti_write(p, STI_INTENCLR, 1);
140 /* update compare A value */
141 em_sti_write(p, STI_COMPA_H, next >> 32);
142 em_sti_write(p, STI_COMPA_L, next & 0xffffffff);
144 /* clear compare A interrupt source */
145 em_sti_write(p, STI_INTFFCLR, 1);
147 /* unmask compare A interrupt */
148 em_sti_write(p, STI_INTENSET, 1);
150 raw_spin_unlock_irqrestore(&p->lock, flags);
155 static irqreturn_t em_sti_interrupt(int irq, void *dev_id)
157 struct em_sti_priv *p = dev_id;
159 p->ced.event_handler(&p->ced);
163 static int em_sti_start(struct em_sti_priv *p, unsigned int user)
169 raw_spin_lock_irqsave(&p->lock, flags);
170 used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
172 ret = em_sti_enable(p);
176 raw_spin_unlock_irqrestore(&p->lock, flags);
181 static void em_sti_stop(struct em_sti_priv *p, unsigned int user)
184 int used_before, used_after;
186 raw_spin_lock_irqsave(&p->lock, flags);
187 used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
189 used_after = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
191 if (used_before && !used_after)
193 raw_spin_unlock_irqrestore(&p->lock, flags);
196 static struct em_sti_priv *cs_to_em_sti(struct clocksource *cs)
198 return container_of(cs, struct em_sti_priv, cs);
201 static cycle_t em_sti_clocksource_read(struct clocksource *cs)
203 return em_sti_count(cs_to_em_sti(cs));
206 static int em_sti_clocksource_enable(struct clocksource *cs)
209 struct em_sti_priv *p = cs_to_em_sti(cs);
211 ret = em_sti_start(p, USER_CLOCKSOURCE);
213 __clocksource_updatefreq_hz(cs, p->rate);
217 static void em_sti_clocksource_disable(struct clocksource *cs)
219 em_sti_stop(cs_to_em_sti(cs), USER_CLOCKSOURCE);
222 static void em_sti_clocksource_resume(struct clocksource *cs)
224 em_sti_clocksource_enable(cs);
227 static int em_sti_register_clocksource(struct em_sti_priv *p)
229 struct clocksource *cs = &p->cs;
231 memset(cs, 0, sizeof(*cs));
232 cs->name = dev_name(&p->pdev->dev);
234 cs->read = em_sti_clocksource_read;
235 cs->enable = em_sti_clocksource_enable;
236 cs->disable = em_sti_clocksource_disable;
237 cs->suspend = em_sti_clocksource_disable;
238 cs->resume = em_sti_clocksource_resume;
239 cs->mask = CLOCKSOURCE_MASK(48);
240 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
242 dev_info(&p->pdev->dev, "used as clock source\n");
244 /* Register with dummy 1 Hz value, gets updated in ->enable() */
245 clocksource_register_hz(cs, 1);
249 static struct em_sti_priv *ced_to_em_sti(struct clock_event_device *ced)
251 return container_of(ced, struct em_sti_priv, ced);
254 static void em_sti_clock_event_mode(enum clock_event_mode mode,
255 struct clock_event_device *ced)
257 struct em_sti_priv *p = ced_to_em_sti(ced);
259 /* deal with old setting first */
261 case CLOCK_EVT_MODE_ONESHOT:
262 em_sti_stop(p, USER_CLOCKEVENT);
269 case CLOCK_EVT_MODE_ONESHOT:
270 dev_info(&p->pdev->dev, "used for oneshot clock events\n");
271 em_sti_start(p, USER_CLOCKEVENT);
272 clockevents_config(&p->ced, p->rate);
274 case CLOCK_EVT_MODE_SHUTDOWN:
275 case CLOCK_EVT_MODE_UNUSED:
276 em_sti_stop(p, USER_CLOCKEVENT);
283 static int em_sti_clock_event_next(unsigned long delta,
284 struct clock_event_device *ced)
286 struct em_sti_priv *p = ced_to_em_sti(ced);
290 next = em_sti_set_next(p, em_sti_count(p) + delta);
291 safe = em_sti_count(p) < (next - 1);
296 static void em_sti_register_clockevent(struct em_sti_priv *p)
298 struct clock_event_device *ced = &p->ced;
300 memset(ced, 0, sizeof(*ced));
301 ced->name = dev_name(&p->pdev->dev);
302 ced->features = CLOCK_EVT_FEAT_ONESHOT;
304 ced->cpumask = cpumask_of(0);
305 ced->set_next_event = em_sti_clock_event_next;
306 ced->set_mode = em_sti_clock_event_mode;
308 dev_info(&p->pdev->dev, "used for clock events\n");
310 /* Register with dummy 1 Hz value, gets updated in ->set_mode() */
311 clockevents_config_and_register(ced, 1, 2, 0xffffffff);
314 static int __devinit em_sti_probe(struct platform_device *pdev)
316 struct em_sti_priv *p;
317 struct resource *res;
320 p = kzalloc(sizeof(*p), GFP_KERNEL);
322 dev_err(&pdev->dev, "failed to allocate driver data\n");
328 platform_set_drvdata(pdev, p);
330 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
332 dev_err(&pdev->dev, "failed to get I/O memory\n");
337 irq = platform_get_irq(pdev, 0);
339 dev_err(&pdev->dev, "failed to get irq\n");
344 /* map memory, let base point to the STI instance */
345 p->base = ioremap_nocache(res->start, resource_size(res));
346 if (p->base == NULL) {
347 dev_err(&pdev->dev, "failed to remap I/O memory\n");
352 /* get hold of clock */
353 p->clk = clk_get(&pdev->dev, "sclk");
354 if (IS_ERR(p->clk)) {
355 dev_err(&pdev->dev, "cannot get clock\n");
356 ret = PTR_ERR(p->clk);
360 if (request_irq(irq, em_sti_interrupt,
361 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
362 dev_name(&pdev->dev), p)) {
363 dev_err(&pdev->dev, "failed to request low IRQ\n");
368 raw_spin_lock_init(&p->lock);
369 em_sti_register_clockevent(p);
370 em_sti_register_clocksource(p);
382 static int __devexit em_sti_remove(struct platform_device *pdev)
384 return -EBUSY; /* cannot unregister clockevent and clocksource */
387 static struct platform_driver em_sti_device_driver = {
388 .probe = em_sti_probe,
389 .remove = __devexit_p(em_sti_remove),
395 module_platform_driver(em_sti_device_driver);
397 MODULE_AUTHOR("Magnus Damm");
398 MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver");
399 MODULE_LICENSE("GPL v2");