Skip to contentSkip to conversation

Add support to fetch baudrate from dtb

PATCH Superseded Venkatesh Yadav Abbarapu 2 participants 5 messages Apr 17, 2023

Subsystems (2)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support to fetch baudrate from dtb

From: Algapally Santosh Sagar <[email protected]>

In this patch series
-Fetch baudrate from the dtb and update
-Add CONFIG_DEFAULT_ENV_IS_RW config for armada boards

Changes in v2:
- Changed to #ifdef from #if CONFIG_IS_ENABLED to enable patching in spl.
- Added SPL_ENV_SUPPORT dependency in SERIAL_DT_BAUD to allow SPL
compilation.
- Moved DEFAULT_ENV_IS_RW to Kconfig also updated armada files
- Moved ENV_RW_FILLER to generic file env_default.h.
- Increased the ENV_RW_FILLER padding to support 8000000 baud.

Algapally Santosh Sagar (2):
configs: Add CONFIG_DEFAULT_ENV_IS_RW config for armada
serial: zynqmp: Fetch baudrate from dtb and update

 configs/mvebu_db-88f3720_defconfig          |  1 +
 configs/mvebu_espressobin-88f3720_defconfig |  1 +
 configs/mvebu_mcbin-88f8040_defconfig       |  1 +
 drivers/serial/Kconfig                      | 16 +++++++++
 drivers/serial/serial-uclass.c              | 32 +++++++++++++++++
 include/configs/mvebu_armada-37xx.h         |  1 -
 include/configs/xilinx_zynqmp.h             |  3 +-
 include/env_default.h                       |  7 +++-
 include/env_internal.h                      |  2 +-
 include/fdtdec.h                            |  8 +++++
 include/serial.h                            |  1 +
 lib/fdtdec.c                                | 40 +++++++++++++++++++++
 12 files changed, 109 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 serial: zynqmp: Fetch baudrate from dtb and update

From: Algapally Santosh Sagar <[email protected]>

The baudrate configured in .config is taken by default by serial. If change of baudrate is required then the .config needs to changed and u-boot recompilation is required or the u-boot environment needs to be updated.

To avoid this, support is added to fetch the baudrate directly from the device tree file and update.
The serial, prints the log with the configured baudrate in the dtb. The commit c4df0f6f315c ("arm: mvebu: Espressobin: Set default value for $fdtfile env variable") is taken as reference for changing the default environment variable.

The default environment stores the default baudrate value, When default baudrate and dtb baudrate are not same glitches are seen on the serial. So, the environment also needs to be updated with the dtb baudrate to avoid the glitches on the serial.

---
 drivers/serial/Kconfig              | 16 ++++++++++++
 drivers/serial/serial-uclass.c      | 32 +++++++++++++++++++++++
 include/configs/mvebu_armada-37xx.h |  1 -
 include/configs/xilinx_zynqmp.h     |  3 ++-
 include/env_default.h               |  7 ++++-
 include/env_internal.h              |  2 +-
 include/fdtdec.h                    |  8 ++++++
 include/serial.h                    |  1 +
 lib/fdtdec.c                        | 40 +++++++++++++++++++++++++++++
 9 files changed, 106 insertions(+), 4 deletions(-)
drivers/serial/Kconfig +16 -0
index 10d07daf27..96cea87f45 100644
@@ -24,6 +24,22 @@ config BAUDRATE
 	  in the SPL stage (most drivers) or for choosing a default baudrate
 	  in the absence of an environment setting (serial_mxc.c).
 
+config SERIAL_DT_BAUD
+	bool "Fetch serial baudrate from device tree"
+	depends on DM_SERIAL && SPL_ENV_SUPPORT
+	select DEFAULT_ENV_IS_RW
+	help
+	  Select this to enable fetching and setting of the baudrate
+	  configured in the DT. Replace the default baudrate with the DT
+	  baudrate and also set it to the environment.
+
+config DEFAULT_ENV_IS_RW
+	bool "Make default environment as writable"
+	depends on DM_SERIAL
+	help
+	  Select this to enable to make default environment writable. This
+	  allows modifying the default environment.
+
 config REQUIRE_SERIAL_CONSOLE
 	bool "Require a serial port for console"
 	# Running without a serial console is not supported by the
