xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision f7ce07062988a26b83ca1448df8d1e4a9232d962)
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>
23*f7ce0706SParker 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 
132*f7ce0706SParker Newman /* EEPROM registers */
133*f7ce0706SParker Newman #define UART_EXAR_REGB			0x8e
134*f7ce0706SParker Newman #define UART_EXAR_REGB_EECK		BIT(4)
135*f7ce0706SParker Newman #define UART_EXAR_REGB_EECS		BIT(5)
136*f7ce0706SParker Newman #define UART_EXAR_REGB_EEDI		BIT(6)
137*f7ce0706SParker Newman #define UART_EXAR_REGB_EEDO		BIT(7)
138*f7ce0706SParker Newman #define UART_EXAR_REGB_EE_ADDR_SIZE	6
139*f7ce0706SParker Newman #define UART_EXAR_REGB_EE_DATA_SIZE	16
140*f7ce0706SParker Newman 
141*f7ce0706SParker Newman #define UART_EXAR_XR17C15X_PORT_OFFSET	0x200
142*f7ce0706SParker Newman #define UART_EXAR_XR17V25X_PORT_OFFSET	0x200
143*f7ce0706SParker Newman #define UART_EXAR_XR17V35X_PORT_OFFSET	0x400
144*f7ce0706SParker 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 
180*f7ce0706SParker Newman /* CTI EEPROM offsets */
181*f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_OSC_FREQ	0x04  /* 2 words */
182*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_OSC_FREQ	0x08  /* 2 words */
183*f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_PART_NUM	0x0A  /* 4 words */
184*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_PART_NUM	0x0E  /* 4 words */
185*f7ce0706SParker Newman #define CTI_EE_OFF_XR17C15X_SERIAL_NUM	0x0E  /* 1 word */
186*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V25X_SERIAL_NUM	0x12  /* 1 word */
187*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_SERIAL_NUM	0x11  /* 2 word */
188*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_BRD_FLAGS	0x13  /* 1 word */
189*f7ce0706SParker Newman #define CTI_EE_OFF_XR17V35X_PORT_FLAGS	0x14  /* 1 word */
190*f7ce0706SParker Newman 
191*f7ce0706SParker Newman #define CTI_EE_MASK_PORT_FLAGS_TYPE	GENMASK(7, 0)
192*f7ce0706SParker Newman #define CTI_EE_MASK_OSC_FREQ_LOWER	GENMASK(15, 0)
193*f7ce0706SParker Newman #define CTI_EE_MASK_OSC_FREQ_UPPER	GENMASK(31, 16)
194*f7ce0706SParker Newman 
195*f7ce0706SParker Newman #define CTI_FPGA_RS485_IO_REG		0x2008
196*f7ce0706SParker Newman #define CTI_FPGA_CFG_INT_EN_REG		0x48
197*f7ce0706SParker Newman #define CTI_FPGA_CFG_INT_EN_EXT_BIT	BIT(15) /* External int enable bit */
198*f7ce0706SParker Newman 
199*f7ce0706SParker Newman #define CTI_DEFAULT_PCI_OSC_FREQ	29491200
200*f7ce0706SParker Newman #define CTI_DEFAULT_PCIE_OSC_FREQ	125000000
201*f7ce0706SParker Newman #define CTI_DEFAULT_FPGA_OSC_FREQ	33333333
202*f7ce0706SParker Newman 
203*f7ce0706SParker Newman /*
204*f7ce0706SParker Newman  * CTI Serial port line types. These match the values stored in the first
205*f7ce0706SParker Newman  * nibble of the CTI EEPROM port_flags word.
206*f7ce0706SParker Newman  */
207*f7ce0706SParker Newman enum cti_port_type {
208*f7ce0706SParker Newman 	CTI_PORT_TYPE_NONE = 0,
209*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232,            // RS232 ONLY
210*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS422_485,        // RS422/RS485 ONLY
211*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_HW, // RS232/422/485 HW ONLY Switchable
212*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_SW, // RS232/422/485 SW ONLY Switchable
213*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_4B, // RS232/422/485 HW/SW (4bit ex. BCG004)
214*f7ce0706SParker Newman 	CTI_PORT_TYPE_RS232_422_485_2B, // RS232/422/485 HW/SW (2bit ex. BBG008)
215*f7ce0706SParker Newman 	CTI_PORT_TYPE_MAX,
216*f7ce0706SParker Newman };
217*f7ce0706SParker Newman 
218*f7ce0706SParker Newman #define CTI_PORT_TYPE_VALID(_port_type) \
219*f7ce0706SParker Newman 	(((_port_type) > CTI_PORT_TYPE_NONE) && \
220*f7ce0706SParker Newman 	((_port_type) < CTI_PORT_TYPE_MAX))
221*f7ce0706SParker Newman 
222*f7ce0706SParker Newman #define CTI_PORT_TYPE_RS485(_port_type) \
223*f7ce0706SParker Newman 	(((_port_type) > CTI_PORT_TYPE_RS232) && \
224*f7ce0706SParker Newman 	((_port_type) < CTI_PORT_TYPE_MAX))
225*f7ce0706SParker 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;
2320d963ebfSJan Kiszka 	int (*register_gpio)(struct pci_dev *, struct uart_8250_port *);
23333969db7SAndy Shevchenko 	void (*unregister_gpio)(struct uart_8250_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);
248d0aeaa83SSudip Mukherjee 	int	(*setup)(struct exar8250 *, struct pci_dev *,
249d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *, int);
250d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
251d0aeaa83SSudip Mukherjee };
252d0aeaa83SSudip Mukherjee 
253d0aeaa83SSudip Mukherjee struct exar8250 {
254d0aeaa83SSudip Mukherjee 	unsigned int		nr;
255*f7ce0706SParker 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 
261*f7ce0706SParker Newman static inline void exar_write_reg(struct exar8250 *priv,
262*f7ce0706SParker Newman 				unsigned int reg, u8 value)
263*f7ce0706SParker Newman {
264*f7ce0706SParker Newman 	writeb(value, priv->virt + reg);
265*f7ce0706SParker Newman }
266*f7ce0706SParker Newman 
267*f7ce0706SParker Newman static inline u8 exar_read_reg(struct exar8250 *priv, unsigned int reg)
268*f7ce0706SParker Newman {
269*f7ce0706SParker Newman 	return readb(priv->virt + reg);
270*f7ce0706SParker Newman }
271*f7ce0706SParker Newman 
272*f7ce0706SParker Newman static inline void exar_ee_select(struct exar8250 *priv)
273*f7ce0706SParker Newman {
274*f7ce0706SParker Newman 	// Set chip select pin high to enable EEPROM reads/writes
275*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, UART_EXAR_REGB_EECS);
276*f7ce0706SParker Newman 	// Min ~500ns delay needed between CS assert and EEPROM access
277*f7ce0706SParker Newman 	udelay(1);
278*f7ce0706SParker Newman }
279*f7ce0706SParker Newman 
280*f7ce0706SParker Newman static inline void exar_ee_deselect(struct exar8250 *priv)
281*f7ce0706SParker Newman {
282*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, 0x00);
283*f7ce0706SParker Newman }
284*f7ce0706SParker Newman 
285*f7ce0706SParker Newman static inline void exar_ee_write_bit(struct exar8250 *priv, int bit)
286*f7ce0706SParker Newman {
287*f7ce0706SParker Newman 	u8 value = UART_EXAR_REGB_EECS;
288*f7ce0706SParker Newman 
289*f7ce0706SParker Newman 	if (bit)
290*f7ce0706SParker Newman 		value |= UART_EXAR_REGB_EEDI;
291*f7ce0706SParker Newman 
292*f7ce0706SParker Newman 	// Clock out the bit on the EEPROM interface
293*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
294*f7ce0706SParker Newman 	// 2us delay = ~500khz clock speed
295*f7ce0706SParker Newman 	udelay(2);
296*f7ce0706SParker Newman 
297*f7ce0706SParker Newman 	value |= UART_EXAR_REGB_EECK;
298*f7ce0706SParker Newman 
299*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
300*f7ce0706SParker Newman 	udelay(2);
301*f7ce0706SParker Newman }
302*f7ce0706SParker Newman 
303*f7ce0706SParker Newman static inline u8 exar_ee_read_bit(struct exar8250 *priv)
304*f7ce0706SParker Newman {
305*f7ce0706SParker Newman 	u8 regb;
306*f7ce0706SParker Newman 	u8 value = UART_EXAR_REGB_EECS;
307*f7ce0706SParker Newman 
308*f7ce0706SParker Newman 	// Clock in the bit on the EEPROM interface
309*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
310*f7ce0706SParker Newman 	// 2us delay = ~500khz clock speed
311*f7ce0706SParker Newman 	udelay(2);
312*f7ce0706SParker Newman 
313*f7ce0706SParker Newman 	value |= UART_EXAR_REGB_EECK;
314*f7ce0706SParker Newman 
315*f7ce0706SParker Newman 	exar_write_reg(priv, UART_EXAR_REGB, value);
316*f7ce0706SParker Newman 	udelay(2);
317*f7ce0706SParker Newman 
318*f7ce0706SParker Newman 	regb = exar_read_reg(priv, UART_EXAR_REGB);
319*f7ce0706SParker Newman 
320*f7ce0706SParker Newman 	return (regb & UART_EXAR_REGB_EEDO ? 1 : 0);
321*f7ce0706SParker Newman }
322*f7ce0706SParker Newman 
323*f7ce0706SParker Newman /**
324*f7ce0706SParker Newman  * exar_ee_read() - Read a word from the EEPROM
325*f7ce0706SParker Newman  * @priv: Device's private structure
326*f7ce0706SParker Newman  * @ee_addr: Offset of EEPROM to read word from
327*f7ce0706SParker Newman  *
328*f7ce0706SParker Newman  * Read a single 16bit word from an Exar UART's EEPROM.
329*f7ce0706SParker Newman  *
330*f7ce0706SParker Newman  * Return: EEPROM word
331*f7ce0706SParker Newman  */
332*f7ce0706SParker Newman static u16 exar_ee_read(struct exar8250 *priv, u8 ee_addr)
333*f7ce0706SParker Newman {
334*f7ce0706SParker Newman 	int i;
335*f7ce0706SParker Newman 	u16 data = 0;
336*f7ce0706SParker Newman 
337*f7ce0706SParker Newman 	exar_ee_select(priv);
338*f7ce0706SParker Newman 
339*f7ce0706SParker Newman 	// Send read command (opcode 110)
340*f7ce0706SParker Newman 	exar_ee_write_bit(priv, 1);
341*f7ce0706SParker Newman 	exar_ee_write_bit(priv, 1);
342*f7ce0706SParker Newman 	exar_ee_write_bit(priv, 0);
343*f7ce0706SParker Newman 
344*f7ce0706SParker Newman 	// Send address to read from
345*f7ce0706SParker Newman 	for (i = 1 << (UART_EXAR_REGB_EE_ADDR_SIZE - 1); i; i >>= 1)
346*f7ce0706SParker Newman 		exar_ee_write_bit(priv, (ee_addr & i));
347*f7ce0706SParker Newman 
348*f7ce0706SParker Newman 	// Read data 1 bit at a time
349*f7ce0706SParker Newman 	for (i = 0; i <= UART_EXAR_REGB_EE_DATA_SIZE; i++) {
350*f7ce0706SParker Newman 		data <<= 1;
351*f7ce0706SParker Newman 		data |= exar_ee_read_bit(priv);
352*f7ce0706SParker Newman 	}
353*f7ce0706SParker Newman 
354*f7ce0706SParker Newman 	exar_ee_deselect(priv);
355*f7ce0706SParker Newman 
356*f7ce0706SParker Newman 	return data;
357*f7ce0706SParker Newman }
358*f7ce0706SParker Newman 
359*f7ce0706SParker Newman /**
360*f7ce0706SParker Newman  * exar_mpio_config_output() - Configure an Exar MPIO as an output
361*f7ce0706SParker Newman  * @priv: Device's private structure
362*f7ce0706SParker Newman  * @mpio_num: MPIO number/offset to configure
363*f7ce0706SParker Newman  *
364*f7ce0706SParker Newman  * Configure a single MPIO as an output and disable tristate. It is reccomended
365*f7ce0706SParker Newman  * to set the level with exar_mpio_set_high()/exar_mpio_set_low() prior to
366*f7ce0706SParker Newman  * calling this function to ensure default MPIO pin state.
367*f7ce0706SParker Newman  *
368*f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
369*f7ce0706SParker Newman  */
370*f7ce0706SParker Newman static int exar_mpio_config_output(struct exar8250 *priv,
371*f7ce0706SParker Newman 				unsigned int mpio_num)
372*f7ce0706SParker Newman {
373*f7ce0706SParker Newman 	unsigned int mpio_offset;
374*f7ce0706SParker Newman 	u8 sel_reg; // MPIO Select register (input/output)
375*f7ce0706SParker Newman 	u8 tri_reg; // MPIO Tristate register
376*f7ce0706SParker Newman 	u8 value;
377*f7ce0706SParker Newman 
378*f7ce0706SParker Newman 	if (mpio_num < 8) {
379*f7ce0706SParker Newman 		sel_reg = UART_EXAR_MPIOSEL_7_0;
380*f7ce0706SParker Newman 		tri_reg = UART_EXAR_MPIO3T_7_0;
381*f7ce0706SParker Newman 		mpio_offset = mpio_num;
382*f7ce0706SParker Newman 	} else if (mpio_num >= 8 && mpio_num < 16) {
383*f7ce0706SParker Newman 		sel_reg = UART_EXAR_MPIOSEL_15_8;
384*f7ce0706SParker Newman 		tri_reg = UART_EXAR_MPIO3T_15_8;
385*f7ce0706SParker Newman 		mpio_offset = mpio_num - 8;
386*f7ce0706SParker Newman 	} else {
387*f7ce0706SParker Newman 		return -EINVAL;
388*f7ce0706SParker Newman 	}
389*f7ce0706SParker Newman 
390*f7ce0706SParker Newman 	// Disable MPIO pin tri-state
391*f7ce0706SParker Newman 	value = exar_read_reg(priv, tri_reg);
392*f7ce0706SParker Newman 	value &= ~BIT(mpio_offset);
393*f7ce0706SParker Newman 	exar_write_reg(priv, tri_reg, value);
394*f7ce0706SParker Newman 
395*f7ce0706SParker Newman 	value = exar_read_reg(priv, sel_reg);
396*f7ce0706SParker Newman 	value &= ~BIT(mpio_offset);
397*f7ce0706SParker Newman 	exar_write_reg(priv, sel_reg, value);
398*f7ce0706SParker Newman 
399*f7ce0706SParker Newman 	return 0;
400*f7ce0706SParker Newman }
401*f7ce0706SParker Newman 
402*f7ce0706SParker Newman /**
403*f7ce0706SParker Newman  * _exar_mpio_set() - Set an Exar MPIO output high or low
404*f7ce0706SParker Newman  * @priv: Device's private structure
405*f7ce0706SParker Newman  * @mpio_num: MPIO number/offset to set
406*f7ce0706SParker Newman  * @high: Set MPIO high if true, low if false
407*f7ce0706SParker Newman  *
408*f7ce0706SParker Newman  * Set a single MPIO high or low. exar_mpio_config_output() must also be called
409*f7ce0706SParker Newman  * to configure the pin as an output.
410*f7ce0706SParker Newman  *
411*f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
412*f7ce0706SParker Newman  */
413*f7ce0706SParker Newman static int _exar_mpio_set(struct exar8250 *priv,
414*f7ce0706SParker Newman 			unsigned int mpio_num, bool high)
415*f7ce0706SParker Newman {
416*f7ce0706SParker Newman 	unsigned int mpio_offset;
417*f7ce0706SParker Newman 	u8 lvl_reg;
418*f7ce0706SParker Newman 	u8 value;
419*f7ce0706SParker Newman 
420*f7ce0706SParker Newman 	if (mpio_num < 8) {
421*f7ce0706SParker Newman 		lvl_reg = UART_EXAR_MPIOLVL_7_0;
422*f7ce0706SParker Newman 		mpio_offset = mpio_num;
423*f7ce0706SParker Newman 	} else if (mpio_num >= 8 && mpio_num < 16) {
424*f7ce0706SParker Newman 		lvl_reg = UART_EXAR_MPIOLVL_15_8;
425*f7ce0706SParker Newman 		mpio_offset = mpio_num - 8;
426*f7ce0706SParker Newman 	} else {
427*f7ce0706SParker Newman 		return -EINVAL;
428*f7ce0706SParker Newman 	}
429*f7ce0706SParker Newman 
430*f7ce0706SParker Newman 	value = exar_read_reg(priv, lvl_reg);
431*f7ce0706SParker Newman 	if (high)
432*f7ce0706SParker Newman 		value |= BIT(mpio_offset);
433*f7ce0706SParker Newman 	else
434*f7ce0706SParker Newman 		value &= ~BIT(mpio_offset);
435*f7ce0706SParker Newman 	exar_write_reg(priv, lvl_reg, value);
436*f7ce0706SParker Newman 
437*f7ce0706SParker Newman 	return 0;
438*f7ce0706SParker Newman }
439*f7ce0706SParker Newman 
440*f7ce0706SParker Newman static int exar_mpio_set_low(struct exar8250 *priv, unsigned int mpio_num)
441*f7ce0706SParker Newman {
442*f7ce0706SParker Newman 	return _exar_mpio_set(priv, mpio_num, false);
443*f7ce0706SParker Newman }
444*f7ce0706SParker Newman 
445*f7ce0706SParker Newman static int exar_mpio_set_high(struct exar8250 *priv, unsigned int mpio_num)
446*f7ce0706SParker Newman {
447*f7ce0706SParker Newman 	return _exar_mpio_set(priv, mpio_num, true);
448*f7ce0706SParker Newman }
449*f7ce0706SParker 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 
638*f7ce0706SParker Newman /**
639*f7ce0706SParker Newman  * cti_tristate_disable() - Disable RS485 transciever tristate
640*f7ce0706SParker Newman  * @priv: Device's private structure
641*f7ce0706SParker Newman  * @port_num: Port number to set tristate off
642*f7ce0706SParker Newman  *
643*f7ce0706SParker Newman  * Most RS485 capable cards have a power on tristate jumper/switch that ensures
644*f7ce0706SParker Newman  * the RS422/RS485 transciever does not drive a multi-drop RS485 bus when it is
645*f7ce0706SParker Newman  * not the master. When this jumper is installed the user must set the RS485
646*f7ce0706SParker Newman  * mode to Full or Half duplex to disable tristate prior to using the port.
647*f7ce0706SParker Newman  *
648*f7ce0706SParker Newman  * Some Exar UARTs have an auto-tristate feature while others require setting
649*f7ce0706SParker Newman  * an MPIO to disable the tristate.
650*f7ce0706SParker Newman  *
651*f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
652*f7ce0706SParker Newman  */
653*f7ce0706SParker Newman static int cti_tristate_disable(struct exar8250 *priv, unsigned int port_num)
654*f7ce0706SParker Newman {
655*f7ce0706SParker Newman 	int ret;
656*f7ce0706SParker Newman 
657*f7ce0706SParker Newman 	ret = exar_mpio_set_high(priv, port_num);
658*f7ce0706SParker Newman 	if (ret)
659*f7ce0706SParker Newman 		return ret;
660*f7ce0706SParker Newman 
661*f7ce0706SParker Newman 	return exar_mpio_config_output(priv, port_num);
662*f7ce0706SParker Newman }
663*f7ce0706SParker Newman 
664*f7ce0706SParker Newman /**
665*f7ce0706SParker Newman  * cti_plx_int_enable() - Enable UART interrupts to PLX bridge
666*f7ce0706SParker Newman  * @priv: Device's private structure
667*f7ce0706SParker Newman  *
668*f7ce0706SParker Newman  * Some older CTI cards require MPIO_0 to be set low to enable the
669*f7ce0706SParker Newman  * interupts from the UART to the PLX PCI->PCIe bridge.
670*f7ce0706SParker Newman  *
671*f7ce0706SParker Newman  * Return: 0 on success, negative error code on failure
672*f7ce0706SParker Newman  */
673*f7ce0706SParker Newman static int cti_plx_int_enable(struct exar8250 *priv)
674*f7ce0706SParker Newman {
675*f7ce0706SParker Newman 	int ret;
676*f7ce0706SParker Newman 
677*f7ce0706SParker Newman 	ret = exar_mpio_set_low(priv, 0);
678*f7ce0706SParker Newman 	if (ret)
679*f7ce0706SParker Newman 		return ret;
680*f7ce0706SParker Newman 
681*f7ce0706SParker Newman 	return exar_mpio_config_output(priv, 0);
682*f7ce0706SParker Newman }
683*f7ce0706SParker Newman 
684*f7ce0706SParker Newman /**
685*f7ce0706SParker Newman  * cti_read_osc_freq() - Read the UART oscillator frequency from EEPROM
686*f7ce0706SParker Newman  * @priv: Device's private structure
687*f7ce0706SParker Newman  * @eeprom_offset: Offset where the oscillator frequency is stored
688*f7ce0706SParker Newman  *
689*f7ce0706SParker Newman  * CTI XR17x15X and XR17V25X cards have the serial boards oscillator frequency
690*f7ce0706SParker Newman  * stored in the EEPROM. FPGA and XR17V35X based cards use the PCI/PCIe clock.
691*f7ce0706SParker Newman  *
692*f7ce0706SParker Newman  * Return: frequency on success, negative error code on failure
693*f7ce0706SParker Newman  */
694*f7ce0706SParker Newman static int cti_read_osc_freq(struct exar8250 *priv, u8 eeprom_offset)
695*f7ce0706SParker Newman {
696*f7ce0706SParker Newman 	u16 lower_word;
697*f7ce0706SParker Newman 	u16 upper_word;
698*f7ce0706SParker Newman 	int osc_freq;
699*f7ce0706SParker Newman 
700*f7ce0706SParker Newman 	lower_word = exar_ee_read(priv, eeprom_offset);
701*f7ce0706SParker Newman 	// Check if EEPROM word was blank
702*f7ce0706SParker Newman 	if (lower_word == 0xFFFF)
703*f7ce0706SParker Newman 		return -EIO;
704*f7ce0706SParker Newman 
705*f7ce0706SParker Newman 	upper_word = exar_ee_read(priv, (eeprom_offset + 1));
706*f7ce0706SParker Newman 	if (upper_word == 0xFFFF)
707*f7ce0706SParker Newman 		return -EIO;
708*f7ce0706SParker Newman 
709*f7ce0706SParker Newman 	osc_freq = FIELD_PREP(CTI_EE_MASK_OSC_FREQ_LOWER, lower_word) |
710*f7ce0706SParker Newman 		FIELD_PREP(CTI_EE_MASK_OSC_FREQ_UPPER, upper_word);
711*f7ce0706SParker Newman 
712*f7ce0706SParker Newman 	return osc_freq;
713*f7ce0706SParker Newman }
714*f7ce0706SParker Newman 
715*f7ce0706SParker Newman /**
716*f7ce0706SParker Newman  * cti_get_port_type_xr17c15x_xr17v25x() - Get port type of xr17c15x/xr17v25x
717*f7ce0706SParker Newman  * @priv: Device's private structure
718*f7ce0706SParker Newman  * @port_num: Port to get type of
719*f7ce0706SParker Newman  *
720*f7ce0706SParker Newman  * CTI xr17c15x and xr17v25x based cards port types are based on PCI IDs.
721*f7ce0706SParker Newman  *
722*f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
723*f7ce0706SParker Newman  */
724*f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_xr17c15x_xr17v25x(struct exar8250 *priv,
725*f7ce0706SParker Newman 							struct pci_dev *pcidev,
726*f7ce0706SParker Newman 							unsigned int port_num)
727*f7ce0706SParker Newman {
728*f7ce0706SParker Newman 	enum cti_port_type port_type;
729*f7ce0706SParker Newman 
730*f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
731*f7ce0706SParker Newman 	// RS232 only cards
732*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_232:
733*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_232:
734*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_232:
735*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232:
736*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_232_NS:
737*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232:
738*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_232_NS:
739*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232;
740*f7ce0706SParker Newman 		break;
741*f7ce0706SParker Newman 	// 1x RS232, 1x RS422/RS485
742*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_1_1:
743*f7ce0706SParker Newman 		port_type = (port_num == 0) ?
744*f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
745*f7ce0706SParker Newman 		break;
746*f7ce0706SParker Newman 	// 2x RS232, 2x RS422/RS485
747*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_2:
748*f7ce0706SParker Newman 		port_type = (port_num < 2) ?
749*f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
750*f7ce0706SParker Newman 		break;
751*f7ce0706SParker Newman 	// 4x RS232, 4x RS422/RS485
752*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4:
753*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
754*f7ce0706SParker Newman 		port_type = (port_num < 4) ?
755*f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
756*f7ce0706SParker Newman 		break;
757*f7ce0706SParker Newman 	// RS232/RS422/RS485 HW (jumper) selectable
758*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2:
759*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4:
760*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8:
761*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_SP_OPTO:
762*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_A:
763*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_SP_OPTO_B:
764*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS:
765*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_A:
766*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_B:
767*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS:
768*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_A:
769*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_B:
770*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS_OPTO:
771*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_A:
772*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_B:
773*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
774*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
775*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
776*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
777*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
778*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
779*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
780*f7ce0706SParker Newman 		break;
781*f7ce0706SParker Newman 	// RS422/RS485 HW (jumper) selectable
782*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_485:
783*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_485:
784*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_485:
785*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
786*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
787*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS422_485;
788*f7ce0706SParker Newman 		break;
789*f7ce0706SParker Newman 	// 6x RS232, 2x RS422/RS485
790*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
791*f7ce0706SParker Newman 		port_type = (port_num < 6) ?
792*f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
793*f7ce0706SParker Newman 		break;
794*f7ce0706SParker Newman 	// 2x RS232, 6x RS422/RS485
795*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
796*f7ce0706SParker Newman 		port_type = (port_num < 2) ?
797*f7ce0706SParker Newman 			CTI_PORT_TYPE_RS232 : CTI_PORT_TYPE_RS422_485;
798*f7ce0706SParker Newman 		break;
799*f7ce0706SParker Newman 	default:
800*f7ce0706SParker Newman 		dev_err(&pcidev->dev, "unknown/unsupported device\n");
801*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_NONE;
802*f7ce0706SParker Newman 	}
803*f7ce0706SParker Newman 
804*f7ce0706SParker Newman 	return port_type;
805*f7ce0706SParker Newman }
806*f7ce0706SParker Newman 
807*f7ce0706SParker Newman /**
808*f7ce0706SParker Newman  * cti_get_port_type_fpga() - Get the port type of a CTI FPGA card
809*f7ce0706SParker Newman  * @priv: Device's private structure
810*f7ce0706SParker Newman  * @port_num: Port to get type of
811*f7ce0706SParker Newman  *
812*f7ce0706SParker Newman  * FPGA based cards port types are based on PCI IDs.
813*f7ce0706SParker Newman  *
814*f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
815*f7ce0706SParker Newman  */
816*f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_fpga(struct exar8250 *priv,
817*f7ce0706SParker Newman 						struct pci_dev *pcidev,
818*f7ce0706SParker Newman 						unsigned int port_num)
819*f7ce0706SParker Newman {
820*f7ce0706SParker Newman 	enum cti_port_type port_type;
821*f7ce0706SParker Newman 
822*f7ce0706SParker Newman 	switch (pcidev->device) {
823*f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG00X:
824*f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG01X:
825*f7ce0706SParker Newman 	case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_16:
826*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
827*f7ce0706SParker Newman 		break;
828*f7ce0706SParker Newman 	default:
829*f7ce0706SParker Newman 		dev_err(&pcidev->dev, "unknown/unsupported device\n");
830*f7ce0706SParker Newman 		return CTI_PORT_TYPE_NONE;
831*f7ce0706SParker Newman 	}
832*f7ce0706SParker Newman 
833*f7ce0706SParker Newman 	return port_type;
834*f7ce0706SParker Newman }
835*f7ce0706SParker Newman 
836*f7ce0706SParker Newman /**
837*f7ce0706SParker Newman  * cti_get_port_type_xr17v35x() - Read port type from the EEPROM
838*f7ce0706SParker Newman  * @priv: Device's private structure
839*f7ce0706SParker Newman  * @port_num: port offset
840*f7ce0706SParker Newman  *
841*f7ce0706SParker Newman  * CTI XR17V35X based cards have the port types stored in the EEPROM.
842*f7ce0706SParker Newman  * This function reads the port type for a single port.
843*f7ce0706SParker Newman  *
844*f7ce0706SParker Newman  * Return: port type on success, CTI_PORT_TYPE_NONE on failure
845*f7ce0706SParker Newman  */
846*f7ce0706SParker Newman static enum cti_port_type cti_get_port_type_xr17v35x(struct exar8250 *priv,
847*f7ce0706SParker Newman 						struct pci_dev *pcidev,
848*f7ce0706SParker Newman 						unsigned int port_num)
849*f7ce0706SParker Newman {
850*f7ce0706SParker Newman 	enum cti_port_type port_type;
851*f7ce0706SParker Newman 	u16 port_flags;
852*f7ce0706SParker Newman 	u8 offset;
853*f7ce0706SParker Newman 
854*f7ce0706SParker Newman 	offset = CTI_EE_OFF_XR17V35X_PORT_FLAGS + port_num;
855*f7ce0706SParker Newman 	port_flags = exar_ee_read(priv, offset);
856*f7ce0706SParker Newman 
857*f7ce0706SParker Newman 	port_type = FIELD_GET(CTI_EE_MASK_PORT_FLAGS_TYPE, port_flags);
858*f7ce0706SParker Newman 	if (!CTI_PORT_TYPE_VALID(port_type)) {
859*f7ce0706SParker Newman 		/*
860*f7ce0706SParker Newman 		 * If the port type is missing the card assume it is a
861*f7ce0706SParker Newman 		 * RS232/RS422/RS485 card to be safe.
862*f7ce0706SParker Newman 		 *
863*f7ce0706SParker Newman 		 * There is one known board (BEG013) that only has
864*f7ce0706SParker Newman 		 * 3 of 4 port types written to the EEPROM so this
865*f7ce0706SParker Newman 		 * acts as a work around.
866*f7ce0706SParker Newman 		 */
867*f7ce0706SParker Newman 		dev_warn(&pcidev->dev,
868*f7ce0706SParker Newman 			"failed to get port %d type from EEPROM\n", port_num);
869*f7ce0706SParker Newman 		port_type = CTI_PORT_TYPE_RS232_422_485_HW;
870*f7ce0706SParker Newman 	}
871*f7ce0706SParker Newman 
872*f7ce0706SParker Newman 	return port_type;
873*f7ce0706SParker Newman }
874*f7ce0706SParker Newman 
875*f7ce0706SParker Newman static int cti_rs485_config_mpio_tristate(struct uart_port *port,
876*f7ce0706SParker Newman 					struct ktermios *termios,
877*f7ce0706SParker Newman 					struct serial_rs485 *rs485)
878*f7ce0706SParker Newman {
879*f7ce0706SParker Newman 	struct exar8250 *priv = (struct exar8250 *)port->private_data;
880*f7ce0706SParker Newman 	int ret;
881*f7ce0706SParker Newman 
882*f7ce0706SParker Newman 	ret = generic_rs485_config(port, termios, rs485);
883*f7ce0706SParker Newman 	if (ret)
884*f7ce0706SParker Newman 		return ret;
885*f7ce0706SParker Newman 
886*f7ce0706SParker Newman 	// Disable power-on RS485 tri-state via MPIO
887*f7ce0706SParker Newman 	return cti_tristate_disable(priv, port->port_id);
888*f7ce0706SParker Newman }
889*f7ce0706SParker Newman 
890*f7ce0706SParker Newman static int cti_port_setup_common(struct exar8250 *priv,
891*f7ce0706SParker Newman 				struct pci_dev *pcidev,
892*f7ce0706SParker Newman 				int idx, unsigned int offset,
893*f7ce0706SParker Newman 				struct uart_8250_port *port)
894*f7ce0706SParker Newman {
895*f7ce0706SParker Newman 	int ret;
896*f7ce0706SParker Newman 
897*f7ce0706SParker Newman 	if (priv->osc_freq == 0)
898*f7ce0706SParker Newman 		return -EINVAL;
899*f7ce0706SParker Newman 
900*f7ce0706SParker Newman 	port->port.port_id = idx;
901*f7ce0706SParker Newman 	port->port.uartclk = priv->osc_freq;
902*f7ce0706SParker Newman 
903*f7ce0706SParker Newman 	ret = serial8250_pci_setup_port(pcidev, port, 0, offset, 0);
904*f7ce0706SParker Newman 	if (ret) {
905*f7ce0706SParker Newman 		dev_err(&pcidev->dev,
906*f7ce0706SParker Newman 			"failed to setup pci for port %d err: %d\n", idx, ret);
907*f7ce0706SParker Newman 		return ret;
908*f7ce0706SParker Newman 	}
909*f7ce0706SParker Newman 
910*f7ce0706SParker Newman 	port->port.private_data = (void *)priv;
911*f7ce0706SParker Newman 	port->port.pm = exar_pm;
912*f7ce0706SParker Newman 	port->port.shutdown = exar_shutdown;
913*f7ce0706SParker Newman 
914*f7ce0706SParker Newman 	return 0;
915*f7ce0706SParker Newman }
916*f7ce0706SParker Newman 
917*f7ce0706SParker Newman static int cti_port_setup_fpga(struct exar8250 *priv,
918*f7ce0706SParker Newman 				struct pci_dev *pcidev,
919*f7ce0706SParker Newman 				struct uart_8250_port *port,
920*f7ce0706SParker Newman 				int idx)
921*f7ce0706SParker Newman {
922*f7ce0706SParker Newman 	enum cti_port_type port_type;
923*f7ce0706SParker Newman 	unsigned int offset;
924*f7ce0706SParker Newman 
925*f7ce0706SParker Newman 	port_type = cti_get_port_type_fpga(priv, pcidev, idx);
926*f7ce0706SParker Newman 
927*f7ce0706SParker Newman 	// FPGA shares port offests with XR17C15X
928*f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17C15X_PORT_OFFSET;
929*f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
930*f7ce0706SParker Newman 
931*f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
932*f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
933*f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
934*f7ce0706SParker Newman 
935*f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
936*f7ce0706SParker Newman 		port->port.rs485_config = generic_rs485_config;
937*f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
938*f7ce0706SParker Newman 	}
939*f7ce0706SParker Newman 
940*f7ce0706SParker Newman 	return cti_port_setup_common(priv, pcidev, idx, offset, port);
941*f7ce0706SParker Newman }
942*f7ce0706SParker Newman 
943*f7ce0706SParker Newman static int cti_port_setup_xr17v35x(struct exar8250 *priv,
944*f7ce0706SParker Newman 				struct pci_dev *pcidev,
945*f7ce0706SParker Newman 				struct uart_8250_port *port,
946*f7ce0706SParker Newman 				int idx)
947*f7ce0706SParker Newman {
948*f7ce0706SParker Newman 	enum cti_port_type port_type;
949*f7ce0706SParker Newman 	unsigned int offset;
950*f7ce0706SParker Newman 	int ret;
951*f7ce0706SParker Newman 
952*f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17v35x(priv, pcidev, idx);
953*f7ce0706SParker Newman 
954*f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17V35X_PORT_OFFSET;
955*f7ce0706SParker Newman 	port->port.type = PORT_XR17V35X;
956*f7ce0706SParker Newman 
957*f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
958*f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
959*f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
960*f7ce0706SParker Newman 
961*f7ce0706SParker Newman 	switch (port_type) {
962*f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS422_485:
963*f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_HW:
964*f7ce0706SParker Newman 		port->port.rs485_config = cti_rs485_config_mpio_tristate;
965*f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
966*f7ce0706SParker Newman 		break;
967*f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_SW:
968*f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_4B:
969*f7ce0706SParker Newman 	case CTI_PORT_TYPE_RS232_422_485_2B:
970*f7ce0706SParker Newman 		port->port.rs485_config = generic_rs485_config;
971*f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
972*f7ce0706SParker Newman 		break;
973*f7ce0706SParker Newman 	default:
974*f7ce0706SParker Newman 		break;
975*f7ce0706SParker Newman 	}
976*f7ce0706SParker Newman 
977*f7ce0706SParker Newman 	ret = cti_port_setup_common(priv, pcidev, idx, offset, port);
978*f7ce0706SParker Newman 	if (ret)
979*f7ce0706SParker Newman 		return ret;
980*f7ce0706SParker Newman 
981*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_8XMODE), 0x00);
982*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_FCTR), UART_FCTR_EXAR_TRGD);
983*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_TXTRG), 128);
984*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_RXTRG), 128);
985*f7ce0706SParker Newman 
986*f7ce0706SParker Newman 	return 0;
987*f7ce0706SParker Newman }
988*f7ce0706SParker Newman 
989*f7ce0706SParker Newman static int cti_port_setup_xr17v25x(struct exar8250 *priv,
990*f7ce0706SParker Newman 				struct pci_dev *pcidev,
991*f7ce0706SParker Newman 				struct uart_8250_port *port,
992*f7ce0706SParker Newman 				int idx)
993*f7ce0706SParker Newman {
994*f7ce0706SParker Newman 	enum cti_port_type port_type;
995*f7ce0706SParker Newman 	unsigned int offset;
996*f7ce0706SParker Newman 	int ret;
997*f7ce0706SParker Newman 
998*f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17c15x_xr17v25x(priv, pcidev, idx);
999*f7ce0706SParker Newman 
1000*f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17V25X_PORT_OFFSET;
1001*f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
1002*f7ce0706SParker Newman 
1003*f7ce0706SParker Newman 	// XR17V25X supports fractional baudrates
1004*f7ce0706SParker Newman 	port->port.get_divisor = xr17v35x_get_divisor;
1005*f7ce0706SParker Newman 	port->port.set_divisor = xr17v35x_set_divisor;
1006*f7ce0706SParker Newman 	port->port.startup = xr17v35x_startup;
1007*f7ce0706SParker Newman 
1008*f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
1009*f7ce0706SParker Newman 		switch (pcidev->subsystem_device) {
1010*f7ce0706SParker Newman 		// These cards support power on 485 tri-state via MPIO
1011*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
1012*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
1013*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
1014*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
1015*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
1016*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
1017*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
1018*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
1019*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
1020*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
1021*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
1022*f7ce0706SParker Newman 			port->port.rs485_config = cti_rs485_config_mpio_tristate;
1023*f7ce0706SParker Newman 			break;
1024*f7ce0706SParker Newman 		// Otherwise auto or no power on 485 tri-state support
1025*f7ce0706SParker Newman 		default:
1026*f7ce0706SParker Newman 			port->port.rs485_config = generic_rs485_config;
1027*f7ce0706SParker Newman 			break;
1028*f7ce0706SParker Newman 		}
1029*f7ce0706SParker Newman 
1030*f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
1031*f7ce0706SParker Newman 	}
1032*f7ce0706SParker Newman 
1033*f7ce0706SParker Newman 	ret = cti_port_setup_common(priv, pcidev, idx, offset, port);
1034*f7ce0706SParker Newman 	if (ret)
1035*f7ce0706SParker Newman 		return ret;
1036*f7ce0706SParker Newman 
1037*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_8XMODE), 0x00);
1038*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_FCTR), UART_FCTR_EXAR_TRGD);
1039*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_TXTRG), 32);
1040*f7ce0706SParker Newman 	exar_write_reg(priv, (offset + UART_EXAR_RXTRG), 32);
1041*f7ce0706SParker Newman 
1042*f7ce0706SParker Newman 	return 0;
1043*f7ce0706SParker Newman }
1044*f7ce0706SParker Newman 
1045*f7ce0706SParker Newman static int cti_port_setup_xr17c15x(struct exar8250 *priv,
1046*f7ce0706SParker Newman 				struct pci_dev *pcidev,
1047*f7ce0706SParker Newman 				struct uart_8250_port *port,
1048*f7ce0706SParker Newman 				int idx)
1049*f7ce0706SParker Newman {
1050*f7ce0706SParker Newman 	enum cti_port_type port_type;
1051*f7ce0706SParker Newman 	unsigned int offset;
1052*f7ce0706SParker Newman 
1053*f7ce0706SParker Newman 	port_type = cti_get_port_type_xr17c15x_xr17v25x(priv, pcidev, idx);
1054*f7ce0706SParker Newman 
1055*f7ce0706SParker Newman 	offset = idx * UART_EXAR_XR17C15X_PORT_OFFSET;
1056*f7ce0706SParker Newman 	port->port.type = PORT_XR17D15X;
1057*f7ce0706SParker Newman 
1058*f7ce0706SParker Newman 	if (CTI_PORT_TYPE_RS485(port_type)) {
1059*f7ce0706SParker Newman 		switch (pcidev->subsystem_device) {
1060*f7ce0706SParker Newman 		// These cards support power on 485 tri-state via MPIO
1061*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP:
1062*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_SP_485:
1063*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_SP:
1064*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_6_2_SP:
1065*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_6_SP:
1066*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_LEFT:
1067*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XP_OPTO_RIGHT:
1068*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XP_OPTO:
1069*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4_XPRS_OPTO:
1070*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP:
1071*f7ce0706SParker Newman 		case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS_LP_485:
1072*f7ce0706SParker Newman 			port->port.rs485_config = cti_rs485_config_mpio_tristate;
1073*f7ce0706SParker Newman 			break;
1074*f7ce0706SParker Newman 		// Otherwise auto or no power on 485 tri-state support
1075*f7ce0706SParker Newman 		default:
1076*f7ce0706SParker Newman 			port->port.rs485_config = generic_rs485_config;
1077*f7ce0706SParker Newman 			break;
1078*f7ce0706SParker Newman 		}
1079*f7ce0706SParker Newman 
1080*f7ce0706SParker Newman 		port->port.rs485_supported = generic_rs485_supported;
1081*f7ce0706SParker Newman 	}
1082*f7ce0706SParker Newman 
1083*f7ce0706SParker Newman 	return cti_port_setup_common(priv, pcidev, idx, offset, port);
1084*f7ce0706SParker Newman }
1085*f7ce0706SParker Newman 
1086*f7ce0706SParker Newman static int cti_board_init_xr17v35x(struct exar8250 *priv,
1087*f7ce0706SParker Newman 				struct pci_dev *pcidev)
1088*f7ce0706SParker Newman {
1089*f7ce0706SParker Newman 	// XR17V35X uses the PCIe clock rather than an oscillator
1090*f7ce0706SParker Newman 	priv->osc_freq = CTI_DEFAULT_PCIE_OSC_FREQ;
1091*f7ce0706SParker Newman 
1092*f7ce0706SParker Newman 	return 0;
1093*f7ce0706SParker Newman }
1094*f7ce0706SParker Newman 
1095*f7ce0706SParker Newman static int cti_board_init_xr17v25x(struct exar8250 *priv,
1096*f7ce0706SParker Newman 				struct pci_dev *pcidev)
1097*f7ce0706SParker Newman {
1098*f7ce0706SParker Newman 	int osc_freq;
1099*f7ce0706SParker Newman 
1100*f7ce0706SParker Newman 	osc_freq = cti_read_osc_freq(priv, CTI_EE_OFF_XR17V25X_OSC_FREQ);
1101*f7ce0706SParker Newman 	if (osc_freq < 0) {
1102*f7ce0706SParker Newman 		dev_warn(&pcidev->dev,
1103*f7ce0706SParker Newman 			"failed to read osc freq from EEPROM, using default\n");
1104*f7ce0706SParker Newman 		osc_freq = CTI_DEFAULT_PCI_OSC_FREQ;
1105*f7ce0706SParker Newman 	}
1106*f7ce0706SParker Newman 
1107*f7ce0706SParker Newman 	priv->osc_freq = osc_freq;
1108*f7ce0706SParker Newman 
1109*f7ce0706SParker Newman 	/* enable interupts on cards that need the "PLX fix" */
1110*f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
1111*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_XPRS:
1112*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_A:
1113*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_16_XPRS_B:
1114*f7ce0706SParker Newman 		cti_plx_int_enable(priv);
1115*f7ce0706SParker Newman 		break;
1116*f7ce0706SParker Newman 	default:
1117*f7ce0706SParker Newman 		break;
1118*f7ce0706SParker Newman 	}
1119*f7ce0706SParker Newman 
1120*f7ce0706SParker Newman 	return 0;
1121*f7ce0706SParker Newman }
1122*f7ce0706SParker Newman 
1123*f7ce0706SParker Newman static int cti_board_init_xr17c15x(struct exar8250 *priv,
1124*f7ce0706SParker Newman 				struct pci_dev *pcidev)
1125*f7ce0706SParker Newman {
1126*f7ce0706SParker Newman 	int osc_freq;
1127*f7ce0706SParker Newman 
1128*f7ce0706SParker Newman 	osc_freq = cti_read_osc_freq(priv, CTI_EE_OFF_XR17C15X_OSC_FREQ);
1129*f7ce0706SParker Newman 	if (osc_freq <= 0) {
1130*f7ce0706SParker Newman 		dev_warn(&pcidev->dev,
1131*f7ce0706SParker Newman 			"failed to read osc freq from EEPROM, using default\n");
1132*f7ce0706SParker Newman 		osc_freq = CTI_DEFAULT_PCI_OSC_FREQ;
1133*f7ce0706SParker Newman 	}
1134*f7ce0706SParker Newman 
1135*f7ce0706SParker Newman 	priv->osc_freq = osc_freq;
1136*f7ce0706SParker Newman 
1137*f7ce0706SParker Newman 	/* enable interrupts on cards that need the "PLX fix" */
1138*f7ce0706SParker Newman 	switch (pcidev->subsystem_device) {
1139*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS:
1140*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_A:
1141*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_B:
1142*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_XPRS_OPTO:
1143*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_A:
1144*f7ce0706SParker Newman 	case PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_XPRS_OPTO_B:
1145*f7ce0706SParker Newman 		cti_plx_int_enable(priv);
1146*f7ce0706SParker Newman 		break;
1147*f7ce0706SParker Newman 	default:
1148*f7ce0706SParker Newman 		break;
1149*f7ce0706SParker Newman 	}
1150*f7ce0706SParker Newman 
1151*f7ce0706SParker Newman 	return 0;
1152*f7ce0706SParker Newman }
1153*f7ce0706SParker Newman 
1154*f7ce0706SParker Newman static int cti_board_init_fpga(struct exar8250 *priv, struct pci_dev *pcidev)
1155*f7ce0706SParker Newman {
1156*f7ce0706SParker Newman 	int ret;
1157*f7ce0706SParker Newman 	u16 cfg_val;
1158*f7ce0706SParker Newman 
1159*f7ce0706SParker Newman 	// FPGA OSC is fixed to the 33MHz PCI clock
1160*f7ce0706SParker Newman 	priv->osc_freq = CTI_DEFAULT_FPGA_OSC_FREQ;
1161*f7ce0706SParker Newman 
1162*f7ce0706SParker Newman 	// Enable external interrupts in special cfg space register
1163*f7ce0706SParker Newman 	ret = pci_read_config_word(pcidev, CTI_FPGA_CFG_INT_EN_REG, &cfg_val);
1164*f7ce0706SParker Newman 	if (ret)
1165*f7ce0706SParker Newman 		return ret;
1166*f7ce0706SParker Newman 
1167*f7ce0706SParker Newman 	cfg_val |= CTI_FPGA_CFG_INT_EN_EXT_BIT;
1168*f7ce0706SParker Newman 	ret = pci_write_config_word(pcidev, CTI_FPGA_CFG_INT_EN_REG, cfg_val);
1169*f7ce0706SParker Newman 	if (ret)
1170*f7ce0706SParker Newman 		return ret;
1171*f7ce0706SParker Newman 
1172*f7ce0706SParker Newman 	// RS485 gate needs to be enabled; otherwise RTS/CTS will not work
1173*f7ce0706SParker Newman 	exar_write_reg(priv, CTI_FPGA_RS485_IO_REG, 0x01);
1174*f7ce0706SParker Newman 
1175*f7ce0706SParker Newman 	return 0;
1176*f7ce0706SParker Newman }
1177*f7ce0706SParker Newman 
1178fc6cc961SJan Kiszka static int
1179d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
1180d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
1181d0aeaa83SSudip Mukherjee {
1182d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
1183d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
1184d0aeaa83SSudip Mukherjee 
1185d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
1186d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
1187d0aeaa83SSudip Mukherjee }
1188d0aeaa83SSudip Mukherjee 
1189bea8be65SJan Kiszka static void setup_gpio(struct pci_dev *pcidev, u8 __iomem *p)
1190d0aeaa83SSudip Mukherjee {
1191bea8be65SJan Kiszka 	/*
1192bea8be65SJan Kiszka 	 * The Commtech adapters required the MPIOs to be driven low. The Exar
1193bea8be65SJan Kiszka 	 * devices will export them as GPIOs, so we pre-configure them safely
1194bea8be65SJan Kiszka 	 * as inputs.
1195bea8be65SJan Kiszka 	 */
11965fdbe136SMatthew Howell 
11975fdbe136SMatthew Howell 	u8 dir = 0x00;
11985fdbe136SMatthew Howell 
11995fdbe136SMatthew Howell 	if  ((pcidev->vendor == PCI_VENDOR_ID_EXAR) &&
12005fdbe136SMatthew Howell 		(pcidev->subsystem_vendor != PCI_VENDOR_ID_SEALEVEL)) {
12015fdbe136SMatthew Howell 		// Configure GPIO as inputs for Commtech adapters
12025fdbe136SMatthew Howell 		dir = 0xff;
12035fdbe136SMatthew Howell 	} else {
12045fdbe136SMatthew Howell 		// Configure GPIO as outputs for SeaLevel adapters
12055fdbe136SMatthew Howell 		dir = 0x00;
12065fdbe136SMatthew Howell 	}
1207bea8be65SJan Kiszka 
1208d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
1209d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
1210d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
1211d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
1212bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_7_0);
1213d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
1214d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
1215d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
1216d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
1217d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
1218bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_15_8);
1219d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
1220d0aeaa83SSudip Mukherjee }
1221d0aeaa83SSudip Mukherjee 
122233969db7SAndy Shevchenko static struct platform_device *__xr17v35x_register_gpio(struct pci_dev *pcidev,
122381171e7dSHeikki Krogerus 							const struct software_node *node)
1224d0aeaa83SSudip Mukherjee {
1225d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
1226d0aeaa83SSudip Mukherjee 
1227d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
1228d0aeaa83SSudip Mukherjee 	if (!pdev)
1229d0aeaa83SSudip Mukherjee 		return NULL;
1230d0aeaa83SSudip Mukherjee 
1231d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
123273f76db8SAndy Shevchenko 	device_set_node(&pdev->dev, dev_fwnode(&pcidev->dev));
1233d3936d74SJan Kiszka 
123481171e7dSHeikki Krogerus 	if (device_add_software_node(&pdev->dev, node) < 0 ||
1235380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
1236d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
1237d0aeaa83SSudip Mukherjee 		return NULL;
1238d0aeaa83SSudip Mukherjee 	}
1239d0aeaa83SSudip Mukherjee 
1240d0aeaa83SSudip Mukherjee 	return pdev;
1241d0aeaa83SSudip Mukherjee }
1242d0aeaa83SSudip Mukherjee 
124333969db7SAndy Shevchenko static void __xr17v35x_unregister_gpio(struct platform_device *pdev)
124433969db7SAndy Shevchenko {
124533969db7SAndy Shevchenko 	device_remove_software_node(&pdev->dev);
124633969db7SAndy Shevchenko 	platform_device_unregister(pdev);
124733969db7SAndy Shevchenko }
124833969db7SAndy Shevchenko 
1249380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
1250a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 0),
1251380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
1252380b1e2fSJan Kiszka 	{ }
1253380b1e2fSJan Kiszka };
1254380b1e2fSJan Kiszka 
125581171e7dSHeikki Krogerus static const struct software_node exar_gpio_node = {
125681171e7dSHeikki Krogerus 	.properties = exar_gpio_properties,
125781171e7dSHeikki Krogerus };
125881171e7dSHeikki Krogerus 
125933969db7SAndy Shevchenko static int xr17v35x_register_gpio(struct pci_dev *pcidev, struct uart_8250_port *port)
12600d963ebfSJan Kiszka {
12610d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
12620d963ebfSJan Kiszka 		port->port.private_data =
126381171e7dSHeikki Krogerus 			__xr17v35x_register_gpio(pcidev, &exar_gpio_node);
12640d963ebfSJan Kiszka 
12650d963ebfSJan Kiszka 	return 0;
12660d963ebfSJan Kiszka }
12670d963ebfSJan Kiszka 
126833969db7SAndy Shevchenko static void xr17v35x_unregister_gpio(struct uart_8250_port *port)
126933969db7SAndy Shevchenko {
127033969db7SAndy Shevchenko 	if (!port->port.private_data)
127133969db7SAndy Shevchenko 		return;
127233969db7SAndy Shevchenko 
127333969db7SAndy Shevchenko 	__xr17v35x_unregister_gpio(port->port.private_data);
127433969db7SAndy Shevchenko 	port->port.private_data = NULL;
127533969db7SAndy Shevchenko }
127633969db7SAndy Shevchenko 
1277687911b3SMatthew Howell static int sealevel_rs485_config(struct uart_port *port, struct ktermios *termios,
1278687911b3SMatthew Howell 				  struct serial_rs485 *rs485)
1279687911b3SMatthew Howell {
1280687911b3SMatthew Howell 	u8 __iomem *p = port->membase;
1281687911b3SMatthew Howell 	u8 old_lcr;
1282687911b3SMatthew Howell 	u8 efr;
1283687911b3SMatthew Howell 	u8 dld;
1284687911b3SMatthew Howell 	int ret;
1285687911b3SMatthew Howell 
1286687911b3SMatthew Howell 	ret = generic_rs485_config(port, termios, rs485);
1287687911b3SMatthew Howell 	if (ret)
1288687911b3SMatthew Howell 		return ret;
1289687911b3SMatthew Howell 
1290687911b3SMatthew Howell 	if (rs485->flags & SER_RS485_ENABLED) {
1291687911b3SMatthew Howell 		old_lcr = readb(p + UART_LCR);
1292687911b3SMatthew Howell 
1293687911b3SMatthew Howell 		/* Set EFR[4]=1 to enable enhanced feature registers */
1294687911b3SMatthew Howell 		efr = readb(p + UART_XR_EFR);
1295687911b3SMatthew Howell 		efr |= UART_EFR_ECB;
1296687911b3SMatthew Howell 		writeb(efr, p + UART_XR_EFR);
1297687911b3SMatthew Howell 
1298687911b3SMatthew Howell 		/* Set MCR to use DTR as Auto-RS485 Enable signal */
1299687911b3SMatthew Howell 		writeb(UART_MCR_OUT1, p + UART_MCR);
1300687911b3SMatthew Howell 
1301687911b3SMatthew Howell 		/* Set LCR[7]=1 to enable access to DLD register */
1302687911b3SMatthew Howell 		writeb(old_lcr | UART_LCR_DLAB, p + UART_LCR);
1303687911b3SMatthew Howell 
1304687911b3SMatthew Howell 		/* Set DLD[7]=1 for inverted RS485 Enable logic */
1305687911b3SMatthew Howell 		dld = readb(p + UART_EXAR_DLD);
1306687911b3SMatthew Howell 		dld |= UART_EXAR_DLD_485_POLARITY;
1307687911b3SMatthew Howell 		writeb(dld, p + UART_EXAR_DLD);
1308687911b3SMatthew Howell 
1309687911b3SMatthew Howell 		writeb(old_lcr, p + UART_LCR);
1310687911b3SMatthew Howell 	}
1311687911b3SMatthew Howell 
1312687911b3SMatthew Howell 	return 0;
1313687911b3SMatthew Howell }
1314687911b3SMatthew Howell 
13150d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
13160d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
131733969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
13189d939894SDaniel Golle 	.rs485_config = generic_rs485_config,
131959c221f8SIlpo Järvinen 	.rs485_supported = &generic_rs485_supported,
13200d963ebfSJan Kiszka };
13210d963ebfSJan Kiszka 
1322ae50bb27SIlpo Järvinen static int iot2040_rs485_config(struct uart_port *port, struct ktermios *termios,
1323413058dfSJan Kiszka 				struct serial_rs485 *rs485)
1324413058dfSJan Kiszka {
1325413058dfSJan Kiszka 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
1326413058dfSJan Kiszka 	u8 __iomem *p = port->membase;
1327413058dfSJan Kiszka 	u8 mask = IOT2040_UART1_MASK;
1328413058dfSJan Kiszka 	u8 mode, value;
1329413058dfSJan Kiszka 
1330413058dfSJan Kiszka 	if (is_rs485) {
1331413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_RX_DURING_TX)
1332413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS422;
1333413058dfSJan Kiszka 		else
1334413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS485;
1335413058dfSJan Kiszka 
1336413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_TERMINATE_BUS)
1337413058dfSJan Kiszka 			mode |= IOT2040_UART_TERMINATE_BUS;
1338413058dfSJan Kiszka 	} else {
1339413058dfSJan Kiszka 		mode = IOT2040_UART_MODE_RS232;
1340413058dfSJan Kiszka 	}
1341413058dfSJan Kiszka 
1342413058dfSJan Kiszka 	if (port->line == 3) {
1343413058dfSJan Kiszka 		mask <<= IOT2040_UART2_SHIFT;
1344413058dfSJan Kiszka 		mode <<= IOT2040_UART2_SHIFT;
1345413058dfSJan Kiszka 	}
1346413058dfSJan Kiszka 
1347413058dfSJan Kiszka 	value = readb(p + UART_EXAR_MPIOLVL_7_0);
1348413058dfSJan Kiszka 	value &= ~mask;
1349413058dfSJan Kiszka 	value |= mode;
1350413058dfSJan Kiszka 	writeb(value, p + UART_EXAR_MPIOLVL_7_0);
1351413058dfSJan Kiszka 
1352ae50bb27SIlpo Järvinen 	return generic_rs485_config(port, termios, rs485);
1353413058dfSJan Kiszka }
1354413058dfSJan Kiszka 
135559c221f8SIlpo Järvinen static const struct serial_rs485 iot2040_rs485_supported = {
13560c2a5f47SLino Sanfilippo 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
13570c2a5f47SLino Sanfilippo 		 SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
135859c221f8SIlpo Järvinen };
135959c221f8SIlpo Järvinen 
1360413058dfSJan Kiszka static const struct property_entry iot2040_gpio_properties[] = {
1361a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 10),
1362413058dfSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 1),
1363413058dfSJan Kiszka 	{ }
1364413058dfSJan Kiszka };
1365413058dfSJan Kiszka 
136681171e7dSHeikki Krogerus static const struct software_node iot2040_gpio_node = {
136781171e7dSHeikki Krogerus 	.properties = iot2040_gpio_properties,
136881171e7dSHeikki Krogerus };
136981171e7dSHeikki Krogerus 
1370413058dfSJan Kiszka static int iot2040_register_gpio(struct pci_dev *pcidev,
1371413058dfSJan Kiszka 			      struct uart_8250_port *port)
1372413058dfSJan Kiszka {
1373413058dfSJan Kiszka 	u8 __iomem *p = port->port.membase;
1374413058dfSJan Kiszka 
1375413058dfSJan Kiszka 	writeb(IOT2040_UARTS_DEFAULT_MODE, p + UART_EXAR_MPIOLVL_7_0);
1376413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_LO_MODE, p + UART_EXAR_MPIOSEL_7_0);
1377413058dfSJan Kiszka 	writeb(IOT2040_UARTS_ENABLE, p + UART_EXAR_MPIOLVL_15_8);
1378413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_HI_MODE, p + UART_EXAR_MPIOSEL_15_8);
1379413058dfSJan Kiszka 
1380413058dfSJan Kiszka 	port->port.private_data =
138181171e7dSHeikki Krogerus 		__xr17v35x_register_gpio(pcidev, &iot2040_gpio_node);
1382413058dfSJan Kiszka 
1383413058dfSJan Kiszka 	return 0;
1384413058dfSJan Kiszka }
1385413058dfSJan Kiszka 
1386413058dfSJan Kiszka static const struct exar8250_platform iot2040_platform = {
1387413058dfSJan Kiszka 	.rs485_config = iot2040_rs485_config,
138859c221f8SIlpo Järvinen 	.rs485_supported = &iot2040_rs485_supported,
1389413058dfSJan Kiszka 	.register_gpio = iot2040_register_gpio,
139033969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
1391413058dfSJan Kiszka };
1392413058dfSJan Kiszka 
13933e51ceeaSSu Bao Cheng /*
13943e51ceeaSSu Bao Cheng  * For SIMATIC IOT2000, only IOT2040 and its variants have the Exar device,
13953e51ceeaSSu Bao Cheng  * IOT2020 doesn't have. Therefore it is sufficient to match on the common
13963e51ceeaSSu Bao Cheng  * board name after the device was found.
13973e51ceeaSSu Bao Cheng  */
1398413058dfSJan Kiszka static const struct dmi_system_id exar_platforms[] = {
1399413058dfSJan Kiszka 	{
1400413058dfSJan Kiszka 		.matches = {
1401413058dfSJan Kiszka 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
1402413058dfSJan Kiszka 		},
1403413058dfSJan Kiszka 		.driver_data = (void *)&iot2040_platform,
1404413058dfSJan Kiszka 	},
1405413058dfSJan Kiszka 	{}
1406413058dfSJan Kiszka };
1407413058dfSJan Kiszka 
14087d356a43SAndy Shevchenko static const struct exar8250_platform *exar_get_platform(void)
14097d356a43SAndy Shevchenko {
14107d356a43SAndy Shevchenko 	const struct dmi_system_id *dmi_match;
14117d356a43SAndy Shevchenko 
14127d356a43SAndy Shevchenko 	dmi_match = dmi_first_match(exar_platforms);
14137d356a43SAndy Shevchenko 	if (dmi_match)
14147d356a43SAndy Shevchenko 		return dmi_match->driver_data;
14157d356a43SAndy Shevchenko 
14167d356a43SAndy Shevchenko 	return &exar8250_default_platform;
14177d356a43SAndy Shevchenko }
14187d356a43SAndy Shevchenko 
1419d0aeaa83SSudip Mukherjee static int
1420d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
1421d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
1422d0aeaa83SSudip Mukherjee {
14237d356a43SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
1424d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
1425d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
1426d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
1427d0aeaa83SSudip Mukherjee 	int ret;
1428d0aeaa83SSudip Mukherjee 
1429d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
14300d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
14310139da50SIlpo Järvinen 	port->port.rs485_supported = *(platform->rs485_supported);
14320d963ebfSJan Kiszka 
1433687911b3SMatthew Howell 	if (pcidev->subsystem_vendor == PCI_VENDOR_ID_SEALEVEL)
1434687911b3SMatthew Howell 		port->port.rs485_config = sealevel_rs485_config;
1435687911b3SMatthew Howell 
1436d0aeaa83SSudip Mukherjee 	/*
1437328c11f2SAndy Shevchenko 	 * Setup the UART clock for the devices on expansion slot to
1438d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
1439d0aeaa83SSudip Mukherjee 	 */
1440328c11f2SAndy Shevchenko 	if (idx >= 8)
1441d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
1442d0aeaa83SSudip Mukherjee 
14435b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
14445b5f252dSJan Kiszka 	if (ret)
14455b5f252dSJan Kiszka 		return ret;
1446d0aeaa83SSudip Mukherjee 
14475b5f252dSJan Kiszka 	p = port->port.membase;
1448d0aeaa83SSudip Mukherjee 
1449d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
1450d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
1451d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
1452d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
1453d0aeaa83SSudip Mukherjee 
14545b5f252dSJan Kiszka 	if (idx == 0) {
14555b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
1456bea8be65SJan Kiszka 		setup_gpio(pcidev, p);
1457d0aeaa83SSudip Mukherjee 
14580d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
14595b5f252dSJan Kiszka 	}
1460d0aeaa83SSudip Mukherjee 
14610d963ebfSJan Kiszka 	return ret;
1462d0aeaa83SSudip Mukherjee }
1463d0aeaa83SSudip Mukherjee 
1464d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
1465d0aeaa83SSudip Mukherjee {
146633969db7SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
1467d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
1468d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
14697c3e8d9dSAndy Shevchenko 
147033969db7SAndy Shevchenko 	platform->unregister_gpio(port);
1471d0aeaa83SSudip Mukherjee }
1472d0aeaa83SSudip Mukherjee 
147372169e42SAaron Sierra static inline void exar_misc_clear(struct exar8250 *priv)
147472169e42SAaron Sierra {
147572169e42SAaron Sierra 	/* Clear all PCI interrupts by reading INT0. No effect on IIR */
147672169e42SAaron Sierra 	readb(priv->virt + UART_EXAR_INT0);
147772169e42SAaron Sierra 
147872169e42SAaron Sierra 	/* Clear INT0 for Expansion Interface slave ports, too */
147972169e42SAaron Sierra 	if (priv->board->num_ports > 8)
148072169e42SAaron Sierra 		readb(priv->virt + 0x2000 + UART_EXAR_INT0);
148172169e42SAaron Sierra }
148272169e42SAaron Sierra 
1483c7e1b405SAaron Sierra /*
1484c7e1b405SAaron Sierra  * These Exar UARTs have an extra interrupt indicator that could fire for a
1485c7e1b405SAaron Sierra  * few interrupts that are not presented/cleared through IIR.  One of which is
1486c7e1b405SAaron Sierra  * a wakeup interrupt when coming out of sleep.  These interrupts are only
1487c7e1b405SAaron Sierra  * cleared by reading global INT0 or INT1 registers as interrupts are
1488c7e1b405SAaron Sierra  * associated with channel 0. The INT[3:0] registers _are_ accessible from each
1489c7e1b405SAaron Sierra  * channel's address space, but for the sake of bus efficiency we register a
1490c7e1b405SAaron Sierra  * dedicated handler at the PCI device level to handle them.
1491c7e1b405SAaron Sierra  */
1492c7e1b405SAaron Sierra static irqreturn_t exar_misc_handler(int irq, void *data)
1493c7e1b405SAaron Sierra {
149472169e42SAaron Sierra 	exar_misc_clear(data);
1495c7e1b405SAaron Sierra 
1496c7e1b405SAaron Sierra 	return IRQ_HANDLED;
1497c7e1b405SAaron Sierra }
1498c7e1b405SAaron Sierra 
1499477f6ee6SParker Newman static unsigned int exar_get_nr_ports(struct exar8250_board *board,
1500477f6ee6SParker Newman 					struct pci_dev *pcidev)
1501477f6ee6SParker Newman {
1502477f6ee6SParker Newman 	unsigned int nr_ports = 0;
1503477f6ee6SParker Newman 
15045aa84fd8SParker Newman 	if (pcidev->vendor == PCI_VENDOR_ID_ACCESSIO) {
1505477f6ee6SParker Newman 		nr_ports = BIT(((pcidev->device & 0x38) >> 3) - 1);
15065aa84fd8SParker Newman 	} else if (board->num_ports > 0) {
15075aa84fd8SParker Newman 		// Check if board struct overrides number of ports
1508477f6ee6SParker Newman 		nr_ports = board->num_ports;
15095aa84fd8SParker Newman 	} else if (pcidev->vendor == PCI_VENDOR_ID_EXAR) {
15105aa84fd8SParker Newman 		// Exar encodes # ports in last nibble of PCI Device ID ex. 0358
1511477f6ee6SParker Newman 		nr_ports = pcidev->device & 0x0f;
15125aa84fd8SParker Newman 	} else  if (pcidev->vendor == PCI_VENDOR_ID_CONNECT_TECH) {
15135aa84fd8SParker Newman 		// Handle CTI FPGA cards
15145aa84fd8SParker Newman 		switch (pcidev->device) {
15155aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG00X:
15165aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_12_XIG01X:
15175aa84fd8SParker Newman 			nr_ports = 12;
15185aa84fd8SParker Newman 			break;
15195aa84fd8SParker Newman 		case PCI_DEVICE_ID_CONNECT_TECH_PCI_XR79X_16:
15205aa84fd8SParker Newman 			nr_ports = 16;
15215aa84fd8SParker Newman 			break;
15225aa84fd8SParker Newman 		default:
15235aa84fd8SParker Newman 			break;
15245aa84fd8SParker Newman 		}
15255aa84fd8SParker Newman 	}
1526477f6ee6SParker Newman 
1527477f6ee6SParker Newman 	return nr_ports;
1528477f6ee6SParker Newman }
1529477f6ee6SParker Newman 
1530d0aeaa83SSudip Mukherjee static int
1531d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
1532d0aeaa83SSudip Mukherjee {
1533d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
1534d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
1535d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
1536d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
1537d0aeaa83SSudip Mukherjee 	int rc;
1538d0aeaa83SSudip Mukherjee 
1539d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
1540d0aeaa83SSudip Mukherjee 	if (!board)
1541d0aeaa83SSudip Mukherjee 		return -EINVAL;
1542d0aeaa83SSudip Mukherjee 
1543d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
1544d0aeaa83SSudip Mukherjee 	if (rc)
1545d0aeaa83SSudip Mukherjee 		return rc;
1546d0aeaa83SSudip Mukherjee 
1547d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
1548d0aeaa83SSudip Mukherjee 
1549477f6ee6SParker Newman 	nr_ports = exar_get_nr_ports(board, pcidev);
1550477f6ee6SParker Newman 	if (nr_ports == 0) {
1551477f6ee6SParker Newman 		dev_err_probe(&pcidev->dev, -ENODEV,
1552477f6ee6SParker Newman 				"failed to get number of ports\n");
1553477f6ee6SParker Newman 		return -ENODEV;
1554477f6ee6SParker Newman 	}
1555d0aeaa83SSudip Mukherjee 
1556df60a8afSAndy Shevchenko 	priv = devm_kzalloc(&pcidev->dev, struct_size(priv, line, nr_ports), GFP_KERNEL);
1557d0aeaa83SSudip Mukherjee 	if (!priv)
1558d0aeaa83SSudip Mukherjee 		return -ENOMEM;
1559d0aeaa83SSudip Mukherjee 
1560d0aeaa83SSudip Mukherjee 	priv->board = board;
1561c7e1b405SAaron Sierra 	priv->virt = pcim_iomap(pcidev, bar, 0);
1562c7e1b405SAaron Sierra 	if (!priv->virt)
1563c7e1b405SAaron Sierra 		return -ENOMEM;
1564d0aeaa83SSudip Mukherjee 
1565172c33cbSJan Kiszka 	pci_set_master(pcidev);
1566172c33cbSJan Kiszka 
1567172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
1568172c33cbSJan Kiszka 	if (rc < 0)
1569172c33cbSJan Kiszka 		return rc;
1570172c33cbSJan Kiszka 
1571d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
15726be254c2SAndy Shevchenko 	uart.port.flags = UPF_SHARE_IRQ | UPF_EXAR_EFR | UPF_FIXED_TYPE | UPF_FIXED_PORT;
1573172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
1574d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
1575d0aeaa83SSudip Mukherjee 
15765bc430afSAndy Shevchenko 	/* Clear interrupts */
15775bc430afSAndy Shevchenko 	exar_misc_clear(priv);
15785bc430afSAndy Shevchenko 
1579c7e1b405SAaron Sierra 	rc = devm_request_irq(&pcidev->dev, uart.port.irq, exar_misc_handler,
1580c7e1b405SAaron Sierra 			 IRQF_SHARED, "exar_uart", priv);
1581c7e1b405SAaron Sierra 	if (rc)
1582c7e1b405SAaron Sierra 		return rc;
1583c7e1b405SAaron Sierra 
1584393b520aSParker Newman 	if (board->board_init) {
1585393b520aSParker Newman 		rc = board->board_init(priv, pcidev);
1586393b520aSParker Newman 		if (rc) {
1587393b520aSParker Newman 			dev_err_probe(&pcidev->dev, rc,
1588393b520aSParker Newman 					"failed to init serial board\n");
1589393b520aSParker Newman 			return rc;
1590393b520aSParker Newman 		}
1591393b520aSParker Newman 	}
1592393b520aSParker Newman 
1593d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
1594d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
1595d0aeaa83SSudip Mukherjee 		if (rc) {
1596d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
1597d0aeaa83SSudip Mukherjee 			break;
1598d0aeaa83SSudip Mukherjee 		}
1599d0aeaa83SSudip Mukherjee 
1600d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
1601d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
1602d0aeaa83SSudip Mukherjee 
1603d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
1604d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
1605d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
1606d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
1607d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
1608d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
1609d0aeaa83SSudip Mukherjee 			break;
1610d0aeaa83SSudip Mukherjee 		}
1611d0aeaa83SSudip Mukherjee 	}
1612d0aeaa83SSudip Mukherjee 	priv->nr = i;
1613d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
1614d0aeaa83SSudip Mukherjee 	return 0;
1615d0aeaa83SSudip Mukherjee }
1616d0aeaa83SSudip Mukherjee 
1617d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
1618d0aeaa83SSudip Mukherjee {
1619d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
1620d0aeaa83SSudip Mukherjee 	unsigned int i;
1621d0aeaa83SSudip Mukherjee 
1622d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1623d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
1624d0aeaa83SSudip Mukherjee 
162573b5a5c0SAndy Shevchenko 	/* Ensure that every init quirk is properly torn down */
1626d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
1627d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
1628d0aeaa83SSudip Mukherjee }
1629d0aeaa83SSudip Mukherjee 
163082f9cefaSAndy Shevchenko static int exar_suspend(struct device *dev)
1631d0aeaa83SSudip Mukherjee {
16327a345dc1SAndy Shevchenko 	struct exar8250 *priv = dev_get_drvdata(dev);
1633d0aeaa83SSudip Mukherjee 	unsigned int i;
1634d0aeaa83SSudip Mukherjee 
1635d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1636d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
1637d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
1638d0aeaa83SSudip Mukherjee 
1639d0aeaa83SSudip Mukherjee 	return 0;
1640d0aeaa83SSudip Mukherjee }
1641d0aeaa83SSudip Mukherjee 
164282f9cefaSAndy Shevchenko static int exar_resume(struct device *dev)
1643d0aeaa83SSudip Mukherjee {
164476b4106cSChuhong Yuan 	struct exar8250 *priv = dev_get_drvdata(dev);
1645d0aeaa83SSudip Mukherjee 	unsigned int i;
1646d0aeaa83SSudip Mukherjee 
164772169e42SAaron Sierra 	exar_misc_clear(priv);
164872169e42SAaron Sierra 
1649d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
1650d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
1651d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
1652d0aeaa83SSudip Mukherjee 
1653d0aeaa83SSudip Mukherjee 	return 0;
1654d0aeaa83SSudip Mukherjee }
1655d0aeaa83SSudip Mukherjee 
165682f9cefaSAndy Shevchenko static DEFINE_SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
1657d0aeaa83SSudip Mukherjee 
1658fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
1659fc6cc961SJan Kiszka 	.num_ports	= 2,
1660fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1661fc6cc961SJan Kiszka };
1662fc6cc961SJan Kiszka 
1663fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
1664fc6cc961SJan Kiszka 	.num_ports	= 4,
1665fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1666fc6cc961SJan Kiszka };
1667fc6cc961SJan Kiszka 
1668fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
1669fc6cc961SJan Kiszka 	.num_ports	= 8,
1670fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
1671fc6cc961SJan Kiszka };
1672fc6cc961SJan Kiszka 
1673*f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17c15x = {
1674*f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17c15x,
1675*f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17c15x,
1676*f7ce0706SParker Newman };
1677*f7ce0706SParker Newman 
1678*f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17v25x = {
1679*f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17v25x,
1680*f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17v25x,
1681*f7ce0706SParker Newman };
1682*f7ce0706SParker Newman 
1683*f7ce0706SParker Newman static const struct exar8250_board pbn_cti_xr17v35x = {
1684*f7ce0706SParker Newman 	.board_init	= cti_board_init_xr17v35x,
1685*f7ce0706SParker Newman 	.setup		= cti_port_setup_xr17v35x,
1686*f7ce0706SParker Newman };
1687*f7ce0706SParker Newman 
1688*f7ce0706SParker Newman static const struct exar8250_board pbn_cti_fpga = {
1689*f7ce0706SParker Newman 	.board_init	= cti_board_init_fpga,
1690*f7ce0706SParker Newman 	.setup		= cti_port_setup_fpga,
1691*f7ce0706SParker Newman };
1692*f7ce0706SParker Newman 
1693d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
1694d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
1695d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
1696d0aeaa83SSudip Mukherjee };
1697d0aeaa83SSudip Mukherjee 
1698d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
1699d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
1700d0aeaa83SSudip Mukherjee };
1701d0aeaa83SSudip Mukherjee 
1702d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
1703d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1704d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1705d0aeaa83SSudip Mukherjee };
1706d0aeaa83SSudip Mukherjee 
1707c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_2 = {
1708c6b9e95dSValmer Huhn 	.num_ports	= 2,
1709c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1710c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1711c6b9e95dSValmer Huhn };
1712c6b9e95dSValmer Huhn 
1713c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_4 = {
1714c6b9e95dSValmer Huhn 	.num_ports	= 4,
1715c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1716c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1717c6b9e95dSValmer Huhn };
1718c6b9e95dSValmer Huhn 
1719c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_8 = {
1720c6b9e95dSValmer Huhn 	.num_ports	= 8,
1721c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
1722c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
1723c6b9e95dSValmer Huhn };
1724c6b9e95dSValmer Huhn 
1725d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
1726d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
1727d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1728d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1729d0aeaa83SSudip Mukherjee };
1730d0aeaa83SSudip Mukherjee 
1731d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
1732d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
1733d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
1734d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
1735d0aeaa83SSudip Mukherjee };
1736d0aeaa83SSudip Mukherjee 
1737*f7ce0706SParker Newman // For Connect Tech cards with Exar vendor/device PCI IDs
1738*f7ce0706SParker Newman #define CTI_EXAR_DEVICE(devid, bd) {                    \
1739*f7ce0706SParker Newman 	PCI_DEVICE_SUB(                                 \
1740*f7ce0706SParker Newman 		PCI_VENDOR_ID_EXAR,                     \
1741*f7ce0706SParker Newman 		PCI_DEVICE_ID_EXAR_##devid,             \
1742*f7ce0706SParker Newman 		PCI_SUBVENDOR_ID_CONNECT_TECH,          \
1743*f7ce0706SParker Newman 		PCI_ANY_ID), 0, 0,                      \
1744*f7ce0706SParker Newman 		(kernel_ulong_t)&bd                     \
1745*f7ce0706SParker Newman 	}
1746*f7ce0706SParker Newman 
1747*f7ce0706SParker Newman // For Connect Tech cards with Connect Tech vendor/device PCI IDs (FPGA based)
1748*f7ce0706SParker Newman #define CTI_PCI_DEVICE(devid, bd) {                     \
1749*f7ce0706SParker Newman 	PCI_DEVICE_SUB(                                 \
1750*f7ce0706SParker Newman 		PCI_VENDOR_ID_CONNECT_TECH,             \
1751*f7ce0706SParker Newman 		PCI_DEVICE_ID_CONNECT_TECH_PCI_##devid, \
1752*f7ce0706SParker Newman 		PCI_ANY_ID,                             \
1753*f7ce0706SParker Newman 		PCI_ANY_ID), 0, 0,                      \
1754*f7ce0706SParker Newman 		(kernel_ulong_t)&bd                     \
1755*f7ce0706SParker Newman 	}
1756*f7ce0706SParker Newman 
175724637007SAndy Shevchenko #define EXAR_DEVICE(vend, devid, bd) { PCI_DEVICE_DATA(vend, devid, &bd) }
1758d0aeaa83SSudip Mukherjee 
1759d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
1760d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
1761d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
1762d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
1763d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_IBM,			\
1764d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
1765d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
1766d0aeaa83SSudip Mukherjee 	}
1767d0aeaa83SSudip Mukherjee 
176895d69886SAndrew Davis #define USR_DEVICE(devid, sdevid, bd) {			\
176995d69886SAndrew Davis 	PCI_DEVICE_SUB(					\
177095d69886SAndrew Davis 		PCI_VENDOR_ID_USR,			\
177195d69886SAndrew Davis 		PCI_DEVICE_ID_EXAR_##devid,		\
177295d69886SAndrew Davis 		PCI_VENDOR_ID_EXAR,			\
177395d69886SAndrew Davis 		PCI_SUBDEVICE_ID_USR_##sdevid), 0, 0,	\
177495d69886SAndrew Davis 		(kernel_ulong_t)&bd			\
177595d69886SAndrew Davis 	}
177695d69886SAndrew Davis 
17773637c460SArvind Yadav static const struct pci_device_id exar_pci_tbl[] = {
17788e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2S, pbn_exar_XR17C15x),
17798e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4S, pbn_exar_XR17C15x),
17808e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8S, pbn_exar_XR17C15x),
17818e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM232_8, pbn_exar_XR17C15x),
17828e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2SM, pbn_exar_XR17C15x),
17838e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4SM, pbn_exar_XR17C15x),
17848e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8SM, pbn_exar_XR17C15x),
178510c5ccc3SJay Dolan 
1786*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C152,       pbn_cti_xr17c15x),
1787*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C154,       pbn_cti_xr17c15x),
1788*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17C158,       pbn_cti_xr17c15x),
1789*f7ce0706SParker Newman 
1790*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V252,       pbn_cti_xr17v25x),
1791*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V254,       pbn_cti_xr17v25x),
1792*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V258,       pbn_cti_xr17v25x),
1793*f7ce0706SParker Newman 
1794*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V352,       pbn_cti_xr17v35x),
1795*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V354,       pbn_cti_xr17v35x),
1796*f7ce0706SParker Newman 	CTI_EXAR_DEVICE(XR17V358,       pbn_cti_xr17v35x),
1797*f7ce0706SParker Newman 
1798*f7ce0706SParker Newman 	CTI_PCI_DEVICE(XR79X_12_XIG00X, pbn_cti_fpga),
1799*f7ce0706SParker Newman 	CTI_PCI_DEVICE(XR79X_12_XIG01X, pbn_cti_fpga),
1800*f7ce0706SParker Newman 	CTI_PCI_DEVICE(XR79X_16,        pbn_cti_fpga),
1801*f7ce0706SParker Newman 
1802d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
1803d0aeaa83SSudip Mukherjee 
180495d69886SAndrew Davis 	/* USRobotics USR298x-OEM PCI Modems */
180595d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2980, pbn_exar_XR17C15x),
180695d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2981, pbn_exar_XR17C15x),
180795d69886SAndrew Davis 
1808d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
180924637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C152, pbn_exar_XR17C15x),
181024637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C154, pbn_exar_XR17C15x),
181124637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C158, pbn_exar_XR17C15x),
1812d0aeaa83SSudip Mukherjee 
1813d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
181424637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V352, pbn_exar_XR17V35x),
181524637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V354, pbn_exar_XR17V35x),
181624637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V358, pbn_exar_XR17V35x),
181724637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V4358, pbn_exar_XR17V4358),
181824637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V8358, pbn_exar_XR17V8358),
1819c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4222PCIE, pbn_fastcom35x_2),
1820c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4224PCIE, pbn_fastcom35x_4),
1821c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4228PCIE, pbn_fastcom35x_8),
1822fc6cc961SJan Kiszka 
182324637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4222PCI335, pbn_fastcom335_2),
182424637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4224PCI335, pbn_fastcom335_4),
182524637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2324PCI335, pbn_fastcom335_4),
182624637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2328PCI335, pbn_fastcom335_8),
1827d0aeaa83SSudip Mukherjee 	{ 0, }
1828d0aeaa83SSudip Mukherjee };
1829d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
1830d0aeaa83SSudip Mukherjee 
1831d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
1832d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
1833d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
1834d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
1835d0aeaa83SSudip Mukherjee 	.driver         = {
183682f9cefaSAndy Shevchenko 		.pm     = pm_sleep_ptr(&exar_pci_pm),
1837d0aeaa83SSudip Mukherjee 	},
1838d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
1839d0aeaa83SSudip Mukherjee };
1840d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
1841d0aeaa83SSudip Mukherjee 
1842d813d900SAndy Shevchenko MODULE_IMPORT_NS(SERIAL_8250_PCI);
1843d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
18442b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
1845d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
1846