aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-zynq/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm/mach-zynq/timer.c')
-rw-r--r--arch/arm/mach-zynq/timer.c297
1 files changed, 166 insertions, 131 deletions
diff --git a/arch/arm/mach-zynq/timer.c b/arch/arm/mach-zynq/timer.c
index c2c96cc7d6e7..9662306aa12f 100644
--- a/arch/arm/mach-zynq/timer.c
+++ b/arch/arm/mach-zynq/timer.c
@@ -23,32 +23,15 @@
23#include <linux/clocksource.h> 23#include <linux/clocksource.h>
24#include <linux/clockchips.h> 24#include <linux/clockchips.h>
25#include <linux/io.h> 25#include <linux/io.h>
26#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>
26 31
27#include <asm/mach/time.h>
28#include <mach/zynq_soc.h> 32#include <mach/zynq_soc.h>
29#include "common.h" 33#include "common.h"
30 34
31#define IRQ_TIMERCOUNTER0 42
32
33/*
34 * This driver configures the 2 16-bit count-up timers as follows:
35 *
36 * T1: Timer 1, clocksource for generic timekeeping
37 * T2: Timer 2, clockevent source for hrtimers
38 * T3: Timer 3, <unused>
39 *
40 * The input frequency to the timer module for emulation is 2.5MHz which is
41 * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
42 * the timers are clocked at 78.125KHz (12.8 us resolution).
43 *
44 * The input frequency to the timer module in silicon will be 200MHz. With the
45 * pre-scaler of 32, the timers are clocked at 6.25MHz (160ns resolution).
46 */
47#define XTTCPSS_CLOCKSOURCE 0 /* Timer 1 as a generic timekeeping */
48#define XTTCPSS_CLOCKEVENT 1 /* Timer 2 as a clock event */
49
50#define XTTCPSS_TIMER_BASE TTC0_BASE
51#define XTTCPCC_EVENT_TIMER_IRQ (IRQ_TIMERCOUNTER0 + 1)
52/* 35/*
53 * Timer Register Offset Definitions of Timer 1, Increment base address by 4 36 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
54 * and use same offsets for Timer 2 37 * and use same offsets for Timer 2
@@ -65,9 +48,14 @@
65 48
66#define XTTCPSS_CNT_CNTRL_DISABLE_MASK 0x1 49#define XTTCPSS_CNT_CNTRL_DISABLE_MASK 0x1
67 50
68/* Setup the timers to use pre-scaling */ 51/* Setup the timers to use pre-scaling, using a fixed value for now that will
69 52 * work across most input frequency, but it may need to be more dynamic
70#define TIMER_RATE (PERIPHERAL_CLOCK_RATE / 32) 53 */
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)
71 59
72/** 60/**
73 * struct xttcpss_timer - This definition defines local timer structure 61 * struct xttcpss_timer - This definition defines local timer structure
@@ -75,11 +63,25 @@
75 * @base_addr: Base address of timer 63 * @base_addr: Base address of timer
76 **/ 64 **/
77struct xttcpss_timer { 65struct xttcpss_timer {
78 void __iomem *base_addr; 66 void __iomem *base_addr;
79}; 67};
80 68
81static struct xttcpss_timer timers[2]; 69struct xttcpss_timer_clocksource {
82static struct clock_event_device xttcpss_clockevent; 70 struct xttcpss_timer xttc;
71 struct clocksource cs;
72};
73
74#define to_xttcpss_timer_clksrc(x) \
75 container_of(x, struct xttcpss_timer_clocksource, cs)
76
77struct xttcpss_timer_clockevent {
78 struct xttcpss_timer xttc;
79 struct clock_event_device ce;
80 struct clk *clk;
81};
82
83#define to_xttcpss_timer_clkevent(x) \
84 container_of(x, struct xttcpss_timer_clockevent, ce)
83 85
84/** 86/**
85 * xttcpss_set_interval - Set the timer interval value 87 * xttcpss_set_interval - Set the timer interval value
@@ -101,7 +103,7 @@ static void xttcpss_set_interval(struct xttcpss_timer *timer,
101 103
102 /* Reset the counter (0x10) so that it starts from 0, one-shot 104 /* Reset the counter (0x10) so that it starts from 0, one-shot
103 mode makes this needed for timing to be right. */ 105 mode makes this needed for timing to be right. */
104 ctrl_reg |= 0x10; 106 ctrl_reg |= CNT_CNTRL_RESET;
105 ctrl_reg &= ~XTTCPSS_CNT_CNTRL_DISABLE_MASK; 107 ctrl_reg &= ~XTTCPSS_CNT_CNTRL_DISABLE_MASK;
106 __raw_writel(ctrl_reg, timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET); 108 __raw_writel(ctrl_reg, timer->base_addr + XTTCPSS_CNT_CNTRL_OFFSET);
107} 109}
@@ -116,90 +118,31 @@ static void xttcpss_set_interval(struct xttcpss_timer *timer,
116 **/ 118 **/
117static irqreturn_t xttcpss_clock_event_interrupt(int irq, void *dev_id) 119static irqreturn_t xttcpss_clock_event_interrupt(int irq, void *dev_id)
118{ 120{
119 struct clock_event_device *evt = &xttcpss_clockevent; 121 struct xttcpss_timer_clockevent *xttce = dev_id;
120 struct xttcpss_timer *timer = dev_id; 122 struct xttcpss_timer *timer = &xttce->xttc;
121 123
122 /* Acknowledge the interrupt and call event handler */ 124 /* Acknowledge the interrupt and call event handler */
123 __raw_writel(__raw_readl(timer->base_addr + XTTCPSS_ISR_OFFSET), 125 __raw_writel(__raw_readl(timer->base_addr + XTTCPSS_ISR_OFFSET),
124 timer->base_addr + XTTCPSS_ISR_OFFSET); 126 timer->base_addr + XTTCPSS_ISR_OFFSET);
125 127
126 evt->event_handler(evt); 128 xttce->ce.event_handler(&xttce->ce);
127 129
128 return IRQ_HANDLED; 130 return IRQ_HANDLED;
129} 131}
130 132
131static struct irqaction event_timer_irq = {
132 .name = "xttcpss clockevent",
133 .flags = IRQF_DISABLED | IRQF_TIMER,
134 .handler = xttcpss_clock_event_interrupt,
135};
136
137/** 133/**
138 * xttcpss_timer_hardware_init - Initialize the timer hardware 134 * __xttc_clocksource_read - Reads the timer counter register
139 *
140 * Initialize the hardware to start the clock source, get the clock
141 * event timer ready to use, and hook up the interrupt.
142 **/
143static void __init xttcpss_timer_hardware_init(void)
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 timers[XTTCPSS_CLOCKSOURCE].base_addr = XTTCPSS_TIMER_BASE;
150
151 __raw_writel(0x0, timers[XTTCPSS_CLOCKSOURCE].base_addr +
152 XTTCPSS_IER_OFFSET);
153 __raw_writel(0x9, timers[XTTCPSS_CLOCKSOURCE].base_addr +
154 XTTCPSS_CLK_CNTRL_OFFSET);
155 __raw_writel(0x10, timers[XTTCPSS_CLOCKSOURCE].base_addr +
156 XTTCPSS_CNT_CNTRL_OFFSET);
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
163 timers[XTTCPSS_CLOCKEVENT].base_addr = XTTCPSS_TIMER_BASE + 4;
164
165 __raw_writel(0x23, timers[XTTCPSS_CLOCKEVENT].base_addr +
166 XTTCPSS_CNT_CNTRL_OFFSET);
167 __raw_writel(0x9, timers[XTTCPSS_CLOCKEVENT].base_addr +
168 XTTCPSS_CLK_CNTRL_OFFSET);
169 __raw_writel(0x1, timers[XTTCPSS_CLOCKEVENT].base_addr +
170 XTTCPSS_IER_OFFSET);
171
172 /* Setup IRQ the clock event timer */
173 event_timer_irq.dev_id = &timers[XTTCPSS_CLOCKEVENT];
174 setup_irq(XTTCPCC_EVENT_TIMER_IRQ, &event_timer_irq);
175}
176
177/**
178 * __raw_readl_cycles - Reads the timer counter register
179 * 135 *
180 * returns: Current timer counter register value 136 * returns: Current timer counter register value
181 **/ 137 **/
182static cycle_t __raw_readl_cycles(struct clocksource *cs) 138static cycle_t __xttc_clocksource_read(struct clocksource *cs)
183{ 139{
184 struct xttcpss_timer *timer = &timers[XTTCPSS_CLOCKSOURCE]; 140 struct xttcpss_timer *timer = &to_xttcpss_timer_clksrc(cs)->xttc;
185 141
186 return (cycle_t)__raw_readl(timer->base_addr + 142 return (cycle_t)__raw_readl(timer->base_addr +
187 XTTCPSS_COUNT_VAL_OFFSET); 143 XTTCPSS_COUNT_VAL_OFFSET);
188} 144}
189 145
190
191/*
192 * Instantiate and initialize the clock source structure
193 */
194static struct clocksource clocksource_xttcpss = {
195 .name = "xttcpss_timer1",
196 .rating = 200, /* Reasonable clock source */
197 .read = __raw_readl_cycles,
198 .mask = CLOCKSOURCE_MASK(16),
199 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
200};
201
202
203/** 146/**
204 * xttcpss_set_next_event - Sets the time interval for next event 147 * xttcpss_set_next_event - Sets the time interval for next event
205 * 148 *
@@ -211,7 +154,8 @@ static struct clocksource clocksource_xttcpss = {
211static int xttcpss_set_next_event(unsigned long cycles, 154static int xttcpss_set_next_event(unsigned long cycles,
212 struct clock_event_device *evt) 155 struct clock_event_device *evt)
213{ 156{
214 struct xttcpss_timer *timer = &timers[XTTCPSS_CLOCKEVENT]; 157 struct xttcpss_timer_clockevent *xttce = to_xttcpss_timer_clkevent(evt);
158 struct xttcpss_timer *timer = &xttce->xttc;
215 159
216 xttcpss_set_interval(timer, cycles); 160 xttcpss_set_interval(timer, cycles);
217 return 0; 161 return 0;
@@ -226,12 +170,15 @@ static int xttcpss_set_next_event(unsigned long cycles,
226static void xttcpss_set_mode(enum clock_event_mode mode, 170static void xttcpss_set_mode(enum clock_event_mode mode,
227 struct clock_event_device *evt) 171 struct clock_event_device *evt)
228{ 172{
229 struct xttcpss_timer *timer = &timers[XTTCPSS_CLOCKEVENT]; 173 struct xttcpss_timer_clockevent *xttce = to_xttcpss_timer_clkevent(evt);
174 struct xttcpss_timer *timer = &xttce->xttc;
230 u32 ctrl_reg; 175 u32 ctrl_reg;
231 176
232 switch (mode) { 177 switch (mode) {
233 case CLOCK_EVT_MODE_PERIODIC: 178 case CLOCK_EVT_MODE_PERIODIC:
234 xttcpss_set_interval(timer, TIMER_RATE / HZ); 179 xttcpss_set_interval(timer,
180 DIV_ROUND_CLOSEST(clk_get_rate(xttce->clk),
181 PRESCALE * HZ));
235 break; 182 break;
236 case CLOCK_EVT_MODE_ONESHOT: 183 case CLOCK_EVT_MODE_ONESHOT:
237 case CLOCK_EVT_MODE_UNUSED: 184 case CLOCK_EVT_MODE_UNUSED:
@@ -252,15 +199,106 @@ static void xttcpss_set_mode(enum clock_event_mode mode,
252 } 199 }
253} 200}
254 201
255/* 202static void __init zynq_ttc_setup_clocksource(struct device_node *np,
256 * Instantiate and initialize the clock event structure 203 void __iomem *base)
257 */ 204{
258static struct clock_event_device xttcpss_clockevent = { 205 struct xttcpss_timer_clocksource *ttccs;
259 .name = "xttcpss_timer2", 206 struct clk *clk;
260 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, 207 int err;
261 .set_next_event = xttcpss_set_next_event, 208 u32 reg;
262 .set_mode = xttcpss_set_mode, 209
263 .rating = 200, 210 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
211 if (WARN_ON(!ttccs))
212 return;
213
214 err = of_property_read_u32(np, "reg", &reg);
215 if (WARN_ON(err))
216 return;
217
218 clk = of_clk_get_by_name(np, "cpu_1x");
219 if (WARN_ON(IS_ERR(clk)))
220 return;
221
222 err = clk_prepare_enable(clk);
223 if (WARN_ON(err))
224 return;
225
226 ttccs->xttc.base_addr = base + reg * 4;
227
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;
233
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);
239
240 err = clocksource_register_hz(&ttccs->cs, clk_get_rate(clk) / PRESCALE);
241 if (WARN_ON(err))
242 return;
243}
244
245static void __init zynq_ttc_setup_clockevent(struct device_node *np,
246 void __iomem *base)
247{
248 struct xttcpss_timer_clockevent *ttcce;
249 int err, irq;
250 u32 reg;
251
252 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
253 if (WARN_ON(!ttcce))
254 return;
255
256 err = of_property_read_u32(np, "reg", &reg);
257 if (WARN_ON(err))
258 return;
259
260 ttcce->xttc.base_addr = base + reg * 4;
261
262 ttcce->clk = of_clk_get_by_name(np, "cpu_1x");
263 if (WARN_ON(IS_ERR(ttcce->clk)))
264 return;
265
266 err = clk_prepare_enable(ttcce->clk);
267 if (WARN_ON(err))
268 return;
269
270 irq = irq_of_parse_and_map(np, 0);
271 if (WARN_ON(!irq))
272 return;
273
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;
279 ttcce->ce.irq = irq;
280
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);
285
286 err = request_irq(irq, xttcpss_clock_event_interrupt, IRQF_TIMER,
287 np->name, ttcce);
288 if (WARN_ON(err))
289 return;
290
291 clockevents_config_and_register(&ttcce->ce,
292 clk_get_rate(ttcce->clk) / PRESCALE,
293 1, 0xfffe);
294}
295
296static 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, },
301 {}
264}; 302};
265 303
266/** 304/**
@@ -269,30 +307,27 @@ static struct clock_event_device xttcpss_clockevent = {
269 * Initializes the timer hardware and register the clock source and clock event 307 * Initializes the timer hardware and register the clock source and clock event
270 * timers with Linux kernal timer framework 308 * timers with Linux kernal timer framework
271 **/ 309 **/
272static void __init xttcpss_timer_init(void) 310void __init xttcpss_timer_init(void)
273{ 311{
274 xttcpss_timer_hardware_init(); 312 struct device_node *np;
275 clocksource_register_hz(&clocksource_xttcpss, TIMER_RATE); 313
276 314 for_each_compatible_node(np, NULL, "xlnx,ttc") {
277 /* Calculate the parameters to allow the clockevent to operate using 315 struct device_node *np_chld;
278 integer math 316 void __iomem *base;
279 */ 317
280 clockevents_calc_mult_shift(&xttcpss_clockevent, TIMER_RATE, 4); 318 base = of_iomap(np, 0);
281 319 if (WARN_ON(!base))
282 xttcpss_clockevent.max_delta_ns = 320 return;
283 clockevent_delta2ns(0xfffe, &xttcpss_clockevent); 321
284 xttcpss_clockevent.min_delta_ns = 322 for_each_available_child_of_node(np, np_chld) {
285 clockevent_delta2ns(1, &xttcpss_clockevent); 323 int (*cb)(struct device_node *np, void __iomem *base);
286 324 const struct of_device_id *match;
287 /* Indicate that clock event is on 1st CPU as SMP boot needs it */ 325
288 326 match = of_match_node(zynq_ttc_match, np_chld);
289 xttcpss_clockevent.cpumask = cpumask_of(0); 327 if (match) {
290 clockevents_register_device(&xttcpss_clockevent); 328 cb = match->data;
329 cb(np_chld, base);
330 }
331 }
332 }
291} 333}
292
293/*
294 * Instantiate and initialize the system timer structure
295 */
296struct sys_timer xttcpss_sys_timer = {
297 .init = xttcpss_timer_init,
298};