Skip to contentSkip to conversation

serial: ns16550: Support run-time configuration

PATCH Superseded Simon Glass 4 participants 9 messages Dec 9, 2019 – Dec 19, 2019 x86

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

 drivers/serial/Kconfig   | 21 +++++++++++++++
 drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++------
 include/ns16550.h        | 13 +++++++++
 3 files changed, 83 insertions(+), 8 deletions(-)
drivers/serial/Kconfig +21 -0
index ece7d87d4c..472a9f0929 100644
@@ -598,6 +598,27 @@ config SYS_NS16550
 	  be used. It can be a constant or a function to get clock, eg,
 	  get_serial_clock().
 
+config NS16550_DYNAMIC
+	bool "Allow NS16550 to be configured at runtime"
+	default y if SYS_COREBOOT || SYS_SLIMBOOTLOADER
+	help
+	  Enable this option to allow device-tree control of the driver.
+
+	  Normally this driver is controlled by the following options:
+
+	  CONFIG_SYS_NS16550_PORT_MAPPED - indicates that port I/O is used for
+	     access. If not enabled, then the UART is memory-mapped.
+	  CONFIG_SYS_NS16550_MEM32 - if memory-mapped, indicates that 32-bit
+	     access should be used (instead of 8-bit)
+	  CONFIG_SYS_NS16550_REG_SIZE - indicates register width and also
+	     endianness. If positive, big-endian access is used. If negative,
+	     little-endian is used.
+
+	  It is not a good practice for a driver to be statically configured,
+	  since it prevents the same driver being used for different types of
+	  UARTs in a system. This option avoids this problem at the cost of a
+	  slightly increased code size.
+
 config INTEL_MID_SERIAL
 	bool "Intel MID platform UART support"
 	depends on DM_SERIAL && OF_CONTROL
drivers/serial/ns16550.c +49 -8

77 lines not on this page · raw message

include/ns16550.h +13 -0
index 701efeea85..ba9b71962d 100644
@@ -31,6 +31,9 @@
 #define CONFIG_SYS_NS16550_REG_SIZE (-1)
 #endif
 
+#ifdef CONFIG_NS16550_DYNAMIC
+#define UART_REG(x)	unsigned char x
+#else
 #if !defined(CONFIG_SYS_NS16550_REG_SIZE) || (CONFIG_SYS_NS16550_REG_SIZE == 0)
 #error "Please define NS16550 registers size."
 #elif defined(CONFIG_SYS_NS16550_MEM32) && !defined(CONFIG_DM_SERIAL)
@@ -44,6 +47,12 @@
 	unsigned char x;						\
 	unsigned char postpad_##x[-CONFIG_SYS_NS16550_REG_SIZE - 1];
 #endif
