blob: 9be6018bd2b89cc8737e2a3c3e19a277c7eb67ed [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>
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>
Soren Brinkmann3d77b302013-07-08 09:51:38 -070024#include <linux/sched_clock.h>
John Linnb85a3ef2011-06-20 11:47:27 -060025
John Linnb85a3ef2011-06-20 11:47:27 -060026/*
Michal Simek4e2bec02014-09-29 01:50:05 +020027 * This driver configures the 2 16/32-bit count-up timers as follows:
Michal Simeke9329002013-03-20 10:15:28 +010028 *
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
41/*
John Linnb85a3ef2011-06-20 11:47:27 -060042 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
43 * and use same offsets for Timer 2
44 */
Michal Simek9e09dc52013-03-27 12:05:28 +010045#define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
46#define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
47#define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
48#define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
49#define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
50#define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
John Linnb85a3ef2011-06-20 11:47:27 -060051
Michal Simek9e09dc52013-03-27 12:05:28 +010052#define TTC_CNT_CNTRL_DISABLE_MASK 0x1
John Linnb85a3ef2011-06-20 11:47:27 -060053
Soren Brinkmann30e1e282013-05-13 10:46:38 -070054#define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
Soren Brinkmannb3e90722014-02-19 15:14:42 -080055#define TTC_CLK_CNTRL_PSV_MASK 0x1e
56#define TTC_CLK_CNTRL_PSV_SHIFT 1
Soren Brinkmann30e1e282013-05-13 10:46:38 -070057
Soren Brinkmann03377e52012-12-19 10:18:41 -080058/*
59 * Setup the timers to use pre-scaling, using a fixed value for now that will
Josh Cartwright91dc9852012-10-31 13:56:14 -060060 * work across most input frequency, but it may need to be more dynamic
61 */
62#define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
63#define PRESCALE 2048 /* The exponent must match this */
64#define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
65#define CLK_CNTRL_PRESCALE_EN 1
Michal Simeke9329002013-03-20 10:15:28 +010066#define CNT_CNTRL_RESET (1 << 4)
John Linnb85a3ef2011-06-20 11:47:27 -060067
Soren Brinkmannb3e90722014-02-19 15:14:42 -080068#define MAX_F_ERR 50
69
John Linnb85a3ef2011-06-20 11:47:27 -060070/**
Michal Simek9e09dc52013-03-27 12:05:28 +010071 * struct ttc_timer - This definition defines local timer structure
John Linnb85a3ef2011-06-20 11:47:27 -060072 *
73 * @base_addr: Base address of timer
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080074 * @freq: Timer input clock frequency
Michal Simeke9329002013-03-20 10:15:28 +010075 * @clk: Associated clock source
76 * @clk_rate_change_nb Notifier block for clock rate changes
77 */
Michal Simek9e09dc52013-03-27 12:05:28 +010078struct ttc_timer {
Michal Simeke9329002013-03-20 10:15:28 +010079 void __iomem *base_addr;
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080080 unsigned long freq;
Michal Simeke9329002013-03-20 10:15:28 +010081 struct clk *clk;
82 struct notifier_block clk_rate_change_nb;
John Linnb85a3ef2011-06-20 11:47:27 -060083};
84
Michal Simek9e09dc52013-03-27 12:05:28 +010085#define to_ttc_timer(x) \
86 container_of(x, struct ttc_timer, clk_rate_change_nb)
Michal Simeke9329002013-03-20 10:15:28 +010087
Michal Simek9e09dc52013-03-27 12:05:28 +010088struct ttc_timer_clocksource {
Soren Brinkmannb3e90722014-02-19 15:14:42 -080089 u32 scale_clk_ctrl_reg_old;
90 u32 scale_clk_ctrl_reg_new;
Michal Simek9e09dc52013-03-27 12:05:28 +010091 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060092 struct clocksource cs;
93};
94
Michal Simek9e09dc52013-03-27 12:05:28 +010095#define to_ttc_timer_clksrc(x) \
96 container_of(x, struct ttc_timer_clocksource, cs)
Josh Cartwright91dc9852012-10-31 13:56:14 -060097
Michal Simek9e09dc52013-03-27 12:05:28 +010098struct ttc_timer_clockevent {
99 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600100 struct clock_event_device ce;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600101};
102
Michal Simek9e09dc52013-03-27 12:05:28 +0100103#define to_ttc_timer_clkevent(x) \
104 container_of(x, struct ttc_timer_clockevent, ce)
John Linnb85a3ef2011-06-20 11:47:27 -0600105
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700106static void __iomem *ttc_sched_clock_val_reg;
107
John Linnb85a3ef2011-06-20 11:47:27 -0600108/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100109 * ttc_set_interval - Set the timer interval value
John Linnb85a3ef2011-06-20 11:47:27 -0600110 *
111 * @timer: Pointer to the timer instance
112 * @cycles: Timer interval ticks
113 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100114static void ttc_set_interval(struct ttc_timer *timer,
John Linnb85a3ef2011-06-20 11:47:27 -0600115 unsigned long cycles)
116{
117 u32 ctrl_reg;
118
119 /* Disable the counter, set the counter value and re-enable counter */
Michal Simek87ab4362014-04-11 15:39:29 +0200120 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
Michal Simek9e09dc52013-03-27 12:05:28 +0100121 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200122 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600123
Michal Simek87ab4362014-04-11 15:39:29 +0200124 writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600125
Soren Brinkmann03377e52012-12-19 10:18:41 -0800126 /*
127 * Reset the counter (0x10) so that it starts from 0, one-shot
128 * mode makes this needed for timing to be right.
129 */
Josh Cartwright91dc9852012-10-31 13:56:14 -0600130 ctrl_reg |= CNT_CNTRL_RESET;
Michal Simek9e09dc52013-03-27 12:05:28 +0100131 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200132 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600133}
134
135/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100136 * ttc_clock_event_interrupt - Clock event timer interrupt handler
John Linnb85a3ef2011-06-20 11:47:27 -0600137 *
138 * @irq: IRQ number of the Timer
Michal Simek9e09dc52013-03-27 12:05:28 +0100139 * @dev_id: void pointer to the ttc_timer instance
John Linnb85a3ef2011-06-20 11:47:27 -0600140 *
141 * returns: Always IRQ_HANDLED - success
142 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100143static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
John Linnb85a3ef2011-06-20 11:47:27 -0600144{
Michal Simek9e09dc52013-03-27 12:05:28 +0100145 struct ttc_timer_clockevent *ttce = dev_id;
146 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600147
148 /* Acknowledge the interrupt and call event handler */
Michal Simek87ab4362014-04-11 15:39:29 +0200149 readl_relaxed(timer->base_addr + TTC_ISR_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600150
Michal Simek9e09dc52013-03-27 12:05:28 +0100151 ttce->ce.event_handler(&ttce->ce);
John Linnb85a3ef2011-06-20 11:47:27 -0600152
153 return IRQ_HANDLED;
154}
155
John Linnb85a3ef2011-06-20 11:47:27 -0600156/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100157 * __ttc_clocksource_read - Reads the timer counter register
John Linnb85a3ef2011-06-20 11:47:27 -0600158 *
159 * returns: Current timer counter register value
160 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100161static cycle_t __ttc_clocksource_read(struct clocksource *cs)
John Linnb85a3ef2011-06-20 11:47:27 -0600162{
Michal Simek9e09dc52013-03-27 12:05:28 +0100163 struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600164
Michal Simek87ab4362014-04-11 15:39:29 +0200165 return (cycle_t)readl_relaxed(timer->base_addr +
Michal Simek9e09dc52013-03-27 12:05:28 +0100166 TTC_COUNT_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600167}
168
Stephen Boyddfded002013-11-20 00:47:32 +0100169static u64 notrace ttc_sched_clock_read(void)
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700170{
Michal Simek87ab4362014-04-11 15:39:29 +0200171 return readl_relaxed(ttc_sched_clock_val_reg);
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700172}
173
John Linnb85a3ef2011-06-20 11:47:27 -0600174/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100175 * ttc_set_next_event - Sets the time interval for next event
John Linnb85a3ef2011-06-20 11:47:27 -0600176 *
177 * @cycles: Timer interval ticks
178 * @evt: Address of clock event instance
179 *
180 * returns: Always 0 - success
181 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100182static int ttc_set_next_event(unsigned long cycles,
John Linnb85a3ef2011-06-20 11:47:27 -0600183 struct clock_event_device *evt)
184{
Michal Simek9e09dc52013-03-27 12:05:28 +0100185 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
186 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600187
Michal Simek9e09dc52013-03-27 12:05:28 +0100188 ttc_set_interval(timer, cycles);
John Linnb85a3ef2011-06-20 11:47:27 -0600189 return 0;
190}
191
192/**
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530193 * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer
John Linnb85a3ef2011-06-20 11:47:27 -0600194 *
John Linnb85a3ef2011-06-20 11:47:27 -0600195 * @evt: Address of clock event instance
196 **/
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530197static int ttc_shutdown(struct clock_event_device *evt)
John Linnb85a3ef2011-06-20 11:47:27 -0600198{
Michal Simek9e09dc52013-03-27 12:05:28 +0100199 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
200 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600201 u32 ctrl_reg;
202
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530203 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
204 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
205 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
206 return 0;
207}
208
209static int ttc_set_periodic(struct clock_event_device *evt)
210{
211 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
212 struct ttc_timer *timer = &ttce->ttc;
213
214 ttc_set_interval(timer,
215 DIV_ROUND_CLOSEST(ttce->ttc.freq, PRESCALE * HZ));
216 return 0;
217}
218
219static int ttc_resume(struct clock_event_device *evt)
220{
221 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
222 struct ttc_timer *timer = &ttce->ttc;
223 u32 ctrl_reg;
224
225 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
226 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
227 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
228 return 0;
John Linnb85a3ef2011-06-20 11:47:27 -0600229}
230
Michal Simek9e09dc52013-03-27 12:05:28 +0100231static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100232 unsigned long event, void *data)
233{
234 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100235 struct ttc_timer *ttc = to_ttc_timer(nb);
236 struct ttc_timer_clocksource *ttccs = container_of(ttc,
237 struct ttc_timer_clocksource, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100238
239 switch (event) {
Michal Simeke9329002013-03-20 10:15:28 +0100240 case PRE_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800241 {
242 u32 psv;
243 unsigned long factor, rate_low, rate_high;
244
245 if (ndata->new_rate > ndata->old_rate) {
246 factor = DIV_ROUND_CLOSEST(ndata->new_rate,
247 ndata->old_rate);
248 rate_low = ndata->old_rate;
249 rate_high = ndata->new_rate;
250 } else {
251 factor = DIV_ROUND_CLOSEST(ndata->old_rate,
252 ndata->new_rate);
253 rate_low = ndata->new_rate;
254 rate_high = ndata->old_rate;
255 }
256
257 if (!is_power_of_2(factor))
258 return NOTIFY_BAD;
259
260 if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
261 return NOTIFY_BAD;
262
263 factor = __ilog2_u32(factor);
264
265 /*
266 * store timer clock ctrl register so we can restore it in case
267 * of an abort.
268 */
269 ttccs->scale_clk_ctrl_reg_old =
Michal Simek87ab4362014-04-11 15:39:29 +0200270 readl_relaxed(ttccs->ttc.base_addr +
271 TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800272
273 psv = (ttccs->scale_clk_ctrl_reg_old &
274 TTC_CLK_CNTRL_PSV_MASK) >>
275 TTC_CLK_CNTRL_PSV_SHIFT;
276 if (ndata->new_rate < ndata->old_rate)
277 psv -= factor;
278 else
279 psv += factor;
280
281 /* prescaler within legal range? */
282 if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
283 return NOTIFY_BAD;
284
285 ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
286 ~TTC_CLK_CNTRL_PSV_MASK;
287 ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
288
289
290 /* scale down: adjust divider in post-change notification */
291 if (ndata->new_rate < ndata->old_rate)
292 return NOTIFY_DONE;
293
294 /* scale up: adjust divider now - before frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200295 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
296 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800297 break;
298 }
299 case POST_RATE_CHANGE:
300 /* scale up: pre-change notification did the adjustment */
301 if (ndata->new_rate > ndata->old_rate)
302 return NOTIFY_OK;
303
304 /* scale down: adjust divider now - after frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200305 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
306 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800307 break;
308
Michal Simeke9329002013-03-20 10:15:28 +0100309 case ABORT_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800310 /* we have to undo the adjustment in case we scale up */
311 if (ndata->new_rate < ndata->old_rate)
312 return NOTIFY_OK;
313
314 /* restore original register value */
Michal Simek87ab4362014-04-11 15:39:29 +0200315 writel_relaxed(ttccs->scale_clk_ctrl_reg_old,
316 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800317 /* fall through */
Michal Simeke9329002013-03-20 10:15:28 +0100318 default:
319 return NOTIFY_DONE;
320 }
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800321
322 return NOTIFY_DONE;
Michal Simeke9329002013-03-20 10:15:28 +0100323}
324
Michal Simek4e2bec02014-09-29 01:50:05 +0200325static void __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
326 u32 timer_width)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600327{
Michal Simek9e09dc52013-03-27 12:05:28 +0100328 struct ttc_timer_clocksource *ttccs;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600329 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600330
331 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
332 if (WARN_ON(!ttccs))
333 return;
334
Michal Simek9e09dc52013-03-27 12:05:28 +0100335 ttccs->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100336
Michal Simek9e09dc52013-03-27 12:05:28 +0100337 err = clk_prepare_enable(ttccs->ttc.clk);
Michal Simekc5263bb2013-03-20 10:24:59 +0100338 if (WARN_ON(err)) {
339 kfree(ttccs);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600340 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100341 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600342
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800343 ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
344
Michal Simek9e09dc52013-03-27 12:05:28 +0100345 ttccs->ttc.clk_rate_change_nb.notifier_call =
346 ttc_rate_change_clocksource_cb;
347 ttccs->ttc.clk_rate_change_nb.next = NULL;
348 if (clk_notifier_register(ttccs->ttc.clk,
349 &ttccs->ttc.clk_rate_change_nb))
Michal Simeke9329002013-03-20 10:15:28 +0100350 pr_warn("Unable to register clock notifier.\n");
Josh Cartwright91dc9852012-10-31 13:56:14 -0600351
Michal Simek9e09dc52013-03-27 12:05:28 +0100352 ttccs->ttc.base_addr = base;
353 ttccs->cs.name = "ttc_clocksource";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600354 ttccs->cs.rating = 200;
Michal Simek9e09dc52013-03-27 12:05:28 +0100355 ttccs->cs.read = __ttc_clocksource_read;
Michal Simek4e2bec02014-09-29 01:50:05 +0200356 ttccs->cs.mask = CLOCKSOURCE_MASK(timer_width);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600357 ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
358
Michal Simeke9329002013-03-20 10:15:28 +0100359 /*
360 * Setup the clock source counter to be an incrementing counter
361 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
362 * it by 32 also. Let it start running now.
363 */
Michal Simek87ab4362014-04-11 15:39:29 +0200364 writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
365 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100366 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200367 writel_relaxed(CNT_CNTRL_RESET,
Michal Simek9e09dc52013-03-27 12:05:28 +0100368 ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600369
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800370 err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
Michal Simekc5263bb2013-03-20 10:24:59 +0100371 if (WARN_ON(err)) {
372 kfree(ttccs);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600373 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100374 }
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700375
376 ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
Michal Simek4e2bec02014-09-29 01:50:05 +0200377 sched_clock_register(ttc_sched_clock_read, timer_width,
378 ttccs->ttc.freq / PRESCALE);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600379}
380
Michal Simek9e09dc52013-03-27 12:05:28 +0100381static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100382 unsigned long event, void *data)
383{
384 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100385 struct ttc_timer *ttc = to_ttc_timer(nb);
386 struct ttc_timer_clockevent *ttcce = container_of(ttc,
387 struct ttc_timer_clockevent, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100388
389 switch (event) {
390 case POST_RATE_CHANGE:
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800391 /* update cached frequency */
392 ttc->freq = ndata->new_rate;
393
Soren Brinkmann5f0ba3b2014-02-19 15:14:41 -0800394 clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE);
395
Michal Simeke9329002013-03-20 10:15:28 +0100396 /* fall through */
Michal Simeke9329002013-03-20 10:15:28 +0100397 case PRE_RATE_CHANGE:
398 case ABORT_RATE_CHANGE:
399 default:
400 return NOTIFY_DONE;
401 }
402}
403
Michal Simek9e09dc52013-03-27 12:05:28 +0100404static void __init ttc_setup_clockevent(struct clk *clk,
Michal Simeke9329002013-03-20 10:15:28 +0100405 void __iomem *base, u32 irq)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600406{
Michal Simek9e09dc52013-03-27 12:05:28 +0100407 struct ttc_timer_clockevent *ttcce;
Michal Simeke9329002013-03-20 10:15:28 +0100408 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600409
410 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
411 if (WARN_ON(!ttcce))
412 return;
413
Michal Simek9e09dc52013-03-27 12:05:28 +0100414 ttcce->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100415
Michal Simek9e09dc52013-03-27 12:05:28 +0100416 err = clk_prepare_enable(ttcce->ttc.clk);
Michal Simekc5263bb2013-03-20 10:24:59 +0100417 if (WARN_ON(err)) {
418 kfree(ttcce);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600419 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100420 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600421
Michal Simek9e09dc52013-03-27 12:05:28 +0100422 ttcce->ttc.clk_rate_change_nb.notifier_call =
423 ttc_rate_change_clockevent_cb;
424 ttcce->ttc.clk_rate_change_nb.next = NULL;
425 if (clk_notifier_register(ttcce->ttc.clk,
426 &ttcce->ttc.clk_rate_change_nb))
Michal Simeke9329002013-03-20 10:15:28 +0100427 pr_warn("Unable to register clock notifier.\n");
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800428 ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600429
Michal Simek9e09dc52013-03-27 12:05:28 +0100430 ttcce->ttc.base_addr = base;
431 ttcce->ce.name = "ttc_clockevent";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600432 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Michal Simek9e09dc52013-03-27 12:05:28 +0100433 ttcce->ce.set_next_event = ttc_set_next_event;
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530434 ttcce->ce.set_state_shutdown = ttc_shutdown;
435 ttcce->ce.set_state_periodic = ttc_set_periodic;
436 ttcce->ce.set_state_oneshot = ttc_shutdown;
437 ttcce->ce.tick_resume = ttc_resume;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600438 ttcce->ce.rating = 200;
439 ttcce->ce.irq = irq;
Soren Brinkmann87e4ee72012-12-19 10:18:42 -0800440 ttcce->ce.cpumask = cpu_possible_mask;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600441
Michal Simeke9329002013-03-20 10:15:28 +0100442 /*
443 * Setup the clock event timer to be an interval timer which
444 * is prescaled by 32 using the interval interrupt. Leave it
445 * disabled for now.
446 */
Michal Simek87ab4362014-04-11 15:39:29 +0200447 writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
448 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100449 ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200450 writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600451
Michal Simek9e09dc52013-03-27 12:05:28 +0100452 err = request_irq(irq, ttc_clock_event_interrupt,
Michael Opdenacker38c30a82013-12-09 10:12:10 +0100453 IRQF_TIMER, ttcce->ce.name, ttcce);
Michal Simekc5263bb2013-03-20 10:24:59 +0100454 if (WARN_ON(err)) {
455 kfree(ttcce);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600456 return;
Michal Simekc5263bb2013-03-20 10:24:59 +0100457 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600458
459 clockevents_config_and_register(&ttcce->ce,
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800460 ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600461}
462
John Linnb85a3ef2011-06-20 11:47:27 -0600463/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100464 * ttc_timer_init - Initialize the timer
John Linnb85a3ef2011-06-20 11:47:27 -0600465 *
466 * Initializes the timer hardware and register the clock source and clock event
467 * timers with Linux kernal timer framework
Michal Simeke9329002013-03-20 10:15:28 +0100468 */
Michal Simek9e09dc52013-03-27 12:05:28 +0100469static void __init ttc_timer_init(struct device_node *timer)
Michal Simeke9329002013-03-20 10:15:28 +0100470{
471 unsigned int irq;
472 void __iomem *timer_baseaddr;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700473 struct clk *clk_cs, *clk_ce;
Michal Simekc5263bb2013-03-20 10:24:59 +0100474 static int initialized;
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700475 int clksel;
Michal Simek4e2bec02014-09-29 01:50:05 +0200476 u32 timer_width = 16;
Michal Simekc5263bb2013-03-20 10:24:59 +0100477
478 if (initialized)
479 return;
480
481 initialized = 1;
Michal Simeke9329002013-03-20 10:15:28 +0100482
483 /*
484 * Get the 1st Triple Timer Counter (TTC) block from the device tree
485 * and use it. Note that the event timer uses the interrupt and it's the
486 * 2nd TTC hence the irq_of_parse_and_map(,1)
487 */
488 timer_baseaddr = of_iomap(timer, 0);
489 if (!timer_baseaddr) {
490 pr_err("ERROR: invalid timer base address\n");
491 BUG();
492 }
493
494 irq = irq_of_parse_and_map(timer, 1);
495 if (irq <= 0) {
496 pr_err("ERROR: invalid interrupt number\n");
497 BUG();
498 }
499
Michal Simek4e2bec02014-09-29 01:50:05 +0200500 of_property_read_u32(timer, "timer-width", &timer_width);
501
Michal Simek87ab4362014-04-11 15:39:29 +0200502 clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700503 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
504 clk_cs = of_clk_get(timer, clksel);
505 if (IS_ERR(clk_cs)) {
Michal Simeke9329002013-03-20 10:15:28 +0100506 pr_err("ERROR: timer input clock not found\n");
507 BUG();
508 }
509
Michal Simek87ab4362014-04-11 15:39:29 +0200510 clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700511 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
512 clk_ce = of_clk_get(timer, clksel);
513 if (IS_ERR(clk_ce)) {
514 pr_err("ERROR: timer input clock not found\n");
515 BUG();
516 }
517
Michal Simek4e2bec02014-09-29 01:50:05 +0200518 ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700519 ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
Michal Simeke9329002013-03-20 10:15:28 +0100520
521 pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
522}
523
Michal Simek9e09dc52013-03-27 12:05:28 +0100524CLOCKSOURCE_OF_DECLARE(ttc, "cdns,ttc", ttc_timer_init);