1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void udelay(int interval_in_us)
{
    if (UNLIKELY(interval_in_us > 3000 || interval_in_us < 0))
        panicf("udelay too high or negative");
    unsigned cycles = KERNEL_TIMER_FREQ / (1000*1000) * interval_in_us;

    if (cycles >= TIMER2_VALUE)
    {
        cycles -= TIMER2_VALUE;
        /* wait for the wrap */
        while(TIMER2_VALUE != TIMER2_BGLOAD);
    }

    int end = TIMER2_VALUE - cycles;
    if (end <= 0) return;
    while(TIMER2_VALUE > (unsigned)end);
}