iomd_clock.c revision 1.2.4.2 1 1.2.4.2 nathanw /* $NetBSD: iomd_clock.c,v 1.2.4.2 2002/01/08 00:23:15 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/types.h>
49 1.2.4.2 nathanw #include <sys/param.h>
50 1.2.4.2 nathanw #include <sys/systm.h>
51 1.2.4.2 nathanw #include <sys/kernel.h>
52 1.2.4.2 nathanw #include <sys/time.h>
53 1.2.4.2 nathanw #include <sys/device.h>
54 1.2.4.2 nathanw
55 1.2.4.2 nathanw #include <machine/intr.h>
56 1.2.4.2 nathanw
57 1.2.4.2 nathanw #include <arm/cpufunc.h>
58 1.2.4.2 nathanw
59 1.2.4.2 nathanw #include <arm/iomd/iomdvar.h>
60 1.2.4.2 nathanw #include <arm/iomd/iomdreg.h>
61 1.2.4.2 nathanw
62 1.2.4.2 nathanw struct clock_softc {
63 1.2.4.2 nathanw struct device sc_dev;
64 1.2.4.2 nathanw bus_space_tag_t sc_iot;
65 1.2.4.2 nathanw bus_space_handle_t sc_ioh;
66 1.2.4.2 nathanw };
67 1.2.4.2 nathanw
68 1.2.4.2 nathanw #define TIMER_FREQUENCY 2000000 /* 2MHz clock */
69 1.2.4.2 nathanw #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY / 1000000)
70 1.2.4.2 nathanw
71 1.2.4.2 nathanw static void *clockirq;
72 1.2.4.2 nathanw static void *statclockirq;
73 1.2.4.2 nathanw static struct clock_softc *clock_sc;
74 1.2.4.2 nathanw static int timer0_count;
75 1.2.4.2 nathanw
76 1.2.4.2 nathanw static int clockmatch __P((struct device *parent, struct cfdata *cf, void *aux));
77 1.2.4.2 nathanw static void clockattach __P((struct device *parent, struct device *self, void *aux));
78 1.2.4.2 nathanw #ifdef DIAGNOSTIC
79 1.2.4.2 nathanw static void checkdelay __P((void));
80 1.2.4.2 nathanw #endif
81 1.2.4.2 nathanw
82 1.2.4.2 nathanw struct cfattach clock_ca = {
83 1.2.4.2 nathanw sizeof(struct clock_softc), clockmatch, clockattach
84 1.2.4.2 nathanw };
85 1.2.4.2 nathanw
86 1.2.4.2 nathanw /*
87 1.2.4.2 nathanw * int clockmatch(struct device *parent, void *match, void *aux)
88 1.2.4.2 nathanw *
89 1.2.4.2 nathanw * Just return ok for this if it is device 0
90 1.2.4.2 nathanw */
91 1.2.4.2 nathanw
92 1.2.4.2 nathanw static int
93 1.2.4.2 nathanw clockmatch(parent, cf, aux)
94 1.2.4.2 nathanw struct device *parent;
95 1.2.4.2 nathanw struct cfdata *cf;
96 1.2.4.2 nathanw void *aux;
97 1.2.4.2 nathanw {
98 1.2.4.2 nathanw struct clk_attach_args *ca = aux;
99 1.2.4.2 nathanw
100 1.2.4.2 nathanw if (strcmp(ca->ca_name, "clk") == 0)
101 1.2.4.2 nathanw return(1);
102 1.2.4.2 nathanw return(0);
103 1.2.4.2 nathanw }
104 1.2.4.2 nathanw
105 1.2.4.2 nathanw
106 1.2.4.2 nathanw /*
107 1.2.4.2 nathanw * void clockattach(struct device *parent, struct device *dev, void *aux)
108 1.2.4.2 nathanw *
109 1.2.4.2 nathanw * Map the IOMD and identify it.
110 1.2.4.2 nathanw * Then configure the child devices based on the IOMD ID.
111 1.2.4.2 nathanw */
112 1.2.4.2 nathanw
113 1.2.4.2 nathanw static void
114 1.2.4.2 nathanw clockattach(parent, self, aux)
115 1.2.4.2 nathanw struct device *parent;
116 1.2.4.2 nathanw struct device *self;
117 1.2.4.2 nathanw void *aux;
118 1.2.4.2 nathanw {
119 1.2.4.2 nathanw struct clock_softc *sc = (struct clock_softc *)self;
120 1.2.4.2 nathanw struct clk_attach_args *ca = aux;
121 1.2.4.2 nathanw
122 1.2.4.2 nathanw sc->sc_iot = ca->ca_iot;
123 1.2.4.2 nathanw sc->sc_ioh = ca->ca_ioh; /* This is a handle for the whole IOMD */
124 1.2.4.2 nathanw
125 1.2.4.2 nathanw clock_sc = sc;
126 1.2.4.2 nathanw
127 1.2.4.2 nathanw /* Cannot do anything until cpu_initclocks() has been called */
128 1.2.4.2 nathanw
129 1.2.4.2 nathanw printf("\n");
130 1.2.4.2 nathanw }
131 1.2.4.2 nathanw
132 1.2.4.2 nathanw
133 1.2.4.2 nathanw /*
134 1.2.4.2 nathanw * int clockhandler(struct clockframe *frame)
135 1.2.4.2 nathanw *
136 1.2.4.2 nathanw * Function called by timer 0 interrupts. This just calls
137 1.2.4.2 nathanw * hardclock(). Eventually the irqhandler can call hardclock() directly
138 1.2.4.2 nathanw * but for now we use this function so that we can debug IRQ's
139 1.2.4.2 nathanw */
140 1.2.4.2 nathanw
141 1.2.4.2 nathanw int
142 1.2.4.2 nathanw clockhandler(frame)
143 1.2.4.2 nathanw struct clockframe *frame;
144 1.2.4.2 nathanw {
145 1.2.4.2 nathanw hardclock(frame);
146 1.2.4.2 nathanw return(0); /* Pass the interrupt on down the chain */
147 1.2.4.2 nathanw }
148 1.2.4.2 nathanw
149 1.2.4.2 nathanw
150 1.2.4.2 nathanw /*
151 1.2.4.2 nathanw * int statclockhandler(struct clockframe *frame)
152 1.2.4.2 nathanw *
153 1.2.4.2 nathanw * Function called by timer 1 interrupts. This just calls
154 1.2.4.2 nathanw * statclock(). Eventually the irqhandler can call statclock() directly
155 1.2.4.2 nathanw * but for now we use this function so that we can debug IRQ's
156 1.2.4.2 nathanw */
157 1.2.4.2 nathanw
158 1.2.4.2 nathanw int
159 1.2.4.2 nathanw statclockhandler(frame)
160 1.2.4.2 nathanw struct clockframe *frame;
161 1.2.4.2 nathanw {
162 1.2.4.2 nathanw statclock(frame);
163 1.2.4.2 nathanw return(0); /* Pass the interrupt on down the chain */
164 1.2.4.2 nathanw }
165 1.2.4.2 nathanw
166 1.2.4.2 nathanw
167 1.2.4.2 nathanw /*
168 1.2.4.2 nathanw * void setstatclockrate(int hz)
169 1.2.4.2 nathanw *
170 1.2.4.2 nathanw * Set the stat clock rate. The stat clock uses timer1
171 1.2.4.2 nathanw */
172 1.2.4.2 nathanw
173 1.2.4.2 nathanw void
174 1.2.4.2 nathanw setstatclockrate(hz)
175 1.2.4.2 nathanw int hz;
176 1.2.4.2 nathanw {
177 1.2.4.2 nathanw int count;
178 1.2.4.2 nathanw
179 1.2.4.2 nathanw count = TIMER_FREQUENCY / hz;
180 1.2.4.2 nathanw
181 1.2.4.2 nathanw printf("Setting statclock to %dHz (%d ticks)\n", hz, count);
182 1.2.4.2 nathanw
183 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
184 1.2.4.2 nathanw IOMD_T1LOW, (count >> 0) & 0xff);
185 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
186 1.2.4.2 nathanw IOMD_T1HIGH, (count >> 8) & 0xff);
187 1.2.4.2 nathanw
188 1.2.4.2 nathanw /* reload the counter */
189 1.2.4.2 nathanw
190 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
191 1.2.4.2 nathanw IOMD_T1GO, 0);
192 1.2.4.2 nathanw }
193 1.2.4.2 nathanw
194 1.2.4.2 nathanw
195 1.2.4.2 nathanw #ifdef DIAGNOSTIC
196 1.2.4.2 nathanw static void
197 1.2.4.2 nathanw checkdelay()
198 1.2.4.2 nathanw {
199 1.2.4.2 nathanw struct timeval start, end, diff;
200 1.2.4.2 nathanw
201 1.2.4.2 nathanw microtime(&start);
202 1.2.4.2 nathanw delay(10000);
203 1.2.4.2 nathanw microtime(&end);
204 1.2.4.2 nathanw timersub(&end, &start, &diff);
205 1.2.4.2 nathanw if (diff.tv_sec > 0)
206 1.2.4.2 nathanw return;
207 1.2.4.2 nathanw if (diff.tv_usec > 10000)
208 1.2.4.2 nathanw return;
209 1.2.4.2 nathanw printf("WARNING: delay(10000) took %ld us\n", diff.tv_usec);
210 1.2.4.2 nathanw }
211 1.2.4.2 nathanw #endif
212 1.2.4.2 nathanw
213 1.2.4.2 nathanw /*
214 1.2.4.2 nathanw * void cpu_initclocks(void)
215 1.2.4.2 nathanw *
216 1.2.4.2 nathanw * Initialise the clocks.
217 1.2.4.2 nathanw * This sets up the two timers in the IOMD and installs the IRQ handlers
218 1.2.4.2 nathanw *
219 1.2.4.2 nathanw * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
220 1.2.4.2 nathanw */
221 1.2.4.2 nathanw
222 1.2.4.2 nathanw void
223 1.2.4.2 nathanw cpu_initclocks()
224 1.2.4.2 nathanw {
225 1.2.4.2 nathanw /*
226 1.2.4.2 nathanw * Load timer 0 with count down value
227 1.2.4.2 nathanw * This timer generates 100Hz interrupts for the system clock
228 1.2.4.2 nathanw */
229 1.2.4.2 nathanw
230 1.2.4.2 nathanw printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
231 1.2.4.2 nathanw
232 1.2.4.2 nathanw timer0_count = TIMER_FREQUENCY / hz;
233 1.2.4.2 nathanw
234 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
235 1.2.4.2 nathanw IOMD_T0LOW, (timer0_count >> 0) & 0xff);
236 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
237 1.2.4.2 nathanw IOMD_T0HIGH, (timer0_count >> 8) & 0xff);
238 1.2.4.2 nathanw
239 1.2.4.2 nathanw /* reload the counter */
240 1.2.4.2 nathanw
241 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
242 1.2.4.2 nathanw IOMD_T0GO, 0);
243 1.2.4.2 nathanw
244 1.2.4.2 nathanw clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
245 1.2.4.2 nathanw clockhandler, 0);
246 1.2.4.2 nathanw
247 1.2.4.2 nathanw if (clockirq == NULL)
248 1.2.4.2 nathanw panic("%s: Cannot installer timer 0 IRQ handler\n",
249 1.2.4.2 nathanw clock_sc->sc_dev.dv_xname);
250 1.2.4.2 nathanw
251 1.2.4.2 nathanw if (stathz) {
252 1.2.4.2 nathanw setstatclockrate(stathz);
253 1.2.4.2 nathanw statclockirq = intr_claim(IRQ_TIMER1, IPL_CLOCK,
254 1.2.4.2 nathanw "tmr1 stat clk", statclockhandler, 0);
255 1.2.4.2 nathanw if (statclockirq == NULL)
256 1.2.4.2 nathanw panic("%s: Cannot installer timer 1 IRQ handler\n",
257 1.2.4.2 nathanw clock_sc->sc_dev.dv_xname);
258 1.2.4.2 nathanw }
259 1.2.4.2 nathanw #ifdef DIAGNOSTIC
260 1.2.4.2 nathanw checkdelay();
261 1.2.4.2 nathanw #endif
262 1.2.4.2 nathanw }
263 1.2.4.2 nathanw
264 1.2.4.2 nathanw
265 1.2.4.2 nathanw /*
266 1.2.4.2 nathanw * void microtime(struct timeval *tvp)
267 1.2.4.2 nathanw *
268 1.2.4.2 nathanw * Fill in the specified timeval struct with the current time
269 1.2.4.2 nathanw * accurate to the microsecond.
270 1.2.4.2 nathanw */
271 1.2.4.2 nathanw
272 1.2.4.2 nathanw void
273 1.2.4.2 nathanw microtime(tvp)
274 1.2.4.2 nathanw struct timeval *tvp;
275 1.2.4.2 nathanw {
276 1.2.4.2 nathanw int s;
277 1.2.4.2 nathanw int tm;
278 1.2.4.2 nathanw int deltatm;
279 1.2.4.2 nathanw static struct timeval oldtv;
280 1.2.4.2 nathanw
281 1.2.4.2 nathanw if (timer0_count == 0)
282 1.2.4.2 nathanw return;
283 1.2.4.2 nathanw
284 1.2.4.2 nathanw s = splhigh();
285 1.2.4.2 nathanw
286 1.2.4.2 nathanw /*
287 1.2.4.2 nathanw * Latch the current value of the timer and then read it.
288 1.2.4.2 nathanw * This garentees an atmoic reading of the time.
289 1.2.4.2 nathanw */
290 1.2.4.2 nathanw
291 1.2.4.2 nathanw bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
292 1.2.4.2 nathanw IOMD_T0LATCH, 0);
293 1.2.4.2 nathanw
294 1.2.4.2 nathanw tm = bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
295 1.2.4.2 nathanw IOMD_T0LOW);
296 1.2.4.2 nathanw tm += (bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
297 1.2.4.2 nathanw IOMD_T0HIGH) << 8);
298 1.2.4.2 nathanw
299 1.2.4.2 nathanw deltatm = timer0_count - tm;
300 1.2.4.2 nathanw if (deltatm < 0)
301 1.2.4.2 nathanw printf("opps deltatm < 0 tm=%d deltatm=%d\n",
302 1.2.4.2 nathanw tm, deltatm);
303 1.2.4.2 nathanw
304 1.2.4.2 nathanw /* Fill in the timeval struct */
305 1.2.4.2 nathanw *tvp = time;
306 1.2.4.2 nathanw
307 1.2.4.2 nathanw tvp->tv_usec += (deltatm / TICKS_PER_MICROSECOND);
308 1.2.4.2 nathanw
309 1.2.4.2 nathanw /* Make sure the micro seconds don't overflow. */
310 1.2.4.2 nathanw while (tvp->tv_usec >= 1000000) {
311 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
312 1.2.4.2 nathanw ++tvp->tv_sec;
313 1.2.4.2 nathanw }
314 1.2.4.2 nathanw
315 1.2.4.2 nathanw /* Make sure the time has advanced. */
316 1.2.4.2 nathanw if (tvp->tv_sec == oldtv.tv_sec &&
317 1.2.4.2 nathanw tvp->tv_usec <= oldtv.tv_usec) {
318 1.2.4.2 nathanw tvp->tv_usec = oldtv.tv_usec + 1;
319 1.2.4.2 nathanw if (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
325 1.2.4.2 nathanw oldtv = *tvp;
326 1.2.4.2 nathanw (void)splx(s);
327 1.2.4.2 nathanw }
328 1.2.4.2 nathanw
329 1.2.4.2 nathanw /*
330 1.2.4.2 nathanw * Estimated loop for n microseconds
331 1.2.4.2 nathanw */
332 1.2.4.2 nathanw
333 1.2.4.2 nathanw /* Need to re-write this to use the timers */
334 1.2.4.2 nathanw
335 1.2.4.2 nathanw /* One day soon I will actually do this */
336 1.2.4.2 nathanw
337 1.2.4.2 nathanw int delaycount = 100;
338 1.2.4.2 nathanw
339 1.2.4.2 nathanw void
340 1.2.4.2 nathanw delay(n)
341 1.2.4.2 nathanw u_int n;
342 1.2.4.2 nathanw {
343 1.2.4.2 nathanw u_int i;
344 1.2.4.2 nathanw
345 1.2.4.2 nathanw if (n == 0) return;
346 1.2.4.2 nathanw while (--n > 0) {
347 1.2.4.2 nathanw if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */
348 1.2.4.2 nathanw for (i = delaycount; --i;);
349 1.2.4.2 nathanw else
350 1.2.4.2 nathanw for (i = 8; --i;);
351 1.2.4.2 nathanw }
352 1.2.4.2 nathanw }
353 1.2.4.2 nathanw
354 1.2.4.2 nathanw /* End of iomd_clock.c */
355