4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Timer/clock support for the Intel i80321 I/O processor.
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD$");
45 #include "opt_perfctrs.h"
46 #include "opt_i80321.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/timetc.h>
54 #include <dev/clock_subr.h>
56 #include <machine/bus.h>
57 #include <arm/cpufunc.h>
59 #include <arm/xscale/i80321reg.h>
60 #include <arm/xscale/i80321var.h>
62 #include <arm/xscale/xscalevar.h>
64 void (*i80321_hardclock_hook
)(void);
66 #ifndef COUNTS_PER_SEC
67 #define COUNTS_PER_SEC 200000000 /* 200MHz */
69 #define COUNTS_PER_USEC (COUNTS_PER_SEC / 1000000)
71 static void tmr1_tc_init(void);
73 static void *clock_ih
;
75 static uint32_t counts_per_hz
;
77 int clockhandler(void *);
79 static inline uint32_t
84 __asm
volatile("mrc p6, 0, %0, c0, c1, 0"
90 tmr0_write(uint32_t val
)
93 __asm
volatile("mcr p6, 0, %0, c0, c1, 0"
98 static inline uint32_t
103 __asm
volatile("mrc p6, 0, %0, c2, c1, 0"
109 tcr0_write(uint32_t val
)
112 __asm
volatile("mcr p6, 0, %0, c2, c1, 0"
118 trr0_write(uint32_t val
)
121 __asm
volatile("mcr p6, 0, %0, c4, c1, 0"
126 static inline uint32_t
131 __asm
volatile("mrc p6, 0, %0, c1, c1, 0"
137 tmr1_write(uint32_t val
)
140 __asm
volatile("mcr p6, 0, %0, c1, c1, 0"
145 static inline uint32_t
150 __asm
volatile("mrc p6, 0, %0, c3, c1, 0"
156 tcr1_write(uint32_t val
)
159 __asm
volatile("mcr p6, 0, %0, c3, c1, 0"
165 trr1_write(uint32_t val
)
168 __asm
volatile("mcr p6, 0, %0, c5, c1, 0"
174 tisr_write(uint32_t val
)
177 __asm
volatile("mcr p6, 0, %0, c6, c1, 0"
183 * i80321_calibrate_delay:
185 * Calibrate the delay loop.
188 i80321_calibrate_delay(void)
192 * Just use hz=100 for now -- we'll adjust it, if necessary,
193 * in cpu_initclocks().
195 counts_per_hz
= COUNTS_PER_SEC
/ 100;
197 tmr0_write(0); /* stop timer */
198 tisr_write(TISR_TMR0
); /* clear interrupt */
199 trr0_write(counts_per_hz
); /* reload value */
200 tcr0_write(counts_per_hz
); /* current value */
202 tmr0_write(TMRx_ENABLE
|TMRx_RELOAD
|TMRx_CSEL_CORE
);
208 * Initialize the clock and get them going.
214 #if defined(PERFCTRS)
218 if (hz
< 50 || COUNTS_PER_SEC
% hz
) {
219 aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz
);
224 * We only have one timer available; stathz and profhz are
225 * always left as 0 (the upper-layer clock code deals with
229 aprint_error("Cannot get %d Hz statclock\n", stathz
);
233 aprint_error("Cannot get %d Hz profclock\n", profhz
);
236 /* Report the clock frequency. */
237 aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz
, stathz
, profhz
);
239 oldirqstate
= disable_interrupts(I32_bit
);
241 /* Hook up the clock interrupt handler. */
242 clock_ih
= i80321_intr_establish(ICU_INT_TMR0
, IPL_CLOCK
,
244 if (clock_ih
== NULL
)
245 panic("cpu_initclocks: unable to register timer interrupt");
247 #if defined(PERFCTRS)
248 pmu_ih
= i80321_intr_establish(ICU_INT_PMU
, IPL_HIGH
,
249 xscale_pmc_dispatch
, NULL
);
251 panic("cpu_initclocks: unable to register timer interrupt");
254 /* Set up the new clock parameters. */
256 tmr0_write(0); /* stop timer */
257 tisr_write(TISR_TMR0
); /* clear interrupt */
259 counts_per_hz
= COUNTS_PER_SEC
/ hz
;
261 trr0_write(counts_per_hz
); /* reload value */
262 tcr0_write(counts_per_hz
); /* current value */
264 tmr0_write(TMRx_ENABLE
|TMRx_RELOAD
|TMRx_CSEL_CORE
);
266 restore_interrupts(oldirqstate
);
274 * Set the rate of the statistics clock.
276 * We assume that hz is either stathz or profhz, and that neither
277 * will change after being set by cpu_initclocks(). We could
278 * recalculate the intervals here, but that would be a pain.
281 setstatclockrate(int newhz
)
289 static inline uint32_t
290 tmr1_tc_get(struct timecounter
*tch
)
292 return (~tcr1_read());
298 static struct timecounter tmr1_tc
= {
299 .tc_get_timecount
= tmr1_tc_get
,
300 .tc_frequency
= COUNTS_PER_SEC
,
301 .tc_counter_mask
= ~0,
302 .tc_name
= "tmr1_count",
307 trr1_write(~0); /* reload value */
308 tcr1_write(~0); /* current value */
310 tmr1_write(TMRx_ENABLE
|TMRx_RELOAD
|TMRx_CSEL_CORE
);
320 * Delay for at least N microseconds.
325 uint32_t cur
, last
, delta
, usecs
;
328 * This works by polling the timer and counting the
329 * number of microseconds that go by.
337 /* Check to see if the timer has wrapped around. */
339 delta
+= (last
+ (counts_per_hz
- cur
));
341 delta
+= (last
- cur
);
345 if (delta
>= COUNTS_PER_USEC
) {
346 usecs
+= delta
/ COUNTS_PER_USEC
;
347 delta
%= COUNTS_PER_USEC
;
355 * Handle the hardclock interrupt.
358 clockhandler(void *arg
)
360 struct clockframe
*frame
= arg
;
362 tisr_write(TISR_TMR0
);
366 if (i80321_hardclock_hook
!= NULL
)
367 (*i80321_hardclock_hook
)();