/* Memory.c - Memory mappings and remapping functions */

/* Copyright - Galileo technology. */

/* modified by Josh Huber to clean some things up, and
 * fit it into the U-Boot framework */

#include <galileo/core.h>
#include <galileo/memory.h>

/********************************************************************
* memoryGetBankBaseAddress - Gets the base address of a memory bank
*      - If the memory bank size is 0 then this base address has no meaning!!!
*
*
* INPUTS:   MEMORY_BANK bank - The bank we ask for its base Address.
* OUTPUT:   N/A
* RETURNS: Memory bank base address.
*********************************************************************/
static unsigned long memoryGetBankRegOffset(MEMORY_BANK bank)
{
    switch (bank)
    {
	case BANK0:
	    return SCS_0_LOW_DECODE_ADDRESS;
	case BANK1:
	    return SCS_1_LOW_DECODE_ADDRESS;
	case BANK2:
	    return SCS_2_LOW_DECODE_ADDRESS;
	case BANK3:
	    return SCS_3_LOW_DECODE_ADDRESS;
    }
    return SCS_0_LOW_DECODE_ADDRESS; /* default value */
}

unsigned int memoryGetBankBaseAddress(MEMORY_BANK bank)
{
    unsigned int base;
    unsigned int regOffset=memoryGetBankRegOffset(bank);

    GT_REG_READ(regOffset,&base);
    base = base << 20;
    return base;
}

/********************************************************************
* memoryGetDeviceBaseAddress - Gets the base address of a device.
*           - If the device size is 0 then this base address has no meaning!!!
*
*
* INPUT:   DEVICE device - The device we ask for its base address.
* OUTPUT:   N/A
* RETURNS: Device base address.
*********************************************************************/
static unsigned int memoryGetDeviceRegOffset(DEVICE device)
{
    switch (device)
    {
	case DEVICE0:
	    return CS_0_LOW_DECODE_ADDRESS;
	case DEVICE1:
	    return CS_1_LOW_DECODE_ADDRESS;
	case DEVICE2:
	    return CS_2_LOW_DECODE_ADDRESS;
	case DEVICE3:
	    return CS_3_LOW_DECODE_ADDRESS;
	case BOOT_DEVICE:
	    return BOOTCS_LOW_DECODE_ADDRESS;
    }
    return CS_0_LOW_DECODE_ADDRESS; /* default value */
}

unsigned int memoryGetDeviceBaseAddress(DEVICE device)
{
    unsigned int regBase;
    unsigned int regEnd;
    unsigned int regOffset=memoryGetDeviceRegOffset(device);

    GT_REG_READ(regOffset, &regBase);
    GT_REG_READ(regOffset+8, &regEnd);

    if(regEnd<=regBase) return 0xffffffff;	/* ERROR !!! */

    regBase = regBase << 20;
    return regBase;
}

/********************************************************************
* memoryGetBankSize - Returns the size of a memory bank.
*
*
* INPUT:    MEMORY_BANK bank - The bank we ask for its size.
* OUTPUT:   N/A
* RETURNS: Memory bank size.
*********************************************************************/
unsigned int memoryGetBankSize(MEMORY_BANK bank)
{
    unsigned int size,base;
    unsigned int highValue;
    unsigned int highAddress=memoryGetBankRegOffset(bank)+8;

    base = memoryGetBankBaseAddress(bank);
    GT_REG_READ(highAddress,&highValue);
    highValue = (highValue + 1) << 20;
    if(base > highValue)
	size=0;
    else
	size = highValue - base;
    return size;
}

/********************************************************************
* memoryGetDeviceSize - Returns the size of a device memory space
*
*
* INPUT:    DEVICE device - The device we ask for its base address.
* OUTPUT:   N/A
* RETURNS:  Size of a device memory space.
*********************************************************************/
unsigned int memoryGetDeviceSize(DEVICE device)
{
    unsigned int size,base;
    unsigned int highValue;
    unsigned int highAddress=memoryGetDeviceRegOffset(device)+8;

    base = memoryGetDeviceBaseAddress(device);
    GT_REG_READ(highAddress,&highValue);
    if (highValue == 0xfff)
    {
	size = (~base) + 1;	/* what the heck is this? */
	return size;
    }
    else
	highValue = (highValue + 1) << 20;

    if(base > highValue)
	size=0;
    else
	size = highValue - base;
    return size;
}

