2 * SuperH Timer Support - MTU2
4 * Copyright (C) 2009 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>
25 #include <linux/delay.h>
27 #include <linux/clk.h>
28 #include <linux/irq.h>
29 #include <linux/err.h>
30 #include <linux/clockchips.h>
31 #include <linux/sh_timer.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/pm_domain.h>
35 #include <linux/pm_runtime.h>
39 struct sh_mtu2_channel
{
40 struct sh_mtu2_priv
*mtu
;
42 struct clock_event_device ced
;
46 struct platform_device
*pdev
;
48 void __iomem
*mapbase
;
51 struct sh_mtu2_channel channel
;
54 static DEFINE_RAW_SPINLOCK(sh_mtu2_lock
);
56 #define TSTR -1 /* shared register */
57 #define TCR 0 /* channel register */
58 #define TMDR 1 /* channel register */
59 #define TIOR 2 /* channel register */
60 #define TIER 3 /* channel register */
61 #define TSR 4 /* channel register */
62 #define TCNT 5 /* channel register */
63 #define TGR 6 /* channel register */
65 static unsigned long mtu2_reg_offs
[] = {
75 static inline unsigned long sh_mtu2_read(struct sh_mtu2_channel
*ch
, int reg_nr
)
77 struct sh_timer_config
*cfg
= ch
->mtu
->pdev
->dev
.platform_data
;
78 void __iomem
*base
= ch
->mtu
->mapbase
;
82 return ioread8(base
+ cfg
->channel_offset
);
84 offs
= mtu2_reg_offs
[reg_nr
];
86 if ((reg_nr
== TCNT
) || (reg_nr
== TGR
))
87 return ioread16(base
+ offs
);
89 return ioread8(base
+ offs
);
92 static inline void sh_mtu2_write(struct sh_mtu2_channel
*ch
, int reg_nr
,
95 struct sh_timer_config
*cfg
= ch
->mtu
->pdev
->dev
.platform_data
;
96 void __iomem
*base
= ch
->mtu
->mapbase
;
100 iowrite8(value
, base
+ cfg
->channel_offset
);
104 offs
= mtu2_reg_offs
[reg_nr
];
106 if ((reg_nr
== TCNT
) || (reg_nr
== TGR
))
107 iowrite16(value
, base
+ offs
);
109 iowrite8(value
, base
+ offs
);
112 static void sh_mtu2_start_stop_ch(struct sh_mtu2_channel
*ch
, int start
)
114 struct sh_timer_config
*cfg
= ch
->mtu
->pdev
->dev
.platform_data
;
115 unsigned long flags
, value
;
117 /* start stop register shared by multiple timer channels */
118 raw_spin_lock_irqsave(&sh_mtu2_lock
, flags
);
119 value
= sh_mtu2_read(ch
, TSTR
);
122 value
|= 1 << cfg
->timer_bit
;
124 value
&= ~(1 << cfg
->timer_bit
);
126 sh_mtu2_write(ch
, TSTR
, value
);
127 raw_spin_unlock_irqrestore(&sh_mtu2_lock
, flags
);
130 static int sh_mtu2_enable(struct sh_mtu2_channel
*ch
)
132 unsigned long periodic
;
136 pm_runtime_get_sync(&ch
->mtu
->pdev
->dev
);
137 dev_pm_syscore_device(&ch
->mtu
->pdev
->dev
, true);
140 ret
= clk_enable(ch
->mtu
->clk
);
142 dev_err(&ch
->mtu
->pdev
->dev
, "cannot enable clock\n");
146 /* make sure channel is disabled */
147 sh_mtu2_start_stop_ch(ch
, 0);
149 rate
= clk_get_rate(ch
->mtu
->clk
) / 64;
150 periodic
= (rate
+ HZ
/2) / HZ
;
152 /* "Periodic Counter Operation" */
153 sh_mtu2_write(ch
, TCR
, 0x23); /* TGRA clear, divide clock by 64 */
154 sh_mtu2_write(ch
, TIOR
, 0);
155 sh_mtu2_write(ch
, TGR
, periodic
);
156 sh_mtu2_write(ch
, TCNT
, 0);
157 sh_mtu2_write(ch
, TMDR
, 0);
158 sh_mtu2_write(ch
, TIER
, 0x01);
161 sh_mtu2_start_stop_ch(ch
, 1);
166 static void sh_mtu2_disable(struct sh_mtu2_channel
*ch
)
168 /* disable channel */
169 sh_mtu2_start_stop_ch(ch
, 0);
172 clk_disable(ch
->mtu
->clk
);
174 dev_pm_syscore_device(&ch
->mtu
->pdev
->dev
, false);
175 pm_runtime_put(&ch
->mtu
->pdev
->dev
);
178 static irqreturn_t
sh_mtu2_interrupt(int irq
, void *dev_id
)
180 struct sh_mtu2_channel
*ch
= dev_id
;
182 /* acknowledge interrupt */
183 sh_mtu2_read(ch
, TSR
);
184 sh_mtu2_write(ch
, TSR
, 0xfe);
186 /* notify clockevent layer */
187 ch
->ced
.event_handler(&ch
->ced
);
191 static struct sh_mtu2_channel
*ced_to_sh_mtu2(struct clock_event_device
*ced
)
193 return container_of(ced
, struct sh_mtu2_channel
, ced
);
196 static void sh_mtu2_clock_event_mode(enum clock_event_mode mode
,
197 struct clock_event_device
*ced
)
199 struct sh_mtu2_channel
*ch
= ced_to_sh_mtu2(ced
);
202 /* deal with old setting first */
204 case CLOCK_EVT_MODE_PERIODIC
:
213 case CLOCK_EVT_MODE_PERIODIC
:
214 dev_info(&ch
->mtu
->pdev
->dev
,
215 "used for periodic clock events\n");
218 case CLOCK_EVT_MODE_UNUSED
:
222 case CLOCK_EVT_MODE_SHUTDOWN
:
228 static void sh_mtu2_clock_event_suspend(struct clock_event_device
*ced
)
230 pm_genpd_syscore_poweroff(&ced_to_sh_mtu2(ced
)->mtu
->pdev
->dev
);
233 static void sh_mtu2_clock_event_resume(struct clock_event_device
*ced
)
235 pm_genpd_syscore_poweron(&ced_to_sh_mtu2(ced
)->mtu
->pdev
->dev
);
238 static void sh_mtu2_register_clockevent(struct sh_mtu2_channel
*ch
,
239 char *name
, unsigned long rating
)
241 struct clock_event_device
*ced
= &ch
->ced
;
244 memset(ced
, 0, sizeof(*ced
));
247 ced
->features
= CLOCK_EVT_FEAT_PERIODIC
;
248 ced
->rating
= rating
;
249 ced
->cpumask
= cpumask_of(0);
250 ced
->set_mode
= sh_mtu2_clock_event_mode
;
251 ced
->suspend
= sh_mtu2_clock_event_suspend
;
252 ced
->resume
= sh_mtu2_clock_event_resume
;
254 dev_info(&ch
->mtu
->pdev
->dev
, "used for clock events\n");
255 clockevents_register_device(ced
);
257 ret
= request_irq(ch
->irq
, sh_mtu2_interrupt
,
258 IRQF_TIMER
| IRQF_IRQPOLL
| IRQF_NOBALANCING
,
259 dev_name(&ch
->mtu
->pdev
->dev
), ch
);
261 dev_err(&ch
->mtu
->pdev
->dev
, "failed to request irq %d\n",
267 static int sh_mtu2_register(struct sh_mtu2_channel
*ch
, char *name
,
268 unsigned long clockevent_rating
)
270 if (clockevent_rating
)
271 sh_mtu2_register_clockevent(ch
, name
, clockevent_rating
);
276 static int sh_mtu2_setup(struct sh_mtu2_priv
*p
, struct platform_device
*pdev
)
278 struct sh_timer_config
*cfg
= pdev
->dev
.platform_data
;
279 struct resource
*res
;
283 memset(p
, 0, sizeof(*p
));
287 dev_err(&p
->pdev
->dev
, "missing platform data\n");
291 platform_set_drvdata(pdev
, p
);
293 res
= platform_get_resource(p
->pdev
, IORESOURCE_MEM
, 0);
295 dev_err(&p
->pdev
->dev
, "failed to get I/O memory\n");
299 p
->channel
.irq
= platform_get_irq(p
->pdev
, 0);
300 if (p
->channel
.irq
< 0) {
301 dev_err(&p
->pdev
->dev
, "failed to get irq\n");
305 /* map memory, let mapbase point to our channel */
306 p
->mapbase
= ioremap_nocache(res
->start
, resource_size(res
));
307 if (p
->mapbase
== NULL
) {
308 dev_err(&p
->pdev
->dev
, "failed to remap I/O memory\n");
312 /* get hold of clock */
313 p
->clk
= clk_get(&p
->pdev
->dev
, "mtu2_fck");
314 if (IS_ERR(p
->clk
)) {
315 dev_err(&p
->pdev
->dev
, "cannot get clock\n");
316 ret
= PTR_ERR(p
->clk
);
320 ret
= clk_prepare(p
->clk
);
326 ret
= sh_mtu2_register(&p
->channel
, (char *)dev_name(&p
->pdev
->dev
),
327 cfg
->clockevent_rating
);
333 clk_unprepare(p
->clk
);
342 static int sh_mtu2_probe(struct platform_device
*pdev
)
344 struct sh_mtu2_priv
*p
= platform_get_drvdata(pdev
);
345 struct sh_timer_config
*cfg
= pdev
->dev
.platform_data
;
348 if (!is_early_platform_device(pdev
)) {
349 pm_runtime_set_active(&pdev
->dev
);
350 pm_runtime_enable(&pdev
->dev
);
354 dev_info(&pdev
->dev
, "kept as earlytimer\n");
358 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
360 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
364 ret
= sh_mtu2_setup(p
, pdev
);
367 pm_runtime_idle(&pdev
->dev
);
370 if (is_early_platform_device(pdev
))
374 if (cfg
->clockevent_rating
)
375 pm_runtime_irq_safe(&pdev
->dev
);
377 pm_runtime_idle(&pdev
->dev
);
382 static int sh_mtu2_remove(struct platform_device
*pdev
)
384 return -EBUSY
; /* cannot unregister clockevent */
387 static struct platform_driver sh_mtu2_device_driver
= {
388 .probe
= sh_mtu2_probe
,
389 .remove
= sh_mtu2_remove
,
395 static int __init
sh_mtu2_init(void)
397 return platform_driver_register(&sh_mtu2_device_driver
);
400 static void __exit
sh_mtu2_exit(void)
402 platform_driver_unregister(&sh_mtu2_device_driver
);
405 early_platform_init("earlytimer", &sh_mtu2_device_driver
);
406 subsys_initcall(sh_mtu2_init
);
407 module_exit(sh_mtu2_exit
);
409 MODULE_AUTHOR("Magnus Damm");
410 MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
411 MODULE_LICENSE("GPL v2");