blob: 82357d94a0e02449777ac988f63cb84d1b79e7ad [file] [log] [blame]
John Linnb85a3ef2011-06-20 11:47:27 -06001/*
2 * This file contains driver for the Xilinx PS Timer Counter IP.
3 *
Michal Simeke9329002013-03-20 10:15:28 +01004 * Copyright (C) 2011-2013 Xilinx
John Linnb85a3ef2011-06-20 11:47:27 -06005 *
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
Michal Simeke9329002013-03-20 10:15:28 +010018#include <linux/clk.h>
John Linnb85a3ef2011-06-20 11:47:27 -060019#include <linux/interrupt.h>
John Linnb85a3ef2011-06-20 11:47:27 -060020#include <linux/clockchips.h>
Josh Cartwright91dc9852012-10-31 13:56:14 -060021#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/slab.h>
24#include <linux/clk-provider.h>
John Linnb85a3ef2011-06-20 11:47:27 -060025#include "common.h"
26
John Linnb85a3ef2011-06-20 11:47:27 -060027/*
Michal Simeke9329002013-03-20 10:15:28 +010028 * This driver configures the 2 16-bit count-up timers as follows:
29 *
30 * T1: Timer 1, clocksource for generic timekeeping
31 * T2: Timer 2, clockevent source for hrtimers
32 * T3: Timer 3, <unused>
33 *
34 * The input frequency to the timer module for emulation is 2.5MHz which is
35 * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
36 * the timers are clocked at 78.125KHz (12.8 us resolution).
37
38 * The input frequency to the timer module in silicon is configurable and
39 * obtained from device tree. The pre-scaler of 32 is used.
40 */
41
42/*
John Linnb85a3ef2011-06-20 11:47:27 -060043 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
44 * and use same offsets for Timer 2
45 */
Soren Brinkmannd16aaf42012-12-19 10:18:39 -080046#define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
47#define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
48#define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080049#define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080050#define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
51#define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
John Linnb85a3ef2011-06-20 11:47:27 -060052
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080053#define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
John Linnb85a3ef2011-06-20 11:47:27 -060054
Soren Brinkmann03377e52012-12-19 10:18:41 -080055/*
56 * Setup the timers to use pre-scaling, using a fixed value for now that will
Josh Cartwright91dc9852012-10-31 13:56:14 -060057 * work across most input frequency, but it may need to be more dynamic
58 */
59#define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
60#define PRESCALE 2048 /* The exponent must match this */
61#define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
62#define CLK_CNTRL_PRESCALE_EN 1
Michal Simeke9329002013-03-20 10:15:28 +010063#define CNT_CNTRL_RESET (1 << 4)
John Linnb85a3ef2011-06-20 11:47:27 -060064
65/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080066 * struct xttcps_timer - This definition defines local timer structure
John Linnb85a3ef2011-06-20 11:47:27 -060067 *
68 * @base_addr: Base address of timer
Michal Simeke9329002013-03-20 10:15:28 +010069 * @clk: Associated clock source
70 * @clk_rate_change_nb Notifier block for clock rate changes
71 */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080072struct xttcps_timer {
Michal Simeke9329002013-03-20 10:15:28 +010073 void __iomem *base_addr;
74 struct clk *clk;
75 struct notifier_block clk_rate_change_nb;
John Linnb85a3ef2011-06-20 11:47:27 -060076};
77
Michal Simeke9329002013-03-20 10:15:28 +010078#define to_xttcps_timer(x) \
79 container_of(x, struct xttcps_timer, clk_rate_change_nb)
80
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080081struct xttcps_timer_clocksource {
82 struct xttcps_timer xttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060083 struct clocksource cs;
84};
85
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080086#define to_xttcps_timer_clksrc(x) \
87 container_of(x, struct xttcps_timer_clocksource, cs)
Josh Cartwright91dc9852012-10-31 13:56:14 -060088
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080089struct xttcps_timer_clockevent {
90 struct xttcps_timer xttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060091 struct clock_event_device ce;
Josh Cartwright91dc9852012-10-31 13:56:14 -060092};
93
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080094#define to_xttcps_timer_clkevent(x) \
95 container_of(x, struct xttcps_timer_clockevent, ce)
John Linnb85a3ef2011-06-20 11:47:27 -060096
97/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -080098 * xttcps_set_interval - Set the timer interval value
John Linnb85a3ef2011-06-20 11:47:27 -060099 *
100 * @timer: Pointer to the timer instance
101 * @cycles: Timer interval ticks
102 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800103static void xttcps_set_interval(struct xttcps_timer *timer,
John Linnb85a3ef2011-06-20 11:47:27 -0600104 unsigned long cycles)
105{
106 u32 ctrl_reg;
107
108 /* Disable the counter, set the counter value and re-enable counter */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800109 ctrl_reg = __raw_readl(timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
110 ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
111 __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600112
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800113 __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600114
Soren Brinkmann03377e52012-12-19 10:18:41 -0800115 /*
116 * Reset the counter (0x10) so that it starts from 0, one-shot
117 * mode makes this needed for timing to be right.
118 */
Josh Cartwright91dc9852012-10-31 13:56:14 -0600119 ctrl_reg |= CNT_CNTRL_RESET;
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800120 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
121 __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600122}
123
124/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800125 * xttcps_clock_event_interrupt - Clock event timer interrupt handler
John Linnb85a3ef2011-06-20 11:47:27 -0600126 *
127 * @irq: IRQ number of the Timer
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800128 * @dev_id: void pointer to the xttcps_timer instance
John Linnb85a3ef2011-06-20 11:47:27 -0600129 *
130 * returns: Always IRQ_HANDLED - success
131 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800132static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
John Linnb85a3ef2011-06-20 11:47:27 -0600133{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800134 struct xttcps_timer_clockevent *xttce = dev_id;
135 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600136
137 /* Acknowledge the interrupt and call event handler */
Soren Brinkmannaf7f0322012-12-19 10:18:37 -0800138 __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600139
Josh Cartwright91dc9852012-10-31 13:56:14 -0600140 xttce->ce.event_handler(&xttce->ce);
John Linnb85a3ef2011-06-20 11:47:27 -0600141
142 return IRQ_HANDLED;
143}
144
John Linnb85a3ef2011-06-20 11:47:27 -0600145/**
Josh Cartwright91dc9852012-10-31 13:56:14 -0600146 * __xttc_clocksource_read - Reads the timer counter register
John Linnb85a3ef2011-06-20 11:47:27 -0600147 *
148 * returns: Current timer counter register value
149 **/
Josh Cartwright91dc9852012-10-31 13:56:14 -0600150static cycle_t __xttc_clocksource_read(struct clocksource *cs)
John Linnb85a3ef2011-06-20 11:47:27 -0600151{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800152 struct xttcps_timer *timer = &to_xttcps_timer_clksrc(cs)->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600153
154 return (cycle_t)__raw_readl(timer->base_addr +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800155 XTTCPS_COUNT_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600156}
157
John Linnb85a3ef2011-06-20 11:47:27 -0600158/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800159 * xttcps_set_next_event - Sets the time interval for next event
John Linnb85a3ef2011-06-20 11:47:27 -0600160 *
161 * @cycles: Timer interval ticks
162 * @evt: Address of clock event instance
163 *
164 * returns: Always 0 - success
165 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800166static int xttcps_set_next_event(unsigned long cycles,
John Linnb85a3ef2011-06-20 11:47:27 -0600167 struct clock_event_device *evt)
168{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800169 struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
170 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600171
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800172 xttcps_set_interval(timer, cycles);
John Linnb85a3ef2011-06-20 11:47:27 -0600173 return 0;
174}
175
176/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800177 * xttcps_set_mode - Sets the mode of timer
John Linnb85a3ef2011-06-20 11:47:27 -0600178 *
179 * @mode: Mode to be set
180 * @evt: Address of clock event instance
181 **/
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800182static void xttcps_set_mode(enum clock_event_mode mode,
John Linnb85a3ef2011-06-20 11:47:27 -0600183 struct clock_event_device *evt)
184{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800185 struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
186 struct xttcps_timer *timer = &xttce->xttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600187 u32 ctrl_reg;
188
189 switch (mode) {
190 case CLOCK_EVT_MODE_PERIODIC:
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800191 xttcps_set_interval(timer,
Michal Simeke9329002013-03-20 10:15:28 +0100192 DIV_ROUND_CLOSEST(clk_get_rate(xttce->xttc.clk),
193 PRESCALE * HZ));
John Linnb85a3ef2011-06-20 11:47:27 -0600194 break;
195 case CLOCK_EVT_MODE_ONESHOT:
196 case CLOCK_EVT_MODE_UNUSED:
197 case CLOCK_EVT_MODE_SHUTDOWN:
198 ctrl_reg = __raw_readl(timer->base_addr +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800199 XTTCPS_CNT_CNTRL_OFFSET);
200 ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
John Linnb85a3ef2011-06-20 11:47:27 -0600201 __raw_writel(ctrl_reg,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800202 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600203 break;
204 case CLOCK_EVT_MODE_RESUME:
205 ctrl_reg = __raw_readl(timer->base_addr +
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800206 XTTCPS_CNT_CNTRL_OFFSET);
207 ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
John Linnb85a3ef2011-06-20 11:47:27 -0600208 __raw_writel(ctrl_reg,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800209 timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600210 break;
211 }
212}
213
Michal Simeke9329002013-03-20 10:15:28 +0100214static int xttcps_rate_change_clocksource_cb(struct notifier_block *nb,
215 unsigned long event, void *data)
216{
217 struct clk_notifier_data *ndata = data;
218 struct xttcps_timer *xttcps = to_xttcps_timer(nb);
219 struct xttcps_timer_clocksource *xttccs = container_of(xttcps,
220 struct xttcps_timer_clocksource, xttc);
221
222 switch (event) {
223 case POST_RATE_CHANGE:
224 /*
225 * Do whatever is necessary to maintain a proper time base
226 *
227 * I cannot find a way to adjust the currently used clocksource
228 * to the new frequency. __clocksource_updatefreq_hz() sounds
229 * good, but does not work. Not sure what's that missing.
230 *
231 * This approach works, but triggers two clocksource switches.
232 * The first after unregister to clocksource jiffies. And
233 * another one after the register to the newly registered timer.
234 *
235 * Alternatively we could 'waste' another HW timer to ping pong
236 * between clock sources. That would also use one register and
237 * one unregister call, but only trigger one clocksource switch
238 * for the cost of another HW timer used by the OS.
239 */
240 clocksource_unregister(&xttccs->cs);
241 clocksource_register_hz(&xttccs->cs,
242 ndata->new_rate / PRESCALE);
243 /* fall through */
244 case PRE_RATE_CHANGE:
245 case ABORT_RATE_CHANGE:
246 default:
247 return NOTIFY_DONE;
248 }
249}
250
251static void __init xttc_setup_clocksource(struct clk *clk, void __iomem *base)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600252{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800253 struct xttcps_timer_clocksource *ttccs;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600254 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600255
256 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
257 if (WARN_ON(!ttccs))
258 return;
259
Michal Simeke9329002013-03-20 10:15:28 +0100260 ttccs->xttc.clk = clk;
261
262 err = clk_prepare_enable(ttccs->xttc.clk);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600263 if (WARN_ON(err))
264 return;
265
Michal Simeke9329002013-03-20 10:15:28 +0100266 ttccs->xttc.clk_rate_change_nb.notifier_call =
267 xttcps_rate_change_clocksource_cb;
268 ttccs->xttc.clk_rate_change_nb.next = NULL;
269 if (clk_notifier_register(ttccs->xttc.clk,
270 &ttccs->xttc.clk_rate_change_nb))
271 pr_warn("Unable to register clock notifier.\n");
Josh Cartwright91dc9852012-10-31 13:56:14 -0600272
Michal Simeke9329002013-03-20 10:15:28 +0100273 ttccs->xttc.base_addr = base;
274 ttccs->cs.name = "xttcps_clocksource";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600275 ttccs->cs.rating = 200;
276 ttccs->cs.read = __xttc_clocksource_read;
277 ttccs->cs.mask = CLOCKSOURCE_MASK(16);
278 ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
279
Michal Simeke9329002013-03-20 10:15:28 +0100280 /*
281 * Setup the clock source counter to be an incrementing counter
282 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
283 * it by 32 also. Let it start running now.
284 */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800285 __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPS_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600286 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800287 ttccs->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600288 __raw_writel(CNT_CNTRL_RESET,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800289 ttccs->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600290
Michal Simeke9329002013-03-20 10:15:28 +0100291 err = clocksource_register_hz(&ttccs->cs,
292 clk_get_rate(ttccs->xttc.clk) / PRESCALE);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600293 if (WARN_ON(err))
294 return;
Michal Simeke9329002013-03-20 10:15:28 +0100295
Josh Cartwright91dc9852012-10-31 13:56:14 -0600296}
297
Michal Simeke9329002013-03-20 10:15:28 +0100298static int xttcps_rate_change_clockevent_cb(struct notifier_block *nb,
299 unsigned long event, void *data)
300{
301 struct clk_notifier_data *ndata = data;
302 struct xttcps_timer *xttcps = to_xttcps_timer(nb);
303 struct xttcps_timer_clockevent *xttcce = container_of(xttcps,
304 struct xttcps_timer_clockevent, xttc);
305
306 switch (event) {
307 case POST_RATE_CHANGE:
308 {
309 unsigned long flags;
310
311 /*
312 * clockevents_update_freq should be called with IRQ disabled on
313 * the CPU the timer provides events for. The timer we use is
314 * common to both CPUs, not sure if we need to run on both
315 * cores.
316 */
317 local_irq_save(flags);
318 clockevents_update_freq(&xttcce->ce,
319 ndata->new_rate / PRESCALE);
320 local_irq_restore(flags);
321
322 /* fall through */
323 }
324 case PRE_RATE_CHANGE:
325 case ABORT_RATE_CHANGE:
326 default:
327 return NOTIFY_DONE;
328 }
329}
330
331static void __init xttc_setup_clockevent(struct clk *clk,
332 void __iomem *base, u32 irq)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600333{
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800334 struct xttcps_timer_clockevent *ttcce;
Michal Simeke9329002013-03-20 10:15:28 +0100335 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600336
337 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
338 if (WARN_ON(!ttcce))
339 return;
340
Michal Simeke9329002013-03-20 10:15:28 +0100341 ttcce->xttc.clk = clk;
342
343 err = clk_prepare_enable(ttcce->xttc.clk);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600344 if (WARN_ON(err))
345 return;
346
Michal Simeke9329002013-03-20 10:15:28 +0100347 ttcce->xttc.clk_rate_change_nb.notifier_call =
348 xttcps_rate_change_clockevent_cb;
349 ttcce->xttc.clk_rate_change_nb.next = NULL;
350 if (clk_notifier_register(ttcce->xttc.clk,
351 &ttcce->xttc.clk_rate_change_nb))
352 pr_warn("Unable to register clock notifier.\n");
Josh Cartwright91dc9852012-10-31 13:56:14 -0600353
Michal Simeke9329002013-03-20 10:15:28 +0100354 ttcce->xttc.base_addr = base;
355 ttcce->ce.name = "xttcps_clockevent";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600356 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800357 ttcce->ce.set_next_event = xttcps_set_next_event;
358 ttcce->ce.set_mode = xttcps_set_mode;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600359 ttcce->ce.rating = 200;
360 ttcce->ce.irq = irq;
Soren Brinkmann87e4ee72012-12-19 10:18:42 -0800361 ttcce->ce.cpumask = cpu_possible_mask;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600362
Michal Simeke9329002013-03-20 10:15:28 +0100363 /*
364 * Setup the clock event timer to be an interval timer which
365 * is prescaled by 32 using the interval interrupt. Leave it
366 * disabled for now.
367 */
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800368 __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600369 __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800370 ttcce->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
371 __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPS_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600372
Michal Simeke9329002013-03-20 10:15:28 +0100373 err = request_irq(irq, xttcps_clock_event_interrupt,
374 IRQF_DISABLED | IRQF_TIMER,
375 ttcce->ce.name, ttcce);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600376 if (WARN_ON(err))
377 return;
378
379 clockevents_config_and_register(&ttcce->ce,
Michal Simeke9329002013-03-20 10:15:28 +0100380 clk_get_rate(ttcce->xttc.clk) / PRESCALE, 1, 0xfffe);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600381}
382
John Linnb85a3ef2011-06-20 11:47:27 -0600383/**
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800384 * xttcps_timer_init - Initialize the timer
John Linnb85a3ef2011-06-20 11:47:27 -0600385 *
386 * Initializes the timer hardware and register the clock source and clock event
387 * timers with Linux kernal timer framework
Michal Simeke9329002013-03-20 10:15:28 +0100388 */
389static void __init xttcps_timer_init_of(struct device_node *timer)
390{
391 unsigned int irq;
392 void __iomem *timer_baseaddr;
393 struct clk *clk;
394
395 /*
396 * Get the 1st Triple Timer Counter (TTC) block from the device tree
397 * and use it. Note that the event timer uses the interrupt and it's the
398 * 2nd TTC hence the irq_of_parse_and_map(,1)
399 */
400 timer_baseaddr = of_iomap(timer, 0);
401 if (!timer_baseaddr) {
402 pr_err("ERROR: invalid timer base address\n");
403 BUG();
404 }
405
406 irq = irq_of_parse_and_map(timer, 1);
407 if (irq <= 0) {
408 pr_err("ERROR: invalid interrupt number\n");
409 BUG();
410 }
411
412 clk = of_clk_get_by_name(timer, "cpu_1x");
413 if (IS_ERR(clk)) {
414 pr_err("ERROR: timer input clock not found\n");
415 BUG();
416 }
417
418 xttc_setup_clocksource(clk, timer_baseaddr);
419 xttc_setup_clockevent(clk, timer_baseaddr + 4, irq);
420
421 pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
422}
423
Soren Brinkmannf184c5c2012-12-19 10:18:36 -0800424void __init xttcps_timer_init(void)
John Linnb85a3ef2011-06-20 11:47:27 -0600425{
Michal Simeke9329002013-03-20 10:15:28 +0100426 const char * const timer_list[] = {
427 "cdns,ttc",
428 NULL
429 };
430 struct device_node *timer;
John Linnb85a3ef2011-06-20 11:47:27 -0600431
Michal Simeke9329002013-03-20 10:15:28 +0100432 timer = of_find_compatible_node(NULL, NULL, timer_list[0]);
433 if (!timer) {
434 pr_err("ERROR: no compatible timer found\n");
435 BUG();
Josh Cartwright91dc9852012-10-31 13:56:14 -0600436 }
Michal Simeke9329002013-03-20 10:15:28 +0100437
438 xttcps_timer_init_of(timer);
John Linnb85a3ef2011-06-20 11:47:27 -0600439}