.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * linux/arch/arm/mach-mmp/time.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
12 | 13 | * The timers module actually includes three timers, each timer with up to |
---|
13 | 14 | * three match comparators. Timer #0 is used here in free-running mode as |
---|
14 | 15 | * the clock source, and match comparator #1 used as clock event device. |
---|
15 | | - * |
---|
16 | | - * This program is free software; you can redistribute it and/or modify |
---|
17 | | - * it under the terms of the GNU General Public License version 2 as |
---|
18 | | - * published by the Free Software Foundation. |
---|
19 | 16 | */ |
---|
20 | 17 | |
---|
21 | 18 | #include <linux/init.h> |
---|
22 | 19 | #include <linux/kernel.h> |
---|
23 | 20 | #include <linux/interrupt.h> |
---|
24 | 21 | #include <linux/clockchips.h> |
---|
| 22 | +#include <linux/clk.h> |
---|
25 | 23 | |
---|
26 | 24 | #include <linux/io.h> |
---|
27 | 25 | #include <linux/irq.h> |
---|
.. | .. |
---|
35 | 33 | #include "regs-timers.h" |
---|
36 | 34 | #include "regs-apbc.h" |
---|
37 | 35 | #include "irqs.h" |
---|
38 | | -#include "cputype.h" |
---|
39 | | -#include "clock.h" |
---|
40 | | - |
---|
41 | | -#ifdef CONFIG_CPU_MMP2 |
---|
42 | | -#define MMP_CLOCK_FREQ 6500000 |
---|
43 | | -#else |
---|
44 | | -#define MMP_CLOCK_FREQ 3250000 |
---|
45 | | -#endif |
---|
| 36 | +#include <linux/soc/mmp/cputype.h> |
---|
46 | 37 | |
---|
47 | 38 | #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE |
---|
48 | 39 | |
---|
.. | .. |
---|
52 | 43 | static void __iomem *mmp_timer_base = TIMERS_VIRT_BASE; |
---|
53 | 44 | |
---|
54 | 45 | /* |
---|
55 | | - * FIXME: the timer needs some delay to stablize the counter capture |
---|
| 46 | + * Read the timer through the CVWR register. Delay is required after requesting |
---|
| 47 | + * a read. The CR register cannot be directly read due to metastability issues |
---|
| 48 | + * documented in the PXA168 software manual. |
---|
56 | 49 | */ |
---|
57 | 50 | static inline uint32_t timer_read(void) |
---|
58 | 51 | { |
---|
59 | | - int delay = 100; |
---|
| 52 | + uint32_t val; |
---|
| 53 | + int delay = 3; |
---|
60 | 54 | |
---|
61 | 55 | __raw_writel(1, mmp_timer_base + TMR_CVWR(1)); |
---|
62 | 56 | |
---|
63 | 57 | while (delay--) |
---|
64 | | - cpu_relax(); |
---|
| 58 | + val = __raw_readl(mmp_timer_base + TMR_CVWR(1)); |
---|
65 | 59 | |
---|
66 | | - return __raw_readl(mmp_timer_base + TMR_CVWR(1)); |
---|
| 60 | + return val; |
---|
67 | 61 | } |
---|
68 | 62 | |
---|
69 | 63 | static u64 notrace mmp_read_sched_clock(void) |
---|
.. | .. |
---|
163 | 157 | |
---|
164 | 158 | __raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */ |
---|
165 | 159 | |
---|
166 | | - ccr &= (cpu_is_mmp2()) ? (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) : |
---|
| 160 | + ccr &= (cpu_is_mmp2() || cpu_is_mmp3()) ? |
---|
| 161 | + (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) : |
---|
167 | 162 | (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3)); |
---|
168 | 163 | __raw_writel(ccr, mmp_timer_base + TMR_CCR); |
---|
169 | 164 | |
---|
.. | .. |
---|
182 | 177 | __raw_writel(0x2, mmp_timer_base + TMR_CER); |
---|
183 | 178 | } |
---|
184 | 179 | |
---|
185 | | -static struct irqaction timer_irq = { |
---|
186 | | - .name = "timer", |
---|
187 | | - .flags = IRQF_TIMER | IRQF_IRQPOLL, |
---|
188 | | - .handler = timer_interrupt, |
---|
189 | | - .dev_id = &ckevt, |
---|
190 | | -}; |
---|
191 | | - |
---|
192 | | -void __init timer_init(int irq) |
---|
| 180 | +void __init mmp_timer_init(int irq, unsigned long rate) |
---|
193 | 181 | { |
---|
194 | 182 | timer_config(); |
---|
195 | 183 | |
---|
196 | | - sched_clock_register(mmp_read_sched_clock, 32, MMP_CLOCK_FREQ); |
---|
| 184 | + sched_clock_register(mmp_read_sched_clock, 32, rate); |
---|
197 | 185 | |
---|
198 | 186 | ckevt.cpumask = cpumask_of(0); |
---|
199 | 187 | |
---|
200 | | - setup_irq(irq, &timer_irq); |
---|
| 188 | + if (request_irq(irq, timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL, |
---|
| 189 | + "timer", &ckevt)) |
---|
| 190 | + pr_err("Failed to request irq %d (timer)\n", irq); |
---|
201 | 191 | |
---|
202 | | - clocksource_register_hz(&cksrc, MMP_CLOCK_FREQ); |
---|
203 | | - clockevents_config_and_register(&ckevt, MMP_CLOCK_FREQ, |
---|
204 | | - MIN_DELTA, MAX_DELTA); |
---|
| 192 | + clocksource_register_hz(&cksrc, rate); |
---|
| 193 | + clockevents_config_and_register(&ckevt, rate, MIN_DELTA, MAX_DELTA); |
---|
205 | 194 | } |
---|
206 | 195 | |
---|
207 | | -#ifdef CONFIG_OF |
---|
208 | | -static const struct of_device_id mmp_timer_dt_ids[] = { |
---|
209 | | - { .compatible = "mrvl,mmp-timer", }, |
---|
210 | | - {} |
---|
211 | | -}; |
---|
212 | | - |
---|
213 | | -void __init mmp_dt_init_timer(void) |
---|
| 196 | +static int __init mmp_dt_init_timer(struct device_node *np) |
---|
214 | 197 | { |
---|
215 | | - struct device_node *np; |
---|
| 198 | + struct clk *clk; |
---|
216 | 199 | int irq, ret; |
---|
| 200 | + unsigned long rate; |
---|
217 | 201 | |
---|
218 | | - np = of_find_matching_node(NULL, mmp_timer_dt_ids); |
---|
219 | | - if (!np) { |
---|
220 | | - ret = -ENODEV; |
---|
221 | | - goto out; |
---|
| 202 | + clk = of_clk_get(np, 0); |
---|
| 203 | + if (!IS_ERR(clk)) { |
---|
| 204 | + ret = clk_prepare_enable(clk); |
---|
| 205 | + if (ret) |
---|
| 206 | + return ret; |
---|
| 207 | + rate = clk_get_rate(clk); |
---|
| 208 | + } else if (cpu_is_pj4()) { |
---|
| 209 | + rate = 6500000; |
---|
| 210 | + } else { |
---|
| 211 | + rate = 3250000; |
---|
222 | 212 | } |
---|
223 | 213 | |
---|
224 | 214 | irq = irq_of_parse_and_map(np, 0); |
---|
225 | | - if (!irq) { |
---|
226 | | - ret = -EINVAL; |
---|
227 | | - goto out; |
---|
228 | | - } |
---|
| 215 | + if (!irq) |
---|
| 216 | + return -EINVAL; |
---|
| 217 | + |
---|
229 | 218 | mmp_timer_base = of_iomap(np, 0); |
---|
230 | | - if (!mmp_timer_base) { |
---|
231 | | - ret = -ENOMEM; |
---|
232 | | - goto out; |
---|
233 | | - } |
---|
234 | | - timer_init(irq); |
---|
235 | | - return; |
---|
236 | | -out: |
---|
237 | | - pr_err("Failed to get timer from device tree with error:%d\n", ret); |
---|
| 219 | + if (!mmp_timer_base) |
---|
| 220 | + return -ENOMEM; |
---|
| 221 | + |
---|
| 222 | + mmp_timer_init(irq, rate); |
---|
| 223 | + return 0; |
---|
238 | 224 | } |
---|
239 | | -#endif |
---|
| 225 | + |
---|
| 226 | +TIMER_OF_DECLARE(mmp_timer, "mrvl,mmp-timer", mmp_dt_init_timer); |
---|