Skip to contentSkip to conversation

serial: 8250: Add AN7581 UART support

PATCH Superseded Christian Marangi 6 participants 11 messages Jul 24 – Aug 8

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 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 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 (1):
serial: 8250: Add Airoha SoC UART and HSUART support

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

PATCH v2 1/2 dt-bindings: serial: 8250: Add Airoha compatibles

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 bb7b9c87a807..b6f7e093d8d7 100644
@@ -110,6 +110,11 @@ properties:
       - const: mrvl,pxa-uart
       - const: nuvoton,wpcm450-uart
       - const: nuvoton,npcm750-uart
+      - const: airoha,en7523-uart
+      - items:
+          - const: airoha,an7581-uart
+          - const: airoha,en7523-uart
+      - const: airoha,an7581-hsuart
       - const: nvidia,tegra20-uart
       - const: nxp,lpc3220-uart
       - items:
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/2 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 | 190 ++++++++++++++++++++++++++
 drivers/tty/serial/8250/8250_port.c   |  16 +++
 drivers/tty/serial/8250/Kconfig       |  11 ++
 drivers/tty/serial/8250/Makefile      |   1 +
 5 files changed, 224 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 +190 -0

190 lines not on this page · raw message

drivers/tty/serial/8250/8250_port.c +16 -0
index 630deb7dd344..22dd114256b3 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},
+		.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
#3 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] 32-bit arithmetic overflow during baud rate calculation before assignment to a 64-bit variable.…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] 32-bit arithmetic overflow during baud rate calculation before assignment to a 64-bit variable.
- [High] Calculated baud rate divisor xyd_x exceeds its 16-bit register field for high baud rates, causing silent truncation.
- [High] Failure to enforce 32-bit I/O access causes the 32-bit custom baud register write to be silently truncated to 8 bits.
- [High] Incomplete initialization of the rxtrig_bytes array breaks sysfs configuration and writes invalid trigger states to the hardware.
- [Medium] Premature integer division in the baud rate calculation amplifies fractional truncation errors.
- [Medium] The custom baud rate calculation hardcodes a 20MHz crystal clock, bypassing the framework's clock frequency synchronization. --

--- Patch [2]: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support --- commit 19616a2ff2ea1d768e60b410919225285f078ea9
Author: Christian Marangi <[email protected]>

serial: 8250: Add Airoha SoC UART and HSUART support

Add support for Airoha AN7523 UART and AN7581 HSUART. These implement a standard 16550 UART with only some custom logic for baud rate handling.

Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +0 -0
new file mode 100644

[ ... ]

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

[ ... ]

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

Quoted 5 lines
drivers/tty/serial/8250/8250_airoha.c new file +5 -0
+	/*
+	 * Calculate XYD_x and XINCLKDR register by searching
+	 * through a table of crystal_clock divisors.
+	 */
+	nom = baud * XYD_Y;

