2023-10-16 03:38:53

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 0/6] tty: serial: 8250: Changes of MOXA PCIe boards in 8250_pci.c

These patch series do some changes to MOXA PCIe boards in 8250_pci.c,
including:

- Modify MOXA enum name within 8250_pci.c
- Cleanup MOXA configurations within 8250_pci.c
- Relocate macros within 8250_pci.c
- Add support for MOXA Mini PCIe boards
- Fix MOXA RS422/RS485 boards not function by default
- Add support for MOXA PCIe boards to switch between different serial
interfaces

Each patch depends on previous one.

---
Changes in v2:
- Split cleanup patch into first 2 patches of this patch series.
- Reposition macro list to the top of the code.
- Use PCI_VDEVICE() instead of PCI_DEVICE() in serial_pci_tbl[].
- Replace some C99 types with kernel types.
- Introduce functions for checking device ID pattern.
- Replace magic constant with BIT().
- Sort macros into pre-existing macro list.
- Rewrite set_interface condition architecture.
- Utilize unused flag to represent RS422 interface instead of adding a
new one.

v1: https://lore.kernel.org/all/[email protected]/

Crescent CY Hsieh (6):
tty: serial: 8250: Modify MOXA enum name within 8250_pci.c
tty: serial: 8250: Cleanup MOXA configurations within 8250_pci.c
tty: serial: 8250: Relocate macros within 8250_pci.c
tty: serial: 8250: Add support for MOXA Mini PCIe boards
tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by
default
tty: serial: 8250: Add support for MOXA PCIe boards to switch
interface between RS422/RS485

drivers/tty/serial/8250/8250_pci.c | 354 +++++++++++++++++++----------
1 file changed, 240 insertions(+), 114 deletions(-)

--
2.34.1


2023-10-16 03:39:14

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 1/6] tty: serial: 8250: Modify MOXA enum name within 8250_pci.c

To improve clarity, modify the MOXA enum name within pci_board_num_t.

Signed-off-by: Crescent CY Hsieh <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 36 +++++++++++++++---------------
1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 62a9bd30b4db..55a788b412bc 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -2854,9 +2854,9 @@ enum pci_board_num_t {
pbn_titan_2_4000000,
pbn_titan_4_4000000,
pbn_titan_8_4000000,
- pbn_moxa8250_2p,
- pbn_moxa8250_4p,
- pbn_moxa8250_8p,
+ pbn_moxa_2,
+ pbn_moxa_4,
+ pbn_moxa_8,
};

/*
@@ -3628,19 +3628,19 @@ static struct pciserial_board pci_boards[] = {
.uart_offset = 0x200,
.first_offset = 0x1000,
},
- [pbn_moxa8250_2p] = {
+ [pbn_moxa_2] = {
.flags = FL_BASE1,
.num_ports = 2,
.base_baud = 921600,
.uart_offset = 0x200,
},
- [pbn_moxa8250_4p] = {
+ [pbn_moxa_4] = {
.flags = FL_BASE1,
.num_ports = 4,
.base_baud = 921600,
.uart_offset = 0x200,
},
- [pbn_moxa8250_8p] = {
+ [pbn_moxa_8] = {
.flags = FL_BASE1,
.num_ports = 8,
.base_baud = 921600,
@@ -5351,40 +5351,40 @@ static const struct pci_device_id serial_pci_tbl[] = {
*/
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102E,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_2p },
+ pbn_moxa_2 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102EL,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_2p },
+ pbn_moxa_2 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_4p },
+ pbn_moxa_4 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114EL,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_4p },
+ pbn_moxa_4 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_B,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118E_A_I,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132EL,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_2p },
+ pbn_moxa_2 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134EL_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_4p },
+ pbn_moxa_4 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP138E_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },
{ PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL_A,
PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa8250_8p },
+ pbn_moxa_8 },

