From 3036fe4787ebaf9c456bc5ce67e423b4163d5e9c Mon Sep 17 00:00:00 2001
From: Nick Saulnier <nsaulnier@ti.com>
Date: Thu, 16 Feb 2023 21:48:06 -0600
Subject: [PATCH] Initial McSPI Debug project

SysConfig changes:
enable Linux IPC
add memory logging (so debug prints should be visible in Linux
/sys/kernel/debug/remoteproc/remoteprocX/trace0)
move debug UART from instance 0 to instance 1

linker.cmd changes:
move all data that was stored in SRAM into DDR and memory internal to the R5F
subsystem. This makes sure no initial SRAM conflicts with Linux. Linux remoteproc
driver may or may not be able to handle putting data into SRAM at this point,
we found a bug in the driver in November 2022 that might not have been fixed
before the SDK 8.5 release.
allocate DDR location for the resource table.

Signed-off-by: Nick Saulnier <nsaulnier@ti.com>
---
 .../am64x-sk/r5fss0-0_freertos/example.syscfg | 14 ++++---
 .../r5fss0-0_freertos/ti-arm-clang/linker.cmd | 38 ++++++++++++++-----
 2 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/example.syscfg b/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/example.syscfg
index 7ae767de78bd..8b3bed7e4e6b 100644
--- a/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/example.syscfg
+++ b/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/example.syscfg
@@ -1,13 +1,14 @@
 /**
  * These arguments were used when this file was generated. They will be automatically applied on subsequent loads
  * via the GUI or CLI. Run CLI with '--help' for additional information on how to override these arguments.
- * @cliArgs --device "AM64x_beta" --package "ALV" --part "Default" --context "r5fss0-0" --product "MCU_PLUS_SDK@07.03.01"
- * @versions {"tool":"1.10.0+2127"}
+ * @cliArgs --device "AM64x_beta" --package "ALV" --part "Default" --context "r5fss0-0" --product "MCU_PLUS_SDK_AM64x@08.05.00"
+ * @versions {"tool":"1.14.0+2667"}
  */
 
 /**
  * Import the modules used in this configuration.
  */
+const ipc        = scripting.addModule("/drivers/ipc/ipc");
 const mcspi      = scripting.addModule("/drivers/mcspi/mcspi", {}, false);
 const mcspi1     = mcspi.addInstance();
 const udma       = scripting.addModule("/drivers/udma/udma", {}, false);
@@ -24,6 +25,8 @@ const mpu_armv76 = mpu_armv7.addInstance();
 /**
  * Write custom configuration values to the imported modules.
  */
+ipc.enableLinuxIpc = true;
+
 mcspi1.$name                       = "CONFIG_MCSPI0";
 mcspi1.inputSelect                 = "0";
 mcspi1.intrEnable                  = "DMA";
@@ -36,8 +39,9 @@ mcspi1.udmaDriver = udma1;
 udma1.instance    = "PKTDMA_0";
 
 debug_log.enableUartLog        = true;
+debug_log.enableMemLog         = true;
 debug_log.uartLog.$name        = "CONFIG_UART_CONSOLE";
-debug_log.uartLog.UART.$assign = "USART0";
+debug_log.uartLog.UART.$assign = "USART1";
 
 mpu_armv71.$name             = "CONFIG_MPU_REGION0";
 mpu_armv71.size              = 31;
@@ -76,5 +80,5 @@ mpu_armv76.size     = 31;
 mcspi1.SPI.CLK.$suggestSolution             = "ball.D13";
 mcspi1.SPI.D0.$suggestSolution              = "ball.A13";
 mcspi1.SPI.D1.$suggestSolution              = "ball.A14";
-debug_log.uartLog.UART.RXD.$suggestSolution = "ball.D15";
-debug_log.uartLog.UART.TXD.$suggestSolution = "ball.C16";
+debug_log.uartLog.UART.RXD.$suggestSolution = "ball.E15";
+debug_log.uartLog.UART.TXD.$suggestSolution = "ball.E14";
diff --git a/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/ti-arm-clang/linker.cmd b/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/ti-arm-clang/linker.cmd
index 17195c6a4146..7b3294968e0b 100644
--- a/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/ti-arm-clang/linker.cmd
+++ b/examples/drivers/mcspi/mcspi_loopback_dma/am64x-sk/r5fss0-0_freertos/ti-arm-clang/linker.cmd
@@ -47,18 +47,26 @@ SECTIONS
         .text.mpu: palign(8)
         .text.boot: palign(8)
         .text:abort: palign(8) /* this helps in loading symbols when using XIP mode */
-    } > MSRAM
+    } > R5F_TCMA
+/* place in R5F TCMA to avoid Linux SRAM conflicts */
 
     /* This is rest of code. This can be placed in DDR if DDR is available and needed */
     GROUP {
         .text:   {} palign(8)   /* This is where code resides */
         .rodata: {} palign(8)   /* This is where const's go */
-    } > MSRAM
+    } > DDR_1
+/* place in DDR just to make sure no Linux SRAM conflicts in initial test */
+
+    GROUP {
+        /* This is the resource table used by linux to know where the IPC "VRINGs" are located */
+        .resource_table: {} palign(4096)
+    } > DDR_0
 
     /* This is rest of initialized data. This can be placed in DDR if DDR is available and needed */
     GROUP {
         .data:   {} palign(8)   /* This is where initialized globals and static go */
-    } > MSRAM
+    } > DDR_1
+/* place in DDR just to make sure no Linux SRAM conflicts in initial test */
 
     /* This is rest of uninitialized data. This can be placed in DDR if DDR is available and needed */
     GROUP {
@@ -67,7 +75,8 @@ SECTIONS
         RUN_END(__BSS_END)
         .sysmem: {} palign(8)   /* This is where the malloc heap goes */
         .stack:  {} palign(8)   /* This is where the main() stack goes */
-    } > MSRAM
+    } > DDR_1
+/* place in DDR just to make sure no Linux SRAM conflicts in initial test */
 
     /* This is where the stacks for different R5F modes go */
     GROUP {
@@ -86,14 +95,16 @@ SECTIONS
         .undefinedstack: {. = . + __UNDEFINED_STACK_SIZE;} align(8)
         RUN_START(__UNDEFINED_STACK_START)
         RUN_END(__UNDEFINED_STACK_END)
-    } > MSRAM
+    } > DDR_1
+/* place in DDR just to make sure no Linux SRAM conflicts in initial test */
 
     /* Sections needed for C++ projects */
     GROUP {
         .ARM.exidx:  {} palign(8)   /* Needed for C++ exception handling */
         .init_array: {} palign(8)   /* Contains function pointers called before main */
         .fini_array: {} palign(8)   /* Contains function pointers called after main */
-    } > MSRAM
+    } > DDR_1
+/* place in DDR just to make sure no Linux SRAM conflicts in initial test */
 
     /* General purpose user shared memory, used in some examples */
     .bss.user_shared_mem (NOLOAD) : {} > USER_SHM_MEM
@@ -132,17 +143,26 @@ MEMORY
      * this memory does not overlap with other R5F's
      */
     MSRAM     : ORIGIN = 0x70080000 , LENGTH = 0x50000
+/* MSRAM unused in this initial example to avoid conflicts */
+/* could be used in the actual design as long as SRAM was appropriately divided up */
 
     /* This section can be used to put XIP section of the application in flash, make sure this does not overlap with
      * other CPUs. Also make sure to add a MPU entry for this section and mark it as cached and code executable
      */
     FLASH     : ORIGIN = 0x60100000 , LENGTH = 0x80000
 
+    /* Resource table must be placed at the start of DDR_0 when R5 cores are early booting with Linux */
+    DDR_0       : ORIGIN = 0xA0100000 , LENGTH = 0x1000
+    DDR_1       : ORIGIN = 0xA0101000 , LENGTH = 0xEFF000
+
+
     /* shared memory segments */
     /* On R5F,
      * - make sure there is a MPU entry which maps below regions as non-cache
      */
-    USER_SHM_MEM            : ORIGIN = 0x701D0000, LENGTH = 0x80
-    LOG_SHM_MEM             : ORIGIN = 0x701D0000 + 0x80, LENGTH = 0x00004000 - 0x80
-    RTOS_NORTOS_IPC_SHM_MEM : ORIGIN = 0x701D4000, LENGTH = 0x0000C000
+    USER_SHM_MEM            : ORIGIN = 0xA5000000, LENGTH = 0x80
+    LOG_SHM_MEM             : ORIGIN = 0xA5000000 + 0x80, LENGTH = 0x00004000 - 0x80
+    RTOS_NORTOS_IPC_SHM_MEM : ORIGIN = 0xA5004000, LENGTH = 0x0000C000
+    LINUX_IPC_SHM_MEM       : ORIGIN = 0xA0000000 , LENGTH = 0x100000
 }
+/* move to DDR region rtos_ipc_memory_region to ensure no SRAM conflicts */
-- 
2.17.1

