On 4/17/23 06:28, Venkatesh Yadav Abbarapu wrote:
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.

Signed-off-by: Algapally Santosh Sagar <[email protected]>
Signed-off-by: Venkatesh Yadav Abbarapu <[email protected]>
---
  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(-)

diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 10d07daf27..96cea87f45 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -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.

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


+
  config REQUIRE_SERIAL_CONSOLE
        bool "Require a serial port for console"
        # Running without a serial console is not supported by the
diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c
index 067fae2614..d77d3bda36 100644
--- a/drivers/serial/serial-uclass.c
+++ b/drivers/serial/serial-uclass.c
@@ -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
diff --git a/include/configs/mvebu_armada-37xx.h b/include/configs/mvebu_armada-37xx.h
index 76e148f55e..18b55be0d8 100644
--- a/include/configs/mvebu_armada-37xx.h
+++ b/include/configs/mvebu_armada-37xx.h
@@ -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)
diff --git a/include/configs/xilinx_zynqmp.h b/include/configs/xilinx_zynqmp.h
index 011f0034c5..8620bda489 100644
--- a/include/configs/xilinx_zynqmp.h
+++ b/include/configs/xilinx_zynqmp.h
@@ -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.

  #endif
/* SPL can't handle all huge variables - define just DFU */
diff --git a/include/env_default.h b/include/env_default.h
index c0df39d62f..8ff0422ccf 100644
--- a/include/env_default.h
+++ b/include/env_default.h
@@ -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

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

Reply via email to