]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - arch/arm/mach-zynq/timer.c
a760b90ff693e45b0d57d4152573757eef50a7ef
[zynq/linux.git] / arch / arm / mach-zynq / timer.c
1 /*
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/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>
24 #include "common.h"
25
26 /*
27  * This driver configures the 2 16-bit count-up timers as follows:
28  *
29  * T1: Timer 1, clocksource for generic timekeeping
30  * T2: Timer 2, clockevent source for hrtimers
31  * T3: Timer 3, <unused>
32  *
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).
36
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.
39  */
40 #define XTTCPS_CLOCKSOURCE      0       /* Timer 1 as a generic timekeeping */
41 #define XTTCPS_CLOCKEVENT       1       /* Timer 2 as a clock event */
42
43 /*
44  * Timer Register Offset Definitions of Timer 1, Increment base address by 4
45  * and use same offsets for Timer 2
46  */
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 */
53
54 #define XTTCPS_CNT_CNTRL_DISABLE_MASK   0x1
55
56 /*
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
59  */
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)
66
67 /**
68  * struct xttcps_timer - This definition defines local timer structure
69  *
70  * @base_addr:  Base address of timer
71  */
72 struct xttcps_timer {
73         void __iomem *base_addr;
74         int frequency;
75         struct clk *clk;
76         struct notifier_block clk_rate_change_nb;
77 };
78
79 static struct xttcps_timer timers[2];
80 static struct clock_event_device xttcps_clockevent;
81
82 /**
83  * xttcps_set_interval - Set the timer interval value
84  *
85  * @timer:      Pointer to the timer instance
86  * @cycles:     Timer interval ticks
87  **/
88 static void xttcps_set_interval(struct xttcps_timer *timer,
89                                         unsigned long cycles)
90 {
91         u32 ctrl_reg;
92
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);
97
98         __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET);
99
100         /*
101          * Reset the counter (0x10) so that it starts from 0, one-shot
102          * mode makes this needed for timing to be right.
103          */
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);
107 }
108
109 /**
110  * xttcps_clock_event_interrupt - Clock event timer interrupt handler
111  *
112  * @irq:        IRQ number of the Timer
113  * @dev_id:     void pointer to the xttcps_timer instance
114  *
115  * returns: Always IRQ_HANDLED - success
116  **/
117 static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
118 {
119         struct clock_event_device *evt = &xttcps_clockevent;
120         struct xttcps_timer *timer = dev_id;
121
122         /* Acknowledge the interrupt and call event handler */
123         __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
124
125         evt->event_handler(evt);
126
127         return IRQ_HANDLED;
128 }
129
130 static struct irqaction event_timer_irq = {
131         .name   = "xttcps clockevent",
132         .flags  = IRQF_DISABLED | IRQF_TIMER,
133         .handler = xttcps_clock_event_interrupt,
134 };
135
136 /**
137  * xttcps_timer_hardware_init - Initialize the timer hardware
138  *
139  * Initialize the hardware to start the clock source, get the clock
140  * event timer ready to use, and hook up the interrupt.
141  */
142 static void __init xttcps_timer_hardware_init(void)
143 {
144         /*
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.
148          */
149         __raw_writel(0x0, timers[XTTCPS_CLOCKSOURCE].base_addr +
150                                 XTTCPS_IER_OFFSET);
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);
156
157         /*
158          * Setup the clock event timer to be an interval timer which
159          * is prescaled by 32 using the interval interrupt. Leave it
160          * disabled for now.
161          */
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 +
168                         XTTCPS_IER_OFFSET);
169 }
170
171 /**
172  * __raw_readl_cycles - Reads the timer counter register
173  *
174  * returns: Current timer counter register value
175  **/
176 static cycle_t __raw_readl_cycles(struct clocksource *cs)
177 {
178         struct xttcps_timer *timer = &timers[XTTCPS_CLOCKSOURCE];
179
180         return (cycle_t)__raw_readl(timer->base_addr +
181                                 XTTCPS_COUNT_VAL_OFFSET);
182 }
183
184 /*
185  * Instantiate and initialize the clock source structure
186  */
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,
193 };
194
195 /**
196  * xttcps_set_next_event - Sets the time interval for next event
197  *
198  * @cycles:     Timer interval ticks
199  * @evt:        Address of clock event instance
200  *
201  * returns: Always 0 - success
202  **/
203 static int xttcps_set_next_event(unsigned long cycles,
204                                         struct clock_event_device *evt)
205 {
206         struct xttcps_timer *timer = &timers[XTTCPS_CLOCKEVENT];
207
208         xttcps_set_interval(timer, cycles);
209         return 0;
210 }
211
212 /**
213  * xttcps_set_mode - Sets the mode of timer
214  *
215  * @mode:       Mode to be set
216  * @evt:        Address of clock event instance
217  **/
218 static void xttcps_set_mode(enum clock_event_mode mode,
219                                         struct clock_event_device *evt)
220 {
221         struct xttcps_timer *timer = &timers[XTTCPS_CLOCKEVENT];
222         u32 ctrl_reg;
223
224         switch (mode) {
225         case CLOCK_EVT_MODE_PERIODIC:
226                 xttcps_set_interval(timer, timer->frequency / HZ);
227                 break;
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);
236                 break;
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);
243                 break;
244         }
245 }
246
247 /*
248  * Instantiate and initialize the clock event structure
249  */
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,
255         .rating         = 200,
256 };
257
258 static int xttcps_timer_rate_change_cb(struct notifier_block *nb,
259                 unsigned long event, void *data)
260 {
261         struct clk_notifier_data *ndata = data;
262
263         switch (event) {
264         case POST_RATE_CHANGE:
265         {
266                 unsigned long flags;
267
268                 timers[XTTCPS_CLOCKSOURCE].frequency =
269                         ndata->new_rate / PRESCALE;
270                 timers[XTTCPS_CLOCKEVENT].frequency =
271                         ndata->new_rate / PRESCALE;
272
273                 /*
274                  * Do whatever is necessare to maintain a proper time base
275                  *
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.
279                  *
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.
283                  *
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.
288                  */
289                 clocksource_unregister(&clocksource_xttcps);
290                 clocksource_register_hz(&clocksource_xttcps,
291                                 ndata->new_rate / PRESCALE);
292
293                 /*
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
297                  * cores.
298                  */
299                 local_irq_save(flags);
300                 clockevents_update_freq(&xttcps_clockevent,
301                                 timers[XTTCPS_CLOCKEVENT].frequency);
302                 local_irq_restore(flags);
303
304                 /* fall through */
305         }
306         case PRE_RATE_CHANGE:
307         case ABORT_RATE_CHANGE:
308         default:
309                 return NOTIFY_DONE;
310         }
311 }
312
313 /**
314  * xttcps_timer_init - Initialize the timer
315  *
316  * Initializes the timer hardware and register the clock source and clock event
317  * timers with Linux kernal timer framework
318  */
319 void __init xttcps_timer_init(void)
320 {
321         unsigned int irq;
322         struct device_node *timer = NULL;
323         void __iomem *timer_baseaddr;
324         const char * const timer_list[] = {
325                 "xlnx,ps7-ttc-1.00.a",
326                 NULL
327         };
328         struct clk *clk;
329
330         /*
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)
334          */
335         timer = of_find_compatible_node(NULL, NULL, timer_list[0]);
336         if (!timer) {
337                 pr_err("ERROR: no compatible timer found\n");
338                 BUG();
339         }
340
341         timer_baseaddr = of_iomap(timer, 0);
342         if (!timer_baseaddr) {
343                 pr_err("ERROR: invalid timer base address\n");
344                 BUG();
345         }
346
347         irq = irq_of_parse_and_map(timer, 1);
348         if (!irq || irq == NO_IRQ) {
349                 pr_err("ERROR: invalid interrupt number\n");
350                 BUG();
351         }
352
353         timers[XTTCPS_CLOCKSOURCE].base_addr = timer_baseaddr;
354         timers[XTTCPS_CLOCKEVENT].base_addr = timer_baseaddr + 4;
355
356         event_timer_irq.dev_id = &timers[XTTCPS_CLOCKEVENT];
357         setup_irq(irq, &event_timer_irq);
358
359         pr_info("%s #0 at %p, irq=%d\n", timer_list[0], timer_baseaddr, irq);
360
361         clk = clk_get_sys("CPU_1X_CLK", NULL);
362         if (IS_ERR(clk)) {
363                 pr_err("ERROR: timer input clock not found\n");
364                 BUG();
365         }
366
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");
383
384         xttcps_timer_hardware_init();
385         clocksource_register_hz(&clocksource_xttcps,
386                                 timers[XTTCPS_CLOCKSOURCE].frequency);
387
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();
394 #endif
395 }