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/interrupt.h>
19 #include <linux/clockchips.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/slab.h>
23 #include <linux/clk-provider.h>
27 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
28 * and use same offsets for Timer 2
30 #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
31 #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
32 #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
33 #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
34 #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
35 #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
37 #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
40 * Setup the timers to use pre-scaling, using a fixed value for now that will
41 * work across most input frequency, but it may need to be more dynamic
43 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
44 #define PRESCALE 2048 /* The exponent must match this */
45 #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
46 #define CLK_CNTRL_PRESCALE_EN 1
47 #define CNT_CNTRL_RESET (1<<4)
50 * struct xttcps_timer - This definition defines local timer structure
52 * @base_addr: Base address of timer
55 void __iomem
*base_addr
;
58 struct xttcps_timer_clocksource
{
59 struct xttcps_timer xttc
;
60 struct clocksource cs
;
63 #define to_xttcps_timer_clksrc(x) \
64 container_of(x, struct xttcps_timer_clocksource, cs)
66 struct xttcps_timer_clockevent
{
67 struct xttcps_timer xttc
;
68 struct clock_event_device ce
;
72 #define to_xttcps_timer_clkevent(x) \
73 container_of(x, struct xttcps_timer_clockevent, ce)
76 * xttcps_set_interval - Set the timer interval value
78 * @timer: Pointer to the timer instance
79 * @cycles: Timer interval ticks
81 static void xttcps_set_interval(struct xttcps_timer
*timer
,
86 /* Disable the counter, set the counter value and re-enable counter */
87 ctrl_reg
= __raw_readl(timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
88 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
89 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
91 __raw_writel(cycles
, timer
->base_addr
+ XTTCPS_INTR_VAL_OFFSET
);
94 * Reset the counter (0x10) so that it starts from 0, one-shot
95 * mode makes this needed for timing to be right.
97 ctrl_reg
|= CNT_CNTRL_RESET
;
98 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
99 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
103 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
105 * @irq: IRQ number of the Timer
106 * @dev_id: void pointer to the xttcps_timer instance
108 * returns: Always IRQ_HANDLED - success
110 static irqreturn_t
xttcps_clock_event_interrupt(int irq
, void *dev_id
)
112 struct xttcps_timer_clockevent
*xttce
= dev_id
;
113 struct xttcps_timer
*timer
= &xttce
->xttc
;
115 /* Acknowledge the interrupt and call event handler */
116 __raw_readl(timer
->base_addr
+ XTTCPS_ISR_OFFSET
);
118 xttce
->ce
.event_handler(&xttce
->ce
);
124 * __xttc_clocksource_read - Reads the timer counter register
126 * returns: Current timer counter register value
128 static cycle_t
__xttc_clocksource_read(struct clocksource
*cs
)
130 struct xttcps_timer
*timer
= &to_xttcps_timer_clksrc(cs
)->xttc
;
132 return (cycle_t
)__raw_readl(timer
->base_addr
+
133 XTTCPS_COUNT_VAL_OFFSET
);
137 * xttcps_set_next_event - Sets the time interval for next event
139 * @cycles: Timer interval ticks
140 * @evt: Address of clock event instance
142 * returns: Always 0 - success
144 static int xttcps_set_next_event(unsigned long cycles
,
145 struct clock_event_device
*evt
)
147 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
148 struct xttcps_timer
*timer
= &xttce
->xttc
;
150 xttcps_set_interval(timer
, cycles
);
155 * xttcps_set_mode - Sets the mode of timer
157 * @mode: Mode to be set
158 * @evt: Address of clock event instance
160 static void xttcps_set_mode(enum clock_event_mode mode
,
161 struct clock_event_device
*evt
)
163 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
164 struct xttcps_timer
*timer
= &xttce
->xttc
;
168 case CLOCK_EVT_MODE_PERIODIC
:
169 xttcps_set_interval(timer
,
170 DIV_ROUND_CLOSEST(clk_get_rate(xttce
->clk
),
173 case CLOCK_EVT_MODE_ONESHOT
:
174 case CLOCK_EVT_MODE_UNUSED
:
175 case CLOCK_EVT_MODE_SHUTDOWN
:
176 ctrl_reg
= __raw_readl(timer
->base_addr
+
177 XTTCPS_CNT_CNTRL_OFFSET
);
178 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
179 __raw_writel(ctrl_reg
,
180 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
182 case CLOCK_EVT_MODE_RESUME
:
183 ctrl_reg
= __raw_readl(timer
->base_addr
+
184 XTTCPS_CNT_CNTRL_OFFSET
);
185 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
186 __raw_writel(ctrl_reg
,
187 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
192 static void __init
zynq_ttc_setup_clocksource(struct device_node
*np
,
195 struct xttcps_timer_clocksource
*ttccs
;
200 ttccs
= kzalloc(sizeof(*ttccs
), GFP_KERNEL
);
204 err
= of_property_read_u32(np
, "reg", ®
);
208 clk
= of_clk_get_by_name(np
, "cpu_1x");
209 if (WARN_ON(IS_ERR(clk
)))
212 err
= clk_prepare_enable(clk
);
216 ttccs
->xttc
.base_addr
= base
+ reg
* 4;
218 ttccs
->cs
.name
= np
->name
;
219 ttccs
->cs
.rating
= 200;
220 ttccs
->cs
.read
= __xttc_clocksource_read
;
221 ttccs
->cs
.mask
= CLOCKSOURCE_MASK(16);
222 ttccs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
224 __raw_writel(0x0, ttccs
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
225 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
226 ttccs
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
227 __raw_writel(CNT_CNTRL_RESET
,
228 ttccs
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
230 err
= clocksource_register_hz(&ttccs
->cs
, clk_get_rate(clk
) / PRESCALE
);
235 static void __init
zynq_ttc_setup_clockevent(struct device_node
*np
,
238 struct xttcps_timer_clockevent
*ttcce
;
242 ttcce
= kzalloc(sizeof(*ttcce
), GFP_KERNEL
);
246 err
= of_property_read_u32(np
, "reg", ®
);
250 ttcce
->xttc
.base_addr
= base
+ reg
* 4;
252 ttcce
->clk
= of_clk_get_by_name(np
, "cpu_1x");
253 if (WARN_ON(IS_ERR(ttcce
->clk
)))
256 err
= clk_prepare_enable(ttcce
->clk
);
260 irq
= irq_of_parse_and_map(np
, 0);
264 ttcce
->ce
.name
= np
->name
;
265 ttcce
->ce
.features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
266 ttcce
->ce
.set_next_event
= xttcps_set_next_event
;
267 ttcce
->ce
.set_mode
= xttcps_set_mode
;
268 ttcce
->ce
.rating
= 200;
270 ttcce
->ce
.cpumask
= cpu_possible_mask
;
272 __raw_writel(0x23, ttcce
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
273 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
274 ttcce
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
275 __raw_writel(0x1, ttcce
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
277 err
= request_irq(irq
, xttcps_clock_event_interrupt
, IRQF_TIMER
,
282 clockevents_config_and_register(&ttcce
->ce
,
283 clk_get_rate(ttcce
->clk
) / PRESCALE
,
287 static const __initconst
struct of_device_id zynq_ttc_match
[] = {
288 { .compatible
= "xlnx,ttc-counter-clocksource",
289 .data
= zynq_ttc_setup_clocksource
, },
290 { .compatible
= "xlnx,ttc-counter-clockevent",
291 .data
= zynq_ttc_setup_clockevent
, },
296 * xttcps_timer_init - Initialize the timer
298 * Initializes the timer hardware and register the clock source and clock event
299 * timers with Linux kernal timer framework
301 void __init
xttcps_timer_init(void)
303 struct device_node
*np
;
305 for_each_compatible_node(np
, NULL
, "xlnx,ttc") {
306 struct device_node
*np_chld
;
309 base
= of_iomap(np
, 0);
313 for_each_available_child_of_node(np
, np_chld
) {
314 int (*cb
)(struct device_node
*np
, void __iomem
*base
);
315 const struct of_device_id
*match
;
317 match
= of_match_node(zynq_ttc_match
, np_chld
);