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>
38 void __iomem *mapbase;
40 struct irqaction irqaction;
41 struct platform_device *pdev;
43 unsigned long periodic;
44 struct clock_event_device ced;
47 static DEFINE_RAW_SPINLOCK(sh_mtu2_lock);
49 #define TSTR -1 /* shared register */
50 #define TCR 0 /* channel register */
51 #define TMDR 1 /* channel register */
52 #define TIOR 2 /* channel register */
53 #define TIER 3 /* channel register */
54 #define TSR 4 /* channel register */
55 #define TCNT 5 /* channel register */
56 #define TGR 6 /* channel register */
58 static unsigned long mtu2_reg_offs[] = {
68 static inline unsigned long sh_mtu2_read(struct sh_mtu2_priv *p, int reg_nr)
70 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
71 void __iomem *base = p->mapbase;
75 return ioread8(base + cfg->channel_offset);
77 offs = mtu2_reg_offs[reg_nr];
79 if ((reg_nr == TCNT) || (reg_nr == TGR))
80 return ioread16(base + offs);
82 return ioread8(base + offs);
85 static inline void sh_mtu2_write(struct sh_mtu2_priv *p, int reg_nr,
88 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
89 void __iomem *base = p->mapbase;
93 iowrite8(value, base + cfg->channel_offset);
97 offs = mtu2_reg_offs[reg_nr];
99 if ((reg_nr == TCNT) || (reg_nr == TGR))
100 iowrite16(value, base + offs);
102 iowrite8(value, base + offs);
105 static void sh_mtu2_start_stop_ch(struct sh_mtu2_priv *p, int start)
107 struct sh_timer_config *cfg = p->pdev->dev.platform_data;
108 unsigned long flags, value;
110 /* start stop register shared by multiple timer channels */
111 raw_spin_lock_irqsave(&sh_mtu2_lock, flags);
112 value = sh_mtu2_read(p, TSTR);
115 value |= 1 << cfg->timer_bit;
117 value &= ~(1 << cfg->timer_bit);
119 sh_mtu2_write(p, TSTR, value);
120 raw_spin_unlock_irqrestore(&sh_mtu2_lock, flags);
123 static int sh_mtu2_enable(struct sh_mtu2_priv *p)
127 pm_runtime_get_sync(&p->pdev->dev);
128 dev_pm_syscore_device(&p->pdev->dev, true);
131 ret = clk_enable(p->clk);
133 dev_err(&p->pdev->dev, "cannot enable clock\n");
137 /* make sure channel is disabled */
138 sh_mtu2_start_stop_ch(p, 0);
140 p->rate = clk_get_rate(p->clk) / 64;
141 p->periodic = (p->rate + HZ/2) / HZ;
143 /* "Periodic Counter Operation" */
144 sh_mtu2_write(p, TCR, 0x23); /* TGRA clear, divide clock by 64 */
145 sh_mtu2_write(p, TIOR, 0);
146 sh_mtu2_write(p, TGR, p->periodic);
147 sh_mtu2_write(p, TCNT, 0);
148 sh_mtu2_write(p, TMDR, 0);
149 sh_mtu2_write(p, TIER, 0x01);
152 sh_mtu2_start_stop_ch(p, 1);
157 static void sh_mtu2_disable(struct sh_mtu2_priv *p)
159 /* disable channel */
160 sh_mtu2_start_stop_ch(p, 0);
165 dev_pm_syscore_device(&p->pdev->dev, false);
166 pm_runtime_put(&p->pdev->dev);
169 static irqreturn_t sh_mtu2_interrupt(int irq, void *dev_id)
171 struct sh_mtu2_priv *p = dev_id;
173 /* acknowledge interrupt */
174 sh_mtu2_read(p, TSR);
175 sh_mtu2_write(p, TSR, 0xfe);
177 /* notify clockevent layer */
178 p->ced.event_handler(&p->ced);
182 static struct sh_mtu2_priv *ced_to_sh_mtu2(struct clock_event_device *ced)
184 return container_of(ced, struct sh_mtu2_priv, ced);
187 static void sh_mtu2_clock_event_mode(enum clock_event_mode mode,
188 struct clock_event_device *ced)
190 struct sh_mtu2_priv *p = ced_to_sh_mtu2(ced);
193 /* deal with old setting first */
195 case CLOCK_EVT_MODE_PERIODIC:
204 case CLOCK_EVT_MODE_PERIODIC:
205 dev_info(&p->pdev->dev, "used for periodic clock events\n");
208 case CLOCK_EVT_MODE_UNUSED:
212 case CLOCK_EVT_MODE_SHUTDOWN:
218 static void sh_mtu2_clock_event_suspend(struct clock_event_device *ced)
220 pm_genpd_syscore_poweroff(&ced_to_sh_mtu2(ced)->pdev->dev);
223 static void sh_mtu2_clock_event_resume(struct clock_event_device *ced)
225 pm_genpd_syscore_poweron(&ced_to_sh_mtu2(ced)->pdev->dev);
228 static void sh_mtu2_register_clockevent(struct sh_mtu2_priv *p,
229 char *name, unsigned long rating)
231 struct clock_event_device *ced = &p->ced;
234 memset(ced, 0, sizeof(*ced));
237 ced->features = CLOCK_EVT_FEAT_PERIODIC;
238 ced->rating = rating;
239 ced->cpumask = cpumask_of(0);
240 ced->set_mode = sh_mtu2_clock_event_mode;
241 ced->suspend = sh_mtu2_clock_event_suspend;
242 ced->resume = sh_mtu2_clock_event_resume;
244 dev_info(&p->pdev->dev, "used for clock events\n");
245 clockevents_register_device(ced);
247 ret = setup_irq(p->irqaction.irq, &p->irqaction);
249 dev_err(&p->pdev->dev, "failed to request irq %d\n",
255 static int sh_mtu2_register(struct sh_mtu2_priv *p, char *name,
256 unsigned long clockevent_rating)
258 if (clockevent_rating)
259 sh_mtu2_register_clockevent(p, name, clockevent_rating);
264 static int sh_mtu2_setup(struct sh_mtu2_priv *p, struct platform_device *pdev)
266 struct sh_timer_config *cfg = pdev->dev.platform_data;
267 struct resource *res;
271 memset(p, 0, sizeof(*p));
275 dev_err(&p->pdev->dev, "missing platform data\n");
279 platform_set_drvdata(pdev, p);
281 res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
283 dev_err(&p->pdev->dev, "failed to get I/O memory\n");
287 irq = platform_get_irq(p->pdev, 0);
289 dev_err(&p->pdev->dev, "failed to get irq\n");
293 /* map memory, let mapbase point to our channel */
294 p->mapbase = ioremap_nocache(res->start, resource_size(res));
295 if (p->mapbase == NULL) {
296 dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
300 /* setup data for setup_irq() (too early for request_irq()) */
301 p->irqaction.name = dev_name(&p->pdev->dev);
302 p->irqaction.handler = sh_mtu2_interrupt;
303 p->irqaction.dev_id = p;
304 p->irqaction.irq = irq;
305 p->irqaction.flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING;
307 /* get hold of clock */
308 p->clk = clk_get(&p->pdev->dev, "mtu2_fck");
309 if (IS_ERR(p->clk)) {
310 dev_err(&p->pdev->dev, "cannot get clock\n");
311 ret = PTR_ERR(p->clk);
315 ret = clk_prepare(p->clk);
319 ret = sh_mtu2_register(p, (char *)dev_name(&p->pdev->dev),
320 cfg->clockevent_rating);
326 clk_unprepare(p->clk);
335 static int sh_mtu2_probe(struct platform_device *pdev)
337 struct sh_mtu2_priv *p = platform_get_drvdata(pdev);
338 struct sh_timer_config *cfg = pdev->dev.platform_data;
341 if (!is_early_platform_device(pdev)) {
342 pm_runtime_set_active(&pdev->dev);
343 pm_runtime_enable(&pdev->dev);
347 dev_info(&pdev->dev, "kept as earlytimer\n");
351 p = kmalloc(sizeof(*p), GFP_KERNEL);
353 dev_err(&pdev->dev, "failed to allocate driver data\n");
357 ret = sh_mtu2_setup(p, pdev);
360 pm_runtime_idle(&pdev->dev);
363 if (is_early_platform_device(pdev))
367 if (cfg->clockevent_rating)
368 pm_runtime_irq_safe(&pdev->dev);
370 pm_runtime_idle(&pdev->dev);
375 static int sh_mtu2_remove(struct platform_device *pdev)
377 return -EBUSY; /* cannot unregister clockevent */
380 static struct platform_driver sh_mtu2_device_driver = {
381 .probe = sh_mtu2_probe,
382 .remove = sh_mtu2_remove,
388 static int __init sh_mtu2_init(void)
390 return platform_driver_register(&sh_mtu2_device_driver);
393 static void __exit sh_mtu2_exit(void)
395 platform_driver_unregister(&sh_mtu2_device_driver);
398 early_platform_init("earlytimer", &sh_mtu2_device_driver);
399 subsys_initcall(sh_mtu2_init);
400 module_exit(sh_mtu2_exit);
402 MODULE_AUTHOR("Magnus Damm");
403 MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
404 MODULE_LICENSE("GPL v2");