xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision 72169e4234d4cd3cd177d3021efde82bbdfa43e8)
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>
21d0aeaa83SSudip Mukherjee #include <linux/8250_pci.h>
2247b1747fSRobert Middleton #include <linux/delay.h>
23d0aeaa83SSudip Mukherjee 
24d0aeaa83SSudip Mukherjee #include <asm/byteorder.h>
25d0aeaa83SSudip Mukherjee 
26d0aeaa83SSudip Mukherjee #include "8250.h"
27d0aeaa83SSudip Mukherjee 
28fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4224PCI335	0x0002
29fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4222PCI335	0x0004
30fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2324PCI335	0x000a
31fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2328PCI335	0x000b
32d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4224PCIE		0x0020
33d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4228PCIE		0x0021
34d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4222PCIE		0x0022
35d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V4358		0x4358
36d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V8358		0x8358
37d0aeaa83SSudip Mukherjee 
38c7e1b405SAaron Sierra #define UART_EXAR_INT0		0x80
397e12357eSJan Kiszka #define UART_EXAR_8XMODE	0x88	/* 8X sampling rate select */
40ef4e281eSAndy Shevchenko #define UART_EXAR_SLEEP		0x8b	/* Sleep mode */
416be254c2SAndy Shevchenko #define UART_EXAR_DVID		0x8d	/* Device identification */
427e12357eSJan Kiszka 
437e12357eSJan Kiszka #define UART_EXAR_FCTR		0x08	/* Feature Control Register */
447e12357eSJan Kiszka #define UART_FCTR_EXAR_IRDA	0x10	/* IrDa data encode select */
457e12357eSJan Kiszka #define UART_FCTR_EXAR_485	0x20	/* Auto 485 half duplex dir ctl */
467e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGA	0x00	/* FIFO trigger table A */
477e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGB	0x60	/* FIFO trigger table B */
487e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGC	0x80	/* FIFO trigger table C */
497e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGD	0xc0	/* FIFO trigger table D programmable */
507e12357eSJan Kiszka 
517e12357eSJan Kiszka #define UART_EXAR_TXTRG		0x0a	/* Tx FIFO trigger level write-only */
527e12357eSJan Kiszka #define UART_EXAR_RXTRG		0x0b	/* Rx FIFO trigger level write-only */
537e12357eSJan Kiszka 
54d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_7_0	0x8f	/* MPIOINT[7:0] */
55d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_7_0	0x90	/* MPIOLVL[7:0] */
56d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_7_0	0x91	/* MPIO3T[7:0] */
57d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_7_0	0x92	/* MPIOINV[7:0] */
58d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_7_0	0x93	/* MPIOSEL[7:0] */
59d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_7_0	0x94	/* MPIOOD[7:0] */
60d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_15_8	0x95	/* MPIOINT[15:8] */
61d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_15_8	0x96	/* MPIOLVL[15:8] */
62d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_15_8	0x97	/* MPIO3T[15:8] */
63d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_15_8	0x98	/* MPIOINV[15:8] */
64d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_15_8	0x99	/* MPIOSEL[15:8] */
65d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_15_8	0x9a	/* MPIOOD[15:8] */
66d0aeaa83SSudip Mukherjee 
67413058dfSJan Kiszka #define UART_EXAR_RS485_DLY(x)	((x) << 4)
68413058dfSJan Kiszka 
69413058dfSJan Kiszka /*
70413058dfSJan Kiszka  * IOT2040 MPIO wiring semantics:
71413058dfSJan Kiszka  *
72413058dfSJan Kiszka  * MPIO		Port	Function
73413058dfSJan Kiszka  * ----		----	--------
74413058dfSJan Kiszka  * 0		2 	Mode bit 0
75413058dfSJan Kiszka  * 1		2	Mode bit 1
76413058dfSJan Kiszka  * 2		2	Terminate bus
77413058dfSJan Kiszka  * 3		-	<reserved>
78413058dfSJan Kiszka  * 4		3	Mode bit 0
79413058dfSJan Kiszka  * 5		3	Mode bit 1
80413058dfSJan Kiszka  * 6		3	Terminate bus
81413058dfSJan Kiszka  * 7		-	<reserved>
82413058dfSJan Kiszka  * 8		2	Enable
83413058dfSJan Kiszka  * 9		3	Enable
84413058dfSJan Kiszka  * 10		-	Red LED
85413058dfSJan Kiszka  * 11..15	-	<unused>
86413058dfSJan Kiszka  */
87413058dfSJan Kiszka 
88413058dfSJan Kiszka /* IOT2040 MPIOs 0..7 */
89413058dfSJan Kiszka #define IOT2040_UART_MODE_RS232		0x01
90413058dfSJan Kiszka #define IOT2040_UART_MODE_RS485		0x02
91413058dfSJan Kiszka #define IOT2040_UART_MODE_RS422		0x03
92413058dfSJan Kiszka #define IOT2040_UART_TERMINATE_BUS	0x04
93413058dfSJan Kiszka 
94413058dfSJan Kiszka #define IOT2040_UART1_MASK		0x0f
95413058dfSJan Kiszka #define IOT2040_UART2_SHIFT		4
96413058dfSJan Kiszka 
97413058dfSJan Kiszka #define IOT2040_UARTS_DEFAULT_MODE	0x11	/* both RS232 */
98413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_LO_MODE	0x88	/* reserved pins as input */
99413058dfSJan Kiszka 
100413058dfSJan Kiszka /* IOT2040 MPIOs 8..15 */
101413058dfSJan Kiszka #define IOT2040_UARTS_ENABLE		0x03
102413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_HI_MODE	0xF8	/* enable & LED as outputs */
103413058dfSJan Kiszka 
104d0aeaa83SSudip Mukherjee struct exar8250;
105d0aeaa83SSudip Mukherjee 
1060d963ebfSJan Kiszka struct exar8250_platform {
1070d963ebfSJan Kiszka 	int (*rs485_config)(struct uart_port *, struct serial_rs485 *);
1080d963ebfSJan Kiszka 	int (*register_gpio)(struct pci_dev *, struct uart_8250_port *);
1090d963ebfSJan Kiszka };
1100d963ebfSJan Kiszka 
111d0aeaa83SSudip Mukherjee /**
112d0aeaa83SSudip Mukherjee  * struct exar8250_board - board information
113d0aeaa83SSudip Mukherjee  * @num_ports: number of serial ports
114d0aeaa83SSudip Mukherjee  * @reg_shift: describes UART register mapping in PCI memory
11526f22d57SAndy Shevchenko  * @setup: quirk run at ->probe() stage
11626f22d57SAndy Shevchenko  * @exit: quirk run at ->remove() stage
117d0aeaa83SSudip Mukherjee  */
118d0aeaa83SSudip Mukherjee struct exar8250_board {
119d0aeaa83SSudip Mukherjee 	unsigned int num_ports;
120d0aeaa83SSudip Mukherjee 	unsigned int reg_shift;
121d0aeaa83SSudip Mukherjee 	int	(*setup)(struct exar8250 *, struct pci_dev *,
122d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *, int);
123d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
124d0aeaa83SSudip Mukherjee };
125d0aeaa83SSudip Mukherjee 
126d0aeaa83SSudip Mukherjee struct exar8250 {
127d0aeaa83SSudip Mukherjee 	unsigned int		nr;
128d0aeaa83SSudip Mukherjee 	struct exar8250_board	*board;
129c7e1b405SAaron Sierra 	void __iomem		*virt;
130d0aeaa83SSudip Mukherjee 	int			line[0];
131d0aeaa83SSudip Mukherjee };
132d0aeaa83SSudip Mukherjee 
133ef4e281eSAndy Shevchenko static void exar_pm(struct uart_port *port, unsigned int state, unsigned int old)
134ef4e281eSAndy Shevchenko {
135ef4e281eSAndy Shevchenko 	/*
136ef4e281eSAndy Shevchenko 	 * Exar UARTs have a SLEEP register that enables or disables each UART
137ef4e281eSAndy Shevchenko 	 * to enter sleep mode separately. On the XR17V35x the register
138ef4e281eSAndy Shevchenko 	 * is accessible to each UART at the UART_EXAR_SLEEP offset, but
139ef4e281eSAndy Shevchenko 	 * the UART channel may only write to the corresponding bit.
140ef4e281eSAndy Shevchenko 	 */
141ef4e281eSAndy Shevchenko 	serial_port_out(port, UART_EXAR_SLEEP, state ? 0xff : 0);
142ef4e281eSAndy Shevchenko }
143ef4e281eSAndy Shevchenko 
144b2b4b8edSAndy Shevchenko /*
145b2b4b8edSAndy Shevchenko  * XR17V35x UARTs have an extra fractional divisor register (DLD)
146b2b4b8edSAndy Shevchenko  * Calculate divisor with extra 4-bit fractional portion
147b2b4b8edSAndy Shevchenko  */
148b2b4b8edSAndy Shevchenko static unsigned int xr17v35x_get_divisor(struct uart_port *p, unsigned int baud,
149b2b4b8edSAndy Shevchenko 					 unsigned int *frac)
150b2b4b8edSAndy Shevchenko {
151b2b4b8edSAndy Shevchenko 	unsigned int quot_16;
152b2b4b8edSAndy Shevchenko 
153b2b4b8edSAndy Shevchenko 	quot_16 = DIV_ROUND_CLOSEST(p->uartclk, baud);
154b2b4b8edSAndy Shevchenko 	*frac = quot_16 & 0x0f;
155b2b4b8edSAndy Shevchenko 
156b2b4b8edSAndy Shevchenko 	return quot_16 >> 4;
157b2b4b8edSAndy Shevchenko }
158b2b4b8edSAndy Shevchenko 
159b2b4b8edSAndy Shevchenko static void xr17v35x_set_divisor(struct uart_port *p, unsigned int baud,
160b2b4b8edSAndy Shevchenko 				 unsigned int quot, unsigned int quot_frac)
161b2b4b8edSAndy Shevchenko {
162b2b4b8edSAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
163b2b4b8edSAndy Shevchenko 
164b2b4b8edSAndy Shevchenko 	/* Preserve bits not related to baudrate; DLD[7:4]. */
165b2b4b8edSAndy Shevchenko 	quot_frac |= serial_port_in(p, 0x2) & 0xf0;
166b2b4b8edSAndy Shevchenko 	serial_port_out(p, 0x2, quot_frac);
167b2b4b8edSAndy Shevchenko }
168b2b4b8edSAndy Shevchenko 
169d0aeaa83SSudip Mukherjee static int default_setup(struct exar8250 *priv, struct pci_dev *pcidev,
170d0aeaa83SSudip Mukherjee 			 int idx, unsigned int offset,
171d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *port)
172d0aeaa83SSudip Mukherjee {
173d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
174d0aeaa83SSudip Mukherjee 	unsigned int bar = 0;
1756be254c2SAndy Shevchenko 	unsigned char status;
176d0aeaa83SSudip Mukherjee 
177d0aeaa83SSudip Mukherjee 	port->port.iotype = UPIO_MEM;
178d0aeaa83SSudip Mukherjee 	port->port.mapbase = pci_resource_start(pcidev, bar) + offset;
179c7e1b405SAaron Sierra 	port->port.membase = priv->virt + offset;
180d0aeaa83SSudip Mukherjee 	port->port.regshift = board->reg_shift;
181d0aeaa83SSudip Mukherjee 
1826be254c2SAndy Shevchenko 	/*
1836be254c2SAndy Shevchenko 	 * XR17V35x UARTs have an extra divisor register, DLD that gets enabled
1846be254c2SAndy Shevchenko 	 * with when DLAB is set which will cause the device to incorrectly match
1856be254c2SAndy Shevchenko 	 * and assign port type to PORT_16650. The EFR for this UART is found
1866be254c2SAndy Shevchenko 	 * at offset 0x09. Instead check the Deice ID (DVID) register
1876be254c2SAndy Shevchenko 	 * for a 2, 4 or 8 port UART.
1886be254c2SAndy Shevchenko 	 */
1896be254c2SAndy Shevchenko 	status = readb(port->port.membase + UART_EXAR_DVID);
1906be254c2SAndy Shevchenko 	if (status == 0x82 || status == 0x84 || status == 0x88) {
1916be254c2SAndy Shevchenko 		port->port.type = PORT_XR17V35X;
192b2b4b8edSAndy Shevchenko 
193b2b4b8edSAndy Shevchenko 		port->port.get_divisor = xr17v35x_get_divisor;
194b2b4b8edSAndy Shevchenko 		port->port.set_divisor = xr17v35x_set_divisor;
1956be254c2SAndy Shevchenko 	} else {
1966be254c2SAndy Shevchenko 		port->port.type = PORT_XR17D15X;
1976be254c2SAndy Shevchenko 	}
1986be254c2SAndy Shevchenko 
199ef4e281eSAndy Shevchenko 	port->port.pm = exar_pm;
200ef4e281eSAndy Shevchenko 
201d0aeaa83SSudip Mukherjee 	return 0;
202d0aeaa83SSudip Mukherjee }
203d0aeaa83SSudip Mukherjee 
204d0aeaa83SSudip Mukherjee static int
205fc6cc961SJan Kiszka pci_fastcom335_setup(struct exar8250 *priv, struct pci_dev *pcidev,
206fc6cc961SJan Kiszka 		     struct uart_8250_port *port, int idx)
207fc6cc961SJan Kiszka {
208fc6cc961SJan Kiszka 	unsigned int offset = idx * 0x200;
209fc6cc961SJan Kiszka 	unsigned int baud = 1843200;
210fc6cc961SJan Kiszka 	u8 __iomem *p;
211fc6cc961SJan Kiszka 	int err;
212fc6cc961SJan Kiszka 
213fc6cc961SJan Kiszka 	port->port.uartclk = baud * 16;
214fc6cc961SJan Kiszka 
215fc6cc961SJan Kiszka 	err = default_setup(priv, pcidev, idx, offset, port);
216fc6cc961SJan Kiszka 	if (err)
217fc6cc961SJan Kiszka 		return err;
218fc6cc961SJan Kiszka 
219fc6cc961SJan Kiszka 	p = port->port.membase;
220fc6cc961SJan Kiszka 
221fc6cc961SJan Kiszka 	writeb(0x00, p + UART_EXAR_8XMODE);
222fc6cc961SJan Kiszka 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
223fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_TXTRG);
224fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_RXTRG);
225fc6cc961SJan Kiszka 
226fc6cc961SJan Kiszka 	/*
227fc6cc961SJan Kiszka 	 * Setup Multipurpose Input/Output pins.
228fc6cc961SJan Kiszka 	 */
229fc6cc961SJan Kiszka 	if (idx == 0) {
230fc6cc961SJan Kiszka 		switch (pcidev->device) {
231fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4222PCI335:
232fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4224PCI335:
233fc6cc961SJan Kiszka 			writeb(0x78, p + UART_EXAR_MPIOLVL_7_0);
234fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
235fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
236fc6cc961SJan Kiszka 			break;
237fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2324PCI335:
238fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2328PCI335:
239fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
240fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOINV_7_0);
241fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOSEL_7_0);
242fc6cc961SJan Kiszka 			break;
243fc6cc961SJan Kiszka 		}
244fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
245fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
246fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
247fc6cc961SJan Kiszka 	}
248fc6cc961SJan Kiszka 
249fc6cc961SJan Kiszka 	return 0;
250fc6cc961SJan Kiszka }
251fc6cc961SJan Kiszka 
252fc6cc961SJan Kiszka static int
253d0aeaa83SSudip Mukherjee pci_connect_tech_setup(struct exar8250 *priv, struct pci_dev *pcidev,
254d0aeaa83SSudip Mukherjee 		       struct uart_8250_port *port, int idx)
255d0aeaa83SSudip Mukherjee {
256d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
257d0aeaa83SSudip Mukherjee 	unsigned int baud = 1843200;
258d0aeaa83SSudip Mukherjee 
259d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
260d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
261d0aeaa83SSudip Mukherjee }
262d0aeaa83SSudip Mukherjee 
263d0aeaa83SSudip Mukherjee static int
264d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
265d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
266d0aeaa83SSudip Mukherjee {
267d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
268d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
269d0aeaa83SSudip Mukherjee 
270d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
271d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
272d0aeaa83SSudip Mukherjee }
273d0aeaa83SSudip Mukherjee 
274bea8be65SJan Kiszka static void setup_gpio(struct pci_dev *pcidev, u8 __iomem *p)
275d0aeaa83SSudip Mukherjee {
276bea8be65SJan Kiszka 	/*
277bea8be65SJan Kiszka 	 * The Commtech adapters required the MPIOs to be driven low. The Exar
278bea8be65SJan Kiszka 	 * devices will export them as GPIOs, so we pre-configure them safely
279bea8be65SJan Kiszka 	 * as inputs.
280bea8be65SJan Kiszka 	 */
281bea8be65SJan Kiszka 	u8 dir = pcidev->vendor == PCI_VENDOR_ID_EXAR ? 0xff : 0x00;
282bea8be65SJan Kiszka 
283d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
284d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
285d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
286d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
287bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_7_0);
288d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
289d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
290d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
291d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
292d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
293bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_15_8);
294d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
295d0aeaa83SSudip Mukherjee }
296d0aeaa83SSudip Mukherjee 
297d0aeaa83SSudip Mukherjee static void *
298380b1e2fSJan Kiszka __xr17v35x_register_gpio(struct pci_dev *pcidev,
299380b1e2fSJan Kiszka 			 const struct property_entry *properties)
300d0aeaa83SSudip Mukherjee {
301d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
302d0aeaa83SSudip Mukherjee 
303d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
304d0aeaa83SSudip Mukherjee 	if (!pdev)
305d0aeaa83SSudip Mukherjee 		return NULL;
306d0aeaa83SSudip Mukherjee 
307d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
3084076cf08SJan Kiszka 	ACPI_COMPANION_SET(&pdev->dev, ACPI_COMPANION(&pcidev->dev));
309d3936d74SJan Kiszka 
310380b1e2fSJan Kiszka 	if (platform_device_add_properties(pdev, properties) < 0 ||
311380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
312d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
313d0aeaa83SSudip Mukherjee 		return NULL;
314d0aeaa83SSudip Mukherjee 	}
315d0aeaa83SSudip Mukherjee 
316d0aeaa83SSudip Mukherjee 	return pdev;
317d0aeaa83SSudip Mukherjee }
318d0aeaa83SSudip Mukherjee 
319380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
320a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 0),
321380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
322380b1e2fSJan Kiszka 	{ }
323380b1e2fSJan Kiszka };
324380b1e2fSJan Kiszka 
3250d963ebfSJan Kiszka static int xr17v35x_register_gpio(struct pci_dev *pcidev,
3260d963ebfSJan Kiszka 				  struct uart_8250_port *port)
3270d963ebfSJan Kiszka {
3280d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
3290d963ebfSJan Kiszka 		port->port.private_data =
330380b1e2fSJan Kiszka 			__xr17v35x_register_gpio(pcidev, exar_gpio_properties);
3310d963ebfSJan Kiszka 
3320d963ebfSJan Kiszka 	return 0;
3330d963ebfSJan Kiszka }
3340d963ebfSJan Kiszka 
3359d939894SDaniel Golle static int generic_rs485_config(struct uart_port *port,
3369d939894SDaniel Golle 				struct serial_rs485 *rs485)
3379d939894SDaniel Golle {
3389d939894SDaniel Golle 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
3399d939894SDaniel Golle 	u8 __iomem *p = port->membase;
3409d939894SDaniel Golle 	u8 value;
3419d939894SDaniel Golle 
3429d939894SDaniel Golle 	value = readb(p + UART_EXAR_FCTR);
3439d939894SDaniel Golle 	if (is_rs485)
3449d939894SDaniel Golle 		value |= UART_FCTR_EXAR_485;
3459d939894SDaniel Golle 	else
3469d939894SDaniel Golle 		value &= ~UART_FCTR_EXAR_485;
3479d939894SDaniel Golle 
3489d939894SDaniel Golle 	writeb(value, p + UART_EXAR_FCTR);
3499d939894SDaniel Golle 
3509d939894SDaniel Golle 	if (is_rs485)
3519d939894SDaniel Golle 		writeb(UART_EXAR_RS485_DLY(4), p + UART_MSR);
3529d939894SDaniel Golle 
3539d939894SDaniel Golle 	port->rs485 = *rs485;
3549d939894SDaniel Golle 
3559d939894SDaniel Golle 	return 0;
3569d939894SDaniel Golle }
3579d939894SDaniel Golle 
3580d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
3590d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
3609d939894SDaniel Golle 	.rs485_config = generic_rs485_config,
3610d963ebfSJan Kiszka };
3620d963ebfSJan Kiszka 
363413058dfSJan Kiszka static int iot2040_rs485_config(struct uart_port *port,
364413058dfSJan Kiszka 				struct serial_rs485 *rs485)
365413058dfSJan Kiszka {
366413058dfSJan Kiszka 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
367413058dfSJan Kiszka 	u8 __iomem *p = port->membase;
368413058dfSJan Kiszka 	u8 mask = IOT2040_UART1_MASK;
369413058dfSJan Kiszka 	u8 mode, value;
370413058dfSJan Kiszka 
371413058dfSJan Kiszka 	if (is_rs485) {
372413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_RX_DURING_TX)
373413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS422;
374413058dfSJan Kiszka 		else
375413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS485;
376413058dfSJan Kiszka 
377413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_TERMINATE_BUS)
378413058dfSJan Kiszka 			mode |= IOT2040_UART_TERMINATE_BUS;
379413058dfSJan Kiszka 	} else {
380413058dfSJan Kiszka 		mode = IOT2040_UART_MODE_RS232;
381413058dfSJan Kiszka 	}
382413058dfSJan Kiszka 
383413058dfSJan Kiszka 	if (port->line == 3) {
384413058dfSJan Kiszka 		mask <<= IOT2040_UART2_SHIFT;
385413058dfSJan Kiszka 		mode <<= IOT2040_UART2_SHIFT;
386413058dfSJan Kiszka 	}
387413058dfSJan Kiszka 
388413058dfSJan Kiszka 	value = readb(p + UART_EXAR_MPIOLVL_7_0);
389413058dfSJan Kiszka 	value &= ~mask;
390413058dfSJan Kiszka 	value |= mode;
391413058dfSJan Kiszka 	writeb(value, p + UART_EXAR_MPIOLVL_7_0);
392413058dfSJan Kiszka 
3939d939894SDaniel Golle 	return generic_rs485_config(port, rs485);
394413058dfSJan Kiszka }
395413058dfSJan Kiszka 
396413058dfSJan Kiszka static const struct property_entry iot2040_gpio_properties[] = {
397a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 10),
398413058dfSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 1),
399413058dfSJan Kiszka 	{ }
400413058dfSJan Kiszka };
401413058dfSJan Kiszka 
402413058dfSJan Kiszka static int iot2040_register_gpio(struct pci_dev *pcidev,
403413058dfSJan Kiszka 			      struct uart_8250_port *port)
404413058dfSJan Kiszka {
405413058dfSJan Kiszka 	u8 __iomem *p = port->port.membase;
406413058dfSJan Kiszka 
407413058dfSJan Kiszka 	writeb(IOT2040_UARTS_DEFAULT_MODE, p + UART_EXAR_MPIOLVL_7_0);
408413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_LO_MODE, p + UART_EXAR_MPIOSEL_7_0);
409413058dfSJan Kiszka 	writeb(IOT2040_UARTS_ENABLE, p + UART_EXAR_MPIOLVL_15_8);
410413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_HI_MODE, p + UART_EXAR_MPIOSEL_15_8);
411413058dfSJan Kiszka 
412413058dfSJan Kiszka 	port->port.private_data =
413413058dfSJan Kiszka 		__xr17v35x_register_gpio(pcidev, iot2040_gpio_properties);
414413058dfSJan Kiszka 
415413058dfSJan Kiszka 	return 0;
416413058dfSJan Kiszka }
417413058dfSJan Kiszka 
418413058dfSJan Kiszka static const struct exar8250_platform iot2040_platform = {
419413058dfSJan Kiszka 	.rs485_config = iot2040_rs485_config,
420413058dfSJan Kiszka 	.register_gpio = iot2040_register_gpio,
421413058dfSJan Kiszka };
422413058dfSJan Kiszka 
4233e51ceeaSSu Bao Cheng /*
4243e51ceeaSSu Bao Cheng  * For SIMATIC IOT2000, only IOT2040 and its variants have the Exar device,
4253e51ceeaSSu Bao Cheng  * IOT2020 doesn't have. Therefore it is sufficient to match on the common
4263e51ceeaSSu Bao Cheng  * board name after the device was found.
4273e51ceeaSSu Bao Cheng  */
428413058dfSJan Kiszka static const struct dmi_system_id exar_platforms[] = {
429413058dfSJan Kiszka 	{
430413058dfSJan Kiszka 		.matches = {
431413058dfSJan Kiszka 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
432413058dfSJan Kiszka 		},
433413058dfSJan Kiszka 		.driver_data = (void *)&iot2040_platform,
434413058dfSJan Kiszka 	},
435413058dfSJan Kiszka 	{}
436413058dfSJan Kiszka };
437413058dfSJan Kiszka 
438d0aeaa83SSudip Mukherjee static int
439d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
440d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
441d0aeaa83SSudip Mukherjee {
4420d963ebfSJan Kiszka 	const struct exar8250_platform *platform;
443413058dfSJan Kiszka 	const struct dmi_system_id *dmi_match;
444d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
445d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
446d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
447d0aeaa83SSudip Mukherjee 	int ret;
448d0aeaa83SSudip Mukherjee 
449413058dfSJan Kiszka 	dmi_match = dmi_first_match(exar_platforms);
450413058dfSJan Kiszka 	if (dmi_match)
451413058dfSJan Kiszka 		platform = dmi_match->driver_data;
452413058dfSJan Kiszka 	else
4530d963ebfSJan Kiszka 		platform = &exar8250_default_platform;
4540d963ebfSJan Kiszka 
455d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
4560d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
4570d963ebfSJan Kiszka 
458d0aeaa83SSudip Mukherjee 	/*
459328c11f2SAndy Shevchenko 	 * Setup the UART clock for the devices on expansion slot to
460d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
461d0aeaa83SSudip Mukherjee 	 */
462328c11f2SAndy Shevchenko 	if (idx >= 8)
463d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
464d0aeaa83SSudip Mukherjee 
4655b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
4665b5f252dSJan Kiszka 	if (ret)
4675b5f252dSJan Kiszka 		return ret;
468d0aeaa83SSudip Mukherjee 
4695b5f252dSJan Kiszka 	p = port->port.membase;
470d0aeaa83SSudip Mukherjee 
471d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
472d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
473d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
474d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
475d0aeaa83SSudip Mukherjee 
4765b5f252dSJan Kiszka 	if (idx == 0) {
4775b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
478bea8be65SJan Kiszka 		setup_gpio(pcidev, p);
479d0aeaa83SSudip Mukherjee 
4800d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
4815b5f252dSJan Kiszka 	}
482d0aeaa83SSudip Mukherjee 
4830d963ebfSJan Kiszka 	return ret;
484d0aeaa83SSudip Mukherjee }
485d0aeaa83SSudip Mukherjee 
486d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
487d0aeaa83SSudip Mukherjee {
488d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
489d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
490d0aeaa83SSudip Mukherjee 	struct platform_device *pdev = port->port.private_data;
491d0aeaa83SSudip Mukherjee 
492d0aeaa83SSudip Mukherjee 	platform_device_unregister(pdev);
493d0aeaa83SSudip Mukherjee 	port->port.private_data = NULL;
494d0aeaa83SSudip Mukherjee }
495d0aeaa83SSudip Mukherjee 
496*72169e42SAaron Sierra static inline void exar_misc_clear(struct exar8250 *priv)
497*72169e42SAaron Sierra {
498*72169e42SAaron Sierra 	/* Clear all PCI interrupts by reading INT0. No effect on IIR */
499*72169e42SAaron Sierra 	readb(priv->virt + UART_EXAR_INT0);
500*72169e42SAaron Sierra 
501*72169e42SAaron Sierra 	/* Clear INT0 for Expansion Interface slave ports, too */
502*72169e42SAaron Sierra 	if (priv->board->num_ports > 8)
503*72169e42SAaron Sierra 		readb(priv->virt + 0x2000 + UART_EXAR_INT0);
504*72169e42SAaron Sierra }
505*72169e42SAaron Sierra 
506c7e1b405SAaron Sierra /*
507c7e1b405SAaron Sierra  * These Exar UARTs have an extra interrupt indicator that could fire for a
508c7e1b405SAaron Sierra  * few interrupts that are not presented/cleared through IIR.  One of which is
509c7e1b405SAaron Sierra  * a wakeup interrupt when coming out of sleep.  These interrupts are only
510c7e1b405SAaron Sierra  * cleared by reading global INT0 or INT1 registers as interrupts are
511c7e1b405SAaron Sierra  * associated with channel 0. The INT[3:0] registers _are_ accessible from each
512c7e1b405SAaron Sierra  * channel's address space, but for the sake of bus efficiency we register a
513c7e1b405SAaron Sierra  * dedicated handler at the PCI device level to handle them.
514c7e1b405SAaron Sierra  */
515c7e1b405SAaron Sierra static irqreturn_t exar_misc_handler(int irq, void *data)
516c7e1b405SAaron Sierra {
517*72169e42SAaron Sierra 	exar_misc_clear(data);
518c7e1b405SAaron Sierra 
519c7e1b405SAaron Sierra 	return IRQ_HANDLED;
520c7e1b405SAaron Sierra }
521c7e1b405SAaron Sierra 
52247b1747fSRobert Middleton static void
52347b1747fSRobert Middleton exar_shutdown(struct uart_port *port)
52447b1747fSRobert Middleton {
52547b1747fSRobert Middleton 	unsigned char lsr;
52647b1747fSRobert Middleton 	bool tx_complete = 0;
52747b1747fSRobert Middleton 	struct uart_8250_port *up = up_to_u8250p(port);
52847b1747fSRobert Middleton 	struct circ_buf *xmit = &port->state->xmit;
52947b1747fSRobert Middleton 	int i = 0;
53047b1747fSRobert Middleton 
53147b1747fSRobert Middleton 	do {
53247b1747fSRobert Middleton 		lsr = serial_in(up, UART_LSR);
53347b1747fSRobert Middleton 		if (lsr & (UART_LSR_TEMT | UART_LSR_THRE))
53447b1747fSRobert Middleton 			tx_complete = 1;
53547b1747fSRobert Middleton 		else
53647b1747fSRobert Middleton 			tx_complete = 0;
53747b1747fSRobert Middleton 		msleep(1);
53847b1747fSRobert Middleton 	} while (!uart_circ_empty(xmit) && !tx_complete && i++ < 1000);
53947b1747fSRobert Middleton 
54047b1747fSRobert Middleton 	serial8250_do_shutdown(port);
54147b1747fSRobert Middleton }
54247b1747fSRobert Middleton 
543d0aeaa83SSudip Mukherjee static int
544d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
545d0aeaa83SSudip Mukherjee {
546d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
547d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
548d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
549d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
550d0aeaa83SSudip Mukherjee 	int rc;
551d0aeaa83SSudip Mukherjee 
552d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
553d0aeaa83SSudip Mukherjee 	if (!board)
554d0aeaa83SSudip Mukherjee 		return -EINVAL;
555d0aeaa83SSudip Mukherjee 
556d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
557d0aeaa83SSudip Mukherjee 	if (rc)
558d0aeaa83SSudip Mukherjee 		return rc;
559d0aeaa83SSudip Mukherjee 
560d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
561d0aeaa83SSudip Mukherjee 
562d0aeaa83SSudip Mukherjee 	nr_ports = board->num_ports ? board->num_ports : pcidev->device & 0x0f;
563d0aeaa83SSudip Mukherjee 
564df60a8afSAndy Shevchenko 	priv = devm_kzalloc(&pcidev->dev, struct_size(priv, line, nr_ports), GFP_KERNEL);
565d0aeaa83SSudip Mukherjee 	if (!priv)
566d0aeaa83SSudip Mukherjee 		return -ENOMEM;
567d0aeaa83SSudip Mukherjee 
568d0aeaa83SSudip Mukherjee 	priv->board = board;
569c7e1b405SAaron Sierra 	priv->virt = pcim_iomap(pcidev, bar, 0);
570c7e1b405SAaron Sierra 	if (!priv->virt)
571c7e1b405SAaron Sierra 		return -ENOMEM;
572d0aeaa83SSudip Mukherjee 
573172c33cbSJan Kiszka 	pci_set_master(pcidev);
574172c33cbSJan Kiszka 
575172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
576172c33cbSJan Kiszka 	if (rc < 0)
577172c33cbSJan Kiszka 		return rc;
578172c33cbSJan Kiszka 
579d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
5806be254c2SAndy Shevchenko 	uart.port.flags = UPF_SHARE_IRQ | UPF_EXAR_EFR | UPF_FIXED_TYPE | UPF_FIXED_PORT;
581172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
582d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
58347b1747fSRobert Middleton 	uart.port.shutdown = exar_shutdown;
584d0aeaa83SSudip Mukherjee 
585c7e1b405SAaron Sierra 	rc = devm_request_irq(&pcidev->dev, uart.port.irq, exar_misc_handler,
586c7e1b405SAaron Sierra 			 IRQF_SHARED, "exar_uart", priv);
587c7e1b405SAaron Sierra 	if (rc)
588c7e1b405SAaron Sierra 		return rc;
589c7e1b405SAaron Sierra 
590*72169e42SAaron Sierra 	/* Clear interrupts */
591*72169e42SAaron Sierra 	exar_misc_clear(priv);
592*72169e42SAaron Sierra 
593d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
594d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
595d0aeaa83SSudip Mukherjee 		if (rc) {
596d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
597d0aeaa83SSudip Mukherjee 			break;
598d0aeaa83SSudip Mukherjee 		}
599d0aeaa83SSudip Mukherjee 
600d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
601d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
602d0aeaa83SSudip Mukherjee 
603d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
604d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
605d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
606d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
607d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
608d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
609d0aeaa83SSudip Mukherjee 			break;
610d0aeaa83SSudip Mukherjee 		}
611d0aeaa83SSudip Mukherjee 	}
612d0aeaa83SSudip Mukherjee 	priv->nr = i;
613d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
614d0aeaa83SSudip Mukherjee 	return 0;
615d0aeaa83SSudip Mukherjee }
616d0aeaa83SSudip Mukherjee 
617d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
618d0aeaa83SSudip Mukherjee {
619d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
620d0aeaa83SSudip Mukherjee 	unsigned int i;
621d0aeaa83SSudip Mukherjee 
622d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
623d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
624d0aeaa83SSudip Mukherjee 
625d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
626d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
627d0aeaa83SSudip Mukherjee }
628d0aeaa83SSudip Mukherjee 
629d0aeaa83SSudip Mukherjee static int __maybe_unused exar_suspend(struct device *dev)
630d0aeaa83SSudip Mukherjee {
631d0aeaa83SSudip Mukherjee 	struct pci_dev *pcidev = to_pci_dev(dev);
632d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
633d0aeaa83SSudip Mukherjee 	unsigned int i;
634d0aeaa83SSudip Mukherjee 
635d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
636d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
637d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
638d0aeaa83SSudip Mukherjee 
639d0aeaa83SSudip Mukherjee 	/* Ensure that every init quirk is properly torn down */
640d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
641d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
642d0aeaa83SSudip Mukherjee 
643d0aeaa83SSudip Mukherjee 	return 0;
644d0aeaa83SSudip Mukherjee }
645d0aeaa83SSudip Mukherjee 
646d0aeaa83SSudip Mukherjee static int __maybe_unused exar_resume(struct device *dev)
647d0aeaa83SSudip Mukherjee {
64876b4106cSChuhong Yuan 	struct exar8250 *priv = dev_get_drvdata(dev);
649d0aeaa83SSudip Mukherjee 	unsigned int i;
650d0aeaa83SSudip Mukherjee 
651*72169e42SAaron Sierra 	exar_misc_clear(priv);
652*72169e42SAaron Sierra 
653d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
654d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
655d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
656d0aeaa83SSudip Mukherjee 
657d0aeaa83SSudip Mukherjee 	return 0;
658d0aeaa83SSudip Mukherjee }
659d0aeaa83SSudip Mukherjee 
660d0aeaa83SSudip Mukherjee static SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
661d0aeaa83SSudip Mukherjee 
662fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
663fc6cc961SJan Kiszka 	.num_ports	= 2,
664fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
665fc6cc961SJan Kiszka };
666fc6cc961SJan Kiszka 
667fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
668fc6cc961SJan Kiszka 	.num_ports	= 4,
669fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
670fc6cc961SJan Kiszka };
671fc6cc961SJan Kiszka 
672fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
673fc6cc961SJan Kiszka 	.num_ports	= 8,
674fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
675fc6cc961SJan Kiszka };
676fc6cc961SJan Kiszka 
677d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_connect = {
678d0aeaa83SSudip Mukherjee 	.setup		= pci_connect_tech_setup,
679d0aeaa83SSudip Mukherjee };
680d0aeaa83SSudip Mukherjee 
681d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
682d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
683d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
684d0aeaa83SSudip Mukherjee };
685d0aeaa83SSudip Mukherjee 
686d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
687d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
688d0aeaa83SSudip Mukherjee };
689d0aeaa83SSudip Mukherjee 
690d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
691d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
692d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
693d0aeaa83SSudip Mukherjee };
694d0aeaa83SSudip Mukherjee 
695d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
696d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
697d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
698d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
699d0aeaa83SSudip Mukherjee };
700d0aeaa83SSudip Mukherjee 
701d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
702d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
703d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
704d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
705d0aeaa83SSudip Mukherjee };
706d0aeaa83SSudip Mukherjee 
707d0aeaa83SSudip Mukherjee #define CONNECT_DEVICE(devid, sdevid, bd) {				\
708d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(							\
709d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,					\
710d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,				\
711d0aeaa83SSudip Mukherjee 		PCI_SUBVENDOR_ID_CONNECT_TECH,				\
712d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_##sdevid), 0, 0,	\
713d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd					\
714d0aeaa83SSudip Mukherjee 	}
715d0aeaa83SSudip Mukherjee 
716d0aeaa83SSudip Mukherjee #define EXAR_DEVICE(vend, devid, bd) {					\
717d0aeaa83SSudip Mukherjee 	PCI_VDEVICE(vend, PCI_DEVICE_ID_##devid), (kernel_ulong_t)&bd	\
718d0aeaa83SSudip Mukherjee 	}
719d0aeaa83SSudip Mukherjee 
720d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
721d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
722d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
723d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
724d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_IBM,			\
725d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
726d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
727d0aeaa83SSudip Mukherjee 	}
728d0aeaa83SSudip Mukherjee 
7293637c460SArvind Yadav static const struct pci_device_id exar_pci_tbl[] = {
730d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_232, pbn_connect),
731d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_232, pbn_connect),
732d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_232, pbn_connect),
733d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_1_1, pbn_connect),
734d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_2_2, pbn_connect),
735d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_4_4, pbn_connect),
736d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2, pbn_connect),
737d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4, pbn_connect),
738d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8, pbn_connect),
739d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_485, pbn_connect),
740d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_485, pbn_connect),
741d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_485, pbn_connect),
742d0aeaa83SSudip Mukherjee 
743d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
744d0aeaa83SSudip Mukherjee 
745d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
746d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C152, pbn_exar_XR17C15x),
747d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C154, pbn_exar_XR17C15x),
748d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C158, pbn_exar_XR17C15x),
749d0aeaa83SSudip Mukherjee 
750d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
751d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V352, pbn_exar_XR17V35x),
752d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V354, pbn_exar_XR17V35x),
753d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V358, pbn_exar_XR17V35x),
754d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V4358, pbn_exar_XR17V4358),
755d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V8358, pbn_exar_XR17V8358),
756d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4222PCIE, pbn_exar_XR17V35x),
757d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4224PCIE, pbn_exar_XR17V35x),
758d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4228PCIE, pbn_exar_XR17V35x),
759fc6cc961SJan Kiszka 
760fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_4222PCI335, pbn_fastcom335_2),
761fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_4224PCI335, pbn_fastcom335_4),
762fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_2324PCI335, pbn_fastcom335_4),
763fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_2328PCI335, pbn_fastcom335_8),
764d0aeaa83SSudip Mukherjee 	{ 0, }
765d0aeaa83SSudip Mukherjee };
766d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
767d0aeaa83SSudip Mukherjee 
768d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
769d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
770d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
771d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
772d0aeaa83SSudip Mukherjee 	.driver         = {
773d0aeaa83SSudip Mukherjee 		.pm     = &exar_pci_pm,
774d0aeaa83SSudip Mukherjee 	},
775d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
776d0aeaa83SSudip Mukherjee };
777d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
778d0aeaa83SSudip Mukherjee 
779d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
7802b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
781d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
782