[PATCH v1 21/31] serial: 8250_mxpcie: implement rx_trig_bytes callbacks and persist per-port level

Crescent Hsieh posted 31 patches 1 day, 11 hours ago
[PATCH v1 21/31] serial: 8250_mxpcie: implement rx_trig_bytes callbacks and persist per-port level
Posted by Crescent Hsieh 1 day, 11 hours ago
This patch implements device-specific RX trigger handling for the
rx_trig_bytes sysfs attribute by programming MOXA_PUART_RTL directly.

Changes:
- Add a per-port structure to persist both the registered line and the
  RX trigger level, allowing the level to be restored on startup.
- Implement uart_port callbacks:
    - set_rxtrig(port, bytes): program MOXA_PUART_RTL and cache the value
    - get_rxtrig(port):        read back MOXA_PUART_RTL
- Use the cached RX trigger level during startup instead of a fixed
  default, while keeping the initial default at 96 for backward
  compatibility.

With these callbacks in place, writes to rx_trig_bytes update the
hardware-specific register immediately, and the selected threshold is
preserved across open/close cycles.

No functional change for other 8250 drivers.

Signed-off-by: Crescent Hsieh <crescentcy.hsieh@moxa.com>
---
 drivers/tty/serial/8250/8250_mxpcie.c | 49 +++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_mxpcie.c b/drivers/tty/serial/8250/8250_mxpcie.c
index 9ba171274221..04b9c9ff5cbf 100644
--- a/drivers/tty/serial/8250/8250_mxpcie.c
+++ b/drivers/tty/serial/8250/8250_mxpcie.c
@@ -93,11 +93,16 @@
 #define MOXA_EVEN_RS_MASK	GENMASK(3, 0)
 #define MOXA_ODD_RS_MASK	GENMASK(7, 4)
 