[Severity: High]
Since baud and XYD_Y are both 32-bit integers, will this multiplication overflow for standard baud rates (like 115200) before being assigned to the 64-bit nom variable? Should one of the operands be cast to u64?

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

Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +4 -0
+	for (i = 0 ; i < ARRAY_SIZE(airoha_clk_div_info) ; i++) {
+		clk_div_info = &airoha_clk_div_info[i];
+
+		xindiv_clk = XINDIV_CLOCK / clk_div_info->div;

[Severity: Medium]
This logic relies on a hardcoded 20MHz XINDIV_CLOCK macro. Does this bypass the framework's clock frequency synchronization, and should it use port->uartclk instead to respect the clock-frequency Device Tree property?

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

Quoted 1 line
drivers/tty/serial/8250/8250_airoha.c new file +1 -0
+		xyd_x = div_u64(nom, xindiv_clk) * 16;

[Severity: Medium]
Does performing the integer division before multiplying by 16 discard fractional data too early and amplify truncation errors? Would div_u64(nom * 16, xindiv_clk) preserve better precision?

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

Quoted 12 lines
drivers/tty/serial/8250/8250_airoha.c new file +12 -0
+
+		/* For the HSUART xyd_x needs to be scaled by a factor of 2 */
+		if (port->type == UART_PORT_AIROHA_HS)
+			xyd_x /= 2;
+
+		if (xyd_x < XYD_Y)
+			break;
+	}
+
+	serial_port_out(port, UART_AIROHA_XINCLKDR, clk_div_info->mask);
+	serial_port_out(port, UART_AIROHA_XYD,
+			FIELD_PREP(UART_AIROHA_XYD_X, xyd_x) |

[Severity: High]
For high baud rates, can xyd_x exceed 65535 when it exits the loop? If so, does FIELD_PREP() silently mask the oversized value to fit into the 16-bit UART_AIROHA_XYD_X mask, writing a truncated and incorrect divisor to the hardware?

Quoted 8 lines
drivers/tty/serial/8250/8250_airoha.c new file +8 -0
+			FIELD_PREP(UART_AIROHA_XYD_Y, XYD_Y));
+
+	/* Restore normal register access. */
+	serial_port_out(port, UART_LCR, up->lcr);
+}
+
+static int airoha_8250_probe(struct platform_device *pdev)
+{

[ ... ]

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

Quoted 7 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
+	uart.port.dev = dev;
+	if (device_is_compatible(dev, "airoha,an7581-hsuart"))
+		uart.port.type = UART_PORT_AIROHA_HS;
+	else
+		uart.port.type = UART_PORT_AIROHA;
+	uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
+			  UPF_FIXED_TYPE | UPF_IOREMAP;

[Severity: High]
Is uart.port.iotype = UPIO_MEM32 missing here?

Without enforcing 32-bit I/O access, if the Device Tree omits reg-io-width = <4>, the port defaults to UPIO_MEM. This causes the 32-bit register writes in airoha_set_divisor() to use 8-bit writeb() accessors, silently truncating the configuration.

Quoted 2 lines
drivers/tty/serial/8250/8250_airoha.c new file +2 -0
+	uart.port.set_divisor = airoha_set_divisor;
+	uart.port.get_divisor = airoha_get_divisor;

[ ... ]

Quoted 4 lines
drivers/tty/serial/8250/8250_port.c +0 -0
index e94a0802cbddc..13abfb471d185 100644

[ ... ]

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

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +6 -0
@@ -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},

[Severity: High]
Does leaving the trailing indices of rxtrig_bytes uninitialized (which implicitly become 0) break sysfs configuration?

When the user requests 4 bytes, bytes_to_fcr_rxtrig() checks bytes < conf_type->rxtrig_bytes[i]. 4 < 4 is false, and it will fall through to evaluate 4 < 0 for the uninitialized indices, eventually returning an undefined hardware state.

Should this array be fully populated with monotonically increasing values, like {1, 4, 4, 4}?

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

Quoted 8 lines
drivers/tty/serial/8250/8250_port.c +8 -0
+		.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: High]
The same uninitialized rxtrig_bytes issue appears to be present here as well.

Quoted 3 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+		.flags		= UART_CAP_FIFO,
+	},
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

In reply to Christian Marangi
Quoted 53 lines · 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 | 190 ++++++++++++++++++++++++++
 drivers/tty/serial/8250/8250_port.c   |  16 +++
 drivers/tty/serial/8250/Kconfig       |  11 ++
 drivers/tty/serial/8250/Makefile      |   1 +
 5 files changed, 224 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 +13 -0
new file mode 100644
index 000000000000..5d6d998bebd6
@@ -0,0 +1,190 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Airoha UART driver
+ *
+ * Copyright (c) 2025 Genexis Sweden AB
+ * Author: Benjamin Larsson <[email protected]>
+ *	   Christian Marangi <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/math64.h>
+#include <linux/property.h>
+#include <linux/serial_reg.h>

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+#include <linux/serial_8250.h>
+
+#include "8250.h"
+
+#define UART_AIROHA_BRDL	0
+#define UART_AIROHA_BRDH	1
+#define UART_AIROHA_XINCLKDR	10
+#define UART_AIROHA_XYD		11
+#define   UART_AIROHA_XYD_X	GENMASK(31, 16)
+#define   UART_AIROHA_XYD_Y	GENMASK(15, 0)

Please add linux/bits.h include.

