Skip to contentSkip to conversation

serial: ns16550: fix debug uart putc called before init

PATCH In review Simon Goldschmidt 3 participants 5 messages Jan 9, 2019 – Jan 16, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 serial: ns16550: fix debug uart putc called before init

If _debug_uart_putc() is called before _debug_uart_init(), the ns16550 debug uart driver hangs in a tight loop waiting for the tx FIFO to get empty.

As this can happen via a printf sneaking in before the port calls debug_uart_init(), introduce a config option to ignore characters before the debug uart is initialized.

This is done by reading the baudrate divisor and aborting if is zero.

The Kconfig option is required as reading the baudrate divisor does not seem to work for all ns16500 compatibles (which is why the last attempt on this has been reverted in 1a67969a99).

Tested on socfpga_cyclone5_socrates.

---

 drivers/serial/Kconfig   | 12 ++++++++++++
 drivers/serial/ns16550.c | 20 ++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)
drivers/serial/Kconfig +12 -0
index b7ff2960ab..8709bf69c0 100644
@@ -446,6 +446,18 @@ config DEBUG_UART_SKIP_INIT
 	  Select this if the UART you want to use for debug output is already
 	  initialized by the time U-Boot starts its execution.
 
+config DEBUG_UART_NS16550_CHECK_ENABLED
+	bool "Check if UART is enabled on output"
+	depends on DEBUG_UART
+	depends on DEBUG_UART_NS16550
+	help
+	  Select this if puts()/putc() might be called before the debug UART
+	  has been initialized. If this is disabled, putc() might sit in a
+	  tight loop if it is called before debug_uart_init() has been called.
+
+	  Note that this does not work for every ns16550-compatible UART and
+	  so has to be enabled carefully or you might notice lost characters.
+
 config ALTERA_JTAG_UART
 	bool "Altera JTAG UART support"
 	depends on DM_SERIAL
drivers/serial/ns16550.c +18 -2
index 560ca2ae34..6cf2be8f2b 100644
@@ -272,12 +272,28 @@ static inline void _debug_uart_init(void)
 	serial_dout(&com_port->lcr, UART_LCRVAL);
 }
 
+static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
+{
+	int ret;
+
+	serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
+	ret = serial_din(&com_port->dll) & 0xff;
+	ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
+	serial_dout(&com_port->lcr, UART_LCRVAL);
+
+	return ret;
+}
+
 static inline void _debug_uart_putc(int ch)
 {
 	struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
 
-	while (!(serial_din(&com_port->lsr) & UART_LSR_THRE))
-		;
+	while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
+#ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
+		if (!NS16550_read_baud_divisor(com_port))
+			return;
+#endif
+	}
 	serial_dout(&com_port->thr, ch);
 }
 
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 serial: ns16550: fix debug uart putc called before init

Quoted 15 lines · Simon Goldschmidt

If _debug_uart_putc() is called before _debug_uart_init(), the ns16550 debug uart driver hangs in a tight loop waiting for the tx FIFO to get empty.

As this can happen via a printf sneaking in before the port calls debug_uart_init(), introduce a config option to ignore characters before the debug uart is initialized.

This is done by reading the baudrate divisor and aborting if is zero.

The Kconfig option is required as reading the baudrate divisor does not seem to work for all ns16500 compatibles (which is why the last attempt on this has been reverted in 1a67969a99).

Quoted 8 lines

Tested on socfpga_cyclone5_socrates.

---

 drivers/serial/Kconfig   | 12 ++++++++++++
 drivers/serial/ns16550.c | 20 ++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)
#2 lore raw
In reply to Simon Goldschmidt
Quoted 10 lines · Simon Goldschmidt

If _debug_uart_putc() is called before _debug_uart_init(), the ns16550 debug uart driver hangs in a tight loop waiting for the tx FIFO to get empty.

As this can happen via a printf sneaking in before the port calls debug_uart_init(), introduce a config option to ignore characters before the debug uart is initialized.

This is done by reading the baudrate divisor and aborting if is zero.

Quoted 8 lines

The Kconfig option is required as reading the baudrate divisor does not seem to work for all ns16500 compatibles (which is why the last attempt on this has been reverted in 1a67969a99).

Tested on socfpga_cyclone5_socrates.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190115/632afd81/attachment.sig>
#3 lore raw
Quoted 17 lines · Tom Rini
Quoted 14 lines · Simon Goldschmidt

If _debug_uart_putc() is called before _debug_uart_init(), the ns16550 debug uart driver hangs in a tight loop waiting for the tx FIFO to get empty.

As this can happen via a printf sneaking in before the port calls debug_uart_init(), introduce a config option to ignore characters before the debug uart is initialized.

This is done by reading the baudrate divisor and aborting if is zero.

The Kconfig option is required as reading the baudrate divisor does not seem to work for all ns16500 compatibles (which is why the last attempt on this has been reverted in 1a67969a99).

Quoted 6 lines
Quoted 4 lines

Tested on socfpga_cyclone5_socrates.

Applied to u-boot/master, thanks!

I don't see this or any of my other patches in u-boot/master (neither github nor denx.de), is that expected?

Regards,
Simon

#4 lore raw
Quoted 20 lines · Simon Goldschmidt
Quoted 19 lines · Tom Rini
Quoted 16 lines · Simon Goldschmidt

If _debug_uart_putc() is called before _debug_uart_init(), the ns16550 debug uart driver hangs in a tight loop waiting for the tx FIFO to get empty.

As this can happen via a printf sneaking in before the port calls debug_uart_init(), introduce a config option to ignore characters before the debug uart is initialized.

This is done by reading the baudrate divisor and aborting if is zero.

The Kconfig option is required as reading the baudrate divisor does not seem to work for all ns16500 compatibles (which is why the last attempt on this has been reverted in 1a67969a99).

Tested on socfpga_cyclone5_socrates.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Applied to u-boot/master, thanks!

I don't see this or any of my other patches in u-boot/master (neither github nor denx.de), is that expected?

No, that was strange. While I swore I had pushed, apparently I had not. Fixed, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190116/9a409462/attachment.sig>