/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#include <ios.h>
#include <sys.h>
#include <iop.h>
#include <sys_int.h>
#include <ioslibc.h>
    
#define US_TO_TICKS_ENHANCED(x) (x + (x >> 1) + (x >>2) + (x >> 3) + (x>>6) \
                                 + (x >> 8) + (x >>9) + (x >> 10) + (x>>12))
#define US_TO_TICKS_COMPAT(x) (x + (x >> 2) + (x >>6))
 
void
udelay(u32 usec)
{
    u32 start, end, ticks;
    if (sysMode == compatmode) {
        ticks = US_TO_TICKS_COMPAT(usec);
    } else {
        ticks = US_TO_TICKS_ENHANCED(usec);
    }
    if (ticks < 2) ticks = 2;

    start = busRd32(SYS_TIMER_REG);
    end = start + ticks;
    if (end > start) {
        while (busRd32(SYS_TIMER_REG) < end );
    } else {
        while ( ((ticks = busRd32(SYS_TIMER_REG)) & 0x80000000) ||
                (ticks < end) );
    }
}
