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/clk.h>
19 #include <linux/clockchips.h>
20 #include <linux/interrupt.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_address.h>
23 #include <asm/smp_twd.h>
27 * This driver configures the 2 16-bit count-up timers as follows:
29 * T1: Timer 1, clocksource for generic timekeeping
30 * T2: Timer 2, clockevent source for hrtimers
31 * T3: Timer 3, <unused>
33 * The input frequency to the timer module for emulation is 2.5MHz which is
34 * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
35 * the timers are clocked at 78.125KHz (12.8 us resolution).
37 * The input frequency to the timer module in silicon is configurable and
38 * obtained from device tree. The pre-scaler of 32 is used.
40 #define XTTCPS_CLOCKSOURCE 0 /* Timer 1 as a generic timekeeping */
41 #define XTTCPS_CLOCKEVENT 1 /* Timer 2 as a clock event */
44 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
45 * and use same offsets for Timer 2
47 #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
48 #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
49 #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
50 #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
51 #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
52 #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
54 #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
57 * Setup the timers to use pre-scaling, using a fixed value for now that will
58 * work across most input frequency, but it may need to be more dynamic
60 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
61 #define PRESCALE 2048 /* The exponent must match this */
62 #define CLK_CNTRL_PRESCALE_EN 1
63 #define CLK_CNTRL_PRESCALE (((PRESCALE_EXPONENT - 1) << 1) | \
64 CLK_CNTRL_PRESCALE_EN)
65 #define CNT_CNTRL_RESET (1 << 4)
68 * struct xttcps_timer - This definition defines local timer structure
70 * @base_addr: Base address of timer
73 void __iomem *base_addr;
76 struct notifier_block clk_rate_change_nb;
79 static struct xttcps_timer timers[2];
80 static struct clock_event_device xttcps_clockevent;
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);
101 * Reset the counter (0x10) so that it starts from 0, one-shot
102 * mode makes this needed for timing to be right.
104 ctrl_reg |= CNT_CNTRL_RESET;
105 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
106 __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
110 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
112 * @irq: IRQ number of the Timer
113 * @dev_id: void pointer to the xttcps_timer instance
115 * returns: Always IRQ_HANDLED - success
117 static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
119 struct clock_event_device *evt = &xttcps_clockevent;
120 struct xttcps_timer *timer = dev_id;
122 /* Acknowledge the interrupt and call event handler */
123 __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
125 evt->event_handler(evt);
130 static struct irqaction event_timer_irq = {
131 .name = "xttcps clockevent",
132 .flags = IRQF_DISABLED | IRQF_TIMER,
133 .handler = xttcps_clock_event_interrupt,
137 * xttcps_timer_hardware_init - Initialize the timer hardware
139 * Initialize the hardware to start the clock source, get the clock
140 * event timer ready to use, and hook up the interrupt.
142 static void __init xttcps_timer_hardware_init(void)
145 * Setup the clock source counter to be an incrementing counter
146 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
147 * it by 32 also. Let it start running now.
149 __raw_writel(0x0, timers[XTTCPS_CLOCKSOURCE].base_addr +
151 __raw_writel(CLK_CNTRL_PRESCALE,
152 timers[XTTCPS_CLOCKSOURCE].base_addr +
153 XTTCPS_CLK_CNTRL_OFFSET);
154 __raw_writel(0x10, timers[XTTCPS_CLOCKSOURCE].base_addr +
155 XTTCPS_CNT_CNTRL_OFFSET);
158 * Setup the clock event timer to be an interval timer which
159 * is prescaled by 32 using the interval interrupt. Leave it
162 __raw_writel(0x23, timers[XTTCPS_CLOCKEVENT].base_addr +
163 XTTCPS_CNT_CNTRL_OFFSET);
164 __raw_writel(CLK_CNTRL_PRESCALE,
165 timers[XTTCPS_CLOCKEVENT].base_addr +
166 XTTCPS_CLK_CNTRL_OFFSET);
167 __raw_writel(0x1, timers[XTTCPS_CLOCKEVENT].base_addr +
172 * __raw_readl_cycles - Reads the timer counter register
174 * returns: Current timer counter register value
176 static cycle_t __raw_readl_cycles(struct clocksource *cs)
178 struct xttcps_timer *timer = &timers[XTTCPS_CLOCKSOURCE];
180 return (cycle_t)__raw_readl(timer->base_addr +
181 XTTCPS_COUNT_VAL_OFFSET);
185 * Instantiate and initialize the clock source structure
187 static struct clocksource clocksource_xttcps = {
188 .name = "xttcps_timer1",
189 .rating = 200, /* Reasonable clock source */
190 .read = __raw_readl_cycles,
191 .mask = CLOCKSOURCE_MASK(16),
192 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
196 * xttcps_set_next_event - Sets the time interval for next event
198 * @cycles: Timer interval ticks
199 * @evt: Address of clock event instance
201 * returns: Always 0 - success
203 static int xttcps_set_next_event(unsigned long cycles,
204 struct clock_event_device *evt)
206 struct xttcps_timer *timer = &timers[XTTCPS_CLOCKEVENT];
208 xttcps_set_interval(timer, cycles);
213 * xttcps_set_mode - Sets the mode of timer
215 * @mode: Mode to be set
216 * @evt: Address of clock event instance
218 static void xttcps_set_mode(enum clock_event_mode mode,
219 struct clock_event_device *evt)
221 struct xttcps_timer *timer = &timers[XTTCPS_CLOCKEVENT];
225 case CLOCK_EVT_MODE_PERIODIC:
226 xttcps_set_interval(timer, timer->frequency / HZ);
228 case CLOCK_EVT_MODE_ONESHOT:
229 case CLOCK_EVT_MODE_UNUSED:
230 case CLOCK_EVT_MODE_SHUTDOWN:
231 ctrl_reg = __raw_readl(timer->base_addr +
232 XTTCPS_CNT_CNTRL_OFFSET);
233 ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
234 __raw_writel(ctrl_reg,
235 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
237 case CLOCK_EVT_MODE_RESUME:
238 ctrl_reg = __raw_readl(timer->base_addr +
239 XTTCPS_CNT_CNTRL_OFFSET);
240 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
241 __raw_writel(ctrl_reg,
242 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
248 * Instantiate and initialize the clock event structure
250 static struct clock_event_device xttcps_clockevent = {
251 .name = "xttcps_timer2",
252 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
253 .set_next_event = xttcps_set_next_event,
254 .set_mode = xttcps_set_mode,
258 static int xttcps_timer_rate_change_cb(struct notifier_block *nb,
259 unsigned long event, void *data)
261 struct clk_notifier_data *ndata = data;
264 case POST_RATE_CHANGE:
268 timers[XTTCPS_CLOCKSOURCE].frequency =
269 ndata->new_rate / PRESCALE;
270 timers[XTTCPS_CLOCKEVENT].frequency =
271 ndata->new_rate / PRESCALE;
274 * Do whatever is necessare to maintain a proper time base
276 * I cannot find a way to adjust the currently used clocksource
277 * to the new frequency. __clocksource_updatefreq_hz() sounds
278 * good, but does not work. Not sure what's that missing.
280 * This approach works, but triggers two clocksource switches.
281 * The first after unregister to clocksource jiffies. And
282 * another one after the register to the newly registered timer.
284 * Alternatively we could 'waste' another HW timer to ping pong
285 * between clock sources. That would also use one register and
286 * one unregister call, but only trigger one clocksource switch
287 * for the cost of another HW timer used by the OS.
289 clocksource_unregister(&clocksource_xttcps);
290 clocksource_register_hz(&clocksource_xttcps,
291 ndata->new_rate / PRESCALE);
294 * clockevents_update_freq should be called with IRQ disabled on
295 * the CPU the timer provides events for. The timer we use is
296 * common to both CPUs, not sure if we need to run on both
299 local_irq_save(flags);
300 clockevents_update_freq(&xttcps_clockevent,
301 timers[XTTCPS_CLOCKEVENT].frequency);
302 local_irq_restore(flags);
306 case PRE_RATE_CHANGE:
307 case ABORT_RATE_CHANGE:
314 * xttcps_timer_init - Initialize the timer
316 * Initializes the timer hardware and register the clock source and clock event
317 * timers with Linux kernal timer framework
319 void __init xttcps_timer_init(void)
322 struct device_node *timer = NULL;
323 void __iomem *timer_baseaddr;
324 const char * const timer_list[] = {
325 "xlnx,ps7-ttc-1.00.a",
331 * Get the 1st Triple Timer Counter (TTC) block from the device tree
332 * and use it. Note that the event timer uses the interrupt and it's the
333 * 2nd TTC hence the irq_of_parse_and_map(,1)
335 timer = of_find_compatible_node(NULL, NULL, timer_list[0]);
337 pr_err("ERROR: no compatible timer found\n");
341 timer_baseaddr = of_iomap(timer, 0);
342 if (!timer_baseaddr) {
343 pr_err("ERROR: invalid timer base address\n");
347 irq = irq_of_parse_and_map(timer, 1);
348 if (!irq || irq == NO_IRQ) {
349 pr_err("ERROR: invalid interrupt number\n");
353 timers[XTTCPS_CLOCKSOURCE].base_addr = timer_baseaddr;
354 timers[XTTCPS_CLOCKEVENT].base_addr = timer_baseaddr + 4;
356 event_timer_irq.dev_id = &timers[XTTCPS_CLOCKEVENT];
357 setup_irq(irq, &event_timer_irq);
359 pr_info("%s #0 at %p, irq=%d\n", timer_list[0], timer_baseaddr, irq);
361 clk = clk_get_sys("CPU_1X_CLK", NULL);
363 pr_err("ERROR: timer input clock not found\n");
367 clk_prepare_enable(clk);
368 timers[XTTCPS_CLOCKSOURCE].clk = clk;
369 timers[XTTCPS_CLOCKEVENT].clk = clk;
370 timers[XTTCPS_CLOCKSOURCE].clk_rate_change_nb.notifier_call =
371 xttcps_timer_rate_change_cb;
372 timers[XTTCPS_CLOCKEVENT].clk_rate_change_nb.notifier_call =
373 xttcps_timer_rate_change_cb;
374 timers[XTTCPS_CLOCKSOURCE].clk_rate_change_nb.next = NULL;
375 timers[XTTCPS_CLOCKEVENT].clk_rate_change_nb.next = NULL;
376 timers[XTTCPS_CLOCKSOURCE].frequency =
377 clk_get_rate(clk) / PRESCALE;
378 timers[XTTCPS_CLOCKEVENT].frequency =
379 clk_get_rate(clk) / PRESCALE;
380 if (clk_notifier_register(clk,
381 &timers[XTTCPS_CLOCKSOURCE].clk_rate_change_nb))
382 pr_warn("Unable to register clock notifier.\n");
384 xttcps_timer_hardware_init();
385 clocksource_register_hz(&clocksource_xttcps,
386 timers[XTTCPS_CLOCKSOURCE].frequency);
388 /* Indicate that clock event is on 1st CPU as SMP boot needs it */
389 xttcps_clockevent.cpumask = cpumask_of(0);
390 clockevents_config_and_register(&xttcps_clockevent,
391 timers[XTTCPS_CLOCKEVENT].frequency, 1, 0xfffe);
392 #ifdef CONFIG_HAVE_ARM_TWD
393 twd_local_timer_of_register();