1 /* $NetBSD: footbridge_clock.c,v 1.15 2002/11/03 21:43:30 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 #include <arm/footbridge/footbridge.h> 53 54 extern struct footbridge_softc *clock_sc; 55 extern u_int dc21285_fclk; 56 57 int clockhandler __P((void *)); 58 int statclockhandler __P((void *)); 59 static int load_timer __P((int, int)); 60 61 /* 62 * Statistics clock variance, in usec. Variance must be a 63 * power of two. Since this gives us an even number, not an odd number, 64 * we discard one case and compensate. That is, a variance of 1024 would 65 * give us offsets in [0..1023]. Instead, we take offsets in [1..1023]. 66 * This is symmetric about the point 512, or statvar/2, and thus averages 67 * to that value (assuming uniform random numbers). 68 */ 69 const int statvar = 1024; 70 int statmin; /* minimum stat clock count in ticks */ 71 int statcountperusec; /* number of ticks per usec at current stathz */ 72 int statprev; /* last value of we set statclock to */ 73 74 #if 0 75 static int clockmatch __P((struct device *parent, struct cfdata *cf, void *aux)); 76 static void clockattach __P((struct device *parent, struct device *self, void *aux)); 77 78 CFATTACH_DECL(footbridge_clock, sizeof(struct clock_softc), 79 clockmatch, clockattach, NULL, NULL); 80 81 /* 82 * int clockmatch(struct device *parent, void *match, void *aux) 83 * 84 * Just return ok for this if it is device 0 85 */ 86 87 static int 88 clockmatch(parent, cf, aux) 89 struct device *parent; 90 struct cfdata *cf; 91 void *aux; 92 { 93 union footbridge_attach_args *fba = aux; 94 95 if (strcmp(fba->fba_ca.ca_name, "clk") == 0) 96 return(1); 97 return(0); 98 } 99 100 101 /* 102 * void clockattach(struct device *parent, struct device *dev, void *aux) 103 * 104 */ 105 106 static void 107 clockattach(parent, self, aux) 108 struct device *parent; 109 struct device *self; 110 void *aux; 111 { 112 struct clock_softc *sc = (struct clock_softc *)self; 113 union footbridge_attach_args *fba = aux; 114 115 sc->sc_iot = fba->fba_ca.ca_iot; 116 sc->sc_ioh = fba->fba_ca.ca_ioh; 117 118 clock_sc = sc; 119 120 /* Cannot do anything until cpu_initclocks() has been called */ 121 122 printf("\n"); 123 } 124 #endif 125 126 /* 127 * int clockhandler(struct clockframe *frame) 128 * 129 * Function called by timer 1 interrupts. 130 * This just clears the interrupt condition and calls hardclock(). 131 */ 132 133 int 134 clockhandler(aframe) 135 void *aframe; 136 { 137 struct clockframe *frame = aframe; 138 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 139 TIMER_1_CLEAR, 0); 140 hardclock(frame); 141 return(0); /* Pass the interrupt on down the chain */ 142 } 143 144 /* 145 * int statclockhandler(struct clockframe *frame) 146 * 147 * Function called by timer 2 interrupts. 148 * This just clears the interrupt condition and calls statclock(). 149 */ 150 151 int 152 statclockhandler(aframe) 153 void *aframe; 154 { 155 struct clockframe *frame = aframe; 156 int newint, r; 157 int currentclock ; 158 159 /* start the clock off again */ 160 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 161 TIMER_2_CLEAR, 0); 162 163 do { 164 r = random() & (statvar-1); 165 } while (r == 0); 166 newint = statmin + (r * statcountperusec); 167 168 /* fetch the current count */ 169 currentclock = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh, 170 TIMER_2_VALUE); 171 172 /* 173 * work out how much time has run, add another usec for time spent 174 * here 175 */ 176 r = ((statprev - currentclock) + statcountperusec); 177 178 if (r < newint) { 179 newint -= r; 180 r = 0; 181 } 182 else 183 printf("statclockhandler: Statclock overrun\n"); 184 185 186 /* 187 * update the clock to the new counter, this reloads the existing 188 * timer 189 */ 190 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 191 TIMER_2_LOAD, newint); 192 statprev = newint; 193 statclock(frame); 194 if (r) 195 /* 196 * We've completely overrun the previous interval, 197 * make sure we report the correct number of ticks. 198 */ 199 statclock(frame); 200 201 return(0); /* Pass the interrupt on down the chain */ 202 } 203 204 static int 205 load_timer(base, hz) 206 int base; 207 int hz; 208 { 209 unsigned int timer_count; 210 int control; 211 212 timer_count = dc21285_fclk / hz; 213 if (timer_count > TIMER_MAX * 16) { 214 control = TIMER_FCLK_256; 215 timer_count >>= 8; 216 } else if (timer_count > TIMER_MAX) { 217 control = TIMER_FCLK_16; 218 timer_count >>= 4; 219 } else 220 control = TIMER_FCLK; 221 222 control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC); 223 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 224 base + TIMER_LOAD, timer_count); 225 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 226 base + TIMER_CONTROL, control); 227 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 228 base + TIMER_CLEAR, 0); 229 return(timer_count); 230 } 231 232 /* 233 * void setstatclockrate(int hz) 234 * 235 * Set the stat clock rate. The stat clock uses timer2 236 */ 237 238 void 239 setstatclockrate(hz) 240 int hz; 241 { 242 int statint; 243 int countpersecond; 244 int statvarticks; 245 246 /* statint == num in counter to drop by desired hz */ 247 statint = statprev = clock_sc->sc_statclock_count = 248 load_timer(TIMER_2_BASE, hz); 249 250 /* Get the total ticks a second */ 251 countpersecond = statint * hz; 252 253 /* now work out how many ticks per usec */ 254 statcountperusec = countpersecond / 1000000; 255 256 /* calculate a variance range of statvar */ 257 statvarticks = statcountperusec * statvar; 258 259 /* minimum is statint - 50% of variant */ 260 statmin = statint - (statvarticks / 2); 261 } 262 263 /* 264 * void cpu_initclocks(void) 265 * 266 * Initialise the clocks. 267 * 268 * Timer 1 is used for the main system clock (hardclock) 269 * Timer 2 is used for the statistics clock (statclock) 270 */ 271 272 void 273 cpu_initclocks() 274 { 275 /* stathz and profhz should be set to something, we have the timer */ 276 if (stathz == 0) 277 stathz = hz; 278 279 if (profhz == 0) 280 profhz = stathz * 5; 281 282 /* Report the clock frequencies */ 283 printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz); 284 285 /* Setup timer 1 and claim interrupt */ 286 clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz); 287 288 /* 289 * Use ticks per 256us for accuracy since ticks per us is often 290 * fractional e.g. @ 66MHz 291 */ 292 clock_sc->sc_clock_ticks_per_256us = 293 ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000); 294 clock_sc->sc_clockintr = footbridge_intr_claim(IRQ_TIMER_1, IPL_CLOCK, 295 "tmr1 hard clk", clockhandler, 0); 296 297 if (clock_sc->sc_clockintr == NULL) 298 panic("%s: Cannot install timer 1 interrupt handler", 299 clock_sc->sc_dev.dv_xname); 300 301 /* If stathz is non-zero then setup the stat clock */ 302 if (stathz) { 303 /* Setup timer 2 and claim interrupt */ 304 setstatclockrate(stathz); 305 clock_sc->sc_statclockintr = footbridge_intr_claim(IRQ_TIMER_2, IPL_STATCLOCK, 306 "tmr2 stat clk", statclockhandler, 0); 307 if (clock_sc->sc_statclockintr == NULL) 308 panic("%s: Cannot install timer 2 interrupt handler", 309 clock_sc->sc_dev.dv_xname); 310 } 311 } 312 313 314 /* 315 * void microtime(struct timeval *tvp) 316 * 317 * Fill in the specified timeval struct with the current time 318 * accurate to the microsecond. 319 */ 320 321 void 322 microtime(tvp) 323 struct timeval *tvp; 324 { 325 int s; 326 int tm; 327 int deltatm; 328 static struct timeval oldtv; 329 330 if (clock_sc == NULL || clock_sc->sc_clock_count == 0) 331 return; 332 333 s = splhigh(); 334 335 tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh, 336 TIMER_1_VALUE); 337 338 deltatm = clock_sc->sc_clock_count - tm; 339 340 #ifdef DIAGNOSTIC 341 if (deltatm < 0) 342 panic("opps deltatm < 0 tm=%d deltatm=%d", tm, deltatm); 343 #endif 344 345 /* Fill in the timeval struct */ 346 *tvp = time; 347 tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us); 348 349 /* Make sure the micro seconds don't overflow. */ 350 while (tvp->tv_usec >= 1000000) { 351 tvp->tv_usec -= 1000000; 352 ++tvp->tv_sec; 353 } 354 355 /* Make sure the time has advanced. */ 356 if (tvp->tv_sec == oldtv.tv_sec && 357 tvp->tv_usec <= oldtv.tv_usec) { 358 tvp->tv_usec = oldtv.tv_usec + 1; 359 if (tvp->tv_usec >= 1000000) { 360 tvp->tv_usec -= 1000000; 361 ++tvp->tv_sec; 362 } 363 } 364 365 oldtv = *tvp; 366 (void)splx(s); 367 } 368 369 /* 370 * Use a timer to track microseconds, if the footbridge hasn't been setup we 371 * rely on an estimated loop, however footbridge is attached very early on. 372 */ 373 374 static int delay_clock_count = 0; 375 static int delay_count_per_usec = 0; 376 377 void 378 calibrate_delay(void) 379 { 380 delay_clock_count = load_timer(TIMER_3_BASE, 100); 381 delay_count_per_usec = delay_clock_count/10000; 382 #ifdef VERBOSE_DELAY_CALIBRATION 383 printf("delay calibration: delay_cc = %d, delay_c/us=%d\n", 384 delay_clock_count, delay_count_per_usec); 385 386 printf("0.."); 387 delay(1000000); 388 printf("1.."); 389 delay(1000000); 390 printf("2.."); 391 delay(1000000); 392 printf("3.."); 393 delay(1000000); 394 printf("4.."); 395 delay(1000000); 396 printf("5.."); 397 delay(1000000); 398 printf("6.."); 399 delay(1000000); 400 printf("7.."); 401 delay(1000000); 402 printf("8.."); 403 delay(1000000); 404 printf("9.."); 405 delay(1000000); 406 printf("10\n"); 407 #endif 408 } 409 410 int delaycount = 500; 411 412 void 413 delay(n) 414 u_int n; 415 { 416 volatile u_int i; 417 uint32_t cur, last, delta, usecs; 418 419 if (n == 0) return; 420 421 422 // not calibrated the timer yet, so try to live with this horrible 423 // loop! 424 if (delay_clock_count == 0) 425 { 426 while (n-- > 0) { 427 for (i = delaycount; --i;); 428 } 429 return; 430 } 431 432 /* 433 * read the current value (do not reset it as delay is reentrant) 434 */ 435 last = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh, 436 TIMER_3_VALUE); 437 438 delta = usecs = 0; 439 440 while (n > usecs) 441 { 442 cur = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh, 443 TIMER_3_VALUE); 444 if (last < cur) 445 /* timer has wrapped */ 446 delta += ((delay_clock_count - cur) + last); 447 else 448 delta += (last - cur); 449 450 if (cur == 0) 451 { 452 /* 453 * reset the timer, note that if something blocks us for more 454 * than 1/100s we may delay for too long, but I believe that 455 * is fairly unlikely. 456 */ 457 bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh, 458 TIMER_3_CLEAR, 0); 459 } 460 last = cur; 461 462 if (delta >= delay_count_per_usec) 463 { 464 usecs += delta / delay_count_per_usec; 465 delta %= delay_count_per_usec; 466 } 467 } 468 } 469 470 /* End of footbridge_clock.c */ 471