/********************************************************************
* memoryGetDeviceWidth - A device can be with: 1,2,4 or 8 Bytes data width.
*                  The width is determine in registers: 'Device Parameters'
*                  registers (0x45c, 0x460, 0x464, 0x468, 0x46c - for each device.
*                  at bits: [21:20].
*
* INPUT:    DEVICE device - Device number
* OUTPUT:   N/A
* RETURNS:  Device width in Bytes (1,2,4 or 8), 0 if error had occurred.
*********************************************************************/
unsigned int memoryGetDeviceWidth(DEVICE device)
{
    unsigned int width;
    unsigned int regValue;

    GT_REG_READ(DEVICE_BANK0PARAMETERS + device*4,&regValue);
    width =  (regValue & 0x00300000) >> 20;
    switch (width)
    {
	case 0:
	    return 1;
	case 1:
	    return 2;
	case 2:
	    return 4;
	case 3:
	    return 8;
	default:
	    return 0;
    }
}

bool memoryMapBank(MEMORY_BANK bank, unsigned int bankBase,unsigned int bankLength)
{
    unsigned int low=0xfff;
    unsigned int high=0x0;
    unsigned int regOffset=memoryGetBankRegOffset(bank);

    if(bankLength!=0) {
	low = (bankBase >> 20) & 0xffff;
	high=((bankBase+bankLength)>>20)-1;
    }

#ifdef DEBUG
    {
	unsigned int oldLow, oldHigh;
	GT_REG_READ(regOffset,&oldLow);
	GT_REG_READ(regOffset+8,&oldHigh);

	printf("b%d %x-%x->%x-%x\n", bank, oldLow, oldHigh, low, high);
    }
#endif

    GT_REG_WRITE(regOffset,low);
    GT_REG_WRITE(regOffset+8,high);

    return true;
}
bool memoryMapDeviceSpace(DEVICE device, unsigned int deviceBase,unsigned int deviceLength)
{
    /* TODO: what are appropriate "unmapped" values? */
    unsigned int low=0xfff;
    unsigned int high=0x0;
    unsigned int regOffset=memoryGetDeviceRegOffset(device);

    if(deviceLength != 0) {
	low=deviceBase>>20;
	high=((deviceBase+deviceLength)>>20)-1;
    } else {
	/* big problems in here... */
	/* this will HANG */
    }

    GT_REG_WRITE(regOffset,low);
    GT_REG_WRITE(regOffset+8,high);

    return true;
}


/********************************************************************
* memoryMapInternalRegistersSpace - Sets new base address for the internals
*                                   registers.
*
* INPUTS:  unsigned int internalRegBase - The new base address.
* RETURNS: true on success, false on failure
*********************************************************************/
bool memoryMapInternalRegistersSpace(unsigned int internalRegBase)
{
    unsigned int currentValue;
    unsigned int internalValue = internalRegBase;

    internalRegBase = (internalRegBase >> 20);
    GT_REG_READ(INTERNAL_SPACE_DECODE,&currentValue);
    internalRegBase = (currentValue & 0xffff0000) | internalRegBase;
    GT_REG_WRITE(INTERNAL_SPACE_DECODE,internalRegBase);
    INTERNAL_REG_BASE_ADDR = internalValue;
    return true;
}

/********************************************************************
* memoryGetInternalRegistersSpace - Gets internal registers Base Address.
*
* INPUTS:  unsigned int internalRegBase - The new base address.
* RETURNS: true on success, false on failure
*********************************************************************/
unsigned int memoryGetInternalRegistersSpace(void)
{
    return INTERNAL_REG_BASE_ADDR;
}

