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