Re: [PATCH u-boot-mvebu v3 02/18] arm: mvebu: turris_omnia: Add header containing MCU command interface and use it

2024-03-28 Thread Stefan Roese

On 3/27/24 17:23, Marek Behún wrote:

Add header containing all MCU command definitions and use it in board
code and in MCU driver.

Signed-off-by: Marek Behún 


Reviewed-by: Stefan Roese 

Thanks,
Stefan


---
  board/CZ.NIC/turris_omnia/turris_omnia.c |  81 +++-
  drivers/gpio/turris_omnia_mcu.c  |  54 +
  include/turris-omnia-mcu-interface.h | 248 +++
  3 files changed, 272 insertions(+), 111 deletions(-)
  create mode 100644 include/turris-omnia-mcu-interface.h

diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c 
b/board/CZ.NIC/turris_omnia/turris_omnia.c
index adeb69a205..6c2d7da528 100644
--- a/board/CZ.NIC/turris_omnia/turris_omnia.c
+++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
@@ -23,6 +23,7 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #include 
  #include 
@@ -59,46 +60,6 @@ DECLARE_GLOBAL_DATA_PTR;
  #define A385_WD_RSTOUT_UNMASK MVEBU_REGISTER(0x20704)
  #define   A385_WD_RSTOUT_UNMASK_GLOBALBIT(8)
  