/********************************************************************
* memorySetProtectRegion - This function modifys one of the 8 regions with
*                          one of the three protection mode.
*                        - Be advised to check the spec before modifying them.
*
*
* Inputs: CPU_PROTECT_REGION - one of the eight regions.
*         CPU_ACCESS - general access.
*         CPU_WRITE - read only access.
*         CPU_CACHE_PROTECT - chache access.
*      we defining CPU because there is another protect from the pci SIDE.
* Returns: false if one of the parameters is wrong and true else
*********************************************************************/
bool memorySetProtectRegion(MEMORY_PROTECT_REGION region,
			    MEMORY_ACCESS memAccess,
			    MEMORY_ACCESS_WRITE memWrite,
			    MEMORY_CACHE_PROTECT cacheProtection,
			    unsigned int baseAddress,
			    unsigned int regionLength)
{
    unsigned int protectHigh = baseAddress + regionLength;

    if(regionLength == 0) /* closing the region */
    {
	GT_REG_WRITE(CPU_LOW_PROTECT_ADDRESS_0 + 0x10*region,0x0000ffff);
	GT_REG_WRITE(CPU_HIGH_PROTECT_ADDRESS_0 + 0x10*region,0);
	return true;
    }
    baseAddress =  (baseAddress & 0xfff00000) >> 20;
    baseAddress = baseAddress | memAccess << 16 |  memWrite << 17
		     | cacheProtection << 18;
    GT_REG_WRITE(CPU_LOW_PROTECT_ADDRESS_0 + 0x10*region,baseAddress);
    protectHigh = (protectHigh & 0xfff00000) >> 20;
    GT_REG_WRITE(CPU_HIGH_PROTECT_ADDRESS_0 + 0x10*region,protectHigh - 1);
    return true;
}

/********************************************************************
* memorySetRegionSnoopMode - This function modifys one of the 4 regions which
*                            supports Cache Coherency.
*
*
* Inputs: SNOOP_REGION region - One of the four regions.
*         SNOOP_TYPE snoopType - There is four optional Types:
*                               1. No Snoop.
*                               2. Snoop to WT region.
*                               3. Snoop to WB region.
*                               4. Snoop & Invalidate to WB region.
*         unsigned int baseAddress - Base Address of this region.
*         unsigned int topAddress - Top Address of this region.
* Returns: false if one of the parameters is wrong and true else
*********************************************************************/
bool memorySetRegionSnoopMode(MEMORY_SNOOP_REGION region,
			      MEMORY_SNOOP_TYPE snoopType,
			      unsigned int baseAddress,
			      unsigned int regionLength)
{
    unsigned int snoopXbaseAddress;
    unsigned int snoopXtopAddress;
    unsigned int data;
    unsigned int snoopHigh = baseAddress + regionLength;

    if( (region > MEM_SNOOP_REGION3) || (snoopType > MEM_SNOOP_WB) )
	return false;
    snoopXbaseAddress = SNOOP_BASE_ADDRESS_0 + 0x10 * region;
    snoopXtopAddress = SNOOP_TOP_ADDRESS_0 + 0x10 * region;
    if(regionLength == 0) /* closing the region */
    {
	GT_REG_WRITE(snoopXbaseAddress,0x0000ffff);
	GT_REG_WRITE(snoopXtopAddress,0);
	return true;
    }
    baseAddress = baseAddress & 0xffff0000;
    data = (baseAddress >> 16) | snoopType << 16;
    GT_REG_WRITE(snoopXbaseAddress,data);
    snoopHigh = (snoopHigh & 0xfff00000) >> 20;
    GT_REG_WRITE(snoopXtopAddress,snoopHigh - 1);
    return true;
}

/********************************************************************
* memoryRemapAddress - This fubction used for address remapping.
*
*
* Inputs: regOffset: remap register
*         remapValue :
* Returns: false if one of the parameters is erroneous,true otherwise.
*********************************************************************/
bool memoryRemapAddress(unsigned int remapReg, unsigned int remapValue)
{
    unsigned int valueForReg;
    valueForReg = (remapValue & 0xfff00000) >> 20;
    GT_REG_WRITE(remapReg, valueForReg);
    return true;
}

