2 * (C) Copyright 2009 Intel Corporation
3 * Author: Jacob Pan (jacob.jun.pan@intel.com)
5 * Shared with ARM platforms, Jamie Iles, Picochip 2011
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Support for the Synopsys DesignWare APB Timers.
13 #include <linux/dw_apb_timer.h>
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
19 #include <linux/slab.h>
21 #define APBT_MIN_PERIOD 4
22 #define APBT_MIN_DELTA_USEC 200
24 #define APBTMR_N_LOAD_COUNT 0x00
25 #define APBTMR_N_CURRENT_VALUE 0x04
26 #define APBTMR_N_CONTROL 0x08
27 #define APBTMR_N_EOI 0x0c
28 #define APBTMR_N_INT_STATUS 0x10
30 #define APBTMRS_INT_STATUS 0xa0
31 #define APBTMRS_EOI 0xa4
32 #define APBTMRS_RAW_INT_STATUS 0xa8
33 #define APBTMRS_COMP_VERSION 0xac
35 #define APBTMR_CONTROL_ENABLE (1 << 0)
36 /* 1: periodic, 0:free running. */
37 #define APBTMR_CONTROL_MODE_PERIODIC (1 << 1)
38 #define APBTMR_CONTROL_INT (1 << 2)
40 static inline struct dw_apb_clock_event_device
*
41 ced_to_dw_apb_ced(struct clock_event_device
*evt
)
43 return container_of(evt
, struct dw_apb_clock_event_device
, ced
);
46 static inline struct dw_apb_clocksource
*
47 clocksource_to_dw_apb_clocksource(struct clocksource
*cs
)
49 return container_of(cs
, struct dw_apb_clocksource
, cs
);
52 static inline u32
apbt_readl(struct dw_apb_timer
*timer
, unsigned long offs
)
54 return readl(timer
->base
+ offs
);
57 static inline void apbt_writel(struct dw_apb_timer
*timer
, u32 val
,
60 writel(val
, timer
->base
+ offs
);
63 static inline u32
apbt_readl_relaxed(struct dw_apb_timer
*timer
, unsigned long offs
)
65 return readl_relaxed(timer
->base
+ offs
);
68 static inline void apbt_writel_relaxed(struct dw_apb_timer
*timer
, u32 val
,
71 writel_relaxed(val
, timer
->base
+ offs
);
74 static void apbt_disable_int(struct dw_apb_timer
*timer
)
76 u32 ctrl
= apbt_readl(timer
, APBTMR_N_CONTROL
);
78 ctrl
|= APBTMR_CONTROL_INT
;
79 apbt_writel(timer
, ctrl
, APBTMR_N_CONTROL
);
83 * dw_apb_clockevent_pause() - stop the clock_event_device from running
85 * @dw_ced: The APB clock to stop generating events.
87 void dw_apb_clockevent_pause(struct dw_apb_clock_event_device
*dw_ced
)
89 disable_irq(dw_ced
->timer
.irq
);
90 apbt_disable_int(&dw_ced
->timer
);
93 static void apbt_eoi(struct dw_apb_timer
*timer
)
95 apbt_readl_relaxed(timer
, APBTMR_N_EOI
);
98 static irqreturn_t
dw_apb_clockevent_irq(int irq
, void *data
)
100 struct clock_event_device
*evt
= data
;
101 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
103 if (!evt
->event_handler
) {
104 pr_info("Spurious APBT timer interrupt %d\n", irq
);
109 dw_ced
->eoi(&dw_ced
->timer
);
111 evt
->event_handler(evt
);
115 static void apbt_enable_int(struct dw_apb_timer
*timer
)
117 u32 ctrl
= apbt_readl(timer
, APBTMR_N_CONTROL
);
118 /* clear pending intr */
119 apbt_readl(timer
, APBTMR_N_EOI
);
120 ctrl
&= ~APBTMR_CONTROL_INT
;
121 apbt_writel(timer
, ctrl
, APBTMR_N_CONTROL
);
124 static int apbt_shutdown(struct clock_event_device
*evt
)
126 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
129 pr_debug("%s CPU %d state=shutdown\n", __func__
,
130 cpumask_first(evt
->cpumask
));
132 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
133 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
134 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
138 static int apbt_set_oneshot(struct clock_event_device
*evt
)
140 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
143 pr_debug("%s CPU %d state=oneshot\n", __func__
,
144 cpumask_first(evt
->cpumask
));
146 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
148 * set free running mode, this mode will let timer reload max
149 * timeout which will give time (3min on 25MHz clock) to rearm
150 * the next event, therefore emulate the one-shot mode.
152 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
153 ctrl
&= ~APBTMR_CONTROL_MODE_PERIODIC
;
155 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
156 /* write again to set free running mode */
157 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
160 * DW APB p. 46, load counter with all 1s before starting free
163 apbt_writel(&dw_ced
->timer
, ~0, APBTMR_N_LOAD_COUNT
);
164 ctrl
&= ~APBTMR_CONTROL_INT
;
165 ctrl
|= APBTMR_CONTROL_ENABLE
;
166 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
170 static int apbt_set_periodic(struct clock_event_device
*evt
)
172 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
173 unsigned long period
= DIV_ROUND_UP(dw_ced
->timer
.freq
, HZ
);
176 pr_debug("%s CPU %d state=periodic\n", __func__
,
177 cpumask_first(evt
->cpumask
));
179 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
180 ctrl
|= APBTMR_CONTROL_MODE_PERIODIC
;
181 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
183 * DW APB p. 46, have to disable timer before load counter,
184 * may cause sync problem.
186 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
187 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
189 pr_debug("Setting clock period %lu for HZ %d\n", period
, HZ
);
190 apbt_writel(&dw_ced
->timer
, period
, APBTMR_N_LOAD_COUNT
);
191 ctrl
|= APBTMR_CONTROL_ENABLE
;
192 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
196 static int apbt_resume(struct clock_event_device
*evt
)
198 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
200 pr_debug("%s CPU %d state=resume\n", __func__
,
201 cpumask_first(evt
->cpumask
));
203 apbt_enable_int(&dw_ced
->timer
);
207 static int apbt_next_event(unsigned long delta
,
208 struct clock_event_device
*evt
)
211 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
214 ctrl
= apbt_readl_relaxed(&dw_ced
->timer
, APBTMR_N_CONTROL
);
215 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
216 apbt_writel_relaxed(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
217 /* write new count */
218 apbt_writel_relaxed(&dw_ced
->timer
, delta
, APBTMR_N_LOAD_COUNT
);
219 ctrl
|= APBTMR_CONTROL_ENABLE
;
220 apbt_writel_relaxed(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
226 * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
228 * @cpu: The CPU the events will be targeted at or -1 if CPU affiliation
230 * @name: The name used for the timer and the IRQ for it.
231 * @rating: The rating to give the timer.
232 * @base: I/O base for the timer registers.
233 * @irq: The interrupt number to use for the timer.
234 * @freq: The frequency that the timer counts at.
236 * This creates a clock_event_device for using with the generic clock layer
237 * but does not start and register it. This should be done with
238 * dw_apb_clockevent_register() as the next step. If this is the first time
239 * it has been called for a timer then the IRQ will be requested, if not it
240 * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
243 struct dw_apb_clock_event_device
*
244 dw_apb_clockevent_init(int cpu
, const char *name
, unsigned rating
,
245 void __iomem
*base
, int irq
, unsigned long freq
)
247 struct dw_apb_clock_event_device
*dw_ced
=
248 kzalloc(sizeof(*dw_ced
), GFP_KERNEL
);
254 dw_ced
->timer
.base
= base
;
255 dw_ced
->timer
.irq
= irq
;
256 dw_ced
->timer
.freq
= freq
;
258 clockevents_calc_mult_shift(&dw_ced
->ced
, freq
, APBT_MIN_PERIOD
);
259 dw_ced
->ced
.max_delta_ns
= clockevent_delta2ns(0x7fffffff,
261 dw_ced
->ced
.max_delta_ticks
= 0x7fffffff;
262 dw_ced
->ced
.min_delta_ns
= clockevent_delta2ns(5000, &dw_ced
->ced
);
263 dw_ced
->ced
.min_delta_ticks
= 5000;
264 dw_ced
->ced
.cpumask
= cpu
< 0 ? cpu_possible_mask
: cpumask_of(cpu
);
265 dw_ced
->ced
.features
= CLOCK_EVT_FEAT_PERIODIC
|
266 CLOCK_EVT_FEAT_ONESHOT
| CLOCK_EVT_FEAT_DYNIRQ
;
267 dw_ced
->ced
.set_state_shutdown
= apbt_shutdown
;
268 dw_ced
->ced
.set_state_periodic
= apbt_set_periodic
;
269 dw_ced
->ced
.set_state_oneshot
= apbt_set_oneshot
;
270 dw_ced
->ced
.set_state_oneshot_stopped
= apbt_shutdown
;
271 dw_ced
->ced
.tick_resume
= apbt_resume
;
272 dw_ced
->ced
.set_next_event
= apbt_next_event
;
273 dw_ced
->ced
.irq
= dw_ced
->timer
.irq
;
274 dw_ced
->ced
.rating
= rating
;
275 dw_ced
->ced
.name
= name
;
277 dw_ced
->irqaction
.name
= dw_ced
->ced
.name
;
278 dw_ced
->irqaction
.handler
= dw_apb_clockevent_irq
;
279 dw_ced
->irqaction
.dev_id
= &dw_ced
->ced
;
280 dw_ced
->irqaction
.irq
= irq
;
281 dw_ced
->irqaction
.flags
= IRQF_TIMER
| IRQF_IRQPOLL
|
284 dw_ced
->eoi
= apbt_eoi
;
285 err
= setup_irq(irq
, &dw_ced
->irqaction
);
287 pr_err("failed to request timer irq\n");
296 * dw_apb_clockevent_resume() - resume a clock that has been paused.
298 * @dw_ced: The APB clock to resume.
300 void dw_apb_clockevent_resume(struct dw_apb_clock_event_device
*dw_ced
)
302 enable_irq(dw_ced
->timer
.irq
);
306 * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
308 * @dw_ced: The APB clock to stop generating the events.
310 void dw_apb_clockevent_stop(struct dw_apb_clock_event_device
*dw_ced
)
312 free_irq(dw_ced
->timer
.irq
, &dw_ced
->ced
);
316 * dw_apb_clockevent_register() - register the clock with the generic layer
318 * @dw_ced: The APB clock to register as a clock_event_device.
320 void dw_apb_clockevent_register(struct dw_apb_clock_event_device
*dw_ced
)
322 apbt_writel(&dw_ced
->timer
, 0, APBTMR_N_CONTROL
);
323 clockevents_register_device(&dw_ced
->ced
);
324 apbt_enable_int(&dw_ced
->timer
);
328 * dw_apb_clocksource_start() - start the clocksource counting.
330 * @dw_cs: The clocksource to start.
332 * This is used to start the clocksource before registration and can be used
333 * to enable calibration of timers.
335 void dw_apb_clocksource_start(struct dw_apb_clocksource
*dw_cs
)
338 * start count down from 0xffff_ffff. this is done by toggling the
339 * enable bit then load initial load count to ~0.
341 u32 ctrl
= apbt_readl(&dw_cs
->timer
, APBTMR_N_CONTROL
);
343 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
344 apbt_writel(&dw_cs
->timer
, ctrl
, APBTMR_N_CONTROL
);
345 apbt_writel(&dw_cs
->timer
, ~0, APBTMR_N_LOAD_COUNT
);
346 /* enable, mask interrupt */
347 ctrl
&= ~APBTMR_CONTROL_MODE_PERIODIC
;
348 ctrl
|= (APBTMR_CONTROL_ENABLE
| APBTMR_CONTROL_INT
);
349 apbt_writel(&dw_cs
->timer
, ctrl
, APBTMR_N_CONTROL
);
350 /* read it once to get cached counter value initialized */
351 dw_apb_clocksource_read(dw_cs
);
354 static u64
__apbt_read_clocksource(struct clocksource
*cs
)
357 struct dw_apb_clocksource
*dw_cs
=
358 clocksource_to_dw_apb_clocksource(cs
);
360 current_count
= apbt_readl_relaxed(&dw_cs
->timer
,
361 APBTMR_N_CURRENT_VALUE
);
363 return (u64
)~current_count
;
366 static void apbt_restart_clocksource(struct clocksource
*cs
)
368 struct dw_apb_clocksource
*dw_cs
=
369 clocksource_to_dw_apb_clocksource(cs
);
371 dw_apb_clocksource_start(dw_cs
);
375 * dw_apb_clocksource_init() - use an APB timer as a clocksource.
377 * @rating: The rating to give the clocksource.
378 * @name: The name for the clocksource.
379 * @base: The I/O base for the timer registers.
380 * @freq: The frequency that the timer counts at.
382 * This creates a clocksource using an APB timer but does not yet register it
383 * with the clocksource system. This should be done with
384 * dw_apb_clocksource_register() as the next step.
386 struct dw_apb_clocksource
*
387 dw_apb_clocksource_init(unsigned rating
, const char *name
, void __iomem
*base
,
390 struct dw_apb_clocksource
*dw_cs
= kzalloc(sizeof(*dw_cs
), GFP_KERNEL
);
395 dw_cs
->timer
.base
= base
;
396 dw_cs
->timer
.freq
= freq
;
397 dw_cs
->cs
.name
= name
;
398 dw_cs
->cs
.rating
= rating
;
399 dw_cs
->cs
.read
= __apbt_read_clocksource
;
400 dw_cs
->cs
.mask
= CLOCKSOURCE_MASK(32);
401 dw_cs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
402 dw_cs
->cs
.resume
= apbt_restart_clocksource
;
408 * dw_apb_clocksource_register() - register the APB clocksource.
410 * @dw_cs: The clocksource to register.
412 void dw_apb_clocksource_register(struct dw_apb_clocksource
*dw_cs
)
414 clocksource_register_hz(&dw_cs
->cs
, dw_cs
->timer
.freq
);
418 * dw_apb_clocksource_read() - read the current value of a clocksource.
420 * @dw_cs: The clocksource to read.
422 u64
dw_apb_clocksource_read(struct dw_apb_clocksource
*dw_cs
)
424 return (u64
)~apbt_readl(&dw_cs
->timer
, APBTMR_N_CURRENT_VALUE
);