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/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/kernel.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/sched_clock.h>
29 #define LPC32XX_TIMER_IR 0x000
30 #define LPC32XX_TIMER_IR_MR0INT BIT(0)
31 #define LPC32XX_TIMER_TCR 0x004
32 #define LPC32XX_TIMER_TCR_CEN BIT(0)
33 #define LPC32XX_TIMER_TCR_CRST BIT(1)
34 #define LPC32XX_TIMER_TC 0x008
35 #define LPC32XX_TIMER_PR 0x00c
36 #define LPC32XX_TIMER_MCR 0x014
37 #define LPC32XX_TIMER_MCR_MR0I BIT(0)
38 #define LPC32XX_TIMER_MCR_MR0R BIT(1)
39 #define LPC32XX_TIMER_MCR_MR0S BIT(2)
40 #define LPC32XX_TIMER_MR0 0x018
41 #define LPC32XX_TIMER_CTCR 0x070
43 struct lpc32xx_clock_event_ddata
{
44 struct clock_event_device evtdev
;
48 /* Needed for the sched clock */
49 static void __iomem
*clocksource_timer_counter
;
51 static u64 notrace
lpc32xx_read_sched_clock(void)
53 return readl(clocksource_timer_counter
);
56 static int lpc32xx_clkevt_next_event(unsigned long delta
,
57 struct clock_event_device
*evtdev
)
59 struct lpc32xx_clock_event_ddata
*ddata
=
60 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
63 * Place timer in reset and program the delta in the prescale
64 * register (PR). When the prescale counter matches the value
65 * in PR the counter register is incremented and the compare
66 * match will trigger. After setup the timer is released from
69 writel_relaxed(LPC32XX_TIMER_TCR_CRST
, ddata
->base
+ LPC32XX_TIMER_TCR
);
70 writel_relaxed(delta
, ddata
->base
+ LPC32XX_TIMER_PR
);
71 writel_relaxed(LPC32XX_TIMER_TCR_CEN
, ddata
->base
+ LPC32XX_TIMER_TCR
);
76 static int lpc32xx_clkevt_shutdown(struct clock_event_device
*evtdev
)
78 struct lpc32xx_clock_event_ddata
*ddata
=
79 container_of(evtdev
, struct lpc32xx_clock_event_ddata
, evtdev
);
81 /* Disable the timer */
82 writel_relaxed(0, ddata
->base
+ LPC32XX_TIMER_TCR
);
87 static int lpc32xx_clkevt_oneshot(struct clock_event_device
*evtdev
)
90 * When using oneshot, we must also disable the timer
91 * to wait for the first call to set_next_event().
93 return lpc32xx_clkevt_shutdown(evtdev
);
96 static irqreturn_t
lpc32xx_clock_event_handler(int irq
, void *dev_id
)
98 struct lpc32xx_clock_event_ddata
*ddata
= dev_id
;
100 /* Clear match on channel 0 */
101 writel_relaxed(LPC32XX_TIMER_IR_MR0INT
, ddata
->base
+ LPC32XX_TIMER_IR
);
103 ddata
->evtdev
.event_handler(&ddata
->evtdev
);
108 static struct lpc32xx_clock_event_ddata lpc32xx_clk_event_ddata
= {
110 .name
= "lpc3220 clockevent",
111 .features
= CLOCK_EVT_FEAT_ONESHOT
,
113 .set_next_event
= lpc32xx_clkevt_next_event
,
114 .set_state_shutdown
= lpc32xx_clkevt_shutdown
,
115 .set_state_oneshot
= lpc32xx_clkevt_oneshot
,
119 static int __init
lpc32xx_clocksource_init(struct device_node
*np
)
126 clk
= of_clk_get_by_name(np
, "timerclk");
128 pr_err("clock get failed (%ld)\n", PTR_ERR(clk
));
132 ret
= clk_prepare_enable(clk
);
134 pr_err("clock enable failed (%d)\n", ret
);
138 base
= of_iomap(np
, 0);
140 pr_err("unable to map registers\n");
141 ret
= -EADDRNOTAVAIL
;
146 * Disable and reset timer then set it to free running timer
147 * mode (CTCR) with no prescaler (PR) or match operations (MCR).
148 * After setup the timer is released from reset and enabled.
150 writel_relaxed(LPC32XX_TIMER_TCR_CRST
, base
+ LPC32XX_TIMER_TCR
);
151 writel_relaxed(0, base
+ LPC32XX_TIMER_PR
);
152 writel_relaxed(0, base
+ LPC32XX_TIMER_MCR
);
153 writel_relaxed(0, base
+ LPC32XX_TIMER_CTCR
);
154 writel_relaxed(LPC32XX_TIMER_TCR_CEN
, base
+ LPC32XX_TIMER_TCR
);
156 rate
= clk_get_rate(clk
);
157 ret
= clocksource_mmio_init(base
+ LPC32XX_TIMER_TC
, "lpc3220 timer",
158 rate
, 300, 32, clocksource_mmio_readl_up
);
160 pr_err("failed to init clocksource (%d)\n", ret
);
161 goto err_clocksource_init
;
164 clocksource_timer_counter
= base
+ LPC32XX_TIMER_TC
;
165 sched_clock_register(lpc32xx_read_sched_clock
, 32, rate
);
169 err_clocksource_init
:
172 clk_disable_unprepare(clk
);
178 static int __init
lpc32xx_clockevent_init(struct device_node
*np
)
185 clk
= of_clk_get_by_name(np
, "timerclk");
187 pr_err("clock get failed (%ld)\n", PTR_ERR(clk
));
191 ret
= clk_prepare_enable(clk
);
193 pr_err("clock enable failed (%d)\n", ret
);
197 base
= of_iomap(np
, 0);
199 pr_err("unable to map registers\n");
200 ret
= -EADDRNOTAVAIL
;
204 irq
= irq_of_parse_and_map(np
, 0);
206 pr_err("get irq failed\n");
212 * Disable timer and clear any pending interrupt (IR) on match
213 * channel 0 (MR0). Configure a compare match value of 1 on MR0
214 * and enable interrupt, reset on match and stop on match (MCR).
216 writel_relaxed(0, base
+ LPC32XX_TIMER_TCR
);
217 writel_relaxed(0, base
+ LPC32XX_TIMER_CTCR
);
218 writel_relaxed(LPC32XX_TIMER_IR_MR0INT
, base
+ LPC32XX_TIMER_IR
);
219 writel_relaxed(1, base
+ LPC32XX_TIMER_MR0
);
220 writel_relaxed(LPC32XX_TIMER_MCR_MR0I
| LPC32XX_TIMER_MCR_MR0R
|
221 LPC32XX_TIMER_MCR_MR0S
, base
+ LPC32XX_TIMER_MCR
);
223 rate
= clk_get_rate(clk
);
224 lpc32xx_clk_event_ddata
.base
= base
;
225 clockevents_config_and_register(&lpc32xx_clk_event_ddata
.evtdev
,
228 ret
= request_irq(irq
, lpc32xx_clock_event_handler
,
229 IRQF_TIMER
| IRQF_IRQPOLL
, "lpc3220 clockevent",
230 &lpc32xx_clk_event_ddata
);
232 pr_err("request irq failed\n");
241 clk_disable_unprepare(clk
);
248 * This function asserts that we have exactly one clocksource and one
249 * clock_event_device in the end.
251 static void __init
lpc32xx_timer_init(struct device_node
*np
)
253 static int has_clocksource
, has_clockevent
;
256 if (!has_clocksource
) {
257 ret
= lpc32xx_clocksource_init(np
);
264 if (!has_clockevent
) {
265 ret
= lpc32xx_clockevent_init(np
);
272 CLOCKSOURCE_OF_DECLARE(lpc32xx_timer
, "nxp,lpc3220-timer", lpc32xx_timer_init
);