| 269 | jz4780_timer_devclass, 0, 0, BUS_PASS_TIMER); |
| 270 | |
| 271 | void |
| 272 | DELAY(int usec) |
| 273 | { |
| 274 | uint32_t counter; |
| 275 | uint32_t delta, now, previous, remaining; |
| 276 | |
| 277 | /* Timer has not yet been initialized */ |
| 278 | if (jz4780_timer_sc == NULL) { |
| 279 | for (; usec > 0; usec--) |
| 280 | for (counter = 200; counter > 0; counter--) { |
| 281 | /* Prevent gcc from optimizing out the loop */ |
| 282 | mips_rd_cause(); |
| 283 | } |
| 284 | return; |
| 285 | } |
| 286 | TSENTER(); |
| 287 | |
| 288 | /* |
| 289 | * Some of the other timers in the source tree do this calculation as: |
| 290 | * |
| 291 | * usec * ((sc->tc.tc_frequency / 1000000) + 1) |
| 292 | * |
| 293 | * which gives a fairly pessimistic result when tc_frequency is an exact |
| 294 | * multiple of 1000000. Given the data type and typical values for |
| 295 | * tc_frequency adding 999999 shouldn't overflow. |
| 296 | */ |
| 297 | remaining = usec * ((jz4780_timer_sc->tc.tc_frequency + 999999) / |
| 298 | 1000000); |
| 299 | |
| 300 | /* |
| 301 | * We add one since the first iteration may catch the counter just |
| 302 | * as it is changing. |
| 303 | */ |
| 304 | remaining += 1; |
| 305 | |
| 306 | previous = jz4780_get_timecount(&jz4780_timer_sc->tc); |
| 307 | |
| 308 | for ( ; ; ) { |
| 309 | now = jz4780_get_timecount(&jz4780_timer_sc->tc); |
| 310 | |
| 311 | /* |
| 312 | * If the timer has rolled over, then we have the case: |
| 313 | * |
| 314 | * if (previous > now) { |
| 315 | * delta = (0 - previous) + now |
| 316 | * } |
| 317 | * |
| 318 | * which is really no different then the normal case. |
| 319 | * Both cases are simply: |
| 320 | * |
| 321 | * delta = now - previous. |
| 322 | */ |
| 323 | delta = now - previous; |
| 324 | |
| 325 | if (delta >= remaining) |
| 326 | break; |
| 327 | |
| 328 | previous = now; |
no test coverage detected