+#endif /* CONFIG_NS16550_DYNAMIC */
+
+enum ns16550_flags {
+	NS16550_FLAG_IO	= 1 << 0, /* Use I/O access (else mem-mapped) */
+	NS16550_FLAG_BE	= 1 << 1, /* Use big-endian access (else little) */
+};
 
 /**
  * struct ns16550_platdata - information about a NS16550 port
@@ -51,7 +60,10 @@
  * @base:		Base register address
  * @reg_width:		IO accesses size of registers (in bytes)
  * @reg_shift:		Shift size of registers (0=byte, 1=16bit, 2=32bit...)
+ * @reg_offset:		Offset to start of registers
  * @clock:		UART base clock speed in Hz
+ * @fcr:		Offset of FCR register (normally UART_FCR_DEFVAL)
+ * @flags:		A few flags (enum ns16550_flags)
  * @bdf:		PCI slot/function (pci_dev_t)
  */
 struct ns16550_platdata {
@@ -61,6 +73,7 @@ struct ns16550_platdata {
 	int reg_offset;
 	int clock;
 	u32 fcr;
+	int flags;
 #if defined(CONFIG_PCI) && defined(CONFIG_SPL)
 	int bdf;
 #endif
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#1 lore raw

PATCH v2 2/4 x86: Update coreboot serial table struct

Since mid 2016, coreboot has additional fields in the serial struct that it passes down to U-Boot. Add these so we are in sync.

---

Changes in v2: None

 arch/x86/include/asm/coreboot_tables.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/x86/include/asm/coreboot_tables.h +19 -0
index 2c54e24e02..61de0077d7 100644
@@ -97,6 +97,25 @@ struct cb_serial {
 	u32 type;
 	u32 baseaddr;
 	u32 baud;
+	u32 regwidth;
+
+	/*
+	 * Crystal or input frequency to the chip containing the UART.
+	 * Provide the board specific details to allow the payload to
+	 * initialize the chip containing the UART and make independent
+	 * decisions as to which dividers to select and their values
+	 * to eventually arrive at the desired console baud-rate.
+	 */
+	u32 input_hertz;
+
+	/*
+	 * UART PCI address: bus, device, function
+	 * 1 << 31 - Valid bit, PCI UART in use
+	 * Bus << 20
+	 * Device << 15
+	 * Function << 12
+	 */
+	u32 uart_pci_addr;
 };
 
 #define CB_TAG_CONSOLE			0x0010
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#2 lore raw

PATCH v2 3/4 x86: serial: Add a coreboot serial driver

In reply to Simon Glass

Coreboot can provide information about the serial device in use on a platform. Add a driver that uses this information to produce a working UART.

---

Changes in v2: None

 drivers/serial/Kconfig           | 11 ++++++++
 drivers/serial/Makefile          |  1 +
 drivers/serial/serial_coreboot.c | 46 ++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 drivers/serial/serial_coreboot.c
drivers/serial/Kconfig +11 -0
index 472a9f0929..c403c2fc25 100644
@@ -539,6 +539,17 @@ config BCM6345_SERIAL
 	help
 	  Select this to enable UART on BCM6345 SoCs.
 
+config COREBOOT_SERIAL
+	bool "Coreboot UART support"
+	depends on DM_SERIAL
+	default y if SYS_COREBOOT
+	select SYS_NS16550
+	help
+	  Select this to enable a ns16550-style UART where the platform data
+	  comes from the coreboot 'sysinfo' tables. This allows U-Boot to have
+	  a serial console on any platform without needing to change the
+	  device tree, etc.
+
 config FSL_LINFLEXUART
 	bool "Freescale Linflex UART support"
 	depends on DM_SERIAL
drivers/serial/Makefile +1 -0
index 06ee30697d..76b1811510 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_AR933X_UART) += serial_ar933x.o
 obj-$(CONFIG_ARM_DCC) += arm_dcc.o
 obj-$(CONFIG_ATMEL_USART) += atmel_usart.o
 obj-$(CONFIG_BCM6345_SERIAL) += serial_bcm6345.o
+obj-$(CONFIG_COREBOOT_SERIAL) += serial_coreboot.o
 obj-$(CONFIG_EFI_APP) += serial_efi.o
 obj-$(CONFIG_LPC32XX_HSUART) += lpc32xx_hsuart.o
 obj-$(CONFIG_MCFUART) += mcfuart.o
drivers/serial/serial_coreboot.c new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#3 lore raw

PATCH v2 4/4 x86: Move coreboot over to use the coreboot UART

In reply to Simon Glass

Use this UART to improve the compatibility of U-Boot when used as a coreboot payload.

---

Changes in v2: None

 arch/x86/dts/coreboot.dts | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/dts/coreboot.dts +5 -1
index a88da6eafd..38ddaafa19 100644
@@ -8,7 +8,6 @@
 /dts-v1/;
 
 /include/ "skeleton.dtsi"
-/include/ "serial.dtsi"
 /include/ "keyboard.dtsi"
 /include/ "pcspkr.dtsi"
 /include/ "reset.dtsi"
@@ -40,6 +39,11 @@
 		u-boot,dm-pre-reloc;
 	};
 
+	serial: serial {
+		u-boot,dm-pre-reloc;
+		compatible = "coreboot-serial";
+	};
+
 	coreboot-fb {
 		compatible = "coreboot-fb";
 	};
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#4 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

In reply to Simon Glass

Am 09.12.2019 um 17:59 schrieb Simon Glass:

Quoted 175 lines

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

  drivers/serial/Kconfig   | 21 +++++++++++++++
  drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++------
  include/ns16550.h        | 13 +++++++++
  3 files changed, 83 insertions(+), 8 deletions(-)
drivers/serial/Kconfig +21 -0
index ece7d87d4c..472a9f0929 100644
@@ -598,6 +598,27 @@ config SYS_NS16550
  	  be used. It can be a constant or a function to get clock, eg,
  	  get_serial_clock().
 
+config NS16550_DYNAMIC
+	bool "Allow NS16550 to be configured at runtime"
+	default y if SYS_COREBOOT || SYS_SLIMBOOTLOADER
+	help
+	  Enable this option to allow device-tree control of the driver.
+
+	  Normally this driver is controlled by the following options:
+
+	  CONFIG_SYS_NS16550_PORT_MAPPED - indicates that port I/O is used for
+	     access. If not enabled, then the UART is memory-mapped.
+	  CONFIG_SYS_NS16550_MEM32 - if memory-mapped, indicates that 32-bit
+	     access should be used (instead of 8-bit)
+	  CONFIG_SYS_NS16550_REG_SIZE - indicates register width and also
+	     endianness. If positive, big-endian access is used. If negative,
+	     little-endian is used.
+
+	  It is not a good practice for a driver to be statically configured,
+	  since it prevents the same driver being used for different types of
+	  UARTs in a system. This option avoids this problem at the cost of a
+	  slightly increased code size.
+
  config INTEL_MID_SERIAL
  	bool "Intel MID platform UART support"
  	depends on DM_SERIAL && OF_CONTROL
drivers/serial/ns16550.c +49 -8
index 754b6e9921..96c4471efd 100644
@@ -92,19 +92,57 @@ static inline int serial_in_shift(void *addr, int shift)
  #define CONFIG_SYS_NS16550_CLK  0
  #endif
 
+static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
+			       int value)
+{
+	if (plat->flags & NS16550_FLAG_BE) {
+		if (plat->reg_width == 1)
+			writeb(value, addr + (1 << plat->reg_shift) - 1);
+		else if (plat->flags & NS16550_FLAG_IO)
+			out_be32(addr, value);
+		else
+			writel(value, addr);
+	} else {
+		if (plat->reg_width == 1)
+			writeb(value, addr);
+		else if (plat->flags & NS16550_FLAG_IO)
+			out_le32(addr, value);
+		else
+			writel(value, addr);
+	}
+}
+
+static int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr)
+{
+	if (plat->flags & NS16550_FLAG_BE) {
+		if (plat->reg_width == 1)
+			return readb(addr + (1 << plat->reg_shift) - 1);
+		else if (plat->flags & NS16550_FLAG_IO)
+			return in_be32(addr);
+		else
+			return readl(addr);
+	} else {
+		if (plat->reg_width == 1)
+			return readb(addr);
+		else if (plat->flags & NS16550_FLAG_IO)
+			return in_le32(addr);
+		else
+			return readl(addr);
+	}
+}
+
  static void ns16550_writeb(NS16550_t port, int offset, int value)
  {
  	struct ns16550_platdata *plat = port->plat;
  	unsigned char *addr;
 
  	offset *= 1 << plat->reg_shift;
-	addr = (unsigned char *)plat->base + offset;
+	addr = (unsigned char *)plat->base + offset + plat->reg_offset;
 
-	/*
-	 * As far as we know it doesn't make sense to support selection of
-	 * these options at run-time, so use the existing CONFIG options.
-	 */
-	serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
+	if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
+		serial_out_dynamic(plat, addr, value);
+	else
+		serial_out_shift(addr, plat->reg_shift, value);
  }
 
  static int ns16550_readb(NS16550_t port, int offset)
@@ -113,9 +151,12 @@ static int ns16550_readb(NS16550_t port, int offset)
  	unsigned char *addr;
 
  	offset *= 1 << plat->reg_shift;
-	addr = (unsigned char *)plat->base + offset;
+	addr = (unsigned char *)plat->base + offset + plat->reg_offset;
 
-	return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
+	if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
+		return serial_in_dynamic(plat, addr);
+	else
+		return serial_in_shift(addr, plat->reg_shift);
  }
 
  static u32 ns16550_getfcr(NS16550_t port)