+struct mxpcie8250_port {
+	int line;
+	u8 rx_trig_level;
+};
+
 struct mxpcie8250 {
 	struct pci_dev *pdev;
 	unsigned int supp_rs;
 	unsigned int num_ports;
-	int line[];
+	struct mxpcie8250_port port[];
 };
 
 enum {
@@ -243,6 +248,8 @@ static void mxpcie8250_set_termios(struct uart_port *port,
 
 static int mxpcie8250_startup(struct uart_port *port)
 {
+	struct pci_dev *pdev = to_pci_dev(port->dev);
+	struct mxpcie8250 *priv = pci_get_drvdata(pdev);
 	struct uart_8250_port *up = up_to_u8250p(port);
 	int i, ret;
 
@@ -255,7 +262,7 @@ static int mxpcie8250_startup(struct uart_port *port)
 	serial_out(up, MOXA_PUART_SFR, MOXA_PUART_SFR_950);
 
 	serial_out(up, MOXA_PUART_TTL, 0);
-	serial_out(up, MOXA_PUART_RTL, 96);
+	serial_out(up, MOXA_PUART_RTL, priv->port[port->port_id].rx_trig_level);
 	serial_out(up, MOXA_PUART_FCL, 16);
 	serial_out(up, MOXA_PUART_FCH, 110);
 
@@ -441,6 +448,31 @@ static void mxpcie8250_break_ctl(struct uart_port *port, int break_state)
 		serial8250_do_break_ctl(port, break_state);
 }
 
+static int mxpcie8250_set_rxtrig(struct uart_port *port, unsigned char bytes)
+{
+	struct uart_8250_port *up = up_to_u8250p(port);
+	struct pci_dev *pdev = to_pci_dev(port->dev);
+	struct mxpcie8250 *priv = pci_get_drvdata(pdev);
+
+	if (bytes > 128)
+		return -EINVAL;
+
+	serial_out(up, MOXA_PUART_RTL, bytes);
+	priv->port[port->port_id].rx_trig_level = bytes;
+
+	return 0;
+}
+
+static int mxpcie8250_get_rxtrig(struct uart_port *port)
+{
+	struct uart_8250_port *up = up_to_u8250p(port);
+	int rx_trig_byte;
+
+	rx_trig_byte = serial_in(up, MOXA_PUART_RTL);
+
+	return rx_trig_byte;
+}
+
 static int mxpcie8250_init(struct pci_dev *pdev)
 {
 	resource_size_t iobar_addr = pci_resource_start(pdev, 2);
@@ -513,7 +545,7 @@ static int mxpcie8250_probe(struct pci_dev *pdev, const struct pci_device_id *id
 
 	num_ports = mxpcie8250_get_nports(pdev->device);
 
-	priv = devm_kzalloc(&pdev->dev, struct_size(priv, line, num_ports), GFP_KERNEL);
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, port, num_ports), GFP_KERNEL);
 
 	if (!priv)
 		return -ENOMEM;
@@ -539,6 +571,8 @@ static int mxpcie8250_probe(struct pci_dev *pdev, const struct pci_device_id *id
 	up.port.unthrottle = mxpcie8250_unthrottle;
 	up.port.handle_irq = mxpcie8250_handle_irq;
 	up.port.break_ctl = mxpcie8250_break_ctl;
+	up.port.set_rxtrig = mxpcie8250_set_rxtrig;
+	up.port.get_rxtrig = mxpcie8250_get_rxtrig;
 
 	for (i = 0; i < num_ports; i++) {
 		if (mxpcie8250_setup(pdev, priv, &up, i))
@@ -547,15 +581,16 @@ static int mxpcie8250_probe(struct pci_dev *pdev, const struct pci_device_id *id
 		dev_dbg(&pdev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
 			up.port.iobase, up.port.irq, up.port.iotype);
 
-		priv->line[i] = serial8250_register_8250_port(&up);
+		priv->port[i].line = serial8250_register_8250_port(&up);
 
-		if (priv->line[i] < 0) {
+		if (priv->port[i].line < 0) {
 			dev_err(&pdev->dev,
 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
 				up.port.iobase, up.port.irq,
-				up.port.iotype, priv->line[i]);
+				up.port.iotype, priv->port[i].line);
 			break;
 		}
+		priv->port[i].rx_trig_level = 96;
 	}
 	pci_set_drvdata(pdev, priv);
 
@@ -568,7 +603,7 @@ static void mxpcie8250_remove(struct pci_dev *pdev)
 	unsigned int i;
 
 	for (i = 0; i < priv->num_ports; i++)
-		serial8250_unregister_port(priv->line[i]);
+		serial8250_unregister_port(priv->port[i].line);
 }
 
 static const struct pci_device_id mxpcie8250_pci_ids[] = {
-- 
2.45.2
Re: [PATCH v1 21/31] serial: 8250_mxpcie: implement rx_trig_bytes callbacks and persist per-port level
Posted by Andy Shevchenko 20 hours ago
On Sun, Nov 30, 2025 at 12:45 PM Crescent Hsieh
<crescentcy.hsieh@moxa.com> wrote:
>
> This patch implements device-specific RX trigger handling for the
> rx_trig_bytes sysfs attribute by programming MOXA_PUART_RTL directly.
>
> Changes:
> - Add a per-port structure to persist both the registered line and the
>   RX trigger level, allowing the level to be restored on startup.
> - Implement uart_port callbacks:
>     - set_rxtrig(port, bytes): program MOXA_PUART_RTL and cache the value
>     - get_rxtrig(port):        read back MOXA_PUART_RTL
> - Use the cached RX trigger level during startup instead of a fixed
>   default, while keeping the initial default at 96 for backward
>   compatibility.
>
> With these callbacks in place, writes to rx_trig_bytes update the
> hardware-specific register immediately, and the selected threshold is
> preserved across open/close cycles.
>
> No functional change for other 8250 drivers.

...

>  static int mxpcie8250_startup(struct uart_port *port)
>  {
> +       struct pci_dev *pdev = to_pci_dev(port->dev);
> +       struct mxpcie8250 *priv = pci_get_drvdata(pdev);

It's a bit of a detour to simply use dev_get_drvdata() in an agnostic
way. With the above the code will need a change once the device uses
another bus.

>         struct uart_8250_port *up = up_to_u8250p(port);
>         int i, ret;

>  }

...

> +static int mxpcie8250_set_rxtrig(struct uart_port *port, unsigned char bytes)
> +{
> +       struct uart_8250_port *up = up_to_u8250p(port);

> +       struct pci_dev *pdev = to_pci_dev(port->dev);
> +       struct mxpcie8250 *priv = pci_get_drvdata(pdev);

Ditto. Aslo locate the lines consistently with the other function
(usually in reversed xmas tree order).

> +       if (bytes > 128)
> +               return -EINVAL;
> +
> +       serial_out(up, MOXA_PUART_RTL, bytes);
> +       priv->port[port->port_id].rx_trig_level = bytes;
> +
> +       return 0;
> +}

...

> +static int mxpcie8250_get_rxtrig(struct uart_port *port)
> +{
> +       struct uart_8250_port *up = up_to_u8250p(port);

> +       int rx_trig_byte;

Seems a useless variable. Is there something in the next changes that
justifies its presence?

> +       rx_trig_byte = serial_in(up, MOXA_PUART_RTL);
> +
> +       return rx_trig_byte;
> +}

...

Also it seems that this change can be split to two:
- preparatory that moves just a line member to a new data structure
- this one which adds required features

-- 
With Best Regards,
Andy Shevchenko