iocost: Fix check condition of iocg abs_vdebt
[linux/fpc-iii.git] / drivers / clocksource / dw_apb_timer.c
blob10ce69548f1baeb808d24a4b0bf82cddca91353c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * (C) Copyright 2009 Intel Corporation
4 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 * Shared with ARM platforms, Jamie Iles, Picochip 2011
8 * Support for the Synopsys DesignWare APB Timers.
9 */
10 #include <linux/dw_apb_timer.h>
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
18 #define APBT_MIN_PERIOD 4
19 #define APBT_MIN_DELTA_USEC 200
21 #define APBTMR_N_LOAD_COUNT 0x00
22 #define APBTMR_N_CURRENT_VALUE 0x04
23 #define APBTMR_N_CONTROL 0x08
24 #define APBTMR_N_EOI 0x0c
25 #define APBTMR_N_INT_STATUS 0x10
27 #define APBTMRS_INT_STATUS 0xa0
28 #define APBTMRS_EOI 0xa4
29 #define APBTMRS_RAW_INT_STATUS 0xa8
30 #define APBTMRS_COMP_VERSION 0xac
32 #define APBTMR_CONTROL_ENABLE (1 << 0)
33 /* 1: periodic, 0:free running. */
34 #define APBTMR_CONTROL_MODE_PERIODIC (1 << 1)
35 #define APBTMR_CONTROL_INT (1 << 2)
37 static inline struct dw_apb_clock_event_device *
38 ced_to_dw_apb_ced(struct clock_event_device *evt)
40 return container_of(evt, struct dw_apb_clock_event_device, ced);
43 static inline struct dw_apb_clocksource *
44 clocksource_to_dw_apb_clocksource(struct clocksource *cs)
46 return container_of(cs, struct dw_apb_clocksource, cs);
49 static inline u32 apbt_readl(struct dw_apb_timer *timer, unsigned long offs)
51 return readl(timer->base + offs);
54 static inline void apbt_writel(struct dw_apb_timer *timer, u32 val,
55 unsigned long offs)
57 writel(val, timer->base + offs);
60 static inline u32 apbt_readl_relaxed(struct dw_apb_timer *timer, unsigned long offs)
62 return readl_relaxed(timer->base + offs);
65 static inline void apbt_writel_relaxed(struct dw_apb_timer *timer, u32 val,
66 unsigned long offs)
68 writel_relaxed(val, timer->base + offs);
71 static void apbt_disable_int(struct dw_apb_timer *timer)
73 u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
75 ctrl |= APBTMR_CONTROL_INT;
76 apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
79 /**
80 * dw_apb_clockevent_pause() - stop the clock_event_device from running
82 * @dw_ced: The APB clock to stop generating events.
84 void dw_apb_clockevent_pause(struct dw_apb_clock_event_device *dw_ced)
86 disable_irq(dw_ced->timer.irq);
87 apbt_disable_int(&dw_ced->timer);
90 static void apbt_eoi(struct dw_apb_timer *timer)
92 apbt_readl_relaxed(timer, APBTMR_N_EOI);
95 static irqreturn_t dw_apb_clockevent_irq(int irq, void *data)
97 struct clock_event_device *evt = data;
98 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
100 if (!evt->event_handler) {
101 pr_info("Spurious APBT timer interrupt %d\n", irq);
102 return IRQ_NONE;
105 if (dw_ced->eoi)
106 dw_ced->eoi(&dw_ced->timer);
108 evt->event_handler(evt);
109 return IRQ_HANDLED;
112 static void apbt_enable_int(struct dw_apb_timer *timer)
114 u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
115 /* clear pending intr */
116 apbt_readl(timer, APBTMR_N_EOI);
117 ctrl &= ~APBTMR_CONTROL_INT;
118 apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
121 static int apbt_shutdown(struct clock_event_device *evt)
123 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
124 u32 ctrl;
126 pr_debug("%s CPU %d state=shutdown\n", __func__,
127 cpumask_first(evt->cpumask));
129 ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
130 ctrl &= ~APBTMR_CONTROL_ENABLE;
131 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
132 return 0;
135 static int apbt_set_oneshot(struct clock_event_device *evt)
137 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
138 u32 ctrl;
140 pr_debug("%s CPU %d state=oneshot\n", __func__,
141 cpumask_first(evt->cpumask));
143 ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
145 * set free running mode, this mode will let timer reload max
146 * timeout which will give time (3min on 25MHz clock) to rearm
147 * the next event, therefore emulate the one-shot mode.
149 ctrl &= ~APBTMR_CONTROL_ENABLE;
150 ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
152 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
153 /* write again to set free running mode */
154 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
157 * DW APB p. 46, load counter with all 1s before starting free
158 * running mode.
160 apbt_writel(&dw_ced->timer, ~0, APBTMR_N_LOAD_COUNT);
161 ctrl &= ~APBTMR_CONTROL_INT;
162 ctrl |= APBTMR_CONTROL_ENABLE;
163 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
164 return 0;
167 static int apbt_set_periodic(struct clock_event_device *evt)
169 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
170 unsigned long period = DIV_ROUND_UP(dw_ced->timer.freq, HZ);
171 u32 ctrl;
173 pr_debug("%s CPU %d state=periodic\n", __func__,
174 cpumask_first(evt->cpumask));
176 ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
177 ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
178 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
180 * DW APB p. 46, have to disable timer before load counter,
181 * may cause sync problem.
183 ctrl &= ~APBTMR_CONTROL_ENABLE;
184 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
185 udelay(1);
186 pr_debug("Setting clock period %lu for HZ %d\n", period, HZ);
187 apbt_writel(&dw_ced->timer, period, APBTMR_N_LOAD_COUNT);
188 ctrl |= APBTMR_CONTROL_ENABLE;
189 apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
190 return 0;
193 static int apbt_resume(struct clock_event_device *evt)
195 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
197 pr_debug("%s CPU %d state=resume\n", __func__,
198 cpumask_first(evt->cpumask));
200 apbt_enable_int(&dw_ced->timer);
201 return 0;
204 static int apbt_next_event(unsigned long delta,
205 struct clock_event_device *evt)
207 u32 ctrl;
208 struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
210 /* Disable timer */
211 ctrl = apbt_readl_relaxed(&dw_ced->timer, APBTMR_N_CONTROL);
212 ctrl &= ~APBTMR_CONTROL_ENABLE;
213 apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
214 /* write new count */
215 apbt_writel_relaxed(&dw_ced->timer, delta, APBTMR_N_LOAD_COUNT);
216 ctrl |= APBTMR_CONTROL_ENABLE;
217 apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
219 return 0;
223 * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
225 * @cpu: The CPU the events will be targeted at or -1 if CPU affiliation
226 * isn't required.
227 * @name: The name used for the timer and the IRQ for it.
228 * @rating: The rating to give the timer.
229 * @base: I/O base for the timer registers.
230 * @irq: The interrupt number to use for the timer.
231 * @freq: The frequency that the timer counts at.
233 * This creates a clock_event_device for using with the generic clock layer
234 * but does not start and register it. This should be done with
235 * dw_apb_clockevent_register() as the next step. If this is the first time
236 * it has been called for a timer then the IRQ will be requested, if not it
237 * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
238 * releasing the IRQ.
240 struct dw_apb_clock_event_device *
241 dw_apb_clockevent_init(int cpu, const char *name, unsigned rating,
242 void __iomem *base, int irq, unsigned long freq)
244 struct dw_apb_clock_event_device *dw_ced =
245 kzalloc(sizeof(*dw_ced), GFP_KERNEL);
246 int err;
248 if (!dw_ced)
249 return NULL;
251 dw_ced->timer.base = base;
252 dw_ced->timer.irq = irq;
253 dw_ced->timer.freq = freq;
255 clockevents_calc_mult_shift(&dw_ced->ced, freq, APBT_MIN_PERIOD);
256 dw_ced->ced.max_delta_ns = clockevent_delta2ns(0x7fffffff,
257 &dw_ced->ced);
258 dw_ced->ced.max_delta_ticks = 0x7fffffff;
259 dw_ced->ced.min_delta_ns = clockevent_delta2ns(5000, &dw_ced->ced);
260 dw_ced->ced.min_delta_ticks = 5000;
261 dw_ced->ced.cpumask = cpu < 0 ? cpu_possible_mask : cpumask_of(cpu);
262 dw_ced->ced.features = CLOCK_EVT_FEAT_PERIODIC |
263 CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ;
264 dw_ced->ced.set_state_shutdown = apbt_shutdown;
265 dw_ced->ced.set_state_periodic = apbt_set_periodic;
266 dw_ced->ced.set_state_oneshot = apbt_set_oneshot;
267 dw_ced->ced.set_state_oneshot_stopped = apbt_shutdown;
268 dw_ced->ced.tick_resume = apbt_resume;
269 dw_ced->ced.set_next_event = apbt_next_event;
270 dw_ced->ced.irq = dw_ced->timer.irq;
271 dw_ced->ced.rating = rating;
272 dw_ced->ced.name = name;
274 dw_ced->irqaction.name = dw_ced->ced.name;
275 dw_ced->irqaction.handler = dw_apb_clockevent_irq;
276 dw_ced->irqaction.dev_id = &dw_ced->ced;
277 dw_ced->irqaction.irq = irq;
278 dw_ced->irqaction.flags = IRQF_TIMER | IRQF_IRQPOLL |
279 IRQF_NOBALANCING;
281 dw_ced->eoi = apbt_eoi;
282 err = setup_irq(irq, &dw_ced->irqaction);
283 if (err) {
284 pr_err("failed to request timer irq\n");
285 kfree(dw_ced);
286 dw_ced = NULL;
289 return dw_ced;
293 * dw_apb_clockevent_resume() - resume a clock that has been paused.
295 * @dw_ced: The APB clock to resume.
297 void dw_apb_clockevent_resume(struct dw_apb_clock_event_device *dw_ced)
299 enable_irq(dw_ced->timer.irq);
303 * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
305 * @dw_ced: The APB clock to stop generating the events.
307 void dw_apb_clockevent_stop(struct dw_apb_clock_event_device *dw_ced)
309 free_irq(dw_ced->timer.irq, &dw_ced->ced);
313 * dw_apb_clockevent_register() - register the clock with the generic layer
315 * @dw_ced: The APB clock to register as a clock_event_device.
317 void dw_apb_clockevent_register(struct dw_apb_clock_event_device *dw_ced)
319 apbt_writel(&dw_ced->timer, 0, APBTMR_N_CONTROL);
320 clockevents_register_device(&dw_ced->ced);
321 apbt_enable_int(&dw_ced->timer);
325 * dw_apb_clocksource_start() - start the clocksource counting.
327 * @dw_cs: The clocksource to start.
329 * This is used to start the clocksource before registration and can be used
330 * to enable calibration of timers.
332 void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs)
335 * start count down from 0xffff_ffff. this is done by toggling the
336 * enable bit then load initial load count to ~0.
338 u32 ctrl = apbt_readl(&dw_cs->timer, APBTMR_N_CONTROL);
340 ctrl &= ~APBTMR_CONTROL_ENABLE;
341 apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
342 apbt_writel(&dw_cs->timer, ~0, APBTMR_N_LOAD_COUNT);
343 /* enable, mask interrupt */
344 ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
345 ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
346 apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
347 /* read it once to get cached counter value initialized */
348 dw_apb_clocksource_read(dw_cs);
351 static u64 __apbt_read_clocksource(struct clocksource *cs)
353 u32 current_count;
354 struct dw_apb_clocksource *dw_cs =
355 clocksource_to_dw_apb_clocksource(cs);
357 current_count = apbt_readl_relaxed(&dw_cs->timer,
358 APBTMR_N_CURRENT_VALUE);
360 return (u64)~current_count;
363 static void apbt_restart_clocksource(struct clocksource *cs)
365 struct dw_apb_clocksource *dw_cs =
366 clocksource_to_dw_apb_clocksource(cs);
368 dw_apb_clocksource_start(dw_cs);
372 * dw_apb_clocksource_init() - use an APB timer as a clocksource.
374 * @rating: The rating to give the clocksource.
375 * @name: The name for the clocksource.
376 * @base: The I/O base for the timer registers.
377 * @freq: The frequency that the timer counts at.
379 * This creates a clocksource using an APB timer but does not yet register it
380 * with the clocksource system. This should be done with
381 * dw_apb_clocksource_register() as the next step.
383 struct dw_apb_clocksource *
384 dw_apb_clocksource_init(unsigned rating, const char *name, void __iomem *base,
385 unsigned long freq)
387 struct dw_apb_clocksource *dw_cs = kzalloc(sizeof(*dw_cs), GFP_KERNEL);
389 if (!dw_cs)
390 return NULL;
392 dw_cs->timer.base = base;
393 dw_cs->timer.freq = freq;
394 dw_cs->cs.name = name;
395 dw_cs->cs.rating = rating;
396 dw_cs->cs.read = __apbt_read_clocksource;
397 dw_cs->cs.mask = CLOCKSOURCE_MASK(32);
398 dw_cs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
399 dw_cs->cs.resume = apbt_restart_clocksource;
401 return dw_cs;
405 * dw_apb_clocksource_register() - register the APB clocksource.
407 * @dw_cs: The clocksource to register.
409 void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs)
411 clocksource_register_hz(&dw_cs->cs, dw_cs->timer.freq);
415 * dw_apb_clocksource_read() - read the current value of a clocksource.
417 * @dw_cs: The clocksource to read.
419 u64 dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs)
421 return (u64)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);