Skip to contentSkip to conversation

serial: 8250: Add AN7581 UART support

PATCH Superseded Christian Marangi 3 participants 5 messages Aug 7 – Aug 8

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 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 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 (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         | 186 ++++++++++++++++++
 drivers/tty/serial/8250/8250_port.c           |  16 ++
 drivers/tty/serial/8250/Kconfig               |  11 ++
 drivers/tty/serial/8250/Makefile              |   1 +
 6 files changed, 225 insertions(+)
 create mode 100644 drivers/tty/serial/8250/8250_airoha.c
Signature · 1 line
2.53.0
#1 lore raw

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

186 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
#3 lore raw

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

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unhandled high baud rates leave UART hardware in an inconsistent, desynchronized state due to…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unhandled high baud rates leave UART hardware in an inconsistent, desynchronized state due to mismatched clock limits.
- [Medium] Silent truncation of 32-bit custom register writes if reg-io-width is omitted from the device tree.
- [Medium] Incorrect initialization of rxtrig_bytes array causes FCR misconfiguration and breaks sysfs reads. --

--- Patch [2]: [PATCH v3 2/2] serial: 8250: Add Airoha SoC UART and HSUART support --- commit af8c5302b76a32f7b1c0a8787d38cbf61806b1bc
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
index 0000000000000..5cc9b8f8a6181

[ ... ]

Quoted 3 lines
drivers/tty/serial/8250/8250_airoha.c new file +3 -0
+static void airoha_set_divisor(struct uart_port *port, unsigned int baud,
+			       unsigned int quot, unsigned int quot_frac)
+{

[ ... ]

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

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]
If userspace requests a high baud rate that cannot find a valid xyd_x, the loop completes with div_bit < 0 and returns early. Could this abandon the divisor setup and leave the hardware completely desynchronized with DLAB set in the LCR register, since the LCR restore step at the end of the function is skipped?

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

Quoted 5 lines
drivers/tty/serial/8250/8250_airoha.c new file +5 -0
+
+	serial_port_out(port, UART_AIROHA_XINCLKDR, BIT(div_bit));
+	serial_port_out(port, UART_AIROHA_XYD,
+			FIELD_PREP(UART_AIROHA_XYD_X, xyd_x) |
+			FIELD_PREP(UART_AIROHA_XYD_Y, XYD_Y));

[Severity: Medium]
Since this constructs a 32-bit payload, could the upper 24 bits be silently truncated by an 8-bit writeb() accessor here?

If the optional reg-io-width property is omitted from the device tree, uart_read_and_validate_port_properties() called during airoha_8250_probe() defaults uart.port.iotype to UPIO_MEM (8-bit) because it wasn't explicitly set to UPIO_MEM32 beforehand.

Quoted 4 lines
drivers/tty/serial/8250/8250_airoha.c new file +4 -0
+
+	/* Restore normal register access. */
+	serial_port_out(port, UART_LCR, up->lcr);
+}

[ ... ]

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +0 -0
index e94a0802cbddc..11728a789922d 100644

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

Quoted 9 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, 4, 4},

[Severity: Medium]
The rxtrig_bytes arrays here don't appear to be strictly monotonically increasing.

If userspace writes '4' to the rx_trig_bytes sysfs file, is it possible this fails the bytes_to_fcr_rxtrig() loop checks (since 4 < 4 and 4 < 0 are false), returning index 3 and misconfiguring the FCR trigger bits?

Quoted 11 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,
+	},
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 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.

Some cosmetic issues below.
With that being addressed,

...

Quoted 5 lines

+++ b/drivers/tty/serial/8250/8250_airoha.c

+ * Author: Benjamin Larsson <[email protected]>
+ * Christian Marangi <[email protected]>

Authors:

...

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

Quoted 3 lines
drivers/tty/serial/8250/8250_airoha.c new file +3 -0
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/bitfield.h>

+ errno.h

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

Quoted 8 lines
drivers/tty/serial/8250/8250_airoha.c new file +7 -0
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/property.h>
+#include <linux/platform_device.h>
+#include <linux/serial_8250.h>
 
+#include <linux/serial_core.h>
+#include <linux/serial_reg.h>

These two are kinda guaranteed by serial_8250.h. At least IIRC some other 8250 drivers rely on this and it sounds logical.

+ types.h // uXX

Quoted 1 line

+#include <linux/units.h>

...

Quoted 1 line

+#define UART_BRD_20M 0x1

Perhaps 0x0001 to show that this is 16-bit value?

...

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

Quoted 8 lines
drivers/tty/serial/8250/8250_airoha.c new file +8 -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;

Shouldn't this be the same as UART_BRD_20M? I mean return a defined constant.

Quoted 1 line

+}

...

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

Quoted 9 lines
drivers/tty/serial/8250/8250_airoha.c new file +9 -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;
+
+	/* Set baud rate calculation defaults (BRDIV ([BRDH,BRDL]) to 1) */

Inner parentheses are not needed.

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

Quoted 10 lines
drivers/tty/serial/8250/8250_airoha.c new file +9 -0
+	serial8250_do_set_divisor(port, baud, UART_BRD_20M);
+
+	/*
+	 * Calculate XYD_x and XINCLKDR register by searching
+	 * through a table of crystal_clock divisors.
+	 */
+	nom = (u64)baud * XYD_Y;
 
+	for (div_bit = ARRAY_SIZE(airoha_clk_divs) - 1;
+	     div_bit >= 0; div_bit--) {

It's okay to place these on a single line.

Quoted 28 lines
drivers/tty/serial/8250/8250_airoha.c new file +28 -0
+		unsigned int div = airoha_clk_divs[div_bit];
+
+		xindiv_clk = XINDIV_CLOCK / div;
+		xyd_x = div_u64(nom * 16, xindiv_clk);
+
+		/* 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;
+	}
+
+	/* 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;
+	}
+
+	serial_port_out(port, UART_AIROHA_XINCLKDR, BIT(div_bit));
+	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);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko