diff mbox series

[3/4] tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by default

Message ID 20231002015702.30509-4-crescentcy.hsieh@moxa.com
State New
Headers show
Series [1/4] tty: serial: 8250: Cleanup MOXA configurations in 8250_pci.c | expand

Commit Message

Crescent CY Hsieh Oct. 2, 2023, 1:57 a.m. UTC
MOXA PCIe RS422/RS485 boards will not function by default because of the
initial default serial interface of all MOXA PCIe boards is set to RS232.

This patch fixes the problem above by setting the initial default serial
interface to RS422 for those MOXA RS422/RS485 PCIe boards.

Signed-off-by: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>
---
 drivers/tty/serial/8250/8250_pci.c | 39 +++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

Comments

Crescent CY Hsieh Oct. 3, 2023, 8:22 a.m. UTC | #1
On Mon, Oct 02, 2023 at 09:14:32AM +0200, Jiri Slaby wrote:
> On 02. 10. 23, 3:57, Crescent CY Hsieh wrote:
> >   static int pci_moxa_init(struct pci_dev *dev)
> >   {
> >   	unsigned short device = dev->device;
> >   	unsigned long iobar_addr = pci_resource_start(dev, 2);
> > +	int i;
> 
> unsigned
> 
> >   	int num_ports = (device & 0x00F0) >> 4;
> 
> And this one too (in the previous patch).

Sorry, I am not sure I get your meaning, could you explain to me?

---
Sincerely,
Crescent CY Hsieh
diff mbox series

Patch

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index a70546ac361e..31bfe5381000 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1890,17 +1890,54 @@  pci_sunix_setup(struct serial_private *priv,
 #define MOXA_PUART_GPIO_EN	0x09
 #define MOXA_PUART_GPIO_OUT	0x0A
 
+#define MOXA_RS232	0x00
+#define MOXA_RS422	0x01
+#define MOXA_RS485_4W	0x0B
+#define MOXA_RS485_2W	0x0F
+#define MOXA_UIR_OFFSET	0x04
+
 #define MOXA_GPIO_SET_ALL_OUTPUT	0x0F
 
+static int pci_moxa_set_interface(struct pci_dev *dev,
+				  unsigned int port_idx,
+				  unsigned char mode)
+{
+	unsigned long iobar_addr = pci_resource_start(dev, 2);
+	unsigned long UIR_addr = iobar_addr + MOXA_UIR_OFFSET + (port_idx / 2);
+	unsigned char val, intf;
+
+	val = inb(UIR_addr);
+
+	if (port_idx % 2) {
+		intf = mode << 4;
+		val &= 0x0F;
+	} else {
+		intf = mode;
+		val &= 0xF0;
+	}
+	val |= intf;
+	outb(val, UIR_addr);
+
+	return 0;
+}
+
 static int pci_moxa_init(struct pci_dev *dev)
 {
 	unsigned short device = dev->device;
 	unsigned long iobar_addr = pci_resource_start(dev, 2);
+	int i;
 	int num_ports = (device & 0x00F0) >> 4;
 	unsigned char val;
 
 	outb(MOXA_GPIO_SET_ALL_OUTPUT, iobar_addr + MOXA_PUART_GPIO_EN);
-
+	/*
+	 * For the device IDs of MOXA PCIe boards match the pattern 0x*3**,
+	 * the initial default serial interface mode should be set to RS422.
+	 */
+	if ((device & 0x0F00) == 0x0300) {
+		for (i = 0; i < num_ports; ++i)
+			pci_moxa_set_interface(dev, i, MOXA_RS422);
+	}
 	/*
 	 * Enable hardware buffer to prevent break signal output when system boot up.
 	 * This hardware buffer is only supported on Mini PCIe series.