drivers/serial/serial-uclass.c +32 -0
index 067fae2614..d77d3bda36 100644
@@ -154,12 +154,44 @@ static void serial_find_console_or_panic(void)
 }
 #endif /* CONFIG_SERIAL_PRESENT */
 
+#ifdef CONFIG_SERIAL_DT_BAUD
+int serial_get_valid_baudrate(int baud)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
+		if (baud == baudrate_table[i])
+			return 0;
+	}
+
+	return -EINVAL;
+}
+#endif
+
 /* Called prior to relocation */
 int serial_init(void)
 {
 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
 	serial_find_console_or_panic();
 	gd->flags |= GD_FLG_SERIAL_READY;
+#ifdef CONFIG_SERIAL_DT_BAUD
+	int ret = 0;
+	char *ptr = &default_environment[0];
+
+	/*
+	 * Fetch the baudrate from the dtb and update the value in the
+	 * default environment.
+	 */
+	ret = fdtdec_get_baud_from_dtb(gd->fdt_blob);
+	if (ret != -EINVAL && ret != -EFAULT) {
+		gd->baudrate = ret;
+
+		while (*ptr != '\0' && *(ptr + 1) != '\0')
+			ptr++;
+		ptr += 2;
+		sprintf(ptr, "baudrate=%d", gd->baudrate);
+	}
+#endif
 	serial_setbrg();
 #endif
 
include/configs/mvebu_armada-37xx.h +0 -1
index 76e148f55e..18b55be0d8 100644
@@ -30,7 +30,6 @@
 /*
  * Environment
  */
-#define DEFAULT_ENV_IS_RW		/* required for configuring default fdtfile= */
 
 #ifdef CONFIG_MMC
 #define BOOT_TARGET_DEVICES_MMC(func, i) func(MMC, mmc, i)
include/configs/xilinx_zynqmp.h +2 -1
index 011f0034c5..8620bda489 100644
@@ -176,7 +176,8 @@
 #ifndef CFG_EXTRA_ENV_SETTINGS
 #define CFG_EXTRA_ENV_SETTINGS \
 	ENV_MEM_LAYOUT_SETTINGS \
-	BOOTENV
+	BOOTENV \
+	ENV_RW_FILLER
 #endif
 
 /* SPL can't handle all huge variables - define just DFU */
include/env_default.h +6 -1
index c0df39d62f..8ff0422ccf 100644
@@ -23,7 +23,7 @@ env_t embedded_environment __UBOOT_ENV_SECTION__(environment) = {
 	{
 #elif defined(DEFAULT_ENV_INSTANCE_STATIC)
 static char default_environment[] = {
-#elif defined(DEFAULT_ENV_IS_RW)
+#elif defined(CONFIG_DEFAULT_ENV_IS_RW)
 char default_environment[] = {
 #else
 const char default_environment[] = {
@@ -47,6 +47,11 @@ const char default_environment[] = {
 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
 	"baudrate="	__stringify(CONFIG_BAUDRATE)	"\0"
 #endif
+#ifdef CONFIG_SERIAL_DT_BAUD
+#define ENV_RW_FILLER "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+#else
+#define ENV_RW_FILLER
+#endif
 #ifdef	CONFIG_LOADS_ECHO
 	"loads_echo="	__stringify(CONFIG_LOADS_ECHO)	"\0"
 #endif
include/env_internal.h +1 -1
index 6a69494646..fcb464263f 100644
@@ -89,7 +89,7 @@ typedef struct environment_s {
 extern env_t embedded_environment;
 #endif /* ENV_IS_EMBEDDED */
 
-#ifdef DEFAULT_ENV_IS_RW
+#ifdef CONFIG_DEFAULT_ENV_IS_RW
 extern char default_environment[];
 #else
 extern const char default_environment[];
include/fdtdec.h +8 -0
index aa61a0fca1..48937a7a46 100644
@@ -657,6 +657,14 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
  */
 int fdtdec_get_alias_highest_id(const void *blob, const char *base);
 
+/**
+ * Get baudrate from the dtb
+ *
+ * @param blob		Device tree blob (if NULL, then error is returned)
+ * @return Baud rate value, or -ve error .
+ */
+int fdtdec_get_baud_from_dtb(const void *blob);
+
 /**
  * Get a property from the /chosen node
  *
include/serial.h +1 -0
index 42bdf3759c..48834b517c 100644
@@ -337,6 +337,7 @@ int serial_setconfig(struct udevice *dev, uint config);
  */
 int serial_getinfo(struct udevice *dev, struct serial_device_info *info);
 
+int serial_get_valid_baudrate(int baud);
 void atmel_serial_initialize(void);
 void mcf_serial_initialize(void);
 void mpc85xx_serial_initialize(void);
lib/fdtdec.c +40 -0
index 0827e16859..2fcc12148e 100644
@@ -570,6 +570,46 @@ int fdtdec_get_alias_highest_id(const void *blob, const char *base)
 	return max;
 }
 
+#ifdef CONFIG_SERIAL_DT_BAUD
+int fdtdec_get_baud_from_dtb(const void *blob)
+{
+	const char *str, *p, *baud_start;
+	u32 baud;
+
+	if (!blob)
+		return -EFAULT;
+
+	str = fdtdec_get_chosen_prop(blob, "stdout-path");
+	if (!str)
+		return -EINVAL;
+
+	p = strchr(str, ':');
+	if (!p)
+		return -EINVAL;
+
+	baud = 0;
+	baud_start = p + 1;
+	while (*baud_start != '\0') {
+		/*
+		 * Uart binding is <baud>{<parity>{<bits>{...}}}
+		 * So the baudrate either is the whole string, or
+		 * ends in the parity characters.
+		 */
+		if (*baud_start == 'n' || *baud_start == 'o' ||
+		    *baud_start == 'e')
+			break;
+
+		baud = baud * 10 + (*baud_start - '0');
+		baud_start++;
+	}
+
+	if (serial_get_valid_baudrate(baud) == -EINVAL)
+		return -EINVAL;
+
+	return baud;
+}
+#endif
+
 const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
 {
 	int chosen_node;
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] serial: zynqmp: Fetch baudrate from dtb and update

Quoted 46 lines · Venkatesh Yadav Abbarapu

From: Algapally Santosh Sagar <[email protected]>

The baudrate configured in .config is taken by default by serial. If change of baudrate is required then the .config needs to changed and u-boot recompilation is required or the u-boot environment needs to be updated.

To avoid this, support is added to fetch the baudrate directly from the device tree file and update.
The serial, prints the log with the configured baudrate in the dtb. The commit c4df0f6f315c ("arm: mvebu: Espressobin: Set default value for $fdtfile env variable") is taken as reference for changing the default environment variable.

The default environment stores the default baudrate value, When default baudrate and dtb baudrate are not same glitches are seen on the serial. So, the environment also needs to be updated with the dtb baudrate to avoid the glitches on the serial.

---
  drivers/serial/Kconfig              | 16 ++++++++++++
  drivers/serial/serial-uclass.c      | 32 +++++++++++++++++++++++
  include/configs/mvebu_armada-37xx.h |  1 -
  include/configs/xilinx_zynqmp.h     |  3 ++-
  include/env_default.h               |  7 ++++-
  include/env_internal.h              |  2 +-
  include/fdtdec.h                    |  8 ++++++
  include/serial.h                    |  1 +
  lib/fdtdec.c                        | 40 +++++++++++++++++++++++++++++
  9 files changed, 106 insertions(+), 4 deletions(-)
drivers/serial/Kconfig +5 -0
index 10d07daf27..96cea87f45 100644
@@ -24,6 +24,22 @@ config BAUDRATE
  	  in the SPL stage (most drivers) or for choosing a default baudrate
  	  in the absence of an environment setting (serial_mxc.c).
 
+config SERIAL_DT_BAUD
+	bool "Fetch serial baudrate from device tree"
+	depends on DM_SERIAL && SPL_ENV_SUPPORT
+	select DEFAULT_ENV_IS_RW
+	help

drivers/serial/Kconfig config BAUDRATE · line 41

Quoted 10 lines
drivers/serial/Kconfig +10 -0
+	  Select this to enable fetching and setting of the baudrate
+	  configured in the DT. Replace the default baudrate with the DT
+	  baudrate and also set it to the environment.
+
+config DEFAULT_ENV_IS_RW
+	bool "Make default environment as writable"
+	depends on DM_SERIAL
+	help
+	  Select this to enable to make default environment writable. This
+	  allows modifying the default environment.

This is one separate patch which is simply move to Kconfig and convert armada.

Quoted 70 lines
drivers/serial/Kconfig +1 -0
+
  config REQUIRE_SERIAL_CONSOLE
  	bool "Require a serial port for console"
  	# Running without a serial console is not supported by the
drivers/serial/serial-uclass.c +32 -0
index 067fae2614..d77d3bda36 100644
@@ -154,12 +154,44 @@ static void serial_find_console_or_panic(void)
  }
  #endif /* CONFIG_SERIAL_PRESENT */
 
+#ifdef CONFIG_SERIAL_DT_BAUD
+int serial_get_valid_baudrate(int baud)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
+		if (baud == baudrate_table[i])
+			return 0;
+	}
+
+	return -EINVAL;
+}
+#endif
+
  /* Called prior to relocation */
  int serial_init(void)
  {
  #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  	serial_find_console_or_panic();
  	gd->flags |= GD_FLG_SERIAL_READY;
+#ifdef CONFIG_SERIAL_DT_BAUD
+	int ret = 0;
+	char *ptr = &default_environment[0];
+
+	/*
+	 * Fetch the baudrate from the dtb and update the value in the
+	 * default environment.
+	 */
+	ret = fdtdec_get_baud_from_dtb(gd->fdt_blob);
+	if (ret != -EINVAL && ret != -EFAULT) {
+		gd->baudrate = ret;
+
+		while (*ptr != '\0' && *(ptr + 1) != '\0')
+			ptr++;
+		ptr += 2;
+		sprintf(ptr, "baudrate=%d", gd->baudrate);
+	}
+#endif
  	serial_setbrg();
  #endif
 
include/configs/mvebu_armada-37xx.h +0 -1
index 76e148f55e..18b55be0d8 100644
@@ -30,7 +30,6 @@
  /*
   * Environment
   */
-#define DEFAULT_ENV_IS_RW		/* required for configuring default fdtfile= */
 
  #ifdef CONFIG_MMC
  #define BOOT_TARGET_DEVICES_MMC(func, i) func(MMC, mmc, i)
include/configs/xilinx_zynqmp.h +0 -0
index 011f0034c5..8620bda489 100644

include/configs/xilinx_zynqmp.h line 180

Quoted 6 lines
include/configs/xilinx_zynqmp.h +2 -1
@@ -176,7 +176,8 @@
  #ifndef CFG_EXTRA_ENV_SETTINGS
  #define CFG_EXTRA_ENV_SETTINGS \
  	ENV_MEM_LAYOUT_SETTINGS \
-	BOOTENV
+	BOOTENV \
+	ENV_RW_FILLER

This is not what TOM was saying. You shouldn't have a need to update every board which wants this feature. Just enable it and get it. It means this should be the part of env_default.h directly.

Quoted 17 lines
include/configs/xilinx_zynqmp.h +0 -0
  #endif
 
  /* SPL can't handle all huge variables - define just DFU */
include/env_default.h +1 -1
index c0df39d62f..8ff0422ccf 100644
@@ -23,7 +23,7 @@ env_t embedded_environment __UBOOT_ENV_SECTION__(environment) = {
  	{
  #elif defined(DEFAULT_ENV_INSTANCE_STATIC)
  static char default_environment[] = {
-#elif defined(DEFAULT_ENV_IS_RW)
+#elif defined(CONFIG_DEFAULT_ENV_IS_RW)
  char default_environment[] = {
  #else
  const char default_environment[] = {

include/env_default.h const char default_environment[] = { · line 54

Quoted 8 lines
include/env_default.h +5 -0
@@ -47,6 +47,11 @@ const char default_environment[] = {
  #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
  	"baudrate="	__stringify(CONFIG_BAUDRATE)	"\0"
  #endif
+#ifdef CONFIG_SERIAL_DT_BAUD
+#define ENV_RW_FILLER "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+#else
+#define ENV_RW_FILLER
+#endif

Can you do just this here?

#ifdef CONFIG_SERIAL_DT_BAUD
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
#endif

Or if this is something what needs to be at the end just move it there.

Thanks,
Michal

#3 lore raw

PATCH v2 2/2 configs: Add CONFIG_DEFAULT_ENV_IS_RW config for armada

In reply to Venkatesh Yadav Abbarapu

From: Algapally Santosh Sagar <[email protected]>

The DEFAULT_ENV_IS_RW is moved to the Kconfig for easier configuration. Hence, the CONFIG_DEFAULT_ENV_IS_RW config is added to the defconfig files to allow enabling them for armada boards.

---
 configs/mvebu_db-88f3720_defconfig          | 1 +
 configs/mvebu_espressobin-88f3720_defconfig | 1 +
 configs/mvebu_mcbin-88f8040_defconfig       | 1 +
 3 files changed, 3 insertions(+)
configs/mvebu_db-88f3720_defconfig +1 -0
index ed0d28fd7d..e3bbaa2173 100644
@@ -21,6 +21,7 @@ CONFIG_USE_PREBOOT=y
 CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_DISPLAY_CPUINFO is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_ARCH_EARLY_INIT_R=y
 CONFIG_BOARD_EARLY_INIT_F=y
configs/mvebu_espressobin-88f3720_defconfig +1 -0
index ce696787e8..a06eb2dd42 100644
@@ -22,6 +22,7 @@ CONFIG_USE_PREBOOT=y
 CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_DISPLAY_CPUINFO is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_ARCH_EARLY_INIT_R=y
 CONFIG_BOARD_EARLY_INIT_F=y
configs/mvebu_mcbin-88f8040_defconfig +1 -0
index 058c04333a..4ee5f242f7 100644
@@ -22,6 +22,7 @@ CONFIG_USE_PREBOOT=y
 CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_DISPLAY_CPUINFO is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_ARCH_EARLY_INIT_R=y
 CONFIG_BOARD_EARLY_INIT_F=y
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH v2 2/2] configs: Add CONFIG_DEFAULT_ENV_IS_RW config for armada

Quoted 43 lines · Venkatesh Yadav Abbarapu

From: Algapally Santosh Sagar <[email protected]>

The DEFAULT_ENV_IS_RW is moved to the Kconfig for easier configuration. Hence, the CONFIG_DEFAULT_ENV_IS_RW config is added to the defconfig files to allow enabling them for armada boards.

---
  configs/mvebu_db-88f3720_defconfig          | 1 +
  configs/mvebu_espressobin-88f3720_defconfig | 1 +
  configs/mvebu_mcbin-88f8040_defconfig       | 1 +
  3 files changed, 3 insertions(+)
configs/mvebu_db-88f3720_defconfig +1 -0
index ed0d28fd7d..e3bbaa2173 100644
@@ -21,6 +21,7 @@ CONFIG_USE_PREBOOT=y
  CONFIG_SYS_CONSOLE_INFO_QUIET=y
  # CONFIG_DISPLAY_CPUINFO is not set
  # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
  CONFIG_DISPLAY_BOARDINFO_LATE=y
  CONFIG_ARCH_EARLY_INIT_R=y
  CONFIG_BOARD_EARLY_INIT_F=y
configs/mvebu_espressobin-88f3720_defconfig +1 -0
index ce696787e8..a06eb2dd42 100644
@@ -22,6 +22,7 @@ CONFIG_USE_PREBOOT=y
  CONFIG_SYS_CONSOLE_INFO_QUIET=y
  # CONFIG_DISPLAY_CPUINFO is not set
  # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
  CONFIG_DISPLAY_BOARDINFO_LATE=y
  CONFIG_ARCH_EARLY_INIT_R=y
  CONFIG_BOARD_EARLY_INIT_F=y
configs/mvebu_mcbin-88f8040_defconfig +0 -0
index 058c04333a..4ee5f242f7 100644

configs/mvebu_mcbin-88f8040_defconfig CONFIG_USE_PREBOOT=y · line 28

Quoted 7 lines
configs/mvebu_mcbin-88f8040_defconfig +1 -0
@@ -22,6 +22,7 @@ CONFIG_USE_PREBOOT=y
  CONFIG_SYS_CONSOLE_INFO_QUIET=y
  # CONFIG_DISPLAY_CPUINFO is not set
  # CONFIG_DISPLAY_BOARDINFO is not set
+CONFIG_DEFAULT_ENV_IS_RW=y
  CONFIG_DISPLAY_BOARDINFO_LATE=y
  CONFIG_ARCH_EARLY_INIT_R=y
  CONFIG_BOARD_EARLY_INIT_F=y

This smells. If this is the part of armada it should be in that patch only.

M