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