From ffd8d8deade8d8d0f4dbc2df53c55f04997b8d06 Mon Sep 17 00:00:00 2001
From: Benoit Rat <benoit@sevensols.com>
Date: Thu, 21 Jun 2012 16:38:24 +0200
Subject: [PATCH] usb-loader:Adding write & run support for DDR (live loading)

---
 .../samba_applets/isp-extram-at91sam9g45.bin  | Bin 7540 -> 7668 bytes
 .../samba_applets/isp-project/extram/main.c   | 372 +++++++++++-------
 2 files changed, 222 insertions(+), 150 deletions(-)

diff --git a/usb-loader/samba_applets/isp-extram-at91sam9g45.bin b/usb-loader/samba_applets/isp-extram-at91sam9g45.bin
index f33386a0def600cec1763dd4265e913ebb9ed6dd..9f2a0e67c88b9440b9afd27fa57e276d2cf1ebbc 100755
GIT binary patch
delta 1154
zcmXw%TTC2f6vxlZ?(9mt3p?dvw@@-bROq-k3j|s4MzQP64z)^cYQ)Q6T$tSkNLdhw
zO%|nAn?6*Tn5IT!jF+VNAkC)wASBlG#RrpaHL(+X7!pnDC2=&E%qBMDe}{E4znt^^
z=Y0R~%*nUizdi8Q&-v~B5|#OjB(kq8uf3s$o5<EmOv@Sdj7;`uX-U{>d0iz&VNa*X
zK3Y1hHtIexXfCVEa#YW!ME$j4vgMMflStR!wU(5n)H%I&=$y8w{+4Ofr!t>sm=&Ms
zH(!xYDf24X@lww7@SBTyc~QwhYA;QBQowv!o>%5m3+g0Hn@br_^_g>W237T?bY<g=
zd`VA336->pG9@RPG$_6jbq0!~o?2^W<*X96j;l;3S1h-^%S_7?ir*^de)B~+pH_}y
z)344EZ$p(SW=Ld;nnISQjLF0Jw&}E=dI9lAC!3)IT=7w_ve_J!1J<C*9te8I4d&DG
zAgZo8e8v2j%v^i`%2y5vM-abs5`n5}5}rVQ+3EINl6kX7J&ZA5at5u4*j$WxGG|&X
ziu^4XL+N{tC49{#6>3dl^^QaBOi|YutETOQHl+TpQJXln)Yiy-rU@1chy8Hq%&h8v
z;tvDnfV>ioHw`rH!!#G1gi5We+6M=jo7sv(UevAD&7m8ds1^2-J??}n4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eE
zZH$}P0Od=Y!dnpyJL#$OQ@9WL6+7y>2nA~ki$XYO%R1l7$!*Wt+=674BHiq<)QZ{(
zyA*yXb*Z$v3$vU}>$swx;YtnBLz%cXpP{IH;B<7Q^h3s9Tie0f^%32N?jiUJcmEiW
zGZZdA;2$j7@o;-&i9%!b3SDSYs1QOzvu~sv-hlFNwR#icYyjGS)hb8y%_GcTfI096
zh)WfUf{maX^n)}Q2NPfl`~rRhkHHi06fAfYdWpz2E7Stoy$U6pl}6f)3BEfzF1&w;
zhs|lBLD=K$=MhTIUY;Y~0adW2MWIvR2KXK$jWL7Vzf7S6U>y7iUV*0N3aQ{CxVPL~
z;T_=@JHGRn&j!q^b>H$ULbwdET&=bt5@IuAAI|>5W9+Qo&gG3;^-sACM%H)Dmk+|u
zhphwMko;e}%FG94(tlJM^P_IIH}rBrr{J_ZQp{;G#sX1(&To7b*ct2#DRj9JSAgU7
z!&f&dfvBWI9Wo-pwfuCzNCo3H;Y_fTt1~BqKl82qxntpQXE?6J;uK5tZ0hcbby8Pn
uGLcLq%=1g*obk3fZmw;)C^TNjpS=U_gTGyjUqfpo!pNC(A+IQkNdE_plQ+r$

diff --git a/usb-loader/samba_applets/isp-project/extram/main.c b/usb-loader/samba_applets/isp-project/extram/main.c
index 8a42b4d42..ed28b89b5 100644
--- a/usb-loader/samba_applets/isp-project/extram/main.c
+++ b/usb-loader/samba_applets/isp-project/extram/main.c
@@ -83,45 +83,70 @@
 //------------------------------------------------------------------------------
 struct _Mailbox {
 
-    /// Command send to the monitor to be executed.
-    unsigned int command;
-    /// Returned status, updated at the end of the monitor execution.
-    unsigned int status;
-
-    /// Input Arguments in the argument area
-    union {
-
-        /// Input arguments for the Init command.
-        struct {
-
-            /// Communication link used.
-            unsigned int comType;
-            /// Trace level.
-            unsigned int traceLevel;
-            /// External memory voltage selection.
-            unsigned int VddMemSel;
-            /// External RAM type.
-            unsigned int ramType;
-            /// External RAM bus width.
-            unsigned int dataBusWidth;
-            /// External DDRAM Model.
-            unsigned int ddrModel;
-
-        } inputInit;
-
-        /// Output arguments for the Init command.
-        struct {
-
-            /// Memory size.
-            unsigned int memorySize;
-            /// Buffer address.
-            unsigned int bufferAddress;
-            /// Buffer size.
-            unsigned int bufferSize;
-        } outputInit;
-    } argument;
+	/// Command send to the monitor to be executed.
+	unsigned int command;
+	/// Returned status, updated at the end of the monitor execution.
+	unsigned int status;
+
+	/// Input Arguments in the argument area
+	union {
+
+		/// Input arguments for the Init command.
+		struct {
+
+			/// Communication link used.
+			unsigned int comType;
+			/// Trace level.
+			unsigned int traceLevel;
+			/// External memory voltage selection.
+			unsigned int VddMemSel;
+			/// External RAM type.
+			unsigned int ramType;
+			/// External RAM bus width.
+			unsigned int dataBusWidth;
+			/// External DDRAM Model.
+			unsigned int ddrModel;
+
+		} inputInit;
+
+		/// Output arguments for the Init command.
+		struct {
+
+			/// Memory size.
+			unsigned int memorySize;
+			/// Buffer address.
+			unsigned int bufferAddress;
+			/// Buffer size.
+			unsigned int bufferSize;
+		} outputInit;
+
+		/// Input arguments for the Write command.
+		struct {
+
+			/// Buffer address.
+			unsigned int bufferAddr;
+			/// Buffer size.
+			unsigned int bufferSize;
+			/// Memory offset.
+			unsigned int memoryOffset;
+
+		} inputWrite;
+
+		/// Output arguments for the Write command.
+		struct {
+
+			/// Bytes written.
+			unsigned int bytesWritten;
+
+		} outputWrite;
+
+	} argument;
 };
 
+//Use to jump to memory space
+typedef void VFptr(void);
+
+
 
 //------------------------------------------------------------------------------
 //         Global variables
@@ -144,22 +169,22 @@ int r_seed;
 
 void my_srand(int seed)
 {
-    r_seed = seed;
+	r_seed = seed;
 }
 
 int my_rand()
 {
-    r_seed *= 101028131;
-    r_seed += 12345;
-    return r_seed;
+	r_seed *= 101028131;
+	r_seed += 12345;
+	return r_seed;
 }
 
 extern int mem_test(unsigned long _start, unsigned long _end, unsigned long pattern_unused);
 
 static unsigned char ExtRAM_TestOk(void)
 {
-    unsigned int i;
-    unsigned int *ptr = (unsigned int *) EXTRAM_ADDR;
+	unsigned int i;
+	unsigned int *ptr = (unsigned int *) EXTRAM_ADDR;
 	TRACE_INFO( "External mem addr: %x\n\r", EXTRAM_ADDR);
 
 	return	mem_test((EXTRAM_ADDR), (EXTRAM_ADDR) + 0x4000000 - 0x4, 0) ? 0: 1;
@@ -168,128 +193,175 @@ static unsigned char ExtRAM_TestOk(void)
 
 void delay(int x)
 {
-    while(x--) asm volatile("nop");
+	while(x--) asm volatile("nop");
 }
 //------------------------------------------------------------------------------
 /// Applet code for initializing the external RAM.
 //------------------------------------------------------------------------------
 int main(int argc, char **argv)
 {
-    struct _Mailbox *pMailbox = (struct _Mailbox *) argv;
-    unsigned int ramType = 0;
-    unsigned int dataBusWidth = 0;
-    unsigned int ddrModel = 0;
-
-    LowLevelInit();
-
-    TRACE_CONFIGURE_ISP(DBGU_STANDARD, 115200, BOARD_MCK);
-    
-    
-
-/*    *AT91C_PMC_PCER = AT91C_ID_PIOA;
-    *AT91C_PIOA_PDR = (1<<31);
-    *AT91C_PIOA_OER = (1<<31);
-    *AT91C_PIOA_BSR = (1<<31);
-    *AT91C_PIOA_PER = (1<<0);
-    *AT91C_PIOA_OER = (1<<0);
-    
-    *AT91C_PMC_PCKR = (1<<8); // select master clock
-    *AT91C_PMC_SCER = (1<<8); // ENABLE PCK0*/
-    
-    TRACE_INFO("Statup: PMC_MCKR %x MCK = %d command = %d\n\r", *AT91C_PMC_MCKR, BOARD_MCK, pMailbox->command);
-    // ----------------------------------------------------------
-    // INIT:
-    // ----------------------------------------------------------
-    if (pMailbox->command == APPLET_CMD_INIT) {
-
-        // Initialize PMC
-//	BOARD_RemapRam();
-
-
-        // Enable User Reset
-        AT91C_BASE_RSTC->RSTC_RMR |= AT91C_RSTC_URSTEN | (0xA5<<24);
-        
-
-        ramType = pMailbox->argument.inputInit.ramType;
-        dataBusWidth = pMailbox->argument.inputInit.dataBusWidth;
-        ddrModel = pMailbox->argument.inputInit.ddrModel;
-
-//#if (DYN_TRACES == 1)
-//        traceLevel = pMailbox->argument.inputInit.traceLevel;
-//#endif
-
-        TRACE_INFO("-- EXTRAM ISP Applet %s --\n\r", SAM_BA_APPLETS_VERSION);
-        TRACE_INFO("-- %s\n\r", BOARD_NAME);
-        TRACE_INFO("-- Compiled: %s %s %s --\n\r", __DATE__, __TIME__, __GIT__);
-        TRACE_INFO("INIT command:\n\r");
-
-        TRACE_INFO("\tCommunication link type : %d\n\r", pMailbox->argument.inputInit.comType);
-        TRACE_INFO("\tData bus width : %d bits\n\r", dataBusWidth);
-        if (ramType == TYPE_SDRAM) {
-           TRACE_INFO("\tExternal RAM type : %s\n\r", "SDRAM");
-        }
-        else {
-            if (ramType == TYPE_DDRAM) {
-                TRACE_INFO("\tExternal RAM type : %s\n\r", "DDRAM");
-            }
-            else {
-                TRACE_INFO("\tExternal RAM type : %s\n\r", "PSRAM");
-            }
-        }
+	struct _Mailbox *pMailbox = (struct _Mailbox *) argv;
+	unsigned int ramType = 0;
+	unsigned int bufferSize, bufferAddr, memoryOffset;
+	unsigned int bytesToWrite;
+	unsigned int tempBufferAddr;
+	unsigned int dataBusWidth = 0;
+	unsigned int ddrModel = 0;
+
+	LowLevelInit();
+
+	TRACE_CONFIGURE_ISP(DBGU_STANDARD, 115200, BOARD_MCK);
+
+
+
+	/*    *AT91C_PMC_PCER = AT91C_ID_PIOA;
+	 *AT91C_PIOA_PDR = (1<<31);
+	 *AT91C_PIOA_OER = (1<<31);
+	 *AT91C_PIOA_BSR = (1<<31);
+	 *AT91C_PIOA_PER = (1<<0);
+	 *AT91C_PIOA_OER = (1<<0);
+
+	 *AT91C_PMC_PCKR = (1<<8); // select master clock
+	 *AT91C_PMC_SCER = (1<<8); // ENABLE PCK0*/
+
+	TRACE_INFO("Statup: PMC_MCKR %x MCK = %d command = %d\n\r", *AT91C_PMC_MCKR, BOARD_MCK, pMailbox->command);
+	// ----------------------------------------------------------
+	// INIT:
+	// ----------------------------------------------------------
+	if (pMailbox->command == APPLET_CMD_INIT) {
+
+		// Initialize PMC
+		//	BOARD_RemapRam();
+
+
+		// Enable User Reset
+		AT91C_BASE_RSTC->RSTC_RMR |= AT91C_RSTC_URSTEN | (0xA5<<24);
+
+
+		ramType = pMailbox->argument.inputInit.ramType;
+		dataBusWidth = pMailbox->argument.inputInit.dataBusWidth;
+		ddrModel = pMailbox->argument.inputInit.ddrModel;
+
+		//#if (DYN_TRACES == 1)
+		//        traceLevel = pMailbox->argument.inputInit.traceLevel;
+		//#endif
+
+		TRACE_INFO("-- EXTRAM ISP Applet %s --\n\r", SAM_BA_APPLETS_VERSION);
+		TRACE_INFO("-- %s\n\r", BOARD_NAME);
+		TRACE_INFO("-- Compiled: %s %s %s --\n\r", __DATE__, __TIME__, __GIT__);
+		TRACE_INFO("INIT command:\n\r");
+
+		TRACE_INFO("\tCommunication link type : %d\n\r", pMailbox->argument.inputInit.comType);
+		TRACE_INFO("\tData bus width : %d bits\n\r", dataBusWidth);
+		if (ramType == TYPE_SDRAM) {
+			TRACE_INFO("\tExternal RAM type : %s\n\r", "SDRAM");
+		}
+		else {
+			if (ramType == TYPE_DDRAM) {
+				TRACE_INFO("\tExternal RAM type : %s\n\r", "DDRAM");
+			}
+			else {
+				TRACE_INFO("\tExternal RAM type : %s\n\r", "PSRAM");
+			}
+		}
 
 #if defined(at91cap9) || defined(at91sam9m10) || defined(at91sam9g45)
-        TRACE_INFO("\tInit EBI Vdd : %s\n\r", (pMailbox->argument.inputInit.VddMemSel)?"3.3V":"1.8V");   
-        BOARD_ConfigureVddMemSel(pMailbox->argument.inputInit.VddMemSel);
+		TRACE_INFO("\tInit EBI Vdd : %s\n\r", (pMailbox->argument.inputInit.VddMemSel)?"3.3V":"1.8V");
+		BOARD_ConfigureVddMemSel(pMailbox->argument.inputInit.VddMemSel);
 #endif //defined(at91cap9)
 
-        if (pMailbox->argument.inputInit.ramType == TYPE_SDRAM) {
-            // Configure SDRAM controller
-            TRACE_INFO("\tInit SDRAM...\n\r");
+		if (pMailbox->argument.inputInit.ramType == TYPE_SDRAM) {
+			// Configure SDRAM controller
+			TRACE_INFO("\tInit SDRAM...\n\r");
 #if defined(PINS_SDRAM)
-            BOARD_ConfigureSdram(dataBusWidth);
+			BOARD_ConfigureSdram(dataBusWidth);
 #endif
-        }
-        else if (pMailbox->argument.inputInit.ramType == TYPE_PSRAM) {
-            TRACE_INFO("\tInit PSRAM...\n\r");   
+		}
+		else if (pMailbox->argument.inputInit.ramType == TYPE_PSRAM) {
+			TRACE_INFO("\tInit PSRAM...\n\r");
 #if defined(at91sam3u4)            
-            BOARD_ConfigurePsram();
+			BOARD_ConfigurePsram();
 #endif            
-        }
-        else { 
-            // Configure DDRAM controller
+		}
+		else {
+			// Configure DDRAM controller
 #if defined(at91cap9dk) || defined(at91sam9m10) || defined(at91sam9g45)
-            TRACE_INFO("\tInit DDRAM ... (model : %d)\n\r", ddrModel);
-	    BOARD_ConfigureVddMemSel(VDDMEMSEL_1V8);
-            BOARD_ConfigureDdram(0, dataBusWidth);
-//	    ddramc_hw_init();
+			TRACE_INFO("\tInit DDRAM ... (model : %d)\n\r", ddrModel);
+			BOARD_ConfigureVddMemSel(VDDMEMSEL_1V8);
+			BOARD_ConfigureDdram(0, dataBusWidth);
+			//	    ddramc_hw_init();
 #endif
-        }
-       TRACE_INFO("\tInit successful.\n\r");
-    }
-    
-    if (pMailbox->command == APPLET_CMD_WRITE || pMailbox->command == APPLET_CMD_READ) 
-    {
-	    
-	    // Test external RAM access
-	    if (ExtRAM_TestOk()) {
-		    pMailbox->status = APPLET_SUCCESS;
-	    }
-	    else {
-		    pMailbox->status = APPLET_FAIL;
-	    }
-	    
-	    pMailbox->argument.outputInit.bufferAddress = ((unsigned int) &end);
-	    pMailbox->argument.outputInit.bufferSize = 0;
-	    pMailbox->argument.outputInit.memorySize = EXTRAM_SIZE;
-    }
-    
-
-    // Acknowledge the end of command
-    TRACE_INFO("\tEnd of applet (command : %x --- status : %x)\n\r", pMailbox->command, pMailbox->status);
-
-    // Notify the host application of the end of the command processing
-    pMailbox->command = ~(pMailbox->command);
-
-    return 0;
+		}
+		TRACE_INFO("\tInit successful.\n\r");
+	}
+
+	// ----------------------------------------------------------
+	// LIST_BAD_BLOCK: (Check DDR)
+	// ----------------------------------------------------------
+	else if (pMailbox->command == APPLET_CMD_LIST_BAD_BLOCKS)
+	{
+
+		// Test external RAM access
+		if (ExtRAM_TestOk()) {
+			pMailbox->status = APPLET_SUCCESS;
+		}
+		else {
+			pMailbox->status = APPLET_FAIL;
+		}
+
+		pMailbox->argument.outputInit.bufferAddress = ((unsigned int) &end);
+		pMailbox->argument.outputInit.bufferSize = 0;
+		pMailbox->argument.outputInit.memorySize = EXTRAM_SIZE;
+	}
+
+
+	// ----------------------------------------------------------
+	// WRITE:
+	// ----------------------------------------------------------
+	else if (pMailbox->command == APPLET_CMD_WRITE)
+	{
+		memoryOffset = pMailbox->argument.inputWrite.memoryOffset;
+		bufferAddr = pMailbox->argument.inputWrite.bufferAddr;
+		bytesToWrite = pMailbox->argument.inputWrite.bufferSize;
+		tempBufferAddr = bufferAddr+memoryOffset;
+
+		TRACE_INFO("WRITE arguments : offset 0x%x, run 0x%x, of 0x%x Bytes\n\r",
+				memoryOffset, tempBufferAddr, bytesToWrite);
+
+		pMailbox->argument.outputWrite.bytesWritten = 0;
+
+		/*
+		 * We must define the following
+		 * 	-  MACH_TYPE_xxx
+		 * 	- Setup the kernel tagged list (http://www.arm.linux.org.uk/developer/booting.php#4)
+		 * 		 first 16KiB of RAM.
+		 *
+		 * 		we recommend to load at 32KiB into RAM
+		 */
+
+		//Fake the end of the applet because we will not be able to do anything after this step
+		if (bytesToWrite < EXTRAM_SIZE)
+		{
+				pMailbox->status = APPLET_SUCCESS;
+		}
+		else
+		{
+			pMailbox->status = APPLET_FAIL;
+		}
+		pMailbox->command = ~(pMailbox->command);
+
+		//Going to
+		((VFptr *)(EXTRAM_ADDR+memoryOffset))();
+
+	}
+
+exit :
+	// Acknowledge the end of command
+	TRACE_INFO("\tEnd of applet (command : %x --- status : %x)\n\r", pMailbox->command, pMailbox->status);
+
+	// Notify the host application of the end of the command processing
+	pMailbox->command = ~(pMailbox->command);
+
+	return 0;
 }
 
-- 
GitLab