/*
* ADDI-DATA GmbH communication cards <[email protected]>
--
2.34.1

2023-10-16 03:39:39

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 2/6] tty: serial: 8250: Cleanup MOXA configurations within 8250_pci.c

To improve clarity, clean up the MOXA configurations within
serial_pci_tbl.

Signed-off-by: Crescent CY Hsieh <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 48 ++++++++----------------------
1 file changed, 12 insertions(+), 36 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 55a788b412bc..fd810a6ee38f 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -5349,42 +5349,18 @@ static const struct pci_device_id serial_pci_tbl[] = {
/*
* MOXA
*/
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102E,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_2 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102EL,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_2 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_4 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114EL,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_4 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_B,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118E_A_I,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132EL,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_2 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134EL_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_4 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP138E_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
- { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL_A,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0,
- pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102E), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114EL), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_A), pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_B), pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL_A), pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118E_A_I), pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP138E_A), pbn_moxa_8 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL_A), pbn_moxa_8 },

/*
* ADDI-DATA GmbH communication cards <[email protected]>
--
2.34.1

2023-10-16 03:39:46

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 3/6] tty: serial: 8250: Relocate macros within 8250_pci.c

Move PCI_DEVICE_ID macros to the top so that these macros can be used
throughout 8250_pci.c

Signed-off-by: Crescent CY Hsieh <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 144 ++++++++++++++---------------
1 file changed, 72 insertions(+), 72 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index fd810a6ee38f..dd48b910bb05 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -26,6 +26,78 @@
#include "8250.h"
#include "8250_pcilib.h"

+#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
+#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
+#define PCI_DEVICE_ID_OCTPRO 0x0001
+#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
+#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
+#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
+#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
+#define PCI_SUBDEVICE_ID_SIIG_DUAL_00 0x2500
+#define PCI_SUBDEVICE_ID_SIIG_DUAL_30 0x2530
+#define PCI_VENDOR_ID_ADVANTECH 0x13fe
+#define PCI_DEVICE_ID_INTEL_CE4100_UART 0x2e66
+#define PCI_DEVICE_ID_ADVANTECH_PCI1600 0x1600
+#define PCI_DEVICE_ID_ADVANTECH_PCI1600_1611 0x1611
+#define PCI_DEVICE_ID_ADVANTECH_PCI3620 0x3620
+#define PCI_DEVICE_ID_ADVANTECH_PCI3618 0x3618
+#define PCI_DEVICE_ID_ADVANTECH_PCIf618 0xf618
+#define PCI_DEVICE_ID_TITAN_200I 0x8028
+#define PCI_DEVICE_ID_TITAN_400I 0x8048
+#define PCI_DEVICE_ID_TITAN_800I 0x8088
+#define PCI_DEVICE_ID_TITAN_800EH 0xA007
+#define PCI_DEVICE_ID_TITAN_800EHB 0xA008
+#define PCI_DEVICE_ID_TITAN_400EH 0xA009
+#define PCI_DEVICE_ID_TITAN_100E 0xA010
+#define PCI_DEVICE_ID_TITAN_200E 0xA012
+#define PCI_DEVICE_ID_TITAN_400E 0xA013
+#define PCI_DEVICE_ID_TITAN_800E 0xA014
+#define PCI_DEVICE_ID_TITAN_200EI 0xA016
+#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
+#define PCI_DEVICE_ID_TITAN_200V3 0xA306
+#define PCI_DEVICE_ID_TITAN_400V3 0xA310
+#define PCI_DEVICE_ID_TITAN_410V3 0xA312
+#define PCI_DEVICE_ID_TITAN_800V3 0xA314
+#define PCI_DEVICE_ID_TITAN_800V3B 0xA315
+#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
+#define PCIE_DEVICE_ID_NEO_2_OX_IBM 0x00F6
+#define PCI_DEVICE_ID_PLX_CRONYX_OMEGA 0xc001
+#define PCI_DEVICE_ID_INTEL_PATSBURG_KT 0x1d3d
+#define PCI_VENDOR_ID_WCH 0x4348
+#define PCI_DEVICE_ID_WCH_CH352_2S 0x3253
+#define PCI_DEVICE_ID_WCH_CH353_4S 0x3453
+#define PCI_DEVICE_ID_WCH_CH353_2S1PF 0x5046
+#define PCI_DEVICE_ID_WCH_CH353_1S1P 0x5053
+#define PCI_DEVICE_ID_WCH_CH353_2S1P 0x7053
+#define PCI_DEVICE_ID_WCH_CH355_4S 0x7173
+#define PCI_VENDOR_ID_AGESTAR 0x5372
+#define PCI_DEVICE_ID_AGESTAR_9375 0x6872
+#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a
+#define PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800 0x818e
+
+#define PCIE_VENDOR_ID_WCH 0x1c00
+#define PCIE_DEVICE_ID_WCH_CH382_2S1P 0x3250
+#define PCIE_DEVICE_ID_WCH_CH384_4S 0x3470
+#define PCIE_DEVICE_ID_WCH_CH384_8S 0x3853
+#define PCIE_DEVICE_ID_WCH_CH382_2S 0x3253
+
+#define PCI_DEVICE_ID_MOXA_CP102E 0x1024
+#define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
+#define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
+#define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
+#define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
+#define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
+#define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
+#define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
+#define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
+#define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
+#define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
+#define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683
+
+/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
+#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
+#define PCI_SUBDEVICE_ID_UNKNOWN_0x1588 0x1588
+
/*
* init function returns:
* > 0 - number of ports
@@ -1903,78 +1975,6 @@ pci_moxa_setup(struct serial_private *priv,
return setup_port(priv, port, bar, offset, 0);
}

-#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
-#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
-#define PCI_DEVICE_ID_OCTPRO 0x0001
-#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
-#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
-#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
-#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
-#define PCI_SUBDEVICE_ID_SIIG_DUAL_00 0x2500
-#define PCI_SUBDEVICE_ID_SIIG_DUAL_30 0x2530
-#define PCI_VENDOR_ID_ADVANTECH 0x13fe
-#define PCI_DEVICE_ID_INTEL_CE4100_UART 0x2e66
-#define PCI_DEVICE_ID_ADVANTECH_PCI1600 0x1600
-#define PCI_DEVICE_ID_ADVANTECH_PCI1600_1611 0x1611
-#define PCI_DEVICE_ID_ADVANTECH_PCI3620 0x3620
-#define PCI_DEVICE_ID_ADVANTECH_PCI3618 0x3618
-#define PCI_DEVICE_ID_ADVANTECH_PCIf618 0xf618
-#define PCI_DEVICE_ID_TITAN_200I 0x8028
-#define PCI_DEVICE_ID_TITAN_400I 0x8048
-#define PCI_DEVICE_ID_TITAN_800I 0x8088
-#define PCI_DEVICE_ID_TITAN_800EH 0xA007
-#define PCI_DEVICE_ID_TITAN_800EHB 0xA008
-#define PCI_DEVICE_ID_TITAN_400EH 0xA009
-#define PCI_DEVICE_ID_TITAN_100E 0xA010
-#define PCI_DEVICE_ID_TITAN_200E 0xA012
-#define PCI_DEVICE_ID_TITAN_400E 0xA013
-#define PCI_DEVICE_ID_TITAN_800E 0xA014
-#define PCI_DEVICE_ID_TITAN_200EI 0xA016
-#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
-#define PCI_DEVICE_ID_TITAN_200V3 0xA306
-#define PCI_DEVICE_ID_TITAN_400V3 0xA310
-#define PCI_DEVICE_ID_TITAN_410V3 0xA312
-#define PCI_DEVICE_ID_TITAN_800V3 0xA314
-#define PCI_DEVICE_ID_TITAN_800V3B 0xA315
-#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
-#define PCIE_DEVICE_ID_NEO_2_OX_IBM 0x00F6
-#define PCI_DEVICE_ID_PLX_CRONYX_OMEGA 0xc001
-#define PCI_DEVICE_ID_INTEL_PATSBURG_KT 0x1d3d
-#define PCI_VENDOR_ID_WCH 0x4348
-#define PCI_DEVICE_ID_WCH_CH352_2S 0x3253
-#define PCI_DEVICE_ID_WCH_CH353_4S 0x3453
-#define PCI_DEVICE_ID_WCH_CH353_2S1PF 0x5046
-#define PCI_DEVICE_ID_WCH_CH353_1S1P 0x5053
-#define PCI_DEVICE_ID_WCH_CH353_2S1P 0x7053
-#define PCI_DEVICE_ID_WCH_CH355_4S 0x7173
-#define PCI_VENDOR_ID_AGESTAR 0x5372
-#define PCI_DEVICE_ID_AGESTAR_9375 0x6872
-#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a
-#define PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800 0x818e
-
-#define PCIE_VENDOR_ID_WCH 0x1c00
-#define PCIE_DEVICE_ID_WCH_CH382_2S1P 0x3250
-#define PCIE_DEVICE_ID_WCH_CH384_4S 0x3470
-#define PCIE_DEVICE_ID_WCH_CH384_8S 0x3853
-#define PCIE_DEVICE_ID_WCH_CH382_2S 0x3253
-
-#define PCI_DEVICE_ID_MOXA_CP102E 0x1024
-#define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
-#define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
-#define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
-#define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
-#define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
-#define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
-#define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
-#define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
-#define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
-#define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
-#define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683
-
-/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
-#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
-#define PCI_SUBDEVICE_ID_UNKNOWN_0x1588 0x1588
-
/*
* Master list of serial port init/setup/exit quirks.
* This does not describe the general nature of the port.
--
2.34.1

2023-10-16 03:39:57

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 4/6] tty: serial: 8250: Add support for MOXA Mini PCIe boards

Add support for MOXA Mini PCIe serial boards:

- CP102N: 2 ports | RS232
- CP104N: 4 ports | RS232
- CP112N: 2 ports | RS232/RS422/RS485
- CP114N: 4 ports | RS232/RS422/RS485
- CP132N: 2 ports | RS422/RS485
- CP134N: 4 ports | RS422/RS485

Signed-off-by: Crescent CY Hsieh <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 56 ++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index dd48b910bb05..d22f727491b5 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -83,14 +83,20 @@

#define PCI_DEVICE_ID_MOXA_CP102E 0x1024
#define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
+#define PCI_DEVICE_ID_MOXA_CP102N 0x1027
#define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
+#define PCI_DEVICE_ID_MOXA_CP104N 0x1046
+#define PCI_DEVICE_ID_MOXA_CP112N 0x1121
#define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
+#define PCI_DEVICE_ID_MOXA_CP114N 0x1145
#define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
#define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
#define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
#define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
#define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
+#define PCI_DEVICE_ID_MOXA_CP132N 0x1323
#define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
+#define PCI_DEVICE_ID_MOXA_CP134N 0x1343
#define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
#define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683

@@ -1959,6 +1965,49 @@ pci_sunix_setup(struct serial_private *priv,
return setup_port(priv, port, bar, offset, 0);
}

+#define MOXA_PUART_GPIO_EN 0x09
+#define MOXA_PUART_GPIO_OUT 0x0A
+
+#define MOXA_GPIO_PIN2 BIT(2)
+
+static bool pci_moxa_is_mini_pcie(unsigned short device)
+{
+ if (device == PCI_DEVICE_ID_MOXA_CP102N ||
+ device == PCI_DEVICE_ID_MOXA_CP104N ||
+ device == PCI_DEVICE_ID_MOXA_CP112N ||
+ device == PCI_DEVICE_ID_MOXA_CP114N ||
+ device == PCI_DEVICE_ID_MOXA_CP132N ||
+ device == PCI_DEVICE_ID_MOXA_CP134N)
+ return true;
+
+ return false;
+}
+
+static int pci_moxa_init(struct pci_dev *dev)
+{
+ unsigned short device = dev->device;
+ resource_size_t iobar_addr = pci_resource_start(dev, 2);
+ unsigned int num_ports = (device & 0x00F0) >> 4;
+ u8 val;
+
+ /*
+ * Enable hardware buffer to prevent break signal output when system boots up.
+ * This hardware buffer is only supported on Mini PCIe series.
+ */
+ if (pci_moxa_is_mini_pcie(device)) {
+ /* Set GPIO direction */
+ val = inb(iobar_addr + MOXA_PUART_GPIO_EN);
+ val |= MOXA_GPIO_PIN2;
+ outb(val, iobar_addr + MOXA_PUART_GPIO_EN);
+ /* Enable low GPIO */
+ val = inb(iobar_addr + MOXA_PUART_GPIO_OUT);
+ val &= ~MOXA_GPIO_PIN2;
+ outb(val, iobar_addr + MOXA_PUART_GPIO_OUT);
+ }
+
+ return num_ports;
+}
+
static int
pci_moxa_setup(struct serial_private *priv,
const struct pciserial_board *board,
@@ -2635,6 +2684,7 @@ static struct pci_serial_quirk pci_serial_quirks[] = {
.device = PCI_ANY_ID,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
+ .init = pci_moxa_init,
.setup = pci_moxa_setup,
},
{
@@ -5351,14 +5401,20 @@ static const struct pci_device_id serial_pci_tbl[] = {
*/
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102E), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104N), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP112N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114EL), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114N), pbn_moxa_4 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_B), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118E_A_I), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134N), pbn_moxa_4 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP138E_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL_A), pbn_moxa_8 },

