2 * Clocksource driver for NXP LPC32xx/18xx/43xx timer
4 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
7 * time-efm32 Copyright (C) 2013 Pengutronix
8 * mach-lpc32xx/timer.c Copyright (C) 2009 - 2010 NXP Semiconductors
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
16 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/clk.h>
19 #include <linux/clockchips.h>
20 #include <linux/clocksource.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/kernel.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/sched_clock.h>
30 #define LPC32XX_TIMER_IR 0x000
31 #define LPC32XX_TIMER_IR_MR0INT BIT(0)
32 #define LPC32XX_TIMER_TCR 0x004
33 #define LPC32XX_TIMER_TCR_CEN BIT(0)
34 #define LPC32XX_TIMER_TCR_CRST BIT(1)
35 #define LPC32XX_TIMER_TC 0x008
36 #define LPC32XX_TIMER_PR 0x00c
37 #define LPC32XX_TIMER_MCR 0x014
38 #define LPC32XX_TIMER_MCR_MR0I BIT(0)
39 #define LPC32XX_TIMER_MCR_MR0R BIT(1)
40 #define LPC32XX_TIMER_MCR_MR0S BIT(2)
41 #define LPC32XX_TIMER_MR0 0x018
42 #define LPC32XX_TIMER_CTCR 0x070
44 struct lpc32xx_clock_event_ddata
{
45 struct clock_event_device evtdev
;
50 /* Needed for the sched clock */
51 static void __iomem
*clocksource_timer_counter
;
53 static u64 notrace
lpc32xx_read_sched_clock(void)
55 return readl(clocksource_timer_counter
);
58 static unsigned long lpc32xx_delay_timer_read(void)
60 return readl(clocksource_timer_counter
);
63 static struct delay_timer lpc32xx_delay_timer
= {
64 .read_current_timer
= lpc32xx_delay_timer_read
,
67 static int lpc32xx_clkevt_next_event(unsigned long delta
,
68 struct clock_event_device
*evtdev
)
70 struct lpc32xx_clock_event_ddata
*ddata
=
71 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
74 * Place timer in reset and program the delta in the match
75 * channel 0 (MR0). When the timer counter matches the value
76 * in MR0 register the match will trigger an interrupt.
77 * After setup the timer is released from reset and enabled.
79 writel_relaxed(LPC32XX_TIMER_TCR_CRST
, ddata
->base
+ LPC32XX_TIMER_TCR
);
80 writel_relaxed(delta
, ddata
->base
+ LPC32XX_TIMER_MR0
);
81 writel_relaxed(LPC32XX_TIMER_TCR_CEN
, ddata
->base
+ LPC32XX_TIMER_TCR
);
86 static int lpc32xx_clkevt_shutdown(struct clock_event_device
*evtdev
)
88 struct lpc32xx_clock_event_ddata
*ddata
=
89 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
91 /* Disable the timer */
92 writel_relaxed(0, ddata
->base
+ LPC32XX_TIMER_TCR
);
97 static int lpc32xx_clkevt_oneshot(struct clock_event_device
*evtdev
)
99 struct lpc32xx_clock_event_ddata
*ddata
=
100 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
103 * When using oneshot, we must also disable the timer
104 * to wait for the first call to set_next_event().
106 writel_relaxed(0, ddata
->base
+ LPC32XX_TIMER_TCR
);
108 /* Enable interrupt, reset on match and stop on match (MCR). */
109 writel_relaxed(LPC32XX_TIMER_MCR_MR0I
| LPC32XX_TIMER_MCR_MR0R
|
110 LPC32XX_TIMER_MCR_MR0S
, ddata
->base
+ LPC32XX_TIMER_MCR
);
114 static int lpc32xx_clkevt_periodic(struct clock_event_device
*evtdev
)
116 struct lpc32xx_clock_event_ddata
*ddata
=
117 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
119 /* Enable interrupt and reset on match. */
120 writel_relaxed(LPC32XX_TIMER_MCR_MR0I
| LPC32XX_TIMER_MCR_MR0R
,
121 ddata
->base
+ LPC32XX_TIMER_MCR
);
124 * Place timer in reset and program the delta in the match
127 writel_relaxed(LPC32XX_TIMER_TCR_CRST
, ddata
->base
+ LPC32XX_TIMER_TCR
);
128 writel_relaxed(ddata
->ticks_per_jiffy
, ddata
->base
+ LPC32XX_TIMER_MR0
);
129 writel_relaxed(LPC32XX_TIMER_TCR_CEN
, ddata
->base
+ LPC32XX_TIMER_TCR
);
134 static irqreturn_t
lpc32xx_clock_event_handler(int irq
, void *dev_id
)
136 struct lpc32xx_clock_event_ddata
*ddata
= dev_id
;
138 /* Clear match on channel 0 */
139 writel_relaxed(LPC32XX_TIMER_IR_MR0INT
, ddata
->base
+ LPC32XX_TIMER_IR
);
141 ddata
->evtdev
.event_handler(&ddata
->evtdev
);
146 static struct lpc32xx_clock_event_ddata lpc32xx_clk_event_ddata
= {
148 .name
= "lpc3220 clockevent",
149 .features
= CLOCK_EVT_FEAT_ONESHOT
|
150 CLOCK_EVT_FEAT_PERIODIC
,
152 .set_next_event
= lpc32xx_clkevt_next_event
,
153 .set_state_shutdown
= lpc32xx_clkevt_shutdown
,
154 .set_state_oneshot
= lpc32xx_clkevt_oneshot
,
155 .set_state_periodic
= lpc32xx_clkevt_periodic
,
159 static int __init
lpc32xx_clocksource_init(struct device_node
*np
)
166 clk
= of_clk_get_by_name(np
, "timerclk");
168 pr_err("clock get failed (%ld)\n", PTR_ERR(clk
));
172 ret
= clk_prepare_enable(clk
);
174 pr_err("clock enable failed (%d)\n", ret
);
178 base
= of_iomap(np
, 0);
180 pr_err("unable to map registers\n");
181 ret
= -EADDRNOTAVAIL
;
186 * Disable and reset timer then set it to free running timer
187 * mode (CTCR) with no prescaler (PR) or match operations (MCR).
188 * After setup the timer is released from reset and enabled.
190 writel_relaxed(LPC32XX_TIMER_TCR_CRST
, base
+ LPC32XX_TIMER_TCR
);
191 writel_relaxed(0, base
+ LPC32XX_TIMER_PR
);
192 writel_relaxed(0, base
+ LPC32XX_TIMER_MCR
);
193 writel_relaxed(0, base
+ LPC32XX_TIMER_CTCR
);
194 writel_relaxed(LPC32XX_TIMER_TCR_CEN
, base
+ LPC32XX_TIMER_TCR
);
196 rate
= clk_get_rate(clk
);
197 ret
= clocksource_mmio_init(base
+ LPC32XX_TIMER_TC
, "lpc3220 timer",
198 rate
, 300, 32, clocksource_mmio_readl_up
);
200 pr_err("failed to init clocksource (%d)\n", ret
);
201 goto err_clocksource_init
;
204 clocksource_timer_counter
= base
+ LPC32XX_TIMER_TC
;
205 lpc32xx_delay_timer
.freq
= rate
;
206 register_current_timer_delay(&lpc32xx_delay_timer
);
207 sched_clock_register(lpc32xx_read_sched_clock
, 32, rate
);
211 err_clocksource_init
:
214 clk_disable_unprepare(clk
);
220 static int __init
lpc32xx_clockevent_init(struct device_node
*np
)
227 clk
= of_clk_get_by_name(np
, "timerclk");
229 pr_err("clock get failed (%ld)\n", PTR_ERR(clk
));
233 ret
= clk_prepare_enable(clk
);
235 pr_err("clock enable failed (%d)\n", ret
);
239 base
= of_iomap(np
, 0);
241 pr_err("unable to map registers\n");
242 ret
= -EADDRNOTAVAIL
;
246 irq
= irq_of_parse_and_map(np
, 0);
248 pr_err("get irq failed\n");
254 * Disable timer and clear any pending interrupt (IR) on match
255 * channel 0 (MR0). Clear the prescaler as it's not used.
257 writel_relaxed(0, base
+ LPC32XX_TIMER_TCR
);
258 writel_relaxed(0, base
+ LPC32XX_TIMER_PR
);
259 writel_relaxed(0, base
+ LPC32XX_TIMER_CTCR
);
260 writel_relaxed(LPC32XX_TIMER_IR_MR0INT
, base
+ LPC32XX_TIMER_IR
);
262 rate
= clk_get_rate(clk
);
263 lpc32xx_clk_event_ddata
.base
= base
;
264 lpc32xx_clk_event_ddata
.ticks_per_jiffy
= DIV_ROUND_CLOSEST(rate
, HZ
);
265 clockevents_config_and_register(&lpc32xx_clk_event_ddata
.evtdev
,
268 ret
= request_irq(irq
, lpc32xx_clock_event_handler
,
269 IRQF_TIMER
| IRQF_IRQPOLL
, "lpc3220 clockevent",
270 &lpc32xx_clk_event_ddata
);
272 pr_err("request irq failed\n");
281 clk_disable_unprepare(clk
);
288 * This function asserts that we have exactly one clocksource and one
289 * clock_event_device in the end.
291 static int __init
lpc32xx_timer_init(struct device_node
*np
)
293 static int has_clocksource
, has_clockevent
;
296 if (!has_clocksource
) {
297 ret
= lpc32xx_clocksource_init(np
);
304 if (!has_clockevent
) {
305 ret
= lpc32xx_clockevent_init(np
);
314 TIMER_OF_DECLARE(lpc32xx_timer
, "nxp,lpc3220-timer", lpc32xx_timer_init
);