[PATCH v3 04/10] serial: uniformize serial port I/O infos display

From: Hugo Villeneuve

Date: Thu May 21 2026 - 14:24:16 EST


From: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>

Uniformize serial port I/O infos display from three different functions
that display mostly the same information, but with some variations, by
adding a common function.

This make use of new functions uart_iotype_mmio() and
uart_iotype_legacy_io() to simplify and improve code readability.
This will prevent displaying irrelevant information for future IO types
(ex: UPIO_BUS), while also addressing the (eventually) invalid check for
"iotype >= UPIO_MEM".

This also allows us to remove the confusing cast to (unsigned long long)
for iobase which is defined as an unsigned long, and use %pa to display the
mapbase pointer, as it is done in earlycon_print_info().

Replace snprintf with more robust scnprintf so we could perhaps one day get
rid of snprintf() entirely.

Signed-off-by: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
---
On 32-bit cpu:
no changes.
On 64-bit cpu:
# cat /proc/tty/driver/sci
before
serinfo:1.0 driver revision:
0: uart:scif mmio:0x1004B800 irq:35 tx:11192 rx:37 RTS|CTS|DTR|DSR|CD
after:
serinfo:1.0 driver revision:
0: uart:scif mmio:0x000000001004b800 irq:35 tx:11339 rx:35 RTS|CTS|DTR|DSR|CD

Using uart_iotype_mmio() now includes the case for UPIO_TSI and UPIO_AU,
which fell before this patch in the else clause.
UPIO_TSI is no longer used by any driver, so not an issue.
UPIO_AU, if this is even possible for earlycon, falls into the "MMIO" category
in uart_line_info() and uart_report_port()...

Maybe the width information could be dropped entirely?
---
drivers/tty/serial/earlycon.c | 17 +++------
drivers/tty/serial/serial_core.c | 76 ++++++++++++++++++++++------------------
include/linux/serial_core.h | 1 +
3 files changed, 47 insertions(+), 47 deletions(-)

diff --git a/drivers/tty/serial/earlycon.c b/drivers/tty/serial/earlycon.c
index ab9af37f6cda35ea2e3ea966fdac8ba5c4475cb2..ce740cdc7cebffca0b4f6be98b8bca66540af72d 100644
--- a/drivers/tty/serial/earlycon.c
+++ b/drivers/tty/serial/earlycon.c
@@ -75,19 +75,12 @@ static void __init earlycon_print_info(struct earlycon_device *device)
{
struct console *earlycon = device->con;
struct uart_port *port = &device->port;
+ char ioinfos[64];

- if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
- port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
- pr_info("%s%d at MMIO%s %pa (options '%s')\n",
- earlycon->name, earlycon->index,
- (port->iotype == UPIO_MEM) ? "" :
- (port->iotype == UPIO_MEM16) ? "16" :
- (port->iotype == UPIO_MEM32) ? "32" : "32be",
- &port->mapbase, device->options);
- else
- pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
- earlycon->name, earlycon->index,
- port->iobase, device->options);
+ uart_get_ioinfos(port, ioinfos, sizeof(ioinfos));
+
+ pr_info("%s%d%s (options '%s')\n", earlycon->name, earlycon->index,
+ ioinfos, device->options);
}

static int __init parse_options(struct earlycon_device *device, char *options)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 432281c96f80ce3be317e3f53324923292b3eb44..afc7c1421ad54aeb7492bbda670939498b9018fc 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1998,9 +1998,9 @@ static void uart_line_info(struct seq_file *m, struct uart_state *state)
struct tty_port *port = &state->port;
enum uart_pm_state pm_state;
struct uart_port *uport;
+ char ioinfos[64];
char stat_buf[32];
unsigned int status;
- int mmio;

guard(mutex)(&port->mutex);

@@ -2008,13 +2008,10 @@ static void uart_line_info(struct seq_file *m, struct uart_state *state)
if (!uport)
return;

