1 /* $NetBSD: iq80310_timer.c,v 1.20 2007/02/19 02:08:12 briggs Exp $ */
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 IQ80310.
41 * The IQ80310 has a 22-bit reloadable timer implemented in the CPLD.
42 * We use it to provide a hardclock interrupt. There is no RTC on
45 * The timer uses the SPCI clock. The timer uses the 33MHz clock by
46 * reading the SPCI_66EN signal and dividing the clock if necessary.
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: iq80310_timer.c,v 1.20 2007/02/19 02:08:12 briggs Exp $");
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/atomic.h>
57 #include <sys/timetc.h>
59 #include <dev/clock_subr.h>
61 #include <machine/bus.h>
62 #include <arm/cpufunc.h>
64 #include <evbarm/iq80310/iq80310reg.h>
65 #include <evbarm/iq80310/iq80310var.h>
66 #include <evbarm/iq80310/obiovar.h>
69 * Some IQ80310-based designs have fewer bits in the timer counter.
70 * Deal with them here.
72 #if defined(IOP310_TEAMASA_NPWR)
73 #define COUNTER_MASK 0x0007ffff
74 #else /* Default to stock IQ80310 */
75 #define COUNTER_MASK 0x003fffff
76 #endif /* list of IQ80310-based designs */
78 #define COUNTS_PER_SEC 33000000 /* 33MHz */
79 #define COUNTS_PER_USEC (COUNTS_PER_SEC / 1000000)
81 static void *clock_ih
;
83 static uint32_t counts_per_hz
;
85 static u_int
iq80310_get_timecount(struct timecounter
*);
87 static struct timecounter iq80310_timecounter
= {
88 iq80310_get_timecount
, /* get_timecount */
90 0xffffffff, /* counter_mask */
91 COUNTS_PER_SEC
, /* frequency */
98 static volatile uint32_t iq80310_base
;
100 int clockhandler(void *);
103 timer_enable(uint8_t bit
)
106 CPLD_WRITE(IQ80310_TIMER_ENABLE
,
107 CPLD_READ(IQ80310_TIMER_ENABLE
) | bit
);
111 timer_disable(uint8_t bit
)
114 CPLD_WRITE(IQ80310_TIMER_ENABLE
,
115 CPLD_READ(IQ80310_TIMER_ENABLE
) & ~bit
);
118 static inline uint32_t
122 uint8_t la0
, la1
, la2
, la3
;
125 * First read latches count.
127 * From RedBoot: harware bug that causes invalid counts to be
128 * latched. The loop appears to work around the problem.
131 la0
= CPLD_READ(IQ80310_TIMER_LA0
);
133 la1
= CPLD_READ(IQ80310_TIMER_LA1
);
134 la2
= CPLD_READ(IQ80310_TIMER_LA2
);
135 la3
= CPLD_READ(IQ80310_TIMER_LA3
);
137 rv
= ((la0
& 0x40) >> 1) | (la0
& 0x1f);
138 rv
|= (((la1
& 0x40) >> 1) | (la1
& 0x1f)) << 6;
139 rv
|= (((la2
& 0x40) >> 1) | (la2
& 0x1f)) << 12;
140 rv
|= (la3
& 0x0f) << 18;
146 timer_write(uint32_t x
)
149 KASSERT((x
& COUNTER_MASK
) == x
);
151 CPLD_WRITE(IQ80310_TIMER_LA0
, x
& 0xff);
152 CPLD_WRITE(IQ80310_TIMER_LA1
, (x
>> 8) & 0xff);
153 CPLD_WRITE(IQ80310_TIMER_LA2
, (x
>> 16) & 0x3f);
157 * iq80310_calibrate_delay:
159 * Calibrate the delay loop.
162 iq80310_calibrate_delay(void)
166 * We'll use the CPLD timer for delay(), as well. We go
167 * ahead and start it up now, just don't enable interrupts
168 * until cpu_initclocks().
170 * Just use hz=100 for now -- we'll adjust it, if necessary,
171 * in cpu_initclocks().
173 counts_per_hz
= COUNTS_PER_SEC
/ 100;
175 timer_disable(TIMER_ENABLE_INTEN
);
176 timer_disable(TIMER_ENABLE_EN
);
178 timer_write(counts_per_hz
);
180 timer_enable(TIMER_ENABLE_EN
);
186 * Initialize the clock and get them going.
193 if (hz
< 50 || COUNTS_PER_SEC
% hz
) {
194 printf("Cannot get %d Hz clock; using 100 Hz\n", hz
);
199 * We only have one timer available; stathz and profhz are
200 * always left as 0 (the upper-layer clock code deals with
204 printf("Cannot get %d Hz statclock\n", stathz
);
208 printf("Cannot get %d Hz profclock\n", profhz
);
211 /* Report the clock frequency. */
212 printf("clock: hz=%d stathz=%d profhz=%d\n", hz
, stathz
, profhz
);
214 /* Hook up the clock interrupt handler. */
215 clock_ih
= iq80310_intr_establish(XINT3_IRQ(XINT3_TIMER
), IPL_CLOCK
,
217 if (clock_ih
== NULL
)
218 panic("cpu_initclocks: unable to register timer interrupt");
220 /* Set up the new clock parameters. */
221 oldirqstate
= disable_interrupts(I32_bit
);
223 timer_disable(TIMER_ENABLE_EN
);
225 counts_per_hz
= COUNTS_PER_SEC
/ hz
;
226 timer_write(counts_per_hz
);
228 timer_enable(TIMER_ENABLE_INTEN
);
229 timer_enable(TIMER_ENABLE_EN
);
231 restore_interrupts(oldirqstate
);
233 tc_init(&iq80310_timecounter
);
239 * Set the rate of the statistics clock.
241 * We assume that hz is either stathz or profhz, and that neither
242 * will change after being set by cpu_initclocks(). We could
243 * recalculate the intervals here, but that would be a pain.
246 setstatclockrate(int newhz
)
250 * Nothing to do, here; we can't change the statclock
251 * rate on the IQ80310.
256 iq80310_get_timecount(struct timecounter
*tc
)
258 u_int oldirqstate
, base
, counter
;
260 oldirqstate
= disable_interrupts(I32_bit
);
262 counter
= timer_read();
263 restore_interrupts(oldirqstate
);
265 return base
+ counter
;
271 * Delay for at least N microseconds.
276 uint32_t cur
, last
, delta
, usecs
;
279 * This works by polling the timer and counting the
280 * number of microseconds that go by.
288 /* Check to see if the timer has wrapped around. */
290 delta
+= ((counts_per_hz
- last
) + cur
);
292 delta
+= (cur
- last
);
296 if (delta
>= COUNTS_PER_USEC
) {
297 usecs
+= delta
/ COUNTS_PER_USEC
;
298 delta
%= COUNTS_PER_USEC
;
306 * Handle the hardclock interrupt.
309 clockhandler(void *arg
)
311 struct clockframe
*frame
= arg
;
313 timer_disable(TIMER_ENABLE_INTEN
);
314 timer_enable(TIMER_ENABLE_INTEN
);
316 atomic_add_32(&iq80310_base
, counts_per_hz
);
321 * Don't run the snake on IOP310-based systems that
322 * don't have the 7-segment display.
324 #if !defined(IOP310_TEAMASA_NPWR)
326 static int snakefreq
;
328 if ((snakefreq
++ & 15) == 0)
329 iq80310_7seg_snake();