#include <common.h>
#include <commproc.h>

#if defined(CFG_I2C_UCODE_PATCH) || defined(CFG_SPI_UCODE_PATCH)

static void UcodeCopy (volatile cpm8xx_t *cpm);

void cpm_load_patch (volatile immap_t *immr)
{
	immr->im_cpm.cp_rccr &= ~0x0003;	/* Disable microcode program area */

	UcodeCopy ((cpm8xx_t *)&immr->im_cpm);	/* Copy ucode patch to DPRAM   */
#ifdef CFG_SPI_UCODE_PATCH
    {
	volatile spi_t *spi = (spi_t *) & immr->im_cpm.cp_dparam[PROFF_SPI];
	/* Activate the microcode per the instructions in the microcode manual */
	/* NOTE:  We're only relocating the SPI parameters (not I2C).          */
	immr->im_cpm.cp_cpmcr1 = 0x802a;	/* Write Trap register 1 value */
	immr->im_cpm.cp_cpmcr2 = 0x8028;	/* Write Trap register 2 value */
	spi->spi_rpbase = CFG_SPI_DPMEM_OFFSET;	/* Where to relocte SPI params */
    }
#endif

#ifdef CFG_I2C_UCODE_PATCH
    {
	volatile iic_t *iip = (iic_t *) & immr->im_cpm.cp_dparam[PROFF_IIC];
	/* Activate the microcode per the instructions in the microcode manual */
	/* NOTE:  We're only relocating the I2C parameters (not SPI).          */
	immr->im_cpm.cp_cpmcr3 = 0x802e;	/* Write Trap register 3 value */
	immr->im_cpm.cp_cpmcr4 = 0x802c;	/* Write Trap register 4 value */
	iip->iic_rpbase = CFG_I2C_DPMEM_OFFSET;	/* Where to relocte I2C params */
    }
#endif

	/*
	 * Enable DPRAM microcode to execute from the first 512 bytes
	 * and a 256 byte extension of DPRAM.
	 */
	immr->im_cpm.cp_rccr |= 0x0001;
}

static ulong patch_2000[] = {
	0x7FFFEFD9, 0x3FFD0000, 0x7FFB49F7, 0x7FF90000,
	0x5FEFADF7, 0x5F88ADF7, 0x5FEFAFF7, 0x5F88AFF7,
	0x3A9CFBC8, 0x77CAE1BB, 0xF4DE7FAD, 0xABAE9330,
	0x4E08FDCF, 0x6E0FAFF8, 0x7CCF76CF, 0xFDAFF9CF,
	0xABF88DC8, 0xA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 0x30DB5F5B, 0xFFFD5F5B, 0xFFEF5F5B,
	0xFFDF0C9C, 0xAFED0A9A, 0xAFDD0C37, 0x5F37AFBD,
	0x7FBDB081, 0x5F8147F8,
};

static ulong patch_2F00[] = {
	0x3E303430, 0x34343737, 0xABBF9B99, 0x4B4FBDBD,
	0x59949334, 0x9FFF37FB, 0x9B177DD9, 0x936956BB,
	0xFBDD697B, 0xDD2FD113, 0x1DB9F7BB, 0x36313963,
	0x79373369, 0x3193137F, 0x7331737A, 0xF7BB9B99,
	0x9BB19795, 0x77FDFD3D, 0x573B773F, 0x737933F7,
	0xB991D115, 0x31699315, 0x31531694, 0xBF4FBDBD,
	0x35931497, 0x35376956, 0xBD697B9D, 0x96931313,
	0x19797937, 0x69350000,
};

static void UcodeCopy (volatile cpm8xx_t *cpm)
{
	vu_long *p;
	int i;

	p = (vu_long *)&(cpm->cp_dpmem[0x0000]);
	for (i=0; i < sizeof(patch_2000)/4; ++i) {
		p[i] = patch_2000[i];
	}

	p = (vu_long *)&(cpm->cp_dpmem[0x0F00]);
	for (i=0; i < sizeof(patch_2F00)/4; ++i) {
		p[i] = patch_2F00[i];
	}
}

#endif	/* CFG_I2C_UCODE_PATCH, CFG_SPI_UCODE_PATCH */