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>
32 #include <mach/zynq_soc.h>
36 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
37 * and use same offsets for Timer 2
39 #define XTTCPSS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
40 #define XTTCPSS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
41 #define XTTCPSS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
42 #define XTTCPSS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
43 #define XTTCPSS_MATCH_1_OFFSET 0x30 /* Match 1 Value Reg, RW */
44 #define XTTCPSS_MATCH_2_OFFSET 0x3C /* Match 2 Value Reg, RW */
45 #define XTTCPSS_MATCH_3_OFFSET 0x48 /* Match 3 Value Reg, RW */
46 #define XTTCPSS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
47 #define XTTCPSS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
49 #define XTTCPSS_CNT_CNTRL_DISABLE_MASK 0x1
51 /* Setup the timers to use pre-scaling, using a fixed value for now that will
52 * work across most input frequency, but it may need to be more dynamic
54 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
55 #define PRESCALE 2048 /* The exponent must match this */
56 #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
57 #define CLK_CNTRL_PRESCALE_EN 1
58 #define CNT_CNTRL_RESET (1<<4)
61 * struct xttcpss_timer - This definition defines local timer structure
63 * @base_addr: Base address of timer
65 struct xttcpss_timer {
66 void __iomem *base_addr;
69 struct xttcpss_timer_clocksource {
70 struct xttcpss_timer xttc;
71 struct clocksource cs;
74 #define to_xttcpss_timer_clksrc(x) \
75 container_of(x, struct xttcpss_timer_clocksource, cs)
77 struct xttcpss_timer_clockevent {
78 struct xttcpss_timer xttc;
79 struct clock_event_device ce;
83 #define to_xttcpss_timer_clkevent(x) \
84 container_of(x, struct xttcpss_timer_clockevent, ce)
87 * xttcpss_set_interval - Set the timer interval value
89 * @timer: Pointer to the timer instance
90 * @cycles: Timer interval ticks
92 static void xttcpss_set_interval(struct xttcpss_timer *timer,
97 /* Disable the counter, set the counter value and re-enable counter */
98 ctrl_reg = __raw_readl(timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
99 ctrl_reg |= XTTCPSS_CNT_CNTRL_DISABLE_MASK;
100 __raw_writel(ctrl_reg, timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
102 __raw_writel(cycles, timer->base_addr + XTTCPSS_INTR_VAL_OFFSET);
104 /* Reset the counter (0x10) so that it starts from 0, one-shot
105 mode makes this needed for timing to be right. */
106 ctrl_reg |= CNT_CNTRL_RESET;
107 ctrl_reg &= ~XTTCPSS_CNT_CNTRL_DISABLE_MASK;
108 __raw_writel(ctrl_reg, timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
112 * xttcpss_clock_event_interrupt - Clock event timer interrupt handler
114 * @irq: IRQ number of the Timer
115 * @dev_id: void pointer to the xttcpss_timer instance
117 * returns: Always IRQ_HANDLED - success
119 static irqreturn_t xttcpss_clock_event_interrupt(int irq, void *dev_id)
121 struct xttcpss_timer_clockevent *xttce = dev_id;
122 struct xttcpss_timer *timer = &xttce->xttc;
124 /* Acknowledge the interrupt and call event handler */
125 __raw_writel(__raw_readl(timer->base_addr + XTTCPSS_ISR_OFFSET),
126 timer->base_addr + XTTCPSS_ISR_OFFSET);
128 xttce->ce.event_handler(&xttce->ce);
134 * __xttc_clocksource_read - Reads the timer counter register
136 * returns: Current timer counter register value
138 static cycle_t __xttc_clocksource_read(struct clocksource *cs)
140 struct xttcpss_timer *timer = &to_xttcpss_timer_clksrc(cs)->xttc;
142 return (cycle_t)__raw_readl(timer->base_addr +
143 XTTCPSS_COUNT_VAL_OFFSET);
147 * xttcpss_set_next_event - Sets the time interval for next event
149 * @cycles: Timer interval ticks
150 * @evt: Address of clock event instance
152 * returns: Always 0 - success
154 static int xttcpss_set_next_event(unsigned long cycles,
155 struct clock_event_device *evt)
157 struct xttcpss_timer_clockevent *xttce = to_xttcpss_timer_clkevent(evt);
158 struct xttcpss_timer *timer = &xttce->xttc;
160 xttcpss_set_interval(timer, cycles);
165 * xttcpss_set_mode - Sets the mode of timer
167 * @mode: Mode to be set
168 * @evt: Address of clock event instance
170 static void xttcpss_set_mode(enum clock_event_mode mode,
171 struct clock_event_device *evt)
173 struct xttcpss_timer_clockevent *xttce = to_xttcpss_timer_clkevent(evt);
174 struct xttcpss_timer *timer = &xttce->xttc;
178 case CLOCK_EVT_MODE_PERIODIC:
179 xttcpss_set_interval(timer,
180 DIV_ROUND_CLOSEST(clk_get_rate(xttce->clk),
183 case CLOCK_EVT_MODE_ONESHOT:
184 case CLOCK_EVT_MODE_UNUSED:
185 case CLOCK_EVT_MODE_SHUTDOWN:
186 ctrl_reg = __raw_readl(timer->base_addr +
187 XTTCPSS_CNT_CNTRL_OFFSET);
188 ctrl_reg |= XTTCPSS_CNT_CNTRL_DISABLE_MASK;
189 __raw_writel(ctrl_reg,
190 timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
192 case CLOCK_EVT_MODE_RESUME:
193 ctrl_reg = __raw_readl(timer->base_addr +
194 XTTCPSS_CNT_CNTRL_OFFSET);
195 ctrl_reg &= ~XTTCPSS_CNT_CNTRL_DISABLE_MASK;
196 __raw_writel(ctrl_reg,
197 timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
202 static void __init zynq_ttc_setup_clocksource(struct device_node *np,
205 struct xttcpss_timer_clocksource *ttccs;
210 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
214 err = of_property_read_u32(np, "reg", ®);
218 clk = of_clk_get_by_name(np, "cpu_1x");
219 if (WARN_ON(IS_ERR(clk)))
222 err = clk_prepare_enable(clk);
226 ttccs->xttc.base_addr = base + reg * 4;
228 ttccs->cs.name = np->name;
229 ttccs->cs.rating = 200;
230 ttccs->cs.read = __xttc_clocksource_read;
231 ttccs->cs.mask = CLOCKSOURCE_MASK(16);
232 ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
234 __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPSS_IER_OFFSET);
235 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
236 ttccs->xttc.base_addr + XTTCPSS_CLK_CNTRL_OFFSET);
237 __raw_writel(CNT_CNTRL_RESET,
238 ttccs->xttc.base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
240 err = clocksource_register_hz(&ttccs->cs, clk_get_rate(clk) / PRESCALE);
245 static void __init zynq_ttc_setup_clockevent(struct device_node *np,
248 struct xttcpss_timer_clockevent *ttcce;
252 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
256 err = of_property_read_u32(np, "reg", ®);
260 ttcce->xttc.base_addr = base + reg * 4;
262 ttcce->clk = of_clk_get_by_name(np, "cpu_1x");
263 if (WARN_ON(IS_ERR(ttcce->clk)))
266 err = clk_prepare_enable(ttcce->clk);
270 irq = irq_of_parse_and_map(np, 0);
274 ttcce->ce.name = np->name;
275 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
276 ttcce->ce.set_next_event = xttcpss_set_next_event;
277 ttcce->ce.set_mode = xttcpss_set_mode;
278 ttcce->ce.rating = 200;
281 __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
282 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
283 ttcce->xttc.base_addr + XTTCPSS_CLK_CNTRL_OFFSET);
284 __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPSS_IER_OFFSET);
286 err = request_irq(irq, xttcpss_clock_event_interrupt, IRQF_TIMER,
291 clockevents_config_and_register(&ttcce->ce,
292 clk_get_rate(ttcce->clk) / PRESCALE,
296 static const __initconst struct of_device_id zynq_ttc_match[] = {
297 { .compatible = "xlnx,ttc-counter-clocksource",
298 .data = zynq_ttc_setup_clocksource, },
299 { .compatible = "xlnx,ttc-counter-clockevent",
300 .data = zynq_ttc_setup_clockevent, },
305 * xttcpss_timer_init - Initialize the timer
307 * Initializes the timer hardware and register the clock source and clock event
308 * timers with Linux kernal timer framework
310 void __init xttcpss_timer_init(void)
312 struct device_node *np;
314 for_each_compatible_node(np, NULL, "xlnx,ttc") {
315 struct device_node *np_chld;
318 base = of_iomap(np, 0);
322 for_each_available_child_of_node(np, np_chld) {
323 int (*cb)(struct device_node *np, void __iomem *base);
324 const struct of_device_id *match;
326 match = of_match_node(zynq_ttc_match, np_chld);