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 unsigned long apbt_readl(struct dw_apb_timer
*timer
, unsigned long offs
)
54 return readl(timer
->base
+ offs
);
57 static void apbt_writel(struct dw_apb_timer
*timer
, unsigned long val
,
60 writel(val
, timer
->base
+ offs
);
63 static void apbt_disable_int(struct dw_apb_timer
*timer
)
65 unsigned long ctrl
= apbt_readl(timer
, APBTMR_N_CONTROL
);
67 ctrl
|= APBTMR_CONTROL_INT
;
68 apbt_writel(timer
, ctrl
, APBTMR_N_CONTROL
);
72 * dw_apb_clockevent_pause() - stop the clock_event_device from running
74 * @dw_ced: The APB clock to stop generating events.
76 void dw_apb_clockevent_pause(struct dw_apb_clock_event_device
*dw_ced
)
78 disable_irq(dw_ced
->timer
.irq
);
79 apbt_disable_int(&dw_ced
->timer
);
82 static void apbt_eoi(struct dw_apb_timer
*timer
)
84 apbt_readl(timer
, APBTMR_N_EOI
);
87 static irqreturn_t
dw_apb_clockevent_irq(int irq
, void *data
)
89 struct clock_event_device
*evt
= data
;
90 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
92 if (!evt
->event_handler
) {
93 pr_info("Spurious APBT timer interrupt %d", irq
);
98 dw_ced
->eoi(&dw_ced
->timer
);
100 evt
->event_handler(evt
);
104 static void apbt_enable_int(struct dw_apb_timer
*timer
)
106 unsigned long ctrl
= apbt_readl(timer
, APBTMR_N_CONTROL
);
107 /* clear pending intr */
108 apbt_readl(timer
, APBTMR_N_EOI
);
109 ctrl
&= ~APBTMR_CONTROL_INT
;
110 apbt_writel(timer
, ctrl
, APBTMR_N_CONTROL
);
113 static void apbt_set_mode(enum clock_event_mode mode
,
114 struct clock_event_device
*evt
)
117 unsigned long period
;
118 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
120 pr_debug("%s CPU %d mode=%d\n", __func__
,
121 cpumask_first(evt
->cpumask
),
125 case CLOCK_EVT_MODE_PERIODIC
:
126 period
= DIV_ROUND_UP(dw_ced
->timer
.freq
, HZ
);
127 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
128 ctrl
|= APBTMR_CONTROL_MODE_PERIODIC
;
129 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
131 * DW APB p. 46, have to disable timer before load counter,
132 * may cause sync problem.
134 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
135 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
137 pr_debug("Setting clock period %lu for HZ %d\n", period
, HZ
);
138 apbt_writel(&dw_ced
->timer
, period
, APBTMR_N_LOAD_COUNT
);
139 ctrl
|= APBTMR_CONTROL_ENABLE
;
140 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
143 case CLOCK_EVT_MODE_ONESHOT
:
144 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
146 * set free running mode, this mode will let timer reload max
147 * timeout which will give time (3min on 25MHz clock) to rearm
148 * the next event, therefore emulate the one-shot mode.
150 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
151 ctrl
&= ~APBTMR_CONTROL_MODE_PERIODIC
;
153 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
154 /* write again to set free running mode */
155 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
158 * DW APB p. 46, load counter with all 1s before starting free
161 apbt_writel(&dw_ced
->timer
, ~0, APBTMR_N_LOAD_COUNT
);
162 ctrl
&= ~APBTMR_CONTROL_INT
;
163 ctrl
|= APBTMR_CONTROL_ENABLE
;
164 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
167 case CLOCK_EVT_MODE_UNUSED
:
168 case CLOCK_EVT_MODE_SHUTDOWN
:
169 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
170 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
171 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
174 case CLOCK_EVT_MODE_RESUME
:
175 apbt_enable_int(&dw_ced
->timer
);
180 static int apbt_next_event(unsigned long delta
,
181 struct clock_event_device
*evt
)
184 struct dw_apb_clock_event_device
*dw_ced
= ced_to_dw_apb_ced(evt
);
187 ctrl
= apbt_readl(&dw_ced
->timer
, APBTMR_N_CONTROL
);
188 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
189 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
190 /* write new count */
191 apbt_writel(&dw_ced
->timer
, delta
, APBTMR_N_LOAD_COUNT
);
192 ctrl
|= APBTMR_CONTROL_ENABLE
;
193 apbt_writel(&dw_ced
->timer
, ctrl
, APBTMR_N_CONTROL
);
199 * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
201 * @cpu: The CPU the events will be targeted at.
202 * @name: The name used for the timer and the IRQ for it.
203 * @rating: The rating to give the timer.
204 * @base: I/O base for the timer registers.
205 * @irq: The interrupt number to use for the timer.
206 * @freq: The frequency that the timer counts at.
208 * This creates a clock_event_device for using with the generic clock layer
209 * but does not start and register it. This should be done with
210 * dw_apb_clockevent_register() as the next step. If this is the first time
211 * it has been called for a timer then the IRQ will be requested, if not it
212 * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
215 struct dw_apb_clock_event_device
*
216 dw_apb_clockevent_init(int cpu
, const char *name
, unsigned rating
,
217 void __iomem
*base
, int irq
, unsigned long freq
)
219 struct dw_apb_clock_event_device
*dw_ced
=
220 kzalloc(sizeof(*dw_ced
), GFP_KERNEL
);
226 dw_ced
->timer
.base
= base
;
227 dw_ced
->timer
.irq
= irq
;
228 dw_ced
->timer
.freq
= freq
;
230 clockevents_calc_mult_shift(&dw_ced
->ced
, freq
, APBT_MIN_PERIOD
);
231 dw_ced
->ced
.max_delta_ns
= clockevent_delta2ns(0x7fffffff,
233 dw_ced
->ced
.min_delta_ns
= clockevent_delta2ns(5000, &dw_ced
->ced
);
234 dw_ced
->ced
.cpumask
= cpumask_of(cpu
);
235 dw_ced
->ced
.features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
236 dw_ced
->ced
.set_mode
= apbt_set_mode
;
237 dw_ced
->ced
.set_next_event
= apbt_next_event
;
238 dw_ced
->ced
.irq
= dw_ced
->timer
.irq
;
239 dw_ced
->ced
.rating
= rating
;
240 dw_ced
->ced
.name
= name
;
242 dw_ced
->irqaction
.name
= dw_ced
->ced
.name
;
243 dw_ced
->irqaction
.handler
= dw_apb_clockevent_irq
;
244 dw_ced
->irqaction
.dev_id
= &dw_ced
->ced
;
245 dw_ced
->irqaction
.irq
= irq
;
246 dw_ced
->irqaction
.flags
= IRQF_TIMER
| IRQF_IRQPOLL
|
249 dw_ced
->eoi
= apbt_eoi
;
250 err
= setup_irq(irq
, &dw_ced
->irqaction
);
252 pr_err("failed to request timer irq\n");
261 * dw_apb_clockevent_resume() - resume a clock that has been paused.
263 * @dw_ced: The APB clock to resume.
265 void dw_apb_clockevent_resume(struct dw_apb_clock_event_device
*dw_ced
)
267 enable_irq(dw_ced
->timer
.irq
);
271 * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
273 * @dw_ced: The APB clock to stop generating the events.
275 void dw_apb_clockevent_stop(struct dw_apb_clock_event_device
*dw_ced
)
277 free_irq(dw_ced
->timer
.irq
, &dw_ced
->ced
);
281 * dw_apb_clockevent_register() - register the clock with the generic layer
283 * @dw_ced: The APB clock to register as a clock_event_device.
285 void dw_apb_clockevent_register(struct dw_apb_clock_event_device
*dw_ced
)
287 apbt_writel(&dw_ced
->timer
, 0, APBTMR_N_CONTROL
);
288 clockevents_register_device(&dw_ced
->ced
);
289 apbt_enable_int(&dw_ced
->timer
);
293 * dw_apb_clocksource_start() - start the clocksource counting.
295 * @dw_cs: The clocksource to start.
297 * This is used to start the clocksource before registration and can be used
298 * to enable calibration of timers.
300 void dw_apb_clocksource_start(struct dw_apb_clocksource
*dw_cs
)
303 * start count down from 0xffff_ffff. this is done by toggling the
304 * enable bit then load initial load count to ~0.
306 unsigned long ctrl
= apbt_readl(&dw_cs
->timer
, APBTMR_N_CONTROL
);
308 ctrl
&= ~APBTMR_CONTROL_ENABLE
;
309 apbt_writel(&dw_cs
->timer
, ctrl
, APBTMR_N_CONTROL
);
310 apbt_writel(&dw_cs
->timer
, ~0, APBTMR_N_LOAD_COUNT
);
311 /* enable, mask interrupt */
312 ctrl
&= ~APBTMR_CONTROL_MODE_PERIODIC
;
313 ctrl
|= (APBTMR_CONTROL_ENABLE
| APBTMR_CONTROL_INT
);
314 apbt_writel(&dw_cs
->timer
, ctrl
, APBTMR_N_CONTROL
);
315 /* read it once to get cached counter value initialized */
316 dw_apb_clocksource_read(dw_cs
);
319 static cycle_t
__apbt_read_clocksource(struct clocksource
*cs
)
321 unsigned long current_count
;
322 struct dw_apb_clocksource
*dw_cs
=
323 clocksource_to_dw_apb_clocksource(cs
);
325 current_count
= apbt_readl(&dw_cs
->timer
, APBTMR_N_CURRENT_VALUE
);
327 return (cycle_t
)~current_count
;
330 static void apbt_restart_clocksource(struct clocksource
*cs
)
332 struct dw_apb_clocksource
*dw_cs
=
333 clocksource_to_dw_apb_clocksource(cs
);
335 dw_apb_clocksource_start(dw_cs
);
339 * dw_apb_clocksource_init() - use an APB timer as a clocksource.
341 * @rating: The rating to give the clocksource.
342 * @name: The name for the clocksource.
343 * @base: The I/O base for the timer registers.
344 * @freq: The frequency that the timer counts at.
346 * This creates a clocksource using an APB timer but does not yet register it
347 * with the clocksource system. This should be done with
348 * dw_apb_clocksource_register() as the next step.
350 struct dw_apb_clocksource
*
351 dw_apb_clocksource_init(unsigned rating
, const char *name
, void __iomem
*base
,
354 struct dw_apb_clocksource
*dw_cs
= kzalloc(sizeof(*dw_cs
), GFP_KERNEL
);
359 dw_cs
->timer
.base
= base
;
360 dw_cs
->timer
.freq
= freq
;
361 dw_cs
->cs
.name
= name
;
362 dw_cs
->cs
.rating
= rating
;
363 dw_cs
->cs
.read
= __apbt_read_clocksource
;
364 dw_cs
->cs
.mask
= CLOCKSOURCE_MASK(32);
365 dw_cs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
366 dw_cs
->cs
.resume
= apbt_restart_clocksource
;
372 * dw_apb_clocksource_register() - register the APB clocksource.
374 * @dw_cs: The clocksource to register.
376 void dw_apb_clocksource_register(struct dw_apb_clocksource
*dw_cs
)
378 clocksource_register_hz(&dw_cs
->cs
, dw_cs
->timer
.freq
);
382 * dw_apb_clocksource_read() - read the current value of a clocksource.
384 * @dw_cs: The clocksource to read.
386 cycle_t
dw_apb_clocksource_read(struct dw_apb_clocksource
*dw_cs
)
388 return (cycle_t
)~apbt_readl(&dw_cs
->timer
, APBTMR_N_CURRENT_VALUE
);