Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clocksource / timer-davinci.c
blobbb4eee31ae082baf17a9d55fed38018060433250
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI DaVinci clocksource driver
5 * Copyright (C) 2019 Texas Instruments
6 * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
7 * (with tiny parts adopted from code by Kevin Hilman <khilman@baylibre.com>)
8 */
10 #include <linux/clk.h>
11 #include <linux/clockchips.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/sched_clock.h>
18 #include <clocksource/timer-davinci.h>
20 #undef pr_fmt
21 #define pr_fmt(fmt) "%s: " fmt, __func__
23 #define DAVINCI_TIMER_REG_TIM12 0x10
24 #define DAVINCI_TIMER_REG_TIM34 0x14
25 #define DAVINCI_TIMER_REG_PRD12 0x18
26 #define DAVINCI_TIMER_REG_PRD34 0x1c
27 #define DAVINCI_TIMER_REG_TCR 0x20
28 #define DAVINCI_TIMER_REG_TGCR 0x24
30 #define DAVINCI_TIMER_TIMMODE_MASK GENMASK(3, 2)
31 #define DAVINCI_TIMER_RESET_MASK GENMASK(1, 0)
32 #define DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED BIT(2)
33 #define DAVINCI_TIMER_UNRESET GENMASK(1, 0)
35 #define DAVINCI_TIMER_ENAMODE_MASK GENMASK(1, 0)
36 #define DAVINCI_TIMER_ENAMODE_DISABLED 0x00
37 #define DAVINCI_TIMER_ENAMODE_ONESHOT BIT(0)
38 #define DAVINCI_TIMER_ENAMODE_PERIODIC BIT(1)
40 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM12 6
41 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM34 22
43 #define DAVINCI_TIMER_MIN_DELTA 0x01
44 #define DAVINCI_TIMER_MAX_DELTA 0xfffffffe
46 #define DAVINCI_TIMER_CLKSRC_BITS 32
48 #define DAVINCI_TIMER_TGCR_DEFAULT \
49 (DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED | DAVINCI_TIMER_UNRESET)
51 struct davinci_clockevent {
52 struct clock_event_device dev;
53 void __iomem *base;
54 unsigned int cmp_off;
58 * This must be globally accessible by davinci_timer_read_sched_clock(), so
59 * let's keep it here.
61 static struct {
62 struct clocksource dev;
63 void __iomem *base;
64 unsigned int tim_off;
65 } davinci_clocksource;
67 static struct davinci_clockevent *
68 to_davinci_clockevent(struct clock_event_device *clockevent)
70 return container_of(clockevent, struct davinci_clockevent, dev);
73 static unsigned int
74 davinci_clockevent_read(struct davinci_clockevent *clockevent,
75 unsigned int reg)
77 return readl_relaxed(clockevent->base + reg);
80 static void davinci_clockevent_write(struct davinci_clockevent *clockevent,
81 unsigned int reg, unsigned int val)
83 writel_relaxed(val, clockevent->base + reg);
86 static void davinci_tim12_shutdown(void __iomem *base)
88 unsigned int tcr;
90 tcr = DAVINCI_TIMER_ENAMODE_DISABLED <<
91 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12;
93 * This function is only ever called if we're using both timer
94 * halves. In this case TIM34 runs in periodic mode and we must
95 * not modify it.
97 tcr |= DAVINCI_TIMER_ENAMODE_PERIODIC <<
98 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34;
100 writel_relaxed(tcr, base + DAVINCI_TIMER_REG_TCR);
103 static void davinci_tim12_set_oneshot(void __iomem *base)
105 unsigned int tcr;
107 tcr = DAVINCI_TIMER_ENAMODE_ONESHOT <<
108 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12;
109 /* Same as above. */
110 tcr |= DAVINCI_TIMER_ENAMODE_PERIODIC <<
111 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34;
113 writel_relaxed(tcr, base + DAVINCI_TIMER_REG_TCR);
116 static int davinci_clockevent_shutdown(struct clock_event_device *dev)
118 struct davinci_clockevent *clockevent;
120 clockevent = to_davinci_clockevent(dev);
122 davinci_tim12_shutdown(clockevent->base);
124 return 0;
127 static int davinci_clockevent_set_oneshot(struct clock_event_device *dev)
129 struct davinci_clockevent *clockevent = to_davinci_clockevent(dev);
131 davinci_clockevent_write(clockevent, DAVINCI_TIMER_REG_TIM12, 0x0);
133 davinci_tim12_set_oneshot(clockevent->base);
135 return 0;
138 static int
139 davinci_clockevent_set_next_event_std(unsigned long cycles,
140 struct clock_event_device *dev)
142 struct davinci_clockevent *clockevent = to_davinci_clockevent(dev);
144 davinci_clockevent_shutdown(dev);
146 davinci_clockevent_write(clockevent, DAVINCI_TIMER_REG_TIM12, 0x0);
147 davinci_clockevent_write(clockevent, DAVINCI_TIMER_REG_PRD12, cycles);
149 davinci_clockevent_set_oneshot(dev);
151 return 0;
154 static int
155 davinci_clockevent_set_next_event_cmp(unsigned long cycles,
156 struct clock_event_device *dev)
158 struct davinci_clockevent *clockevent = to_davinci_clockevent(dev);
159 unsigned int curr_time;
161 curr_time = davinci_clockevent_read(clockevent,
162 DAVINCI_TIMER_REG_TIM12);
163 davinci_clockevent_write(clockevent,
164 clockevent->cmp_off, curr_time + cycles);
166 return 0;
169 static irqreturn_t davinci_timer_irq_timer(int irq, void *data)
171 struct davinci_clockevent *clockevent = data;
173 if (!clockevent_state_oneshot(&clockevent->dev))
174 davinci_tim12_shutdown(clockevent->base);
176 clockevent->dev.event_handler(&clockevent->dev);
178 return IRQ_HANDLED;
181 static u64 notrace davinci_timer_read_sched_clock(void)
183 return readl_relaxed(davinci_clocksource.base +
184 davinci_clocksource.tim_off);
187 static u64 davinci_clocksource_read(struct clocksource *dev)
189 return davinci_timer_read_sched_clock();
193 * Standard use-case: we're using tim12 for clockevent and tim34 for
194 * clocksource. The default is making the former run in oneshot mode
195 * and the latter in periodic mode.
197 static void davinci_clocksource_init_tim34(void __iomem *base)
199 int tcr;
201 tcr = DAVINCI_TIMER_ENAMODE_PERIODIC <<
202 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34;
203 tcr |= DAVINCI_TIMER_ENAMODE_ONESHOT <<
204 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12;
206 writel_relaxed(0x0, base + DAVINCI_TIMER_REG_TIM34);
207 writel_relaxed(UINT_MAX, base + DAVINCI_TIMER_REG_PRD34);
208 writel_relaxed(tcr, base + DAVINCI_TIMER_REG_TCR);
212 * Special use-case on da830: the DSP may use tim34. We're using tim12 for
213 * both clocksource and clockevent. We set tim12 to periodic and don't touch
214 * tim34.
216 static void davinci_clocksource_init_tim12(void __iomem *base)
218 unsigned int tcr;
220 tcr = DAVINCI_TIMER_ENAMODE_PERIODIC <<
221 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12;
223 writel_relaxed(0x0, base + DAVINCI_TIMER_REG_TIM12);
224 writel_relaxed(UINT_MAX, base + DAVINCI_TIMER_REG_PRD12);
225 writel_relaxed(tcr, base + DAVINCI_TIMER_REG_TCR);
228 static void davinci_timer_init(void __iomem *base)
230 /* Set clock to internal mode and disable it. */
231 writel_relaxed(0x0, base + DAVINCI_TIMER_REG_TCR);
233 * Reset both 32-bit timers, set no prescaler for timer 34, set the
234 * timer to dual 32-bit unchained mode, unreset both 32-bit timers.
236 writel_relaxed(DAVINCI_TIMER_TGCR_DEFAULT,
237 base + DAVINCI_TIMER_REG_TGCR);
238 /* Init both counters to zero. */
239 writel_relaxed(0x0, base + DAVINCI_TIMER_REG_TIM12);
240 writel_relaxed(0x0, base + DAVINCI_TIMER_REG_TIM34);
243 int __init davinci_timer_register(struct clk *clk,
244 const struct davinci_timer_cfg *timer_cfg)
246 struct davinci_clockevent *clockevent;
247 unsigned int tick_rate;
248 void __iomem *base;
249 int rv;
251 rv = clk_prepare_enable(clk);
252 if (rv) {
253 pr_err("Unable to prepare and enable the timer clock\n");
254 return rv;
257 if (!request_mem_region(timer_cfg->reg.start,
258 resource_size(&timer_cfg->reg),
259 "davinci-timer")) {
260 pr_err("Unable to request memory region\n");
261 return -EBUSY;
264 base = ioremap(timer_cfg->reg.start, resource_size(&timer_cfg->reg));
265 if (!base) {
266 pr_err("Unable to map the register range\n");
267 return -ENOMEM;
270 davinci_timer_init(base);
271 tick_rate = clk_get_rate(clk);
273 clockevent = kzalloc(sizeof(*clockevent), GFP_KERNEL);
274 if (!clockevent)
275 return -ENOMEM;
277 clockevent->dev.name = "tim12";
278 clockevent->dev.features = CLOCK_EVT_FEAT_ONESHOT;
279 clockevent->dev.cpumask = cpumask_of(0);
280 clockevent->base = base;
282 if (timer_cfg->cmp_off) {
283 clockevent->cmp_off = timer_cfg->cmp_off;
284 clockevent->dev.set_next_event =
285 davinci_clockevent_set_next_event_cmp;
286 } else {
287 clockevent->dev.set_next_event =
288 davinci_clockevent_set_next_event_std;
289 clockevent->dev.set_state_oneshot =
290 davinci_clockevent_set_oneshot;
291 clockevent->dev.set_state_shutdown =
292 davinci_clockevent_shutdown;
295 rv = request_irq(timer_cfg->irq[DAVINCI_TIMER_CLOCKEVENT_IRQ].start,
296 davinci_timer_irq_timer, IRQF_TIMER,
297 "clockevent/tim12", clockevent);
298 if (rv) {
299 pr_err("Unable to request the clockevent interrupt\n");
300 return rv;
303 davinci_clocksource.dev.rating = 300;
304 davinci_clocksource.dev.read = davinci_clocksource_read;
305 davinci_clocksource.dev.mask =
306 CLOCKSOURCE_MASK(DAVINCI_TIMER_CLKSRC_BITS);
307 davinci_clocksource.dev.flags = CLOCK_SOURCE_IS_CONTINUOUS;
308 davinci_clocksource.base = base;
310 if (timer_cfg->cmp_off) {
311 davinci_clocksource.dev.name = "tim12";
312 davinci_clocksource.tim_off = DAVINCI_TIMER_REG_TIM12;
313 davinci_clocksource_init_tim12(base);
314 } else {
315 davinci_clocksource.dev.name = "tim34";
316 davinci_clocksource.tim_off = DAVINCI_TIMER_REG_TIM34;
317 davinci_clocksource_init_tim34(base);
320 clockevents_config_and_register(&clockevent->dev, tick_rate,
321 DAVINCI_TIMER_MIN_DELTA,
322 DAVINCI_TIMER_MAX_DELTA);
324 rv = clocksource_register_hz(&davinci_clocksource.dev, tick_rate);
325 if (rv) {
326 pr_err("Unable to register clocksource\n");
327 return rv;
330 sched_clock_register(davinci_timer_read_sched_clock,
331 DAVINCI_TIMER_CLKSRC_BITS, tick_rate);
333 return 0;
336 static int __init of_davinci_timer_register(struct device_node *np)
338 struct davinci_timer_cfg timer_cfg = { };
339 struct clk *clk;
340 int rv;
342 rv = of_address_to_resource(np, 0, &timer_cfg.reg);
343 if (rv) {
344 pr_err("Unable to get the register range for timer\n");
345 return rv;
348 rv = of_irq_to_resource_table(np, timer_cfg.irq,
349 DAVINCI_TIMER_NUM_IRQS);
350 if (rv != DAVINCI_TIMER_NUM_IRQS) {
351 pr_err("Unable to get the interrupts for timer\n");
352 return rv;
355 clk = of_clk_get(np, 0);
356 if (IS_ERR(clk)) {
357 pr_err("Unable to get the timer clock\n");
358 return PTR_ERR(clk);
361 rv = davinci_timer_register(clk, &timer_cfg);
362 if (rv)
363 clk_put(clk);
365 return rv;
367 TIMER_OF_DECLARE(davinci_timer, "ti,da830-timer", of_davinci_timer_register);