-enum mcu_commands {

-   CMD_GET_STATUS_WORD = 0x01,
-   CMD_GET_RESET   = 0x09,
-   CMD_GET_FW_VERSION_APP  = 0x0a,
-   CMD_WATCHDOG_STATE  = 0x0b,
-   CMD_GET_FW_VERSION_BOOT = 0x0e,
-
-   /* available if STS_FEATURES_SUPPORTED bit set in status word */
-   CMD_GET_FEATURES= 0x10,
-
-   /* available if EXT_CMD bit set in features */
-   CMD_EXT_CONTROL = 0x12,
-};
-
-enum status_word_bits {
-   STS_MCU_TYPE_MASK   = GENMASK(1, 0),
-   STS_MCU_TYPE_STM32  = 0,
-   STS_MCU_TYPE_GD32   = 1,
-   STS_MCU_TYPE_MKL= 2,
-   STS_MCU_TYPE_UNKN   = 3,
-   STS_FEATURES_SUPPORTED  = BIT(2),
-   CARD_DET_STSBIT = 0x0010,
-   MSATA_IND_STSBIT= 0x0020,
-};
-
-/* CMD_GET_FEATURES */
-enum features_e {
-   FEAT_PERIPH_MCU = BIT(0),
-   FEAT_EXT_CMDS   = BIT(1),
-};
-
-/* CMD_EXT_CONTROL */
-enum ext_ctl_e {
-   EXT_CTL_nRES_LAN= BIT(1),
-   EXT_CTL_nRES_PHY= BIT(2),
-   EXT_CTL_nPERST0 = BIT(3),
-   EXT_CTL_nPERST1 = BIT(4),
-   EXT_CTL_nPERST2 = BIT(5),
-};
-
  /*
   * Those values and defines are taken from the Marvell U-Boot version
   * "u-boot-2013.01-2014_T3.0"
@@ -219,7 +180,7 @@ static bool disable_mcu_watchdog(void)
  
  	puts("Disabling MCU watchdog... ");
  
-	ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);

+   ret = omnia_mcu_write(CMD_SET_WATCHDOG_STATE, "\x00", 1);
if (ret) {
printf("omnia_mcu_write failed: %i\n", ret);
return false;
@@ -256,17 +217,17 @@ static bool omnia_detect_sata(const char *msata_slot)
return false;
}
  
-	if (!(stsword & CARD_DET_STSBIT)) {

+   if (!(stsword & STS_CARD_DET)) {
puts("none\n");
return false;
}
  
-	if (stsword & MSATA_IND_STSBIT)

+   if (stsword & STS_MSATA_IND)
puts("mSATA\n");
else
puts("MiniPCIe\n");
  
-	return stsword & MSATA_IND_STSBIT ? true : false;

+   return stsword & STS_MSATA_IND;
  }
  
  static bool omnia_detect_wwan_usb3(const char *wwan_slot)

@@ -393,18 +354,7 @@ static int omnia_get_ram_size_gb(void)
  
  static const char * const omnia_get_mcu_type(void)

  {
-   static const char * const mcu_types[] = {
-   [STS_MCU_TYPE_STM32] = "STM32",
-   [STS_MCU_TYPE_GD32]  = "GD32",
-   [STS_MCU_TYPE_MKL]   = "MKL",
-   [STS_MCU_TYPE_UNKN]  = "unknown",
-   };
-   static const char * const mcu_types_with_perip_resets[] = {
-   [STS_MCU_TYPE_STM32] = "STM32 (with peripheral resets)",
-   [STS_MCU_TYPE_GD32]  = "GD32 (with peripheral resets)",
-   [STS_MCU_TYPE_MKL]   = "MKL (with peripheral resets)",
-   [STS_MCU_TYPE_UNKN]  = "unknown (with peripheral resets)",
-   };
+   static char result[] = "xxx (with peripheral resets)";
u16 stsword, features;
int ret;
  
@@ -412,13 +362,28 @@ static const char * const omnia_get_mcu_type(void)

if (ret)
return "unknown";
  
+	switch (stsword & STS_MCU_TYPE_MASK) {

+   case STS_MCU_TYPE_STM32:
+   strcpy(result, "STM32");
+   break;
+   case STS_MCU_TYPE_GD32:
+   strcpy(result, "GD32");
+   break;
+   case STS_MCU_TYPE_MKL:
+   strcpy(result, "MKL");
+   break;
+   default:
+   strcpy(result, "unknown");
+   break;
+   }
+
if (stsword & STS_FEATURES_SUPPORTED) {
ret = omnia_mcu_read(CMD_GET_FEATURES, &features, 
sizeof(features));
if (ret == 0 && (features & FEAT_PERIPH_MCU))
-   return mcu_types_with_perip_resets[stsword & 
STS_MCU_TYPE_MASK];
+   strcat(result, " (with peripheral resets)");
   

[PATCH u-boot-mvebu v3 02/18] arm: mvebu: turris_omnia: Add header containing MCU command interface and use it

2024-03-27 Thread Marek Behún
Add header containing all MCU command definitions and use it in board
code and in MCU driver.

Signed-off-by: Marek Behún 
---
 board/CZ.NIC/turris_omnia/turris_omnia.c |  81 +++-
 drivers/gpio/turris_omnia_mcu.c  |  54 +
 include/turris-omnia-mcu-interface.h | 248 +++
 3 files changed, 272 insertions(+), 111 deletions(-)
 create mode 100644 include/turris-omnia-mcu-interface.h

diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c 
b/board/CZ.NIC/turris_omnia/turris_omnia.c
index adeb69a205..6c2d7da528 100644
--- a/board/CZ.NIC/turris_omnia/turris_omnia.c
+++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
@@ -23,6 +23,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -59,46 +60,6 @@ DECLARE_GLOBAL_DATA_PTR;
 #define A385_WD_RSTOUT_UNMASK  MVEBU_REGISTER(0x20704)
 #define   A385_WD_RSTOUT_UNMASK_GLOBAL BIT(8)
 
-enum mcu_commands {
-   CMD_GET_STATUS_WORD = 0x01,
-   CMD_GET_RESET   = 0x09,
-   CMD_GET_FW_VERSION_APP  = 0x0a,
-   CMD_WATCHDOG_STATE  = 0x0b,
-   CMD_GET_FW_VERSION_BOOT = 0x0e,
-
-   /* available if STS_FEATURES_SUPPORTED bit set in status word */
-   CMD_GET_FEATURES= 0x10,
-
-   /* available if EXT_CMD bit set in features */
-   CMD_EXT_CONTROL = 0x12,
-};
-
-enum status_word_bits {
-   STS_MCU_TYPE_MASK   = GENMASK(1, 0),
-   STS_MCU_TYPE_STM32  = 0,
-   STS_MCU_TYPE_GD32   = 1,
-   STS_MCU_TYPE_MKL= 2,
-   STS_MCU_TYPE_UNKN   = 3,
-   STS_FEATURES_SUPPORTED  = BIT(2),
-   CARD_DET_STSBIT = 0x0010,
-   MSATA_IND_STSBIT= 0x0020,
-};
-
-/* CMD_GET_FEATURES */
-enum features_e {
-   FEAT_PERIPH_MCU = BIT(0),
-   FEAT_EXT_CMDS   = BIT(1),
-};
-
-/* CMD_EXT_CONTROL */
-enum ext_ctl_e {
-   EXT_CTL_nRES_LAN= BIT(1),
-   EXT_CTL_nRES_PHY= BIT(2),
-   EXT_CTL_nPERST0 = BIT(3),
-   EXT_CTL_nPERST1 = BIT(4),
-   EXT_CTL_nPERST2 = BIT(5),
-};
-
 /*
  * Those values and defines are taken from the Marvell U-Boot version
  * "u-boot-2013.01-2014_T3.0"
@@ -219,7 +180,7 @@ static bool disable_mcu_watchdog(void)
 
puts("Disabling MCU watchdog... ");
 
-   ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
+   ret = omnia_mcu_write(CMD_SET_WATCHDOG_STATE, "\x00", 1);
if (ret) {
printf("omnia_mcu_write failed: %i\n", ret);
return false;
@@ -256,17 +217,17 @@ static bool omnia_detect_sata(const char *msata_slot)
return false;
}
 
-   if (!(stsword & CARD_DET_STSBIT)) {
+   if (!(stsword & STS_CARD_DET)) {
puts("none\n");
return false;
}
 
-   if (stsword & MSATA_IND_STSBIT)
+   if (stsword & STS_MSATA_IND)
puts("mSATA\n");
else
puts("MiniPCIe\n");
 
-   return stsword & MSATA_IND_STSBIT ? true : false;
+   return stsword & STS_MSATA_IND;
 }
 
 static bool omnia_detect_wwan_usb3(const char *wwan_slot)
@@ -393,18 +354,7 @@ static int omnia_get_ram_size_gb(void)
 
 static const char * const omnia_get_mcu_type(void)
 {
-   static const char * const mcu_types[] = {
-   [STS_MCU_TYPE_STM32] = "STM32",
-   [STS_MCU_TYPE_GD32]  = "GD32",
-   [STS_MCU_TYPE_MKL]   = "MKL",
-   [STS_MCU_TYPE_UNKN]  = "unknown",
-   };
-   static const char * const mcu_types_with_perip_resets[] = {
-   [STS_MCU_TYPE_STM32] = "STM32 (with peripheral resets)",
-   [STS_MCU_TYPE_GD32]  = "GD32 (with peripheral resets)",
-   [STS_MCU_TYPE_MKL]   = "MKL (with peripheral resets)",
-   [STS_MCU_TYPE_UNKN]  = "unknown (with peripheral resets)",
-   };
+   static char result[] = "xxx (with peripheral resets)";
u16 stsword, features;
int ret;
 
@@ -412,13 +362,28 @@ static const char * const omnia_get_mcu_type(void)
if (ret)
return "unknown";
 
+   switch (stsword & STS_MCU_TYPE_MASK) {
+   case STS_MCU_TYPE_STM32:
+   strcpy(result, "STM32");
+   break;
+   case STS_MCU_TYPE_GD32:
+   strcpy(result, "GD32");
+   break;
+   case STS_MCU_TYPE_MKL:
+   strcpy(result, "MKL");
+   break;
+   default:
+   strcpy(result, "unknown");
+   break;
+   }
+
if (stsword & STS_FEATURES_SUPPORTED) {
ret = omnia_mcu_read(CMD_GET_FEATURES, &features, 
sizeof(features));
if (ret == 0 && (features & FEAT_PERIPH_MCU))
-   return mcu_types_with_perip_resets[stsword & 
STS_MCU_TYPE_MASK];
+   strcat(result, " (with peripheral resets)");
}
 
-   return mcu_types[stsword & STS_MCU_TYPE_MASK];
+   return result;
 }
 
 sta