/*
 *               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 <iostypes.h>
#include <iosctypes.h>
#include <ioslibc.h>
#include <sc/sc.h>

#define RNG_SAMPLING_BITS 10000

void
delay(int num)
{
    u32 t1, t0, diff;
    
    t0 = IO_READ(SYS_TIMER_REG);

    for ( ; ;) {
        t1 = IO_READ(SYS_TIMER_REG);
        diff = (t1 >= t0) ? t1 - t0 : t1 + (0xFFFFFFFF - t0);

        if (diff > num) break;
    }

    return;
}

extern IOSError __iosGenerateHash(IOSCHashContext context, u8 * inputData, 
                                u32 inputSize, u32 chainingFlag, 
                                IOSCHash hashData, s32, void *);
static u8 bytes[8]__attribute__((aligned(64)));
static u8 mixedKey[64]__attribute__((aligned(64)));

s32
rngGetByte(u8 *byte)
{
    int i, j;
    IOSCHashContext context;
    IOSCHash hashData;
    IOSCError err = IOSC_ERROR_OK;

    IO_WRITE(SYS_RNG_REG, (SYS_RNG_SEL_USB << SYS_RNG_SEL_SHIFT) & SYS_RNG_SEL_MASK);
    //IO_WRITE(SYS_RNG_REG, (SYS_RNG_SEL_XTAL << SYS_RNG_SEL_SHIFT) & SYS_RNG_SEL_MASK);
    err = __iosGenerateHash(context, 0, 0, IOSC_HASH_FIRST, 0, -1, 0);
    if (err != IOS_ERROR_OK) {
        goto end;
    }

    /* xtal:50000 delay for 1 bit, so 50000*8 for a byte 
     * usb:10000 delay for 1 bit, so 10000*8 for a byte 
     * the "on time" for the 48 Mhz lo freq input is about 10000 ps. 
     * so if the jitter is even 1 ps, in 10000 samples of the hi 
     * freq clock, we get 10000 ps of jitter/variability. thats one bit of
     * randomness. Get a byte after every delay and hash to correct biasedness 
     * and other statistical parameters.
     * hashing more data is better since we have about 3 times the 
     * number of zeros as ones. 
     *
     */
    for(i = 0; i < sizeof(bytes); i++){
        bytes[i] = 0;
        delay(RNG_SAMPLING_BITS/200);
        for(j = 0; j < 8; j++){
            bytes[i] = bytes[i] << 1;
            bytes[i] |= (SYS_RNG_BIT & IO_READ(SYS_RNG_REG));
        }
    }

    err = __iosGenerateHash(context, mixedKey, sizeof(mixedKey), IOSC_HASH_MIDDLE, 0, -1, 0);    
    if (err != IOS_ERROR_OK) {
        goto end;
    }

    err = __iosGenerateHash(context, bytes, sizeof(bytes), IOSC_HASH_LAST, hashData, -1, 0);    
    if (err != IOS_ERROR_OK) {
        goto end;
    }

    *byte = hashData[0]; /* take one byte */

end:
    return err;
}

extern IOSCError keyGetData(u32 handle, u8 *material, u32 size);

s32
rngGetBytes(u8 *bytes, u32 numBytes)
{
    int i;
    IOSCError err = IOSC_ERROR_OK;
    u8 obtainByte;
    u32 usbCtrl = 0;

    if(numBytes > IOSC_MAX_RAND_BYTES){
        return IOSC_ERROR_INVALID_SIZE;
    }
    usbCtrl = IO_READ(USB_CTRL_REG);
    if((usbCtrl & USB_CTRL_PLL_PD) == USB_CTRL_PLL_PD){
        /* if its powered down, set to power up */
        IO_WRITE(USB_CTRL_REG, (usbCtrl & ~(USB_CTRL_PLL_PD)) | USB_CTRL_WE);
        delay(300000);
    }
    usbCtrl = IO_READ(USB_CTRL_REG);
    memset(mixedKey, 0x0, sizeof(mixedKey));
    err = keyGetData(IOSC_FS_MAC_HANDLE, mixedKey, sizeof(CSLOSHMACKey));
    if (err != IOS_ERROR_OK) {
        goto end;
    }
    for(i = 0; i < numBytes; i++){
        bytes[i] = 0;
        err = rngGetByte(&obtainByte);
        if (err != IOS_ERROR_OK) {
            goto end;
        }
        bytes[i] = obtainByte;
    }
end:
    return err;
}





















