blob: 7a08811df9aa61505a763a3be3a5968339453120 [file] [log] [blame]
John Linnb85a3ef2011-06-20 11:47:27 -06001/*
Michal Simek9e09dc52013-03-27 12:05:28 +01002 * This file contains driver for the Cadence Triple Timer Counter Rev 06
John Linnb85a3ef2011-06-20 11:47:27 -06003 *
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>
Soren Brinkmannb3e90722014-02-19 15:14:42 -080019#include <linux/clk-provider.h>
John Linnb85a3ef2011-06-20 11:47:27 -060020#include <linux/interrupt.h>
John Linnb85a3ef2011-06-20 11:47:27 -060021#include <linux/clockchips.h>
Josh Cartwright91dc9852012-10-31 13:56:14 -060022#include <linux/of_address.h>
23#include <linux/of_irq.h>
24#include <linux/slab.h>
Soren Brinkmann3d77b302013-07-08 09:51:38 -070025#include <linux/sched_clock.h>
John Linnb85a3ef2011-06-20 11:47:27 -060026
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 */
Michal Simek9e09dc52013-03-27 12:05:28 +010046#define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
47#define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
48#define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
49#define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
50#define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
51#define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
John Linnb85a3ef2011-06-20 11:47:27 -060052
Michal Simek9e09dc52013-03-27 12:05:28 +010053#define TTC_CNT_CNTRL_DISABLE_MASK 0x1
John Linnb85a3ef2011-06-20 11:47:27 -060054
Soren Brinkmann30e1e282013-05-13 10:46:38 -070055#define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
Soren Brinkmannb3e90722014-02-19 15:14:42 -080056#define TTC_CLK_CNTRL_PSV_MASK 0x1e
57#define TTC_CLK_CNTRL_PSV_SHIFT 1
Soren Brinkmann30e1e282013-05-13 10:46:38 -070058
Soren Brinkmann03377e52012-12-19 10:18:41 -080059/*
60 * Setup the timers to use pre-scaling, using a fixed value for now that will
Josh Cartwright91dc9852012-10-31 13:56:14 -060061 * work across most input frequency, but it may need to be more dynamic
62 */
63#define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
64#define PRESCALE 2048 /* The exponent must match this */
65#define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
66#define CLK_CNTRL_PRESCALE_EN 1
Michal Simeke9329002013-03-20 10:15:28 +010067#define CNT_CNTRL_RESET (1 << 4)
John Linnb85a3ef2011-06-20 11:47:27 -060068
Soren Brinkmannb3e90722014-02-19 15:14:42 -080069#define MAX_F_ERR 50
70
John Linnb85a3ef2011-06-20 11:47:27 -060071/**
Michal Simek9e09dc52013-03-27 12:05:28 +010072 * struct ttc_timer - This definition defines local timer structure
John Linnb85a3ef2011-06-20 11:47:27 -060073 *
74 * @base_addr: Base address of timer
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080075 * @freq: Timer input clock frequency
Michal Simeke9329002013-03-20 10:15:28 +010076 * @clk: Associated clock source
77 * @clk_rate_change_nb Notifier block for clock rate changes
78 */
Michal Simek9e09dc52013-03-27 12:05:28 +010079struct ttc_timer {
Michal Simeke9329002013-03-20 10:15:28 +010080 void __iomem *base_addr;
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080081 unsigned long freq;
Michal Simeke9329002013-03-20 10:15:28 +010082 struct clk *clk;
83 struct notifier_block clk_rate_change_nb;
John Linnb85a3ef2011-06-20 11:47:27 -060084};
85
Michal Simek9e09dc52013-03-27 12:05:28 +010086#define to_ttc_timer(x) \
87 container_of(x, struct ttc_timer, clk_rate_change_nb)
Michal Simeke9329002013-03-20 10:15:28 +010088
Michal Simek9e09dc52013-03-27 12:05:28 +010089struct ttc_timer_clocksource {
Soren Brinkmannb3e90722014-02-19 15:14:42 -080090 u32 scale_clk_ctrl_reg_old;
91 u32 scale_clk_ctrl_reg_new;
Michal Simek9e09dc52013-03-27 12:05:28 +010092 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060093 struct clocksource cs;
94};
95
Michal Simek9e09dc52013-03-27 12:05:28 +010096#define to_ttc_timer_clksrc(x) \
97 container_of(x, struct ttc_timer_clocksource, cs)
Josh Cartwright91dc9852012-10-31 13:56:14 -060098
Michal Simek9e09dc52013-03-27 12:05:28 +010099struct ttc_timer_clockevent {
100 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600101 struct clock_event_device ce;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600102};
103
Michal Simek9e09dc52013-03-27 12:05:28 +0100104#define to_ttc_timer_clkevent(x) \
105 container_of(x, struct ttc_timer_clockevent, ce)
John Linnb85a3ef2011-06-20 11:47:27 -0600106
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700107static void __iomem *ttc_sched_clock_val_reg;
108
John Linnb85a3ef2011-06-20 11:47:27 -0600109/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100110 * ttc_set_interval - Set the timer interval value
John Linnb85a3ef2011-06-20 11:47:27 -0600111 *
112 * @timer: Pointer to the timer instance
113 * @cycles: Timer interval ticks
114 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100115static void ttc_set_interval(struct ttc_timer *timer,
John Linnb85a3ef2011-06-20 11:47:27 -0600116 unsigned long cycles)
117{
118 u32 ctrl_reg;
119
120 /* Disable the counter, set the counter value and re-enable counter */
Michal Simek87ab4362014-04-11 15:39:29 +0200121 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
Michal Simek9e09dc52013-03-27 12:05:28 +0100122 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200123 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600124
Michal Simek87ab4362014-04-11 15:39:29 +0200125 writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600126
Soren Brinkmann03377e52012-12-19 10:18:41 -0800127 /*
128 * Reset the counter (0x10) so that it starts from 0, one-shot
129 * mode makes this needed for timing to be right.
130 */
Josh Cartwright91dc9852012-10-31 13:56:14 -0600131 ctrl_reg |= CNT_CNTRL_RESET;
Michal Simek9e09dc52013-03-27 12:05:28 +0100132 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200133 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600134}
135
136/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100137 * ttc_clock_event_interrupt - Clock event timer interrupt handler
John Linnb85a3ef2011-06-20 11:47:27 -0600138 *
139 * @irq: IRQ number of the Timer
Michal Simek9e09dc52013-03-27 12:05:28 +0100140 * @dev_id: void pointer to the ttc_timer instance
John Linnb85a3ef2011-06-20 11:47:27 -0600141 *
142 * returns: Always IRQ_HANDLED - success
143 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100144static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
John Linnb85a3ef2011-06-20 11:47:27 -0600145{
Michal Simek9e09dc52013-03-27 12:05:28 +0100146 struct ttc_timer_clockevent *ttce = dev_id;
147 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600148
149 /* Acknowledge the interrupt and call event handler */
Michal Simek87ab4362014-04-11 15:39:29 +0200150 readl_relaxed(timer->base_addr + TTC_ISR_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600151
Michal Simek9e09dc52013-03-27 12:05:28 +0100152 ttce->ce.event_handler(&ttce->ce);
John Linnb85a3ef2011-06-20 11:47:27 -0600153
154 return IRQ_HANDLED;
155}
156
John Linnb85a3ef2011-06-20 11:47:27 -0600157/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100158 * __ttc_clocksource_read - Reads the timer counter register
John Linnb85a3ef2011-06-20 11:47:27 -0600159 *
160 * returns: Current timer counter register value
161 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100162static cycle_t __ttc_clocksource_read(struct clocksource *cs)
John Linnb85a3ef2011-06-20 11:47:27 -0600163{
Michal Simek9e09dc52013-03-27 12:05:28 +0100164 struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600165
Michal Simek87ab4362014-04-11 15:39:29 +0200166 return (cycle_t)readl_relaxed(timer->base_addr +
Michal Simek9e09dc52013-03-27 12:05:28 +0100167 TTC_COUNT_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600168}
169
Stephen Boyddfded002013-11-20 00:47:32 +0100170static u64 notrace ttc_sched_clock_read(void)
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700171{
Michal Simek87ab4362014-04-11 15:39:29 +0200172 return readl_relaxed(ttc_sched_clock_val_reg);
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700173}
174
John Linnb85a3ef2011-06-20 11:47:27 -0600175/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100176 * ttc_set_next_event - Sets the time interval for next event
John Linnb85a3ef2011-06-20 11:47:27 -0600177 *
178 * @cycles: Timer interval ticks
179 * @evt: Address of clock event instance
180 *
181 * returns: Always 0 - success
182 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100183static int ttc_set_next_event(unsigned long cycles,
John Linnb85a3ef2011-06-20 11:47:27 -0600184 struct clock_event_device *evt)
185{
Michal Simek9e09dc52013-03-27 12:05:28 +0100186 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
187 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600188
Michal Simek9e09dc52013-03-27 12:05:28 +0100189 ttc_set_interval(timer, cycles);
John Linnb85a3ef2011-06-20 11:47:27 -0600190 return 0;
191}
192
193/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100194 * ttc_set_mode - Sets the mode of timer
John Linnb85a3ef2011-06-20 11:47:27 -0600195 *
196 * @mode: Mode to be set
197 * @evt: Address of clock event instance
198 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100199static void ttc_set_mode(enum clock_event_mode mode,
John Linnb85a3ef2011-06-20 11:47:27 -0600200 struct clock_event_device *evt)
201{
Michal Simek9e09dc52013-03-27 12:05:28 +0100202 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
203 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600204 u32 ctrl_reg;
205
206 switch (mode) {
207 case CLOCK_EVT_MODE_PERIODIC:
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800208 ttc_set_interval(timer, DIV_ROUND_CLOSEST(ttce->ttc.freq,
209 PRESCALE * HZ));
John Linnb85a3ef2011-06-20 11:47:27 -0600210 break;
211 case CLOCK_EVT_MODE_ONESHOT:
212 case CLOCK_EVT_MODE_UNUSED:
213 case CLOCK_EVT_MODE_SHUTDOWN:
Michal Simek87ab4362014-04-11 15:39:29 +0200214 ctrl_reg = readl_relaxed(timer->base_addr +
Michal Simek9e09dc52013-03-27 12:05:28 +0100215 TTC_CNT_CNTRL_OFFSET);
216 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200217 writel_relaxed(ctrl_reg,
Michal Simek9e09dc52013-03-27 12:05:28 +0100218 timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600219 break;
220 case CLOCK_EVT_MODE_RESUME:
Michal Simek87ab4362014-04-11 15:39:29 +0200221 ctrl_reg = readl_relaxed(timer->base_addr +
Michal Simek9e09dc52013-03-27 12:05:28 +0100222 TTC_CNT_CNTRL_OFFSET);
223 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200224 writel_relaxed(ctrl_reg,
Michal Simek9e09dc52013-03-27 12:05:28 +0100225 timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600226 break;
227 }
228}
229
Michal Simek9e09dc52013-03-27 12:05:28 +0100230static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100231 unsigned long event, void *data)
232{
233 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100234 struct ttc_timer *ttc = to_ttc_timer(nb);
235 struct ttc_timer_clocksource *ttccs = container_of(ttc,
236 struct ttc_timer_clocksource, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100237
238 switch (event) {
Michal Simeke9329002013-03-20 10:15:28 +0100239 case PRE_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800240 {
241 u32 psv;
242 unsigned long factor, rate_low, rate_high;
243
244 if (ndata->new_rate > ndata->old_rate) {
245 factor = DIV_ROUND_CLOSEST(ndata->new_rate,
246 ndata->old_rate);
247 rate_low = ndata->old_rate;
248 rate_high = ndata->new_rate;
249 } else {
250 factor = DIV_ROUND_CLOSEST(ndata->old_rate,
251 ndata->new_rate);
252 rate_low = ndata->new_rate;
253 rate_high = ndata->old_rate;
254 }
255
256 if (!is_power_of_2(factor))
257 return NOTIFY_BAD;
258
259 if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
260 return NOTIFY_BAD;
261
262 factor = __ilog2_u32(factor);
263
264 /*
265 * store timer clock ctrl register so we can restore it in case
266 * of an abort.
267 */
268 ttccs->scale_clk_ctrl_reg_old =
Michal Simek87ab4362014-04-11 15:39:29 +0200269 readl_relaxed(ttccs->ttc.base_addr +
270 TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800271
272 psv = (ttccs->scale_clk_ctrl_reg_old &
273 TTC_CLK_CNTRL_PSV_MASK) >>
274 TTC_CLK_CNTRL_PSV_SHIFT;
275 if (ndata->new_rate < ndata->old_rate)
276 psv -= factor;
277 else
278 psv += factor;
279
280 /* prescaler within legal range? */
281 if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
282 return NOTIFY_BAD;
283
284 ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
285 ~TTC_CLK_CNTRL_PSV_MASK;
286 ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
287
288
289 /* scale down: adjust divider in post-change notification */
290 if (ndata->new_rate < ndata->old_rate)
291 return NOTIFY_DONE;
292
293 /* scale up: adjust divider now - before frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200294 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
295 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800296 break;
297 }
298 case POST_RATE_CHANGE:
299 /* scale up: pre-change notification did the adjustment */
300 if (ndata->new_rate > ndata->old_rate)
301 return NOTIFY_OK;
302
303 /* scale down: adjust divider now - after frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200304 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
305 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800306 break;
307
Michal Simeke9329002013-03-20 10:15:28 +0100308 case ABORT_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800309 /* we have to undo the adjustment in case we scale up */
310 if (ndata->new_rate < ndata->old_rate)
311 return NOTIFY_OK;
312
313 /* restore original register value */
Michal Simek87ab4362014-04-11 15:39:29 +0200314 writel_relaxed(ttccs->scale_clk_ctrl_reg_old,
315 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800316 /* fall through */
Michal Simeke9329002013-03-20 10:15:28 +0100317 default:
318 return NOTIFY_DONE;
319 }
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800320
321 return NOTIFY_DONE;
Michal Simeke9329002013-03-20 10:15:28 +0100322}
323
Michal Simek9e09dc52013-03-27 12:05:28 +0100324static void __init ttc_setup_clocksource(struct clk *clk, void __iomem *base)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600325{
Michal Simek9e09dc52013-03-27 12:05:28 +0100326 struct ttc_timer_clocksource *ttccs;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600327 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600328
329 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
330 if (WARN_ON(!ttccs))
331 return;
332
Michal Simek9e09dc52013-03-27 12:05:28 +0100333 ttccs->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100334
Michal Simek9e09dc52013-03-27 12:05:28 +0100335 err = clk_prepare_enable(ttccs->ttc.clk);
Michal Simekc5263bb2013-03-20 10:24:59 +0100336 if (WARN_ON(err)) {
337 kfree(ttccs);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600338 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100339 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600340
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800341 ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
342
Michal Simek9e09dc52013-03-27 12:05:28 +0100343 ttccs->ttc.clk_rate_change_nb.notifier_call =
344 ttc_rate_change_clocksource_cb;
345 ttccs->ttc.clk_rate_change_nb.next = NULL;
346 if (clk_notifier_register(ttccs->ttc.clk,
347 &ttccs->ttc.clk_rate_change_nb))
Michal Simeke9329002013-03-20 10:15:28 +0100348 pr_warn("Unable to register clock notifier.\n");
Josh Cartwright91dc9852012-10-31 13:56:14 -0600349
Michal Simek9e09dc52013-03-27 12:05:28 +0100350 ttccs->ttc.base_addr = base;
351 ttccs->cs.name = "ttc_clocksource";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600352 ttccs->cs.rating = 200;
Michal Simek9e09dc52013-03-27 12:05:28 +0100353 ttccs->cs.read = __ttc_clocksource_read;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600354 ttccs->cs.mask = CLOCKSOURCE_MASK(16);
355 ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
356
Michal Simeke9329002013-03-20 10:15:28 +0100357 /*
358 * Setup the clock source counter to be an incrementing counter
359 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
360 * it by 32 also. Let it start running now.
361 */
Michal Simek87ab4362014-04-11 15:39:29 +0200362 writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
363 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100364 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200365 writel_relaxed(CNT_CNTRL_RESET,
Michal Simek9e09dc52013-03-27 12:05:28 +0100366 ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600367
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800368 err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
Michal Simekc5263bb2013-03-20 10:24:59 +0100369 if (WARN_ON(err)) {
370 kfree(ttccs);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600371 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100372 }
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700373
374 ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
Linus Torvalds6c646142014-01-20 11:34:26 -0800375 sched_clock_register(ttc_sched_clock_read, 16, ttccs->ttc.freq / PRESCALE);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600376}
377
Michal Simek9e09dc52013-03-27 12:05:28 +0100378static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100379 unsigned long event, void *data)
380{
381 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100382 struct ttc_timer *ttc = to_ttc_timer(nb);
383 struct ttc_timer_clockevent *ttcce = container_of(ttc,
384 struct ttc_timer_clockevent, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100385
386 switch (event) {
387 case POST_RATE_CHANGE:
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800388 /* update cached frequency */
389 ttc->freq = ndata->new_rate;
390
Soren Brinkmann5f0ba3b2014-02-19 15:14:41 -0800391 clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE);
392
Michal Simeke9329002013-03-20 10:15:28 +0100393 /* fall through */
Michal Simeke9329002013-03-20 10:15:28 +0100394 case PRE_RATE_CHANGE:
395 case ABORT_RATE_CHANGE:
396 default:
397 return NOTIFY_DONE;
398 }
399}
400
Michal Simek9e09dc52013-03-27 12:05:28 +0100401static void __init ttc_setup_clockevent(struct clk *clk,
Michal Simeke9329002013-03-20 10:15:28 +0100402 void __iomem *base, u32 irq)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600403{
Michal Simek9e09dc52013-03-27 12:05:28 +0100404 struct ttc_timer_clockevent *ttcce;
Michal Simeke9329002013-03-20 10:15:28 +0100405 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600406
407 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
408 if (WARN_ON(!ttcce))
409 return;
410
Michal Simek9e09dc52013-03-27 12:05:28 +0100411 ttcce->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100412
Michal Simek9e09dc52013-03-27 12:05:28 +0100413 err = clk_prepare_enable(ttcce->ttc.clk);
Michal Simekc5263bb2013-03-20 10:24:59 +0100414 if (WARN_ON(err)) {
415 kfree(ttcce);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600416 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100417 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600418
Michal Simek9e09dc52013-03-27 12:05:28 +0100419 ttcce->ttc.clk_rate_change_nb.notifier_call =
420 ttc_rate_change_clockevent_cb;
421 ttcce->ttc.clk_rate_change_nb.next = NULL;
422 if (clk_notifier_register(ttcce->ttc.clk,
423 &ttcce->ttc.clk_rate_change_nb))
Michal Simeke9329002013-03-20 10:15:28 +0100424 pr_warn("Unable to register clock notifier.\n");
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800425 ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600426
Michal Simek9e09dc52013-03-27 12:05:28 +0100427 ttcce->ttc.base_addr = base;
428 ttcce->ce.name = "ttc_clockevent";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600429 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Michal Simek9e09dc52013-03-27 12:05:28 +0100430 ttcce->ce.set_next_event = ttc_set_next_event;
431 ttcce->ce.set_mode = ttc_set_mode;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600432 ttcce->ce.rating = 200;
433 ttcce->ce.irq = irq;
Soren Brinkmann87e4ee72012-12-19 10:18:42 -0800434 ttcce->ce.cpumask = cpu_possible_mask;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600435
Michal Simeke9329002013-03-20 10:15:28 +0100436 /*
437 * Setup the clock event timer to be an interval timer which
438 * is prescaled by 32 using the interval interrupt. Leave it
439 * disabled for now.
440 */
Michal Simek87ab4362014-04-11 15:39:29 +0200441 writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
442 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100443 ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200444 writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600445
Michal Simek9e09dc52013-03-27 12:05:28 +0100446 err = request_irq(irq, ttc_clock_event_interrupt,
Michael Opdenacker38c30a82013-12-09 10:12:10 +0100447 IRQF_TIMER, ttcce->ce.name, ttcce);
Michal Simekc5263bb2013-03-20 10:24:59 +0100448 if (WARN_ON(err)) {
449 kfree(ttcce);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600450 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100451 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600452
453 clockevents_config_and_register(&ttcce->ce,
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800454 ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600455}
456
John Linnb85a3ef2011-06-20 11:47:27 -0600457/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100458 * ttc_timer_init - Initialize the timer
John Linnb85a3ef2011-06-20 11:47:27 -0600459 *
460 * Initializes the timer hardware and register the clock source and clock event
461 * timers with Linux kernal timer framework
Michal Simeke9329002013-03-20 10:15:28 +0100462 */
Michal Simek9e09dc52013-03-27 12:05:28 +0100463static void __init ttc_timer_init(struct device_node *timer)
Michal Simeke9329002013-03-20 10:15:28 +0100464{
465 unsigned int irq;
466 void __iomem *timer_baseaddr;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700467 struct clk *clk_cs, *clk_ce;
Michal Simekc5263bb2013-03-20 10:24:59 +0100468 static int initialized;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700469 int clksel;
Michal Simekc5263bb2013-03-20 10:24:59 +0100470
471 if (initialized)
472 return;
473
474 initialized = 1;
Michal Simeke9329002013-03-20 10:15:28 +0100475
476 /*
477 * Get the 1st Triple Timer Counter (TTC) block from the device tree
478 * and use it. Note that the event timer uses the interrupt and it's the
479 * 2nd TTC hence the irq_of_parse_and_map(,1)
480 */
481 timer_baseaddr = of_iomap(timer, 0);
482 if (!timer_baseaddr) {
483 pr_err("ERROR: invalid timer base address\n");
484 BUG();
485 }
486
487 irq = irq_of_parse_and_map(timer, 1);
488 if (irq <= 0) {
489 pr_err("ERROR: invalid interrupt number\n");
490 BUG();
491 }
492
Michal Simek87ab4362014-04-11 15:39:29 +0200493 clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700494 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
495 clk_cs = of_clk_get(timer, clksel);
496 if (IS_ERR(clk_cs)) {
Michal Simeke9329002013-03-20 10:15:28 +0100497 pr_err("ERROR: timer input clock not found\n");
498 BUG();
499 }
500
Michal Simek87ab4362014-04-11 15:39:29 +0200501 clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700502 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
503 clk_ce = of_clk_get(timer, clksel);
504 if (IS_ERR(clk_ce)) {
505 pr_err("ERROR: timer input clock not found\n");
506 BUG();
507 }
508
509 ttc_setup_clocksource(clk_cs, timer_baseaddr);
510 ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
Michal Simeke9329002013-03-20 10:15:28 +0100511
512 pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
513}
514
Michal Simek9e09dc52013-03-27 12:05:28 +0100515CLOCKSOURCE_OF_DECLARE(ttc, "cdns,ttc", ttc_timer_init);