[PATCH v2 1/3] dev/serial: Simplify some Zynq UART functions

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Mar 27 19:43:34 UTC 2024


Make the initialization and polled functions independent of the Termios
context.  This helps to implement the kernel I/O support without a dependency
on the Termios framework.
---
 bsps/aarch64/xilinx-zynqmp/console/console.c  | 23 ++++-----------
 bsps/arm/xilinx-zynq/console/debug-console.c  | 15 ++--------
 .../console/console-config.c                  | 23 ++++-----------
 .../xilinx-zynqmp/console/console-config.c    | 23 ++++-----------
 bsps/include/dev/serial/zynq-uart-regs.h      | 20 +++++++++++++
 bsps/include/dev/serial/zynq-uart.h           | 23 ---------------
 bsps/shared/dev/serial/zynq-uart-polled.c     | 28 ++++++-------------
 bsps/shared/dev/serial/zynq-uart.c            | 18 ++++++++----
 8 files changed, 62 insertions(+), 111 deletions(-)

diff --git a/bsps/aarch64/xilinx-zynqmp/console/console.c b/bsps/aarch64/xilinx-zynqmp/console/console.c
index 9ce0b1da63..1e5df997e8 100644
--- a/bsps/aarch64/xilinx-zynqmp/console/console.c
+++ b/bsps/aarch64/xilinx-zynqmp/console/console.c
@@ -45,6 +45,7 @@
 #include <bsp/irq.h>
 
 #include <dev/serial/zynq-uart.h>
+#include <dev/serial/zynq-uart-regs.h>
 
 #include <bspopts.h>
 #include <libfdt.h>