include/ns16550.h +12 -0
index 701efeea85..ba9b71962d 100644
@@ -31,6 +31,9 @@
  #define CONFIG_SYS_NS16550_REG_SIZE (-1)
  #endif
 
+#ifdef CONFIG_NS16550_DYNAMIC
+#define UART_REG(x)	unsigned char x
+#else
  #if !defined(CONFIG_SYS_NS16550_REG_SIZE) || (CONFIG_SYS_NS16550_REG_SIZE == 0)
  #error "Please define NS16550 registers size."
  #elif defined(CONFIG_SYS_NS16550_MEM32) && !defined(CONFIG_DM_SERIAL)
@@ -44,6 +47,12 @@
  	unsigned char x;						\
  	unsigned char postpad_##x[-CONFIG_SYS_NS16550_REG_SIZE - 1];
  #endif
+#endif /* CONFIG_NS16550_DYNAMIC */
+
+enum ns16550_flags {
+	NS16550_FLAG_IO	= 1 << 0, /* Use I/O access (else mem-mapped) */
+	NS16550_FLAG_BE	= 1 << 1, /* Use big-endian access (else little) */
+};
 
  /**
   * struct ns16550_platdata - information about a NS16550 port
@@ -51,7 +60,10 @@
   * @base:		Base register address
   * @reg_width:		IO accesses size of registers (in bytes)
   * @reg_shift:		Shift size of registers (0=byte, 1=16bit, 2=32bit...)
+ * @reg_offset:		Offset to start of registers
   * @clock:		UART base clock speed in Hz
+ * @fcr:		Offset of FCR register (normally UART_FCR_DEFVAL)
+ * @flags:		A few flags (enum ns16550_flags)
   * @bdf:		PCI slot/function (pci_dev_t)
   */
  struct ns16550_platdata {

include/ns16550.h struct ns16550_platdata { · line 76

Quoted 4 lines
include/ns16550.h +1 -0
@@ -61,6 +73,7 @@ struct ns16550_platdata {
  	int reg_offset;
  	int clock;
  	u32 fcr;
+	int flags;

I'm not too fond of adding 4 bytes here for nothing on most platforms, but unless you come up with a better idea than cluttering this file with ifdefs:

Quoted 4 lines
include/ns16550.h +0 -0
  #if defined(CONFIG_PCI) && defined(CONFIG_SPL)
  	int bdf;
  #endif
#5 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

In reply to Simon Glass
Quoted 17 lines · Simon Glass

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

Quoted 5 lines
 drivers/serial/Kconfig   | 21 +++++++++++++++
 drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++------
 include/ns16550.h        | 13 +++++++++
 3 files changed, 83 insertions(+), 8 deletions(-)
#6 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

In reply to Simon Glass

Hi Simon,

Quoted 79 lines

-----Original Message-----
From: U-Boot <[email protected]> On Behalf Of Simon Glass
Sent: Monday, December 9, 2019 8:59 AM
To: U-Boot Mailing List <[email protected]>
Cc: Stefan Roese <[email protected]>; Angelo Dureghello <[email protected]>
Subject: [PATCH v2 1/4] serial: ns16550: Support run-time configuration

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

 drivers/serial/Kconfig   | 21 +++++++++++++++
 drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++----
--
 include/ns16550.h        | 13 +++++++++
 3 files changed, 83 insertions(+), 8 deletions(-)
drivers/serial/Kconfig index renamed from drivers/serial/Kconfig +0 -0

ece7d87d4c..472a9f0929 100644

drivers/serial/Kconfig +9 -0
@@ -598,6 +598,27 @@ config SYS_NS16550
 	  be used. It can be a constant or a function to get clock, eg,
 	  get_serial_clock().
 
+config NS16550_DYNAMIC
+	bool "Allow NS16550 to be configured at runtime"
+	default y if SYS_COREBOOT || SYS_SLIMBOOTLOADER
+	help
+	  Enable this option to allow device-tree control of the driver.
+
+	  Normally this driver is controlled by the following options:
+
+	  CONFIG_SYS_NS16550_PORT_MAPPED - indicates that port I/O is

used for

drivers/serial/Kconfig +2 -0
+	     access. If not enabled, then the UART is memory-mapped.
+	  CONFIG_SYS_NS16550_MEM32 - if memory-mapped, indicates that

32-bit

drivers/serial/Kconfig +10 -0
+	     access should be used (instead of 8-bit)
+	  CONFIG_SYS_NS16550_REG_SIZE - indicates register width and also
+	     endianness. If positive, big-endian access is used. If negative,
+	     little-endian is used.
+
+	  It is not a good practice for a driver to be statically configured,
+	  since it prevents the same driver being used for different types of
+	  UARTs in a system. This option avoids this problem at the cost of a
+	  slightly increased code size.
+
 config INTEL_MID_SERIAL
 	bool "Intel MID platform UART support"
 	depends on DM_SERIAL && OF_CONTROL
drivers/serial/ns16550.c index renamed from drivers/serial/ns16550.c +0 -0

754b6e9921..96c4471efd 100644

drivers/serial/ns16550.c +9 -0
@@ -92,19 +92,57 @@ static inline int serial_in_shift(void *addr, int shift)
 #define CONFIG_SYS_NS16550_CLK  0  #endif
 
+static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
+			       int value)
+{
+	if (plat->flags & NS16550_FLAG_BE) {
+		if (plat->reg_width == 1)
+			writeb(value, addr + (1 << plat->reg_shift) - 1);
+		else if (plat->flags & NS16550_FLAG_IO)
+			out_be32(addr, value);
+		else

drivers/serial/ns16550.c static inline int serial_in_shift(void *addr, int shift) · line 112

Quoted 10 lines
drivers/serial/ns16550.c +10 -0
+			writel(value, addr);
+	} else {
+		if (plat->reg_width == 1)
+			writeb(value, addr);
+		else if (plat->flags & NS16550_FLAG_IO)
+			out_le32(addr, value);
+		else
+			writel(value, addr);
+	}
+}

IO needs to use outb(). It breaks QEMU 0x3f8 IO (reg_width = 1, flags=IO). I have verified 0x3f8 IO on QEMU and MMIO32 on APL with below code.

	if (plat->flags & NS16550_FLAG_IO) {
		outb(value, addr);
	} else {
		if (plat->flags & NS16550_FLAG_BE) {
			if (plat->reg_width == 1)
				writeb(value, addr + (1 << plat->reg_shift) - 1);
			else
				out_be32(addr, value);
		} else {
			if (plat->reg_width == 1)
				writeb(value, addr);
			else
				out_le32(addr, value);
		}
	}
Quoted 8 lines
drivers/serial/ns16550.c +8 -0
+
+static int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr) {
+	if (plat->flags & NS16550_FLAG_BE) {
+		if (plat->reg_width == 1)
+			return readb(addr + (1 << plat->reg_shift) - 1);
+		else if (plat->flags & NS16550_FLAG_IO)
+			return in_be32(addr);
+		else

drivers/serial/ns16550.c static inline int serial_in_shift(void *addr, int shift) · line 132

Quoted 10 lines
drivers/serial/ns16550.c +10 -0
+			return readl(addr);
+	} else {
+		if (plat->reg_width == 1)
+			return readb(addr);
+		else if (plat->flags & NS16550_FLAG_IO)
+			return in_le32(addr);
+		else
+			return readl(addr);
+	}
+}

Similar as above.

	if (plat->flags & NS16550_FLAG_IO) {
		return inb((ulong)addr);
	} else {
		if (plat->flags & NS16550_FLAG_BE) {
			if (plat->reg_width == 1)
				return readb(addr + (1 << plat->reg_shift) - 1);
			else
				return in_be32(addr);
		} else {
			if (plat->reg_width == 1)
				return readb(addr);
			else
				return in_le32(addr);
		}
	}
Quoted 84 lines
drivers/serial/ns16550.c +11 -8
+
 static void ns16550_writeb(NS16550_t port, int offset, int value)  {
 	struct ns16550_platdata *plat = port->plat;
 	unsigned char *addr;
 
 	offset *= 1 << plat->reg_shift;
-	addr = (unsigned char *)plat->base + offset;
+	addr = (unsigned char *)plat->base + offset + plat->reg_offset;
 
-	/*
-	 * As far as we know it doesn't make sense to support selection of
-	 * these options at run-time, so use the existing CONFIG options.
-	 */
-	serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
+	if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
+		serial_out_dynamic(plat, addr, value);
+	else
+		serial_out_shift(addr, plat->reg_shift, value);
 }
 
 static int ns16550_readb(NS16550_t port, int offset) @@ -113,9 +151,12 @@
 static int ns16550_readb(NS16550_t port, int offset)
 	unsigned char *addr;
 
 	offset *= 1 << plat->reg_shift;
-	addr = (unsigned char *)plat->base + offset;
+	addr = (unsigned char *)plat->base + offset + plat->reg_offset;
 
-	return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
+	if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
+		return serial_in_dynamic(plat, addr);
+	else
+		return serial_in_shift(addr, plat->reg_shift);
 }
 
 static u32 ns16550_getfcr(NS16550_t port) diff --git a/include/ns16550.h

