blob: 4efd0cf3b602d60a304eb0e823b929935efbda47 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
John Linnb85a3ef2011-06-20 11:47:27 -06002/*
Michal Simek9e09dc52013-03-27 12:05:28 +01003 * This file contains driver for the Cadence Triple Timer Counter Rev 06
John Linnb85a3ef2011-06-20 11:47:27 -06004 *
Michal Simeke9329002013-03-20 10:15:28 +01005 * Copyright (C) 2011-2013 Xilinx
John Linnb85a3ef2011-06-20 11:47:27 -06006 *
7 * based on arch/mips/kernel/time.c timer driver
John Linnb85a3ef2011-06-20 11:47:27 -06008 */
9
Michal Simeke9329002013-03-20 10:15:28 +010010#include <linux/clk.h>
John Linnb85a3ef2011-06-20 11:47:27 -060011#include <linux/interrupt.h>
John Linnb85a3ef2011-06-20 11:47:27 -060012#include <linux/clockchips.h>
Stephen Rothwell459fa242017-06-11 15:22:10 +100013#include <linux/clocksource.h>
Josh Cartwright91dc9852012-10-31 13:56:14 -060014#include <linux/of_address.h>
15#include <linux/of_irq.h>
16#include <linux/slab.h>
Soren Brinkmann3d77b302013-07-08 09:51:38 -070017#include <linux/sched_clock.h>
Rajan Vajaf5ac8962019-11-07 02:36:28 -080018#include <linux/module.h>
19#include <linux/of_platform.h>
John Linnb85a3ef2011-06-20 11:47:27 -060020
John Linnb85a3ef2011-06-20 11:47:27 -060021/*
Michal Simek4e2bec02014-09-29 01:50:05 +020022 * This driver configures the 2 16/32-bit count-up timers as follows:
Michal Simeke9329002013-03-20 10:15:28 +010023 *
24 * T1: Timer 1, clocksource for generic timekeeping
25 * T2: Timer 2, clockevent source for hrtimers
26 * T3: Timer 3, <unused>
27 *
28 * The input frequency to the timer module for emulation is 2.5MHz which is
29 * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
30 * the timers are clocked at 78.125KHz (12.8 us resolution).
31
32 * The input frequency to the timer module in silicon is configurable and
33 * obtained from device tree. The pre-scaler of 32 is used.
34 */
35
36/*
John Linnb85a3ef2011-06-20 11:47:27 -060037 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
38 * and use same offsets for Timer 2
39 */
Michal Simek9e09dc52013-03-27 12:05:28 +010040#define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
41#define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
42#define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
43#define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
44#define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
45#define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
John Linnb85a3ef2011-06-20 11:47:27 -060046
Michal Simek9e09dc52013-03-27 12:05:28 +010047#define TTC_CNT_CNTRL_DISABLE_MASK 0x1
John Linnb85a3ef2011-06-20 11:47:27 -060048
Soren Brinkmann30e1e282013-05-13 10:46:38 -070049#define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
Soren Brinkmannb3e90722014-02-19 15:14:42 -080050#define TTC_CLK_CNTRL_PSV_MASK 0x1e
51#define TTC_CLK_CNTRL_PSV_SHIFT 1
Soren Brinkmann30e1e282013-05-13 10:46:38 -070052
Soren Brinkmann03377e52012-12-19 10:18:41 -080053/*
54 * Setup the timers to use pre-scaling, using a fixed value for now that will
Josh Cartwright91dc9852012-10-31 13:56:14 -060055 * work across most input frequency, but it may need to be more dynamic
56 */
57#define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
58#define PRESCALE 2048 /* The exponent must match this */
59#define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
60#define CLK_CNTRL_PRESCALE_EN 1
Michal Simeke9329002013-03-20 10:15:28 +010061#define CNT_CNTRL_RESET (1 << 4)
John Linnb85a3ef2011-06-20 11:47:27 -060062
Soren Brinkmannb3e90722014-02-19 15:14:42 -080063#define MAX_F_ERR 50
64
John Linnb85a3ef2011-06-20 11:47:27 -060065/**
Michal Simek9e09dc52013-03-27 12:05:28 +010066 * struct ttc_timer - This definition defines local timer structure
John Linnb85a3ef2011-06-20 11:47:27 -060067 *
68 * @base_addr: Base address of timer
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080069 * @freq: Timer input clock frequency
Michal Simeke9329002013-03-20 10:15:28 +010070 * @clk: Associated clock source
71 * @clk_rate_change_nb Notifier block for clock rate changes
72 */
Michal Simek9e09dc52013-03-27 12:05:28 +010073struct ttc_timer {
Michal Simeke9329002013-03-20 10:15:28 +010074 void __iomem *base_addr;
Soren Brinkmannc1dcc922013-11-26 17:04:50 -080075 unsigned long freq;
Michal Simeke9329002013-03-20 10:15:28 +010076 struct clk *clk;
77 struct notifier_block clk_rate_change_nb;
John Linnb85a3ef2011-06-20 11:47:27 -060078};
79
Michal Simek9e09dc52013-03-27 12:05:28 +010080#define to_ttc_timer(x) \
81 container_of(x, struct ttc_timer, clk_rate_change_nb)
Michal Simeke9329002013-03-20 10:15:28 +010082
Michal Simek9e09dc52013-03-27 12:05:28 +010083struct ttc_timer_clocksource {
Soren Brinkmannb3e90722014-02-19 15:14:42 -080084 u32 scale_clk_ctrl_reg_old;
85 u32 scale_clk_ctrl_reg_new;
Michal Simek9e09dc52013-03-27 12:05:28 +010086 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060087 struct clocksource cs;
88};
89
Michal Simek9e09dc52013-03-27 12:05:28 +010090#define to_ttc_timer_clksrc(x) \
91 container_of(x, struct ttc_timer_clocksource, cs)
Josh Cartwright91dc9852012-10-31 13:56:14 -060092
Michal Simek9e09dc52013-03-27 12:05:28 +010093struct ttc_timer_clockevent {
94 struct ttc_timer ttc;
Josh Cartwright91dc9852012-10-31 13:56:14 -060095 struct clock_event_device ce;
Josh Cartwright91dc9852012-10-31 13:56:14 -060096};
97
Michal Simek9e09dc52013-03-27 12:05:28 +010098#define to_ttc_timer_clkevent(x) \
99 container_of(x, struct ttc_timer_clockevent, ce)
John Linnb85a3ef2011-06-20 11:47:27 -0600100
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700101static void __iomem *ttc_sched_clock_val_reg;
102
John Linnb85a3ef2011-06-20 11:47:27 -0600103/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100104 * ttc_set_interval - Set the timer interval value
John Linnb85a3ef2011-06-20 11:47:27 -0600105 *
106 * @timer: Pointer to the timer instance
107 * @cycles: Timer interval ticks
108 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100109static void ttc_set_interval(struct ttc_timer *timer,
John Linnb85a3ef2011-06-20 11:47:27 -0600110 unsigned long cycles)
111{
112 u32 ctrl_reg;
113
114 /* Disable the counter, set the counter value and re-enable counter */
Michal Simek87ab4362014-04-11 15:39:29 +0200115 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
Michal Simek9e09dc52013-03-27 12:05:28 +0100116 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200117 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600118
Michal Simek87ab4362014-04-11 15:39:29 +0200119 writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600120
Soren Brinkmann03377e52012-12-19 10:18:41 -0800121 /*
122 * Reset the counter (0x10) so that it starts from 0, one-shot
123 * mode makes this needed for timing to be right.
124 */
Josh Cartwright91dc9852012-10-31 13:56:14 -0600125 ctrl_reg |= CNT_CNTRL_RESET;
Michal Simek9e09dc52013-03-27 12:05:28 +0100126 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
Michal Simek87ab4362014-04-11 15:39:29 +0200127 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600128}
129
130/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100131 * ttc_clock_event_interrupt - Clock event timer interrupt handler
John Linnb85a3ef2011-06-20 11:47:27 -0600132 *
133 * @irq: IRQ number of the Timer
Michal Simek9e09dc52013-03-27 12:05:28 +0100134 * @dev_id: void pointer to the ttc_timer instance
John Linnb85a3ef2011-06-20 11:47:27 -0600135 *
136 * returns: Always IRQ_HANDLED - success
137 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100138static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
John Linnb85a3ef2011-06-20 11:47:27 -0600139{
Michal Simek9e09dc52013-03-27 12:05:28 +0100140 struct ttc_timer_clockevent *ttce = dev_id;
141 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600142
143 /* Acknowledge the interrupt and call event handler */
Michal Simek87ab4362014-04-11 15:39:29 +0200144 readl_relaxed(timer->base_addr + TTC_ISR_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600145
Michal Simek9e09dc52013-03-27 12:05:28 +0100146 ttce->ce.event_handler(&ttce->ce);
John Linnb85a3ef2011-06-20 11:47:27 -0600147
148 return IRQ_HANDLED;
149}
150
John Linnb85a3ef2011-06-20 11:47:27 -0600151/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100152 * __ttc_clocksource_read - Reads the timer counter register
John Linnb85a3ef2011-06-20 11:47:27 -0600153 *
154 * returns: Current timer counter register value
155 **/
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100156static u64 __ttc_clocksource_read(struct clocksource *cs)
John Linnb85a3ef2011-06-20 11:47:27 -0600157{
Michal Simek9e09dc52013-03-27 12:05:28 +0100158 struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600159
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100160 return (u64)readl_relaxed(timer->base_addr +
Michal Simek9e09dc52013-03-27 12:05:28 +0100161 TTC_COUNT_VAL_OFFSET);
John Linnb85a3ef2011-06-20 11:47:27 -0600162}
163
Stephen Boyddfded002013-11-20 00:47:32 +0100164static u64 notrace ttc_sched_clock_read(void)
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700165{
Michal Simek87ab4362014-04-11 15:39:29 +0200166 return readl_relaxed(ttc_sched_clock_val_reg);
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700167}
168
John Linnb85a3ef2011-06-20 11:47:27 -0600169/**
Michal Simek9e09dc52013-03-27 12:05:28 +0100170 * ttc_set_next_event - Sets the time interval for next event
John Linnb85a3ef2011-06-20 11:47:27 -0600171 *
172 * @cycles: Timer interval ticks
173 * @evt: Address of clock event instance
174 *
175 * returns: Always 0 - success
176 **/
Michal Simek9e09dc52013-03-27 12:05:28 +0100177static int ttc_set_next_event(unsigned long cycles,
John Linnb85a3ef2011-06-20 11:47:27 -0600178 struct clock_event_device *evt)
179{
Michal Simek9e09dc52013-03-27 12:05:28 +0100180 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
181 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600182
Michal Simek9e09dc52013-03-27 12:05:28 +0100183 ttc_set_interval(timer, cycles);
John Linnb85a3ef2011-06-20 11:47:27 -0600184 return 0;
185}
186
187/**
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530188 * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer
John Linnb85a3ef2011-06-20 11:47:27 -0600189 *
John Linnb85a3ef2011-06-20 11:47:27 -0600190 * @evt: Address of clock event instance
191 **/
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530192static int ttc_shutdown(struct clock_event_device *evt)
John Linnb85a3ef2011-06-20 11:47:27 -0600193{
Michal Simek9e09dc52013-03-27 12:05:28 +0100194 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
195 struct ttc_timer *timer = &ttce->ttc;
John Linnb85a3ef2011-06-20 11:47:27 -0600196 u32 ctrl_reg;
197
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530198 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
199 ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
200 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
201 return 0;
202}
203
204static int ttc_set_periodic(struct clock_event_device *evt)
205{
206 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
207 struct ttc_timer *timer = &ttce->ttc;
208
209 ttc_set_interval(timer,
210 DIV_ROUND_CLOSEST(ttce->ttc.freq, PRESCALE * HZ));
211 return 0;
212}
213
214static int ttc_resume(struct clock_event_device *evt)
215{
216 struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
217 struct ttc_timer *timer = &ttce->ttc;
218 u32 ctrl_reg;
219
220 ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
221 ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
222 writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
223 return 0;
John Linnb85a3ef2011-06-20 11:47:27 -0600224}
225
Michal Simek9e09dc52013-03-27 12:05:28 +0100226static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100227 unsigned long event, void *data)
228{
229 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100230 struct ttc_timer *ttc = to_ttc_timer(nb);
231 struct ttc_timer_clocksource *ttccs = container_of(ttc,
232 struct ttc_timer_clocksource, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100233
234 switch (event) {
Michal Simeke9329002013-03-20 10:15:28 +0100235 case PRE_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800236 {
237 u32 psv;
238 unsigned long factor, rate_low, rate_high;
239
240 if (ndata->new_rate > ndata->old_rate) {
241 factor = DIV_ROUND_CLOSEST(ndata->new_rate,
242 ndata->old_rate);
243 rate_low = ndata->old_rate;
244 rate_high = ndata->new_rate;
245 } else {
246 factor = DIV_ROUND_CLOSEST(ndata->old_rate,
247 ndata->new_rate);
248 rate_low = ndata->new_rate;
249 rate_high = ndata->old_rate;
250 }
251
252 if (!is_power_of_2(factor))
253 return NOTIFY_BAD;
254
255 if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
256 return NOTIFY_BAD;
257
258 factor = __ilog2_u32(factor);
259
260 /*
261 * store timer clock ctrl register so we can restore it in case
262 * of an abort.
263 */
264 ttccs->scale_clk_ctrl_reg_old =
Michal Simek87ab4362014-04-11 15:39:29 +0200265 readl_relaxed(ttccs->ttc.base_addr +
266 TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800267
268 psv = (ttccs->scale_clk_ctrl_reg_old &
269 TTC_CLK_CNTRL_PSV_MASK) >>
270 TTC_CLK_CNTRL_PSV_SHIFT;
271 if (ndata->new_rate < ndata->old_rate)
272 psv -= factor;
273 else
274 psv += factor;
275
276 /* prescaler within legal range? */
277 if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
278 return NOTIFY_BAD;
279
280 ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
281 ~TTC_CLK_CNTRL_PSV_MASK;
282 ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
283
284
285 /* scale down: adjust divider in post-change notification */
286 if (ndata->new_rate < ndata->old_rate)
287 return NOTIFY_DONE;
288
289 /* scale up: adjust divider now - before frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200290 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
291 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800292 break;
293 }
294 case POST_RATE_CHANGE:
295 /* scale up: pre-change notification did the adjustment */
296 if (ndata->new_rate > ndata->old_rate)
297 return NOTIFY_OK;
298
299 /* scale down: adjust divider now - after frequency change */
Michal Simek87ab4362014-04-11 15:39:29 +0200300 writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
301 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800302 break;
303
Michal Simeke9329002013-03-20 10:15:28 +0100304 case ABORT_RATE_CHANGE:
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800305 /* we have to undo the adjustment in case we scale up */
306 if (ndata->new_rate < ndata->old_rate)
307 return NOTIFY_OK;
308
309 /* restore original register value */
Michal Simek87ab4362014-04-11 15:39:29 +0200310 writel_relaxed(ttccs->scale_clk_ctrl_reg_old,
311 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500312 fallthrough;
Michal Simeke9329002013-03-20 10:15:28 +0100313 default:
314 return NOTIFY_DONE;
315 }
Soren Brinkmannb3e90722014-02-19 15:14:42 -0800316
317 return NOTIFY_DONE;
Michal Simeke9329002013-03-20 10:15:28 +0100318}
319
Daniel Lezcano70504f32016-05-31 19:52:09 +0200320static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
Michal Simek4e2bec02014-09-29 01:50:05 +0200321 u32 timer_width)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600322{
Michal Simek9e09dc52013-03-27 12:05:28 +0100323 struct ttc_timer_clocksource *ttccs;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600324 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600325
326 ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200327 if (!ttccs)
328 return -ENOMEM;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600329
Michal Simek9e09dc52013-03-27 12:05:28 +0100330 ttccs->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100331
Michal Simek9e09dc52013-03-27 12:05:28 +0100332 err = clk_prepare_enable(ttccs->ttc.clk);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200333 if (err) {
Michal Simekc5263bb2013-03-20 10:24:59 +0100334 kfree(ttccs);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200335 return err;
Michal Simekc5263bb2013-03-20 10:24:59 +0100336 }
Josh Cartwright91dc9852012-10-31 13:56:14 -0600337
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800338 ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
339
Michal Simek9e09dc52013-03-27 12:05:28 +0100340 ttccs->ttc.clk_rate_change_nb.notifier_call =
341 ttc_rate_change_clocksource_cb;
342 ttccs->ttc.clk_rate_change_nb.next = NULL;
Daniel Lezcano70504f32016-05-31 19:52:09 +0200343
344 err = clk_notifier_register(ttccs->ttc.clk,
345 &ttccs->ttc.clk_rate_change_nb);
346 if (err)
Michal Simeke9329002013-03-20 10:15:28 +0100347 pr_warn("Unable to register clock notifier.\n");
Josh Cartwright91dc9852012-10-31 13:56:14 -0600348
Michal Simek9e09dc52013-03-27 12:05:28 +0100349 ttccs->ttc.base_addr = base;
350 ttccs->cs.name = "ttc_clocksource";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600351 ttccs->cs.rating = 200;
Michal Simek9e09dc52013-03-27 12:05:28 +0100352 ttccs->cs.read = __ttc_clocksource_read;
Michal Simek4e2bec02014-09-29 01:50:05 +0200353 ttccs->cs.mask = CLOCKSOURCE_MASK(timer_width);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600354 ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
355
Michal Simeke9329002013-03-20 10:15:28 +0100356 /*
357 * Setup the clock source counter to be an incrementing counter
358 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
359 * it by 32 also. Let it start running now.
360 */
Michal Simek87ab4362014-04-11 15:39:29 +0200361 writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
362 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100363 ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200364 writel_relaxed(CNT_CNTRL_RESET,
Michal Simek9e09dc52013-03-27 12:05:28 +0100365 ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600366
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800367 err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200368 if (err) {
Michal Simekc5263bb2013-03-20 10:24:59 +0100369 kfree(ttccs);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200370 return err;
Michal Simekc5263bb2013-03-20 10:24:59 +0100371 }
Soren Brinkmann3d77b302013-07-08 09:51:38 -0700372
373 ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
Michal Simek4e2bec02014-09-29 01:50:05 +0200374 sched_clock_register(ttc_sched_clock_read, timer_width,
375 ttccs->ttc.freq / PRESCALE);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200376
377 return 0;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600378}
379
Michal Simek9e09dc52013-03-27 12:05:28 +0100380static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
Michal Simeke9329002013-03-20 10:15:28 +0100381 unsigned long event, void *data)
382{
383 struct clk_notifier_data *ndata = data;
Michal Simek9e09dc52013-03-27 12:05:28 +0100384 struct ttc_timer *ttc = to_ttc_timer(nb);
385 struct ttc_timer_clockevent *ttcce = container_of(ttc,
386 struct ttc_timer_clockevent, ttc);
Michal Simeke9329002013-03-20 10:15:28 +0100387
388 switch (event) {
389 case POST_RATE_CHANGE:
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800390 /* update cached frequency */
391 ttc->freq = ndata->new_rate;
392
Soren Brinkmann5f0ba3b2014-02-19 15:14:41 -0800393 clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE);
394
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500395 fallthrough;
Michal Simeke9329002013-03-20 10:15:28 +0100396 case PRE_RATE_CHANGE:
397 case ABORT_RATE_CHANGE:
398 default:
399 return NOTIFY_DONE;
400 }
401}
402
Daniel Lezcano70504f32016-05-31 19:52:09 +0200403static int __init ttc_setup_clockevent(struct clk *clk,
404 void __iomem *base, u32 irq)
Josh Cartwright91dc9852012-10-31 13:56:14 -0600405{
Michal Simek9e09dc52013-03-27 12:05:28 +0100406 struct ttc_timer_clockevent *ttcce;
Michal Simeke9329002013-03-20 10:15:28 +0100407 int err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600408
409 ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200410 if (!ttcce)
411 return -ENOMEM;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600412
Michal Simek9e09dc52013-03-27 12:05:28 +0100413 ttcce->ttc.clk = clk;
Michal Simeke9329002013-03-20 10:15:28 +0100414
Michal Simek9e09dc52013-03-27 12:05:28 +0100415 err = clk_prepare_enable(ttcce->ttc.clk);
Yu Kuai93bf9202020-11-16 21:51:23 +0800416 if (err)
417 goto out_kfree;
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;
Daniel Lezcano70504f32016-05-31 19:52:09 +0200422
423 err = clk_notifier_register(ttcce->ttc.clk,
424 &ttcce->ttc.clk_rate_change_nb);
425 if (err) {
Michal Simeke9329002013-03-20 10:15:28 +0100426 pr_warn("Unable to register clock notifier.\n");
Yu Kuai93bf9202020-11-16 21:51:23 +0800427 goto out_kfree;
Daniel Lezcano70504f32016-05-31 19:52:09 +0200428 }
429
Soren Brinkmannc1dcc922013-11-26 17:04:50 -0800430 ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600431
Michal Simek9e09dc52013-03-27 12:05:28 +0100432 ttcce->ttc.base_addr = base;
433 ttcce->ce.name = "ttc_clockevent";
Josh Cartwright91dc9852012-10-31 13:56:14 -0600434 ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Michal Simek9e09dc52013-03-27 12:05:28 +0100435 ttcce->ce.set_next_event = ttc_set_next_event;
Viresh Kumar5c0a4bb2015-06-18 16:24:16 +0530436 ttcce->ce.set_state_shutdown = ttc_shutdown;
437 ttcce->ce.set_state_periodic = ttc_set_periodic;
438 ttcce->ce.set_state_oneshot = ttc_shutdown;
439 ttcce->ce.tick_resume = ttc_resume;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600440 ttcce->ce.rating = 200;
441 ttcce->ce.irq = irq;
Soren Brinkmann87e4ee72012-12-19 10:18:42 -0800442 ttcce->ce.cpumask = cpu_possible_mask;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600443
Michal Simeke9329002013-03-20 10:15:28 +0100444 /*
445 * Setup the clock event timer to be an interval timer which
446 * is prescaled by 32 using the interval interrupt. Leave it
447 * disabled for now.
448 */
Michal Simek87ab4362014-04-11 15:39:29 +0200449 writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
450 writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
Michal Simek9e09dc52013-03-27 12:05:28 +0100451 ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
Michal Simek87ab4362014-04-11 15:39:29 +0200452 writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
Josh Cartwright91dc9852012-10-31 13:56:14 -0600453
Michal Simek9e09dc52013-03-27 12:05:28 +0100454 err = request_irq(irq, ttc_clock_event_interrupt,
Michael Opdenacker38c30a82013-12-09 10:12:10 +0100455 IRQF_TIMER, ttcce->ce.name, ttcce);
Yu Kuai93bf9202020-11-16 21:51:23 +0800456 if (err)
457 goto out_kfree;
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);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200461
462 return 0;
Yu Kuai93bf9202020-11-16 21:51:23 +0800463
464out_kfree:
465 kfree(ttcce);
466 return err;
Josh Cartwright91dc9852012-10-31 13:56:14 -0600467}
468
Rajan Vajaf5ac8962019-11-07 02:36:28 -0800469static int __init ttc_timer_probe(struct platform_device *pdev)
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;
Daniel Lezcano70504f32016-05-31 19:52:09 +0200475 int clksel, ret;
Michal Simek4e2bec02014-09-29 01:50:05 +0200476 u32 timer_width = 16;
Rajan Vajaf5ac8962019-11-07 02:36:28 -0800477 struct device_node *timer = pdev->dev.of_node;
Michal Simekc5263bb2013-03-20 10:24:59 +0100478
479 if (initialized)
Daniel Lezcano70504f32016-05-31 19:52:09 +0200480 return 0;
Michal Simekc5263bb2013-03-20 10:24:59 +0100481
482 initialized = 1;
Michal Simeke9329002013-03-20 10:15:28 +0100483
484 /*
485 * Get the 1st Triple Timer Counter (TTC) block from the device tree
486 * and use it. Note that the event timer uses the interrupt and it's the
487 * 2nd TTC hence the irq_of_parse_and_map(,1)
488 */
489 timer_baseaddr = of_iomap(timer, 0);
490 if (!timer_baseaddr) {
491 pr_err("ERROR: invalid timer base address\n");
Daniel Lezcano70504f32016-05-31 19:52:09 +0200492 return -ENXIO;
Michal Simeke9329002013-03-20 10:15:28 +0100493 }
494
495 irq = irq_of_parse_and_map(timer, 1);
496 if (irq <= 0) {
497 pr_err("ERROR: invalid interrupt number\n");
Daniel Lezcano70504f32016-05-31 19:52:09 +0200498 return -EINVAL;
Michal Simeke9329002013-03-20 10:15:28 +0100499 }
500
Michal Simek4e2bec02014-09-29 01:50:05 +0200501 of_property_read_u32(timer, "timer-width", &timer_width);
502
Michal Simek87ab4362014-04-11 15:39:29 +0200503 clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700504 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
505 clk_cs = of_clk_get(timer, clksel);
506 if (IS_ERR(clk_cs)) {
Michal Simeke9329002013-03-20 10:15:28 +0100507 pr_err("ERROR: timer input clock not found\n");
Daniel Lezcano70504f32016-05-31 19:52:09 +0200508 return PTR_ERR(clk_cs);
Michal Simeke9329002013-03-20 10:15:28 +0100509 }
510
Michal Simek87ab4362014-04-11 15:39:29 +0200511 clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700512 clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
513 clk_ce = of_clk_get(timer, clksel);
514 if (IS_ERR(clk_ce)) {
515 pr_err("ERROR: timer input clock not found\n");
Christophe Jaillet34c720a2016-07-06 07:35:23 +0200516 return PTR_ERR(clk_ce);
Soren Brinkmann30e1e282013-05-13 10:46:38 -0700517 }
518
Daniel Lezcano70504f32016-05-31 19:52:09 +0200519 ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
520 if (ret)
521 return ret;
522
523 ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
524 if (ret)
525 return ret;
Michal Simeke9329002013-03-20 10:15:28 +0100526
Rob Herring2a4849d2018-08-27 20:52:14 -0500527 pr_info("%pOFn #0 at %p, irq=%d\n", timer, timer_baseaddr, irq);
Daniel Lezcano70504f32016-05-31 19:52:09 +0200528
529 return 0;
Michal Simeke9329002013-03-20 10:15:28 +0100530}
531
Rajan Vajaf5ac8962019-11-07 02:36:28 -0800532static const struct of_device_id ttc_timer_of_match[] = {
533 {.compatible = "cdns,ttc"},
534 {},
535};
536
537MODULE_DEVICE_TABLE(of, ttc_timer_of_match);
538
539static struct platform_driver ttc_timer_driver = {
540 .driver = {
541 .name = "cdns_ttc_timer",
542 .of_match_table = ttc_timer_of_match,
543 },
544};
545builtin_platform_driver_probe(ttc_timer_driver, ttc_timer_probe);