@@ -236,42 +237,30 @@ rtems_status_code console_initialize(
 
 void zynqmp_debug_console_flush(void)
 {
-  zynq_uart_reset_tx_flush(&zynqmp_uart_instances[BSP_CONSOLE_MINOR]);
+  zynq_uart_reset_tx_flush(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 static void zynqmp_debug_console_out(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_write_polled(base, c);
+  zynq_uart_write_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs, c);
 }
 
 static void zynqmp_debug_console_init(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   BSP_output_char = zynqmp_debug_console_out;
 }
 
 static void zynqmp_debug_console_early_init(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   BSP_output_char = zynqmp_debug_console_out;
   zynqmp_debug_console_out(c);
 }
 
 static int zynqmp_debug_console_in(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  return zynq_uart_read_polled(base);
+  return zynq_uart_read_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 BSP_output_char_function_type BSP_output_char = zynqmp_debug_console_early_init;
diff --git a/bsps/arm/xilinx-zynq/console/debug-console.c b/bsps/arm/xilinx-zynq/console/debug-console.c
index d398ca7988..4c636038af 100644
--- a/bsps/arm/xilinx-zynq/console/debug-console.c
+++ b/bsps/arm/xilinx-zynq/console/debug-console.c
@@ -44,24 +44,18 @@
 
 static void zynq_debug_console_out(char c)
 {
-  rtems_termios_device_context *base =
-    &zynq_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_write_polled(base, c);
+  zynq_uart_write_char_polled(zynq_uart_instances[BSP_CONSOLE_MINOR].regs, c);
 }
 
 static void zynq_debug_console_early_init(char c);
 
 static void zynq_debug_console_init(void)
 {
-  rtems_termios_device_context *base =
-    &zynq_uart_instances[BSP_CONSOLE_MINOR].base;
-
   if (BSP_output_char != zynq_debug_console_early_init) {
     return;
   }
 
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynq_uart_instances[BSP_CONSOLE_MINOR].regs);
   BSP_output_char = zynq_debug_console_out;
 }
 
@@ -73,10 +67,7 @@ static void zynq_debug_console_early_init(char c)
 
 static int zynq_debug_console_in(void)
 {
-  rtems_termios_device_context *base =
-    &zynq_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  return zynq_uart_read_polled(base);
+  return zynq_uart_read_char_polled(zynq_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 BSP_output_char_function_type BSP_output_char = zynq_debug_console_early_init;
diff --git a/bsps/arm/xilinx-zynqmp-rpu/console/console-config.c b/bsps/arm/xilinx-zynqmp-rpu/console/console-config.c
index f52e008f2b..eacf6ddcce 100644
--- a/bsps/arm/xilinx-zynqmp-rpu/console/console-config.c
+++ b/bsps/arm/xilinx-zynqmp-rpu/console/console-config.c
@@ -37,6 +37,7 @@
 
 #include <bsp/irq.h>
 #include <dev/serial/zynq-uart.h>
+#include <dev/serial/zynq-uart-regs.h>
 
 #include <bspopts.h>
 
@@ -81,41 +82,29 @@ rtems_status_code console_initialize(
 
 void zynqmp_debug_console_flush(void)
 {
-  zynq_uart_reset_tx_flush(&zynqmp_uart_instances[BSP_CONSOLE_MINOR]);
+  zynq_uart_reset_tx_flush(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 static void zynqmp_debug_console_out(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_write_polled(base, c);
+  zynq_uart_write_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs, c);
 }
 
 static void zynqmp_debug_console_init(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   BSP_output_char = zynqmp_debug_console_out;
 }
 
 static void zynqmp_debug_console_early_init(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   zynqmp_debug_console_out(c);
 }
 
 static int zynqmp_debug_console_in(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  return zynq_uart_read_polled(base);
+  return zynq_uart_read_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 BSP_output_char_function_type BSP_output_char = zynqmp_debug_console_early_init;
diff --git a/bsps/arm/xilinx-zynqmp/console/console-config.c b/bsps/arm/xilinx-zynqmp/console/console-config.c
index eadd7f11a2..ea148836a5 100644
--- a/bsps/arm/xilinx-zynqmp/console/console-config.c
+++ b/bsps/arm/xilinx-zynqmp/console/console-config.c
@@ -37,6 +37,7 @@
 
 #include <bsp/irq.h>
 #include <dev/serial/zynq-uart.h>
+#include <dev/serial/zynq-uart-regs.h>
 
 #include <bspopts.h>
 
@@ -83,41 +84,29 @@ rtems_status_code console_initialize(
 
 void zynqmp_debug_console_flush(void)
 {
-  zynq_uart_reset_tx_flush(&zynqmp_uart_instances[BSP_CONSOLE_MINOR]);
+  zynq_uart_reset_tx_flush(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 static void zynqmp_debug_console_out(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_write_polled(base, c);
+  zynq_uart_write_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs, c);
 }
 
 static void zynqmp_debug_console_init(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   BSP_output_char = zynqmp_debug_console_out;
 }
 
 static void zynqmp_debug_console_early_init(char c)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
   zynqmp_debug_console_out(c);
 }
 
 static int zynqmp_debug_console_in(void)
 {
-  rtems_termios_device_context *base =
-    &zynqmp_uart_instances[BSP_CONSOLE_MINOR].base;
-
-  return zynq_uart_read_polled(base);
+  return zynq_uart_read_char_polled(zynqmp_uart_instances[BSP_CONSOLE_MINOR].regs);
 }
 
 BSP_output_char_function_type BSP_output_char = zynqmp_debug_console_early_init;
diff --git a/bsps/include/dev/serial/zynq-uart-regs.h b/bsps/include/dev/serial/zynq-uart-regs.h
index 3574532b85..5e872d16c3 100644
--- a/bsps/include/dev/serial/zynq-uart-regs.h
+++ b/bsps/include/dev/serial/zynq-uart-regs.h
@@ -43,6 +43,8 @@
 
 #include <bsp/utility.h>
 
+#define ZYNQ_UART_DEFAULT_BAUD 115200
+
 #define ZYNQ_UART_FIFO_DEPTH 64
 
 typedef struct zynq_uart {
@@ -158,6 +160,24 @@ typedef struct zynq_uart {
 #define ZYNQ_UART_TX_FIFO_TRG_LVL_TTRIG_SET(reg, val) BSP_FLD32SET(reg, val, 0, 5)
 } zynq_uart;
 
+void zynq_uart_initialize(volatile zynq_uart *regs);
+
+int zynq_uart_read_char_polled(volatile zynq_uart *regs);
+
+void zynq_uart_write_char_polled(volatile zynq_uart *regs, char c);
+
+/**
+  * Flush TX FIFO and wait until it is empty. Used in bsp_reset.
+  */
+void zynq_uart_reset_tx_flush(volatile zynq_uart *regs);
+
+int zynq_cal_baud_rate(
+  uint32_t  baudrate,
+  uint32_t* brgr,
+  uint32_t* bauddiv,
+  uint32_t  modereg
+);
+
 /** @} */
 
 #endif /* LIBBSP_ARM_XILINX_ZYNQ_UART_REGS_H */
diff --git a/bsps/include/dev/serial/zynq-uart.h b/bsps/include/dev/serial/zynq-uart.h
index e7854af5f1..002adcdbd6 100644
--- a/bsps/include/dev/serial/zynq-uart.h
+++ b/bsps/include/dev/serial/zynq-uart.h
@@ -59,29 +59,6 @@ typedef struct {
 
 extern const rtems_termios_device_handler zynq_uart_handler;
 
-#define ZYNQ_UART_DEFAULT_BAUD 115200
-
-void zynq_uart_initialize(rtems_termios_device_context *base);
-
-int zynq_uart_read_polled(rtems_termios_device_context *base);
-
-void zynq_uart_write_polled(
-  rtems_termios_device_context *base,
-  char c
-);
-
-/**
-  * Flush TX FIFO and wait until it is empty. Used in bsp_reset.
-  */
-void zynq_uart_reset_tx_flush(zynq_uart_context *ctx);
-
-int zynq_cal_baud_rate(
-  uint32_t  baudrate,
-  uint32_t* brgr,
-  uint32_t* bauddiv,
-  uint32_t  modereg
-);
-
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/bsps/shared/dev/serial/zynq-uart-polled.c b/bsps/shared/dev/serial/zynq-uart-polled.c
index 7d5dd8ff1a..dbf75539f6 100644
--- a/bsps/shared/dev/serial/zynq-uart-polled.c
+++ b/bsps/shared/dev/serial/zynq-uart-polled.c
@@ -124,10 +124,8 @@ int zynq_cal_baud_rate(uint32_t  baudrate,
   return 0;
 }
 
-void zynq_uart_initialize(rtems_termios_device_context *base)
+void zynq_uart_initialize(volatile zynq_uart *regs)
 {
-  zynq_uart_context *ctx = (zynq_uart_context *) base;
-  volatile zynq_uart *regs = ctx->regs;
   uint32_t brgr = 0x3e;
   uint32_t bauddiv = 0x6;
   uint32_t mode_clks = regs->mode & ZYNQ_UART_MODE_CLKS;
@@ -154,18 +152,15 @@ void zynq_uart_initialize(rtems_termios_device_context *base)
     | ZYNQ_UART_MODE_CHRL(ZYNQ_UART_MODE_CHRL_8)
     | mode_clks;
 
-  while (zynq_uart_read_polled(base) >= 0) {
+  while (zynq_uart_read_char_polled(regs) >= 0) {
     /* Drop */
   }
 
-  zynq_uart_reset_tx_flush(ctx);
+  zynq_uart_reset_tx_flush(regs);
 }
 
-int zynq_uart_read_polled(rtems_termios_device_context *base)
+int zynq_uart_read_char_polled(volatile zynq_uart *regs)
 {
-  zynq_uart_context *ctx = (zynq_uart_context *) base;
-  volatile zynq_uart *regs = ctx->regs;
-
   if ((regs->channel_sts & ZYNQ_UART_CHANNEL_STS_REMPTY) != 0) {
     return -1;
   } else {
@@ -173,14 +168,8 @@ int zynq_uart_read_polled(rtems_termios_device_context *base)
   }
 }
 
-void zynq_uart_write_polled(
-  rtems_termios_device_context *base,
-  char c
-)
+void zynq_uart_write_char_polled(volatile zynq_uart *regs, char c)
 {
-  zynq_uart_context *ctx = (zynq_uart_context *) base;
-  volatile zynq_uart *regs = ctx->regs;
-
   while ((regs->channel_sts & ZYNQ_UART_CHANNEL_STS_TNFUL) != 0) {
     /* Wait */
   }
@@ -188,13 +177,12 @@ void zynq_uart_write_polled(
   regs->tx_rx_fifo = ZYNQ_UART_TX_RX_FIFO_FIFO(c);
 }
 
-void zynq_uart_reset_tx_flush(zynq_uart_context *ctx)
+void zynq_uart_reset_tx_flush(volatile zynq_uart *regs)
 {
-  volatile zynq_uart *regs = ctx->regs;
-  int                 c = 4;
+  int c = 4;
 
   while (c-- > 0)
-    zynq_uart_write_polled(&ctx->base, '\r');
+    zynq_uart_write_char_polled(regs, '\r');
 
   while ((regs->channel_sts & ZYNQ_UART_CHANNEL_STS_TEMPTY) == 0 ||
          (regs->channel_sts & ZYNQ_UART_CHANNEL_STS_TACTIVE) != 0) {
diff --git a/bsps/shared/dev/serial/zynq-uart.c b/bsps/shared/dev/serial/zynq-uart.c
index 390ee1f527..0489288271 100644
--- a/bsps/shared/dev/serial/zynq-uart.c
+++ b/bsps/shared/dev/serial/zynq-uart.c
@@ -67,14 +67,14 @@ static bool zynq_uart_first_open(
   rtems_libio_open_close_args_t *args
 )
 {
-#ifdef ZYNQ_CONSOLE_USE_INTERRUPTS
   zynq_uart_context *ctx = (zynq_uart_context *) base;
   volatile zynq_uart *regs = ctx->regs;
+#ifdef ZYNQ_CONSOLE_USE_INTERRUPTS
   rtems_status_code sc;
 #endif
 
   rtems_termios_set_initial_baud(tty, ZYNQ_UART_DEFAULT_BAUD);
-  zynq_uart_initialize(base);
+  zynq_uart_initialize(regs);
 
 #ifdef ZYNQ_CONSOLE_USE_INTERRUPTS
   regs->rx_fifo_trg_lvl = 1;
@@ -109,15 +109,23 @@ static void zynq_uart_last_close(
 }
 #endif
 
+#ifndef ZYNQ_CONSOLE_USE_INTERRUPTS
+static int zynq_uart_read_polled(rtems_termios_device_context *base)
+{
+  zynq_uart_context *ctx = (zynq_uart_context *) base;
+  return zynq_uart_read_char_polled(ctx->regs);
+}
+#endif
+
 static void zynq_uart_write_support(
   rtems_termios_device_context *base,
   const char *buf,
   size_t len
 )
 {
-#ifdef ZYNQ_CONSOLE_USE_INTERRUPTS
   zynq_uart_context *ctx = (zynq_uart_context *) base;
   volatile zynq_uart *regs = ctx->regs;
+#ifdef ZYNQ_CONSOLE_USE_INTERRUPTS
 
   regs->irq_dis = ZYNQ_UART_TEMPTY;
 
@@ -135,9 +143,9 @@ static void zynq_uart_write_support(
     regs->irq_en = ZYNQ_UART_TEMPTY;
   }
 #else
-  ssize_t i;
+  size_t i;
   for (i = 0; i < len; ++i) {
-    zynq_uart_write_polled(base, buf[i]);
+    zynq_uart_write_char_polled(regs, buf[i]);
   }
 #endif
 }
-- 
2.35.3



More information about the devel mailing list