Quoted 64 lines
drivers/tty/serial/8250/8250_airoha.c new file +64 -0
+
+struct airoha_8250_priv {
+	int line;
+};
+
+struct airoha_8250_clk_div_info {
+	unsigned int div;
+	u32 mask;
+};
+
+#define UART_BRDL_20M		0x01
+#define UART_BRDH_20M		0x00
+
+#define XINDIV_CLOCK		20000000
+#define XYD_Y			65000
+
+static const struct airoha_8250_clk_div_info airoha_clk_div_info[] = {
+	{ .div = 10, .mask = BIT(2) },
+	{ .div = 4, .mask = BIT(1) },
+	{ .div = 2, .mask = BIT(0) },
+};
+
+static unsigned int airoha_get_divisor(struct uart_port *port,
+				       unsigned int baud,
+				       unsigned int *frac)
+{
+	/* Hardware always uses BRDIV = 1. */
+	*frac = 0;
+
+	return 1;
+}
+
+/*
+ * Airoha UART baud rate calculation logic
+ *
+ * crystal_clock = 20 MHz (fixed frequency)
+ * xindiv_clock = crystal_clock / clock_div
+ * (x/y) = XYD, 32 bit register with 16 bits of x and then 16 bits of y
+ * clock_div = XINCLK_DIVCNT (default set to 10 (0x4)),
+ *           - 3 bit register [ 1, 2, 4, 8, 10, 12, 16, 20 ]
+ *
+ * baud_rate = ((xindiv_clock) * (x/y)) / ([BRDH,BRDL] * 16)
+ *
+ * Selecting divider needs to fulfill
+ * 1.8432 MHz <= xindiv_clk <= APB clock / 2
+ * The clocks are unknown but a divider of value 1 did not result in a valid
+ * waveform.
+ *
+ * XYD_y seems to need to be larger then XYD_x for proper waveform generation.
+ * Setting [BRDH,BRDL] to [0,1] and XYD_y to 65000 gives even values
+ * for usual baud rates.
+ */
+static void airoha_set_divisor(struct uart_port *port, unsigned int baud,
+			       unsigned int quot, unsigned int quot_frac)
+{
+	const struct airoha_8250_clk_div_info *clk_div_info;
+	struct uart_8250_port *up = up_to_u8250p(port);
+	u32 xindiv_clk;
+	u64 xyd_x, nom;
+	int i;
+
+	/* Set DLAB to access the baud rate divider registers (BRDH, BRDL) */
+	serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
+

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */
+	serial_port_out(port, UART_AIROHA_BRDL, UART_BRDL_20M);
+	serial_port_out(port, UART_AIROHA_BRDH, UART_BRDH_20M);
+
+	/*
+	 * Calculate XYD_x and XINCLKDR register by searching
+	 * through a table of crystal_clock divisors.
+	 */
+	nom = baud * XYD_Y;
+	for (i = 0 ; i < ARRAY_SIZE(airoha_clk_div_info) ; i++) {

Please add include for ARRAY_SIZE()

Quoted 13 lines
drivers/tty/serial/8250/8250_airoha.c new file +13 -0
+		clk_div_info = &airoha_clk_div_info[i];
+
+		xindiv_clk = XINDIV_CLOCK / clk_div_info->div;
+		xyd_x = div_u64(nom, xindiv_clk) * 16;
+
+		/* For the HSUART xyd_x needs to be scaled by a factor of 2 */
+		if (port->type == UART_PORT_AIROHA_HS)
+			xyd_x /= 2;
+
+		if (xyd_x < XYD_Y)
+			break;
+	}
+

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+	serial_port_out(port, UART_AIROHA_XINCLKDR, clk_div_info->mask);
+	serial_port_out(port, UART_AIROHA_XYD,
+			FIELD_PREP(UART_AIROHA_XYD_X, xyd_x) |
+			FIELD_PREP(UART_AIROHA_XYD_Y, XYD_Y));
+
+	/* Restore normal register access. */
+	serial_port_out(port, UART_LCR, up->lcr);
+}
+
+static int airoha_8250_probe(struct platform_device *pdev)

You need to add include for platform_device as you'll be dereferencing it below.

