[RFC PATCH 7/7] serial: 8250_dwlib: Support for 9th bit multipoint addressing

From: Ilpo Järvinen
Date: Wed Mar 02 2022 - 04:58:04 EST


This change adds 9th bit multipoint addressing mode for DW UART using
the new ioctls introduced in the previous change. 9th bit addressing
can be used only when HW RS485 is available.

Updating RAR (receive address register) is bit tricky because
busy indication is not be available when DW UART is strictly
16550 compatible, which is the case with the hardware I was
testing with. RAR should not be updated while receive is in
progress which is now achieved by deasserting RE and waiting
for one frame (in case rx would be in progress, the driver
seems to have no way of knowing it w/o busy indication).

Co-developed-by: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
Signed-off-by: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
Co-developed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Co-developed-by: Raymond Tan <raymond.tan@xxxxxxxxx>
Signed-off-by: Raymond Tan <raymond.tan@xxxxxxxxx>
Co-developed-by: Lakshmi Sowjanya <lakshmi.sowjanya.d@xxxxxxxxx>
Signed-off-by: Lakshmi Sowjanya <lakshmi.sowjanya.d@xxxxxxxxx>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@xxxxxxxxxxxxxxx>
---
drivers/tty/serial/8250/8250_dwlib.c | 124 +++++++++++++++++++++++++++
drivers/tty/serial/8250/8250_dwlib.h | 3 +
2 files changed, 127 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_dwlib.c b/drivers/tty/serial/8250/8250_dwlib.c
index 51b0f55ee9d0..57bf6ca0309b 100644
--- a/drivers/tty/serial/8250/8250_dwlib.c
+++ b/drivers/tty/serial/8250/8250_dwlib.c
@@ -3,9 +3,11 @@

#include <linux/bitops.h>
#include <linux/bitfield.h>
+#include <linux/delay.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/kernel.h>
+#include <linux/math.h>
#include <linux/property.h>
#include <linux/serial_8250.h>
#include <linux/serial_core.h>
@@ -17,6 +19,9 @@
#define DW_UART_DE_EN 0xb0 /* Driver Output Enable Register */
#define DW_UART_RE_EN 0xb4 /* Receiver Output Enable Register */
#define DW_UART_DLF 0xc0 /* Divisor Latch Fraction Register */
+#define DW_UART_RAR 0xc4 /* Receive Address Register */
+#define DW_UART_TAR 0xc8 /* Transmit Address Register */
+#define DW_UART_LCR_EXT 0xcc /* Line Extended Control Register */
#define DW_UART_CPR 0xf4 /* Component Parameter Register */
#define DW_UART_UCV 0xf8 /* UART Component Version */

@@ -29,6 +34,12 @@
#define DW_UART_TCR_XFER_MODE_SW_DE_OR_RE FIELD_PREP(DW_UART_TCR_XFER_MODE, 1)
#define DW_UART_TCR_XFER_MODE_DE_OR_RE FIELD_PREP(DW_UART_TCR_XFER_MODE, 2)

+/* Line Extended Control Register bits */
+#define DW_UART_LCR_EXT_DLS_E BIT(0)
+#define DW_UART_LCR_EXT_ADDR_MATCH BIT(1)
+#define DW_UART_LCR_EXT_SEND_ADDR BIT(2)
+#define DW_UART_LCR_EXT_TRANSMIT_MODE BIT(3)
+
/* Component Parameter Register bits */
#define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
#define DW_UART_CPR_AFCE_MODE (1 << 4)
@@ -91,16 +102,126 @@ static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
serial8250_do_set_divisor(p, baud, quot, quot_frac);
}

+/* Wait until re is de-asserted for sure. Without BUSY indication available,
+ * only available course of action is to wait until current frame is received.
+ */
+static void dw8250_wait_re_deassert(struct uart_port *p)
+{
+ struct dw8250_port_data *d = p->private_data;
+
+ if (d->rar_timeout_us)
+ udelay(d->rar_timeout_us);
+}
+
+static void dw8250_update_rar(struct uart_port *p, u32 addr)
+{
+ u32 re_en = dw8250_readl_ext(p, DW_UART_RE_EN);
+
+ /* RAR shouldn't be changed while receiving. Thus, de-assert RE_EN
+ * if asserted and wait.
+ */
+ if (re_en)
+ dw8250_writel_ext(p, DW_UART_RE_EN, 0);
+ dw8250_wait_re_deassert(p);
+ dw8250_writel_ext(p, DW_UART_RAR, addr);
+ if (re_en)
+ dw8250_writel_ext(p, DW_UART_RE_EN, re_en);
+}
+
+static void dw8250_addrmode_setup(struct uart_port *p, bool enable_addrmode)
+{
+ struct dw8250_port_data *d = p->private_data;
+
+ if (enable_addrmode) {
+ /* Clear RAR & TAR of any previous values */
+ dw8250_writel_ext(p, DW_UART_RAR, 0);
+ dw8250_writel_ext(p, DW_UART_TAR, 0);
+ dw8250_writel_ext(p, DW_UART_LCR_EXT, DW_UART_LCR_EXT_DLS_E);
+ } else {
+ dw8250_writel_ext(p, DW_UART_LCR_EXT, 0);
+ }
+
+ d->addrmode = enable_addrmode;
+}
+
void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, struct ktermios *old)
{
+ struct dw8250_port_data *d = p->private_data;
+
p->status &= ~UPSTAT_AUTOCTS;
if (termios->c_cflag & CRTSCTS)
p->status |= UPSTAT_AUTOCTS;

+ if (!(p->rs485.flags & SER_RS485_ENABLED) || !d->hw_rs485_support)
+ termios->c_cflag &= ~ADDRB;
+
+ if (!old || (termios->c_cflag ^ old->c_cflag) & ADDRB)
+ dw8250_addrmode_setup(p, termios->c_cflag & ADDRB);
+
serial8250_do_set_termios(p, termios, old);
+
+ d->rar_timeout_us = termios->c_ispeed ?
+ DIV_ROUND_UP(tty_get_frame_size(termios->c_cflag) * USEC_PER_SEC,
+ termios->c_ispeed) : 0;
}
EXPORT_SYMBOL_GPL(dw8250_do_set_termios);