--
2.34.1

2023-10-16 03:39:57

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 5/6] tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by default

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 <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 42 ++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index d22f727491b5..72dd27141801 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1970,6 +1970,12 @@ pci_sunix_setup(struct serial_private *priv,

#define MOXA_GPIO_PIN2 BIT(2)

+#define MOXA_RS232 0x00
+#define MOXA_RS422 0x01
+#define MOXA_RS485_4W 0x0B
+#define MOXA_RS485_2W 0x0F
+#define MOXA_UIR_OFFSET 0x04
+
static bool pci_moxa_is_mini_pcie(unsigned short device)
{
if (device == PCI_DEVICE_ID_MOXA_CP102N ||
@@ -1983,13 +1989,49 @@ static bool pci_moxa_is_mini_pcie(unsigned short device)
return false;
}

+static bool pci_moxa_match_x3xx(unsigned short device)
+{
+ return (device & 0x0F00) == 0x0300;
+}
+
+static int pci_moxa_set_interface(const struct pci_dev *dev,
+ unsigned int port_idx,
+ u8 mode)
+{
+ resource_size_t iobar_addr = pci_resource_start(dev, 2);
+ resource_size_t UIR_addr = iobar_addr + MOXA_UIR_OFFSET + port_idx / 2;
+ u8 val;
+
+ val = inb(UIR_addr);
+
+ if (port_idx % 2) {
+ val &= 0x0F;
+ val |= mode << 4;
+ } else {
+ val &= 0xF0;
+ val |= mode;
+ }
+ outb(val, UIR_addr);
+
+ return 0;
+}
+
static int pci_moxa_init(struct pci_dev *dev)
{
unsigned short device = dev->device;
resource_size_t iobar_addr = pci_resource_start(dev, 2);
+ unsigned int i;
unsigned int num_ports = (device & 0x00F0) >> 4;
u8 val;

+ /*
+ * 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 (pci_moxa_match_x3xx(device)) {
+ 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 boots up.
* This hardware buffer is only supported on Mini PCIe series.
--
2.34.1

2023-10-16 03:40:24

by Crescent CY Hsieh

[permalink] [raw]
Subject: [Patch v2 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485

MOXA PCIe boards have 4 serial interfaces and don't require additional
stuff to switch between interfaces:

- RS232
- RS422
- RS485_2W (half-duplex)
- RS485_4W (full-duplex)

By using ioctl command "TIOCRS485", it can switch between default
interface and RS485 if supported.

That means, for RS422/RS485 board, it can switch between RS422 and
RS485 by setting the flags within struct serial_rs485.

However, for the RS232/RS422/RS485 board, it can only switch between
RS232 and RS485, there's no flag for switching interface into RS422.

This patch uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
workaround solution.

Signed-off-by: Crescent CY Hsieh <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 52 ++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 72dd27141801..e2e8a28242bc 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1976,6 +1976,10 @@ pci_sunix_setup(struct serial_private *priv,
#define MOXA_RS485_2W 0x0F
#define MOXA_UIR_OFFSET 0x04

+static const struct serial_rs485 pci_moxa_rs485_supported = {
+ .flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
+};
+
static bool pci_moxa_is_mini_pcie(unsigned short device)
{
if (device == PCI_DEVICE_ID_MOXA_CP102N ||
@@ -1989,6 +1993,11 @@ static bool pci_moxa_is_mini_pcie(unsigned short device)
return false;
}

+static bool pci_moxa_match_x1xx(unsigned short device)
+{
+ return (device & 0x0F00) == 0x0100;
+}
+
static bool pci_moxa_match_x3xx(unsigned short device)
{
return (device & 0x0F00) == 0x0300;
@@ -2016,6 +2025,36 @@ static int pci_moxa_set_interface(const struct pci_dev *dev,
return 0;
}

+static int pci_moxa_rs485_config(struct uart_port *port,
+ struct ktermios *termios,
+ struct serial_rs485 *rs485)
+{
+ struct pci_dev *dev = to_pci_dev(port->dev);
+ unsigned short device = dev->device;
+ u8 mode = MOXA_RS232;
+
+ if (rs485->flags & SER_RS485_ENABLED) {
+ /* Use SER_RS485_TERMINATE_BUS to represent RS422 as a workaround. */
+ if (rs485->flags & SER_RS485_TERMINATE_BUS) {
+ mode = MOXA_RS422;
+ } else {
+ if (rs485->flags & SER_RS485_RX_DURING_TX)
+ mode = MOXA_RS485_4W;
+ else
+ mode = MOXA_RS485_2W;
+ }
+ } else {
+ /*
+ * RS232 is not supported for those device IDs of
+ * MOXA PCIe boards match the pattern 0x*3**.
+ */
+ if (pci_moxa_match_x3xx(device))
+ return -EINVAL;
+ }
+
+ return pci_moxa_set_interface(dev, port->port_id, mode);
+}
+
static int pci_moxa_init(struct pci_dev *dev)
{
unsigned short device = dev->device;
@@ -2055,9 +2094,22 @@ pci_moxa_setup(struct serial_private *priv,
const struct pciserial_board *board,
struct uart_8250_port *port, int idx)
{
+ struct pci_dev *dev = priv->dev;
+ unsigned short device = dev->device;
unsigned int bar = FL_GET_BASE(board->flags);
int offset;

+ /*
+ * For the device IDs of MOXA PCIe boards match the pattern 0x*1** and 0x*3**,
+ * these boards support switching interface between RS422/RS485 using TIOCSRS485.
+ */
+ if (pci_moxa_match_x1xx(device) || pci_moxa_match_x3xx(device)) {
+ port->port.rs485_config = pci_moxa_rs485_config;
+ port->port.rs485_supported = pci_moxa_rs485_supported;
+
+ if (pci_moxa_match_x3xx(device))
+ port->port.rs485.flags |= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS;
+ }
if (board->num_ports == 4 && idx == 3)
offset = 7 * board->uart_offset;
else
--
2.34.1

2023-10-16 05:11:29

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 1/6] tty: serial: 8250: Modify MOXA enum name within 8250_pci.c

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> To improve clarity, modify the MOXA enum name within pci_board_num_t.
>
> Signed-off-by: Crescent CY Hsieh <[email protected]>

Reviewed-by: Jiri Slaby <[email protected]>

--
js
suse labs

2023-10-16 05:13:41

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 2/6] tty: serial: 8250: Cleanup MOXA configurations within 8250_pci.c

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> To improve clarity, clean up the MOXA configurations within
> serial_pci_tbl.

Perhaps append "using PCI_VDEVICE".

> Signed-off-by: Crescent CY Hsieh <[email protected]>

Reviewed-by: Jiri Slaby <[email protected]>

--
js
suse labs

2023-10-16 05:15:05

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 3/6] tty: serial: 8250: Relocate macros within 8250_pci.c

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> Move PCI_DEVICE_ID macros to the top so that these macros can be used
> throughout 8250_pci.c
>
> Signed-off-by: Crescent CY Hsieh <[email protected]>

