iomd_clock.c revision 1.2.4.3 1 1.2.4.3 nathanw /* $NetBSD: iomd_clock.c,v 1.2.4.3 2002/02/28 04:07:36 nathanw Exp $ */
2 1.2.4.2 nathanw
3 1.2.4.2 nathanw /*
4 1.2.4.2 nathanw * Copyright (c) 1994-1997 Mark Brinicombe.
5 1.2.4.2 nathanw * Copyright (c) 1994 Brini.
6 1.2.4.2 nathanw * All rights reserved.
7 1.2.4.2 nathanw *
8 1.2.4.2 nathanw * This code is derived from software written for Brini by Mark Brinicombe
9 1.2.4.2 nathanw *
10 1.2.4.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 nathanw * modification, are permitted provided that the following conditions
12 1.2.4.2 nathanw * are met:
13 1.2.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.2.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.2.4.2 nathanw * must display the following acknowledgement:
20 1.2.4.2 nathanw * This product includes software developed by Mark Brinicombe.
21 1.2.4.2 nathanw * 4. The name of the company nor the name of the author may be used to
22 1.2.4.2 nathanw * endorse or promote products derived from this software without specific
23 1.2.4.2 nathanw * prior written permission.
24 1.2.4.2 nathanw *
25 1.2.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 1.2.4.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 1.2.4.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.2.4.2 nathanw * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 1.2.4.2 nathanw * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 1.2.4.2 nathanw * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 1.2.4.2 nathanw * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.2.4.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.2.4.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.2.4.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.2.4.2 nathanw * SUCH DAMAGE.
36 1.2.4.2 nathanw *
37 1.2.4.2 nathanw * RiscBSD kernel project
38 1.2.4.2 nathanw *
39 1.2.4.2 nathanw * clock.c
40 1.2.4.2 nathanw *
41 1.2.4.2 nathanw * Timer related machine specific code
42 1.2.4.2 nathanw *
43 1.2.4.2 nathanw * Created : 29/09/94
44 1.2.4.2 nathanw */
45 1.2.4.2 nathanw
46 1.2.4.2 nathanw /* Include header files */
47 1.2.4.2 nathanw
48 1.2.4.2 nathanw #include <sys/param.h>
49 1.2.4.3 nathanw
50 1.2.4.3 nathanw __RCSID("$NetBSD");
51 1.2.4.3 nathanw
52 1.2.4.2 nathanw #include <sys/systm.h>
53 1.2.4.2 nathanw #include <sys/kernel.h>
54 1.2.4.2 nathanw #include <sys/time.h>
55 1.2.4.2 nathanw #include <sys/device.h>
56 1.2.4.2 nathanw
57 1.2.4.2 nathanw #include <machine/intr.h>
58 1.2.4.2 nathanw
59 1.2.4.2 nathanw #include <arm/cpufunc.h>
60 1.2.4.2 nathanw
61 1.2.4.2 nathanw #include <arm/iomd/iomdvar.h>
62 1.2.4.2 nathanw #include <arm/iomd/iomdreg.h>
63 1.2.4.2 nathanw
64 1.2.4.2 nathanw struct clock_softc {
65 1.2.4.2 nathanw struct device sc_dev;
66 1.2.4.2 nathanw bus_space_tag_t sc_iot;
67 1.2.4.2 nathanw bus_space_handle_t sc_ioh;
68 1.2.4.2 nathanw };
69 1.2.4.2 nathanw
70 1.2.4.2 nathanw #define TIMER_FREQUENCY 2000000 /* 2MHz clock */
71 1.2.4.2 nathanw #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY / 1000000)
72 1.2.4.2 nathanw
73 1.2.4.2 nathanw static void *clockirq;
74 1.2.4.2 nathanw static void *statclockirq;
75 1.2.4.2 nathanw static struct clock_softc *clock_sc;
76 1.2.4.2 nathanw static int timer0_count;
77 1.2.4.2 nathanw
78 1.2.4.2 nathanw static int clockmatch __P((struct device *parent, struct cfdata *cf, void *aux));
79 1.2.4.2 nathanw static void clockattach __P((struct device *parent, struct device *self, void *aux));
80 1.2.4.2 nathanw #ifdef DIAGNOSTIC
81 1.2.4.2 nathanw static void checkdelay __P((void));
82 1.2.4.2 nathanw #endif
83 1.2.4.2 nathanw
84 1.2.4.3 nathanw int clockhandler __P((void *));
85 1.2.4.3 nathanw int statclockhandler __P((void *));
86 1.2.4.3 nathanw
87 1.2.4.2 nathanw struct cfattach clock_ca = {
88 1.2.4.2 nathanw sizeof(struct clock_softc), clockmatch, clockattach
89 1.2.4.2 nathanw };
90 1.2.4.2 nathanw
91 1.2.4.2 nathanw /*
92 1.2.4.2 nathanw * int clockmatch(struct device *parent, void *match, void *aux)
93 1.2.4.2 nathanw *
94 1.2.4.2 nathanw * Just return ok for this if it is device 0
95 1.2.4.2 nathanw */
96 1.2.4.2 nathanw
97 1.2.4.2 nathanw static int
98 1.2.4.2 nathanw clockmatch(parent, cf, aux)
99 1.2.4.2 nathanw struct device *parent;
100 1.2.4.2 nathanw struct cfdata *cf;
101 1.2.4.2 nathanw void *aux;
102 1.2.4.2 nathanw {
103 1.2.4.2 nathanw struct clk_attach_args *ca = aux;
104 1.2.4.2 nathanw
105 1.2.4.2 nathanw if (strcmp(ca->ca_name, "clk") == 0)
106 1.2.4.2 nathanw return(1);
107 1.2.4.2 nathanw return(0);
108 1.2.4.2 nathanw }
109 1.2.4.2 nathanw
110 1.2.4.2 nathanw
111 1.2.4.2 nathanw /*
112 1.2.4.2 nathanw * void clockattach(struct device *parent, struct device *dev, void *aux)
113 1.2.4.2 nathanw *
114 1.2.4.2 nathanw * Map the IOMD and identify it.
115 1.2.4.2 nathanw * Then configure the child devices based on the IOMD ID.
116 1.2.4.2 nathanw */
117 1.2.4.2 nathanw
118 1.2.4.2 nathanw static void
119 1.2.4.2 nathanw clockattach(parent, self, aux)
120 1.2.4.2 nathanw struct device *parent;
121 1.2.4.2 nathanw struct device *self;
122 1.2.4.2 nathanw void *aux;
123 1.2.4.2 nathanw {
124 1.2.4.2 nathanw struct clock_softc *sc = (struct clock_softc *)self;
125 1.2.4.2 nathanw struct clk_attach_args *ca = aux;
126 1.2.4.2 nathanw
127 1.2.4.2 nathanw sc->sc_iot = ca->ca_iot;
128 1.2.4.2 nathanw sc->sc_ioh = ca->ca_ioh; /* This is a handle for the whole IOMD */
129 1.2.4.2 nathanw
130 1.2.4.2 nathanw clock_sc = sc;
131 1.2.4.2 nathanw
132 1.2.4.2 nathanw /* Cannot do anything until cpu_initclocks() has been called */
133 1.2.4.2 nathanw
134 1.2.4.2 nathanw printf("\n");
135 1.2.4.2 nathanw }
136 1.2.4.2 nathanw
137 1.2.4.2 nathanw
138 1.2.4.2 nathanw /*
139 1.2.4.2 nathanw * int clockhandler(struct clockframe *frame)
140 1.2.4.2 nathanw *
141 1.2.4.2 nathanw * Function called by timer 0 interrupts. This just calls
142 1.2.4.2 nathanw * hardclock(). Eventually the irqhandler can call hardclock() directly
143 1.2.4.2 nathanw * but for now we use this function so that we can debug IRQ's
144 1.2.4.2 nathanw */
145 1.2.4.2 nathanw
146 1.2.4.2 nathanw int
147 1.2.4.3 nathanw clockhandler(cookie)
148 1.2.4.3 nathanw void *cookie;
149 1.2.4.2 nathanw {
150 1.2.4.3 nathanw struct clockframe *frame = cookie;
151 1.2.4.3 nathanw
152 1.2.4.2 nathanw hardclock(frame);
153 1.2.4.2 nathanw return(0); /* Pass the interrupt on down the chain */
154 1.2.4.2 nathanw }
155 1.2.4.2 nathanw
156 1.2.4.2 nathanw
157 1.2.4.2 nathanw /*
158 1.2.4.2 nathanw * int statclockhandler(struct clockframe *frame)
159 1.2.4.2 nathanw *
160 1.2.4.2 nathanw * Function called by timer 1 interrupts. This just calls
161 1.2.4.2 nathanw * statclock(). Eventually the irqhandler can call statclock() directly
162 1.2.4.2 nathanw * but for now we use this function so that we can debug IRQ's
163 1.2.4.2 nathanw */
164 1.2.4.2 nathanw
165 1.2.4.2 nathanw int
166 1.2.4.3 nathanw statclockhandler(cookie)
167 1.2.4.3 nathanw void *cookie;
168 1.2.4.2 nathanw {
169 1.2.4.3 nathanw struct clockframe *frame = cookie;
170 1.2.4.3 nathanw
171 1.2.4.2 nathanw statclock(frame);
172 1.2.4.2 nathanw return(0); /* Pass the interrupt on down the chain */
173 1.2.4.2 nathanw }
174 1.2.4.2 nathanw
175 1.2.4.2 nathanw
176 1.2.4.2 nathanw /*
177 1.2.4.2 nathanw * void setstatclockrate(int hz)
178 1.2.4.2 nathanw *
179 1.2.4.2 nathanw * Set the stat clock rate. The stat clock uses timer1
180 1.2.4.2 nathanw */
181 1.2.4.2 nathanw
182 1.2.4.2 nathanw void
183 1.2.4.2 nathanw setstatclockrate(hz)
184 1.2.4.2 nathanw int hz;
185 1.2.4.2 nathanw {
186 1.2.4.2 nathanw int count;
187 1.2.4.2 nathanw
188 1.2.4.2 nathanw count = TIMER_FREQUENCY / hz;
189 1.2.4.2 nathanw
190 1.2.4.2 nathanw printf("Setting statclock to %dHz (%d ticks)\n", hz, count);
191 1.2.4.2 nathanw
192 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
193 1.2.4.2 nathanw IOMD_T1LOW, (count >> 0) & 0xff);
194 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
195 1.2.4.2 nathanw IOMD_T1HIGH, (count >> 8) & 0xff);
196 1.2.4.2 nathanw
197 1.2.4.2 nathanw /* reload the counter */
198 1.2.4.2 nathanw
199 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
200 1.2.4.2 nathanw IOMD_T1GO, 0);
201 1.2.4.2 nathanw }
202 1.2.4.2 nathanw
203 1.2.4.2 nathanw
204 1.2.4.2 nathanw #ifdef DIAGNOSTIC
205 1.2.4.2 nathanw static void
206 1.2.4.2 nathanw checkdelay()
207 1.2.4.2 nathanw {
208 1.2.4.2 nathanw struct timeval start, end, diff;
209 1.2.4.2 nathanw
210 1.2.4.2 nathanw microtime(&start);
211 1.2.4.2 nathanw delay(10000);
212 1.2.4.2 nathanw microtime(&end);
213 1.2.4.2 nathanw timersub(&end, &start, &diff);
214 1.2.4.2 nathanw if (diff.tv_sec > 0)
215 1.2.4.2 nathanw return;
216 1.2.4.2 nathanw if (diff.tv_usec > 10000)
217 1.2.4.2 nathanw return;
218 1.2.4.2 nathanw printf("WARNING: delay(10000) took %ld us\n", diff.tv_usec);
219 1.2.4.2 nathanw }
220 1.2.4.2 nathanw #endif
221 1.2.4.2 nathanw
222 1.2.4.2 nathanw /*
223 1.2.4.2 nathanw * void cpu_initclocks(void)
224 1.2.4.2 nathanw *
225 1.2.4.2 nathanw * Initialise the clocks.
226 1.2.4.2 nathanw * This sets up the two timers in the IOMD and installs the IRQ handlers
227 1.2.4.2 nathanw *
228 1.2.4.2 nathanw * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
229 1.2.4.2 nathanw */
230 1.2.4.2 nathanw
231 1.2.4.2 nathanw void
232 1.2.4.2 nathanw cpu_initclocks()
233 1.2.4.2 nathanw {
234 1.2.4.2 nathanw /*
235 1.2.4.2 nathanw * Load timer 0 with count down value
236 1.2.4.2 nathanw * This timer generates 100Hz interrupts for the system clock
237 1.2.4.2 nathanw */
238 1.2.4.2 nathanw
239 1.2.4.2 nathanw printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
240 1.2.4.2 nathanw
241 1.2.4.2 nathanw timer0_count = TIMER_FREQUENCY / hz;
242 1.2.4.2 nathanw
243 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
244 1.2.4.2 nathanw IOMD_T0LOW, (timer0_count >> 0) & 0xff);
245 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
246 1.2.4.2 nathanw IOMD_T0HIGH, (timer0_count >> 8) & 0xff);
247 1.2.4.2 nathanw
248 1.2.4.2 nathanw /* reload the counter */
249 1.2.4.2 nathanw
250 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
251 1.2.4.2 nathanw IOMD_T0GO, 0);
252 1.2.4.2 nathanw
253 1.2.4.2 nathanw clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
254 1.2.4.2 nathanw clockhandler, 0);
255 1.2.4.2 nathanw
256 1.2.4.2 nathanw if (clockirq == NULL)
257 1.2.4.2 nathanw panic("%s: Cannot installer timer 0 IRQ handler\n",
258 1.2.4.2 nathanw clock_sc->sc_dev.dv_xname);
259 1.2.4.2 nathanw
260 1.2.4.2 nathanw if (stathz) {
261 1.2.4.2 nathanw setstatclockrate(stathz);
262 1.2.4.2 nathanw statclockirq = intr_claim(IRQ_TIMER1, IPL_CLOCK,
263 1.2.4.2 nathanw "tmr1 stat clk", statclockhandler, 0);
264 1.2.4.2 nathanw if (statclockirq == NULL)
265 1.2.4.2 nathanw panic("%s: Cannot installer timer 1 IRQ handler\n",
266 1.2.4.2 nathanw clock_sc->sc_dev.dv_xname);
267 1.2.4.2 nathanw }
268 1.2.4.2 nathanw #ifdef DIAGNOSTIC
269 1.2.4.2 nathanw checkdelay();
270 1.2.4.2 nathanw #endif
271 1.2.4.2 nathanw }
272 1.2.4.2 nathanw
273 1.2.4.2 nathanw
274 1.2.4.2 nathanw /*
275 1.2.4.2 nathanw * void microtime(struct timeval *tvp)
276 1.2.4.2 nathanw *
277 1.2.4.2 nathanw * Fill in the specified timeval struct with the current time
278 1.2.4.2 nathanw * accurate to the microsecond.
279 1.2.4.2 nathanw */
280 1.2.4.2 nathanw
281 1.2.4.2 nathanw void
282 1.2.4.2 nathanw microtime(tvp)
283 1.2.4.2 nathanw struct timeval *tvp;
284 1.2.4.2 nathanw {
285 1.2.4.2 nathanw int s;
286 1.2.4.2 nathanw int tm;
287 1.2.4.2 nathanw int deltatm;
288 1.2.4.2 nathanw static struct timeval oldtv;
289 1.2.4.2 nathanw
290 1.2.4.2 nathanw if (timer0_count == 0)
291 1.2.4.2 nathanw return;
292 1.2.4.2 nathanw
293 1.2.4.2 nathanw s = splhigh();
294 1.2.4.2 nathanw
295 1.2.4.2 nathanw /*
296 1.2.4.2 nathanw * Latch the current value of the timer and then read it.
297 1.2.4.2 nathanw * This garentees an atmoic reading of the time.
298 1.2.4.2 nathanw */
299 1.2.4.2 nathanw
300 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
301 1.2.4.2 nathanw IOMD_T0LATCH, 0);
302 1.2.4.2 nathanw
303 1.2.4.2 nathanw tm = bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
304 1.2.4.2 nathanw IOMD_T0LOW);
305 1.2.4.2 nathanw tm += (bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
306 1.2.4.2 nathanw IOMD_T0HIGH) << 8);
307 1.2.4.2 nathanw
308 1.2.4.2 nathanw deltatm = timer0_count - tm;
309 1.2.4.2 nathanw if (deltatm < 0)
310 1.2.4.2 nathanw printf("opps deltatm < 0 tm=%d deltatm=%d\n",
311 1.2.4.2 nathanw tm, deltatm);
312 1.2.4.2 nathanw
313 1.2.4.2 nathanw /* Fill in the timeval struct */
314 1.2.4.2 nathanw *tvp = time;
315 1.2.4.2 nathanw
316 1.2.4.2 nathanw tvp->tv_usec += (deltatm / TICKS_PER_MICROSECOND);
317 1.2.4.2 nathanw
318 1.2.4.2 nathanw /* Make sure the micro seconds don't overflow. */
319 1.2.4.2 nathanw while (tvp->tv_usec >= 1000000) {
320 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
321 1.2.4.2 nathanw ++tvp->tv_sec;
322 1.2.4.2 nathanw }
323 1.2.4.2 nathanw
324 1.2.4.2 nathanw /* Make sure the time has advanced. */
325 1.2.4.2 nathanw if (tvp->tv_sec == oldtv.tv_sec &&
326 1.2.4.2 nathanw tvp->tv_usec <= oldtv.tv_usec) {
327 1.2.4.2 nathanw tvp->tv_usec = oldtv.tv_usec + 1;
328 1.2.4.2 nathanw if (tvp->tv_usec >= 1000000) {
329 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
330 1.2.4.2 nathanw ++tvp->tv_sec;
331 1.2.4.2 nathanw }
332 1.2.4.2 nathanw }
333 1.2.4.2 nathanw
334 1.2.4.2 nathanw oldtv = *tvp;
335 1.2.4.2 nathanw (void)splx(s);
336 1.2.4.2 nathanw }
337 1.2.4.2 nathanw
338 1.2.4.2 nathanw /*
339 1.2.4.2 nathanw * Estimated loop for n microseconds
340 1.2.4.2 nathanw */
341 1.2.4.2 nathanw
342 1.2.4.2 nathanw /* Need to re-write this to use the timers */
343 1.2.4.2 nathanw
344 1.2.4.2 nathanw /* One day soon I will actually do this */
345 1.2.4.2 nathanw
346 1.2.4.2 nathanw int delaycount = 100;
347 1.2.4.2 nathanw
348 1.2.4.2 nathanw void
349 1.2.4.2 nathanw delay(n)
350 1.2.4.2 nathanw u_int n;
351 1.2.4.2 nathanw {
352 1.2.4.2 nathanw u_int i;
353 1.2.4.2 nathanw
354 1.2.4.2 nathanw if (n == 0) return;
355 1.2.4.2 nathanw while (--n > 0) {
356 1.2.4.2 nathanw if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */
357 1.2.4.2 nathanw for (i = delaycount; --i;);
358 1.2.4.2 nathanw else
359 1.2.4.2 nathanw for (i = 8; --i;);
360 1.2.4.2 nathanw }
361 1.2.4.2 nathanw }
362 1.2.4.2 nathanw
363 1.2.4.2 nathanw /* End of iomd_clock.c */
364