/*
 * \file bl_main.c
 *
 * Boot loader
 *
*/

/* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
 * ALL RIGHTS RESERVED
 */
 
#ifdef XIP_NOR

#include "hw_control_AM335x.h"
#include "soc_AM335x.h"
#include "soc_AM335x.h"
#include "hw_types.h"
#include "hw_cm_per.h"
#include "gpio.h"
  
#include "bl.h"
#include "bl_copy.h"

#define NOR_SCTR_SIZE (0x10000)


#define NOR_1ST_SCTR_ADDR (0x08000000)
#define NOR_2ND_SCTR_ADDR (0x08010000)


/******************************************************************************
**                     External Variable Declararions
*******************************************************************************/

/******************************************************************************
**                     Local Function Declararion 
*******************************************************************************/
void SetGPIOForNOR(void);


/******************************************************************************
**                     Global variable Definitions
*******************************************************************************/

/******************************************************************************
**                          FUNCTION DEFINITIONS
*******************************************************************************/
	/*
    typedef struct _ti_header_
    {
		unsigned int magic_number;
   	 	unsigned int image_size;
   	 	unsigned int load_addr;
		unsigned int run_addr;		
    }ti_header;*/

void pinmux_NOR()
{

	unsigned int count = 0x3FFF; // By Mohit Hada for testing LED Glow..
	unsigned short int  *p = (unsigned short int *) 0x80000000;
	unsigned short int k = 0;

// commented by Mohit Hada on 15/08/2015..

//#ifdef HW_ICE_V2
//	HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_VSYNC) =           //A16
//					2 |
//					  CONTROL_CONF_LCD_VSYNC_CONF_LCD_VSYNC_RXACTIVE |
//					  CONTROL_CONF_LCD_VSYNC_CONF_LCD_VSYNC_PUDEN;
//
//	HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_HSYNC) =          //A17
//					2 |
//						  CONTROL_CONF_LCD_HSYNC_CONF_LCD_HSYNC_RXACTIVE |
//		    				 CONTROL_CONF_LCD_HSYNC_CONF_LCD_HSYNC_PUDEN;
//
//	HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_DATA(6)) =			//A18
//					1 |
//						  CONTROL_CONF_LCD_DATA6_CONF_LCD_DATA6_RXACTIVE |
//		    				 CONTROL_CONF_LCD_DATA6_CONF_LCD_DATA6_PUDEN;
//
//
//	HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_DATA(7)) =         //A19
//					1 |
//						  CONTROL_CONF_LCD_DATA7_CONF_LCD_DATA7_RXACTIVE |
//		    				 CONTROL_CONF_LCD_DATA7_CONF_LCD_DATA7_PUDEN;
//#elif defined (HW_ICE)
//    //Should set GPIO pin for GPMC addressing from here if the application resides in NOR address >128K
//	HWREG( SOC_PRCM_REGS + CM_PER_GPIO2_CLKCTRL )  |= 0x2;
//
//	//SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT3
//	HWREG( SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT3) = (7 | 0x20 | 0x08);
//	//SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT2
//	HWREG( SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT2) = (7 | 0x20 | 0x08);
//	//SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT1
//	HWREG( SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT1) = (7 | 0x20 | 0x08);
//	//SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT0
//	HWREG( SOC_CONTROL_REGS + CONTROL_CONF_MMC0_DAT0) = (7 | 0x20 | 0x08);
//
//	GPIODirModeSet(SOC_GPIO_2_REGS,26,GPIO_DIR_OUTPUT);
//	GPIODirModeSet(SOC_GPIO_2_REGS,27,GPIO_DIR_OUTPUT);
//	GPIODirModeSet(SOC_GPIO_2_REGS,28,GPIO_DIR_OUTPUT);
//	GPIODirModeSet(SOC_GPIO_2_REGS,29,GPIO_DIR_OUTPUT);
//#endif

	// Modify the below code for GPI-1O interface activation for GPMC_A1 to GPMC_A3 on NOR CAPE from BBB

	////// TODO:- This line whether it is enough for clock configuration needs to eb checked....



	    HWREG( SOC_PRCM_REGS + CM_PER_GPIO1_CLKCTRL )  |= 0x2;

		GPIODirModeSet(SOC_GPIO_1_REGS,17,GPIO_DIR_OUTPUT);
		GPIODirModeSet(SOC_GPIO_1_REGS,18,GPIO_DIR_OUTPUT);
		GPIODirModeSet(SOC_GPIO_1_REGS,19,GPIO_DIR_OUTPUT);

		GPIODirModeSet(SOC_GPIO_1_REGS,21,GPIO_DIR_OUTPUT); // For LED

		//GPMC_A1 configured as GPIO
		HWREG( SOC_CONTROL_REGS + CONTROL_CONF_GPMC_A(1)) = (7 | 0x20 | 0x08);
		//GPMC_A2 configured as GPIO
		HWREG( SOC_CONTROL_REGS + CONTROL_CONF_GPMC_A(2)) = (7 | 0x20 | 0x08);
		//GPMC_A3 configured as GPIO
		HWREG( SOC_CONTROL_REGS + CONTROL_CONF_GPMC_A(3)) = (7 | 0x20 | 0x08);

		HWREG( SOC_CONTROL_REGS + CONTROL_CONF_GPMC_A(5)) = (7 | 0x20 | 0x08); // For LED

		/// TODO TODO:- VERY IMPORTANT TO INITIALIZE THE GPIO VALES TO ZERO.....

		GPIOPinWrite(SOC_GPIO_1_REGS,17,GPIO_PIN_LOW); // making the pin low
		GPIOPinWrite(SOC_GPIO_1_REGS,18,GPIO_PIN_LOW); // making the pin low
		GPIOPinWrite(SOC_GPIO_1_REGS,19,GPIO_PIN_LOW); // making the pin low

		*p = 0x0F0F;

		GPIOPinWrite(SOC_GPIO_1_REGS,21,GPIO_PIN_HIGH); // making the pin for LED

		k = *p;

		if(k==0x0F0F)
			GPIOPinWrite(SOC_GPIO_1_REGS,21,GPIO_PIN_HIGH); // making the pin for LED
		else
			GPIOPinWrite(SOC_GPIO_1_REGS,21,GPIO_PIN_LOW); // making the pin for LED

//	    while(count--);
//		GPIOPinWrite(SOC_GPIO_1_REGS,23,GPIO_PIN_LOW); // making the pin for LED


}


int find_NORImage_exec()
{
	unsigned int t_addr = 0;

// commented by Mohit Hada for testing on 15/08/2015
//#ifndef HW_ICE_V2
//	unsigned int t_blk = 0;
//#endif

	unsigned int sector;

	unsigned short int k;

	void (*appEntryNOR)();
	ti_header t_header;
	
    pinmux_NOR();

    // Added by Mohit Hada on 15/08/2015 for testing..
    ////////////////////////////////////////////////////////////////////////////
    t_header.magic_number = 0x00000000;
    t_header.image_size = 0x00000000;
    t_header.load_addr = 0x00000000;
    t_header.run_addr = 0x08003D00; // GPMC Address after 4Mbyte...

    appEntryNOR = (void (*)(void)) t_header.run_addr;

	(*appEntryNOR)( );
	////////////////////////////////////////////////////////////////////////////

	// commented by mohit hada on 15/08/2015 for testing

//	t_addr = NOR_1ST_SCTR_ADDR;
//
//	for(sector = 1 ; sector < 10 ; sector++)
//	{
//	#ifdef HW_ICE_V2
//		t_addr = NOR_1ST_SCTR_ADDR+ (sector * NOR_SCTR_SIZE);
//	#else
//		t_addr = ((sector%2)?NOR_2ND_SCTR_ADDR:NOR_1ST_SCTR_ADDR);
//		t_blk = (sector/2);
//		//Find GPIO Pin value
//		t_blk <<= 26;
//		//Set GPIO Pin value
//		*(( int*) 0x481AC13C) = t_blk;
//	#endif
//		t_header.magic_number = ((ti_header*)t_addr)->magic_number;
//		t_header.image_size = ((ti_header*)t_addr)->image_size;
//		t_header.load_addr = ((ti_header*)t_addr)->load_addr;
//		t_header.run_addr = ((ti_header*)t_addr)->run_addr;
//
//
//		if((t_header.magic_number == MAGIC_NUM_NOR) &&
//		   (t_header.image_size != 0))
//		{
//			appEntryNOR = (void (*)(void)) t_header.run_addr;
//			(*appEntryNOR)( );
//		}
// 	}
//	#ifndef HW_ICE_V2
//	*(( int*) 0x481AC13C) = 0;
//	#endif
	return -1;
}


//void SetGPIOForNOR(void)
//{
//	void (*appEntryNOR)();
//	//*(( int*) 0x481AC13C) = 0x04000000;
//	appEntryNOR = (void (*)(void)) 0x08010000;
//	(*appEntryNOR)( );
//}
#endif
/***************************** End Of File ***********************************/