/********************************************************************
* memoryGetDeviceParam - This function used for getting device parameters from
*                        DEVICE BANK PARAMETERS REGISTER
*
*
* Inputs:        - deviceParam: STRUCT with paramiters for DEVICE BANK
*                  PARAMETERS REGISTER
*                - deviceNum : number of device
* Returns: false if one of the parameters is erroneous,true otherwise.
*********************************************************************/
bool memoryGetDeviceParam(DEVICE_PARAM *deviceParam, DEVICE deviceNum)
{
    unsigned int valueOfReg;
    unsigned int calcData;

    GT_REG_READ(DEVICE_BANK0PARAMETERS + 4 * deviceNum, &valueOfReg);
    calcData = (0x7 & valueOfReg) + ((0x400000 & valueOfReg) >> 19);
    deviceParam -> turnOff = calcData;          /* Turn Off */

    calcData = ((0x78 & valueOfReg) >> 3) + ((0x800000 & valueOfReg) >> 19);
    deviceParam -> acc2First = calcData;        /* Access To First */

    calcData = ((0x780 & valueOfReg) >> 7) + ((0x1000000 & valueOfReg) >> 20);
    deviceParam -> acc2Next = calcData;         /* Access To Next */

    calcData = ((0x3800 & valueOfReg) >> 11) + ((0x2000000 & valueOfReg) >> 22);
    deviceParam -> ale2Wr = calcData;           /* Ale To Write */

    calcData = ((0x1c000 & valueOfReg) >> 14) + ((0x4000000 & valueOfReg) >> 23);
    deviceParam -> wrLow = calcData;            /* Write Active */

    calcData = ((0xe0000 & valueOfReg) >> 17) + ((0x8000000 & valueOfReg) >> 24);
    deviceParam -> wrHigh = calcData;           /* Write High */

    calcData = ((0x300000 & valueOfReg) >> 20);
    switch (calcData)
    {
    case 0:
	deviceParam -> deviceWidth = 1;         /* one Byte - 8-bit */
	break;
    case 1:
	deviceParam -> deviceWidth = 2;         /* two Bytes - 16-bit */
	break;
    case 2:
	deviceParam -> deviceWidth = 4;         /* four Bytes - 32-bit */
	break;
    case 3:
	deviceParam -> deviceWidth = 8;         /* eight Bytes - 64-bit */
	break;
    default:
	deviceParam -> deviceWidth = 1;
	break;
    }
    return true;
}

/********************************************************************
* memorySetDeviceParam - This function used for setting device parameters to
*                        DEVICE BANK PARAMETERS REGISTER
*
*
* Inputs:        - deviceParam: STRUCT for store paramiters from DEVICE BANK
*                  PARAMETERS REGISTER
*                - deviceNum : number of device
* Returns: false if one of the parameters is erroneous,true otherwise.
*********************************************************************/
bool memorySetDeviceParam(DEVICE_PARAM *deviceParam, DEVICE deviceNum)
{
    unsigned int valueForReg;

    if((deviceParam -> turnOff >= 0xf) || (deviceParam -> acc2First >= 0x1f) ||
       (deviceParam -> acc2Next >= 0x1f) || (deviceParam -> ale2Wr >= 0xf) ||
	(deviceParam -> wrLow >= 0xf) || (deviceParam -> wrHigh >= 0xf))
	return false;
    valueForReg = (((deviceParam -> turnOff) & 0x7) |
		   (((deviceParam -> turnOff) & 0x8) << 19) |
		   (((deviceParam -> acc2First) & 0xf) << 3) |
		   (((deviceParam -> acc2First) & 0x10) << 19) |
		   (((deviceParam -> acc2Next) & 0xf) << 7) |
		   (((deviceParam -> acc2Next) & 0x10) << 20) |
		   (((deviceParam -> ale2Wr) & 0x7) << 11) |
		   (((deviceParam -> ale2Wr) & 0xf) << 22) |
		   (((deviceParam -> wrLow) & 0x7) << 14) |
		   (((deviceParam -> wrLow) & 0xf) << 23) |
		   (((deviceParam -> wrHigh) & 0x7) << 17) |
		   (((deviceParam -> wrHigh) & 0xf) << 24));
    /* insert the device width: */
    switch(deviceParam->deviceWidth)
    {
    case 1:
	valueForReg = valueForReg | _8BIT;
	break;
    case 2:
	valueForReg = valueForReg | _16BIT;
	break;
    case 4:
	valueForReg = valueForReg | _32BIT;
	break;
    case 8:
	valueForReg = valueForReg | _64BIT;
	break;
    default:
	valueForReg = valueForReg | _8BIT;
	break;
    }
    GT_REG_WRITE(DEVICE_BANK0PARAMETERS + 4 * deviceNum, valueForReg);
    return true;
}