| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: clock.c,v 1.37 2005/12/24 20:06:58 perry Exp $ */ | | 1 | /* $NetBSD: clock.c,v 1.37.34.1 2009/01/03 11:17:41 jdc Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1982, 1990 The Regents of the University of California. | | 4 | * Copyright (c) 1982, 1990 The Regents of the University of California. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * the Systems Programming Group of the University of Utah Computer | | 8 | * the Systems Programming Group of the University of Utah Computer |
9 | * Science Department. | | 9 | * Science Department. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
| @@ -67,27 +67,27 @@ | | | @@ -67,27 +67,27 @@ |
67 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 67 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
68 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 68 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
69 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 69 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
70 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 70 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
71 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 71 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
72 | * SUCH DAMAGE. | | 72 | * SUCH DAMAGE. |
73 | * | | 73 | * |
74 | * from: Utah $Hdr: clock.c 1.18 91/01/21$ | | 74 | * from: Utah $Hdr: clock.c 1.18 91/01/21$ |
75 | * | | 75 | * |
76 | * @(#)clock.c 7.6 (Berkeley) 5/7/91 | | 76 | * @(#)clock.c 7.6 (Berkeley) 5/7/91 |
77 | */ | | 77 | */ |
78 | | | 78 | |
79 | #include <sys/cdefs.h> | | 79 | #include <sys/cdefs.h> |
80 | __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.37 2005/12/24 20:06:58 perry Exp $"); | | 80 | __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.37.34.1 2009/01/03 11:17:41 jdc Exp $"); |
81 | | | 81 | |
82 | #include <sys/param.h> | | 82 | #include <sys/param.h> |
83 | #include <sys/kernel.h> | | 83 | #include <sys/kernel.h> |
84 | #include <sys/systm.h> | | 84 | #include <sys/systm.h> |
85 | #include <sys/device.h> | | 85 | #include <sys/device.h> |
86 | #include <sys/uio.h> | | 86 | #include <sys/uio.h> |
87 | #include <sys/conf.h> | | 87 | #include <sys/conf.h> |
88 | #include <sys/proc.h> | | 88 | #include <sys/proc.h> |
89 | #include <sys/event.h> | | 89 | #include <sys/event.h> |
90 | | | 90 | |
91 | #include <dev/clock_subr.h> | | 91 | #include <dev/clock_subr.h> |
92 | | | 92 | |
93 | #include <machine/psl.h> | | 93 | #include <machine/psl.h> |
| @@ -323,59 +323,55 @@ clkread() | | | @@ -323,59 +323,55 @@ clkread() |
323 | return(delta + tick); | | 323 | return(delta + tick); |
324 | return(delta); | | 324 | return(delta); |
325 | } | | 325 | } |
326 | | | 326 | |
327 | #define TIMB_FREQ 614400 | | 327 | #define TIMB_FREQ 614400 |
328 | #define TIMB_LIMIT 256 | | 328 | #define TIMB_LIMIT 256 |
329 | | | 329 | |
330 | /* | | 330 | /* |
331 | * Wait "n" microseconds. | | 331 | * Wait "n" microseconds. |
332 | * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz. | | 332 | * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz. |
333 | * Note: timer had better have been programmed before this is first used! | | 333 | * Note: timer had better have been programmed before this is first used! |
334 | */ | | 334 | */ |
335 | void | | 335 | void |
336 | delay(n) | | 336 | delay(unsigned int n) |
337 | int n; | | | |
338 | { | | 337 | { |
339 | int ticks, otick; | | 338 | int ticks, otick, remaining; |
340 | | | 339 | |
341 | /* | | 340 | /* |
342 | * Read the counter first, so that the rest of the setup overhead is | | 341 | * Read the counter first, so that the rest of the setup overhead is |
343 | * counted. | | 342 | * counted. |
344 | */ | | 343 | */ |
345 | otick = MFP->mf_tbdr; | | 344 | otick = MFP->mf_tbdr; |
346 | | | 345 | |
347 | /* | | 346 | if (n <= UINT_MAX / TIMB_FREQ) { |
348 | * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so | | 347 | /* |
349 | * we can take advantage of the intermediate 64-bit quantity to prevent | | 348 | * For unsigned arithmetic, division can be replaced with |
350 | * loss of significance. | | 349 | * multiplication with the inverse and a shift. |
351 | */ | | 350 | */ |
352 | n -= 5; | | 351 | remaining = n * TIMB_FREQ / 1000000; |
353 | if(n < 0) | | 352 | } else { |
354 | return; | | 353 | /* This is a very long delay. |
355 | { | | 354 | * Being slow here doesn't matter. |
356 | u_int temp; | | 355 | */ |
357 | | | 356 | remaining = (unsigned long long) n * TIMB_FREQ / 1000000; |
358 | __asm volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp) | | | |
359 | : "d" (TIMB_FREQ), "d" (n)); | | | |
360 | __asm volatile ("divul %1,%2:%0" : "=d" (n) | | | |
361 | : "d"(1000000),"d"(temp),"0"(n)); | | | |
362 | } | | 357 | } |
363 | | | 358 | |
364 | while(n > 0) { | | 359 | while(remaining > 0) { |
365 | ticks = MFP->mf_tbdr; | | 360 | ticks = MFP->mf_tbdr; |
366 | if(ticks > otick) | | 361 | if(ticks > otick) |
367 | n -= TIMB_LIMIT - (ticks - otick); | | 362 | remaining -= TIMB_LIMIT - (ticks - otick); |
368 | else n -= otick - ticks; | | 363 | else |
| | | 364 | remaining -= otick - ticks; |
369 | otick = ticks; | | 365 | otick = ticks; |
370 | } | | 366 | } |
371 | } | | 367 | } |
372 | | | 368 | |
373 | #ifdef GPROF | | 369 | #ifdef GPROF |
374 | /* | | 370 | /* |
375 | * profclock() is expanded in line in lev6intr() unless profiling kernel. | | 371 | * profclock() is expanded in line in lev6intr() unless profiling kernel. |
376 | * Assumes it is called with clock interrupts blocked. | | 372 | * Assumes it is called with clock interrupts blocked. |
377 | */ | | 373 | */ |
378 | profclock(pc, ps) | | 374 | profclock(pc, ps) |
379 | caddr_t pc; | | 375 | caddr_t pc; |
380 | int ps; | | 376 | int ps; |
381 | { | | 377 | { |