xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision c5f597479dfe8d6770d8a823f1f5626ad86c1367)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d0aeaa83SSudip Mukherjee /*
3d0aeaa83SSudip Mukherjee  *  Probe module for 8250/16550-type Exar chips PCI serial ports.
4d0aeaa83SSudip Mukherjee  *
5d0aeaa83SSudip Mukherjee  *  Based on drivers/tty/serial/8250/8250_pci.c,
6d0aeaa83SSudip Mukherjee  *
7d0aeaa83SSudip Mukherjee  *  Copyright (C) 2017 Sudip Mukherjee, All Rights Reserved.
8d0aeaa83SSudip Mukherjee  */
966c736daSAndy Shevchenko #include <linux/bits.h>
1066c736daSAndy Shevchenko #include <linux/delay.h>
1166c736daSAndy Shevchenko #include <linux/device.h>
12413058dfSJan Kiszka #include <linux/dmi.h>
1366c736daSAndy Shevchenko #include <linux/interrupt.h>
14d0aeaa83SSudip Mukherjee #include <linux/io.h>
1566c736daSAndy Shevchenko #include <linux/math.h>
16d0aeaa83SSudip Mukherjee #include <linux/module.h>
17d0aeaa83SSudip Mukherjee #include <linux/pci.h>
1873f76db8SAndy Shevchenko #include <linux/platform_device.h>
1982f9cefaSAndy Shevchenko #include <linux/pm.h>
20380b1e2fSJan Kiszka #include <linux/property.h>
2166c736daSAndy Shevchenko #include <linux/string.h>
2266c736daSAndy Shevchenko #include <linux/types.h>
23f7ce0706SParker Newman #include <linux/bitfield.h>
2466c736daSAndy Shevchenko 
2566c736daSAndy Shevchenko #include <linux/serial_8250.h>
26d0aeaa83SSudip Mukherjee #include <linux/serial_core.h>
27d0aeaa83SSudip Mukherjee #include <linux/serial_reg.h>
28d0aeaa83SSudip Mukherjee 
29d0aeaa83SSudip Mukherjee #include <asm/byteorder.h>
30d0aeaa83SSudip Mukherjee 
31d0aeaa83SSudip Mukherjee #include "8250.h"
32d813d900SAndy Shevchenko #include "8250_pcilib.h"
33d0aeaa83SSudip Mukherjee 
3424637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2S		0x1052
3524637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4S		0x105d
3624637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8S		0x106c
3724637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM232_8		0x10a8
3824637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2SM		0x10d2
3924637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4SM		0x10db
4024637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8SM		0x10ea
4110c5ccc3SJay Dolan 
42fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4224PCI335	0x0002
43fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4222PCI335	0x0004
44fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2324PCI335	0x000a
45fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2328PCI335	0x000b
46d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4224PCIE		0x0020
47d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4228PCIE		0x0021
48d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4222PCIE		0x0022
4995d69886SAndrew Davis 
50b86ae40fSParker Newman #define PCI_VENDOR_ID_CONNECT_TECH				0x12c4
51b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_SP_OPTO        0x0340
52b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_A      0x0341
53b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_B      0x0342
54b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS           0x0350
55b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_A         0x0351
56b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_B         0x0352
57b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS           0x0353
58b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_A        0x0354
59b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_B        0x0355
60b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS_OPTO      0x0360
61b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_A    0x0361
62b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_B    0x0362
63b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP             0x0370
64b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232         0x0371
65b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485         0x0372
66b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP           0x0373
67b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP           0x0374
68b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP           0x0375
69b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232_NS      0x0376
70b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT   0x0380
71b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT  0x0381
72b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO        0x0382
73b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO    0x0392
74b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP        0x03A0
75b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232    0x03A1
76b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485    0x03A2
77b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232_NS 0x03A3
78b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XEG001               0x0602
79b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_BASE           0x1000
80b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_2              0x1002
81b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_4              0x1004
82b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_8              0x1008
83b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_12             0x100C
84b86ae40fSParker Newman #define PCI_SUBDEVICE_ID_CONNECT_TECH_PCIE_XR35X_16             0x1010
85b86ae40fSParker Newman #define PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG00X          0x110c
86b86ae40fSParker Newman #define PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG01X          0x110d
87b86ae40fSParker Newman #define PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_16                 0x1110
88b86ae40fSParker Newman 
89d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V4358		0x4358
90d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V8358		0x8358
91b86ae40fSParker Newman #define PCI_DEVICE_ID_EXAR_XR17V252		0x0252
92b86ae40fSParker Newman #define PCI_DEVICE_ID_EXAR_XR17V254		0x0254
93b86ae40fSParker Newman #define PCI_DEVICE_ID_EXAR_XR17V258		0x0258
94d0aeaa83SSudip Mukherjee 
9595d69886SAndrew Davis #define PCI_SUBDEVICE_ID_USR_2980		0x0128
9695d69886SAndrew Davis #define PCI_SUBDEVICE_ID_USR_2981		0x0129
9795d69886SAndrew Davis 
98c7e1b405SAaron Sierra #define UART_EXAR_INT0		0x80
997e12357eSJan Kiszka #define UART_EXAR_8XMODE	0x88	/* 8X sampling rate select */
100ef4e281eSAndy Shevchenko #define UART_EXAR_SLEEP		0x8b	/* Sleep mode */
1016be254c2SAndy Shevchenko #define UART_EXAR_DVID		0x8d	/* Device identification */
1027e12357eSJan Kiszka 
1037e12357eSJan Kiszka #define UART_EXAR_FCTR		0x08	/* Feature Control Register */
1047e12357eSJan Kiszka #define UART_FCTR_EXAR_IRDA	0x10	/* IrDa data encode select */
1057e12357eSJan Kiszka #define UART_FCTR_EXAR_485	0x20	/* Auto 485 half duplex dir ctl */
1067e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGA	0x00	/* FIFO trigger table A */
1077e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGB	0x60	/* FIFO trigger table B */
1087e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGC	0x80	/* FIFO trigger table C */
1097e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGD	0xc0	/* FIFO trigger table D programmable */
1107e12357eSJan Kiszka 
1117e12357eSJan Kiszka #define UART_EXAR_TXTRG		0x0a	/* Tx FIFO trigger level write-only */
1127e12357eSJan Kiszka #define UART_EXAR_RXTRG		0x0b	/* Rx FIFO trigger level write-only */
1137e12357eSJan Kiszka 
114d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_7_0	0x8f	/* MPIOINT[7:0] */
115d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_7_0	0x90	/* MPIOLVL[7:0] */
116d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_7_0	0x91	/* MPIO3T[7:0] */
117d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_7_0	0x92	/* MPIOINV[7:0] */
118d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_7_0	0x93	/* MPIOSEL[7:0] */
119d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_7_0	0x94	/* MPIOOD[7:0] */
120d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_15_8	0x95	/* MPIOINT[15:8] */
121d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_15_8	0x96	/* MPIOLVL[15:8] */
122d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_15_8	0x97	/* MPIO3T[15:8] */
123d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_15_8	0x98	/* MPIOINV[15:8] */
124d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_15_8	0x99	/* MPIOSEL[15:8] */
125d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_15_8	0x9a	/* MPIOOD[15:8] */
126d0aeaa83SSudip Mukherjee 
127413058dfSJan Kiszka #define UART_EXAR_RS485_DLY(x)	((x) << 4)
128413058dfSJan Kiszka 
129687911b3SMatthew Howell #define UART_EXAR_DLD			0x02 /* Divisor Fractional */
130687911b3SMatthew Howell #define UART_EXAR_DLD_485_POLARITY	0x80 /* RS-485 Enable Signal Polarity */
131687911b3SMatthew Howell 
132f7ce0706SParker Newman /* EEPROM registers */
133f7ce0706SParker Newman #define UART_EXAR_REGB			0x8e
134f7ce0706SParker Newman #define UART_EXAR_REGB_EECK		BIT(4)
135f7ce0706SParker Newman #define UART_EXAR_REGB_EECS		BIT(5)
136f7ce0706SParker Newman #define UART_EXAR_REGB_EEDI		BIT(6)
137f7ce0706SParker Newman #define UART_EXAR_REGB_EEDO		BIT(7)
138f7ce0706SParker Newman #define UART_EXAR_REGB_EE_ADDR_SIZE	6
139f7ce0706SParker Newman #define UART_EXAR_REGB_EE_DATA_SIZE	16
140f7ce0706SParker Newman 
141f7ce0706SParker Newman #define UART_EXAR_XR17C15X_PORT_OFFSET	0x200
142f7ce0706SParker Newman #define UART_EXAR_XR17V25X_PORT_OFFSET	0x200
143f7ce0706SParker Newman #define UART_EXAR_XR17V35X_PORT_OFFSET	0x400
144f7ce0706SParker Newman 
145413058dfSJan Kiszka /*
146413058dfSJan Kiszka  * IOT2040 MPIO wiring semantics:
147413058dfSJan Kiszka  *
148413058dfSJan Kiszka  * MPIO		Port	Function
149413058dfSJan Kiszka  * ----		----	--------
150413058dfSJan Kiszka  * 0		2	Mode bit 0
151413058dfSJan Kiszka  * 1		2	Mode bit 1
152413058dfSJan Kiszka  * 2		2	Terminate bus
153413058dfSJan Kiszka  * 3		-	<reserved>
154413058dfSJan Kiszka  * 4		3	Mode bit 0
155413058dfSJan Kiszka  * 5		3	Mode bit 1
156413058dfSJan Kiszka  * 6		3	Terminate bus
157413058dfSJan Kiszka  * 7		-	<reserved>
158413058dfSJan Kiszka  * 8		2	Enable
159413058dfSJan Kiszka  * 9		3	Enable
160413058dfSJan Kiszka  * 10		-	Red LED
161413058dfSJan Kiszka  * 11..15	-	<unused>
162413058dfSJan Kiszka  */
163413058dfSJan Kiszka 
164413058dfSJan Kiszka /* IOT2040 MPIOs 0..7 */
165413058dfSJan Kiszka #define IOT2040_UART_MODE_RS232		0x01
166413058dfSJan Kiszka #define IOT2040_UART_MODE_RS485		0x02
167413058dfSJan Kiszka #define IOT2040_UART_MODE_RS422		0x03
168413058dfSJan Kiszka #define IOT2040_UART_TERMINATE_BUS	0x04
169413058dfSJan Kiszka 
170413058dfSJan Kiszka #define IOT2040_UART1_MASK		0x0f
171413058dfSJan Kiszka #define IOT2040_UART2_SHIFT		4
172413058dfSJan Kiszka 
173413058dfSJan Kiszka #define IOT2040_UARTS_DEFAULT_MODE	0x11	/* both RS232 */
174413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_LO_MODE	0x88	/* reserved pins as input */
175413058dfSJan Kiszka 
176413058dfSJan Kiszka /* IOT2040 MPIOs 8..15 */
177413058dfSJan Kiszka #define IOT2040_UARTS_ENABLE		0x03
178413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_HI_MODE	0xF8	/* enable & LED as outputs */
179413058dfSJan Kiszka 
180f7ce0706SParker Newman /* CTI EEPROM offsets */
181f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_OSC_FREQ	0x04  /* 2 words */
182f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_OSC_FREQ	0x08  /* 2 words */
183f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_PART_NUM	0x0A  /* 4 words */
184f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_PART_NUM	0x0E  /* 4 words */
185f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_SERIAL_NUM	0x0E  /* 1 word */
186f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_SERIAL_NUM	0x12  /* 1 word */
187f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_SERIAL_NUM	0x11  /* 2 word */
188f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_BRD_FLAGS	0x13  /* 1 word */
189f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_PORT_FLAGS	0x14  /* 1 word */
190f7ce0706SParker Newman 
191f7ce0706SParker Newman #define CTI_EE_MASK_PORT_FLAGS_TYPE	GENMASK(7, 0)
192f7ce0706SParker Newman #define CTI_EE_MASK_OSC_FREQ_LOWER	GENMASK(15, 0)
193f7ce0706SParker Newman #define CTI_EE_MASK_OSC_FREQ_UPPER	GENMASK(31, 16)
194f7ce0706SParker Newman 
195f7ce0706SParker Newman #define CTI_FPGA_RS485_IO_REG		0x2008
196f7ce0706SParker Newman #define CTI_FPGA_CFG_INT_EN_REG		0x48
197f7ce0706SParker Newman #define CTI_FPGA_CFG_INT_EN_EXT_BIT	BIT(15) /* External int enable bit */
198f7ce0706SParker Newman 
199f7ce0706SParker Newman #define CTI_DEFAULT_PCI_OSC_FREQ	29491200
200f7ce0706SParker Newman #define CTI_DEFAULT_PCIE_OSC_FREQ	125000000
201f7ce0706SParker Newman #define CTI_DEFAULT_FPGA_OSC_FREQ	33333333
202f7ce0706SParker Newman 
203f7ce0706SParker Newman /*
204f7ce0706SParker Newman  * CTI Serial port line types. These match the values stored in the first
205f7ce0706SParker Newman  * nibble of the CTI EEPROM port_flags word.
206f7ce0706SParker Newman  */
207f7ce0706SParker Newman enum cti_port_type {
208f7ce0706SParker Newman 	CTI_PORT_TYPE_NONE = 0,
209f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232,            // RS232 ONLY
210f7ce0706SParker Newman 	CTI_PORT_TYPE_RS422_485,        // RS422/RS485 ONLY
211f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_HW, // RS232/422/485 HW ONLY Switchable
212f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_SW, // RS232/422/485 SW ONLY Switchable
213f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_4B, // RS232/422/485 HW/SW (4bit ex. BCG004)
214f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_2B, // RS232/422/485 HW/SW (2bit ex. BBG008)
215f7ce0706SParker Newman 	CTI_PORT_TYPE_MAX,
216f7ce0706SParker Newman };
217f7ce0706SParker Newman 
218f7ce0706SParker Newman #define CTI_PORT_TYPE_VALID(_port_type) \
219f7ce0706SParker Newman 	(((_port_type) > CTI_PORT_TYPE_NONE) && \
220f7ce0706SParker Newman 	((_port_type) < CTI_PORT_TYPE_MAX))
221f7ce0706SParker Newman 
222f7ce0706SParker Newman #define CTI_PORT_TYPE_RS485(_port_type) \
223f7ce0706SParker Newman 	(((_port_type) > CTI_PORT_TYPE_RS232) && \
224f7ce0706SParker Newman 	((_port_type) < CTI_PORT_TYPE_MAX))
225f7ce0706SParker Newman 
226d0aeaa83SSudip Mukherjee struct exar8250;
227d0aeaa83SSudip Mukherjee 
2280d963ebfSJan Kiszka struct exar8250_platform {
229ae50bb27SIlpo Järvinen 	int (*rs485_config)(struct uart_port *port, struct ktermios *termios,
230ae50bb27SIlpo Järvinen 			    struct serial_rs485 *rs485);
23159c221f8SIlpo Järvinen 	const struct serial_rs485 *rs485_supported;
232c6795fbfSParker Newman 	int (*register_gpio)(struct pci_dev *pcidev, struct uart_8250_port *port);
233c6795fbfSParker Newman 	void (*unregister_gpio)(struct uart_8250_port *port);
2340d963ebfSJan Kiszka };
2350d963ebfSJan Kiszka 
236d0aeaa83SSudip Mukherjee /**
237d0aeaa83SSudip Mukherjee  * struct exar8250_board - board information
238d0aeaa83SSudip Mukherjee  * @num_ports: number of serial ports
239d0aeaa83SSudip Mukherjee  * @reg_shift: describes UART register mapping in PCI memory
240393b520aSParker Newman  * @board_init: quirk run once at ->probe() stage before setting up ports
241393b520aSParker Newman  * @setup: quirk run at ->probe() stage for each port
24226f22d57SAndy Shevchenko  * @exit: quirk run at ->remove() stage
243d0aeaa83SSudip Mukherjee  */
244d0aeaa83SSudip Mukherjee struct exar8250_board {
245d0aeaa83SSudip Mukherjee 	unsigned int num_ports;
246d0aeaa83SSudip Mukherjee 	unsigned int reg_shift;
247393b520aSParker Newman 	int     (*board_init)(struct exar8250 *priv, struct pci_dev *pcidev);
248c6795fbfSParker Newman 	int	(*setup)(struct exar8250 *priv, struct pci_dev *pcidev,
249c6795fbfSParker Newman 			 struct uart_8250_port *port, int idx);
250d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
251d0aeaa83SSudip Mukherjee };
252d0aeaa83SSudip Mukherjee 
253d0aeaa83SSudip Mukherjee struct exar8250 {
254d0aeaa83SSudip Mukherjee 	unsigned int		nr;
255f7ce0706SParker Newman 	unsigned int		osc_freq;
256d0aeaa83SSudip Mukherjee 	struct exar8250_board	*board;
257c7e1b405SAaron Sierra 	void __iomem		*virt;
25800d963abSGustavo A. R. Silva 	int			line[];
259d0aeaa83SSudip Mukherjee };
260d0aeaa83SSudip Mukherjee 
261f7ce0706SParker Newman static inline void exar_write_reg(struct exar8250 *priv,
262f7ce0706SParker Newman 				unsigned int reg, u8 value)
263f7ce0706SParker Newman {
264f7ce0706SParker Newman 	writeb(value, priv->virt + reg);
265f7ce0706SParker Newman }
266f7ce0706SParker Newman 
267f7ce0706SParker Newman static inline u8 exar_read_reg(struct exar8250 *priv, unsigned int reg)
268f7ce0706SParker Newman {
269f7ce0706SParker Newman 	return readb(priv->virt + reg);
270f7ce0706SParker Newman }
271f7ce0706SParker Newman 
272f7ce0706SParker Newman static inline void exar_ee_select(struct exar8250 *priv)
273f7ce0706SParker Newman {
274f7ce0706SParker Newman 	// Set chip select pin high to enable EEPROM reads/writes
275f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, UART_EXAR_REGB_EECS);
276f7ce0706SParker Newman 	// Min ~500ns delay needed between CS assert and EEPROM access
277f7ce0706SParker Newman 	udelay(1);
278f7ce0706SParker Newman }
279f7ce0706SParker Newman 
280f7ce0706SParker Newman static inline void exar_ee_deselect(struct exar8250 *priv)
281f7ce0706SParker Newman {
282f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, 0x00);
283f7ce0706SParker Newman }
284f7ce0706SParker Newman 
285f7ce0706SParker Newman static inline void exar_ee_write_bit(struct exar8250 *priv, int bit)
286f7ce0706SParker Newman {
287f7ce0706SParker Newman 	u8 value = UART_EXAR_REGB_EECS;
288f7ce0706SParker Newman 
289f7ce0706SParker Newman 	if (bit)
290f7ce0706SParker Newman 		value |= UART_EXAR_REGB_EEDI;
291f7ce0706SParker Newman 
292f7ce0706SParker Newman 	// Clock out the bit on the EEPROM interface
293f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
294f7ce0706SParker Newman 	// 2us delay = ~500khz clock speed
295f7ce0706SParker Newman 	udelay(2);
296f7ce0706SParker Newman 
297f7ce0706SParker Newman 	value |= UART_EXAR_REGB_EECK;
298f7ce0706SParker Newman 
299f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
300f7ce0706SParker Newman 	udelay(2);
301f7ce0706SParker Newman }
302f7ce0706SParker Newman 
303f7ce0706SParker Newman static inline u8 exar_ee_read_bit(struct exar8250 *priv)
304f7ce0706SParker Newman {
305f7ce0706SParker Newman 	u8 regb;
306f7ce0706SParker Newman 	u8 value = UART_EXAR_REGB_EECS;
307f7ce0706SParker Newman 
308f7ce0706SParker Newman 	// Clock in the bit on the EEPROM interface
309f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
310f7ce0706SParker Newman 	// 2us delay = ~500khz clock speed
311f7ce0706SParker Newman 	udelay(2);
312f7ce0706SParker Newman 
313f7ce0706SParker Newman 	value |= UART_EXAR_REGB_EECK;
314f7ce0706SParker Newman 
315f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
316f7ce0706SParker Newman 	udelay(2);
317f7ce0706SParker Newman 
318f7ce0706SParker Newman 	regb = exar_read_reg(priv, UART_EXAR_REGB);
319f7ce0706SParker Newman 
320f7ce0706SParker Newman 	return (regb & UART_EXAR_REGB_EEDO ? 1 : 0);
321f7ce0706SParker Newman }
322f7ce0706SParker Newman 
323f7ce0706SParker Newman /**
324f7ce0706SParker Newman  * exar_ee_read() - Read a word from the EEPROM
325f7ce0706SParker Newman  * @priv: Device's private structure
326f7ce0706SParker Newman  * @ee_addr: Offset of EEPROM to read word from
327f7ce0706SParker Newman  *
328f7ce0706SParker Newman  * Read a single 16bit word from an Exar UART's EEPROM.
329f7ce0706SParker Newman  *
330f7ce0706SParker Newman  * Return: EEPROM word
331f7ce0706SParker Newman  */
332f7ce0706SParker Newman static u16 exar_ee_read(struct exar8250 *priv, u8 ee_addr)
333f7ce0706SParker Newman {
334f7ce0706SParker Newman 	int i;
335f7ce0706SParker Newman 	u16 data = 0;
336f7ce0706SParker Newman 
337f7ce0706SParker Newman 	exar_ee_select(priv);
338f7ce0706SParker Newman 
339f7ce0706SParker Newman 	// Send read command (opcode 110)
340f7ce0706SParker Newman 	exar_ee_write_bit(priv, 1);
341f7ce0706SParker Newman 	exar_ee_write_bit(priv, 1);
342f7ce0706SParker Newman 	exar_ee_write_bit(priv, 0);
343f7ce0706SParker Newman 
344f7ce0706SParker Newman 	// Send address to read from
345f7ce0706SParker Newman 	for (i = 1 << (UART_EXAR_REGB_EE_ADDR_SIZE - 1); i; i >>= 1)
346f7ce0706SParker Newman 		exar_ee_write_bit(priv, (ee_addr & i));
347f7ce0706SParker Newman 
348f7ce0706SParker Newman 	// Read data 1 bit at a time
349f7ce0706SParker Newman 	for (i = 0; i <= UART_EXAR_REGB_EE_DATA_SIZE; i++) {
350f7ce0706SParker Newman 		data <<= 1;
351f7ce0706SParker Newman 		data |= exar_ee_read_bit(priv);
352f7ce0706SParker Newman 	}
353f7ce0706SParker Newman 
354f7ce0706SParker Newman 	exar_ee_deselect(priv);
355f7ce0706SParker Newman 
356f7ce0706SParker Newman 	return data;
357f7ce0706SParker Newman }
358f7ce0706SParker Newman 
359f7ce0706SParker Newman /**
360f7ce0706SParker Newman  * exar_mpio_config_output() - Configure an Exar MPIO as an output
361f7ce0706SParker Newman  * @priv: Device's private structure
362f7ce0706SParker Newman  * @mpio_num: MPIO number/offset to configure
363f7ce0706SParker Newman  *
364f7ce0706SParker Newman  * Configure a single MPIO as an output and disable tristate. It is reccomended
365f7ce0706SParker Newman  * to set the level with exar_mpio_set_high()/exar_mpio_set_low() prior to
366f7ce0706SParker Newman  * calling this function to ensure default MPIO pin state.
367f7ce0706SParker Newman  *
368f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
369f7ce0706SParker Newman  */
370f7ce0706SParker Newman static int exar_mpio_config_output(struct exar8250 *priv,
371f7ce0706SParker Newman 				unsigned int mpio_num)
372f7ce0706SParker Newman {
373f7ce0706SParker Newman 	unsigned int mpio_offset;
374f7ce0706SParker Newman 	u8 sel_reg; // MPIO Select register (input/output)
375f7ce0706SParker Newman 	u8 tri_reg; // MPIO Tristate register
376f7ce0706SParker Newman 	u8 value;
377f7ce0706SParker Newman 
378f7ce0706SParker Newman 	if (mpio_num < 8) {
379f7ce0706SParker Newman 		sel_reg = UART_EXAR_MPIOSEL_7_0;
380f7ce0706SParker Newman 		tri_reg = UART_EXAR_MPIO3T_7_0;
381f7ce0706SParker Newman 		mpio_offset = mpio_num;
382f7ce0706SParker Newman 	} else if (mpio_num >= 8 && mpio_num < 16) {
383f7ce0706SParker Newman 		sel_reg = UART_EXAR_MPIOSEL_15_8;
384f7ce0706SParker Newman 		tri_reg = UART_EXAR_MPIO3T_15_8;
385f7ce0706SParker Newman 		mpio_offset = mpio_num - 8;
386f7ce0706SParker Newman 	} else {
387f7ce0706SParker Newman 		return -EINVAL;
388f7ce0706SParker Newman 	}
389f7ce0706SParker Newman 
390f7ce0706SParker Newman 	// Disable MPIO pin tri-state
391f7ce0706SParker Newman 	value = exar_read_reg(priv, tri_reg);
392f7ce0706SParker Newman 	value &= ~BIT(mpio_offset);
393f7ce0706SParker Newman 	exar_write_reg(priv, tri_reg, value);
394f7ce0706SParker Newman 
395f7ce0706SParker Newman 	value = exar_read_reg(priv, sel_reg);
396f7ce0706SParker Newman 	value &= ~BIT(mpio_offset);
397f7ce0706SParker Newman 	exar_write_reg(priv, sel_reg, value);
398f7ce0706SParker Newman 
399f7ce0706SParker Newman 	return 0;
400f7ce0706SParker Newman }
401f7ce0706SParker Newman 
402f7ce0706SParker Newman /**
403f7ce0706SParker Newman  * _exar_mpio_set() - Set an Exar MPIO output high or low
404f7ce0706SParker Newman  * @priv: Device's private structure
405f7ce0706SParker Newman  * @mpio_num: MPIO number/offset to set
406f7ce0706SParker Newman  * @high: Set MPIO high if true, low if false
407f7ce0706SParker Newman  *
408f7ce0706SParker Newman  * Set a single MPIO high or low. exar_mpio_config_output() must also be called
409f7ce0706SParker Newman  * to configure the pin as an output.
410f7ce0706SParker Newman  *
411f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
412f7ce0706SParker Newman  */
413f7ce0706SParker Newman static int _exar_mpio_set(struct exar8250 *priv,
414f7ce0706SParker Newman 			unsigned int mpio_num, bool high)
415f7ce0706SParker Newman {
416f7ce0706SParker Newman 	unsigned int mpio_offset;
417f7ce0706SParker Newman 	u8 lvl_reg;
418f7ce0706SParker Newman 	u8 value;
419f7ce0706SParker Newman 
420f7ce0706SParker Newman 	if (mpio_num < 8) {
421f7ce0706SParker Newman 		lvl_reg = UART_EXAR_MPIOLVL_7_0;
422f7ce0706SParker Newman 		mpio_offset = mpio_num;
423f7ce0706SParker Newman 	} else if (mpio_num >= 8 && mpio_num < 16) {
424f7ce0706SParker Newman 		lvl_reg = UART_EXAR_MPIOLVL_15_8;
425f7ce0706SParker Newman 		mpio_offset = mpio_num - 8;
426f7ce0706SParker Newman 	} else {
427f7ce0706SParker Newman 		return -EINVAL;
428f7ce0706SParker Newman 	}
429f7ce0706SParker Newman 
430f7ce0706SParker Newman 	value = exar_read_reg(priv, lvl_reg);
431f7ce0706SParker Newman 	if (high)
432f7ce0706SParker Newman 		value |= BIT(mpio_offset);
433f7ce0706SParker Newman 	else
434f7ce0706SParker Newman 		value &= ~BIT(mpio_offset);
435f7ce0706SParker Newman 	exar_write_reg(priv, lvl_reg, value);
436f7ce0706SParker Newman 
437f7ce0706SParker Newman 	return 0;
438f7ce0706SParker Newman }
439f7ce0706SParker Newman 
440f7ce0706SParker Newman static int exar_mpio_set_low(struct exar8250 *priv, unsigned int mpio_num)
441f7ce0706SParker Newman {
442f7ce0706SParker Newman 	return _exar_mpio_set(priv, mpio_num, false);
443f7ce0706SParker Newman }
444f7ce0706SParker Newman 
445f7ce0706SParker Newman static int exar_mpio_set_high(struct exar8250 *priv, unsigned int mpio_num)
446f7ce0706SParker Newman {
447f7ce0706SParker Newman 	return _exar_mpio_set(priv, mpio_num, true);
448f7ce0706SParker Newman }
449f7ce0706SParker Newman 
450209a20d4SParker Newman static int generic_rs485_config(struct uart_port *port, struct ktermios *termios,
451209a20d4SParker Newman 				struct serial_rs485 *rs485)
452209a20d4SParker Newman {
453209a20d4SParker Newman 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
454209a20d4SParker Newman 	u8 __iomem *p = port->membase;
455209a20d4SParker Newman 	u8 value;
456209a20d4SParker Newman 
457209a20d4SParker Newman 	value = readb(p + UART_EXAR_FCTR);
458209a20d4SParker Newman 	if (is_rs485)
459209a20d4SParker Newman 		value |= UART_FCTR_EXAR_485;
460209a20d4SParker Newman 	else
461209a20d4SParker Newman 		value &= ~UART_FCTR_EXAR_485;
462209a20d4SParker Newman 
463209a20d4SParker Newman 	writeb(value, p + UART_EXAR_FCTR);
464209a20d4SParker Newman 
465209a20d4SParker Newman 	if (is_rs485)
466209a20d4SParker Newman 		writeb(UART_EXAR_RS485_DLY(4), p + UART_MSR);
467209a20d4SParker Newman 
468209a20d4SParker Newman 	return 0;
469209a20d4SParker Newman }
470209a20d4SParker Newman 
471209a20d4SParker Newman static const struct serial_rs485 generic_rs485_supported = {
472209a20d4SParker Newman 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND,
473209a20d4SParker Newman };
474209a20d4SParker Newman 
475ef4e281eSAndy Shevchenko static void exar_pm(struct uart_port *port, unsigned int state, unsigned int old)
476ef4e281eSAndy Shevchenko {
477ef4e281eSAndy Shevchenko 	/*
478ef4e281eSAndy Shevchenko 	 * Exar UARTs have a SLEEP register that enables or disables each UART
479ef4e281eSAndy Shevchenko 	 * to enter sleep mode separately. On the XR17V35x the register
480ef4e281eSAndy Shevchenko 	 * is accessible to each UART at the UART_EXAR_SLEEP offset, but
481ef4e281eSAndy Shevchenko 	 * the UART channel may only write to the corresponding bit.
482ef4e281eSAndy Shevchenko 	 */
483ef4e281eSAndy Shevchenko 	serial_port_out(port, UART_EXAR_SLEEP, state ? 0xff : 0);
484ef4e281eSAndy Shevchenko }
485ef4e281eSAndy Shevchenko 
486b2b4b8edSAndy Shevchenko /*
487b2b4b8edSAndy Shevchenko  * XR17V35x UARTs have an extra fractional divisor register (DLD)
488b2b4b8edSAndy Shevchenko  * Calculate divisor with extra 4-bit fractional portion
489b2b4b8edSAndy Shevchenko  */
490b2b4b8edSAndy Shevchenko static unsigned int xr17v35x_get_divisor(struct uart_port *p, unsigned int baud,
491b2b4b8edSAndy Shevchenko 					 unsigned int *frac)
492b2b4b8edSAndy Shevchenko {
493b2b4b8edSAndy Shevchenko 	unsigned int quot_16;
494b2b4b8edSAndy Shevchenko 
495b2b4b8edSAndy Shevchenko 	quot_16 = DIV_ROUND_CLOSEST(p->uartclk, baud);
496b2b4b8edSAndy Shevchenko 	*frac = quot_16 & 0x0f;
497b2b4b8edSAndy Shevchenko 
498b2b4b8edSAndy Shevchenko 	return quot_16 >> 4;
499b2b4b8edSAndy Shevchenko }
500b2b4b8edSAndy Shevchenko 
501b2b4b8edSAndy Shevchenko static void xr17v35x_set_divisor(struct uart_port *p, unsigned int baud,
502b2b4b8edSAndy Shevchenko 				 unsigned int quot, unsigned int quot_frac)
503b2b4b8edSAndy Shevchenko {
504b2b4b8edSAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
505b2b4b8edSAndy Shevchenko 
506b2b4b8edSAndy Shevchenko 	/* Preserve bits not related to baudrate; DLD[7:4]. */
507b2b4b8edSAndy Shevchenko 	quot_frac |= serial_port_in(p, 0x2) & 0xf0;
508b2b4b8edSAndy Shevchenko 	serial_port_out(p, 0x2, quot_frac);
509b2b4b8edSAndy Shevchenko }
510b2b4b8edSAndy Shevchenko 
5116e731137SAndy Shevchenko static int xr17v35x_startup(struct uart_port *port)
5126e731137SAndy Shevchenko {
5136e731137SAndy Shevchenko 	/*
5146e731137SAndy Shevchenko 	 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
5156e731137SAndy Shevchenko 	 * MCR [7:5] and MSR [7:0]
5166e731137SAndy Shevchenko 	 */
5176e731137SAndy Shevchenko 	serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
5186e731137SAndy Shevchenko 
5196e731137SAndy Shevchenko 	/*
5206e731137SAndy Shevchenko 	 * Make sure all interrups are masked until initialization is
5216e731137SAndy Shevchenko 	 * complete and the FIFOs are cleared
522b1207d86SJohn Ogness 	 *
523b1207d86SJohn Ogness 	 * Synchronize UART_IER access against the console.
5246e731137SAndy Shevchenko 	 */
5252b71b31fSThomas Gleixner 	uart_port_lock_irq(port);
5266e731137SAndy Shevchenko 	serial_port_out(port, UART_IER, 0);
5272b71b31fSThomas Gleixner 	uart_port_unlock_irq(port);
5286e731137SAndy Shevchenko 
5296e731137SAndy Shevchenko 	return serial8250_do_startup(port);
5306e731137SAndy Shevchenko }
5316e731137SAndy Shevchenko 
532653d00c8SAndy Shevchenko static void exar_shutdown(struct uart_port *port)
533653d00c8SAndy Shevchenko {
53467e977f3SZheng Bin 	bool tx_complete = false;
535653d00c8SAndy Shevchenko 	struct uart_8250_port *up = up_to_u8250p(port);
5361788cf6aSJiri Slaby (SUSE) 	struct tty_port *tport = &port->state->port;
537653d00c8SAndy Shevchenko 	int i = 0;
538f8ba5680SIlpo Järvinen 	u16 lsr;
539653d00c8SAndy Shevchenko 
540653d00c8SAndy Shevchenko 	do {
541653d00c8SAndy Shevchenko 		lsr = serial_in(up, UART_LSR);
542653d00c8SAndy Shevchenko 		if (lsr & (UART_LSR_TEMT | UART_LSR_THRE))
54367e977f3SZheng Bin 			tx_complete = true;
544653d00c8SAndy Shevchenko 		else
54567e977f3SZheng Bin 			tx_complete = false;
5463f72879eSAndy Shevchenko 		usleep_range(1000, 1100);
5471788cf6aSJiri Slaby (SUSE) 	} while (!kfifo_is_empty(&tport->xmit_fifo) &&
5481788cf6aSJiri Slaby (SUSE) 			!tx_complete && i++ < 1000);
549653d00c8SAndy Shevchenko 
550653d00c8SAndy Shevchenko 	serial8250_do_shutdown(port);
551653d00c8SAndy Shevchenko }
552653d00c8SAndy Shevchenko 
553d0aeaa83SSudip Mukherjee static int default_setup(struct exar8250 *priv, struct pci_dev *pcidev,
554d0aeaa83SSudip Mukherjee 			 int idx, unsigned int offset,
555d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *port)
556d0aeaa83SSudip Mukherjee {
557d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
5586be254c2SAndy Shevchenko 	unsigned char status;
559d813d900SAndy Shevchenko 	int err;
560d0aeaa83SSudip Mukherjee 
561d813d900SAndy Shevchenko 	err = serial8250_pci_setup_port(pcidev, port, 0, offset, board->reg_shift);
562d813d900SAndy Shevchenko 	if (err)
563d813d900SAndy Shevchenko 		return err;
564d0aeaa83SSudip Mukherjee 
5656be254c2SAndy Shevchenko 	/*
5666be254c2SAndy Shevchenko 	 * XR17V35x UARTs have an extra divisor register, DLD that gets enabled
5676be254c2SAndy Shevchenko 	 * with when DLAB is set which will cause the device to incorrectly match
5686be254c2SAndy Shevchenko 	 * and assign port type to PORT_16650. The EFR for this UART is found
5696be254c2SAndy Shevchenko 	 * at offset 0x09. Instead check the Deice ID (DVID) register
5706be254c2SAndy Shevchenko 	 * for a 2, 4 or 8 port UART.
5716be254c2SAndy Shevchenko 	 */
5726be254c2SAndy Shevchenko 	status = readb(port->port.membase + UART_EXAR_DVID);
5736be254c2SAndy Shevchenko 	if (status == 0x82 || status == 0x84 || status == 0x88) {
5746be254c2SAndy Shevchenko 		port->port.type = PORT_XR17V35X;
575b2b4b8edSAndy Shevchenko 
576b2b4b8edSAndy Shevchenko 		port->port.get_divisor = xr17v35x_get_divisor;
577b2b4b8edSAndy Shevchenko 		port->port.set_divisor = xr17v35x_set_divisor;
5786e731137SAndy Shevchenko 
5796e731137SAndy Shevchenko 		port->port.startup = xr17v35x_startup;
5806be254c2SAndy Shevchenko 	} else {
5816be254c2SAndy Shevchenko 		port->port.type = PORT_XR17D15X;
5826be254c2SAndy Shevchenko 	}
5836be254c2SAndy Shevchenko 
584ef4e281eSAndy Shevchenko 	port->port.pm = exar_pm;
585653d00c8SAndy Shevchenko 	port->port.shutdown = exar_shutdown;
586ef4e281eSAndy Shevchenko 
587d0aeaa83SSudip Mukherjee 	return 0;
588d0aeaa83SSudip Mukherjee }
589d0aeaa83SSudip Mukherjee 
590d0aeaa83SSudip Mukherjee static int
591fc6cc961SJan Kiszka pci_fastcom335_setup(struct exar8250 *priv, struct pci_dev *pcidev,
592fc6cc961SJan Kiszka 		     struct uart_8250_port *port, int idx)
593fc6cc961SJan Kiszka {
594fc6cc961SJan Kiszka 	unsigned int offset = idx * 0x200;
595fc6cc961SJan Kiszka 	unsigned int baud = 1843200;
596fc6cc961SJan Kiszka 	u8 __iomem *p;
597fc6cc961SJan Kiszka 	int err;
598fc6cc961SJan Kiszka 
599fc6cc961SJan Kiszka 	port->port.uartclk = baud * 16;
600fc6cc961SJan Kiszka 
601fc6cc961SJan Kiszka 	err = default_setup(priv, pcidev, idx, offset, port);
602fc6cc961SJan Kiszka 	if (err)
603fc6cc961SJan Kiszka 		return err;
604fc6cc961SJan Kiszka 
605fc6cc961SJan Kiszka 	p = port->port.membase;
606fc6cc961SJan Kiszka 
607fc6cc961SJan Kiszka 	writeb(0x00, p + UART_EXAR_8XMODE);
608fc6cc961SJan Kiszka 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
609fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_TXTRG);
610fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_RXTRG);
611fc6cc961SJan Kiszka 
612fc6cc961SJan Kiszka 	/*
613fc6cc961SJan Kiszka 	 * Setup Multipurpose Input/Output pins.
614fc6cc961SJan Kiszka 	 */
615fc6cc961SJan Kiszka 	if (idx == 0) {
616fc6cc961SJan Kiszka 		switch (pcidev->device) {
617fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4222PCI335:
618fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4224PCI335:
619fc6cc961SJan Kiszka 			writeb(0x78, p + UART_EXAR_MPIOLVL_7_0);
620fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
621fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
622fc6cc961SJan Kiszka 			break;
623fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2324PCI335:
624fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2328PCI335:
625fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
626fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOINV_7_0);
627fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOSEL_7_0);
628fc6cc961SJan Kiszka 			break;
629fc6cc961SJan Kiszka 		}
630fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
631fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
632fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
633fc6cc961SJan Kiszka 	}
634fc6cc961SJan Kiszka 
635fc6cc961SJan Kiszka 	return 0;
636fc6cc961SJan Kiszka }
637fc6cc961SJan Kiszka 
638f7ce0706SParker Newman /**
639f7ce0706SParker Newman  * cti_tristate_disable() - Disable RS485 transciever tristate
640f7ce0706SParker Newman  * @priv: Device's private structure
641f7ce0706SParker Newman  * @port_num: Port number to set tristate off
642f7ce0706SParker Newman  *
643f7ce0706SParker Newman  * Most RS485 capable cards have a power on tristate jumper/switch that ensures
6441cf8520aSAndy Shevchenko  * the RS422/RS485 transceiver does not drive a multi-drop RS485 bus when it is
645f7ce0706SParker Newman  * not the master. When this jumper is installed the user must set the RS485
646f7ce0706SParker Newman  * mode to Full or Half duplex to disable tristate prior to using the port.
647f7ce0706SParker Newman  *
648f7ce0706SParker Newman  * Some Exar UARTs have an auto-tristate feature while others require setting
649f7ce0706SParker Newman  * an MPIO to disable the tristate.
650f7ce0706SParker Newman  *
651f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
652f7ce0706SParker Newman  */
653f7ce0706SParker Newman static int cti_tristate_disable(struct exar8250 *priv, unsigned int port_num)
654f7ce0706SParker Newman {
655f7ce0706SParker Newman 	int ret;
656f7ce0706SParker Newman 
657f7ce0706SParker Newman 	ret = exar_mpio_set_high(priv, port_num);
658f7ce0706SParker Newman 	if (ret)
659f7ce0706SParker Newman 		return ret;
660f7ce0706SParker Newman 
661f7ce0706SParker Newman 	return exar_mpio_config_output(priv, port_num);
662f7ce0706SParker Newman }
663f7ce0706SParker Newman 
664f7ce0706SParker Newman /**
665f7ce0706SParker Newman  * cti_plx_int_enable() - Enable UART interrupts to PLX bridge
666f7ce0706SParker Newman  * @priv: Device's private structure
667f7ce0706SParker Newman  *
668f7ce0706SParker Newman  * Some older CTI cards require MPIO_0 to be set low to enable the
6691cf8520aSAndy Shevchenko  * interrupts from the UART to the PLX PCI->PCIe bridge.
670f7ce0706SParker Newman  *
671f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
672f7ce0706SParker Newman  */
673f7ce0706SParker Newman static int cti_plx_int_enable(struct exar8250 *priv)
674f7ce0706SParker Newman {
675f7ce0706SParker Newman 	int ret;
676f7ce0706SParker Newman 
677f7ce0706SParker Newman 	ret = exar_mpio_set_low(priv, 0);
678f7ce0706SParker Newman 	if (ret)
679f7ce0706SParker Newman 		return ret;
680f7ce0706SParker Newman 
681f7ce0706SParker Newman 	return exar_mpio_config_output(priv, 0);
682f7ce0706SParker Newman }
683f7ce0706SParker Newman 
684f7ce0706SParker Newman /**
685f7ce0706SParker Newman  * cti_read_osc_freq() - Read the UART oscillator frequency from EEPROM
686f7ce0706SParker Newman  * @priv: Device's private structure
687f7ce0706SParker Newman  * @eeprom_offset: Offset where the oscillator frequency is stored
688f7ce0706SParker Newman  *
689f7ce0706SParker Newman  * CTI XR17x15X and XR17V25X cards have the serial boards oscillator frequency
690f7ce0706SParker Newman  * stored in the EEPROM. FPGA and XR17V35X based cards use the PCI/PCIe clock.
691f7ce0706SParker Newman  *
692f7ce0706SParker Newman  * Return: frequency on success, negative error code on failure
693f7ce0706SParker Newman  */
694f7ce0706SParker Newman static int cti_read_osc_freq(struct exar8250 *priv, u8 eeprom_offset)
695f7ce0706SParker Newman {
696f7ce0706SParker Newman 	u16 lower_word;
697f7ce0706SParker Newman 	u16 upper_word;
698f7ce0706SParker Newman 
699f7ce0706SParker Newman 	lower_word = exar_ee_read(priv, eeprom_offset);
700f7ce0706SParker Newman 	// Check if EEPROM word was blank
701f7ce0706SParker Newman 	if (lower_word == 0xFFFF)
702f7ce0706SParker Newman 		return -EIO;
703f7ce0706SParker Newman 
704f7ce0706SParker Newman 	upper_word = exar_ee_read(priv, (eeprom_offset + 1));
705f7ce0706SParker Newman 	if (upper_word == 0xFFFF)
706f7ce0706SParker Newman 		return -EIO;
707f7ce0706SParker Newman 
708*c5f59747SAndy Shevchenko 	return FIELD_PREP(CTI_EE_MASK_OSC_FREQ_LOWER, lower_word) |
709f7ce0706SParker Newman 	       FIELD_PREP(CTI_EE_MASK_OSC_FREQ_UPPER, upper_word);
710f7ce0706SParker Newman }
711f7ce0706SParker Newman 
712f7ce0706SParker Newman /**
713f7ce0706SParker Newman  * cti_get_port_type_xr17c15x_xr17v25x() - Get port type of xr17c15x/xr17v25x
714f7ce0706SParker Newman  * @priv: Device's private structure
71519234a5fSAndy Shevchenko  * @pcidev: Pointer to the PCI device for this port
716f7ce0706SParker Newman  * @port_num: Port to get type of
717f7ce0706SParker Newman  *
718f7ce0706SParker Newman  * CTI xr17c15x and xr17v25x based cards port types are based on PCI IDs.
719f7ce0706SParker Newman  *
720f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
721f7ce0706SParker Newman  */
722f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_xr17c15x_xr17v25x(struct exar8250 *priv,
723f7ce0706SParker Newman 							struct pci_dev *pcidev,
724f7ce0706SParker Newman 							unsigned int port_num)
725f7ce0706SParker Newman {
726f7ce0706SParker Newman 	enum cti_port_type port_type;
727f7ce0706SParker Newman 
728f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
729f7ce0706SParker Newman 	// RS232 only cards
730f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_232:
731f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_232:
732f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_232:
733f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232:
734f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232_NS:
735f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232:
736f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232_NS:
737f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232;
738f7ce0706SParker Newman 		break;
739f7ce0706SParker Newman 	// 1x RS232, 1x RS422/RS485
740f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_1_1:
741f7ce0706SParker Newman 		port_type = (port_num == 0) ?
742f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
743f7ce0706SParker Newman 		break;
744f7ce0706SParker Newman 	// 2x RS232, 2x RS422/RS485
745f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_2:
746f7ce0706SParker Newman 		port_type = (port_num < 2) ?
747f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
748f7ce0706SParker Newman 		break;
749f7ce0706SParker Newman 	// 4x RS232, 4x RS422/RS485
750f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4:
751f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
752f7ce0706SParker Newman 		port_type = (port_num < 4) ?
753f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
754f7ce0706SParker Newman 		break;
755f7ce0706SParker Newman 	// RS232/RS422/RS485 HW (jumper) selectable
756f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2:
757f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4:
758f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8:
759f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_SP_OPTO:
760f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_A:
761f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_B:
762f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS:
763f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_A:
764f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_B:
765f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS:
766f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_A:
767f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_B:
768f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS_OPTO:
769f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_A:
770f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_B:
771f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
772f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
773f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
774f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
775f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
776f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
777f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
778f7ce0706SParker Newman 		break;
779f7ce0706SParker Newman 	// RS422/RS485 HW (jumper) selectable
780f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_485:
781f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_485:
782f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_485:
783f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
784f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
785f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS422_485;
786f7ce0706SParker Newman 		break;
787f7ce0706SParker Newman 	// 6x RS232, 2x RS422/RS485
788f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
789f7ce0706SParker Newman 		port_type = (port_num < 6) ?
790f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
791f7ce0706SParker Newman 		break;
792f7ce0706SParker Newman 	// 2x RS232, 6x RS422/RS485
793f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
794f7ce0706SParker Newman 		port_type = (port_num < 2) ?
795f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
796f7ce0706SParker Newman 		break;
797f7ce0706SParker Newman 	default:
798f7ce0706SParker Newman 		dev_err(&pcidev->dev, "unknown/unsupported device\n");
799f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_NONE;
800f7ce0706SParker Newman 	}
801f7ce0706SParker Newman 
802f7ce0706SParker Newman 	return port_type;
803f7ce0706SParker Newman }
804f7ce0706SParker Newman 
805f7ce0706SParker Newman /**
806f7ce0706SParker Newman  * cti_get_port_type_fpga() - Get the port type of a CTI FPGA card
807f7ce0706SParker Newman  * @priv: Device's private structure
80819234a5fSAndy Shevchenko  * @pcidev: Pointer to the PCI device for this port
809f7ce0706SParker Newman  * @port_num: Port to get type of
810f7ce0706SParker Newman  *
811f7ce0706SParker Newman  * FPGA based cards port types are based on PCI IDs.
812f7ce0706SParker Newman  *
813f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
814f7ce0706SParker Newman  */
815f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_fpga(struct exar8250 *priv,
816f7ce0706SParker Newman 						struct pci_dev *pcidev,
817f7ce0706SParker Newman 						unsigned int port_num)
818f7ce0706SParker Newman {
819f7ce0706SParker Newman 	enum cti_port_type port_type;
820f7ce0706SParker Newman 
821f7ce0706SParker Newman 	switch (pcidev->device) {
822f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG00X:
823f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG01X:
824f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_16:
825f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
826f7ce0706SParker Newman 		break;
827f7ce0706SParker Newman 	default:
828f7ce0706SParker Newman 		dev_err(&pcidev->dev, "unknown/unsupported device\n");
829f7ce0706SParker Newman 		return CTI_PORT_TYPE_NONE;
830f7ce0706SParker Newman 	}
831f7ce0706SParker Newman 
832f7ce0706SParker Newman 	return port_type;
833f7ce0706SParker Newman }
834f7ce0706SParker Newman 
835f7ce0706SParker Newman /**
836f7ce0706SParker Newman  * cti_get_port_type_xr17v35x() - Read port type from the EEPROM
837f7ce0706SParker Newman  * @priv: Device's private structure
83819234a5fSAndy Shevchenko  * @pcidev: Pointer to the PCI device for this port
839f7ce0706SParker Newman  * @port_num: port offset
840f7ce0706SParker Newman  *
841f7ce0706SParker Newman  * CTI XR17V35X based cards have the port types stored in the EEPROM.
842f7ce0706SParker Newman  * This function reads the port type for a single port.
843f7ce0706SParker Newman  *
844f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
845f7ce0706SParker Newman  */
846f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_xr17v35x(struct exar8250 *priv,
847f7ce0706SParker Newman 						struct pci_dev *pcidev,
848f7ce0706SParker Newman 						unsigned int port_num)
849f7ce0706SParker Newman {
850f7ce0706SParker Newman 	enum cti_port_type port_type;
851f7ce0706SParker Newman 	u16 port_flags;
852f7ce0706SParker Newman 	u8 offset;
853f7ce0706SParker Newman 
854f7ce0706SParker Newman 	offset = CTI_EE_OFF_XR17V35X_PORT_FLAGS + port_num;
855f7ce0706SParker Newman 	port_flags = exar_ee_read(priv, offset);
856f7ce0706SParker Newman 
857f7ce0706SParker Newman 	port_type = FIELD_GET(CTI_EE_MASK_PORT_FLAGS_TYPE, port_flags);
858f7ce0706SParker Newman 	if (!CTI_PORT_TYPE_VALID(port_type)) {
859f7ce0706SParker Newman 		/*
860f7ce0706SParker Newman 		 * If the port type is missing the card assume it is a
861f7ce0706SParker Newman 		 * RS232/RS422/RS485 card to be safe.
862f7ce0706SParker Newman 		 *
863f7ce0706SParker Newman 		 * There is one known board (BEG013) that only has
864f7ce0706SParker Newman 		 * 3 of 4 port types written to the EEPROM so this
865f7ce0706SParker Newman 		 * acts as a work around.
866f7ce0706SParker Newman 		 */
867f7ce0706SParker Newman 		dev_warn(&pcidev->dev,
868f7ce0706SParker Newman 			"failed to get port %d type from EEPROM\n", port_num);
869f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
870f7ce0706SParker Newman 	}
871f7ce0706SParker Newman 
872f7ce0706SParker Newman 	return port_type;
873f7ce0706SParker Newman }
874f7ce0706SParker Newman 
875f7ce0706SParker Newman static int cti_rs485_config_mpio_tristate(struct uart_port *port,
876f7ce0706SParker Newman 					struct ktermios *termios,
877f7ce0706SParker Newman 					struct serial_rs485 *rs485)
878f7ce0706SParker Newman {
879f7ce0706SParker Newman 	struct exar8250 *priv = (struct exar8250 *)port->private_data;
880f7ce0706SParker Newman 	int ret;
881f7ce0706SParker Newman 
882f7ce0706SParker Newman 	ret = generic_rs485_config(port, termios, rs485);
883f7ce0706SParker Newman 	if (ret)
884f7ce0706SParker Newman 		return ret;
885f7ce0706SParker Newman 
886f7ce0706SParker Newman 	// Disable power-on RS485 tri-state via MPIO
887f7ce0706SParker Newman 	return cti_tristate_disable(priv, port->port_id);
888f7ce0706SParker Newman }
889f7ce0706SParker Newman 
890*c5f59747SAndy Shevchenko static void cti_board_init_osc_freq(struct exar8250 *priv, struct pci_dev *pcidev, u8 eeprom_offset)
891*c5f59747SAndy Shevchenko {
892*c5f59747SAndy Shevchenko 	int osc_freq;
893*c5f59747SAndy Shevchenko 
894*c5f59747SAndy Shevchenko 	osc_freq = cti_read_osc_freq(priv, eeprom_offset);
895*c5f59747SAndy Shevchenko 	if (osc_freq <= 0) {
896*c5f59747SAndy Shevchenko 		dev_warn(&pcidev->dev, "failed to read OSC freq from EEPROM, using default\n");
897*c5f59747SAndy Shevchenko 		osc_freq = CTI_DEFAULT_PCI_OSC_FREQ;
898*c5f59747SAndy Shevchenko 	}
899*c5f59747SAndy Shevchenko 
900*c5f59747SAndy Shevchenko 	priv->osc_freq = osc_freq;
901*c5f59747SAndy Shevchenko }
902*c5f59747SAndy Shevchenko 
903f7ce0706SParker Newman static int cti_port_setup_common(struct exar8250 *priv,
904f7ce0706SParker Newman 				struct pci_dev *pcidev,
905f7ce0706SParker Newman 				int idx, unsigned int offset,
906f7ce0706SParker Newman 				struct uart_8250_port *port)
907f7ce0706SParker Newman {
908f7ce0706SParker Newman 	int ret;
909f7ce0706SParker Newman 
910f7ce0706SParker Newman 	if (priv->osc_freq == 0)
911f7ce0706SParker Newman 		return -EINVAL;
912f7ce0706SParker Newman 
913f7ce0706SParker Newman 	port->port.port_id = idx;
914f7ce0706SParker Newman 	port->port.uartclk = priv->osc_freq;
915f7ce0706SParker Newman 
916f7ce0706SParker Newman 	ret = serial8250_pci_setup_port(pcidev, port, 0, offset, 0);
917f7ce0706SParker Newman 	if (ret) {
918f7ce0706SParker Newman 		dev_err(&pcidev->dev,
919f7ce0706SParker Newman 			"failed to setup pci for port %d err: %d\n", idx, ret);
920f7ce0706SParker Newman 		return ret;
921f7ce0706SParker Newman 	}
922f7ce0706SParker Newman 
923f7ce0706SParker Newman 	port->port.private_data = (void *)priv;
924f7ce0706SParker Newman 	port->port.pm = exar_pm;
925f7ce0706SParker Newman 	port->port.shutdown = exar_shutdown;
926f7ce0706SParker Newman 
927f7ce0706SParker Newman 	return 0;
928f7ce0706SParker Newman }
929f7ce0706SParker Newman 
930f7ce0706SParker Newman static int cti_port_setup_fpga(struct exar8250 *priv,
931f7ce0706SParker Newman 				struct pci_dev *pcidev,
932f7ce0706SParker Newman 				struct uart_8250_port *port,
933f7ce0706SParker Newman 				int idx)
934f7ce0706SParker Newman {
935f7ce0706SParker Newman 	enum cti_port_type port_type;
936f7ce0706SParker Newman 	unsigned int offset;
937f7ce0706SParker Newman 
938f7ce0706SParker Newman 	port_type = cti_get_port_type_fpga(priv, pcidev, idx);
939f7ce0706SParker Newman 
9401cf8520aSAndy Shevchenko 	// FPGA shares port offsets with XR17C15X
941f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17C15X_PORT_OFFSET;
942f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
943f7ce0706SParker Newman 
944f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
945f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
946f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
947f7ce0706SParker Newman 
948f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
949f7ce0706SParker Newman 		port->port.rs485_config = generic_rs485_config;
950f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
951f7ce0706SParker Newman 	}
952f7ce0706SParker Newman 
953f7ce0706SParker Newman 	return cti_port_setup_common(priv, pcidev, idx, offset, port);
954f7ce0706SParker Newman }
955f7ce0706SParker Newman 
956f7ce0706SParker Newman static int cti_port_setup_xr17v35x(struct exar8250 *priv,
957f7ce0706SParker Newman 				struct pci_dev *pcidev,
958f7ce0706SParker Newman 				struct uart_8250_port *port,
959f7ce0706SParker Newman 				int idx)
960f7ce0706SParker Newman {
961f7ce0706SParker Newman 	enum cti_port_type port_type;
962f7ce0706SParker Newman 	unsigned int offset;
963f7ce0706SParker Newman 	int ret;
964f7ce0706SParker Newman 
965f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17v35x(priv, pcidev, idx);
966f7ce0706SParker Newman 
967f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17V35X_PORT_OFFSET;
968f7ce0706SParker Newman 	port->port.type = PORT_XR17V35X;
969f7ce0706SParker Newman 
970f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
971f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
972f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
973f7ce0706SParker Newman 
974f7ce0706SParker Newman 	switch (port_type) {
975f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS422_485:
976f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_HW:
977f7ce0706SParker Newman 		port->port.rs485_config = cti_rs485_config_mpio_tristate;
978f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
979f7ce0706SParker Newman 		break;
980f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_SW:
981f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_4B:
982f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_2B:
983f7ce0706SParker Newman 		port->port.rs485_config = generic_rs485_config;
984f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
985f7ce0706SParker Newman 		break;
986f7ce0706SParker Newman 	default:
987f7ce0706SParker Newman 		break;
988f7ce0706SParker Newman 	}
989f7ce0706SParker Newman 
990f7ce0706SParker Newman 	ret = cti_port_setup_common(priv, pcidev, idx, offset, port);
991f7ce0706SParker Newman 	if (ret)
992f7ce0706SParker Newman 		return ret;
993f7ce0706SParker Newman 
994f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_8XMODE), 0x00);
995f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_FCTR), UART_FCTR_EXAR_TRGD);
996f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_TXTRG), 128);
997f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_RXTRG), 128);
998f7ce0706SParker Newman 
999f7ce0706SParker Newman 	return 0;
1000f7ce0706SParker Newman }
1001f7ce0706SParker Newman 
1002f7ce0706SParker Newman static int cti_port_setup_xr17v25x(struct exar8250 *priv,
1003f7ce0706SParker Newman 				struct pci_dev *pcidev,
1004f7ce0706SParker Newman 				struct uart_8250_port *port,
1005f7ce0706SParker Newman 				int idx)
1006f7ce0706SParker Newman {
1007f7ce0706SParker Newman 	enum cti_port_type port_type;
1008f7ce0706SParker Newman 	unsigned int offset;
1009f7ce0706SParker Newman 	int ret;
1010f7ce0706SParker Newman 
1011f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17c15x_xr17v25x(priv, pcidev, idx);
1012f7ce0706SParker Newman 
1013f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17V25X_PORT_OFFSET;
1014f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
1015f7ce0706SParker Newman 
1016f7ce0706SParker Newman 	// XR17V25X supports fractional baudrates
1017f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
1018f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
1019f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
1020f7ce0706SParker Newman 
1021f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
1022f7ce0706SParker Newman 		switch (pcidev->subsystem_device) {
1023f7ce0706SParker Newman 		// These cards support power on 485 tri-state via MPIO
1024f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
1025f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
1026f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
1027f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
1028f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
1029f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
1030f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
1031f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
1032f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
1033f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
1034f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
1035f7ce0706SParker Newman 			port->port.rs485_config = cti_rs485_config_mpio_tristate;
1036f7ce0706SParker Newman 			break;
1037f7ce0706SParker Newman 		// Otherwise auto or no power on 485 tri-state support
1038f7ce0706SParker Newman 		default:
1039f7ce0706SParker Newman 			port->port.rs485_config = generic_rs485_config;
1040f7ce0706SParker Newman 			break;
1041f7ce0706SParker Newman 		}
1042f7ce0706SParker Newman 
1043f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
1044f7ce0706SParker Newman 	}
1045f7ce0706SParker Newman 
1046f7ce0706SParker Newman 	ret = cti_port_setup_common(priv, pcidev, idx, offset, port);
1047f7ce0706SParker Newman 	if (ret)
1048f7ce0706SParker Newman 		return ret;
1049f7ce0706SParker Newman 
1050f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_8XMODE), 0x00);
1051f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_FCTR), UART_FCTR_EXAR_TRGD);
1052f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_TXTRG), 32);
1053f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_RXTRG), 32);
1054f7ce0706SParker Newman 
1055f7ce0706SParker Newman 	return 0;
1056f7ce0706SParker Newman }
1057f7ce0706SParker Newman 
1058f7ce0706SParker Newman static int cti_port_setup_xr17c15x(struct exar8250 *priv,
1059f7ce0706SParker Newman 				struct pci_dev *pcidev,
1060f7ce0706SParker Newman 				struct uart_8250_port *port,
1061f7ce0706SParker Newman 				int idx)
1062f7ce0706SParker Newman {
1063f7ce0706SParker Newman 	enum cti_port_type port_type;
1064f7ce0706SParker Newman 	unsigned int offset;
1065f7ce0706SParker Newman 
1066f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17c15x_xr17v25x(priv, pcidev, idx);
1067f7ce0706SParker Newman 
1068f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17C15X_PORT_OFFSET;
1069f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
1070f7ce0706SParker Newman 
1071f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
1072f7ce0706SParker Newman 		switch (pcidev->subsystem_device) {
1073f7ce0706SParker Newman 		// These cards support power on 485 tri-state via MPIO
1074f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
1075f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
1076f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
1077f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
1078f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
1079f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
1080f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
1081f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
1082f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
1083f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
1084f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
1085f7ce0706SParker Newman 			port->port.rs485_config = cti_rs485_config_mpio_tristate;
1086f7ce0706SParker Newman 			break;
1087f7ce0706SParker Newman 		// Otherwise auto or no power on 485 tri-state support
1088f7ce0706SParker Newman 		default:
1089f7ce0706SParker Newman 			port->port.rs485_config = generic_rs485_config;
1090f7ce0706SParker Newman 			break;
1091f7ce0706SParker Newman 		}
1092f7ce0706SParker Newman 
1093f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
1094f7ce0706SParker Newman 	}
1095f7ce0706SParker Newman 
1096f7ce0706SParker Newman 	return cti_port_setup_common(priv, pcidev, idx, offset, port);
1097f7ce0706SParker Newman }
1098f7ce0706SParker Newman 
1099f7ce0706SParker Newman static int cti_board_init_xr17v35x(struct exar8250 *priv,
1100f7ce0706SParker Newman 				struct pci_dev *pcidev)
1101f7ce0706SParker Newman {
1102f7ce0706SParker Newman 	// XR17V35X uses the PCIe clock rather than an oscillator
1103f7ce0706SParker Newman 	priv->osc_freq = CTI_DEFAULT_PCIE_OSC_FREQ;
1104f7ce0706SParker Newman 
1105f7ce0706SParker Newman 	return 0;
1106f7ce0706SParker Newman }
1107f7ce0706SParker Newman 
1108*c5f59747SAndy Shevchenko static int cti_board_init_xr17v25x(struct exar8250 *priv, struct pci_dev *pcidev)
1109f7ce0706SParker Newman {
1110*c5f59747SAndy Shevchenko 	cti_board_init_osc_freq(priv, pcidev, CTI_EE_OFF_XR17V25X_OSC_FREQ);
1111f7ce0706SParker Newman 
11121cf8520aSAndy Shevchenko 	/* enable interrupts on cards that need the "PLX fix" */
1113f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
1114f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS:
1115f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_A:
1116f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_B:
1117f7ce0706SParker Newman 		cti_plx_int_enable(priv);
1118f7ce0706SParker Newman 		break;
1119f7ce0706SParker Newman 	default:
1120f7ce0706SParker Newman 		break;
1121f7ce0706SParker Newman 	}
1122f7ce0706SParker Newman 
1123f7ce0706SParker Newman 	return 0;
1124f7ce0706SParker Newman }
1125f7ce0706SParker Newman 
1126*c5f59747SAndy Shevchenko static int cti_board_init_xr17c15x(struct exar8250 *priv, struct pci_dev *pcidev)
1127f7ce0706SParker Newman {
1128*c5f59747SAndy Shevchenko 	cti_board_init_osc_freq(priv, pcidev, CTI_EE_OFF_XR17C15X_OSC_FREQ);
1129f7ce0706SParker Newman 
1130f7ce0706SParker Newman 	/* enable interrupts on cards that need the "PLX fix" */
1131f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
1132f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS:
1133f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_A:
1134f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_B:
1135f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS_OPTO:
1136f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_A:
1137f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_B:
1138f7ce0706SParker Newman 		cti_plx_int_enable(priv);
1139f7ce0706SParker Newman 		break;
1140f7ce0706SParker Newman 	default:
1141f7ce0706SParker Newman 		break;
1142f7ce0706SParker Newman 	}
1143f7ce0706SParker Newman 
1144f7ce0706SParker Newman 	return 0;
1145f7ce0706SParker Newman }
1146f7ce0706SParker Newman 
1147f7ce0706SParker Newman static int cti_board_init_fpga(struct exar8250 *priv, struct pci_dev *pcidev)
1148f7ce0706SParker Newman {
1149f7ce0706SParker Newman 	int ret;
1150f7ce0706SParker Newman 	u16 cfg_val;
1151f7ce0706SParker Newman 
1152f7ce0706SParker Newman 	// FPGA OSC is fixed to the 33MHz PCI clock
1153f7ce0706SParker Newman 	priv->osc_freq = CTI_DEFAULT_FPGA_OSC_FREQ;
1154f7ce0706SParker Newman 
1155f7ce0706SParker Newman 	// Enable external interrupts in special cfg space register
1156f7ce0706SParker Newman 	ret = pci_read_config_word(pcidev, CTI_FPGA_CFG_INT_EN_REG, &cfg_val);
1157f7ce0706SParker Newman 	if (ret)
115853ea31bbSAndy Shevchenko 		return pcibios_err_to_errno(ret);
1159f7ce0706SParker Newman 
1160f7ce0706SParker Newman 	cfg_val |= CTI_FPGA_CFG_INT_EN_EXT_BIT;
1161f7ce0706SParker Newman 	ret = pci_write_config_word(pcidev, CTI_FPGA_CFG_INT_EN_REG, cfg_val);
1162f7ce0706SParker Newman 	if (ret)
116353ea31bbSAndy Shevchenko 		return pcibios_err_to_errno(ret);
1164f7ce0706SParker Newman 
1165f7ce0706SParker Newman 	// RS485 gate needs to be enabled; otherwise RTS/CTS will not work
1166f7ce0706SParker Newman 	exar_write_reg(priv, CTI_FPGA_RS485_IO_REG, 0x01);
1167f7ce0706SParker Newman 
1168f7ce0706SParker Newman 	return 0;
1169f7ce0706SParker Newman }
1170f7ce0706SParker Newman 
1171fc6cc961SJan Kiszka static int
1172d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
1173d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
1174d0aeaa83SSudip Mukherjee {
1175d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
1176d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
1177d0aeaa83SSudip Mukherjee 
1178d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
1179d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
1180d0aeaa83SSudip Mukherjee }
1181d0aeaa83SSudip Mukherjee 
1182bea8be65SJan Kiszka static void setup_gpio(struct pci_dev *pcidev, u8 __iomem *p)
1183d0aeaa83SSudip Mukherjee {
1184bea8be65SJan Kiszka 	/*
1185bea8be65SJan Kiszka 	 * The Commtech adapters required the MPIOs to be driven low. The Exar
1186bea8be65SJan Kiszka 	 * devices will export them as GPIOs, so we pre-configure them safely
1187bea8be65SJan Kiszka 	 * as inputs.
1188bea8be65SJan Kiszka 	 */
11895fdbe136SMatthew Howell 
11905fdbe136SMatthew Howell 	u8 dir = 0x00;
11915fdbe136SMatthew Howell 
11925fdbe136SMatthew Howell 	if  ((pcidev->vendor == PCI_VENDOR_ID_EXAR) &&
11935fdbe136SMatthew Howell 		(pcidev->subsystem_vendor != PCI_VENDOR_ID_SEALEVEL)) {
11945fdbe136SMatthew Howell 		// Configure GPIO as inputs for Commtech adapters
11955fdbe136SMatthew Howell 		dir = 0xff;
11965fdbe136SMatthew Howell 	} else {
11975fdbe136SMatthew Howell 		// Configure GPIO as outputs for SeaLevel adapters
11985fdbe136SMatthew Howell 		dir = 0x00;
11995fdbe136SMatthew Howell 	}
1200bea8be65SJan Kiszka 
1201d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
1202d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
1203d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
1204d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
1205bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_7_0);
1206d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
1207d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
1208d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
1209d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
1210d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
1211bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_15_8);
1212d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
1213d0aeaa83SSudip Mukherjee }
1214d0aeaa83SSudip Mukherjee 
121533969db7SAndy Shevchenko static struct platform_device *__xr17v35x_register_gpio(struct pci_dev *pcidev,
121681171e7dSHeikki Krogerus 							const struct software_node *node)
1217d0aeaa83SSudip Mukherjee {
1218d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
1219d0aeaa83SSudip Mukherjee 
1220d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
1221d0aeaa83SSudip Mukherjee 	if (!pdev)
1222d0aeaa83SSudip Mukherjee 		return NULL;
1223d0aeaa83SSudip Mukherjee 
1224d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
122573f76db8SAndy Shevchenko 	device_set_node(&pdev->dev, dev_fwnode(&pcidev->dev));
1226d3936d74SJan Kiszka 
122781171e7dSHeikki Krogerus 	if (device_add_software_node(&pdev->dev, node) < 0 ||
1228380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
1229d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
1230d0aeaa83SSudip Mukherjee 		return NULL;
1231d0aeaa83SSudip Mukherjee 	}
1232d0aeaa83SSudip Mukherjee 
1233d0aeaa83SSudip Mukherjee 	return pdev;
1234d0aeaa83SSudip Mukherjee }
1235d0aeaa83SSudip Mukherjee 
123633969db7SAndy Shevchenko static void __xr17v35x_unregister_gpio(struct platform_device *pdev)
123733969db7SAndy Shevchenko {
123833969db7SAndy Shevchenko 	device_remove_software_node(&pdev->dev);
123933969db7SAndy Shevchenko 	platform_device_unregister(pdev);
124033969db7SAndy Shevchenko }
124133969db7SAndy Shevchenko 
1242380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
1243a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 0),
1244380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
1245380b1e2fSJan Kiszka 	{ }
1246380b1e2fSJan Kiszka };
1247380b1e2fSJan Kiszka 
124881171e7dSHeikki Krogerus static const struct software_node exar_gpio_node = {
124981171e7dSHeikki Krogerus 	.properties = exar_gpio_properties,
125081171e7dSHeikki Krogerus };
125181171e7dSHeikki Krogerus 
125233969db7SAndy Shevchenko static int xr17v35x_register_gpio(struct pci_dev *pcidev, struct uart_8250_port *port)
12530d963ebfSJan Kiszka {
12540d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
12550d963ebfSJan Kiszka 		port->port.private_data =
125681171e7dSHeikki Krogerus 			__xr17v35x_register_gpio(pcidev, &exar_gpio_node);
12570d963ebfSJan Kiszka 
12580d963ebfSJan Kiszka 	return 0;
12590d963ebfSJan Kiszka }
12600d963ebfSJan Kiszka 
126133969db7SAndy Shevchenko static void xr17v35x_unregister_gpio(struct uart_8250_port *port)
126233969db7SAndy Shevchenko {
126333969db7SAndy Shevchenko 	if (!port->port.private_data)
126433969db7SAndy Shevchenko 		return;
126533969db7SAndy Shevchenko 
126633969db7SAndy Shevchenko 	__xr17v35x_unregister_gpio(port->port.private_data);
126733969db7SAndy Shevchenko 	port->port.private_data = NULL;
126833969db7SAndy Shevchenko }
126933969db7SAndy Shevchenko 
1270687911b3SMatthew Howell static int sealevel_rs485_config(struct uart_port *port, struct ktermios *termios,
1271687911b3SMatthew Howell 				  struct serial_rs485 *rs485)
1272687911b3SMatthew Howell {
1273687911b3SMatthew Howell 	u8 __iomem *p = port->membase;
1274687911b3SMatthew Howell 	u8 old_lcr;
1275687911b3SMatthew Howell 	u8 efr;
1276687911b3SMatthew Howell 	u8 dld;
1277687911b3SMatthew Howell 	int ret;
1278687911b3SMatthew Howell 
1279687911b3SMatthew Howell 	ret = generic_rs485_config(port, termios, rs485);
1280687911b3SMatthew Howell 	if (ret)
1281687911b3SMatthew Howell 		return ret;
1282687911b3SMatthew Howell 
1283687911b3SMatthew Howell 	if (rs485->flags & SER_RS485_ENABLED) {
1284687911b3SMatthew Howell 		old_lcr = readb(p + UART_LCR);
1285687911b3SMatthew Howell 
1286687911b3SMatthew Howell 		/* Set EFR[4]=1 to enable enhanced feature registers */
1287687911b3SMatthew Howell 		efr = readb(p + UART_XR_EFR);
1288687911b3SMatthew Howell 		efr |= UART_EFR_ECB;
1289687911b3SMatthew Howell 		writeb(efr, p + UART_XR_EFR);
1290687911b3SMatthew Howell 
1291687911b3SMatthew Howell 		/* Set MCR to use DTR as Auto-RS485 Enable signal */
1292687911b3SMatthew Howell 		writeb(UART_MCR_OUT1, p + UART_MCR);
1293687911b3SMatthew Howell 
1294687911b3SMatthew Howell 		/* Set LCR[7]=1 to enable access to DLD register */
1295687911b3SMatthew Howell 		writeb(old_lcr | UART_LCR_DLAB, p + UART_LCR);
1296687911b3SMatthew Howell 
1297687911b3SMatthew Howell 		/* Set DLD[7]=1 for inverted RS485 Enable logic */
1298687911b3SMatthew Howell 		dld = readb(p + UART_EXAR_DLD);
1299687911b3SMatthew Howell 		dld |= UART_EXAR_DLD_485_POLARITY;
1300687911b3SMatthew Howell 		writeb(dld, p + UART_EXAR_DLD);
1301687911b3SMatthew Howell 
1302687911b3SMatthew Howell 		writeb(old_lcr, p + UART_LCR);
1303687911b3SMatthew Howell 	}
1304687911b3SMatthew Howell 
1305687911b3SMatthew Howell 	return 0;
1306687911b3SMatthew Howell }
1307687911b3SMatthew Howell 
13080d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
13090d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
131033969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
13119d939894SDaniel Golle 	.rs485_config = generic_rs485_config,
131259c221f8SIlpo Järvinen 	.rs485_supported = &generic_rs485_supported,
13130d963ebfSJan Kiszka };
13140d963ebfSJan Kiszka 
1315ae50bb27SIlpo Järvinen static int iot2040_rs485_config(struct uart_port *port, struct ktermios *termios,
1316413058dfSJan Kiszka 				struct serial_rs485 *rs485)
1317413058dfSJan Kiszka {
1318413058dfSJan Kiszka 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
1319413058dfSJan Kiszka 	u8 __iomem *p = port->membase;
1320413058dfSJan Kiszka 	u8 mask = IOT2040_UART1_MASK;
1321413058dfSJan Kiszka 	u8 mode, value;
1322413058dfSJan Kiszka 
1323413058dfSJan Kiszka 	if (is_rs485) {
1324413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_RX_DURING_TX)
1325413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS422;
1326413058dfSJan Kiszka 		else
1327413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS485;
1328413058dfSJan Kiszka 
1329413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_TERMINATE_BUS)
1330413058dfSJan Kiszka 			mode |= IOT2040_UART_TERMINATE_BUS;
1331413058dfSJan Kiszka 	} else {
1332413058dfSJan Kiszka 		mode = IOT2040_UART_MODE_RS232;
1333413058dfSJan Kiszka 	}
1334413058dfSJan Kiszka 
1335413058dfSJan Kiszka 	if (port->line == 3) {
1336413058dfSJan Kiszka 		mask <<= IOT2040_UART2_SHIFT;
1337413058dfSJan Kiszka 		mode <<= IOT2040_UART2_SHIFT;
1338413058dfSJan Kiszka 	}
1339413058dfSJan Kiszka 
1340413058dfSJan Kiszka 	value = readb(p + UART_EXAR_MPIOLVL_7_0);
1341413058dfSJan Kiszka 	value &= ~mask;
1342413058dfSJan Kiszka 	value |= mode;
1343413058dfSJan Kiszka 	writeb(value, p + UART_EXAR_MPIOLVL_7_0);
1344413058dfSJan Kiszka 
1345ae50bb27SIlpo Järvinen 	return generic_rs485_config(port, termios, rs485);
1346413058dfSJan Kiszka }
1347413058dfSJan Kiszka 
134859c221f8SIlpo Järvinen static const struct serial_rs485 iot2040_rs485_supported = {
13490c2a5f47SLino Sanfilippo 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
13500c2a5f47SLino Sanfilippo 		 SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
135159c221f8SIlpo Järvinen };
135259c221f8SIlpo Järvinen 
1353413058dfSJan Kiszka static const struct property_entry iot2040_gpio_properties[] = {
1354a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 10),
1355413058dfSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 1),
1356413058dfSJan Kiszka 	{ }
1357413058dfSJan Kiszka };
1358413058dfSJan Kiszka 
135981171e7dSHeikki Krogerus static const struct software_node iot2040_gpio_node = {
136081171e7dSHeikki Krogerus 	.properties = iot2040_gpio_properties,
136181171e7dSHeikki Krogerus };
136281171e7dSHeikki Krogerus 
1363413058dfSJan Kiszka static int iot2040_register_gpio(struct pci_dev *pcidev,
1364413058dfSJan Kiszka 			      struct uart_8250_port *port)
1365413058dfSJan Kiszka {
1366413058dfSJan Kiszka 	u8 __iomem *p = port->port.membase;
1367413058dfSJan Kiszka 
1368413058dfSJan Kiszka 	writeb(IOT2040_UARTS_DEFAULT_MODE, p + UART_EXAR_MPIOLVL_7_0);
1369413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_LO_MODE, p + UART_EXAR_MPIOSEL_7_0);
1370413058dfSJan Kiszka 	writeb(IOT2040_UARTS_ENABLE, p + UART_EXAR_MPIOLVL_15_8);
1371413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_HI_MODE, p + UART_EXAR_MPIOSEL_15_8);
1372413058dfSJan Kiszka 
1373413058dfSJan Kiszka 	port->port.private_data =
137481171e7dSHeikki Krogerus 		__xr17v35x_register_gpio(pcidev, &iot2040_gpio_node);
1375413058dfSJan Kiszka 
1376413058dfSJan Kiszka 	return 0;
1377413058dfSJan Kiszka }
1378413058dfSJan Kiszka 
1379413058dfSJan Kiszka static const struct exar8250_platform iot2040_platform = {
1380413058dfSJan Kiszka 	.rs485_config = iot2040_rs485_config,
138159c221f8SIlpo Järvinen 	.rs485_supported = &iot2040_rs485_supported,
1382413058dfSJan Kiszka 	.register_gpio = iot2040_register_gpio,
138333969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
1384413058dfSJan Kiszka };
1385413058dfSJan Kiszka 
13863e51ceeaSSu Bao Cheng /*
13873e51ceeaSSu Bao Cheng  * For SIMATIC IOT2000, only IOT2040 and its variants have the Exar device,
13883e51ceeaSSu Bao Cheng  * IOT2020 doesn't have. Therefore it is sufficient to match on the common
13893e51ceeaSSu Bao Cheng  * board name after the device was found.
13903e51ceeaSSu Bao Cheng  */
1391413058dfSJan Kiszka static const struct dmi_system_id exar_platforms[] = {
1392413058dfSJan Kiszka 	{
1393413058dfSJan Kiszka 		.matches = {
1394413058dfSJan Kiszka 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
1395413058dfSJan Kiszka 		},
1396413058dfSJan Kiszka 		.driver_data = (void *)&iot2040_platform,
1397413058dfSJan Kiszka 	},
1398413058dfSJan Kiszka 	{}
1399413058dfSJan Kiszka };
1400413058dfSJan Kiszka 
14017d356a43SAndy Shevchenko static const struct exar8250_platform *exar_get_platform(void)
14027d356a43SAndy Shevchenko {
14037d356a43SAndy Shevchenko 	const struct dmi_system_id *dmi_match;
14047d356a43SAndy Shevchenko 
14057d356a43SAndy Shevchenko 	dmi_match = dmi_first_match(exar_platforms);
14067d356a43SAndy Shevchenko 	if (dmi_match)
14077d356a43SAndy Shevchenko 		return dmi_match->driver_data;
14087d356a43SAndy Shevchenko 
14097d356a43SAndy Shevchenko 	return &exar8250_default_platform;
14107d356a43SAndy Shevchenko }
14117d356a43SAndy Shevchenko 
1412d0aeaa83SSudip Mukherjee static int
1413d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
1414d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
1415d0aeaa83SSudip Mukherjee {
14167d356a43SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
1417d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
1418d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
1419d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
1420d0aeaa83SSudip Mukherjee 	int ret;
1421d0aeaa83SSudip Mukherjee 
1422d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
14230d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
14240139da50SIlpo Järvinen 	port->port.rs485_supported = *(platform->rs485_supported);
14250d963ebfSJan Kiszka 
1426687911b3SMatthew Howell 	if (pcidev->subsystem_vendor == PCI_VENDOR_ID_SEALEVEL)
1427687911b3SMatthew Howell 		port->port.rs485_config = sealevel_rs485_config;
1428687911b3SMatthew Howell 
1429d0aeaa83SSudip Mukherjee 	/*
1430328c11f2SAndy Shevchenko 	 * Setup the UART clock for the devices on expansion slot to
1431d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
1432d0aeaa83SSudip Mukherjee 	 */
1433328c11f2SAndy Shevchenko 	if (idx >= 8)
1434d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
1435d0aeaa83SSudip Mukherjee 
14365b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
14375b5f252dSJan Kiszka 	if (ret)
14385b5f252dSJan Kiszka 		return ret;
1439d0aeaa83SSudip Mukherjee 
14405b5f252dSJan Kiszka 	p = port->port.membase;
1441d0aeaa83SSudip Mukherjee 
1442d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
1443d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
1444d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
1445d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
1446d0aeaa83SSudip Mukherjee 
14475b5f252dSJan Kiszka 	if (idx == 0) {
14485b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
1449bea8be65SJan Kiszka 		setup_gpio(pcidev, p);
1450d0aeaa83SSudip Mukherjee 
14510d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
14525b5f252dSJan Kiszka 	}
1453d0aeaa83SSudip Mukherjee 
14540d963ebfSJan Kiszka 	return ret;
1455d0aeaa83SSudip Mukherjee }
1456d0aeaa83SSudip Mukherjee 
1457d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
1458d0aeaa83SSudip Mukherjee {
145933969db7SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
1460d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
1461d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
14627c3e8d9dSAndy Shevchenko 
146333969db7SAndy Shevchenko 	platform->unregister_gpio(port);
1464d0aeaa83SSudip Mukherjee }
1465d0aeaa83SSudip Mukherjee 
146672169e42SAaron Sierra static inline void exar_misc_clear(struct exar8250 *priv)
146772169e42SAaron Sierra {
146872169e42SAaron Sierra 	/* Clear all PCI interrupts by reading INT0. No effect on IIR */
146972169e42SAaron Sierra 	readb(priv->virt + UART_EXAR_INT0);
147072169e42SAaron Sierra 
147172169e42SAaron Sierra 	/* Clear INT0 for Expansion Interface slave ports, too */
147272169e42SAaron Sierra 	if (priv->board->num_ports > 8)
147372169e42SAaron Sierra 		readb(priv->virt + 0x2000 + UART_EXAR_INT0);
147472169e42SAaron Sierra }
147572169e42SAaron Sierra 
1476c7e1b405SAaron Sierra /*
1477c7e1b405SAaron Sierra  * These Exar UARTs have an extra interrupt indicator that could fire for a
1478c7e1b405SAaron Sierra  * few interrupts that are not presented/cleared through IIR.  One of which is
1479c7e1b405SAaron Sierra  * a wakeup interrupt when coming out of sleep.  These interrupts are only
1480c7e1b405SAaron Sierra  * cleared by reading global INT0 or INT1 registers as interrupts are
1481c7e1b405SAaron Sierra  * associated with channel 0. The INT[3:0] registers _are_ accessible from each
1482c7e1b405SAaron Sierra  * channel's address space, but for the sake of bus efficiency we register a
1483c7e1b405SAaron Sierra  * dedicated handler at the PCI device level to handle them.
1484c7e1b405SAaron Sierra  */
1485c7e1b405SAaron Sierra static irqreturn_t exar_misc_handler(int irq, void *data)
1486c7e1b405SAaron Sierra {
148772169e42SAaron Sierra 	exar_misc_clear(data);
1488c7e1b405SAaron Sierra 
1489c7e1b405SAaron Sierra 	return IRQ_HANDLED;
1490c7e1b405SAaron Sierra }
1491c7e1b405SAaron Sierra 
1492477f6ee6SParker Newman static unsigned int exar_get_nr_ports(struct exar8250_board *board,
1493477f6ee6SParker Newman 					struct pci_dev *pcidev)
1494477f6ee6SParker Newman {
1495477f6ee6SParker Newman 	unsigned int nr_ports = 0;
1496477f6ee6SParker Newman 
14975aa84fd8SParker Newman 	if (pcidev->vendor == PCI_VENDOR_ID_ACCESSIO) {
1498477f6ee6SParker Newman 		nr_ports = BIT(((pcidev->device & 0x38) >> 3) - 1);
14995aa84fd8SParker Newman 	} else if (board->num_ports > 0) {
15005aa84fd8SParker Newman 		// Check if board struct overrides number of ports
1501477f6ee6SParker Newman 		nr_ports = board->num_ports;
15025aa84fd8SParker Newman 	} else if (pcidev->vendor == PCI_VENDOR_ID_EXAR) {
15035aa84fd8SParker Newman 		// Exar encodes # ports in last nibble of PCI Device ID ex. 0358
1504477f6ee6SParker Newman 		nr_ports = pcidev->device & 0x0f;
15055aa84fd8SParker Newman 	} else  if (pcidev->vendor == PCI_VENDOR_ID_CONNECT_TECH) {
15065aa84fd8SParker Newman 		// Handle CTI FPGA cards
15075aa84fd8SParker Newman 		switch (pcidev->device) {
15085aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG00X:
15095aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG01X:
15105aa84fd8SParker Newman 			nr_ports = 12;
15115aa84fd8SParker Newman 			break;
15125aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_16:
15135aa84fd8SParker Newman 			nr_ports = 16;
15145aa84fd8SParker Newman 			break;
15155aa84fd8SParker Newman 		default:
15165aa84fd8SParker Newman 			break;
15175aa84fd8SParker Newman 		}
15185aa84fd8SParker Newman 	}
1519477f6ee6SParker Newman 
1520477f6ee6SParker Newman 	return nr_ports;
1521477f6ee6SParker Newman }
1522477f6ee6SParker Newman 
1523d0aeaa83SSudip Mukherjee static int
1524d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
1525d0aeaa83SSudip Mukherjee {
1526d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
1527d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
1528d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
1529d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
1530d0aeaa83SSudip Mukherjee 	int rc;
1531d0aeaa83SSudip Mukherjee 
1532d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
1533d0aeaa83SSudip Mukherjee 	if (!board)
1534d0aeaa83SSudip Mukherjee 		return -EINVAL;
1535d0aeaa83SSudip Mukherjee 
1536d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
1537d0aeaa83SSudip Mukherjee 	if (rc)
1538d0aeaa83SSudip Mukherjee 		return rc;
1539d0aeaa83SSudip Mukherjee 
1540d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
1541d0aeaa83SSudip Mukherjee 
1542477f6ee6SParker Newman 	nr_ports = exar_get_nr_ports(board, pcidev);
1543477f6ee6SParker Newman 	if (nr_ports == 0) {
1544477f6ee6SParker Newman 		dev_err_probe(&pcidev->dev, -ENODEV,
1545477f6ee6SParker Newman 				"failed to get number of ports\n");
1546477f6ee6SParker Newman 		return -ENODEV;
1547477f6ee6SParker Newman 	}
1548d0aeaa83SSudip Mukherjee 
1549df60a8afSAndy Shevchenko 	priv = devm_kzalloc(&pcidev->dev, struct_size(priv, line, nr_ports), GFP_KERNEL);
1550d0aeaa83SSudip Mukherjee 	if (!priv)
1551d0aeaa83SSudip Mukherjee 		return -ENOMEM;
1552d0aeaa83SSudip Mukherjee 
1553d0aeaa83SSudip Mukherjee 	priv->board = board;
1554c7e1b405SAaron Sierra 	priv->virt = pcim_iomap(pcidev, bar, 0);
1555c7e1b405SAaron Sierra 	if (!priv->virt)
1556c7e1b405SAaron Sierra 		return -ENOMEM;
1557d0aeaa83SSudip Mukherjee 
1558172c33cbSJan Kiszka 	pci_set_master(pcidev);
1559172c33cbSJan Kiszka 
1560172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
1561172c33cbSJan Kiszka 	if (rc < 0)
1562172c33cbSJan Kiszka 		return rc;
1563172c33cbSJan Kiszka 
1564d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
15656be254c2SAndy Shevchenko 	uart.port.flags = UPF_SHARE_IRQ | UPF_EXAR_EFR | UPF_FIXED_TYPE | UPF_FIXED_PORT;
1566172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
1567d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
1568d0aeaa83SSudip Mukherjee 
15695bc430afSAndy Shevchenko 	/* Clear interrupts */
15705bc430afSAndy Shevchenko 	exar_misc_clear(priv);
15715bc430afSAndy Shevchenko 
1572c7e1b405SAaron Sierra 	rc = devm_request_irq(&pcidev->dev, uart.port.irq, exar_misc_handler,
1573c7e1b405SAaron Sierra 			 IRQF_SHARED, "exar_uart", priv);
1574c7e1b405SAaron Sierra 	if (rc)
1575c7e1b405SAaron Sierra 		return rc;
1576c7e1b405SAaron Sierra 
1577393b520aSParker Newman 	if (board->board_init) {
1578393b520aSParker Newman 		rc = board->board_init(priv, pcidev);
1579393b520aSParker Newman 		if (rc) {
1580393b520aSParker Newman 			dev_err_probe(&pcidev->dev, rc,
1581393b520aSParker Newman 					"failed to init serial board\n");
1582393b520aSParker Newman 			return rc;
1583393b520aSParker Newman 		}
1584393b520aSParker Newman 	}
1585393b520aSParker Newman 
1586d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
1587d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
1588d0aeaa83SSudip Mukherjee 		if (rc) {
1589d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
1590d0aeaa83SSudip Mukherjee 			break;
1591d0aeaa83SSudip Mukherjee 		}
1592d0aeaa83SSudip Mukherjee 
1593d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
1594d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
1595d0aeaa83SSudip Mukherjee 
1596d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
1597d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
1598d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
1599d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
1600d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
1601d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
1602d0aeaa83SSudip Mukherjee 			break;
1603d0aeaa83SSudip Mukherjee 		}
1604d0aeaa83SSudip Mukherjee 	}
1605d0aeaa83SSudip Mukherjee 	priv->nr = i;
1606d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
1607d0aeaa83SSudip Mukherjee 	return 0;
1608d0aeaa83SSudip Mukherjee }
1609d0aeaa83SSudip Mukherjee 
1610d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
1611d0aeaa83SSudip Mukherjee {
1612d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
1613d0aeaa83SSudip Mukherjee 	unsigned int i;
1614d0aeaa83SSudip Mukherjee 
1615d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1616d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
1617d0aeaa83SSudip Mukherjee 
161873b5a5c0SAndy Shevchenko 	/* Ensure that every init quirk is properly torn down */
1619d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
1620d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
1621d0aeaa83SSudip Mukherjee }
1622d0aeaa83SSudip Mukherjee 
162382f9cefaSAndy Shevchenko static int exar_suspend(struct device *dev)
1624d0aeaa83SSudip Mukherjee {
16257a345dc1SAndy Shevchenko 	struct exar8250 *priv = dev_get_drvdata(dev);
1626d0aeaa83SSudip Mukherjee 	unsigned int i;
1627d0aeaa83SSudip Mukherjee 
1628d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1629d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
1630d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
1631d0aeaa83SSudip Mukherjee 
1632d0aeaa83SSudip Mukherjee 	return 0;
1633d0aeaa83SSudip Mukherjee }
1634d0aeaa83SSudip Mukherjee 
163582f9cefaSAndy Shevchenko static int exar_resume(struct device *dev)
1636d0aeaa83SSudip Mukherjee {
163776b4106cSChuhong Yuan 	struct exar8250 *priv = dev_get_drvdata(dev);
1638d0aeaa83SSudip Mukherjee 	unsigned int i;
1639d0aeaa83SSudip Mukherjee 
164072169e42SAaron Sierra 	exar_misc_clear(priv);
164172169e42SAaron Sierra 
1642d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1643d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
1644d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
1645d0aeaa83SSudip Mukherjee 
1646d0aeaa83SSudip Mukherjee 	return 0;
1647d0aeaa83SSudip Mukherjee }
1648d0aeaa83SSudip Mukherjee 
164982f9cefaSAndy Shevchenko static DEFINE_SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
1650d0aeaa83SSudip Mukherjee 
1651fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
1652fc6cc961SJan Kiszka 	.num_ports	= 2,
1653fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1654fc6cc961SJan Kiszka };
1655fc6cc961SJan Kiszka 
1656fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
1657fc6cc961SJan Kiszka 	.num_ports	= 4,
1658fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1659fc6cc961SJan Kiszka };
1660fc6cc961SJan Kiszka 
1661fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
1662fc6cc961SJan Kiszka 	.num_ports	= 8,
1663fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1664fc6cc961SJan Kiszka };
1665fc6cc961SJan Kiszka 
1666f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17c15x = {
1667f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17c15x,
1668f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17c15x,
1669f7ce0706SParker Newman };
1670f7ce0706SParker Newman 
1671f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17v25x = {
1672f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17v25x,
1673f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17v25x,
1674f7ce0706SParker Newman };
1675f7ce0706SParker Newman 
1676f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17v35x = {
1677f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17v35x,
1678f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17v35x,
1679f7ce0706SParker Newman };
1680f7ce0706SParker Newman 
1681f7ce0706SParker Newman static const struct exar8250_board pbn_cti_fpga = {
1682f7ce0706SParker Newman 	.board_init	= cti_board_init_fpga,
1683f7ce0706SParker Newman 	.setup		= cti_port_setup_fpga,
1684f7ce0706SParker Newman };
1685f7ce0706SParker Newman 
1686d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
1687d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
1688d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
1689d0aeaa83SSudip Mukherjee };
1690d0aeaa83SSudip Mukherjee 
1691d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
1692d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
1693d0aeaa83SSudip Mukherjee };
1694d0aeaa83SSudip Mukherjee 
1695d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
1696d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1697d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1698d0aeaa83SSudip Mukherjee };
1699d0aeaa83SSudip Mukherjee 
1700c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_2 = {
1701c6b9e95dSValmer Huhn 	.num_ports	= 2,
1702c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1703c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1704c6b9e95dSValmer Huhn };
1705c6b9e95dSValmer Huhn 
1706c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_4 = {
1707c6b9e95dSValmer Huhn 	.num_ports	= 4,
1708c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1709c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1710c6b9e95dSValmer Huhn };
1711c6b9e95dSValmer Huhn 
1712c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_8 = {
1713c6b9e95dSValmer Huhn 	.num_ports	= 8,
1714c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1715c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1716c6b9e95dSValmer Huhn };
1717c6b9e95dSValmer Huhn 
1718d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
1719d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
1720d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1721d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1722d0aeaa83SSudip Mukherjee };
1723d0aeaa83SSudip Mukherjee 
1724d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
1725d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
1726d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1727d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1728d0aeaa83SSudip Mukherjee };
1729d0aeaa83SSudip Mukherjee 
1730f7ce0706SParker Newman #define CTI_EXAR_DEVICE(devid, bd) {                    \
1731f7ce0706SParker Newman 	PCI_DEVICE_SUB(                                 \
1732f7ce0706SParker Newman 		PCI_VENDOR_ID_EXAR,                     \
1733f7ce0706SParker Newman 		PCI_DEVICE_ID_EXAR_##devid,             \
1734f7ce0706SParker Newman 		PCI_SUBVENDOR_ID_CONNECT_TECH,          \
1735f7ce0706SParker Newman 		PCI_ANY_ID), 0, 0,                      \
1736f7ce0706SParker Newman 		(kernel_ulong_t)&bd                     \
1737f7ce0706SParker Newman 	}
1738f7ce0706SParker Newman 
173924637007SAndy Shevchenko #define EXAR_DEVICE(vend, devid, bd) { PCI_DEVICE_DATA(vend, devid, &bd) }
1740d0aeaa83SSudip Mukherjee 
1741d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
1742d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
1743d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
1744d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
1745a85f12adSAndy Shevchenko 		PCI_SUBVENDOR_ID_IBM,			\
1746d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
1747d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
1748d0aeaa83SSudip Mukherjee 	}
1749d0aeaa83SSudip Mukherjee 
175095d69886SAndrew Davis #define USR_DEVICE(devid, sdevid, bd) {			\
175195d69886SAndrew Davis 	PCI_DEVICE_SUB(					\
175295d69886SAndrew Davis 		PCI_VENDOR_ID_USR,			\
175395d69886SAndrew Davis 		PCI_DEVICE_ID_EXAR_##devid,		\
175495d69886SAndrew Davis 		PCI_VENDOR_ID_EXAR,			\
175595d69886SAndrew Davis 		PCI_SUBDEVICE_ID_USR_##sdevid), 0, 0,	\
175695d69886SAndrew Davis 		(kernel_ulong_t)&bd			\
175795d69886SAndrew Davis 	}
175895d69886SAndrew Davis 
17593637c460SArvind Yadav static const struct pci_device_id exar_pci_tbl[] = {
17608e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2S, pbn_exar_XR17C15x),
17618e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4S, pbn_exar_XR17C15x),
17628e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8S, pbn_exar_XR17C15x),
17638e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM232_8, pbn_exar_XR17C15x),
17648e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2SM, pbn_exar_XR17C15x),
17658e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4SM, pbn_exar_XR17C15x),
17668e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8SM, pbn_exar_XR17C15x),
176710c5ccc3SJay Dolan 
17688e9f8261SAndy Shevchenko 	/* Connect Tech cards with Exar vendor/device PCI IDs */
1769f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C152,       pbn_cti_xr17c15x),
1770f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C154,       pbn_cti_xr17c15x),
1771f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C158,       pbn_cti_xr17c15x),
1772f7ce0706SParker Newman 
1773f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V252,       pbn_cti_xr17v25x),
1774f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V254,       pbn_cti_xr17v25x),
1775f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V258,       pbn_cti_xr17v25x),
1776f7ce0706SParker Newman 
1777f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V352,       pbn_cti_xr17v35x),
1778f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V354,       pbn_cti_xr17v35x),
1779f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V358,       pbn_cti_xr17v35x),
1780f7ce0706SParker Newman 
17818e9f8261SAndy Shevchenko 	/* Connect Tech cards with Connect Tech vendor/device PCI IDs (FPGA based) */
17828e9f8261SAndy Shevchenko 	EXAR_DEVICE(CONNECT_TECH, PCI_XR79X_12_XIG00X, pbn_cti_fpga),
17838e9f8261SAndy Shevchenko 	EXAR_DEVICE(CONNECT_TECH, PCI_XR79X_12_XIG01X, pbn_cti_fpga),
17848e9f8261SAndy Shevchenko 	EXAR_DEVICE(CONNECT_TECH, PCI_XR79X_16,        pbn_cti_fpga),
1785f7ce0706SParker Newman 
1786d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
1787d0aeaa83SSudip Mukherjee 
178895d69886SAndrew Davis 	/* USRobotics USR298x-OEM PCI Modems */
178995d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2980, pbn_exar_XR17C15x),
179095d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2981, pbn_exar_XR17C15x),
179195d69886SAndrew Davis 
1792d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
179324637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C152, pbn_exar_XR17C15x),
179424637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C154, pbn_exar_XR17C15x),
179524637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C158, pbn_exar_XR17C15x),
1796d0aeaa83SSudip Mukherjee 
1797d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
179824637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V352, pbn_exar_XR17V35x),
179924637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V354, pbn_exar_XR17V35x),
180024637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V358, pbn_exar_XR17V35x),
180124637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V4358, pbn_exar_XR17V4358),
180224637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V8358, pbn_exar_XR17V8358),
1803c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4222PCIE, pbn_fastcom35x_2),
1804c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4224PCIE, pbn_fastcom35x_4),
1805c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4228PCIE, pbn_fastcom35x_8),
1806fc6cc961SJan Kiszka 
180724637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4222PCI335, pbn_fastcom335_2),
180824637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4224PCI335, pbn_fastcom335_4),
180924637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2324PCI335, pbn_fastcom335_4),
181024637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2328PCI335, pbn_fastcom335_8),
1811d0aeaa83SSudip Mukherjee 	{ 0, }
1812d0aeaa83SSudip Mukherjee };
1813d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
1814d0aeaa83SSudip Mukherjee 
1815d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
1816d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
1817d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
1818d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
1819d0aeaa83SSudip Mukherjee 	.driver         = {
182082f9cefaSAndy Shevchenko 		.pm     = pm_sleep_ptr(&exar_pci_pm),
1821d0aeaa83SSudip Mukherjee 	},
1822d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
1823d0aeaa83SSudip Mukherjee };
1824d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
1825d0aeaa83SSudip Mukherjee 
1826d813d900SAndy Shevchenko MODULE_IMPORT_NS(SERIAL_8250_PCI);
1827d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
18282b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
1829d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
1830