/*********************************************************************************

Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved. 

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  

Description:
			 Configures the Power Management and EBIU modules for the 
			 ADSP-BF537 EZ-KIT LITE.
			 
*********************************************************************************/

#include "PowerDemo.h"

static void InitPower(void);
static void InitSDRAM(void);

/*********************************************************************

	Function:		Init

	Description:	Performs Processor specific initialization

*********************************************************************/
void Init(void)
{
	// Initialize the EBIU module (SDRAM)
	InitSDRAM();
	
	// Initialize the power management module
	InitPower();	
}

/*********************************************************************

	Function:		InitPower

	Description:	Initializes the Power Management module and sets 
				    the power settings for 0.85V

*********************************************************************/
static void InitPower(void)
{
	ADI_PWR_RESULT 	PWRResult;
	
	// The following command table describes the configuration for the ADSP-BF537 EZ-KIT.
	// All commands (except ADI_PWR_CMD_END) are equivalent to the single command 
	//		{ ADI_PWR_CMD_SET_EZKIT, (void*)ADI_PWR_EZKIT_BF537_600MHZ }
	//
	ADI_PWR_COMMAND_PAIR ezkit_power[] = {	
		{ ADI_PWR_CMD_SET_PROC_VARIANT, (void*)ADI_PWR_PROC_BF537SKBC1600 },	// Processor variant
		{ ADI_PWR_CMD_SET_PACKAGE,      (void*)ADI_PWR_PACKAGE_MBGA       },   // Packaging type of processor
		{ ADI_PWR_CMD_SET_VDDEXT,       (void*)ADI_PWR_VDDEXT_330         },	// External voltage supply (3.3V)
		{ ADI_PWR_CMD_SET_CLKIN,        (void*)25 /* MHz */               },   // 25Mhz oscillator connected to CLKIN
		{ ADI_PWR_CMD_END, (void*)0}											// No more commands
	};	
	//
	// All commands (except ADI_PWR_CMD_END) are equivalent to the single command 
	//		{ ADI_PWR_CMD_SET_EZKIT, (void*)ADI_PWR_EZKIT_BF537_600MHZ }
	
	// Pass the above table to the Intialization routine
	
	PWRResult = adi_pwr_Init( ezkit_power );
	if ((PWRResult != ADI_PWR_RESULT_SUCCESS) && (PWRResult != ADI_PWR_RESULT_CALL_IGNORED)) {
		ezErrorCheck(1);
	}

	// set the processor to give the fastest clock frequencies for the
	// lowest voltage level.
	
	adi_pwr_SetMaxFreqForVolt( ADI_PWR_VLEV_085 );
	
}

////////////////////////////////////////////////////////////////////////////
// variables required to configure the timing specifications for the 
// Micron MT48LC32M8A2-75 memory modules
//
static ADI_EBIU_TIMING_VALUE sdram_refresh = { 											// SDRAM Refresh rate: 
												8192, 									// 8192 cycles
												{ 64, ADI_EBIU_TIMING_UNIT_MILLISEC	} 	// 64 milliseconds
};

static ADI_EBIU_SDRAM_BANK_VALUE bank_size =  {0, ADI_EBIU_SDRAM_BANK_64MB};			// 64MB 

// cast to first union member is used to avoid warning message from compiler
static ADI_EBIU_SDRAM_BANK_VALUE bank_caw = {0, (ADI_EBIU_SDRAM_BANK_SIZE)ADI_EBIU_SDRAM_BANK_COL_10BIT}; // 10 bit column addess width

static ADI_EBIU_TIME ebiu_44_nanosecs = {44, ADI_EBIU_TIMING_UNIT_NANOSEC};				// 44 nanoseconds
static ADI_EBIU_TIME ebiu_20_nanosecs = {20, ADI_EBIU_TIMING_UNIT_NANOSEC};				// 20 nanoseconds
static ADI_EBIU_TIMING_VALUE twr_min = { 												// Minimum TWR value
												1, 										// 1 cycle
												{7500, ADI_EBIU_TIMING_UNIT_PICOSEC} 	// 7.5 nanoseconds
};

#define DO_NOT_CHANGE_MMR_SETTINGS 0

/*********************************************************************

	Function:		InitSDRAM

	Description:	Initializes the EBIU module with the required SDRAM 
				    settings.

*********************************************************************/
static void InitSDRAM(void)
{
	ADI_EBIU_RESULT EBIUResult;
	
	// The following command table describes the configuration for the ADSP-BF537 EZ-KIT, 
	// which uses two Micron MT48LC32M8A2-75 (8Meg x 8 x 4 banks) memory modules to give
	// a total of 64MB.
	//
	// This is the most general specification, as the values can be set according to the 
	// datasheet of the memory modules to be configured.
	//
	ADI_EBIU_COMMAND_PAIR ezkit_sdram[] = {	
		{ ADI_EBIU_CMD_SET_SDRAM_BANK_SIZE,		(void*)&bank_size     		},
		{ ADI_EBIU_CMD_SET_SDRAM_BANK_COL_WIDTH,(void*)&bank_caw    		}, 
		{ ADI_EBIU_CMD_SET_SDRAM_CL_THRESHOLD,	(void*)100      		},
		{ ADI_EBIU_CMD_SET_SDRAM_TRASMIN,		(void*)&ebiu_44_nanosecs   	},
		{ ADI_EBIU_CMD_SET_SDRAM_TRPMIN,		(void*)&ebiu_20_nanosecs   	},
		{ ADI_EBIU_CMD_SET_SDRAM_TRCDMIN,		(void*)&ebiu_20_nanosecs   	},
		{ ADI_EBIU_CMD_SET_SDRAM_TWRMIN,		(void*)&twr_min 	        },
		{ ADI_EBIU_CMD_SET_SDRAM_REFRESH,		(void*)&sdram_refresh 		},
		// The next command disables Fast back to back read/write sequences
		// which is safe for all systems
		{ ADI_EBIU_CMD_SET_SDRAM_FBBRW, 		(void*)0					},
		{ ADI_EBIU_CMD_END, 0}
	};
	// 
	// If a Micron memory module is used. the following commands will suffice
	//		{ ADI_EBIU_CMD_SET_SDRAM_BANK_SIZE,		(void*)&bank_size                            },
	//		{ ADI_EBIU_CMD_SET_SDRAM_MODULE, 		(void*)ADI_EBIU_SDRAM_MODULE_MT48LC32M8A2_75 },
	//		{ ADI_EBIU_CMD_END, 0}
	//
	// Finally, when the BF537 EZ-KIT is to be used the above can be replaced by
	//		{ ADI_EBIU_CMD_SET_EZKIT, (void*)ADI_EBIU_EZKIT_BF537 }
	//		{ ADI_EBIU_CMD_END, 0}
	//
	// when the .
	//
	
	
	// Pass the above table to the Intialization routine
	
	EBIUResult = adi_ebiu_Init( ezkit_sdram, DO_NOT_CHANGE_MMR_SETTINGS );
	if ((EBIUResult != ADI_EBIU_RESULT_SUCCESS) && (EBIUResult != ADI_EBIU_RESULT_CALL_IGNORED)) {
		ezErrorCheck(1);
	}
}



