2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
6 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
12 #include <linux/timer.h>
13 #include <linux/of_gpio.h>
14 #include <linux/clk.h>
16 #include <asm/mach-ralink/ralink_regs.h>
18 #define TIMER_REG_TMRSTAT 0x00
19 #define TIMER_REG_TMR0LOAD 0x10
20 #define TIMER_REG_TMR0CTL 0x18
22 #define TMRSTAT_TMR0INT BIT(0)
24 #define TMR0CTL_ENABLE BIT(7)
25 #define TMR0CTL_MODE_PERIODIC BIT(4)
26 #define TMR0CTL_PRESCALER 1
27 #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
28 #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
32 void __iomem
*membase
;
34 unsigned long timer_freq
;
35 unsigned long timer_div
;
38 static inline void rt_timer_w32(struct rt_timer
*rt
, u8 reg
, u32 val
)
40 __raw_writel(val
, rt
->membase
+ reg
);
43 static inline u32
rt_timer_r32(struct rt_timer
*rt
, u8 reg
)
45 return __raw_readl(rt
->membase
+ reg
);
48 static irqreturn_t
rt_timer_irq(int irq
, void *_rt
)
50 struct rt_timer
*rt
= (struct rt_timer
*) _rt
;
52 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
53 rt_timer_w32(rt
, TIMER_REG_TMRSTAT
, TMRSTAT_TMR0INT
);
59 static int rt_timer_request(struct rt_timer
*rt
)
61 int err
= request_irq(rt
->irq
, rt_timer_irq
, IRQF_DISABLED
,
62 dev_name(rt
->dev
), rt
);
64 dev_err(rt
->dev
, "failed to request irq\n");
66 u32 t
= TMR0CTL_MODE_PERIODIC
| TMR0CTL_PRESCALE_VAL
;
67 rt_timer_w32(rt
, TIMER_REG_TMR0CTL
, t
);
72 static void rt_timer_free(struct rt_timer
*rt
)
74 free_irq(rt
->irq
, rt
);
77 static int rt_timer_config(struct rt_timer
*rt
, unsigned long divisor
)
79 if (rt
->timer_freq
< divisor
)
80 rt
->timer_div
= rt
->timer_freq
;
82 rt
->timer_div
= divisor
;
84 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
89 static int rt_timer_enable(struct rt_timer
*rt
)
93 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
95 t
= rt_timer_r32(rt
, TIMER_REG_TMR0CTL
);
97 rt_timer_w32(rt
, TIMER_REG_TMR0CTL
, t
);
102 static void rt_timer_disable(struct rt_timer
*rt
)
106 t
= rt_timer_r32(rt
, TIMER_REG_TMR0CTL
);
107 t
&= ~TMR0CTL_ENABLE
;
108 rt_timer_w32(rt
, TIMER_REG_TMR0CTL
, t
);
111 static int rt_timer_probe(struct platform_device
*pdev
)
113 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
117 rt
= devm_kzalloc(&pdev
->dev
, sizeof(*rt
), GFP_KERNEL
);
119 dev_err(&pdev
->dev
, "failed to allocate memory\n");
123 rt
->irq
= platform_get_irq(pdev
, 0);
125 dev_err(&pdev
->dev
, "failed to load irq\n");
129 rt
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
130 if (IS_ERR(rt
->membase
))
131 return PTR_ERR(rt
->membase
);
133 clk
= devm_clk_get(&pdev
->dev
, NULL
);
135 dev_err(&pdev
->dev
, "failed get clock rate\n");
139 rt
->timer_freq
= clk_get_rate(clk
) / TMR0CTL_PRESCALE_DIV
;
143 rt
->dev
= &pdev
->dev
;
144 platform_set_drvdata(pdev
, rt
);
146 rt_timer_request(rt
);
147 rt_timer_config(rt
, 2);
150 dev_info(&pdev
->dev
, "maximum frequency is %luHz\n", rt
->timer_freq
);
155 static int rt_timer_remove(struct platform_device
*pdev
)
157 struct rt_timer
*rt
= platform_get_drvdata(pdev
);
159 rt_timer_disable(rt
);
165 static const struct of_device_id rt_timer_match
[] = {
166 { .compatible
= "ralink,rt2880-timer" },
169 MODULE_DEVICE_TABLE(of
, rt_timer_match
);
171 static struct platform_driver rt_timer_driver
= {
172 .probe
= rt_timer_probe
,
173 .remove
= rt_timer_remove
,
176 .owner
= THIS_MODULE
,
177 .of_match_table
= rt_timer_match
181 module_platform_driver(rt_timer_driver
);
183 MODULE_DESCRIPTION("Ralink RT2880 timer");
184 MODULE_AUTHOR("John Crispin <blogic@openwrt.org");
185 MODULE_LICENSE("GPL");