- mmio = uport->iotype >= UPIO_MEM;
- seq_printf(m, "%u: uart:%s %s%08llX irq:%u",
- uport->line, uart_type(uport),
- mmio ? "mmio:0x" : "port:",
- mmio ? (unsigned long long)uport->mapbase
- : (unsigned long long)uport->iobase,
- uport->irq);
+ seq_printf(m, "%u: uart:%s", uport->line, uart_type(uport));
+ uart_get_ioinfos(uport, ioinfos, sizeof(ioinfos));
+ seq_printf(m, "%s", ioinfos);
+ seq_printf(m, " irq:%u", uport->irq);

if (uport->type == PORT_UNKNOWN) {
seq_putc(m, '\n');
@@ -2476,38 +2473,47 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
}
EXPORT_SYMBOL(uart_resume_port);

+static const char *uart_get_mmio_width(struct uart_port *port)
+{
+ switch (port->iotype) {
+ case UPIO_MEM16:
+ return "16";
+ case UPIO_MEM32:
+ case UPIO_MEM32BE:
+ return "32be";
+ case UPIO_AU:
+ case UPIO_MEM:
+ default:
+ return "";
+ }
+}
+
+void uart_get_ioinfos(struct uart_port *port, char *buf, size_t size)
+{
+ buf[0] = '\0';
+
+ if (uart_iotype_mmio(port->iotype)) {
+ scnprintf(buf, size, " MMIO%s:%pa", uart_get_mmio_width(port), &port->mapbase);
+ } else if (uart_iotype_io(port->iotype)) {
+ if (port->iotype == UPIO_PORT)
+ scnprintf(buf, size, " I/O:0x%lx", port->iobase);
+ else if (port->iotype == UPIO_HUB6)
+ scnprintf(buf, size, " I/O:0x%lx, offset 0x%x", port->iobase, port->hub6);
+ }
+}
+EXPORT_SYMBOL(uart_get_ioinfos);
+
static inline void
uart_report_port(struct uart_driver *drv, struct uart_port *port)
{
- char address[64];
+ char ioinfos[64];

- switch (port->iotype) {
- case UPIO_PORT:
- snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
- break;
- case UPIO_HUB6:
- snprintf(address, sizeof(address),
- "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
- break;
- case UPIO_MEM:
- case UPIO_MEM16:
- case UPIO_MEM32:
- case UPIO_MEM32BE:
- case UPIO_AU:
- case UPIO_TSI:
- snprintf(address, sizeof(address),
- "MMIO 0x%llx", (unsigned long long)port->mapbase);
- break;
- default:
- strscpy(address, "*unknown*", sizeof(address));
- break;
- }
+ uart_get_ioinfos(port, ioinfos, sizeof(ioinfos));

- pr_info("%s%s%s at %s (irq = %u, base_baud = %u) is a %s\n",
- port->dev ? dev_name(port->dev) : "",
- port->dev ? ": " : "",
- port->name,
- address, port->irq, port->uartclk / 16, uart_type(port));
+ pr_info("%s%s%s%s (irq = %u, base_baud = %u) is a %s\n",
+ port->dev ? dev_name(port->dev) : "",
+ port->dev ? ": " : "",
+ port->name, ioinfos, port->irq, port->uartclk / 16, uart_type(port));

/* The magic multiplier feature is a bit obscure, so report it too. */
if (port->flags & UPF_MAGIC_MULTIPLIER)
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index a06da6b24c1ad48afffa1e355377992aec0debff..b91ad22be11b0d0b82928b9277cf29807796830e 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -1319,6 +1319,7 @@ static inline int uart_handle_break(struct uart_port *port)

int uart_get_rs485_mode(struct uart_port *port);

+void uart_get_ioinfos(struct uart_port *port, char *buf, size_t size);
bool uart_iotype_mmio(enum uart_iotype iotype);
bool uart_iotype_io(enum uart_iotype iotype);


--
2.47.3