Skip to contentSkip to conversation

serial: 8250: Add AN7581 UART support

PATCH Superseded Christian Marangi 3 participants 7 messages Aug 10 – Aug 11

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover serial: 8250: Add AN7581 UART support

This is a new version of [1] to support UART driver for Airoha SoC.

One additional function was needed serial8250_get_baud_rate() for the driver to work correctly for baud rate calculation.

While at it also try to clarify a long standing issue with new UART driver when adding new ids for uart_config[].

[1] https://lore.kernel.org/all/[email protected]/

Changes v5:
- Improve 8250 YAML schema with enum
- Keep alphabetical order for YAML schema

Changes v4:
- Add Review tag
- Add errno and types header
- Drop redundant () from comment
- Use define for get_divisor

Changes v3:
- Address review from ai bot
- Add all missing kernel headers
- Improve divisor calculation
- Fix wrong uart config table
- Simplify divisor table handling

Changes v2:
- Drop intermediate patch
- Use .set_divisor OPs
- Use div_u64 instead of / 40 for 32 bit overflow
- Drop dedicated match table and use compatible for type

Changes compared to [1]:
- Fix all formal error
- Use better compatible names
- Drop unneeded header
- Drop usage of irq (it's filled by the generic function)
- General code cleanup and reorg
- Split to patch and add the UAPI map patch

Benjamin Larsson (1):
dt-bindings: serial: 8250: Add Airoha compatibles

Christian Marangi (2):
dt-bindings: serial: 8250: Reorganize compatible to single enum
serial: 8250: Add Airoha SoC UART and HSUART support

 .../devicetree/bindings/serial/8250.yaml      |  32 +--
 drivers/tty/serial/8250/8250.h                |   6 +
 drivers/tty/serial/8250/8250_airoha.c         | 187 ++++++++++++++++++
 drivers/tty/serial/8250/8250_port.c           |  16 ++
 drivers/tty/serial/8250/Kconfig               |  11 ++
 drivers/tty/serial/8250/Makefile              |   1 +
 6 files changed, 240 insertions(+), 13 deletions(-)
 create mode 100644 drivers/tty/serial/8250/8250_airoha.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 1/3 dt-bindings: serial: 8250: Reorganize compatible to single enum

Instead of declaring each single compatible string with a const, move them to a single enum and reorder them.

First put the generic ns one and then follow alphabetical order.

---
 .../devicetree/bindings/serial/8250.yaml      | 27 ++++++++++---------
 1 file changed, 14 insertions(+), 13 deletions(-)
Documentation/devicetree/bindings/serial/8250.yaml +14 -13
index bb7b9c87a807..039bd392f876 100644
@@ -99,19 +99,20 @@ allOf:
 properties:
   compatible:
     oneOf:
-      - const: ns8250
-      - const: ns16450
-      - const: ns16550
-      - const: ns16550a
-      - const: ns16850
-      - const: aspeed,ast2400-vuart
-      - const: aspeed,ast2500-vuart
-      - const: intel,xscale-uart
-      - const: mrvl,pxa-uart
-      - const: nuvoton,wpcm450-uart
-      - const: nuvoton,npcm750-uart
-      - const: nvidia,tegra20-uart
-      - const: nxp,lpc3220-uart
+      - enum:
+          - ns8250
+          - ns16450
+          - ns16550
+          - ns16550a
+          - ns16850
+          - aspeed,ast2400-vuart
+          - aspeed,ast2500-vuart
+          - intel,xscale-uart
+          - mrvl,pxa-uart
+          - nuvoton,npcm750-uart
+          - nuvoton,wpcm450-uart
+          - nvidia,tegra20-uart
+          - nxp,lpc3220-uart
       - items:
           - enum:
               - exar,xr16l2552
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v5 1/3] dt-bindings: serial: 8250: Reorganize compatible to single enum

Quoted 2 lines · Christian Marangi

Instead of declaring each single compatible string with a const, move them to a single enum and reorder them.

Reordering is tricky to spot. You mean reordered Nuvoton?

Quoted 2 lines

First put the generic ns one and then follow alphabetical order.

s/one/first/

Quoted 5 lines
---
 .../devicetree/bindings/serial/8250.yaml      | 27 ++++++++++---------
 1 file changed, 14 insertions(+), 13 deletions(-)

Best regards,
Krzysztof

#3 lore raw

PATCH v5 2/3 dt-bindings: serial: 8250: Add Airoha compatibles

In reply to Christian Marangi

From: Benjamin Larsson <[email protected]>

The Airoha SoC family have a mostly 16550-compatible UART and High-Speed UART hardware with the exception of custom baud rate settings register.

---
 Documentation/devicetree/bindings/serial/8250.yaml | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/serial/8250.yaml +5 -0
index 039bd392f876..9a3f596f8a0e 100644
@@ -105,6 +105,8 @@ properties:
           - ns16550
           - ns16550a
           - ns16850
+          - airoha,an7581-hsuart
+          - airoha,en7523-uart
           - aspeed,ast2400-vuart
           - aspeed,ast2500-vuart
           - intel,xscale-uart
@@ -113,6 +115,9 @@ properties:
           - nuvoton,wpcm450-uart
           - nvidia,tegra20-uart
           - nxp,lpc3220-uart
+      - items:
+          - const: airoha,an7581-uart
+          - const: airoha,en7523-uart
       - items:
           - enum:
               - exar,xr16l2552
Signature · 1 line
2.53.0
Acked-by on v5 2/3
#4 lore raw

Re: [PATCH v5 2/3] dt-bindings: serial: 8250: Add Airoha compatibles

Quoted 5 lines · Christian Marangi

From: Benjamin Larsson <[email protected]>

The Airoha SoC family have a mostly 16550-compatible UART and High-Speed UART hardware with the exception of custom baud rate settings register.

Looks wrongly wrapped.

Please wrap commit message according to Linux coding style / submission process (neither too early nor over the limit):
https://elixir.bootlin.com/linux/v6.4-rc1/source/Documentation/process/submitting-patches.rst#L597

Best regards,
Krzysztof

#5 lore raw

PATCH v5 3/3 serial: 8250: Add Airoha SoC UART and HSUART support

In reply to Christian Marangi

Add support for Airoha AN7523 UART and AN7581 HSUART.

These implement a standard 16550 UART with only some custom logic for baud rate handling.

---
 drivers/tty/serial/8250/8250.h        |   6 +
 drivers/tty/serial/8250/8250_airoha.c | 187 ++++++++++++++++++++++++++
 drivers/tty/serial/8250/8250_port.c   |  16 +++
 drivers/tty/serial/8250/Kconfig       |  11 ++
 drivers/tty/serial/8250/Makefile      |   1 +
 5 files changed, 221 insertions(+)
 create mode 100644 drivers/tty/serial/8250/8250_airoha.c
drivers/tty/serial/8250/8250.h +6 -0
index 77fe0588fd6b..e6b2b9431229 100644
@@ -73,6 +73,12 @@ struct serial8250_config {
 	unsigned int	flags;
 };
 
+enum uart_port_type {
+	/* Internal 8250 only */
+	UART_PORT_AIROHA		= 124,
+	UART_PORT_AIROHA_HS		= 125,
+};
+
 #define UART_CAP_FIFO	BIT(8)	/* UART has FIFO */
 #define UART_CAP_EFR	BIT(9)	/* UART has EFR */
 #define UART_CAP_SLEEP	BIT(10)	/* UART has IER sleep */
drivers/tty/serial/8250/8250_airoha.c new file +187 -0

187 lines not on this page · raw message

drivers/tty/serial/8250/8250_port.c +16 -0
index 630deb7dd344..c888cae7d98b 100644
@@ -311,6 +311,22 @@ static const struct serial8250_config uart_config[] = {
 		.rxtrig_bytes	= {1, 8, 16, 30},
 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
 	},
+	[UART_PORT_AIROHA] = {
+		.name		= "Airoha UART",
+		.fifo_size	= 8,
+		.tx_loadsz	= 1,
+		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR,
+		.rxtrig_bytes	= {1, 4, 4, 4},
+		.flags		= UART_CAP_FIFO,
+	},
+	[UART_PORT_AIROHA_HS] = {
+		.name		= "Airoha HSUART",
+		.fifo_size	= 128,
+		.tx_loadsz	= 128,
+		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR,
+		.rxtrig_bytes	= {1, 4},
+		.flags		= UART_CAP_FIFO,
+	},
 };
 
 /* Uart divisor latch read */
