* Wait "n" microseconds. * Relies on timer 1 counting down from (i8254_freq / hz) * Note: timer had better have been programmed before this is first used! */
| 224 | * Note: timer had better have been programmed before this is first used! |
| 225 | */ |
| 226 | void |
| 227 | i8254_delay(int n) |
| 228 | { |
| 229 | int delta, prev_tick, tick, ticks_left; |
| 230 | #ifdef DELAYDEBUG |
| 231 | int getit_calls = 1; |
| 232 | int n1; |
| 233 | static int state = 0; |
| 234 | |
| 235 | if (state == 0) { |
| 236 | state = 1; |
| 237 | for (n1 = 1; n1 <= 10000000; n1 *= 10) |
| 238 | DELAY(n1); |
| 239 | state = 2; |
| 240 | } |
| 241 | if (state == 1) |
| 242 | printf("DELAY(%d)...", n); |
| 243 | #endif |
| 244 | /* |
| 245 | * Read the counter first, so that the rest of the setup overhead is |
| 246 | * counted. Guess the initial overhead is 20 usec (on most systems it |
| 247 | * takes about 1.5 usec for each of the i/o's in getit(). The loop |
| 248 | * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The |
| 249 | * multiplications and divisions to scale the count take a while). |
| 250 | * |
| 251 | * However, if ddb is active then use a fake counter since reading |
| 252 | * the i8254 counter involves acquiring a lock. ddb must not do |
| 253 | * locking for many reasons, but it calls here for at least atkbd |
| 254 | * input. |
| 255 | */ |
| 256 | #ifdef KDB |
| 257 | if (kdb_active) |
| 258 | prev_tick = 1; |
| 259 | else |
| 260 | #endif |
| 261 | prev_tick = getit(); |
| 262 | n -= 0; /* XXX actually guess no initial overhead */ |
| 263 | /* |
| 264 | * Calculate (n * (i8254_freq / 1e6)) without using floating point |
| 265 | * and without any avoidable overflows. |
| 266 | */ |
| 267 | if (n <= 0) |
| 268 | ticks_left = 0; |
| 269 | else if (n < 256) |
| 270 | /* |
| 271 | * Use fixed point to avoid a slow division by 1000000. |
| 272 | * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest. |
| 273 | * 2^15 is the first power of 2 that gives exact results |
| 274 | * for n between 0 and 256. |
| 275 | */ |
| 276 | ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15; |
| 277 | else |
| 278 | /* |
| 279 | * Don't bother using fixed point, although gcc-2.7.2 |
| 280 | * generates particularly poor code for the long long |
| 281 | * division, since even the slow way will complete long |
| 282 | * before the delay is up (unless we're interrupted). |
| 283 | */ |