+static int dw8250_rs485_set_addr(struct uart_port *p, struct serial_addr *addr)
+{
+ struct dw8250_port_data *d = p->private_data;
+ u32 lcr;
+
+ if (!(p->rs485.flags & SER_RS485_ENABLED) || !d->addrmode)
+ return -EINVAL;
+
+ addr->flags &= SER_ADDR_RECV | SER_ADDR_RECV_CLEAR | SER_ADDR_DEST;
+ if (!addr->flags)
+ return -EINVAL;
+
+ lcr = dw8250_readl_ext(p, DW_UART_LCR_EXT);
+ if (addr->flags & SER_ADDR_RECV) {
+ dw8250_update_rar(p, addr->addr & 0xff);
+ lcr |= DW_UART_LCR_EXT_ADDR_MATCH;
+ addr->flags &= ~SER_ADDR_RECV_CLEAR;
+ } else if (addr->flags & SER_ADDR_RECV_CLEAR) {
+ lcr &= DW_UART_LCR_EXT_ADDR_MATCH;
+ }
+ if (addr->flags & SER_ADDR_DEST) {
+ dw8250_writel_ext(p, DW_UART_TAR, addr->addr & 0xff);
+ lcr |= DW_UART_LCR_EXT_SEND_ADDR;
+ }
+ dw8250_writel_ext(p, DW_UART_LCR_EXT, lcr);
+
+ return 0;
+}
+
+static int dw8250_rs485_get_addr(struct uart_port *p, struct serial_addr *addr)
+{
+ struct dw8250_port_data *d = p->private_data;
+
+ if (!(p->rs485.flags & SER_RS485_ENABLED) || !d->addrmode)
+ return -EINVAL;
+
+ if (addr->flags == SER_ADDR_DEST) {
+ addr->addr = dw8250_readl_ext(p, DW_UART_TAR) & 0xff;
+ return 0;
+ }
+ if (addr->flags == SER_ADDR_RECV) {
+ u32 lcr = dw8250_readl_ext(p, DW_UART_LCR_EXT);
+
+ if (!(lcr & DW_UART_LCR_EXT_ADDR_MATCH)) {
+ addr->flags = SER_ADDR_RECV_CLEAR;
+ addr->addr = 0;
+ } else {
+ addr->addr = dw8250_readl_ext(p, DW_UART_RAR) & 0xff;
+ }
+
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
static void dw8250_rs485_start_tx(struct uart_8250_port *up)
{
struct uart_port *p = &(up->port);
@@ -157,6 +278,7 @@ static int dw8250_rs485_config(struct uart_port *p, struct serial_rs485 *rs485)
tcr &= ~DW_UART_TCR_RS485_EN;
dw8250_writel_ext(p, DW_UART_DE_EN, 0);
dw8250_writel_ext(p, DW_UART_RE_EN, 0);
+ dw8250_addrmode_setup(p, false);
}

/* Resetting the default DE_POL & RE_POL */
@@ -203,6 +325,8 @@ void dw8250_setup_port(struct uart_port *p)
p->rs485_config = dw8250_rs485_config;
up->rs485_start_tx = dw8250_rs485_start_tx;
up->rs485_stop_tx = dw8250_rs485_stop_tx;
+ p->set_addr = dw8250_rs485_set_addr;
+ p->get_addr = dw8250_rs485_get_addr;
} else {
p->rs485_config = serial8250_em485_config;
up->rs485_start_tx = serial8250_em485_start_tx;
diff --git a/drivers/tty/serial/8250/8250_dwlib.h b/drivers/tty/serial/8250/8250_dwlib.h
index a8fa020ca544..bfb1d8af7f0c 100644
--- a/drivers/tty/serial/8250/8250_dwlib.h
+++ b/drivers/tty/serial/8250/8250_dwlib.h
@@ -17,6 +17,9 @@ struct dw8250_port_data {

/* RS485 variables */
bool hw_rs485_support;
+ /* 9-bit framing (9th bit is address indicator) */
+ bool addrmode;
+ unsigned int rar_timeout_us;
};

void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, struct ktermios *old);
--
2.30.2