Quoted 18 lines
drivers/tty/serial/8250/8250_airoha.c new file +18 -0
+{
+	struct uart_8250_port uart = { };
+	struct device *dev = &pdev->dev;
+	struct airoha_8250_priv *priv;
+	struct resource *res;
+	int ret;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return dev_err_probe(dev, -EINVAL, "invalid address\n");
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	uart.port.dev = dev;
+	if (device_is_compatible(dev, "airoha,an7581-hsuart"))
+		uart.port.type = UART_PORT_AIROHA_HS;

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+	else
+		uart.port.type = UART_PORT_AIROHA;
+	uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
+			  UPF_FIXED_TYPE | UPF_IOREMAP;
+	uart.port.set_divisor = airoha_set_divisor;
+	uart.port.get_divisor = airoha_get_divisor;
+	uart.port.mapbase = res->start;
+	uart.port.mapsize = resource_size(res);
+
+	ret = uart_read_and_validate_port_properties(&uart.port);

Add include for this.

Quoted 11 lines
drivers/tty/serial/8250/8250_airoha.c new file +11 -0
+	if (ret)
+		return ret;
+
+	ret = serial8250_register_8250_port(&uart);
+	if (ret < 0)
+		return ret;
+
+	priv->line = ret;
+	platform_set_drvdata(pdev, priv);
+
+	return 0;

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+}
+
+static void airoha_8250_remove(struct platform_device *ofdev)
+{
+	struct airoha_8250_priv *priv = platform_get_drvdata(ofdev);
+
+	serial8250_unregister_port(priv->line);
+}
+
+static const struct of_device_id airoha_8250_dt_ids[] = {

Add include for of_device_id.

I think linux/of.h is the one you want here, as it will be including the new linux/device-id/of.h.

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

Quoted 5 lines
drivers/tty/serial/8250/8250_airoha.c new file +5 -0
+	{ .compatible = "airoha,en7523-uart" },
+	{ .compatible = "airoha,an7581-hsuart" },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, airoha_8250_dt_ids);

+ linux/module.h

Quoted 6 lines
drivers/tty/serial/8250/8250_airoha.c new file +6 -0
+
+static struct platform_driver airoha_8250_driver = {
+	.driver = {
+		.name = "8250_airoha",
+		.of_match_table = airoha_8250_dt_ids,
+	},

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +10 -0
+	.probe = airoha_8250_probe,
+	.remove = airoha_8250_remove,
+};
+
+module_platform_driver(airoha_8250_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Airoha UART driver");
+MODULE_AUTHOR("Benjamin Larsson <[email protected]>");
+MODULE_AUTHOR("Christian Marangi <[email protected]>");

These too would need include but it will be the same module.h as earlier.

It seems you've not really paid attention to includes so far, so you should do a pass through you code and check you're directly including this you use (defines, macros, functions, structs you're dereferencing). I might have missed some.

Do not rely on indirect includes as they're fragile and hamper (potential) header refactoring efforts by introducing build landmines. Only when the include path is "guaranteed" such as linux/of.h -> linux/device-id/of.h or linux/xx.h -> asm/xx.h you can rely on the linux/ one.

Quoted 14 lines
drivers/tty/serial/8250/8250_port.c +6 -0
index 630deb7dd344..22dd114256b3 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},

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

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +10 -0
+		.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,
+	},

Not exactly a problem in your patch but I really which this whole uart_config[] thing would be deprecated properly.

We already have fifosize and fcr in uart_port, that should already cover most of the variations but I guess a bit of supporting code would be needed. serial8250_config_port() assigns fcr from uart_config[] and I'm not entire sure if it would be enough to just do like

	if (up->fcr)
		up->fcr = uart_config[up->port.type].fcr;

(That is, is this also used to reset configuration? I dunno for sure.)

Signature · 1 line
 i.
#5 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

Quoted 1 line · Ilpo Järvinen

On Fri, 24 Jul 2026, Christian Marangi wrote:

...

Quoted 6 lines
Quoted 1 line

