footbridge_clock.c revision 1.4 1 /* $NetBSD: footbridge_clock.c,v 1.4 2002/01/05 22:41:47 chris Exp $ */
2
3 /*
4 * Copyright (c) 1997 Mark Brinicombe.
5 * Copyright (c) 1997 Causality Limited.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Mark Brinicombe
19 * for the NetBSD Project.
20 * 4. The name of the company nor the name of the author may be used to
21 * endorse or promote products derived from this software without specific
22 * prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /* Include header files */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/time.h>
44 #include <sys/device.h>
45
46 #include <machine/intr.h>
47
48 #include <arm/cpufunc.h>
49
50 #include <arm/footbridge/dc21285reg.h>
51 #include <arm/footbridge/footbridgevar.h>
52
53 extern struct footbridge_softc *clock_sc;
54 extern u_int dc21285_fclk;
55
56 int clockhandler __P((void *));
57 int statclockhandler __P((void *));
58 static int load_timer __P((int, int));
59
60
61 #if 0
62 static int clockmatch __P((struct device *parent, struct cfdata *cf, void *aux));
63 static void clockattach __P((struct device *parent, struct device *self, void *aux));
64
65 struct cfattach footbridge_clock_ca = {
66 sizeof(struct clock_softc), clockmatch, clockattach
67 };
68
69 /*
70 * int clockmatch(struct device *parent, void *match, void *aux)
71 *
72 * Just return ok for this if it is device 0
73 */
74
75 static int
76 clockmatch(parent, cf, aux)
77 struct device *parent;
78 struct cfdata *cf;
79 void *aux;
80 {
81 union footbridge_attach_args *fba = aux;
82
83 if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
84 return(1);
85 return(0);
86 }
87
88
89 /*
90 * void clockattach(struct device *parent, struct device *dev, void *aux)
91 *
92 */
93
94 static void
95 clockattach(parent, self, aux)
96 struct device *parent;
97 struct device *self;
98 void *aux;
99 {
100 struct clock_softc *sc = (struct clock_softc *)self;
101 union footbridge_attach_args *fba = aux;
102
103 sc->sc_iot = fba->fba_ca.ca_iot;
104 sc->sc_ioh = fba->fba_ca.ca_ioh;
105
106 clock_sc = sc;
107
108 /* Cannot do anything until cpu_initclocks() has been called */
109
110 printf("\n");
111 }
112 #endif
113
114 /*
115 * int clockhandler(struct clockframe *frame)
116 *
117 * Function called by timer 1 interrupts.
118 * This just clears the interrupt condition and calls hardclock().
119 */
120
121 int
122 clockhandler(aframe)
123 void *aframe;
124 {
125 struct clockframe *frame = aframe;
126 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
127 TIMER_1_CLEAR, 0);
128 hardclock(frame);
129 return(0); /* Pass the interrupt on down the chain */
130 }
131
132
133 /*
134 * int statclockhandler(struct clockframe *frame)
135 *
136 * Function called by timer 2 interrupts.
137 * This just clears the interrupt condition and calls statclock().
138 */
139
140 int
141 statclockhandler(aframe)
142 void *aframe;
143 {
144 struct clockframe *frame = aframe;
145 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
146 TIMER_2_CLEAR, 0);
147 statclock(frame);
148 return(0); /* Pass the interrupt on down the chain */
149 }
150
151 static int
152 load_timer(base, hz)
153 int base;
154 int hz;
155 {
156 unsigned int timer_count;
157 int control;
158
159 timer_count = dc21285_fclk / hz;
160 if (timer_count > TIMER_MAX * 16) {
161 control = TIMER_FCLK_256;
162 timer_count >>= 8;
163 } else if (timer_count > TIMER_MAX) {
164 control = TIMER_FCLK_16;
165 timer_count >>= 4;
166 } else
167 control = TIMER_FCLK;
168
169 control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
170 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
171 base + TIMER_LOAD, timer_count);
172 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
173 base + TIMER_CONTROL, control);
174 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
175 base + TIMER_CLEAR, 0);
176 return(timer_count);
177 }
178
179 /*
180 * void setstatclockrate(int hz)
181 *
182 * Set the stat clock rate. The stat clock uses timer2
183 */
184
185 void
186 setstatclockrate(hz)
187 int hz;
188 {
189
190 clock_sc->sc_statclock_count = load_timer(TIMER_2_BASE, hz);
191 }
192
193 /*
194 * void cpu_initclocks(void)
195 *
196 * Initialise the clocks.
197 *
198 * Timer 1 is used for the main system clock (hardclock)
199 * Timer 2 is used for the statistics clock (statclock)
200 */
201
202 void
203 cpu_initclocks()
204 {
205
206 /* Report the clock frequencies */
207 printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
208
209 /* Setup timer 1 and claim interrupt */
210 clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
211
212 /*
213 * Use ticks per 256us for accuracy since ticks per us is often
214 * fractional e.g. @ 66MHz
215 */
216 clock_sc->sc_clock_ticks_per_256us =
217 ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
218 clock_sc->sc_clockintr = intr_claim(IRQ_TIMER_1, IPL_CLOCK,
219 "tmr1 hard clk", clockhandler, 0);
220
221 if (clock_sc->sc_clockintr == NULL)
222 panic("%s: Cannot install timer 1 interrupt handler\n",
223 clock_sc->sc_dev.dv_xname);
224
225 /* If stathz is non-zero then setup the stat clock */
226 if (stathz) {
227 /* Setup timer 2 and claim interrupt */
228 setstatclockrate(stathz);
229 clock_sc->sc_statclockintr = intr_claim(IRQ_TIMER_2, IPL_CLOCK,
230 "tmr2 stat clk", statclockhandler, 0);
231 if (clock_sc->sc_statclockintr == NULL)
232 panic("%s: Cannot install timer 2 interrupt handler\n",
233 clock_sc->sc_dev.dv_xname);
234 }
235 }
236
237
238 /*
239 * void microtime(struct timeval *tvp)
240 *
241 * Fill in the specified timeval struct with the current time
242 * accurate to the microsecond.
243 */
244
245 void
246 microtime(tvp)
247 struct timeval *tvp;
248 {
249 int s;
250 int tm;
251 int deltatm;
252 static struct timeval oldtv;
253
254 if (clock_sc == NULL || clock_sc->sc_clock_count == 0)
255 return;
256
257 s = splhigh();
258
259 tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
260 TIMER_1_VALUE);
261
262 deltatm = clock_sc->sc_clock_count - tm;
263
264 #ifdef DIAGNOSTIC
265 if (deltatm < 0)
266 panic("opps deltatm < 0 tm=%d deltatm=%d\n", tm, deltatm);
267 #endif
268
269 /* Fill in the timeval struct */
270 *tvp = time;
271 tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us);
272
273 /* Make sure the micro seconds don't overflow. */
274 while (tvp->tv_usec >= 1000000) {
275 tvp->tv_usec -= 1000000;
276 ++tvp->tv_sec;
277 }
278
279 /* Make sure the time has advanced. */
280 if (tvp->tv_sec == oldtv.tv_sec &&
281 tvp->tv_usec <= oldtv.tv_usec) {
282 tvp->tv_usec = oldtv.tv_usec + 1;
283 if (tvp->tv_usec >= 1000000) {
284 tvp->tv_usec -= 1000000;
285 ++tvp->tv_sec;
286 }
287 }
288
289 oldtv = *tvp;
290 (void)splx(s);
291 }
292
293 /*
294 * Estimated loop for n microseconds
295 */
296
297 /* Need to re-write this to use the timers */
298
299 /* One day soon I will actually do this */
300
301 int delaycount = 50;
302
303 void
304 delay(n)
305 u_int n;
306 {
307 u_int i;
308
309 if (n == 0) return;
310 while (--n > 0) {
311 if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */
312 for (i = delaycount; --i;);
313 else
314 for (i = 8; --i;);
315 }
316 }
317
318 /* End of footbridge_clock.c */
319