Skip to contentSkip to conversation

sandbox: Minor improvements to serial driver

PATCH New Simon Glass 1 participant 5 messages Nov 9, 2020 – Dec 10, 2020

lore
Share & tools
lore
#0 lore raw

cover sandbox: Minor improvements to serial driver

This series updates the sandbox serial driver to clean up the code a little.

Simon Glass (3):
serial: sandbox: Drop unnecessary #ifdefs
sandbox: serial: Convert to livetree
sandbox: serial: Update to use membuff

 drivers/serial/sandbox.c | 63 ++++++++++++++--------------------------
 1 file changed, 22 insertions(+), 41 deletions(-)
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
#1 lore raw

PATCH 1/3 serial: sandbox: Drop unnecessary #ifdefs

CONFIG_OF_CONTROL is always enabled for sandbox (as it should be for all boards), so we can drop it. Also use IS_ENABLED() for the SPL check.

---

 drivers/serial/sandbox.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/serial/sandbox.c +2 -9
index db2fbac6295..e3967deaaf2 100644
@@ -22,8 +22,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#if CONFIG_IS_ENABLED(OF_CONTROL)
-
 /*
  *
  *   serial_buf: A buffer that holds keyboard characters for the
@@ -122,9 +120,8 @@ static int sandbox_serial_pending(struct udevice *dev, bool input)
 		return 0;
 
 	os_usleep(100);
-#ifndef CONFIG_SPL_BUILD
-	video_sync_all();
-#endif
+	if (!IS_ENABLED(CONFIG_SPL_BUILD))
+		video_sync_all();
 	if (next_index == serial_buf_read)
 		return 1;	/* buffer full */
 
@@ -146,7 +143,6 @@ static int sandbox_serial_getc(struct udevice *dev)
 	serial_buf_read = increment_buffer_index(serial_buf_read);
 	return result;
 }
-#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
 
 #ifdef CONFIG_DEBUG_UART_SANDBOX
 
@@ -211,7 +207,6 @@ static int sandbox_serial_getinfo(struct udevice *dev,
 	return 0;
 }
 
-#if CONFIG_IS_ENABLED(OF_CONTROL)
 static const char * const ansi_colour[] = {
 	"black", "red", "green", "yellow", "blue", "megenta", "cyan",
 	"white",
@@ -277,5 +272,3 @@ U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
 	.platdata = &platdata_non_fdt,
 };
 #endif
-
-#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
#2 lore raw

PATCH 1/3 serial: sandbox: Drop unnecessary #ifdefs

CONFIG_OF_CONTROL is always enabled for sandbox (as it should be for all boards), so we can drop it. Also use IS_ENABLED() for the SPL check.

---

 drivers/serial/sandbox.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

Applied to u-boot-dm, thanks!

#3 lore raw

PATCH 2/3 sandbox: serial: Convert to livetree

In reply to Simon Glass

Use a livetree function to read the colour.

---

 drivers/serial/sandbox.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/serial/sandbox.c +1 -3
index e3967deaaf2..8c74c3a1b8c 100644
@@ -12,7 +12,6 @@
 #include <common.h>
 #include <console.h>
 #include <dm.h>
-#include <fdtdec.h>
 #include <lcd.h>
 #include <os.h>
 #include <serial.h>
@@ -221,8 +220,7 @@ static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
 	if (CONFIG_IS_ENABLED(OF_PLATDATA))
 		return 0;
 	plat->colour = -1;
-	colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
-			     "sandbox,text-colour", NULL);
+	colour = dev_read_string(dev, "sandbox,text-colour");
 	if (colour) {
 		for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
 			if (!strcmp(colour, ansi_colour[i])) {
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
#4 lore raw

PATCH 3/3 sandbox: serial: Update to use membuff

In reply to Simon Glass

Rather than implementing our own circular queue, use membuff. This allows us to read multiple bytes at once into the serial input.

---

 drivers/serial/sandbox.c | 48 ++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
drivers/serial/sandbox.c +19 -29
index 8c74c3a1b8c..8e269a205d7 100644
@@ -21,24 +21,18 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/*
- *
- *   serial_buf: A buffer that holds keyboard characters for the
- *		 Sandbox U-Boot.
- *
- * invariants:
- *   serial_buf_write		 == serial_buf_read -> empty buffer
- *   (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
- */
-static unsigned char serial_buf[16];
-static unsigned int serial_buf_write;
-static unsigned int serial_buf_read;
-
 struct sandbox_serial_platdata {
 	int colour;	/* Text colour to use for output, -1 for none */
 };
 
+/**
+ * struct sandbox_serial_priv - Private data for this driver
+ *
+ * @buf: holds input characters available to be read by this driver
+ */
 struct sandbox_serial_priv {
+	struct membuff buf;
+	char serial_buf[16];
 	bool start_of_line;
 };
 
@@ -71,6 +65,7 @@ static int sandbox_serial_probe(struct udevice *dev)
 
 	if (state->term_raw != STATE_TERM_RAW)
 		disable_ctrlc(1);
+	membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf));
 
 	return 0;
 }
@@ -104,16 +99,12 @@ static int sandbox_serial_putc(struct udevice *dev, const char ch)
 	return 0;
 }
 
-static unsigned int increment_buffer_index(unsigned int index)
-{
-	return (index + 1) % ARRAY_SIZE(serial_buf);
-}
-
 static int sandbox_serial_pending(struct udevice *dev, bool input)
 {
-	const unsigned int next_index =
-		increment_buffer_index(serial_buf_write);
+	struct sandbox_serial_priv *priv = dev_get_priv(dev);
 	ssize_t count;
+	char *data;
+	int avail;
 
 	if (!input)
 		return 0;
@@ -121,26 +112,25 @@ static int sandbox_serial_pending(struct udevice *dev, bool input)
 	os_usleep(100);
 	if (!IS_ENABLED(CONFIG_SPL_BUILD))
 		video_sync_all();
-	if (next_index == serial_buf_read)
+	avail = membuff_putraw(&priv->buf, 100, false, &data);
+	if (!avail)
 		return 1;	/* buffer full */
 
-	count = os_read(0, &serial_buf[serial_buf_write], 1);
-	if (count == 1)
-		serial_buf_write = next_index;
+	count = os_read(0, data, avail);
+	if (count > 0)
+		membuff_putraw(&priv->buf, count, true, &data);
 
-	return serial_buf_write != serial_buf_read;
+	return membuff_avail(&priv->buf);
 }
 
 static int sandbox_serial_getc(struct udevice *dev)
 {
-	int result;
+	struct sandbox_serial_priv *priv = dev_get_priv(dev);
 
 	if (!sandbox_serial_pending(dev, true))
 		return -EAGAIN;	/* buffer empty */
 
-	result = serial_buf[serial_buf_read];
-	serial_buf_read = increment_buffer_index(serial_buf_read);
-	return result;
+	return membuff_getbyte(&priv->buf);
 }
 
 #ifdef CONFIG_DEBUG_UART_SANDBOX
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog