xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision 2b71b31f203b7b518b0a13316bd0294126905497)
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  */
94076cf08SJan Kiszka #include <linux/acpi.h>
10413058dfSJan Kiszka #include <linux/dmi.h>
11d0aeaa83SSudip Mukherjee #include <linux/io.h>
12d0aeaa83SSudip Mukherjee #include <linux/kernel.h>
13d0aeaa83SSudip Mukherjee #include <linux/module.h>
14d0aeaa83SSudip Mukherjee #include <linux/pci.h>
15380b1e2fSJan Kiszka #include <linux/property.h>
16d0aeaa83SSudip Mukherjee #include <linux/serial_core.h>
17d0aeaa83SSudip Mukherjee #include <linux/serial_reg.h>
18d0aeaa83SSudip Mukherjee #include <linux/slab.h>
19d0aeaa83SSudip Mukherjee #include <linux/string.h>
20d0aeaa83SSudip Mukherjee #include <linux/tty.h>
2147b1747fSRobert Middleton #include <linux/delay.h>
22d0aeaa83SSudip Mukherjee 
23d0aeaa83SSudip Mukherjee #include <asm/byteorder.h>
24d0aeaa83SSudip Mukherjee 
25d0aeaa83SSudip Mukherjee #include "8250.h"
26d0aeaa83SSudip Mukherjee 
2724637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2S		0x1052
2824637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4S		0x105d
2924637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8S		0x106c
3024637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM232_8		0x10a8
3124637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2SM		0x10d2
3224637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4SM		0x10db
3324637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8SM		0x10ea
3410c5ccc3SJay Dolan 
35fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4224PCI335	0x0002
36fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4222PCI335	0x0004
37fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2324PCI335	0x000a
38fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2328PCI335	0x000b
39d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4224PCIE		0x0020
40d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4228PCIE		0x0021
41d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4222PCIE		0x0022
4295d69886SAndrew Davis 
43d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V4358		0x4358
44d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V8358		0x8358
45d0aeaa83SSudip Mukherjee 
4695d69886SAndrew Davis #define PCI_SUBDEVICE_ID_USR_2980		0x0128
4795d69886SAndrew Davis #define PCI_SUBDEVICE_ID_USR_2981		0x0129
4895d69886SAndrew Davis 
4914ee78d5SMatthew Howell #define PCI_DEVICE_ID_SEALEVEL_710xC		0x1001
5014ee78d5SMatthew Howell #define PCI_DEVICE_ID_SEALEVEL_720xC		0x1002
5114ee78d5SMatthew Howell #define PCI_DEVICE_ID_SEALEVEL_740xC		0x1004
5214ee78d5SMatthew Howell #define PCI_DEVICE_ID_SEALEVEL_780xC		0x1008
5314ee78d5SMatthew Howell #define PCI_DEVICE_ID_SEALEVEL_716xC		0x1010
5414ee78d5SMatthew Howell 
55c7e1b405SAaron Sierra #define UART_EXAR_INT0		0x80
567e12357eSJan Kiszka #define UART_EXAR_8XMODE	0x88	/* 8X sampling rate select */
57ef4e281eSAndy Shevchenko #define UART_EXAR_SLEEP		0x8b	/* Sleep mode */
586be254c2SAndy Shevchenko #define UART_EXAR_DVID		0x8d	/* Device identification */
597e12357eSJan Kiszka 
607e12357eSJan Kiszka #define UART_EXAR_FCTR		0x08	/* Feature Control Register */
617e12357eSJan Kiszka #define UART_FCTR_EXAR_IRDA	0x10	/* IrDa data encode select */
627e12357eSJan Kiszka #define UART_FCTR_EXAR_485	0x20	/* Auto 485 half duplex dir ctl */
637e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGA	0x00	/* FIFO trigger table A */
647e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGB	0x60	/* FIFO trigger table B */
657e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGC	0x80	/* FIFO trigger table C */
667e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGD	0xc0	/* FIFO trigger table D programmable */
677e12357eSJan Kiszka 
687e12357eSJan Kiszka #define UART_EXAR_TXTRG		0x0a	/* Tx FIFO trigger level write-only */
697e12357eSJan Kiszka #define UART_EXAR_RXTRG		0x0b	/* Rx FIFO trigger level write-only */
707e12357eSJan Kiszka 
71d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_7_0	0x8f	/* MPIOINT[7:0] */
72d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_7_0	0x90	/* MPIOLVL[7:0] */
73d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_7_0	0x91	/* MPIO3T[7:0] */
74d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_7_0	0x92	/* MPIOINV[7:0] */
75d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_7_0	0x93	/* MPIOSEL[7:0] */
76d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_7_0	0x94	/* MPIOOD[7:0] */
77d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_15_8	0x95	/* MPIOINT[15:8] */
78d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_15_8	0x96	/* MPIOLVL[15:8] */
79d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_15_8	0x97	/* MPIO3T[15:8] */
80d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_15_8	0x98	/* MPIOINV[15:8] */
81d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_15_8	0x99	/* MPIOSEL[15:8] */
82d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_15_8	0x9a	/* MPIOOD[15:8] */
83d0aeaa83SSudip Mukherjee 
84413058dfSJan Kiszka #define UART_EXAR_RS485_DLY(x)	((x) << 4)
85413058dfSJan Kiszka 
86413058dfSJan Kiszka /*
87413058dfSJan Kiszka  * IOT2040 MPIO wiring semantics:
88413058dfSJan Kiszka  *
89413058dfSJan Kiszka  * MPIO		Port	Function
90413058dfSJan Kiszka  * ----		----	--------
91413058dfSJan Kiszka  * 0		2 	Mode bit 0
92413058dfSJan Kiszka  * 1		2	Mode bit 1
93413058dfSJan Kiszka  * 2		2	Terminate bus
94413058dfSJan Kiszka  * 3		-	<reserved>
95413058dfSJan Kiszka  * 4		3	Mode bit 0
96413058dfSJan Kiszka  * 5		3	Mode bit 1
97413058dfSJan Kiszka  * 6		3	Terminate bus
98413058dfSJan Kiszka  * 7		-	<reserved>
99413058dfSJan Kiszka  * 8		2	Enable
100413058dfSJan Kiszka  * 9		3	Enable
101413058dfSJan Kiszka  * 10		-	Red LED
102413058dfSJan Kiszka  * 11..15	-	<unused>
103413058dfSJan Kiszka  */
104413058dfSJan Kiszka 
105413058dfSJan Kiszka /* IOT2040 MPIOs 0..7 */
106413058dfSJan Kiszka #define IOT2040_UART_MODE_RS232		0x01
107413058dfSJan Kiszka #define IOT2040_UART_MODE_RS485		0x02
108413058dfSJan Kiszka #define IOT2040_UART_MODE_RS422		0x03
109413058dfSJan Kiszka #define IOT2040_UART_TERMINATE_BUS	0x04
110413058dfSJan Kiszka 
111413058dfSJan Kiszka #define IOT2040_UART1_MASK		0x0f
112413058dfSJan Kiszka #define IOT2040_UART2_SHIFT		4
113413058dfSJan Kiszka 
114413058dfSJan Kiszka #define IOT2040_UARTS_DEFAULT_MODE	0x11	/* both RS232 */
115413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_LO_MODE	0x88	/* reserved pins as input */
116413058dfSJan Kiszka 
117413058dfSJan Kiszka /* IOT2040 MPIOs 8..15 */
118413058dfSJan Kiszka #define IOT2040_UARTS_ENABLE		0x03
119413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_HI_MODE	0xF8	/* enable & LED as outputs */
120413058dfSJan Kiszka 
121d0aeaa83SSudip Mukherjee struct exar8250;
122d0aeaa83SSudip Mukherjee 
1230d963ebfSJan Kiszka struct exar8250_platform {
124ae50bb27SIlpo Järvinen 	int (*rs485_config)(struct uart_port *port, struct ktermios *termios,
125ae50bb27SIlpo Järvinen 			    struct serial_rs485 *rs485);
12659c221f8SIlpo Järvinen 	const struct serial_rs485 *rs485_supported;
1270d963ebfSJan Kiszka 	int (*register_gpio)(struct pci_dev *, struct uart_8250_port *);
12833969db7SAndy Shevchenko 	void (*unregister_gpio)(struct uart_8250_port *);
1290d963ebfSJan Kiszka };
1300d963ebfSJan Kiszka 
131d0aeaa83SSudip Mukherjee /**
132d0aeaa83SSudip Mukherjee  * struct exar8250_board - board information
133d0aeaa83SSudip Mukherjee  * @num_ports: number of serial ports
134d0aeaa83SSudip Mukherjee  * @reg_shift: describes UART register mapping in PCI memory
13526f22d57SAndy Shevchenko  * @setup: quirk run at ->probe() stage
13626f22d57SAndy Shevchenko  * @exit: quirk run at ->remove() stage
137d0aeaa83SSudip Mukherjee  */
138d0aeaa83SSudip Mukherjee struct exar8250_board {
139d0aeaa83SSudip Mukherjee 	unsigned int num_ports;
140d0aeaa83SSudip Mukherjee 	unsigned int reg_shift;
141d0aeaa83SSudip Mukherjee 	int	(*setup)(struct exar8250 *, struct pci_dev *,
142d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *, int);
143d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
144d0aeaa83SSudip Mukherjee };
145d0aeaa83SSudip Mukherjee 
146d0aeaa83SSudip Mukherjee struct exar8250 {
147d0aeaa83SSudip Mukherjee 	unsigned int		nr;
148d0aeaa83SSudip Mukherjee 	struct exar8250_board	*board;
149c7e1b405SAaron Sierra 	void __iomem		*virt;
15000d963abSGustavo A. R. Silva 	int			line[];
151d0aeaa83SSudip Mukherjee };
152d0aeaa83SSudip Mukherjee 
153ef4e281eSAndy Shevchenko static void exar_pm(struct uart_port *port, unsigned int state, unsigned int old)
154ef4e281eSAndy Shevchenko {
155ef4e281eSAndy Shevchenko 	/*
156ef4e281eSAndy Shevchenko 	 * Exar UARTs have a SLEEP register that enables or disables each UART
157ef4e281eSAndy Shevchenko 	 * to enter sleep mode separately. On the XR17V35x the register
158ef4e281eSAndy Shevchenko 	 * is accessible to each UART at the UART_EXAR_SLEEP offset, but
159ef4e281eSAndy Shevchenko 	 * the UART channel may only write to the corresponding bit.
160ef4e281eSAndy Shevchenko 	 */
161ef4e281eSAndy Shevchenko 	serial_port_out(port, UART_EXAR_SLEEP, state ? 0xff : 0);
162ef4e281eSAndy Shevchenko }
163ef4e281eSAndy Shevchenko 
164b2b4b8edSAndy Shevchenko /*
165b2b4b8edSAndy Shevchenko  * XR17V35x UARTs have an extra fractional divisor register (DLD)
166b2b4b8edSAndy Shevchenko  * Calculate divisor with extra 4-bit fractional portion
167b2b4b8edSAndy Shevchenko  */
168b2b4b8edSAndy Shevchenko static unsigned int xr17v35x_get_divisor(struct uart_port *p, unsigned int baud,
169b2b4b8edSAndy Shevchenko 					 unsigned int *frac)
170b2b4b8edSAndy Shevchenko {
171b2b4b8edSAndy Shevchenko 	unsigned int quot_16;
172b2b4b8edSAndy Shevchenko 
173b2b4b8edSAndy Shevchenko 	quot_16 = DIV_ROUND_CLOSEST(p->uartclk, baud);
174b2b4b8edSAndy Shevchenko 	*frac = quot_16 & 0x0f;
175b2b4b8edSAndy Shevchenko 
176b2b4b8edSAndy Shevchenko 	return quot_16 >> 4;
177b2b4b8edSAndy Shevchenko }
178b2b4b8edSAndy Shevchenko 
179b2b4b8edSAndy Shevchenko static void xr17v35x_set_divisor(struct uart_port *p, unsigned int baud,
180b2b4b8edSAndy Shevchenko 				 unsigned int quot, unsigned int quot_frac)
181b2b4b8edSAndy Shevchenko {
182b2b4b8edSAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
183b2b4b8edSAndy Shevchenko 
184b2b4b8edSAndy Shevchenko 	/* Preserve bits not related to baudrate; DLD[7:4]. */
185b2b4b8edSAndy Shevchenko 	quot_frac |= serial_port_in(p, 0x2) & 0xf0;
186b2b4b8edSAndy Shevchenko 	serial_port_out(p, 0x2, quot_frac);
187b2b4b8edSAndy Shevchenko }
188b2b4b8edSAndy Shevchenko 
1896e731137SAndy Shevchenko static int xr17v35x_startup(struct uart_port *port)
1906e731137SAndy Shevchenko {
1916e731137SAndy Shevchenko 	/*
1926e731137SAndy Shevchenko 	 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
1936e731137SAndy Shevchenko 	 * MCR [7:5] and MSR [7:0]
1946e731137SAndy Shevchenko 	 */
1956e731137SAndy Shevchenko 	serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
1966e731137SAndy Shevchenko 
1976e731137SAndy Shevchenko 	/*
1986e731137SAndy Shevchenko 	 * Make sure all interrups are masked until initialization is
1996e731137SAndy Shevchenko 	 * complete and the FIFOs are cleared
200b1207d86SJohn Ogness 	 *
201b1207d86SJohn Ogness 	 * Synchronize UART_IER access against the console.
2026e731137SAndy Shevchenko 	 */
203*2b71b31fSThomas Gleixner 	uart_port_lock_irq(port);
2046e731137SAndy Shevchenko 	serial_port_out(port, UART_IER, 0);
205*2b71b31fSThomas Gleixner 	uart_port_unlock_irq(port);
2066e731137SAndy Shevchenko 
2076e731137SAndy Shevchenko 	return serial8250_do_startup(port);
2086e731137SAndy Shevchenko }
2096e731137SAndy Shevchenko 
210653d00c8SAndy Shevchenko static void exar_shutdown(struct uart_port *port)
211653d00c8SAndy Shevchenko {
21267e977f3SZheng Bin 	bool tx_complete = false;
213653d00c8SAndy Shevchenko 	struct uart_8250_port *up = up_to_u8250p(port);
214653d00c8SAndy Shevchenko 	struct circ_buf *xmit = &port->state->xmit;
215653d00c8SAndy Shevchenko 	int i = 0;
216f8ba5680SIlpo Järvinen 	u16 lsr;
217653d00c8SAndy Shevchenko 
218653d00c8SAndy Shevchenko 	do {
219653d00c8SAndy Shevchenko 		lsr = serial_in(up, UART_LSR);
220653d00c8SAndy Shevchenko 		if (lsr & (UART_LSR_TEMT | UART_LSR_THRE))
22167e977f3SZheng Bin 			tx_complete = true;
222653d00c8SAndy Shevchenko 		else
22367e977f3SZheng Bin 			tx_complete = false;
2243f72879eSAndy Shevchenko 		usleep_range(1000, 1100);
225653d00c8SAndy Shevchenko 	} while (!uart_circ_empty(xmit) && !tx_complete && i++ < 1000);
226653d00c8SAndy Shevchenko 
227653d00c8SAndy Shevchenko 	serial8250_do_shutdown(port);
228653d00c8SAndy Shevchenko }
229653d00c8SAndy Shevchenko 
230d0aeaa83SSudip Mukherjee static int default_setup(struct exar8250 *priv, struct pci_dev *pcidev,
231d0aeaa83SSudip Mukherjee 			 int idx, unsigned int offset,
232d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *port)
233d0aeaa83SSudip Mukherjee {
234d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
235d0aeaa83SSudip Mukherjee 	unsigned int bar = 0;
2366be254c2SAndy Shevchenko 	unsigned char status;
237d0aeaa83SSudip Mukherjee 
238d0aeaa83SSudip Mukherjee 	port->port.iotype = UPIO_MEM;
239d0aeaa83SSudip Mukherjee 	port->port.mapbase = pci_resource_start(pcidev, bar) + offset;
240c7e1b405SAaron Sierra 	port->port.membase = priv->virt + offset;
241d0aeaa83SSudip Mukherjee 	port->port.regshift = board->reg_shift;
242d0aeaa83SSudip Mukherjee 
2436be254c2SAndy Shevchenko 	/*
2446be254c2SAndy Shevchenko 	 * XR17V35x UARTs have an extra divisor register, DLD that gets enabled
2456be254c2SAndy Shevchenko 	 * with when DLAB is set which will cause the device to incorrectly match
2466be254c2SAndy Shevchenko 	 * and assign port type to PORT_16650. The EFR for this UART is found
2476be254c2SAndy Shevchenko 	 * at offset 0x09. Instead check the Deice ID (DVID) register
2486be254c2SAndy Shevchenko 	 * for a 2, 4 or 8 port UART.
2496be254c2SAndy Shevchenko 	 */
2506be254c2SAndy Shevchenko 	status = readb(port->port.membase + UART_EXAR_DVID);
2516be254c2SAndy Shevchenko 	if (status == 0x82 || status == 0x84 || status == 0x88) {
2526be254c2SAndy Shevchenko 		port->port.type = PORT_XR17V35X;
253b2b4b8edSAndy Shevchenko 
254b2b4b8edSAndy Shevchenko 		port->port.get_divisor = xr17v35x_get_divisor;
255b2b4b8edSAndy Shevchenko 		port->port.set_divisor = xr17v35x_set_divisor;
2566e731137SAndy Shevchenko 
2576e731137SAndy Shevchenko 		port->port.startup = xr17v35x_startup;
2586be254c2SAndy Shevchenko 	} else {
2596be254c2SAndy Shevchenko 		port->port.type = PORT_XR17D15X;
2606be254c2SAndy Shevchenko 	}
2616be254c2SAndy Shevchenko 
262ef4e281eSAndy Shevchenko 	port->port.pm = exar_pm;
263653d00c8SAndy Shevchenko 	port->port.shutdown = exar_shutdown;
264ef4e281eSAndy Shevchenko 
265d0aeaa83SSudip Mukherjee 	return 0;
266d0aeaa83SSudip Mukherjee }
267d0aeaa83SSudip Mukherjee 
268d0aeaa83SSudip Mukherjee static int
269fc6cc961SJan Kiszka pci_fastcom335_setup(struct exar8250 *priv, struct pci_dev *pcidev,
270fc6cc961SJan Kiszka 		     struct uart_8250_port *port, int idx)
271fc6cc961SJan Kiszka {
272fc6cc961SJan Kiszka 	unsigned int offset = idx * 0x200;
273fc6cc961SJan Kiszka 	unsigned int baud = 1843200;
274fc6cc961SJan Kiszka 	u8 __iomem *p;
275fc6cc961SJan Kiszka 	int err;
276fc6cc961SJan Kiszka 
277fc6cc961SJan Kiszka 	port->port.uartclk = baud * 16;
278fc6cc961SJan Kiszka 
279fc6cc961SJan Kiszka 	err = default_setup(priv, pcidev, idx, offset, port);
280fc6cc961SJan Kiszka 	if (err)
281fc6cc961SJan Kiszka 		return err;
282fc6cc961SJan Kiszka 
283fc6cc961SJan Kiszka 	p = port->port.membase;
284fc6cc961SJan Kiszka 
285fc6cc961SJan Kiszka 	writeb(0x00, p + UART_EXAR_8XMODE);
286fc6cc961SJan Kiszka 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
287fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_TXTRG);
288fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_RXTRG);
289fc6cc961SJan Kiszka 
290fc6cc961SJan Kiszka 	/*
291fc6cc961SJan Kiszka 	 * Setup Multipurpose Input/Output pins.
292fc6cc961SJan Kiszka 	 */
293fc6cc961SJan Kiszka 	if (idx == 0) {
294fc6cc961SJan Kiszka 		switch (pcidev->device) {
295fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4222PCI335:
296fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4224PCI335:
297fc6cc961SJan Kiszka 			writeb(0x78, p + UART_EXAR_MPIOLVL_7_0);
298fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
299fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
300fc6cc961SJan Kiszka 			break;
301fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2324PCI335:
302fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2328PCI335:
303fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
304fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOINV_7_0);
305fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOSEL_7_0);
306fc6cc961SJan Kiszka 			break;
307fc6cc961SJan Kiszka 		}
308fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
309fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
310fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
311fc6cc961SJan Kiszka 	}
312fc6cc961SJan Kiszka 
313fc6cc961SJan Kiszka 	return 0;
314fc6cc961SJan Kiszka }
315fc6cc961SJan Kiszka 
316fc6cc961SJan Kiszka static int
317d0aeaa83SSudip Mukherjee pci_connect_tech_setup(struct exar8250 *priv, struct pci_dev *pcidev,
318d0aeaa83SSudip Mukherjee 		       struct uart_8250_port *port, int idx)
319d0aeaa83SSudip Mukherjee {
320d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
321d0aeaa83SSudip Mukherjee 	unsigned int baud = 1843200;
322d0aeaa83SSudip Mukherjee 
323d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
324d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
325d0aeaa83SSudip Mukherjee }
326d0aeaa83SSudip Mukherjee 
327d0aeaa83SSudip Mukherjee static int
328d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
329d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
330d0aeaa83SSudip Mukherjee {
331d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
332d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
333d0aeaa83SSudip Mukherjee 
334d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
335d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
336d0aeaa83SSudip Mukherjee }
337d0aeaa83SSudip Mukherjee 
338bea8be65SJan Kiszka static void setup_gpio(struct pci_dev *pcidev, u8 __iomem *p)
339d0aeaa83SSudip Mukherjee {
340bea8be65SJan Kiszka 	/*
341bea8be65SJan Kiszka 	 * The Commtech adapters required the MPIOs to be driven low. The Exar
342bea8be65SJan Kiszka 	 * devices will export them as GPIOs, so we pre-configure them safely
343bea8be65SJan Kiszka 	 * as inputs.
344bea8be65SJan Kiszka 	 */
3455fdbe136SMatthew Howell 
3465fdbe136SMatthew Howell 	u8 dir = 0x00;
3475fdbe136SMatthew Howell 
3485fdbe136SMatthew Howell 	if  ((pcidev->vendor == PCI_VENDOR_ID_EXAR) &&
3495fdbe136SMatthew Howell 		(pcidev->subsystem_vendor != PCI_VENDOR_ID_SEALEVEL)) {
3505fdbe136SMatthew Howell 		// Configure GPIO as inputs for Commtech adapters
3515fdbe136SMatthew Howell 		dir = 0xff;
3525fdbe136SMatthew Howell 	} else {
3535fdbe136SMatthew Howell 		// Configure GPIO as outputs for SeaLevel adapters
3545fdbe136SMatthew Howell 		dir = 0x00;
3555fdbe136SMatthew Howell 	}
356bea8be65SJan Kiszka 
357d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
358d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
359d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
360d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
361bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_7_0);
362d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
363d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
364d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
365d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
366d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
367bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_15_8);
368d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
369d0aeaa83SSudip Mukherjee }
370d0aeaa83SSudip Mukherjee 
37133969db7SAndy Shevchenko static struct platform_device *__xr17v35x_register_gpio(struct pci_dev *pcidev,
37281171e7dSHeikki Krogerus 							const struct software_node *node)
373d0aeaa83SSudip Mukherjee {
374d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
375d0aeaa83SSudip Mukherjee 
376d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
377d0aeaa83SSudip Mukherjee 	if (!pdev)
378d0aeaa83SSudip Mukherjee 		return NULL;
379d0aeaa83SSudip Mukherjee 
380d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
3814076cf08SJan Kiszka 	ACPI_COMPANION_SET(&pdev->dev, ACPI_COMPANION(&pcidev->dev));
382d3936d74SJan Kiszka 
38381171e7dSHeikki Krogerus 	if (device_add_software_node(&pdev->dev, node) < 0 ||
384380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
385d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
386d0aeaa83SSudip Mukherjee 		return NULL;
387d0aeaa83SSudip Mukherjee 	}
388d0aeaa83SSudip Mukherjee 
389d0aeaa83SSudip Mukherjee 	return pdev;
390d0aeaa83SSudip Mukherjee }
391d0aeaa83SSudip Mukherjee 
39233969db7SAndy Shevchenko static void __xr17v35x_unregister_gpio(struct platform_device *pdev)
39333969db7SAndy Shevchenko {
39433969db7SAndy Shevchenko 	device_remove_software_node(&pdev->dev);
39533969db7SAndy Shevchenko 	platform_device_unregister(pdev);
39633969db7SAndy Shevchenko }
39733969db7SAndy Shevchenko 
398380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
399a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 0),
400380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
401380b1e2fSJan Kiszka 	{ }
402380b1e2fSJan Kiszka };
403380b1e2fSJan Kiszka 
40481171e7dSHeikki Krogerus static const struct software_node exar_gpio_node = {
40581171e7dSHeikki Krogerus 	.properties = exar_gpio_properties,
40681171e7dSHeikki Krogerus };
40781171e7dSHeikki Krogerus 
40833969db7SAndy Shevchenko static int xr17v35x_register_gpio(struct pci_dev *pcidev, struct uart_8250_port *port)
4090d963ebfSJan Kiszka {
4100d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
4110d963ebfSJan Kiszka 		port->port.private_data =
41281171e7dSHeikki Krogerus 			__xr17v35x_register_gpio(pcidev, &exar_gpio_node);
4130d963ebfSJan Kiszka 
4140d963ebfSJan Kiszka 	return 0;
4150d963ebfSJan Kiszka }
4160d963ebfSJan Kiszka 
41733969db7SAndy Shevchenko static void xr17v35x_unregister_gpio(struct uart_8250_port *port)
41833969db7SAndy Shevchenko {
41933969db7SAndy Shevchenko 	if (!port->port.private_data)
42033969db7SAndy Shevchenko 		return;
42133969db7SAndy Shevchenko 
42233969db7SAndy Shevchenko 	__xr17v35x_unregister_gpio(port->port.private_data);
42333969db7SAndy Shevchenko 	port->port.private_data = NULL;
42433969db7SAndy Shevchenko }
42533969db7SAndy Shevchenko 
426ae50bb27SIlpo Järvinen static int generic_rs485_config(struct uart_port *port, struct ktermios *termios,
4279d939894SDaniel Golle 				struct serial_rs485 *rs485)
4289d939894SDaniel Golle {
4299d939894SDaniel Golle 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
4309d939894SDaniel Golle 	u8 __iomem *p = port->membase;
4319d939894SDaniel Golle 	u8 value;
4329d939894SDaniel Golle 
4339d939894SDaniel Golle 	value = readb(p + UART_EXAR_FCTR);
4349d939894SDaniel Golle 	if (is_rs485)
4359d939894SDaniel Golle 		value |= UART_FCTR_EXAR_485;
4369d939894SDaniel Golle 	else
4379d939894SDaniel Golle 		value &= ~UART_FCTR_EXAR_485;
4389d939894SDaniel Golle 
4399d939894SDaniel Golle 	writeb(value, p + UART_EXAR_FCTR);
4409d939894SDaniel Golle 
4419d939894SDaniel Golle 	if (is_rs485)
4429d939894SDaniel Golle 		writeb(UART_EXAR_RS485_DLY(4), p + UART_MSR);
4439d939894SDaniel Golle 
4449d939894SDaniel Golle 	return 0;
4459d939894SDaniel Golle }
4469d939894SDaniel Golle 
44759c221f8SIlpo Järvinen static const struct serial_rs485 generic_rs485_supported = {
44859c221f8SIlpo Järvinen 	.flags = SER_RS485_ENABLED,
44959c221f8SIlpo Järvinen };
45059c221f8SIlpo Järvinen 
4510d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
4520d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
45333969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
4549d939894SDaniel Golle 	.rs485_config = generic_rs485_config,
45559c221f8SIlpo Järvinen 	.rs485_supported = &generic_rs485_supported,
4560d963ebfSJan Kiszka };
4570d963ebfSJan Kiszka 
458ae50bb27SIlpo Järvinen static int iot2040_rs485_config(struct uart_port *port, struct ktermios *termios,
459413058dfSJan Kiszka 				struct serial_rs485 *rs485)
460413058dfSJan Kiszka {
461413058dfSJan Kiszka 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
462413058dfSJan Kiszka 	u8 __iomem *p = port->membase;
463413058dfSJan Kiszka 	u8 mask = IOT2040_UART1_MASK;
464413058dfSJan Kiszka 	u8 mode, value;
465413058dfSJan Kiszka 
466413058dfSJan Kiszka 	if (is_rs485) {
467413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_RX_DURING_TX)
468413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS422;
469413058dfSJan Kiszka 		else
470413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS485;
471413058dfSJan Kiszka 
472413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_TERMINATE_BUS)
473413058dfSJan Kiszka 			mode |= IOT2040_UART_TERMINATE_BUS;
474413058dfSJan Kiszka 	} else {
475413058dfSJan Kiszka 		mode = IOT2040_UART_MODE_RS232;
476413058dfSJan Kiszka 	}
477413058dfSJan Kiszka 
478413058dfSJan Kiszka 	if (port->line == 3) {
479413058dfSJan Kiszka 		mask <<= IOT2040_UART2_SHIFT;
480413058dfSJan Kiszka 		mode <<= IOT2040_UART2_SHIFT;
481413058dfSJan Kiszka 	}
482413058dfSJan Kiszka 
483413058dfSJan Kiszka 	value = readb(p + UART_EXAR_MPIOLVL_7_0);
484413058dfSJan Kiszka 	value &= ~mask;
485413058dfSJan Kiszka 	value |= mode;
486413058dfSJan Kiszka 	writeb(value, p + UART_EXAR_MPIOLVL_7_0);
487413058dfSJan Kiszka 
488ae50bb27SIlpo Järvinen 	return generic_rs485_config(port, termios, rs485);
489413058dfSJan Kiszka }
490413058dfSJan Kiszka 
49159c221f8SIlpo Järvinen static const struct serial_rs485 iot2040_rs485_supported = {
49259c221f8SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
49359c221f8SIlpo Järvinen };
49459c221f8SIlpo Järvinen 
495413058dfSJan Kiszka static const struct property_entry iot2040_gpio_properties[] = {
496a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 10),
497413058dfSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 1),
498413058dfSJan Kiszka 	{ }
499413058dfSJan Kiszka };
500413058dfSJan Kiszka 
50181171e7dSHeikki Krogerus static const struct software_node iot2040_gpio_node = {
50281171e7dSHeikki Krogerus 	.properties = iot2040_gpio_properties,
50381171e7dSHeikki Krogerus };
50481171e7dSHeikki Krogerus 
505413058dfSJan Kiszka static int iot2040_register_gpio(struct pci_dev *pcidev,
506413058dfSJan Kiszka 			      struct uart_8250_port *port)
507413058dfSJan Kiszka {
508413058dfSJan Kiszka 	u8 __iomem *p = port->port.membase;
509413058dfSJan Kiszka 
510413058dfSJan Kiszka 	writeb(IOT2040_UARTS_DEFAULT_MODE, p + UART_EXAR_MPIOLVL_7_0);
511413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_LO_MODE, p + UART_EXAR_MPIOSEL_7_0);
512413058dfSJan Kiszka 	writeb(IOT2040_UARTS_ENABLE, p + UART_EXAR_MPIOLVL_15_8);
513413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_HI_MODE, p + UART_EXAR_MPIOSEL_15_8);
514413058dfSJan Kiszka 
515413058dfSJan Kiszka 	port->port.private_data =
51681171e7dSHeikki Krogerus 		__xr17v35x_register_gpio(pcidev, &iot2040_gpio_node);
517413058dfSJan Kiszka 
518413058dfSJan Kiszka 	return 0;
519413058dfSJan Kiszka }
520413058dfSJan Kiszka 
521413058dfSJan Kiszka static const struct exar8250_platform iot2040_platform = {
522413058dfSJan Kiszka 	.rs485_config = iot2040_rs485_config,
52359c221f8SIlpo Järvinen 	.rs485_supported = &iot2040_rs485_supported,
524413058dfSJan Kiszka 	.register_gpio = iot2040_register_gpio,
52533969db7SAndy Shevchenko 	.unregister_gpio = xr17v35x_unregister_gpio,
526413058dfSJan Kiszka };
527413058dfSJan Kiszka 
5283e51ceeaSSu Bao Cheng /*
5293e51ceeaSSu Bao Cheng  * For SIMATIC IOT2000, only IOT2040 and its variants have the Exar device,
5303e51ceeaSSu Bao Cheng  * IOT2020 doesn't have. Therefore it is sufficient to match on the common
5313e51ceeaSSu Bao Cheng  * board name after the device was found.
5323e51ceeaSSu Bao Cheng  */
533413058dfSJan Kiszka static const struct dmi_system_id exar_platforms[] = {
534413058dfSJan Kiszka 	{
535413058dfSJan Kiszka 		.matches = {
536413058dfSJan Kiszka 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
537413058dfSJan Kiszka 		},
538413058dfSJan Kiszka 		.driver_data = (void *)&iot2040_platform,
539413058dfSJan Kiszka 	},
540413058dfSJan Kiszka 	{}
541413058dfSJan Kiszka };
542413058dfSJan Kiszka 
5437d356a43SAndy Shevchenko static const struct exar8250_platform *exar_get_platform(void)
5447d356a43SAndy Shevchenko {
5457d356a43SAndy Shevchenko 	const struct dmi_system_id *dmi_match;
5467d356a43SAndy Shevchenko 
5477d356a43SAndy Shevchenko 	dmi_match = dmi_first_match(exar_platforms);
5487d356a43SAndy Shevchenko 	if (dmi_match)
5497d356a43SAndy Shevchenko 		return dmi_match->driver_data;
5507d356a43SAndy Shevchenko 
5517d356a43SAndy Shevchenko 	return &exar8250_default_platform;
5527d356a43SAndy Shevchenko }
5537d356a43SAndy Shevchenko 
554d0aeaa83SSudip Mukherjee static int
555d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
556d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
557d0aeaa83SSudip Mukherjee {
5587d356a43SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
559d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
560d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
561d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
562d0aeaa83SSudip Mukherjee 	int ret;
563d0aeaa83SSudip Mukherjee 
564d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
5650d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
5660139da50SIlpo Järvinen 	port->port.rs485_supported = *(platform->rs485_supported);
5670d963ebfSJan Kiszka 
568d0aeaa83SSudip Mukherjee 	/*
569328c11f2SAndy Shevchenko 	 * Setup the UART clock for the devices on expansion slot to
570d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
571d0aeaa83SSudip Mukherjee 	 */
572328c11f2SAndy Shevchenko 	if (idx >= 8)
573d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
574d0aeaa83SSudip Mukherjee 
5755b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
5765b5f252dSJan Kiszka 	if (ret)
5775b5f252dSJan Kiszka 		return ret;
578d0aeaa83SSudip Mukherjee 
5795b5f252dSJan Kiszka 	p = port->port.membase;
580d0aeaa83SSudip Mukherjee 
581d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
582d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
583d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
584d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
585d0aeaa83SSudip Mukherjee 
5865b5f252dSJan Kiszka 	if (idx == 0) {
5875b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
588bea8be65SJan Kiszka 		setup_gpio(pcidev, p);
589d0aeaa83SSudip Mukherjee 
5900d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
5915b5f252dSJan Kiszka 	}
592d0aeaa83SSudip Mukherjee 
5930d963ebfSJan Kiszka 	return ret;
594d0aeaa83SSudip Mukherjee }
595d0aeaa83SSudip Mukherjee 
596d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
597d0aeaa83SSudip Mukherjee {
59833969db7SAndy Shevchenko 	const struct exar8250_platform *platform = exar_get_platform();
599d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
600d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
6017c3e8d9dSAndy Shevchenko 
60233969db7SAndy Shevchenko 	platform->unregister_gpio(port);
603d0aeaa83SSudip Mukherjee }
604d0aeaa83SSudip Mukherjee 
60572169e42SAaron Sierra static inline void exar_misc_clear(struct exar8250 *priv)
60672169e42SAaron Sierra {
60772169e42SAaron Sierra 	/* Clear all PCI interrupts by reading INT0. No effect on IIR */
60872169e42SAaron Sierra 	readb(priv->virt + UART_EXAR_INT0);
60972169e42SAaron Sierra 
61072169e42SAaron Sierra 	/* Clear INT0 for Expansion Interface slave ports, too */
61172169e42SAaron Sierra 	if (priv->board->num_ports > 8)
61272169e42SAaron Sierra 		readb(priv->virt + 0x2000 + UART_EXAR_INT0);
61372169e42SAaron Sierra }
61472169e42SAaron Sierra 
615c7e1b405SAaron Sierra /*
616c7e1b405SAaron Sierra  * These Exar UARTs have an extra interrupt indicator that could fire for a
617c7e1b405SAaron Sierra  * few interrupts that are not presented/cleared through IIR.  One of which is
618c7e1b405SAaron Sierra  * a wakeup interrupt when coming out of sleep.  These interrupts are only
619c7e1b405SAaron Sierra  * cleared by reading global INT0 or INT1 registers as interrupts are
620c7e1b405SAaron Sierra  * associated with channel 0. The INT[3:0] registers _are_ accessible from each
621c7e1b405SAaron Sierra  * channel's address space, but for the sake of bus efficiency we register a
622c7e1b405SAaron Sierra  * dedicated handler at the PCI device level to handle them.
623c7e1b405SAaron Sierra  */
624c7e1b405SAaron Sierra static irqreturn_t exar_misc_handler(int irq, void *data)
625c7e1b405SAaron Sierra {
62672169e42SAaron Sierra 	exar_misc_clear(data);
627c7e1b405SAaron Sierra 
628c7e1b405SAaron Sierra 	return IRQ_HANDLED;
629c7e1b405SAaron Sierra }
630c7e1b405SAaron Sierra 
631d0aeaa83SSudip Mukherjee static int
632d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
633d0aeaa83SSudip Mukherjee {
634d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
635d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
636d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
637d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
638d0aeaa83SSudip Mukherjee 	int rc;
639d0aeaa83SSudip Mukherjee 
640d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
641d0aeaa83SSudip Mukherjee 	if (!board)
642d0aeaa83SSudip Mukherjee 		return -EINVAL;
643d0aeaa83SSudip Mukherjee 
644d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
645d0aeaa83SSudip Mukherjee 	if (rc)
646d0aeaa83SSudip Mukherjee 		return rc;
647d0aeaa83SSudip Mukherjee 
648d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
649d0aeaa83SSudip Mukherjee 
6508e4413aaSAndy Shevchenko 	if (pcidev->vendor == PCI_VENDOR_ID_ACCESSIO)
6518e4413aaSAndy Shevchenko 		nr_ports = BIT(((pcidev->device & 0x38) >> 3) - 1);
6528e4413aaSAndy Shevchenko 	else if (board->num_ports)
6538e4413aaSAndy Shevchenko 		nr_ports = board->num_ports;
65414ee78d5SMatthew Howell 	else if (pcidev->vendor == PCI_VENDOR_ID_SEALEVEL)
65514ee78d5SMatthew Howell 		nr_ports = pcidev->device & 0xff;
6568e4413aaSAndy Shevchenko 	else
6578e4413aaSAndy Shevchenko 		nr_ports = pcidev->device & 0x0f;
658d0aeaa83SSudip Mukherjee 
659df60a8afSAndy Shevchenko 	priv = devm_kzalloc(&pcidev->dev, struct_size(priv, line, nr_ports), GFP_KERNEL);
660d0aeaa83SSudip Mukherjee 	if (!priv)
661d0aeaa83SSudip Mukherjee 		return -ENOMEM;
662d0aeaa83SSudip Mukherjee 
663d0aeaa83SSudip Mukherjee 	priv->board = board;
664c7e1b405SAaron Sierra 	priv->virt = pcim_iomap(pcidev, bar, 0);
665c7e1b405SAaron Sierra 	if (!priv->virt)
666c7e1b405SAaron Sierra 		return -ENOMEM;
667d0aeaa83SSudip Mukherjee 
668172c33cbSJan Kiszka 	pci_set_master(pcidev);
669172c33cbSJan Kiszka 
670172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
671172c33cbSJan Kiszka 	if (rc < 0)
672172c33cbSJan Kiszka 		return rc;
673172c33cbSJan Kiszka 
674d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
6756be254c2SAndy Shevchenko 	uart.port.flags = UPF_SHARE_IRQ | UPF_EXAR_EFR | UPF_FIXED_TYPE | UPF_FIXED_PORT;
676172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
677d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
678d0aeaa83SSudip Mukherjee 
679c7e1b405SAaron Sierra 	rc = devm_request_irq(&pcidev->dev, uart.port.irq, exar_misc_handler,
680c7e1b405SAaron Sierra 			 IRQF_SHARED, "exar_uart", priv);
681c7e1b405SAaron Sierra 	if (rc)
682c7e1b405SAaron Sierra 		return rc;
683c7e1b405SAaron Sierra 
68472169e42SAaron Sierra 	/* Clear interrupts */
68572169e42SAaron Sierra 	exar_misc_clear(priv);
68672169e42SAaron Sierra 
687d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
688d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
689d0aeaa83SSudip Mukherjee 		if (rc) {
690d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
691d0aeaa83SSudip Mukherjee 			break;
692d0aeaa83SSudip Mukherjee 		}
693d0aeaa83SSudip Mukherjee 
694d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
695d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
696d0aeaa83SSudip Mukherjee 
697d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
698d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
699d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
700d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
701d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
702d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
703d0aeaa83SSudip Mukherjee 			break;
704d0aeaa83SSudip Mukherjee 		}
705d0aeaa83SSudip Mukherjee 	}
706d0aeaa83SSudip Mukherjee 	priv->nr = i;
707d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
708d0aeaa83SSudip Mukherjee 	return 0;
709d0aeaa83SSudip Mukherjee }
710d0aeaa83SSudip Mukherjee 
711d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
712d0aeaa83SSudip Mukherjee {
713d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
714d0aeaa83SSudip Mukherjee 	unsigned int i;
715d0aeaa83SSudip Mukherjee 
716d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
717d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
718d0aeaa83SSudip Mukherjee 
719d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
720d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
721d0aeaa83SSudip Mukherjee }
722d0aeaa83SSudip Mukherjee 
723d0aeaa83SSudip Mukherjee static int __maybe_unused exar_suspend(struct device *dev)
724d0aeaa83SSudip Mukherjee {
725d0aeaa83SSudip Mukherjee 	struct pci_dev *pcidev = to_pci_dev(dev);
726d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
727d0aeaa83SSudip Mukherjee 	unsigned int i;
728d0aeaa83SSudip Mukherjee 
729d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
730d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
731d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
732d0aeaa83SSudip Mukherjee 
733d0aeaa83SSudip Mukherjee 	/* Ensure that every init quirk is properly torn down */
734d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
735d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
736d0aeaa83SSudip Mukherjee 
737d0aeaa83SSudip Mukherjee 	return 0;
738d0aeaa83SSudip Mukherjee }
739d0aeaa83SSudip Mukherjee 
740d0aeaa83SSudip Mukherjee static int __maybe_unused exar_resume(struct device *dev)
741d0aeaa83SSudip Mukherjee {
74276b4106cSChuhong Yuan 	struct exar8250 *priv = dev_get_drvdata(dev);
743d0aeaa83SSudip Mukherjee 	unsigned int i;
744d0aeaa83SSudip Mukherjee 
74572169e42SAaron Sierra 	exar_misc_clear(priv);
74672169e42SAaron Sierra 
747d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
748d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
749d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
750d0aeaa83SSudip Mukherjee 
751d0aeaa83SSudip Mukherjee 	return 0;
752d0aeaa83SSudip Mukherjee }
753d0aeaa83SSudip Mukherjee 
754d0aeaa83SSudip Mukherjee static SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
755d0aeaa83SSudip Mukherjee 
756fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
757fc6cc961SJan Kiszka 	.num_ports	= 2,
758fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
759fc6cc961SJan Kiszka };
760fc6cc961SJan Kiszka 
761fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
762fc6cc961SJan Kiszka 	.num_ports	= 4,
763fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
764fc6cc961SJan Kiszka };
765fc6cc961SJan Kiszka 
766fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
767fc6cc961SJan Kiszka 	.num_ports	= 8,
768fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
769fc6cc961SJan Kiszka };
770fc6cc961SJan Kiszka 
771d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_connect = {
772d0aeaa83SSudip Mukherjee 	.setup		= pci_connect_tech_setup,
773d0aeaa83SSudip Mukherjee };
774d0aeaa83SSudip Mukherjee 
775d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
776d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
777d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
778d0aeaa83SSudip Mukherjee };
779d0aeaa83SSudip Mukherjee 
780d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
781d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
782d0aeaa83SSudip Mukherjee };
783d0aeaa83SSudip Mukherjee 
784d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
785d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
786d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
787d0aeaa83SSudip Mukherjee };
788d0aeaa83SSudip Mukherjee 
789c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_2 = {
790c6b9e95dSValmer Huhn 	.num_ports	= 2,
791c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
792c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
793c6b9e95dSValmer Huhn };
794c6b9e95dSValmer Huhn 
795c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_4 = {
796c6b9e95dSValmer Huhn 	.num_ports	= 4,
797c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
798c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
799c6b9e95dSValmer Huhn };
800c6b9e95dSValmer Huhn 
801c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_8 = {
802c6b9e95dSValmer Huhn 	.num_ports	= 8,
803c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
804c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
805c6b9e95dSValmer Huhn };
806c6b9e95dSValmer Huhn 
807d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
808d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
809d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
810d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
811d0aeaa83SSudip Mukherjee };
812d0aeaa83SSudip Mukherjee 
813d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
814d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
815d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
816d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
817d0aeaa83SSudip Mukherjee };
818d0aeaa83SSudip Mukherjee 
819d0aeaa83SSudip Mukherjee #define CONNECT_DEVICE(devid, sdevid, bd) {				\
820d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(							\
821d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,					\
822d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,				\
823d0aeaa83SSudip Mukherjee 		PCI_SUBVENDOR_ID_CONNECT_TECH,				\
824d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_##sdevid), 0, 0,	\
825d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd					\
826d0aeaa83SSudip Mukherjee 	}
827d0aeaa83SSudip Mukherjee 
82824637007SAndy Shevchenko #define EXAR_DEVICE(vend, devid, bd) { PCI_DEVICE_DATA(vend, devid, &bd) }
829d0aeaa83SSudip Mukherjee 
830d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
831d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
832d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
833d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
834d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_IBM,			\
835d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
836d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
837d0aeaa83SSudip Mukherjee 	}
838d0aeaa83SSudip Mukherjee 
83995d69886SAndrew Davis #define USR_DEVICE(devid, sdevid, bd) {			\
84095d69886SAndrew Davis 	PCI_DEVICE_SUB(					\
84195d69886SAndrew Davis 		PCI_VENDOR_ID_USR,			\
84295d69886SAndrew Davis 		PCI_DEVICE_ID_EXAR_##devid,		\
84395d69886SAndrew Davis 		PCI_VENDOR_ID_EXAR,			\
84495d69886SAndrew Davis 		PCI_SUBDEVICE_ID_USR_##sdevid), 0, 0,	\
84595d69886SAndrew Davis 		(kernel_ulong_t)&bd			\
84695d69886SAndrew Davis 	}
84795d69886SAndrew Davis 
8483637c460SArvind Yadav static const struct pci_device_id exar_pci_tbl[] = {
8498e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2S, pbn_exar_XR17C15x),
8508e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4S, pbn_exar_XR17C15x),
8518e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8S, pbn_exar_XR17C15x),
8528e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM232_8, pbn_exar_XR17C15x),
8538e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2SM, pbn_exar_XR17C15x),
8548e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4SM, pbn_exar_XR17C15x),
8558e4413aaSAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8SM, pbn_exar_XR17C15x),
85610c5ccc3SJay Dolan 
857d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_232, pbn_connect),
858d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_232, pbn_connect),
859d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_232, pbn_connect),
860d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_1_1, pbn_connect),
861d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_2_2, pbn_connect),
862d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_4_4, pbn_connect),
863d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2, pbn_connect),
864d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4, pbn_connect),
865d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8, pbn_connect),
866d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_485, pbn_connect),
867d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_485, pbn_connect),
868d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_485, pbn_connect),
869d0aeaa83SSudip Mukherjee 
870d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
871d0aeaa83SSudip Mukherjee 
87295d69886SAndrew Davis 	/* USRobotics USR298x-OEM PCI Modems */
87395d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2980, pbn_exar_XR17C15x),
87495d69886SAndrew Davis 	USR_DEVICE(XR17C152, 2981, pbn_exar_XR17C15x),
87595d69886SAndrew Davis 
876d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
87724637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C152, pbn_exar_XR17C15x),
87824637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C154, pbn_exar_XR17C15x),
87924637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C158, pbn_exar_XR17C15x),
880d0aeaa83SSudip Mukherjee 
881d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
88224637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V352, pbn_exar_XR17V35x),
88324637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V354, pbn_exar_XR17V35x),
88424637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V358, pbn_exar_XR17V35x),
88524637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V4358, pbn_exar_XR17V4358),
88624637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V8358, pbn_exar_XR17V8358),
887c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4222PCIE, pbn_fastcom35x_2),
888c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4224PCIE, pbn_fastcom35x_4),
889c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4228PCIE, pbn_fastcom35x_8),
890fc6cc961SJan Kiszka 
89124637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4222PCI335, pbn_fastcom335_2),
89224637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4224PCI335, pbn_fastcom335_4),
89324637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2324PCI335, pbn_fastcom335_4),
89424637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2328PCI335, pbn_fastcom335_8),
89514ee78d5SMatthew Howell 
89614ee78d5SMatthew Howell 	EXAR_DEVICE(SEALEVEL, 710xC, pbn_exar_XR17V35x),
89714ee78d5SMatthew Howell 	EXAR_DEVICE(SEALEVEL, 720xC, pbn_exar_XR17V35x),
89814ee78d5SMatthew Howell 	EXAR_DEVICE(SEALEVEL, 740xC, pbn_exar_XR17V35x),
89914ee78d5SMatthew Howell 	EXAR_DEVICE(SEALEVEL, 780xC, pbn_exar_XR17V35x),
90014ee78d5SMatthew Howell 	EXAR_DEVICE(SEALEVEL, 716xC, pbn_exar_XR17V35x),
901d0aeaa83SSudip Mukherjee 	{ 0, }
902d0aeaa83SSudip Mukherjee };
903d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
904d0aeaa83SSudip Mukherjee 
905d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
906d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
907d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
908d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
909d0aeaa83SSudip Mukherjee 	.driver         = {
910d0aeaa83SSudip Mukherjee 		.pm     = &exar_pci_pm,
911d0aeaa83SSudip Mukherjee 	},
912d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
913d0aeaa83SSudip Mukherjee };
914d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
915d0aeaa83SSudip Mukherjee 
916d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
9172b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
918d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
919