drivers/tty/serial/8250/Kconfig +11 -0
index fc3e58d62233..310da7af7a49 100644
@@ -337,6 +337,17 @@ config SERIAL_8250_ACORN
 	  system, say Y to this option.  The driver can handle 1, 2, or 3 port
 	  cards.  If unsure, say N.
 
+config SERIAL_8250_AIROHA
+	tristate "Airoha UART support"
+	depends on ARCH_AIROHA || COMPILE_TEST
+	depends on SERIAL_8250
+	help
+	  Selecting this option enables an Airoha SoC specific baud rate
+	  calculation routine on an otherwise 16550 compatible UART hardware.
+
+	  If you have an Airoha based board and want to use the serial port,
+	  say Y to this option. If unsure, say N.
+
 config SERIAL_8250_BCM2835AUX
 	tristate "BCM2835 auxiliar mini UART support"
 	depends on ARCH_BCM2835 || COMPILE_TEST
drivers/tty/serial/8250/Makefile +1 -0
index 6d21402b4435..83257696ea8a 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_SERIAL_8250_CONSOLE)	+= 8250_early.o
 
 obj-$(CONFIG_SERIAL_8250_ACCENT)	+= 8250_accent.o
 obj-$(CONFIG_SERIAL_8250_ACORN)		+= 8250_acorn.o
+obj-$(CONFIG_SERIAL_8250_AIROHA)	+= 8250_airoha.o
 obj-$(CONFIG_SERIAL_8250_ASPEED_VUART)	+= 8250_aspeed_vuart.o
 obj-$(CONFIG_SERIAL_8250_BCM2835AUX)	+= 8250_bcm2835aux.o
 obj-$(CONFIG_SERIAL_8250_BCM7271)	+= 8250_bcm7271.o
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH v5 3/3] serial: 8250: Add Airoha SoC UART and HSUART support

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Deadlock (if console) and Log Flooding (if non-console) via unratelimited dev_err() under port…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Deadlock (if console) and Log Flooding (if non-console) via unratelimited dev_err() under port lock
- [High] Silent probe failure or mismatched baud rate validation due to uninitialized port->uartclk
- [Medium] Driver squats on global UAPI port type namespace
- [Medium] Incomplete rxtrig_bytes initialization sets undefined hardware state and breaks sysfs reads --

--- Patch [3]: [PATCH v5 3/3] serial: 8250: Add Airoha SoC UART and HSUART support --- commit 5a2afaf1ec03e067e37973b1f2ee37f044a204f9
Author: Christian Marangi <[email protected]>

serial: 8250: Add Airoha SoC UART and HSUART support

This commit adds support for Airoha AN7523 UART and AN7581 HSUART. It provides a standard 16550 UART driver with custom logic for baud rate handling.

