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_MATCH_1_OFFSET 0x30 /* Match 1 Value Reg, RW */
43 #define XTTCPS_MATCH_2_OFFSET 0x3C /* Match 2 Value Reg, RW */
44 #define XTTCPS_MATCH_3_OFFSET 0x48 /* Match 3 Value Reg, RW */
45 #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
46 #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
48 #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
50 /* Setup the timers to use pre-scaling, using a fixed value for now that will
51 * work across most input frequency, but it may need to be more dynamic
53 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
54 #define PRESCALE 2048 /* The exponent must match this */
55 #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
56 #define CLK_CNTRL_PRESCALE_EN 1
57 #define CNT_CNTRL_RESET (1<<4)
60 * struct xttcps_timer - This definition defines local timer structure
62 * @base_addr: Base address of timer
65 void __iomem
*base_addr
;
68 struct xttcps_timer_clocksource
{
69 struct xttcps_timer xttc
;
70 struct clocksource cs
;
73 #define to_xttcps_timer_clksrc(x) \
74 container_of(x, struct xttcps_timer_clocksource, cs)
76 struct xttcps_timer_clockevent
{
77 struct xttcps_timer xttc
;
78 struct clock_event_device ce
;
82 #define to_xttcps_timer_clkevent(x) \
83 container_of(x, struct xttcps_timer_clockevent, ce)
86 * xttcps_set_interval - Set the timer interval value
88 * @timer: Pointer to the timer instance
89 * @cycles: Timer interval ticks
91 static void xttcps_set_interval(struct xttcps_timer
*timer
,
96 /* Disable the counter, set the counter value and re-enable counter */
97 ctrl_reg
= __raw_readl(timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
98 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
99 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
101 __raw_writel(cycles
, timer
->base_addr
+ XTTCPS_INTR_VAL_OFFSET
);
103 /* Reset the counter (0x10) so that it starts from 0, one-shot
104 mode makes this needed for timing to be right. */
105 ctrl_reg
|= CNT_CNTRL_RESET
;
106 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
107 __raw_writel(ctrl_reg
, timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
111 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
113 * @irq: IRQ number of the Timer
114 * @dev_id: void pointer to the xttcps_timer instance
116 * returns: Always IRQ_HANDLED - success
118 static irqreturn_t
xttcps_clock_event_interrupt(int irq
, void *dev_id
)
120 struct xttcps_timer_clockevent
*xttce
= dev_id
;
121 struct xttcps_timer
*timer
= &xttce
->xttc
;
123 /* Acknowledge the interrupt and call event handler */
124 __raw_readl(timer
->base_addr
+ XTTCPS_ISR_OFFSET
);
126 xttce
->ce
.event_handler(&xttce
->ce
);
132 * __xttc_clocksource_read - Reads the timer counter register
134 * returns: Current timer counter register value
136 static cycle_t
__xttc_clocksource_read(struct clocksource
*cs
)
138 struct xttcps_timer
*timer
= &to_xttcps_timer_clksrc(cs
)->xttc
;
140 return (cycle_t
)__raw_readl(timer
->base_addr
+
141 XTTCPS_COUNT_VAL_OFFSET
);
145 * xttcps_set_next_event - Sets the time interval for next event
147 * @cycles: Timer interval ticks
148 * @evt: Address of clock event instance
150 * returns: Always 0 - success
152 static int xttcps_set_next_event(unsigned long cycles
,
153 struct clock_event_device
*evt
)
155 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
156 struct xttcps_timer
*timer
= &xttce
->xttc
;
158 xttcps_set_interval(timer
, cycles
);
163 * xttcps_set_mode - Sets the mode of timer
165 * @mode: Mode to be set
166 * @evt: Address of clock event instance
168 static void xttcps_set_mode(enum clock_event_mode mode
,
169 struct clock_event_device
*evt
)
171 struct xttcps_timer_clockevent
*xttce
= to_xttcps_timer_clkevent(evt
);
172 struct xttcps_timer
*timer
= &xttce
->xttc
;
176 case CLOCK_EVT_MODE_PERIODIC
:
177 xttcps_set_interval(timer
,
178 DIV_ROUND_CLOSEST(clk_get_rate(xttce
->clk
),
181 case CLOCK_EVT_MODE_ONESHOT
:
182 case CLOCK_EVT_MODE_UNUSED
:
183 case CLOCK_EVT_MODE_SHUTDOWN
:
184 ctrl_reg
= __raw_readl(timer
->base_addr
+
185 XTTCPS_CNT_CNTRL_OFFSET
);
186 ctrl_reg
|= XTTCPS_CNT_CNTRL_DISABLE_MASK
;
187 __raw_writel(ctrl_reg
,
188 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
190 case CLOCK_EVT_MODE_RESUME
:
191 ctrl_reg
= __raw_readl(timer
->base_addr
+
192 XTTCPS_CNT_CNTRL_OFFSET
);
193 ctrl_reg
&= ~XTTCPS_CNT_CNTRL_DISABLE_MASK
;
194 __raw_writel(ctrl_reg
,
195 timer
->base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
200 static void __init
zynq_ttc_setup_clocksource(struct device_node
*np
,
203 struct xttcps_timer_clocksource
*ttccs
;
208 ttccs
= kzalloc(sizeof(*ttccs
), GFP_KERNEL
);
212 err
= of_property_read_u32(np
, "reg", ®
);
216 clk
= of_clk_get_by_name(np
, "cpu_1x");
217 if (WARN_ON(IS_ERR(clk
)))
220 err
= clk_prepare_enable(clk
);
224 ttccs
->xttc
.base_addr
= base
+ reg
* 4;
226 ttccs
->cs
.name
= np
->name
;
227 ttccs
->cs
.rating
= 200;
228 ttccs
->cs
.read
= __xttc_clocksource_read
;
229 ttccs
->cs
.mask
= CLOCKSOURCE_MASK(16);
230 ttccs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
232 __raw_writel(0x0, ttccs
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
233 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
234 ttccs
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
235 __raw_writel(CNT_CNTRL_RESET
,
236 ttccs
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
238 err
= clocksource_register_hz(&ttccs
->cs
, clk_get_rate(clk
) / PRESCALE
);
243 static void __init
zynq_ttc_setup_clockevent(struct device_node
*np
,
246 struct xttcps_timer_clockevent
*ttcce
;
250 ttcce
= kzalloc(sizeof(*ttcce
), GFP_KERNEL
);
254 err
= of_property_read_u32(np
, "reg", ®
);
258 ttcce
->xttc
.base_addr
= base
+ reg
* 4;
260 ttcce
->clk
= of_clk_get_by_name(np
, "cpu_1x");
261 if (WARN_ON(IS_ERR(ttcce
->clk
)))
264 err
= clk_prepare_enable(ttcce
->clk
);
268 irq
= irq_of_parse_and_map(np
, 0);
272 ttcce
->ce
.name
= np
->name
;
273 ttcce
->ce
.features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
274 ttcce
->ce
.set_next_event
= xttcps_set_next_event
;
275 ttcce
->ce
.set_mode
= xttcps_set_mode
;
276 ttcce
->ce
.rating
= 200;
279 __raw_writel(0x23, ttcce
->xttc
.base_addr
+ XTTCPS_CNT_CNTRL_OFFSET
);
280 __raw_writel(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
281 ttcce
->xttc
.base_addr
+ XTTCPS_CLK_CNTRL_OFFSET
);
282 __raw_writel(0x1, ttcce
->xttc
.base_addr
+ XTTCPS_IER_OFFSET
);
284 err
= request_irq(irq
, xttcps_clock_event_interrupt
, IRQF_TIMER
,
289 clockevents_config_and_register(&ttcce
->ce
,
290 clk_get_rate(ttcce
->clk
) / PRESCALE
,
294 static const __initconst
struct of_device_id zynq_ttc_match
[] = {
295 { .compatible
= "xlnx,ttc-counter-clocksource",
296 .data
= zynq_ttc_setup_clocksource
, },
297 { .compatible
= "xlnx,ttc-counter-clockevent",
298 .data
= zynq_ttc_setup_clockevent
, },
303 * xttcps_timer_init - Initialize the timer
305 * Initializes the timer hardware and register the clock source and clock event
306 * timers with Linux kernal timer framework
308 void __init
xttcps_timer_init(void)
310 struct device_node
*np
;
312 for_each_compatible_node(np
, NULL
, "xlnx,ttc") {
313 struct device_node
*np_chld
;
316 base
= of_iomap(np
, 0);
320 for_each_available_child_of_node(np
, np_chld
) {
321 int (*cb
)(struct device_node
*np
, void __iomem
*base
);
322 const struct of_device_id
*match
;
324 match
= of_match_node(zynq_ttc_match
, np_chld
);