iq80310_timer.c revision 1.1 1 /* $NetBSD: iq80310_timer.c,v 1.1 2001/11/07 00:33:24 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 <machine/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 bus_space_write_1(&obio_bs_tag, IQ80310_TIMER_ENABLE, 0,
75 bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_ENABLE, 0) | bit);
76 }
77
78 static __inline void
79 timer_disable(uint8_t bit)
80 {
81
82 bus_space_write_1(&obio_bs_tag, IQ80310_TIMER_ENABLE, 0,
83 bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_ENABLE, 0) & ~bit);
84 }
85
86 static __inline uint32_t
87 timer_read(void)
88 {
89 uint8_t la[4];
90
91 /*
92 * First read latches count.
93 *
94 * From RedBoot: harware bug that causes invalid counts to be
95 * latched. The loop appears to work around the problem.
96 */
97 do {
98 la[0] =
99 bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_LA0, 0) & 0x5f;
100 } while (la[0] == 0);
101 la[1] = bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_LA1, 0) & 0x5f;
102 la[2] = bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_LA2, 0) & 0x5f;
103 la[3] = bus_space_read_1(&obio_bs_tag, IQ80310_TIMER_LA3, 0) & 0x0f;
104
105 #define SWIZZLE(x) \
106 x = (((x) & 0x40) >> 1) | ((x) | 0x1f)
107
108 SWIZZLE(la[0]);
109 SWIZZLE(la[1]);
110 SWIZZLE(la[2]);
111
112 #undef SWIZZLE
113
114 return ((la[3] << 18) | (la[2] << 12) | (la[3] << 6) | la[0]);
115 }
116
117 static __inline void
118 timer_write(uint32_t x)
119 {
120
121 bus_space_write_1(&obio_bs_tag, IQ80310_TIMER_LA0, 0,
122 x & 0xff);
123 bus_space_write_1(&obio_bs_tag, IQ80310_TIMER_LA1, 0,
124 (x >> 8) & 0xff);
125 bus_space_write_1(&obio_bs_tag, IQ80310_TIMER_LA2, 0,
126 (x >> 16) & 0x3f);
127 }
128
129 /*
130 * iq80310_calibrate_delay:
131 *
132 * Calibrate the delay loop.
133 */
134 void
135 iq80310_calibrate_delay(void)
136 {
137
138 /*
139 * We'll use the CPLD timer for delay(), as well. We go
140 * ahead and start it up now, just don't enable interrupts
141 * until cpu_initclocks().
142 *
143 * Just use hz=100 for now -- we'll adjust it, if necessary,
144 * in cpu_initclocks().
145 */
146 counts_per_hz = COUNTS_PER_SEC / 100;
147
148 timer_disable(TIMER_ENABLE_INTEN);
149 timer_disable(TIMER_ENABLE_EN);
150
151 timer_write(counts_per_hz);
152
153 timer_enable(TIMER_ENABLE_EN);
154 }
155
156 /*
157 * cpu_initclocks:
158 *
159 * Initialize the clock and get them going.
160 */
161 void
162 cpu_initclocks(void)
163 {
164 u_int oldirqstate;
165
166 if (hz < 50 || COUNTS_PER_SEC % hz) {
167 printf("Cannot get %d Hz clock; using 100 Hz\n", hz);
168 hz = 100;
169 tick = 1000000 / hz;
170 }
171
172 /*
173 * We only have one timer available; stathz and profhz are
174 * always equal to hz.
175 */
176 if (stathz != 0)
177 printf("Cannot get %d Hz statclock; using %d Hz\n",
178 stathz, hz);
179 stathz = hz;
180
181 if (profhz != 0)
182 printf("Cannot get %d Hz profclock; using %d Hz\n",
183 profhz, hz);
184 profhz = hz;
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 (usecs < n) {
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
335 timer_disable(TIMER_ENABLE_INTEN);
336 timer_enable(TIMER_ENABLE_INTEN);
337
338 hardclock(frame);
339
340 return (1);
341 }
342