iq80310_timer.c revision 1.6 1 /* $NetBSD: iq80310_timer.c,v 1.6 2001/12/01 02:04:27 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
23 * written permission.
24 *
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.
36 */
37
38 /*
39 * Timer/clock support for the Intel IQ80310.
40 *
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
43 * the IQ80310.
44 *
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.
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/time.h>
53
54 #include <machine/bus.h>
55 #include <arm/cpufunc.h>
56
57 #include <evbarm/iq80310/iq80310reg.h>
58 #include <evbarm/iq80310/iq80310var.h>
59 #include <evbarm/iq80310/obiovar.h>
60
61 #define COUNTS_PER_SEC 33000000 /* 33MHz */
62 #define COUNTS_PER_USEC (COUNTS_PER_SEC / 1000000)
63
64 static void *clock_ih;
65
66 static uint32_t counts_per_hz;
67
68 int clockhandler(void *);
69
70 static __inline void
71 timer_enable(uint8_t bit)
72 {
73
74 CPLD_WRITE(IQ80310_TIMER_ENABLE,
75 CPLD_READ(IQ80310_TIMER_ENABLE) | bit);
76 }
77
78 static __inline void
79 timer_disable(uint8_t bit)
80 {
81
82 CPLD_WRITE(IQ80310_TIMER_ENABLE,
83 CPLD_READ(IQ80310_TIMER_ENABLE) & ~bit);
84 }
85
86 static __inline uint32_t
87 timer_read(void)
88 {
89 uint32_t rv;
90 uint8_t la[4];
91
92 /*
93 * First read latches count.
94 *
95 * From RedBoot: harware bug that causes invalid counts to be
96 * latched. The loop appears to work around the problem.
97 */
98 do {
99 la[0] = CPLD_READ(IQ80310_TIMER_LA0) & 0x5f;
100 } while (la[0] == 0);
101 la[1] = CPLD_READ(IQ80310_TIMER_LA1) & 0x5f;
102 la[2] = CPLD_READ(IQ80310_TIMER_LA2) & 0x5f;
103 la[3] = CPLD_READ(IQ80310_TIMER_LA3) & 0x0f;
104
105 rv = ((la[0] & 0x40) >> 1) | (la[0] & 0x1f);
106 rv |= (((la[1] & 0x40) >> 1) | (la[1] & 0x1f)) << 6;
107 rv |= (((la[2] & 0x40) >> 1) | (la[2] & 0x1f)) << 12;
108 rv |= la[3] << 18;
109
110 return (rv);
111 }
112
113 static __inline void
114 timer_write(uint32_t x)
115 {
116
117 CPLD_WRITE(IQ80310_TIMER_LA0, x & 0xff);
118 CPLD_WRITE(IQ80310_TIMER_LA1, (x >> 8) & 0xff);
119 CPLD_WRITE(IQ80310_TIMER_LA2, (x >> 16) & 0x3f);
120 }
121
122 /*
123 * iq80310_calibrate_delay:
124 *
125 * Calibrate the delay loop.
126 */
127 void
128 iq80310_calibrate_delay(void)
129 {
130
131 /*
132 * We'll use the CPLD timer for delay(), as well. We go
133 * ahead and start it up now, just don't enable interrupts
134 * until cpu_initclocks().
135 *
136 * Just use hz=100 for now -- we'll adjust it, if necessary,
137 * in cpu_initclocks().
138 */
139 counts_per_hz = COUNTS_PER_SEC / 100;
140
141 timer_disable(TIMER_ENABLE_INTEN);
142 timer_disable(TIMER_ENABLE_EN);
143
144 timer_write(counts_per_hz);
145
146 timer_enable(TIMER_ENABLE_EN);
147 }
148
149 /*
150 * cpu_initclocks:
151 *
152 * Initialize the clock and get them going.
153 */
154 void
155 cpu_initclocks(void)
156 {
157 u_int oldirqstate;
158
159 if (hz < 50 || COUNTS_PER_SEC % hz) {
160 printf("Cannot get %d Hz clock; using 100 Hz\n", hz);
161 hz = 100;
162 }
163 tick = 1000000 / hz; /* number of microseconds between interrupts */
164 tickfix = 1000000 - (hz * tick);
165 if (tickfix) {
166 int ftp;
167
168 ftp = min(ffs(tickfix), ffs(hz));
169 tickfix >>= (ftp - 1);
170 tickfixinterval = hz >> (ftp - 1);
171 }
172
173 /*
174 * We only have one timer available; stathz and profhz are
175 * always left as 0 (the upper-layer clock code deals with
176 * this situation).
177 */
178 if (stathz != 0)
179 printf("Cannot get %d Hz statclock\n", stathz);
180 stathz = 0;
181
182 if (profhz != 0)
183 printf("Cannot get %d Hz profclock\n", profhz);
184 profhz = 0;
185
186 /* Report the clock frequency. */
187 printf("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
188
189 /* Hook up the clock interrupt handler. */
190 clock_ih = iq80310_intr_establish(XINT3_IRQ(XINT3_TIMER), IPL_CLOCK,
191 clockhandler, NULL);
192 if (clock_ih == NULL)
193 panic("cpu_initclocks: unable to register timer interrupt");
194
195 /* Set up the new clock parameters. */
196 oldirqstate = disable_interrupts(I32_bit);
197
198 timer_disable(TIMER_ENABLE_EN);
199
200 counts_per_hz = COUNTS_PER_SEC / hz;
201 timer_write(counts_per_hz);
202
203 timer_enable(TIMER_ENABLE_INTEN);
204 timer_enable(TIMER_ENABLE_EN);
205
206 restore_interrupts(oldirqstate);
207 }
208
209 /*
210 * setstatclockrate:
211 *
212 * Set the rate of the statistics clock.
213 *
214 * We assume that hz is either stathz or profhz, and that neither
215 * will change after being set by cpu_initclocks(). We could
216 * recalculate the intervals here, but that would be a pain.
217 */
218 void
219 setstatclockrate(int hz)
220 {
221
222 /*
223 * Nothing to do, here; we can't change the statclock
224 * rate on the IQ80310.
225 */
226 }
227
228 /*
229 * microtime:
230 *
231 * Fill in the specified timeval struct with the current time
232 * accurate to the microsecond.
233 */
234 void
235 microtime(struct timeval *tvp)
236 {
237 static struct timeval lasttv;
238 u_int oldirqstate;
239 uint32_t counts;
240
241 oldirqstate = disable_interrupts(I32_bit);
242
243 counts = timer_read();
244
245 /* Fill in the timeval struct. */
246 *tvp = time;
247 tvp->tv_usec += (counts / COUNTS_PER_USEC);
248
249 /* Make sure microseconds doesn't overflow. */
250 while (tvp->tv_usec >= 1000000) {
251 tvp->tv_usec -= 1000000;
252 tvp->tv_sec++;
253 }
254
255 /* Make sure the time has advanced. */
256 if (tvp->tv_sec == lasttv.tv_sec &&
257 tvp->tv_usec <= lasttv.tv_usec) {
258 tvp->tv_usec = lasttv.tv_usec + 1;
259 if (tvp->tv_usec >= 1000000) {
260 tvp->tv_usec -= 1000000;
261 tvp->tv_sec++;
262 }
263 }
264
265 lasttv = *tvp;
266
267 restore_interrupts(oldirqstate);
268 }
269
270 /*
271 * delay:
272 *
273 * Delay for at least N microseconds.
274 */
275 void
276 delay(u_int n)
277 {
278 uint32_t cur, last, delta, usecs;
279
280 /*
281 * This works by polling the timer and counting the
282 * number of microseconds that go by.
283 */
284 last = timer_read();
285 delta = usecs = 0;
286
287 while (n > usecs) {
288 cur = timer_read();
289
290 /* Check to see if the timer has wrapped around. */
291 if (cur < last)
292 delta += ((counts_per_hz - last) + cur);
293 else
294 delta += (cur - last);
295
296 last = cur;
297
298 if (delta >= COUNTS_PER_USEC) {
299 usecs += delta / COUNTS_PER_USEC;
300 delta %= COUNTS_PER_USEC;
301 }
302 }
303 }
304
305 /*
306 * inittodr:
307 *
308 * Initialize time from the time-of-day register.
309 */
310 void
311 inittodr(time_t base)
312 {
313 }
314
315 /*
316 * resettodr:
317 *
318 * Reset the time-of-day register with the current time.
319 */
320 void
321 resettodr(void)
322 {
323 }
324
325 /*
326 * clockhandler:
327 *
328 * Handle the hardclock interrupt.
329 */
330 int
331 clockhandler(void *arg)
332 {
333 struct clockframe *frame = arg;
334 static int snakefreq;
335
336 timer_disable(TIMER_ENABLE_INTEN);
337 timer_enable(TIMER_ENABLE_INTEN);
338
339 hardclock(frame);
340
341 if ((snakefreq++ & 15) == 0)
342 iq80310_7seg_snake();
343
344 return (1);
345 }
346