 /* Memory initialization adapted from ATI's code */
/***
 *
 *  Trade secret of ATI Technologies Inc.
 *  Unpublished work, Copyright 2005 ATI Technologies Inc.
 *
 *  All rights reserved.  This notice is intended as a precaution against
 *  inadvertent publication and does not imply publication or any waiver
 *  of confidentiality.  The year included in the foregoing notice is the
 *  year of creation of the work.
 *
 ***/
#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>

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

void
initARAMEmulation(void)
{
//    u32 reg;

    // Write AMCR in the DSP to select 128Mbit, and disable mode setting
    // busWrt16(0x0d005012, 0x43);
    // Actually that write can not be done from the IOP.  Hmm...

    // Only use a single rank
    writeDDRMemReg(DDR_RANKSEL, 7);
    writeSeqMemReg(DDR_SEQ_RANK2, 0);
    // CKE on all the time - this should be redundant, as it's already set before this
    writeSeqMemReg(DDR_SEQ_CKEEN, 1);
    writeSeqMemReg(DDR_SEQ_CKEDYN, 0);
    writeSeqMemReg(DDR_SEQ_WR2PR, 14); // Only used in ARAM mode
    writeSeqMemReg(DDR_SEQ_WRPR, 8); // In ARAM emu mode, this is used for tRP.
    writeSeqMemReg(DDR_SEQ_TRAS, 14); // Only used in ARAM mode

    // Must deassert the expanded mem range bit for DSP in the ACR.  Might this
    // be done already before reinitializing memory?  I don't know yet.
//    reg = busRd32(ACR_PROT_REGS_BASE + ACRCOMPAT_OFFSET + AHB_TRUSTED_OFFSET);
//    reg &= 0xFFFEFFFF; // I should use a macro constant here.
//    busWrt32(ACR_PROT_REGS_BASE + ACRCOMPAT_OFFSET + AHB_TRUSTED_OFFSET, reg);

    // Enable aram emu and assert reset
    writeSeqMemReg(DDR_SEQ_ARAM, 0xE);
    udelay(2);

    // Enable aram emu and deassert reset
    writeSeqMemReg(DDR_SEQ_ARAM, 0xF);
    udelay(2);

    // The only thing left to do is to enable DDR_COMPAT
    // This puts the memory controller into strict compat mode.
    // However, the IOP needs to access NAPA to load PPC programs, so
    // delay writing the DDR_COMPAT register
}