b/include/ns16550.h index 701efeea85..ba9b71962d 100644

include/ns16550.h +3 -0
@@ -31,6 +31,9 @@
 #define CONFIG_SYS_NS16550_REG_SIZE (-1)  #endif
 
+#ifdef CONFIG_NS16550_DYNAMIC
+#define UART_REG(x)	unsigned char x
+#else
 #if !defined(CONFIG_SYS_NS16550_REG_SIZE) ||

(CONFIG_SYS_NS16550_REG_SIZE == 0) #error "Please define NS16550 registers size."
#elif defined(CONFIG_SYS_NS16550_MEM32)
&& !defined(CONFIG_DM_SERIAL) @@ -44,6 +47,12 @@

include/ns16550.h +0 -0
 	unsigned char x;						\
 	unsigned char postpad_##x[-CONFIG_SYS_NS16550_REG_SIZE - 1];

#endif

include/ns16550.h +4 -0
+#endif /* CONFIG_NS16550_DYNAMIC */
+
+enum ns16550_flags {
+	NS16550_FLAG_IO	= 1 << 0, /* Use I/O access (else mem-

mapped) */

include/ns16550.h +1 -0
+	NS16550_FLAG_BE	= 1 << 1, /* Use big-endian access (else little)

*/

include/ns16550.h +6 -1
+};
 
 /**
  * struct ns16550_platdata - information about a NS16550 port @@ -51,7
+60,10 @@
  * @base:		Base register address
  * @reg_width:		IO accesses size of registers (in bytes)
  * @reg_shift:		Shift size of registers (0=byte, 1=16bit, 2=32bit...)
+ * @reg_offset:		Offset to start of registers
  * @clock:		UART base clock speed in Hz
+ * @fcr:		Offset of FCR register (normally UART_FCR_DEFVAL)
+ * @flags:		A few flags (enum ns16550_flags)
  * @bdf:		PCI slot/function (pci_dev_t)
  */
 struct ns16550_platdata {
@@ -61,6 +73,7 @@ struct ns16550_platdata {
 	int reg_offset;
 	int clock;
 	u32 fcr;
+	int flags;
 #if defined(CONFIG_PCI) && defined(CONFIG_SPL)
 	int bdf;
 #endif
--
Quoted 1 line

2.24.0.393.g34dc348eaf-goog

Best Regards,
Aiden

#7 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

Hi Simon,

Quoted 94 lines · Aiden

Hi Simon,

Quoted 79 lines

-----Original Message-----
From: U-Boot <[email protected]> On Behalf Of Simon Glass
Sent: Monday, December 9, 2019 8:59 AM
To: U-Boot Mailing List <[email protected]>
Cc: Stefan Roese <[email protected]>; Angelo Dureghello <[email protected]>
Subject: [PATCH v2 1/4] serial: ns16550: Support run-time configuration

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

 drivers/serial/Kconfig   | 21 +++++++++++++++
 drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++----
--
 include/ns16550.h        | 13 +++++++++
 3 files changed, 83 insertions(+), 8 deletions(-)
drivers/serial/Kconfig index renamed from drivers/serial/Kconfig +0 -0

ece7d87d4c..472a9f0929 100644

drivers/serial/Kconfig +9 -0
@@ -598,6 +598,27 @@ config SYS_NS16550
        be used. It can be a constant or a function to get clock, eg,
        get_serial_clock().
 
+config NS16550_DYNAMIC
+     bool "Allow NS16550 to be configured at runtime"
+     default y if SYS_COREBOOT || SYS_SLIMBOOTLOADER
+     help
+       Enable this option to allow device-tree control of the driver.
+
+       Normally this driver is controlled by the following options:
+
+       CONFIG_SYS_NS16550_PORT_MAPPED - indicates that port I/O is

used for

drivers/serial/Kconfig +2 -0
+          access. If not enabled, then the UART is memory-mapped.
+       CONFIG_SYS_NS16550_MEM32 - if memory-mapped, indicates that

32-bit

drivers/serial/Kconfig +10 -0
+          access should be used (instead of 8-bit)
+       CONFIG_SYS_NS16550_REG_SIZE - indicates register width and also
+          endianness. If positive, big-endian access is used. If negative,
+          little-endian is used.
+
+       It is not a good practice for a driver to be statically configured,
+       since it prevents the same driver being used for different types of
+       UARTs in a system. This option avoids this problem at the cost of a
+       slightly increased code size.
+
 config INTEL_MID_SERIAL
      bool "Intel MID platform UART support"
      depends on DM_SERIAL && OF_CONTROL
drivers/serial/ns16550.c index renamed from drivers/serial/ns16550.c +0 -0

754b6e9921..96c4471efd 100644

drivers/serial/ns16550.c +9 -0
@@ -92,19 +92,57 @@ static inline int serial_in_shift(void *addr, int shift)
 #define CONFIG_SYS_NS16550_CLK  0  #endif
 
+static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
+                            int value)
+{
+     if (plat->flags & NS16550_FLAG_BE) {
+             if (plat->reg_width == 1)
+                     writeb(value, addr + (1 << plat->reg_shift) - 1);
+             else if (plat->flags & NS16550_FLAG_IO)
+                     out_be32(addr, value);
+             else

drivers/serial/ns16550.c static inline int serial_in_shift(void *addr, int shift) · line 112

Quoted 10 lines
drivers/serial/ns16550.c +10 -0
+                     writel(value, addr);
+     } else {
+             if (plat->reg_width == 1)
+                     writeb(value, addr);
+             else if (plat->flags & NS16550_FLAG_IO)
+                     out_le32(addr, value);
+             else
+                     writel(value, addr);
+     }
+}

IO needs to use outb(). It breaks QEMU 0x3f8 IO (reg_width = 1, flags=IO). I have verified 0x3f8 IO on QEMU and MMIO32 on APL with below code.

Quoted 16 lines
        if (plat->flags & NS16550_FLAG_IO) {
                outb(value, addr);
        } else {
                if (plat->flags & NS16550_FLAG_BE) {
                        if (plat->reg_width == 1)
                                writeb(value, addr + (1 << plat->reg_shift) - 1);
                        else
                                out_be32(addr, value);
                } else {
                        if (plat->reg_width == 1)
                                writeb(value, addr);
                        else
                                out_le32(addr, value);
                }
        }

Would you post a v3 that fixes the issue that Aiden pointed out?

Regards,
Bin

#8 lore raw

PATCH v2 1/4 serial: ns16550: Support run-time configuration

Hi Bin,

Quoted 115 lines · Bin Meng

Hi Simon,

Quoted 94 lines · Aiden

Hi Simon,

Quoted 79 lines

-----Original Message-----
From: U-Boot <[email protected]> On Behalf Of Simon Glass
Sent: Monday, December 9, 2019 8:59 AM
To: U-Boot Mailing List <[email protected]>
Cc: Stefan Roese <[email protected]>; Angelo Dureghello <[email protected]>
Subject: [PATCH v2 1/4] serial: ns16550: Support run-time configuration

At present this driver uses an assortment of CONFIG options to control how it accesses the hardware. This is painful for platforms that are supposed to be controlled by a device tree or a previous-stage bootloader.

Add a new CONFIG option to enable fully dynamic configuration. This controls register spacing, size, offset and endianness.

---

Changes in v2:
- runtime -> run-time
- Enable run-time config for slimbootloader too
- Improve Kconfig help based on Bin's comments
- Use ns16550 in patch subject

 drivers/serial/Kconfig   | 21 +++++++++++++++
 drivers/serial/ns16550.c | 57 ++++++++++++++++++++++++++++++++++----
--
 include/ns16550.h        | 13 +++++++++
 3 files changed, 83 insertions(+), 8 deletions(-)
drivers/serial/Kconfig index renamed from drivers/serial/Kconfig +0 -0

ece7d87d4c..472a9f0929 100644

drivers/serial/Kconfig +9 -0
@@ -598,6 +598,27 @@ config SYS_NS16550
        be used. It can be a constant or a function to get clock, eg,
        get_serial_clock().
 
+config NS16550_DYNAMIC
+     bool "Allow NS16550 to be configured at runtime"
+     default y if SYS_COREBOOT || SYS_SLIMBOOTLOADER
+     help
+       Enable this option to allow device-tree control of the driver.
+
+       Normally this driver is controlled by the following options:
+
+       CONFIG_SYS_NS16550_PORT_MAPPED - indicates that port I/O is

used for

drivers/serial/Kconfig +2 -0
+          access. If not enabled, then the UART is memory-mapped.
+       CONFIG_SYS_NS16550_MEM32 - if memory-mapped, indicates that

32-bit

drivers/serial/Kconfig +10 -0
+          access should be used (instead of 8-bit)
+       CONFIG_SYS_NS16550_REG_SIZE - indicates register width and also
+          endianness. If positive, big-endian access is used. If negative,
+          little-endian is used.
+
+       It is not a good practice for a driver to be statically configured,
+       since it prevents the same driver being used for different types of
+       UARTs in a system. This option avoids this problem at the cost of a
+       slightly increased code size.
+
 config INTEL_MID_SERIAL
      bool "Intel MID platform UART support"
      depends on DM_SERIAL && OF_CONTROL
drivers/serial/ns16550.c index renamed from drivers/serial/ns16550.c +0 -0

754b6e9921..96c4471efd 100644

drivers/serial/ns16550.c +9 -0
@@ -92,19 +92,57 @@ static inline int serial_in_shift(void *addr, int shift)
 #define CONFIG_SYS_NS16550_CLK  0  #endif
 
+static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
+                            int value)
+{
+     if (plat->flags & NS16550_FLAG_BE) {
+             if (plat->reg_width == 1)
+                     writeb(value, addr + (1 << plat->reg_shift) - 1);
+             else if (plat->flags & NS16550_FLAG_IO)
+                     out_be32(addr, value);
+             else

drivers/serial/ns16550.c static inline int serial_in_shift(void *addr, int shift) · line 112

Quoted 10 lines
drivers/serial/ns16550.c +10 -0
+                     writel(value, addr);
+     } else {
+             if (plat->reg_width == 1)
+                     writeb(value, addr);
+             else if (plat->flags & NS16550_FLAG_IO)
+                     out_le32(addr, value);
+             else
+                     writel(value, addr);
+     }
+}

IO needs to use outb(). It breaks QEMU 0x3f8 IO (reg_width = 1, flags=IO). I have verified 0x3f8 IO on QEMU and MMIO32 on APL with below code.

Quoted 16 lines
        if (plat->flags & NS16550_FLAG_IO) {
                outb(value, addr);
        } else {
                if (plat->flags & NS16550_FLAG_BE) {
                        if (plat->reg_width == 1)
                                writeb(value, addr + (1 << plat->reg_shift) - 1);
                        else
                                out_be32(addr, value);
                } else {
                        if (plat->reg_width == 1)
                                writeb(value, addr);
                        else
                                out_le32(addr, value);
                }
        }
Quoted 1 line

Would you post a v3 that fixes the issue that Aiden pointed out?

Yes, but actually I have found that we need one more case, so it needs a bit more effort. Will post an update series by Monday.

Regards,
SImon