+static const struct of_device_id airoha_8250_dt_ids[] = {

Add include for of_device_id.

I think linux/of.h is the one you want here, as it will be including the new linux/device-id/of.h.

platform_device.h covers it.

Quoted 4 lines
Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +4 -0
+	{ .compatible = "airoha,en7523-uart" },
+	{ .compatible = "airoha,an7581-hsuart" },
+	{ },
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

In reply to Christian Marangi
Quoted 4 lines · 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.

I tried not to clash with Ilpo's review (all he said is still applicable here).

...

Quoted 1 line

+#define XINDIV_CLOCK 20000000

20 * HZ_PER_MHZ

(should include units.h)

...

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

Quoted 5 lines
drivers/tty/serial/8250/8250_airoha.c new file +5 -0
+static const struct airoha_8250_clk_div_info airoha_clk_div_info[] = {
+	{ .div = 10, .mask = BIT(2) },
+	{ .div = 4, .mask = BIT(1) },
+	{ .div = 2, .mask = BIT(0) },
+};

The (reversed) array of integers should suffice.

static const unsigned int airoha_clk_divs[] = { 2, 4, 10 };

See below for more.

...

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

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

Not used outside of the loop, define it there.

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

Quoted 6 lines
drivers/tty/serial/8250/8250_airoha.c new file +6 -0
+	/* Set DLAB to access the baud rate divider registers (BRDH, BRDL) */
+	serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
+
+	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */
+	serial_port_out(port, UART_AIROHA_BRDL, UART_BRDL_20M);
+	serial_port_out(port, UART_AIROHA_BRDH, UART_BRDH_20M);

The above three calls repeat serial8250_do_set_divisor(), don't they?

	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */
	serial8250_do_set_divisor(port, baud, 1);

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

Quoted 9 lines
drivers/tty/serial/8250/8250_airoha.c new file +8 -0
+	/*
+	 * Calculate XYD_x and XINCLKDR register by searching
+	 * through a table of crystal_clock divisors.
+	 */
+	nom = baud * XYD_Y;
 
+	for (i = 0 ; i < ARRAY_SIZE(airoha_clk_div_info) ; i++) {
+		clk_div_info = &airoha_clk_div_info[i];
+		xindiv_clk = XINDIV_CLOCK / clk_div_info->div;
	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info) - 1; i >= 0; i--) {
		xindiv_clk = XINDIV_CLOCK / BIT(i);

Also variant (but may be a little bit confusing)

	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info); i; i--) {
		xindiv_clk = XINDIV_CLOCK / BIT(i - 1);
Quoted 7 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
+		xyd_x = div_u64(nom, xindiv_clk) * 16;
+
+		/* For the HSUART xyd_x needs to be scaled by a factor of 2 */
+		if (port->type == UART_PORT_AIROHA_HS)
+			xyd_x /= 2;
+
+		if (xyd_x < XYD_Y)

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +9 -0
+			break;
+	}
+
+	serial_port_out(port, UART_AIROHA_XINCLKDR, clk_div_info->mask);
+	serial_port_out(port, UART_AIROHA_XYD,
+			FIELD_PREP(UART_AIROHA_XYD_X, xyd_x) |
+			FIELD_PREP(UART_AIROHA_XYD_Y, XYD_Y));
 
+	/* Restore normal register access. */
+	serial_port_out(port, UART_LCR, up->lcr);

Hmm... do you really need this? It doesn't seem required (at least for many other 8250 compatible devices).

Quoted 1 line

+}

...

Quoted 21 lines
drivers/tty/serial/8250/8250_airoha.c new file +21 -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;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return dev_err_probe(dev, -EINVAL, "invalid address\n");
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	uart.port.dev = dev;
+	if (device_is_compatible(dev, "airoha,an7581-hsuart"))
+		uart.port.type = UART_PORT_AIROHA_HS;
+	else
+		uart.port.type = UART_PORT_AIROHA;

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
 
+	uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
+			  UPF_FIXED_TYPE | UPF_IOREMAP;
 
+	uart.port.set_divisor = airoha_set_divisor;
+	uart.port.get_divisor = airoha_get_divisor;
+	uart.port.mapbase = res->start;
+	uart.port.mapsize = resource_size(res);
 
+	ret = uart_read_and_validate_port_properties(&uart.port);

I'm not sure about 'validate' as only one or two drivers use it. Just double check that this is indeed what you want (read the kernel-doc for that function carefully, it's not that trivial, unfortunately).

Quoted 12 lines
drivers/tty/serial/8250/8250_airoha.c new file +12 -0
+	if (ret)
+		return ret;
+
+	ret = serial8250_register_8250_port(&uart);
+	if (ret < 0)
+		return ret;
+
+	priv->line = ret;
+	platform_set_drvdata(pdev, priv);
+
+	return 0;
+}

