2 * This file contains driver for the Xilinx PS Timer Counter IP.
4 * Copyright (C) 2011 Xilinx
6 * based on arch/mips/kernel/time.c timer driver
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/types.h>
23 #include <linux/clocksource.h>
24 #include <linux/clockchips.h>
27 #include <linux/of_address.h>
28 #include <linux/of_irq.h>
29 #include <linux/slab.h>
30 #include <linux/clk-provider.h>
35 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
36 * and use same offsets for Timer 2
38 #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
39 #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
40 #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
41 #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
42 #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
43 #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
45 #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
47 /* Setup the timers to use pre-scaling, using a fixed value for now that will
48 * work across most input frequency, but it may need to be more dynamic
50 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
51 #define PRESCALE 2048 /* The exponent must match this */
52 #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
53 #define CLK_CNTRL_PRESCALE_EN 1
54 #define CNT_CNTRL_RESET (1<<4)
57 * struct xttcps_timer - This definition defines local timer structure
59 * @base_addr: Base address of timer
62 void __iomem
*base_addr
;
65 struct xttcps_timer_clocksource
{
66 struct xttcps_timer xttc
;
67 struct clocksource cs
;
70 #define to_xttcps_timer_clksrc(x) \
71 container_of(x, struct xttcps_timer_clocksource, cs)
73 struct xttcps_timer_clockevent
{
74 struct xttcps_timer xttc
;
75 struct clock_event_device ce
;
79 #define to_xttcps_timer_clkevent(x) \
80 container_of(x, struct xttcps_timer_clockevent, ce)
83 * xttcps_set_interval - Set the timer interval value
85 * @timer: Pointer to the timer instance
86 * @cycles: Timer interval ticks
88 static void xttcps_set_interval(struct xttcps_timer
*timer
,
93 /* Disable the counter, set the counter value and re-enable counter */
94 ctrl_reg
= __raw_readl(timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
95 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
96 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
98 __raw_writel(cycles
, timer
->base_addr
+ XTTCPS_INTR_VAL_OFFSET
);
100 /* Reset the counter (0x10) so that it starts from 0, one-shot
101 mode makes this needed for timing to be right. */
102 ctrl_reg
|= CNT_CNTRL_RESET
;
103 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
104 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
108 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
110 * @irq: IRQ number of the Timer
111 * @dev_id: void pointer to the xttcps_timer instance
113 * returns: Always IRQ_HANDLED - success
115 static irqreturn_t
xttcps_clock_event_interrupt(int irq
, void *dev_id
)
117 struct xttcps_timer_clockevent
*xttce
= dev_id
;
118 struct xttcps_timer
*timer
= &xttce
->xttc
;
120 /* Acknowledge the interrupt and call event handler */
121 __raw_readl(timer
->base_addr
+ XTTCPS_ISR_OFFSET
);
123 xttce
->ce
.event_handler(&xttce
->ce
);
129 * __xttc_clocksource_read - Reads the timer counter register
131 * returns: Current timer counter register value
133 static cycle_t
__xttc_clocksource_read(struct clocksource
*cs
)
135 struct xttcps_timer
*timer
= &to_xttcps_timer_clksrc(cs
)->xttc
;
137 return (cycle_t
)__raw_readl(timer
->base_addr
+
138 XTTCPS_COUNT_VAL_OFFSET
);
142 * xttcps_set_next_event - Sets the time interval for next event
144 * @cycles: Timer interval ticks
145 * @evt: Address of clock event instance
147 * returns: Always 0 - success
149 static int xttcps_set_next_event(unsigned long cycles
,
150 struct clock_event_device
*evt
)
152 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
153 struct xttcps_timer
*timer
= &xttce
->xttc
;
155 xttcps_set_interval(timer
, cycles
);
160 * xttcps_set_mode - Sets the mode of timer
162 * @mode: Mode to be set
163 * @evt: Address of clock event instance
165 static void xttcps_set_mode(enum clock_event_mode mode
,
166 struct clock_event_device
*evt
)
168 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
169 struct xttcps_timer
*timer
= &xttce
->xttc
;
173 case CLOCK_EVT_MODE_PERIODIC
:
174 xttcps_set_interval(timer
,
175 DIV_ROUND_CLOSEST(clk_get_rate(xttce
->clk
),
178 case CLOCK_EVT_MODE_ONESHOT
:
179 case CLOCK_EVT_MODE_UNUSED
:
180 case CLOCK_EVT_MODE_SHUTDOWN
:
181 ctrl_reg
= __raw_readl(timer
->base_addr
+
182 XTTCPS_CNT_CNTRL_OFFSET
);
183 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
184 __raw_writel(ctrl_reg
,
185 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
187 case CLOCK_EVT_MODE_RESUME
:
188 ctrl_reg
= __raw_readl(timer
->base_addr
+
189 XTTCPS_CNT_CNTRL_OFFSET
);
190 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
191 __raw_writel(ctrl_reg
,
192 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
197 static void __init
zynq_ttc_setup_clocksource(struct device_node
*np
,
200 struct xttcps_timer_clocksource
*ttccs
;
205 ttccs
= kzalloc(sizeof(*ttccs
), GFP_KERNEL
);
209 err
= of_property_read_u32(np
, "reg", ®
);
213 clk
= of_clk_get_by_name(np
, "cpu_1x");
214 if (WARN_ON(IS_ERR(clk
)))
217 err
= clk_prepare_enable(clk
);
221 ttccs
->xttc
.base_addr
= base
+ reg
* 4;
223 ttccs
->cs
.name
= np
->name
;
224 ttccs
->cs
.rating
= 200;
225 ttccs
->cs
.read
= __xttc_clocksource_read
;
226 ttccs
->cs
.mask
= CLOCKSOURCE_MASK(16);
227 ttccs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
229 __raw_writel(0x0, ttccs
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
230 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
231 ttccs
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
232 __raw_writel(CNT_CNTRL_RESET
,
233 ttccs
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
235 err
= clocksource_register_hz(&ttccs
->cs
, clk_get_rate(clk
) / PRESCALE
);
240 static void __init
zynq_ttc_setup_clockevent(struct device_node
*np
,
243 struct xttcps_timer_clockevent
*ttcce
;
247 ttcce
= kzalloc(sizeof(*ttcce
), GFP_KERNEL
);
251 err
= of_property_read_u32(np
, "reg", ®
);
255 ttcce
->xttc
.base_addr
= base
+ reg
* 4;
257 ttcce
->clk
= of_clk_get_by_name(np
, "cpu_1x");
258 if (WARN_ON(IS_ERR(ttcce
->clk
)))
261 err
= clk_prepare_enable(ttcce
->clk
);
265 irq
= irq_of_parse_and_map(np
, 0);
269 ttcce
->ce
.name
= np
->name
;
270 ttcce
->ce
.features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
271 ttcce
->ce
.set_next_event
= xttcps_set_next_event
;
272 ttcce
->ce
.set_mode
= xttcps_set_mode
;
273 ttcce
->ce
.rating
= 200;
276 __raw_writel(0x23, ttcce
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
277 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
278 ttcce
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
279 __raw_writel(0x1, ttcce
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
281 err
= request_irq(irq
, xttcps_clock_event_interrupt
, IRQF_TIMER
,
286 clockevents_config_and_register(&ttcce
->ce
,
287 clk_get_rate(ttcce
->clk
) / PRESCALE
,
291 static const __initconst
struct of_device_id zynq_ttc_match
[] = {
292 { .compatible
= "xlnx,ttc-counter-clocksource",
293 .data
= zynq_ttc_setup_clocksource
, },
294 { .compatible
= "xlnx,ttc-counter-clockevent",
295 .data
= zynq_ttc_setup_clockevent
, },
300 * xttcps_timer_init - Initialize the timer
302 * Initializes the timer hardware and register the clock source and clock event
303 * timers with Linux kernal timer framework
305 void __init
xttcps_timer_init(void)
307 struct device_node
*np
;
309 for_each_compatible_node(np
, NULL
, "xlnx,ttc") {
310 struct device_node
*np_chld
;
313 base
= of_iomap(np
, 0);
317 for_each_available_child_of_node(np
, np_chld
) {
318 int (*cb
)(struct device_node
*np
, void __iomem
*base
);
319 const struct of_device_id
*match
;
321 match
= of_match_node(zynq_ttc_match
, np_chld
);