/* Adapted from ATI's bfmini.c */

#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>

#include <ahb_fdl_defs.h>
#include <io_reg.h>
#include <mem_reg.h>
#include "sys_int.h"
#include <sys.h>

static u32 compatppcbootcode[] = {
    0x7c631a78,        // xor     r3,r3,r3
    0x7c73fba6,        // mtspr   1011,r3
    0x4c00012c,        // isync
    0x3c400000,        // lis     r2,0
    0x60420100,        // ori     r2,r2,5376
    0x7c5a03a6,        // mtsrr0  r2
    0x38a00000,        // li      r5,0
    0x7cbb03a6,        // mtsrr1  r5
    0x4c000064,        // rfi
    0x60000000,        // nop
    0x60000000,
    0x60000000
};

static u32 enhancedppcbootcode[] = {
    0x7c631a78,        // xor       r3,r3,r3
    0x6463d7b0,        // oris      r3,r3,55216
    0x7c73fba6,        // mtspr     1011,r3
    0x4c00012c,        // isync
    0x3c400000,        // lis       r2,0
    0x60420100,        // ori       r2,r2,5376
    0x7c5a03a6,        // mtsrr0    r2
    0x38a00000,        // li        r5,0
    0x7cbb03a6,        // mtsrr1    r5
    0x4c000064,        // rfi
    0x60000000,        // nop
    0x60000000,
    0x60000000
};

/* XXX horti: Disable after debug phase */
#if 0 
static void
exiDumpBootCode()
{
    u32 exiBufAddr;
    int i;
    u32 val, saveAIPProt;

    saveAIPProt = val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);
    AIPPROT_SET_ENAHBIOPI(val, 1);
    busWrt32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET, val);
    // Read AIPPROT to avoid hazard condition before writing EXI registers
    val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);

    exiBufAddr = PHYS_TO_PROTECTED(AIP_EXI_BASE + EXI_BOOTCODE_OFFSET);

    busPrintf("exiDumpBootCode():");
    for (i = 0; i < MAX_EXI_BOOTCODE_SLOTS; i++) {
        busPrintf("exibootcode[0x%08x]: 0x%08x", exiBufAddr, busRd32(exiBufAddr));
        exiBufAddr += sizeof(u32);
    }

    busWrt32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET, saveAIPProt);
    // Read AIPPROT to avoid hazard condition
    val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);
}
#endif
static void
exiLoadBootCode(u32 *buf, int n) 
{
    u32 exiBufAddr;
    u32 val, saveAIPProt;

    saveAIPProt = val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);
    AIPPROT_SET_ENAHBIOPI(val, 1);
    busWrt32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET, val);
    // Read AIPPROT to avoid hazard condition before writing EXI registers
    val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);

    exiBufAddr = PHYS_TO_PROTECTED(AIP_EXI_BASE + EXI_BOOTCODE_OFFSET);
    if (n > MAX_EXI_BOOTCODE_SLOTS) {
        n = MAX_EXI_BOOTCODE_SLOTS;
        printf("Warning: only loading %d instrs into EXI boot code", n);
    }
    while (n--) {
//        printf("writing 0x%08x to addr 0x%08x\n", *buf, exiBufAddr);
        busWrt32(exiBufAddr, *buf);
        buf++;
        exiBufAddr += sizeof(u32);
    }
    // dummy read to avoid hazard condition of changing AIPPROT
    // If you change the ENAHBIOPI you can trash a write.  Reading
    // will flush the last EXI write
    exiBufAddr -= sizeof(u32);
    val = busRd32(exiBufAddr);
    busWrt32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET, saveAIPProt);
    // Read AIPPROT to avoid hazard condition
    val = busRd32(ACR_PROT_REGS_BASE + AIPPROT_OFFSET);
    
//    exiDumpBootCode();
}

void
setupEXIRegBoot(enum sysMode_e mode, u32 *bootcode, u32 bootcodeSize)
{
    u32 reg;
    u32 compatSize = sizeof(compatppcbootcode)/sizeof(u32);
    u32 enhancedSize = sizeof(enhancedppcbootcode)/sizeof(u32);

    if ((NULL != bootcode) && (bootcodeSize > 0)) {
        /*user specified boot code array*/
//        printf("Loading user specified code into EXI boot registers\n");
        exiLoadBootCode(bootcode, bootcodeSize);
    }
    else if ( compatmode == mode ) 
        exiLoadBootCode(compatppcbootcode, compatSize); 
    else 
        exiLoadBootCode(enhancedppcbootcode, enhancedSize);
    reg = busRd32(ACR_PROT_REGS_BASE + ACRCOMPAT_OFFSET);
    ACRCOMPAT_SET_EXI_USE_BOOTREG(reg, 1);
    busWrt32(ACR_PROT_REGS_BASE + ACRCOMPAT_OFFSET, reg);
}
