blob: f1d224bf162d033588e55105a445bd7ad93477b8 [file] [log] [blame]
John Linnb85a3ef2011-06-20 11:47:27 -06001/*
2 * This file contains driver for the Xilinx PS Timer Counter IP.
3 *
4 * Copyright (C) 2011 Xilinx
5 *
6 * based on arch/mips/kernel/time.c timer driver
7 *
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.
11 *
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.
16 */
17
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>
25#include <linux/io.h>
Josh Cartwright91dc9852012-10-31 13:56:14 -060026#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_irq.h>
29#include <linux/slab.h>
30#include <linux/clk-provider.h>
John Linnb85a3ef2011-06-20 11:47:27 -060031
John Linnb85a3ef2011-06-20 11:47:27 -060032#include "common.h"
33
John Linnb85a3ef2011-06-20 11:47:27 -060034/*
35 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
36 * and use same offsets for Timer 2
37 */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080038#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 */
John Linnb85a3ef2011-06-20 11:47:27 -060047
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080048#define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
John Linnb85a3ef2011-06-20 11:47:27 -060049
Josh Cartwright91dc9852012-10-31 13:56:14 -060050/* 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
52 */
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)
John Linnb85a3ef2011-06-20 11:47:27 -060058
59/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080060 * struct xttcps_timer - This definition defines local timer structure
John Linnb85a3ef2011-06-20 11:47:27 -060061 *
62 * @base_addr: Base address of timer
63 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080064struct xttcps_timer {
Josh Cartwright91dc9852012-10-31 13:56:14 -060065 void __iomem *base_addr;
John Linnb85a3ef2011-06-20 11:47:27 -060066};
67
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080068struct xttcps_timer_clocksource {
69 struct xttcps_timer xttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060070 struct clocksource cs;
71};
72
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080073#define to_xttcps_timer_clksrc(x) \
74 container_of(x, struct xttcps_timer_clocksource, cs)
Josh Cartwright91dc9852012-10-31 13:56:14 -060075
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080076struct xttcps_timer_clockevent {
77 struct xttcps_timer xttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060078 struct clock_event_device ce;
79 struct clk *clk;
80};
81
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080082#define to_xttcps_timer_clkevent(x) \
83 container_of(x, struct xttcps_timer_clockevent, ce)
John Linnb85a3ef2011-06-20 11:47:27 -060084
85/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080086 * xttcps_set_interval - Set the timer interval value
John Linnb85a3ef2011-06-20 11:47:27 -060087 *
88 * @timer: Pointer to the timer instance
89 * @cycles: Timer interval ticks
90 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080091static void xttcps_set_interval(struct xttcps_timer *timer,
John Linnb85a3ef2011-06-20 11:47:27 -060092 unsigned long cycles)
93{
94 u32 ctrl_reg;
95
96 /* Disable the counter, set the counter value and re-enable counter */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080097 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);
John Linnb85a3ef2011-06-20 11:47:27 -0600100
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800101 __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600102
103 /* Reset the counter (0x10) so that it starts from 0, one-shot
104 mode makes this needed for timing to be right. */
Josh Cartwright91dc9852012-10-31 13:56:14 -0600105 ctrl_reg |= CNT_CNTRL_RESET;
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800106 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
107 __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600108}
109
110/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800111 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
John Linnb85a3ef2011-06-20 11:47:27 -0600112 *
113 * @irq: IRQ number of the Timer
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800114 * @dev_id: void pointer to the xttcps_timer instance
John Linnb85a3ef2011-06-20 11:47:27 -0600115 *
116 * returns: Always IRQ_HANDLED - success
117 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800118static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
John Linnb85a3ef2011-06-20 11:47:27 -0600119{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800120 struct xttcps_timer_clockevent *xttce = dev_id;
121 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600122
123 /* Acknowledge the interrupt and call event handler */
Soren Brinkmannaf7f0322012-12-19 10:18:37 -0800124 __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600125
Josh Cartwright91dc9852012-10-31 13:56:14 -0600126 xttce->ce.event_handler(&xttce->ce);
John Linnb85a3ef2011-06-20 11:47:27 -0600127
128 return IRQ_HANDLED;
129}
130
John Linnb85a3ef2011-06-20 11:47:27 -0600131/**
Josh Cartwright91dc9852012-10-31 13:56:14 -0600132 * __xttc_clocksource_read - Reads the timer counter register
John Linnb85a3ef2011-06-20 11:47:27 -0600133 *
134 * returns: Current timer counter register value
135 **/
Josh Cartwright91dc9852012-10-31 13:56:14 -0600136static cycle_t __xttc_clocksource_read(struct clocksource *cs)
John Linnb85a3ef2011-06-20 11:47:27 -0600137{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800138 struct xttcps_timer *timer = &to_xttcps_timer_clksrc(cs)->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600139
140 return (cycle_t)__raw_readl(timer->base_addr +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800141 XTTCPS_COUNT_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600142}
143
John Linnb85a3ef2011-06-20 11:47:27 -0600144/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800145 * xttcps_set_next_event - Sets the time interval for next event
John Linnb85a3ef2011-06-20 11:47:27 -0600146 *
147 * @cycles: Timer interval ticks
148 * @evt: Address of clock event instance
149 *
150 * returns: Always 0 - success
151 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800152static int xttcps_set_next_event(unsigned long cycles,
John Linnb85a3ef2011-06-20 11:47:27 -0600153 struct clock_event_device *evt)
154{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800155 struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
156 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600157
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800158 xttcps_set_interval(timer, cycles);
John Linnb85a3ef2011-06-20 11:47:27 -0600159 return 0;
160}
161
162/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800163 * xttcps_set_mode - Sets the mode of timer
John Linnb85a3ef2011-06-20 11:47:27 -0600164 *
165 * @mode: Mode to be set
166 * @evt: Address of clock event instance
167 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800168static void xttcps_set_mode(enum clock_event_mode mode,
John Linnb85a3ef2011-06-20 11:47:27 -0600169 struct clock_event_device *evt)
170{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800171 struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
172 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600173 u32 ctrl_reg;
174
175 switch (mode) {
176 case CLOCK_EVT_MODE_PERIODIC:
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800177 xttcps_set_interval(timer,
Josh Cartwright91dc9852012-10-31 13:56:14 -0600178 DIV_ROUND_CLOSEST(clk_get_rate(xttce->clk),
179 PRESCALE * HZ));
John Linnb85a3ef2011-06-20 11:47:27 -0600180 break;
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 +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800185 XTTCPS_CNT_CNTRL_OFFSET);
186 ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
John Linnb85a3ef2011-06-20 11:47:27 -0600187 __raw_writel(ctrl_reg,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800188 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600189 break;
190 case CLOCK_EVT_MODE_RESUME:
191 ctrl_reg = __raw_readl(timer->base_addr +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800192 XTTCPS_CNT_CNTRL_OFFSET);
193 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
John Linnb85a3ef2011-06-20 11:47:27 -0600194 __raw_writel(ctrl_reg,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800195 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600196 break;
197 }
198}
199
Josh Cartwright91dc9852012-10-31 13:56:14 -0600200static void __init zynq_ttc_setup_clocksource(struct device_node *np,
201 void __iomem *base)
202{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800203 struct xttcps_timer_clocksource *ttccs;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600204 struct clk *clk;
205 int err;
206 u32 reg;
207
208 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
209 if (WARN_ON(!ttccs))
210 return;
211
212 err = of_property_read_u32(np, "reg", &reg);
213 if (WARN_ON(err))
214 return;
215
216 clk = of_clk_get_by_name(np, "cpu_1x");
217 if (WARN_ON(IS_ERR(clk)))
218 return;
219
220 err = clk_prepare_enable(clk);
221 if (WARN_ON(err))
222 return;
223
224 ttccs->xttc.base_addr = base + reg * 4;
225
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;
231
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800232 __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPS_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600233 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800234 ttccs->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600235 __raw_writel(CNT_CNTRL_RESET,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800236 ttccs->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600237
238 err = clocksource_register_hz(&ttccs->cs, clk_get_rate(clk) / PRESCALE);
239 if (WARN_ON(err))
240 return;
241}
242
243static void __init zynq_ttc_setup_clockevent(struct device_node *np,
244 void __iomem *base)
245{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800246 struct xttcps_timer_clockevent *ttcce;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600247 int err, irq;
248 u32 reg;
249
250 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
251 if (WARN_ON(!ttcce))
252 return;
253
254 err = of_property_read_u32(np, "reg", &reg);
255 if (WARN_ON(err))
256 return;
257
258 ttcce->xttc.base_addr = base + reg * 4;
259
260 ttcce->clk = of_clk_get_by_name(np, "cpu_1x");
261 if (WARN_ON(IS_ERR(ttcce->clk)))
262 return;
263
264 err = clk_prepare_enable(ttcce->clk);
265 if (WARN_ON(err))
266 return;
267
268 irq = irq_of_parse_and_map(np, 0);
269 if (WARN_ON(!irq))
270 return;
271
272 ttcce->ce.name = np->name;
273 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800274 ttcce->ce.set_next_event = xttcps_set_next_event;
275 ttcce->ce.set_mode = xttcps_set_mode;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600276 ttcce->ce.rating = 200;
277 ttcce->ce.irq = irq;
278
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800279 __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600280 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800281 ttcce->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
282 __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPS_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600283
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800284 err = request_irq(irq, xttcps_clock_event_interrupt, IRQF_TIMER,
Josh Cartwright91dc9852012-10-31 13:56:14 -0600285 np->name, ttcce);
286 if (WARN_ON(err))
287 return;
288
289 clockevents_config_and_register(&ttcce->ce,
290 clk_get_rate(ttcce->clk) / PRESCALE,
291 1, 0xfffe);
292}
293
294static 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, },
299 {}
John Linnb85a3ef2011-06-20 11:47:27 -0600300};
301
302/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800303 * xttcps_timer_init - Initialize the timer
John Linnb85a3ef2011-06-20 11:47:27 -0600304 *
305 * Initializes the timer hardware and register the clock source and clock event
306 * timers with Linux kernal timer framework
307 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800308void __init xttcps_timer_init(void)
John Linnb85a3ef2011-06-20 11:47:27 -0600309{
Josh Cartwright91dc9852012-10-31 13:56:14 -0600310 struct device_node *np;
John Linnb85a3ef2011-06-20 11:47:27 -0600311
Josh Cartwright91dc9852012-10-31 13:56:14 -0600312 for_each_compatible_node(np, NULL, "xlnx,ttc") {
313 struct device_node *np_chld;
314 void __iomem *base;
John Linnb85a3ef2011-06-20 11:47:27 -0600315
Josh Cartwright91dc9852012-10-31 13:56:14 -0600316 base = of_iomap(np, 0);
317 if (WARN_ON(!base))
318 return;
John Linnb85a3ef2011-06-20 11:47:27 -0600319
Josh Cartwright91dc9852012-10-31 13:56:14 -0600320 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;
John Linnb85a3ef2011-06-20 11:47:27 -0600323
Josh Cartwright91dc9852012-10-31 13:56:14 -0600324 match = of_match_node(zynq_ttc_match, np_chld);
325 if (match) {
326 cb = match->data;
327 cb(np_chld, base);
328 }
329 }
330 }
John Linnb85a3ef2011-06-20 11:47:27 -0600331}