...

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

Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +4 -0
+static const struct of_device_id airoha_8250_dt_ids[] = {
+	{ .compatible = "airoha,en7523-uart" },
+	{ .compatible = "airoha,an7581-hsuart" },
+	{ },

No comma in the terminator.

Quoted 1 line

+};

...

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +9 -0
+static struct platform_driver airoha_8250_driver = {
+	.driver = {
+		.name = "8250_airoha",
+		.of_match_table = airoha_8250_dt_ids,
+	},
+	.probe = airoha_8250_probe,
+	.remove = airoha_8250_remove,
+};
 
+

Drop this redundant blank line.

Quoted 1 line

+module_platform_driver(airoha_8250_driver);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

Quoted 1 line · Andy Shevchenko

On Fri, Jul 24, 2026 at 08:30:06PM +0200, Christian Marangi wrote:

...

Quoted 11 lines

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

Quoted 6 lines
drivers/tty/serial/8250/8250_airoha.c new file +6 -0
+	/* Set DLAB to access the baud rate divider registers (BRDH, BRDL) */
+	serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
+
+	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */
+	serial_port_out(port, UART_AIROHA_BRDL, UART_BRDL_20M);
+	serial_port_out(port, UART_AIROHA_BRDH, UART_BRDH_20M);

The above three calls repeat serial8250_do_set_divisor(), don't they?

	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */
	serial8250_do_set_divisor(port, baud, 1);

Or with the united definition

	serial8250_do_set_divisor(port, baud, UART_BRD_20M);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Christian Marangi
drivers/tty/serial/8250/8250_airoha.c new file +5 -0
+	/*
+	 * Calculate XYD_x and XINCLKDR register by searching
+	 * through a table of crystal_clock divisors.
+	 */
+	nom = baud * XYD_Y;
Quoted 5 lines

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

Quoted 3 lines
drivers/tty/serial/8250/8250_airoha.c new file +3 -0
+	for (i = 0 ; i < ARRAY_SIZE(airoha_clk_div_info) ; i++) {
+		clk_div_info = &airoha_clk_div_info[i];
+		xindiv_clk = XINDIV_CLOCK / clk_div_info->div;
	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info) - 1; i >= 0; i--) {

Due to unsignedness, isn't this a lopp for ever?

Quoted 3 lines
		xindiv_clk = XINDIV_CLOCK / BIT(i);

Also variant (but may be a little bit confusing)

But a little bit more correct :).

Quoted 3 lines
	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info); i; i--) {
		xindiv_clk = XINDIV_CLOCK / BIT(i - 1);

thanks,

Signature · 2 lines
js
suse labs
#9 lore raw

Re: [PATCH v2 2/2] serial: 8250: Add Airoha SoC UART and HSUART support

Quoted 2 lines · Jiri Slaby
Quoted 1 line · Andy Shevchenko

On Fri, Jul 24, 2026 at 08:30:06PM +0200, Christian Marangi wrote:

...

Quoted 7 lines
Quoted 5 lines

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

Quoted 3 lines
drivers/tty/serial/8250/8250_airoha.c new file +3 -0
+	for (i = 0 ; i < ARRAY_SIZE(airoha_clk_div_info) ; i++) {
+		clk_div_info = &airoha_clk_div_info[i];
+		xindiv_clk = XINDIV_CLOCK / clk_div_info->div;
	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info) - 1; i >= 0; i--) {

Due to unsignedness, isn't this a lopp for ever?

Oh, true! In the v3 it seems using correct type (signed one).

Quoted 8 lines
Quoted 3 lines
		xindiv_clk = XINDIV_CLOCK / BIT(i);

Also variant (but may be a little bit confusing)

But a little bit more correct :).

Quoted 2 lines
	for (unsigned int i = ARRAY_SIZE(airoha_clk_div_info); i; i--) {
		xindiv_clk = XINDIV_CLOCK / BIT(i - 1);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
In reply to Christian Marangi
Quoted 10 lines · Christian Marangi

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]/

For your next version, please address the review comments found here:

	https://sashiko.dev/#/patchset/[email protected]

thanks,

greg k-h