1 // SPDX-License-Identifier: GPL-2.0-only
6 * Copyright (C) 2013 John Crispin <john@phrozen.org>
9 #include <linux/platform_device.h>
10 #include <linux/interrupt.h>
11 #include <linux/timer.h>
12 #include <linux/of_gpio.h>
13 #include <linux/clk.h>
15 #include <asm/mach-ralink/ralink_regs.h>
17 #define TIMER_REG_TMRSTAT 0x00
18 #define TIMER_REG_TMR0LOAD 0x10
19 #define TIMER_REG_TMR0CTL 0x18
21 #define TMRSTAT_TMR0INT BIT(0)
23 #define TMR0CTL_ENABLE BIT(7)
24 #define TMR0CTL_MODE_PERIODIC BIT(4)
25 #define TMR0CTL_PRESCALER 1
26 #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
27 #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
31 void __iomem
*membase
;
33 unsigned long timer_freq
;
34 unsigned long timer_div
;
37 static inline void rt_timer_w32(struct rt_timer
*rt
, u8 reg
, u32 val
)
39 __raw_writel(val
, rt
->membase
+ reg
);
42 static inline u32
rt_timer_r32(struct rt_timer
*rt
, u8 reg
)
44 return __raw_readl(rt
->membase
+ reg
);
47 static irqreturn_t
rt_timer_irq(int irq
, void *_rt
)
49 struct rt_timer
*rt
= (struct rt_timer
*) _rt
;
51 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
52 rt_timer_w32(rt
, TIMER_REG_TMRSTAT
, TMRSTAT_TMR0INT
);
58 static int rt_timer_request(struct rt_timer
*rt
)
60 int err
= request_irq(rt
->irq
, rt_timer_irq
, 0,
61 dev_name(rt
->dev
), rt
);
63 dev_err(rt
->dev
, "failed to request irq\n");
65 u32 t
= TMR0CTL_MODE_PERIODIC
| TMR0CTL_PRESCALE_VAL
;
66 rt_timer_w32(rt
, TIMER_REG_TMR0CTL
, t
);
71 static int rt_timer_config(struct rt_timer
*rt
, unsigned long divisor
)
73 if (rt
->timer_freq
< divisor
)
74 rt
->timer_div
= rt
->timer_freq
;
76 rt
->timer_div
= divisor
;
78 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
83 static int rt_timer_enable(struct rt_timer
*rt
)
87 rt_timer_w32(rt
, TIMER_REG_TMR0LOAD
, rt
->timer_freq
/ rt
->timer_div
);
89 t
= rt_timer_r32(rt
, TIMER_REG_TMR0CTL
);
91 rt_timer_w32(rt
, TIMER_REG_TMR0CTL
, t
);
96 static int rt_timer_probe(struct platform_device
*pdev
)
98 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
102 rt
= devm_kzalloc(&pdev
->dev
, sizeof(*rt
), GFP_KERNEL
);
104 dev_err(&pdev
->dev
, "failed to allocate memory\n");
108 rt
->irq
= platform_get_irq(pdev
, 0);
112 rt
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
113 if (IS_ERR(rt
->membase
))
114 return PTR_ERR(rt
->membase
);
116 clk
= devm_clk_get(&pdev
->dev
, NULL
);
118 dev_err(&pdev
->dev
, "failed get clock rate\n");
122 rt
->timer_freq
= clk_get_rate(clk
) / TMR0CTL_PRESCALE_DIV
;
126 rt
->dev
= &pdev
->dev
;
127 platform_set_drvdata(pdev
, rt
);
129 rt_timer_request(rt
);
130 rt_timer_config(rt
, 2);
133 dev_info(&pdev
->dev
, "maximum frequency is %luHz\n", rt
->timer_freq
);
138 static const struct of_device_id rt_timer_match
[] = {
139 { .compatible
= "ralink,rt2880-timer" },
143 static struct platform_driver rt_timer_driver
= {
144 .probe
= rt_timer_probe
,
147 .of_match_table
= rt_timer_match
,
148 .suppress_bind_attrs
= true,
151 builtin_platform_driver(rt_timer_driver
);