drivers/tty/serial/8250/8250.h struct serial8250_config { · line 80

Quoted 13 lines
drivers/tty/serial/8250/8250.h +5 -0
index 77fe0588fd6bd..e6b2b94312292 100644
@@ -73,6 +73,12 @@ struct serial8250_config {
 	unsigned int	flags;
 };
 
+enum uart_port_type {
+	/* Internal 8250 only */
+	UART_PORT_AIROHA		= 124,
+	UART_PORT_AIROHA_HS		= 125,
+};

[Severity: Medium]
Can this driver safely use these values without them being allocated in the global UAPI header?

The port->type values are part of the global userspace UAPI, defined in include/uapi/linux/serial_core.h, and are exposed to userspace via the TIOCGSERIAL ioctl. Since the highest officially allocated port type is 123, assigning 124 and 125 internally rather than in the UAPI header risks a future collision.

If a new generic serial driver is legitimately assigned 124 in the UAPI header, it will silently collide with UART_PORT_AIROHA, causing uart_config array index collisions and userspace misidentification of the port type.

[ ... ]

Quoted 5 lines
drivers/tty/serial/8250/8250_airoha.c new file +0 -0
new file mode 100644
index 0000000000000..fe72a39561c28

[ ... ]

Quoted 7 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
+static void airoha_set_divisor(struct uart_port *port, unsigned int baud,
+			       unsigned int quot, unsigned int quot_frac)
+{
+	struct uart_8250_port *up = up_to_u8250p(port);
+	u32 xindiv_clk;
+	u64 xyd_x, nom;
+	int div_bit;

[ ... ]

drivers/tty/serial/8250/8250_airoha.c line 107

Quoted 6 lines
drivers/tty/serial/8250/8250_airoha.c new file +6 -0
+	/* Couldn't find a valid xyd_x */
+	if (div_bit < 0) {
+		dev_err(port->dev, "failed to find suitable clock divisor for baud %u\n",
+			baud);
+		return;
+	}

[Severity: High]
Is it safe to call dev_err() here?

This function is invoked via serial8250_do_set_termios() with the raw spinlock port->lock held. If this port is the active serial console, dev_err() invokes printk(), which synchronously calls serial8250_console_write(). The console write function will attempt to acquire the exact same port->lock, resulting in an instant self-deadlock.

If the port is not the console, the error is unratelimited. This allows a local user with write access to the TTY device to spam the kernel log by requesting unsupported high baud rates via the TCSETS2 ioctl in a tight loop.

[ ... ]

Quoted 7 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
+static int airoha_8250_probe(struct platform_device *pdev)
+{
+	struct uart_8250_port uart = { };
+	struct device *dev = &pdev->dev;
+	struct airoha_8250_priv *priv;
+	struct resource *res;
+	int ret;

[ ... ]

drivers/tty/serial/8250/8250_airoha.c line 146

Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +4 -0
+	uart.port.mapbase = res->start;
+	uart.port.mapsize = resource_size(res);
+
+	ret = uart_read_and_validate_port_properties(&uart.port);

[Severity: High]
Does this code need to initialize uart.port.uartclk before parsing properties?

The hardcoded divisor logic in airoha_set_divisor() relies on a 20 MHz crystal (XINDIV_CLOCK = 20 * HZ_PER_MHZ). However, uartclk isn't explicitly set before or after calling uart_read_and_validate_port_properties().

If the device tree omits the legacy clock-frequency property (e.g., using the modern clocks property instead), uartclk remains 0. This causes serial8250_register_8250_port() to reject the port with -EINVAL, resulting in a silent probe failure.

If the device tree specifies a different frequency (like a 100MHz APB bus clock), the serial core will bound baud rates based on that incorrect frequency. This would allow unsupported high baud rates to reach the hardcoded 20MHz divisor logic, repeatedly triggering the deadlock or log-flooding issue above.

[ ... ]

Quoted 12 lines
drivers/tty/serial/8250/8250_port.c +4 -0
index e94a0802cbddc..11728a789922d 100644
@@ -311,6 +311,22 @@ static const struct serial8250_config uart_config[] = {
 		.rxtrig_bytes	= {1, 8, 16, 30},
 		.flags		= UART_CAP_FIFO | UART_CAP_AFE,
 	},
+	[UART_PORT_AIROHA] = {
+		.name		= "Airoha UART",
+		.fifo_size	= 8,
+		.tx_loadsz	= 1,

drivers/tty/serial/8250/8250_port.c static const struct serial8250_config uart_config[] = { · line 327

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +10 -0
+		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR,
+		.rxtrig_bytes	= {1, 4, 4, 4},
+		.flags		= UART_CAP_FIFO,
+	},
+	[UART_PORT_AIROHA_HS] = {
+		.name		= "Airoha HSUART",
+		.fifo_size	= 128,
+		.tx_loadsz	= 128,
+		.fcr		= UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR,
+		.rxtrig_bytes	= {1, 4},

[Severity: Medium]
Will this partially initialized rxtrig_bytes array break sysfs reads?

The rxtrig_bytes array is 4 elements long, but only two are initialized here, leaving the remaining elements as implicitly 0.

When a root user configures a trigger level > 4 via /sys/class/tty/ttyS*/rx_trig_bytes, bytes_to_fcr_rxtrig() iterates over this array checking bytes < conf_type->rxtrig_bytes[i]. Because the uninitialized elements are 0, the condition evaluates to false, and the function returns an undefined trigger state (UART_FCR_R_TRIG_11).

When sysfs later tries to read the value via fcr_get_rxtrig_bytes(), it will fetch 0 from the array and return -EOPNOTSUPP. Padding the array with identical maximum values (e.g., {1, 4, 4, 4}) like the standard Airoha UART would avoid this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3