[PATCH v7 2/2] serial: zynqmp: Fetch baudrate from dtb and update

2023-09-21 Thread Venkatesh Yadav Abbarapu
From: Algapally Santosh Sagar 

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 
Signed-off-by: Venkatesh Yadav Abbarapu 
---
 doc/README.serial_dt_baud  | 41 
 drivers/core/ofnode.c  | 18 ++
 drivers/serial/Kconfig |  9 +++
 drivers/serial/serial-uclass.c | 43 ++
 include/dm/ofnode.h| 14 +--
 include/env_default.h  |  6 -
 include/serial.h   | 15 
 7 files changed, 143 insertions(+), 3 deletions(-)
 create mode 100644 doc/README.serial_dt_baud

diff --git a/doc/README.serial_dt_baud b/doc/README.serial_dt_baud
new file mode 100644
index 00..f8768d0e1b
--- /dev/null
+++ b/doc/README.serial_dt_baud
@@ -0,0 +1,41 @@
+Fetch serial baudrate from DT
+-
+
+To support fetching of baudrate from DT, the following is done:-
+
+The baudrate configured in Kconfig symbol CONFIG_BAUDRATE is taken by default 
by serial.
+If change of baudrate is required then the Kconfig symbol CONFIG_BAUDRATE 
needs to
+changed and U-Boot recompilation is required or the U-Boot environment needs 
to be updated.
+
+To avoid this, add support to fetch the baudrate directly from the device tree 
file and
+update the environment.
+
+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 which is enabled by OF_SERIAL_BAUD.
+
+The Kconfig SPL_ENV_SUPPORT needs to be enabled to allow patching in SPL.
+
+The Kconfig DEFAULT_ENV_IS_RW which is enabled by OF_SERIAL_BAUD with making 
the environment
+writable.
+
+The ofnode_read_baud() function parses and fetches the baudrate value from the 
DT. This value
+is validated and updated to baudrate during serial init. Padding is added at 
the end of the
+default environment and the dt baudrate is updated with the latest value.
+
+Example:-
+
+The serial port options are of the form "pnf", where "" is the baud 
rate, "p" is parity ("n", "o", or "e"),
+"n" is number of bits, and "f" is flow control ("r" for RTS or omit it). 
Default is "115200n8".
+
+chosen {
+   bootargs = "earlycon console=ttyPS0,115200 clk_ignore_unused 
root=/dev/ram0 rw init_fatal_sh=1";
+   stdout-path = "serial0:115200n8";
+   };
+
+From the chosen node, stdout-path property is obtained as string.
+
+   stdout-path = "serial0:115200n8";
+
+The string is parsed to get the baudrate 115200. This string is converted to 
integer and updated to the environment.
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 8df16e56af..42f51ca93c 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -895,6 +895,24 @@ ofnode ofnode_get_chosen_node(const char *name)
return ofnode_path(prop);
 }
 
+int ofnode_read_baud(void)
+{
+   const char *str, *p;
+   u32 baud;
+
+   str = ofnode_read_chosen_string("stdout-path");
+   if (!str)
+   return -EINVAL;
+
+   /* Parse string serial0:115200n8 */
+   p = strchr(str, ':');
+   if (!p)
+   return -EINVAL;
+
+   baud = dectoul(p + 1, NULL);
+   return baud;
+}
+
 const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
 {
ofnode node;
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 4d27034c3f..7fdf600dc5 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -24,6 +24,15 @@ 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 OF_SERIAL_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 

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

2023-09-21 Thread Michal Simek




On 9/21/23 06:21, Venkatesh Yadav Abbarapu wrote:

From: Algapally Santosh Sagar 

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 
Signed-off-by: Venkatesh Yadav Abbarapu 
---
  doc/README.serial_dt_baud  | 41 
  drivers/core/ofnode.c  | 18 ++
  drivers/serial/Kconfig |  9 +++
  drivers/serial/serial-uclass.c | 43 ++
  include/dm/ofnode.h| 14 +--
  include/env_default.h  |  6 -
  include/serial.h   | 15 
  7 files changed, 143 insertions(+), 3 deletions(-)
  create mode 100644 doc/README.serial_dt_baud

diff --git a/doc/README.serial_dt_baud b/doc/README.serial_dt_baud
new file mode 100644
index 00..f8768d0e1b
--- /dev/null
+++ b/doc/README.serial_dt_baud
@@ -0,0 +1,41 @@
+Fetch serial baudrate from DT
+-
+
+To support fetching of baudrate from DT, the following is done:-
+
+The baudrate configured in Kconfig symbol CONFIG_BAUDRATE is taken by default 
by serial.
+If change of baudrate is required then the Kconfig symbol CONFIG_BAUDRATE 
needs to
+changed and U-Boot recompilation is required or the U-Boot environment needs 
to be updated.
+
+To avoid this, add support to fetch the baudrate directly from the device tree 
file and
+update the environment.
+
+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 which is enabled by OF_SERIAL_BAUD.
+
+The Kconfig SPL_ENV_SUPPORT needs to be enabled to allow patching in SPL.
+
+The Kconfig DEFAULT_ENV_IS_RW which is enabled by OF_SERIAL_BAUD with making 
the environment
+writable.
+
+The ofnode_read_baud() function parses and fetches the baudrate value from the 
DT. This value
+is validated and updated to baudrate during serial init. Padding is added at 
the end of the
+default environment and the dt baudrate is updated with the latest value.
+
+Example:-
+
+The serial port options are of the form "pnf", where "" is the baud rate, "p" is parity 
("n", "o", or "e"),
+"n" is number of bits, and "f" is flow control ("r" for RTS or omit it). Default is 
"115200n8".
+
+chosen {
+   bootargs = "earlycon console=ttyPS0,115200 clk_ignore_unused 
root=/dev/ram0 rw init_fatal_sh=1";
+   stdout-path = "serial0:115200n8";
+   };
+
+From the chosen node, stdout-path property is obtained as string.
+
+   stdout-path = "serial0:115200n8";
+
+The string is parsed to get the baudrate 115200. This string is converted to 
integer and updated to the environment.
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 8df16e56af..42f51ca93c 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -895,6 +895,24 @@ ofnode ofnode_get_chosen_node(const char *name)
return ofnode_path(prop);
  }
  
+int ofnode_read_baud(void)


I expect Simon will want to have test to cover this too.


+{
+   const char *str, *p;
+   u32 baud;
+
+   str = ofnode_read_chosen_string("stdout-path");
+   if (!str)
+   return -EINVAL;
+
+   /* Parse string serial0:115200n8 */
+   p = strchr(str, ':');
+   if (!p)
+   return -EINVAL;
+
+   baud = dectoul(p + 1, NULL);
+   return baud;
+}
+
  const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
  {
ofnode node;
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 4d27034c3f..7fdf600dc5 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -24,6 +24,15 @@ 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 OF_SERIAL_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 t