Reviewed-by: Jiri Slaby <[email protected]>

--
js
suse labs

2023-10-16 05:16:37

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 4/6] tty: serial: 8250: Add support for MOXA Mini PCIe boards

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> Add support for MOXA Mini PCIe serial boards:
>
> - CP102N: 2 ports | RS232
> - CP104N: 4 ports | RS232
> - CP112N: 2 ports | RS232/RS422/RS485
> - CP114N: 4 ports | RS232/RS422/RS485
> - CP132N: 2 ports | RS422/RS485
> - CP134N: 4 ports | RS422/RS485
>
> Signed-off-by: Crescent CY Hsieh <[email protected]>

Reviewed-by: Jiri Slaby <[email protected]>

--
js
suse labs

2023-10-16 05:18:30

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 5/6] tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by default

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> 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 <[email protected]>

Reviewed-by: Jiri Slaby <[email protected]>

--
js
suse labs

2023-10-16 05:28:04

by Jiri Slaby

[permalink] [raw]
Subject: Re: [Patch v2 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485

On 16. 10. 23, 5:37, Crescent CY Hsieh wrote:
> MOXA PCIe boards have 4 serial interfaces and don't require additional
> stuff to switch between interfaces:
>
> - RS232
> - RS422
> - RS485_2W (half-duplex)
> - RS485_4W (full-duplex)
>
> By using ioctl command "TIOCRS485", it can switch between default
> interface and RS485 if supported.
>
> That means, for RS422/RS485 board, it can switch between RS422 and
> RS485 by setting the flags within struct serial_rs485.
>
> However, for the RS232/RS422/RS485 board, it can only switch between
> RS232 and RS485, there's no flag for switching interface into RS422.
>
> This patch uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
> workaround solution.
>
> Signed-off-by: Crescent CY Hsieh <[email protected]>
> ---
> drivers/tty/serial/8250/8250_pci.c | 52 ++++++++++++++++++++++++++++++
> 1 file changed, 52 insertions(+)
>
> diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
> index 72dd27141801..e2e8a28242bc 100644
> --- a/drivers/tty/serial/8250/8250_pci.c
> +++ b/drivers/tty/serial/8250/8250_pci.c
> @@ -1976,6 +1976,10 @@ pci_sunix_setup(struct serial_private *priv,
> #define MOXA_RS485_2W 0x0F
> #define MOXA_UIR_OFFSET 0x04
>
> +static const struct serial_rs485 pci_moxa_rs485_supported = {
> + .flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
> +};
> +
> static bool pci_moxa_is_mini_pcie(unsigned short device)
> {
> if (device == PCI_DEVICE_ID_MOXA_CP102N ||
> @@ -1989,6 +1993,11 @@ static bool pci_moxa_is_mini_pcie(unsigned short device)
> return false;
> }
>
> +static bool pci_moxa_match_x1xx(unsigned short device)
> +{
> + return (device & 0x0F00) == 0x0100;
> +}
> +
> static bool pci_moxa_match_x3xx(unsigned short device)

I wonder if it made more sense to have only a single function like:

static bool pci_moxa_match(unsigned short device, unsigned short matching)
{
return (device & 0x0F00) == matching;
}

and call:
pci_moxa_match(device, 0x0100)
pci_moxa_match(device, 0x0300)
?

Perhaps name the function better. According to what the second digit in
the device ID in fact means.

> {
> return (device & 0x0F00) == 0x0300;
> @@ -2016,6 +2025,36 @@ static int pci_moxa_set_interface(const struct pci_dev *dev,
> return 0;
> }
>
> +static int pci_moxa_rs485_config(struct uart_port *port,
> + struct ktermios *termios,
> + struct serial_rs485 *rs485)
> +{
> + struct pci_dev *dev = to_pci_dev(port->dev);
> + unsigned short device = dev->device;
> + u8 mode = MOXA_RS232;
> +
> + if (rs485->flags & SER_RS485_ENABLED) {
> + /* Use SER_RS485_TERMINATE_BUS to represent RS422 as a workaround. */
> + if (rs485->flags & SER_RS485_TERMINATE_BUS) {
> + mode = MOXA_RS422;
> + } else {
> + if (rs485->flags & SER_RS485_RX_DURING_TX)
> + mode = MOXA_RS485_4W;
> + else
> + mode = MOXA_RS485_2W;
> + }
> + } else {
> + /*
> + * RS232 is not supported for those device IDs of
> + * MOXA PCIe boards match the pattern 0x*3**.

I am not native, but I can hardly parse this.

Maybe:
RS232 is not supported on MOXA PCIe boards with device IDs matching the
pattern 0x*3**.
?

> + */
> + if (pci_moxa_match_x3xx(device))
> + return -EINVAL;

Perhaps EOPNOTSUPP?

> + }
> +
> + return pci_moxa_set_interface(dev, port->port_id, mode);
> +}
> +
> static int pci_moxa_init(struct pci_dev *dev)
> {
> unsigned short device = dev->device;

thanks,
--
js
suse labs