xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision 81171e7d31a63e0e7f5c83701f097695e60a12db)
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 
2824637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2S		0x1052
2924637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4S		0x105d
3024637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8S		0x106c
3124637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM232_8		0x10a8
3224637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_2SM		0x10d2
3324637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_4SM		0x10db
3424637007SAndy Shevchenko #define PCI_DEVICE_ID_ACCESSIO_COM_8SM		0x10ea
3510c5ccc3SJay Dolan 
36fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4224PCI335	0x0002
37fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4222PCI335	0x0004
38fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2324PCI335	0x000a
39fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2328PCI335	0x000b
40d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4224PCIE		0x0020
41d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4228PCIE		0x0021
42d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4222PCIE		0x0022
43d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V4358		0x4358
44d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V8358		0x8358
45d0aeaa83SSudip Mukherjee 
46c7e1b405SAaron Sierra #define UART_EXAR_INT0		0x80
477e12357eSJan Kiszka #define UART_EXAR_8XMODE	0x88	/* 8X sampling rate select */
48ef4e281eSAndy Shevchenko #define UART_EXAR_SLEEP		0x8b	/* Sleep mode */
496be254c2SAndy Shevchenko #define UART_EXAR_DVID		0x8d	/* Device identification */
507e12357eSJan Kiszka 
517e12357eSJan Kiszka #define UART_EXAR_FCTR		0x08	/* Feature Control Register */
527e12357eSJan Kiszka #define UART_FCTR_EXAR_IRDA	0x10	/* IrDa data encode select */
537e12357eSJan Kiszka #define UART_FCTR_EXAR_485	0x20	/* Auto 485 half duplex dir ctl */
547e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGA	0x00	/* FIFO trigger table A */
557e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGB	0x60	/* FIFO trigger table B */
567e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGC	0x80	/* FIFO trigger table C */
577e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGD	0xc0	/* FIFO trigger table D programmable */
587e12357eSJan Kiszka 
597e12357eSJan Kiszka #define UART_EXAR_TXTRG		0x0a	/* Tx FIFO trigger level write-only */
607e12357eSJan Kiszka #define UART_EXAR_RXTRG		0x0b	/* Rx FIFO trigger level write-only */
617e12357eSJan Kiszka 
62d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_7_0	0x8f	/* MPIOINT[7:0] */
63d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_7_0	0x90	/* MPIOLVL[7:0] */
64d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_7_0	0x91	/* MPIO3T[7:0] */
65d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_7_0	0x92	/* MPIOINV[7:0] */
66d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_7_0	0x93	/* MPIOSEL[7:0] */
67d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_7_0	0x94	/* MPIOOD[7:0] */
68d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_15_8	0x95	/* MPIOINT[15:8] */
69d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_15_8	0x96	/* MPIOLVL[15:8] */
70d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_15_8	0x97	/* MPIO3T[15:8] */
71d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_15_8	0x98	/* MPIOINV[15:8] */
72d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_15_8	0x99	/* MPIOSEL[15:8] */
73d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_15_8	0x9a	/* MPIOOD[15:8] */
74d0aeaa83SSudip Mukherjee 
75413058dfSJan Kiszka #define UART_EXAR_RS485_DLY(x)	((x) << 4)
76413058dfSJan Kiszka 
77413058dfSJan Kiszka /*
78413058dfSJan Kiszka  * IOT2040 MPIO wiring semantics:
79413058dfSJan Kiszka  *
80413058dfSJan Kiszka  * MPIO		Port	Function
81413058dfSJan Kiszka  * ----		----	--------
82413058dfSJan Kiszka  * 0		2 	Mode bit 0
83413058dfSJan Kiszka  * 1		2	Mode bit 1
84413058dfSJan Kiszka  * 2		2	Terminate bus
85413058dfSJan Kiszka  * 3		-	<reserved>
86413058dfSJan Kiszka  * 4		3	Mode bit 0
87413058dfSJan Kiszka  * 5		3	Mode bit 1
88413058dfSJan Kiszka  * 6		3	Terminate bus
89413058dfSJan Kiszka  * 7		-	<reserved>
90413058dfSJan Kiszka  * 8		2	Enable
91413058dfSJan Kiszka  * 9		3	Enable
92413058dfSJan Kiszka  * 10		-	Red LED
93413058dfSJan Kiszka  * 11..15	-	<unused>
94413058dfSJan Kiszka  */
95413058dfSJan Kiszka 
96413058dfSJan Kiszka /* IOT2040 MPIOs 0..7 */
97413058dfSJan Kiszka #define IOT2040_UART_MODE_RS232		0x01
98413058dfSJan Kiszka #define IOT2040_UART_MODE_RS485		0x02
99413058dfSJan Kiszka #define IOT2040_UART_MODE_RS422		0x03
100413058dfSJan Kiszka #define IOT2040_UART_TERMINATE_BUS	0x04
101413058dfSJan Kiszka 
102413058dfSJan Kiszka #define IOT2040_UART1_MASK		0x0f
103413058dfSJan Kiszka #define IOT2040_UART2_SHIFT		4
104413058dfSJan Kiszka 
105413058dfSJan Kiszka #define IOT2040_UARTS_DEFAULT_MODE	0x11	/* both RS232 */
106413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_LO_MODE	0x88	/* reserved pins as input */
107413058dfSJan Kiszka 
108413058dfSJan Kiszka /* IOT2040 MPIOs 8..15 */
109413058dfSJan Kiszka #define IOT2040_UARTS_ENABLE		0x03
110413058dfSJan Kiszka #define IOT2040_UARTS_GPIO_HI_MODE	0xF8	/* enable & LED as outputs */
111413058dfSJan Kiszka 
112d0aeaa83SSudip Mukherjee struct exar8250;
113d0aeaa83SSudip Mukherjee 
1140d963ebfSJan Kiszka struct exar8250_platform {
1150d963ebfSJan Kiszka 	int (*rs485_config)(struct uart_port *, struct serial_rs485 *);
1160d963ebfSJan Kiszka 	int (*register_gpio)(struct pci_dev *, struct uart_8250_port *);
1170d963ebfSJan Kiszka };
1180d963ebfSJan Kiszka 
119d0aeaa83SSudip Mukherjee /**
120d0aeaa83SSudip Mukherjee  * struct exar8250_board - board information
121d0aeaa83SSudip Mukherjee  * @num_ports: number of serial ports
122d0aeaa83SSudip Mukherjee  * @reg_shift: describes UART register mapping in PCI memory
12326f22d57SAndy Shevchenko  * @setup: quirk run at ->probe() stage
12426f22d57SAndy Shevchenko  * @exit: quirk run at ->remove() stage
125d0aeaa83SSudip Mukherjee  */
126d0aeaa83SSudip Mukherjee struct exar8250_board {
127d0aeaa83SSudip Mukherjee 	unsigned int num_ports;
128d0aeaa83SSudip Mukherjee 	unsigned int reg_shift;
129d0aeaa83SSudip Mukherjee 	int	(*setup)(struct exar8250 *, struct pci_dev *,
130d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *, int);
131d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
132d0aeaa83SSudip Mukherjee };
133d0aeaa83SSudip Mukherjee 
134d0aeaa83SSudip Mukherjee struct exar8250 {
135d0aeaa83SSudip Mukherjee 	unsigned int		nr;
136d0aeaa83SSudip Mukherjee 	struct exar8250_board	*board;
137c7e1b405SAaron Sierra 	void __iomem		*virt;
13800d963abSGustavo A. R. Silva 	int			line[];
139d0aeaa83SSudip Mukherjee };
140d0aeaa83SSudip Mukherjee 
141ef4e281eSAndy Shevchenko static void exar_pm(struct uart_port *port, unsigned int state, unsigned int old)
142ef4e281eSAndy Shevchenko {
143ef4e281eSAndy Shevchenko 	/*
144ef4e281eSAndy Shevchenko 	 * Exar UARTs have a SLEEP register that enables or disables each UART
145ef4e281eSAndy Shevchenko 	 * to enter sleep mode separately. On the XR17V35x the register
146ef4e281eSAndy Shevchenko 	 * is accessible to each UART at the UART_EXAR_SLEEP offset, but
147ef4e281eSAndy Shevchenko 	 * the UART channel may only write to the corresponding bit.
148ef4e281eSAndy Shevchenko 	 */
149ef4e281eSAndy Shevchenko 	serial_port_out(port, UART_EXAR_SLEEP, state ? 0xff : 0);
150ef4e281eSAndy Shevchenko }
151ef4e281eSAndy Shevchenko 
152b2b4b8edSAndy Shevchenko /*
153b2b4b8edSAndy Shevchenko  * XR17V35x UARTs have an extra fractional divisor register (DLD)
154b2b4b8edSAndy Shevchenko  * Calculate divisor with extra 4-bit fractional portion
155b2b4b8edSAndy Shevchenko  */
156b2b4b8edSAndy Shevchenko static unsigned int xr17v35x_get_divisor(struct uart_port *p, unsigned int baud,
157b2b4b8edSAndy Shevchenko 					 unsigned int *frac)
158b2b4b8edSAndy Shevchenko {
159b2b4b8edSAndy Shevchenko 	unsigned int quot_16;
160b2b4b8edSAndy Shevchenko 
161b2b4b8edSAndy Shevchenko 	quot_16 = DIV_ROUND_CLOSEST(p->uartclk, baud);
162b2b4b8edSAndy Shevchenko 	*frac = quot_16 & 0x0f;
163b2b4b8edSAndy Shevchenko 
164b2b4b8edSAndy Shevchenko 	return quot_16 >> 4;
165b2b4b8edSAndy Shevchenko }
166b2b4b8edSAndy Shevchenko 
167b2b4b8edSAndy Shevchenko static void xr17v35x_set_divisor(struct uart_port *p, unsigned int baud,
168b2b4b8edSAndy Shevchenko 				 unsigned int quot, unsigned int quot_frac)
169b2b4b8edSAndy Shevchenko {
170b2b4b8edSAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
171b2b4b8edSAndy Shevchenko 
172b2b4b8edSAndy Shevchenko 	/* Preserve bits not related to baudrate; DLD[7:4]. */
173b2b4b8edSAndy Shevchenko 	quot_frac |= serial_port_in(p, 0x2) & 0xf0;
174b2b4b8edSAndy Shevchenko 	serial_port_out(p, 0x2, quot_frac);
175b2b4b8edSAndy Shevchenko }
176b2b4b8edSAndy Shevchenko 
1776e731137SAndy Shevchenko static int xr17v35x_startup(struct uart_port *port)
1786e731137SAndy Shevchenko {
1796e731137SAndy Shevchenko 	/*
1806e731137SAndy Shevchenko 	 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
1816e731137SAndy Shevchenko 	 * MCR [7:5] and MSR [7:0]
1826e731137SAndy Shevchenko 	 */
1836e731137SAndy Shevchenko 	serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
1846e731137SAndy Shevchenko 
1856e731137SAndy Shevchenko 	/*
1866e731137SAndy Shevchenko 	 * Make sure all interrups are masked until initialization is
1876e731137SAndy Shevchenko 	 * complete and the FIFOs are cleared
1886e731137SAndy Shevchenko 	 */
1896e731137SAndy Shevchenko 	serial_port_out(port, UART_IER, 0);
1906e731137SAndy Shevchenko 
1916e731137SAndy Shevchenko 	return serial8250_do_startup(port);
1926e731137SAndy Shevchenko }
1936e731137SAndy Shevchenko 
194653d00c8SAndy Shevchenko static void exar_shutdown(struct uart_port *port)
195653d00c8SAndy Shevchenko {
196653d00c8SAndy Shevchenko 	unsigned char lsr;
19767e977f3SZheng Bin 	bool tx_complete = false;
198653d00c8SAndy Shevchenko 	struct uart_8250_port *up = up_to_u8250p(port);
199653d00c8SAndy Shevchenko 	struct circ_buf *xmit = &port->state->xmit;
200653d00c8SAndy Shevchenko 	int i = 0;
201653d00c8SAndy Shevchenko 
202653d00c8SAndy Shevchenko 	do {
203653d00c8SAndy Shevchenko 		lsr = serial_in(up, UART_LSR);
204653d00c8SAndy Shevchenko 		if (lsr & (UART_LSR_TEMT | UART_LSR_THRE))
20567e977f3SZheng Bin 			tx_complete = true;
206653d00c8SAndy Shevchenko 		else
20767e977f3SZheng Bin 			tx_complete = false;
2083f72879eSAndy Shevchenko 		usleep_range(1000, 1100);
209653d00c8SAndy Shevchenko 	} while (!uart_circ_empty(xmit) && !tx_complete && i++ < 1000);
210653d00c8SAndy Shevchenko 
211653d00c8SAndy Shevchenko 	serial8250_do_shutdown(port);
212653d00c8SAndy Shevchenko }
213653d00c8SAndy Shevchenko 
214d0aeaa83SSudip Mukherjee static int default_setup(struct exar8250 *priv, struct pci_dev *pcidev,
215d0aeaa83SSudip Mukherjee 			 int idx, unsigned int offset,
216d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *port)
217d0aeaa83SSudip Mukherjee {
218d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
219d0aeaa83SSudip Mukherjee 	unsigned int bar = 0;
2206be254c2SAndy Shevchenko 	unsigned char status;
221d0aeaa83SSudip Mukherjee 
222d0aeaa83SSudip Mukherjee 	port->port.iotype = UPIO_MEM;
223d0aeaa83SSudip Mukherjee 	port->port.mapbase = pci_resource_start(pcidev, bar) + offset;
224c7e1b405SAaron Sierra 	port->port.membase = priv->virt + offset;
225d0aeaa83SSudip Mukherjee 	port->port.regshift = board->reg_shift;
226d0aeaa83SSudip Mukherjee 
2276be254c2SAndy Shevchenko 	/*
2286be254c2SAndy Shevchenko 	 * XR17V35x UARTs have an extra divisor register, DLD that gets enabled
2296be254c2SAndy Shevchenko 	 * with when DLAB is set which will cause the device to incorrectly match
2306be254c2SAndy Shevchenko 	 * and assign port type to PORT_16650. The EFR for this UART is found
2316be254c2SAndy Shevchenko 	 * at offset 0x09. Instead check the Deice ID (DVID) register
2326be254c2SAndy Shevchenko 	 * for a 2, 4 or 8 port UART.
2336be254c2SAndy Shevchenko 	 */
2346be254c2SAndy Shevchenko 	status = readb(port->port.membase + UART_EXAR_DVID);
2356be254c2SAndy Shevchenko 	if (status == 0x82 || status == 0x84 || status == 0x88) {
2366be254c2SAndy Shevchenko 		port->port.type = PORT_XR17V35X;
237b2b4b8edSAndy Shevchenko 
238b2b4b8edSAndy Shevchenko 		port->port.get_divisor = xr17v35x_get_divisor;
239b2b4b8edSAndy Shevchenko 		port->port.set_divisor = xr17v35x_set_divisor;
2406e731137SAndy Shevchenko 
2416e731137SAndy Shevchenko 		port->port.startup = xr17v35x_startup;
2426be254c2SAndy Shevchenko 	} else {
2436be254c2SAndy Shevchenko 		port->port.type = PORT_XR17D15X;
2446be254c2SAndy Shevchenko 	}
2456be254c2SAndy Shevchenko 
246ef4e281eSAndy Shevchenko 	port->port.pm = exar_pm;
247653d00c8SAndy Shevchenko 	port->port.shutdown = exar_shutdown;
248ef4e281eSAndy Shevchenko 
249d0aeaa83SSudip Mukherjee 	return 0;
250d0aeaa83SSudip Mukherjee }
251d0aeaa83SSudip Mukherjee 
252d0aeaa83SSudip Mukherjee static int
253fc6cc961SJan Kiszka pci_fastcom335_setup(struct exar8250 *priv, struct pci_dev *pcidev,
254fc6cc961SJan Kiszka 		     struct uart_8250_port *port, int idx)
255fc6cc961SJan Kiszka {
256fc6cc961SJan Kiszka 	unsigned int offset = idx * 0x200;
257fc6cc961SJan Kiszka 	unsigned int baud = 1843200;
258fc6cc961SJan Kiszka 	u8 __iomem *p;
259fc6cc961SJan Kiszka 	int err;
260fc6cc961SJan Kiszka 
261fc6cc961SJan Kiszka 	port->port.uartclk = baud * 16;
262fc6cc961SJan Kiszka 
263fc6cc961SJan Kiszka 	err = default_setup(priv, pcidev, idx, offset, port);
264fc6cc961SJan Kiszka 	if (err)
265fc6cc961SJan Kiszka 		return err;
266fc6cc961SJan Kiszka 
267fc6cc961SJan Kiszka 	p = port->port.membase;
268fc6cc961SJan Kiszka 
269fc6cc961SJan Kiszka 	writeb(0x00, p + UART_EXAR_8XMODE);
270fc6cc961SJan Kiszka 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
271fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_TXTRG);
272fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_RXTRG);
273fc6cc961SJan Kiszka 
274fc6cc961SJan Kiszka 	/*
275fc6cc961SJan Kiszka 	 * Setup Multipurpose Input/Output pins.
276fc6cc961SJan Kiszka 	 */
277fc6cc961SJan Kiszka 	if (idx == 0) {
278fc6cc961SJan Kiszka 		switch (pcidev->device) {
279fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4222PCI335:
280fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4224PCI335:
281fc6cc961SJan Kiszka 			writeb(0x78, p + UART_EXAR_MPIOLVL_7_0);
282fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
283fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
284fc6cc961SJan Kiszka 			break;
285fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2324PCI335:
286fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2328PCI335:
287fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
288fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOINV_7_0);
289fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOSEL_7_0);
290fc6cc961SJan Kiszka 			break;
291fc6cc961SJan Kiszka 		}
292fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
293fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
294fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
295fc6cc961SJan Kiszka 	}
296fc6cc961SJan Kiszka 
297fc6cc961SJan Kiszka 	return 0;
298fc6cc961SJan Kiszka }
299fc6cc961SJan Kiszka 
300fc6cc961SJan Kiszka static int
301d0aeaa83SSudip Mukherjee pci_connect_tech_setup(struct exar8250 *priv, struct pci_dev *pcidev,
302d0aeaa83SSudip Mukherjee 		       struct uart_8250_port *port, int idx)
303d0aeaa83SSudip Mukherjee {
304d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
305d0aeaa83SSudip Mukherjee 	unsigned int baud = 1843200;
306d0aeaa83SSudip Mukherjee 
307d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
308d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
309d0aeaa83SSudip Mukherjee }
310d0aeaa83SSudip Mukherjee 
311d0aeaa83SSudip Mukherjee static int
312d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
313d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
314d0aeaa83SSudip Mukherjee {
315d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
316d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
317d0aeaa83SSudip Mukherjee 
318d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
319d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
320d0aeaa83SSudip Mukherjee }
321d0aeaa83SSudip Mukherjee 
322bea8be65SJan Kiszka static void setup_gpio(struct pci_dev *pcidev, u8 __iomem *p)
323d0aeaa83SSudip Mukherjee {
324bea8be65SJan Kiszka 	/*
325bea8be65SJan Kiszka 	 * The Commtech adapters required the MPIOs to be driven low. The Exar
326bea8be65SJan Kiszka 	 * devices will export them as GPIOs, so we pre-configure them safely
327bea8be65SJan Kiszka 	 * as inputs.
328bea8be65SJan Kiszka 	 */
3295fdbe136SMatthew Howell 
3305fdbe136SMatthew Howell 	u8 dir = 0x00;
3315fdbe136SMatthew Howell 
3325fdbe136SMatthew Howell 	if  ((pcidev->vendor == PCI_VENDOR_ID_EXAR) &&
3335fdbe136SMatthew Howell 		(pcidev->subsystem_vendor != PCI_VENDOR_ID_SEALEVEL)) {
3345fdbe136SMatthew Howell 		// Configure GPIO as inputs for Commtech adapters
3355fdbe136SMatthew Howell 		dir = 0xff;
3365fdbe136SMatthew Howell 	} else {
3375fdbe136SMatthew Howell 		// Configure GPIO as outputs for SeaLevel adapters
3385fdbe136SMatthew Howell 		dir = 0x00;
3395fdbe136SMatthew Howell 	}
340bea8be65SJan Kiszka 
341d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
342d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
343d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
344d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
345bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_7_0);
346d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
347d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
348d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
349d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
350d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
351bea8be65SJan Kiszka 	writeb(dir,  p + UART_EXAR_MPIOSEL_15_8);
352d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
353d0aeaa83SSudip Mukherjee }
354d0aeaa83SSudip Mukherjee 
355d0aeaa83SSudip Mukherjee static void *
356380b1e2fSJan Kiszka __xr17v35x_register_gpio(struct pci_dev *pcidev,
357*81171e7dSHeikki Krogerus 			 const struct software_node *node)
358d0aeaa83SSudip Mukherjee {
359d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
360d0aeaa83SSudip Mukherjee 
361d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
362d0aeaa83SSudip Mukherjee 	if (!pdev)
363d0aeaa83SSudip Mukherjee 		return NULL;
364d0aeaa83SSudip Mukherjee 
365d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
3664076cf08SJan Kiszka 	ACPI_COMPANION_SET(&pdev->dev, ACPI_COMPANION(&pcidev->dev));
367d3936d74SJan Kiszka 
368*81171e7dSHeikki Krogerus 	if (device_add_software_node(&pdev->dev, node) < 0 ||
369380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
370d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
371d0aeaa83SSudip Mukherjee 		return NULL;
372d0aeaa83SSudip Mukherjee 	}
373d0aeaa83SSudip Mukherjee 
374d0aeaa83SSudip Mukherjee 	return pdev;
375d0aeaa83SSudip Mukherjee }
376d0aeaa83SSudip Mukherjee 
377380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
378a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 0),
379380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
380380b1e2fSJan Kiszka 	{ }
381380b1e2fSJan Kiszka };
382380b1e2fSJan Kiszka 
383*81171e7dSHeikki Krogerus static const struct software_node exar_gpio_node = {
384*81171e7dSHeikki Krogerus 	.properties = exar_gpio_properties,
385*81171e7dSHeikki Krogerus };
386*81171e7dSHeikki Krogerus 
3870d963ebfSJan Kiszka static int xr17v35x_register_gpio(struct pci_dev *pcidev,
3880d963ebfSJan Kiszka 				  struct uart_8250_port *port)
3890d963ebfSJan Kiszka {
3900d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
3910d963ebfSJan Kiszka 		port->port.private_data =
392*81171e7dSHeikki Krogerus 			__xr17v35x_register_gpio(pcidev, &exar_gpio_node);
3930d963ebfSJan Kiszka 
3940d963ebfSJan Kiszka 	return 0;
3950d963ebfSJan Kiszka }
3960d963ebfSJan Kiszka 
3979d939894SDaniel Golle static int generic_rs485_config(struct uart_port *port,
3989d939894SDaniel Golle 				struct serial_rs485 *rs485)
3999d939894SDaniel Golle {
4009d939894SDaniel Golle 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
4019d939894SDaniel Golle 	u8 __iomem *p = port->membase;
4029d939894SDaniel Golle 	u8 value;
4039d939894SDaniel Golle 
4049d939894SDaniel Golle 	value = readb(p + UART_EXAR_FCTR);
4059d939894SDaniel Golle 	if (is_rs485)
4069d939894SDaniel Golle 		value |= UART_FCTR_EXAR_485;
4079d939894SDaniel Golle 	else
4089d939894SDaniel Golle 		value &= ~UART_FCTR_EXAR_485;
4099d939894SDaniel Golle 
4109d939894SDaniel Golle 	writeb(value, p + UART_EXAR_FCTR);
4119d939894SDaniel Golle 
4129d939894SDaniel Golle 	if (is_rs485)
4139d939894SDaniel Golle 		writeb(UART_EXAR_RS485_DLY(4), p + UART_MSR);
4149d939894SDaniel Golle 
4159d939894SDaniel Golle 	port->rs485 = *rs485;
4169d939894SDaniel Golle 
4179d939894SDaniel Golle 	return 0;
4189d939894SDaniel Golle }
4199d939894SDaniel Golle 
4200d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
4210d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
4229d939894SDaniel Golle 	.rs485_config = generic_rs485_config,
4230d963ebfSJan Kiszka };
4240d963ebfSJan Kiszka 
425413058dfSJan Kiszka static int iot2040_rs485_config(struct uart_port *port,
426413058dfSJan Kiszka 				struct serial_rs485 *rs485)
427413058dfSJan Kiszka {
428413058dfSJan Kiszka 	bool is_rs485 = !!(rs485->flags & SER_RS485_ENABLED);
429413058dfSJan Kiszka 	u8 __iomem *p = port->membase;
430413058dfSJan Kiszka 	u8 mask = IOT2040_UART1_MASK;
431413058dfSJan Kiszka 	u8 mode, value;
432413058dfSJan Kiszka 
433413058dfSJan Kiszka 	if (is_rs485) {
434413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_RX_DURING_TX)
435413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS422;
436413058dfSJan Kiszka 		else
437413058dfSJan Kiszka 			mode = IOT2040_UART_MODE_RS485;
438413058dfSJan Kiszka 
439413058dfSJan Kiszka 		if (rs485->flags & SER_RS485_TERMINATE_BUS)
440413058dfSJan Kiszka 			mode |= IOT2040_UART_TERMINATE_BUS;
441413058dfSJan Kiszka 	} else {
442413058dfSJan Kiszka 		mode = IOT2040_UART_MODE_RS232;
443413058dfSJan Kiszka 	}
444413058dfSJan Kiszka 
445413058dfSJan Kiszka 	if (port->line == 3) {
446413058dfSJan Kiszka 		mask <<= IOT2040_UART2_SHIFT;
447413058dfSJan Kiszka 		mode <<= IOT2040_UART2_SHIFT;
448413058dfSJan Kiszka 	}
449413058dfSJan Kiszka 
450413058dfSJan Kiszka 	value = readb(p + UART_EXAR_MPIOLVL_7_0);
451413058dfSJan Kiszka 	value &= ~mask;
452413058dfSJan Kiszka 	value |= mode;
453413058dfSJan Kiszka 	writeb(value, p + UART_EXAR_MPIOLVL_7_0);
454413058dfSJan Kiszka 
4559d939894SDaniel Golle 	return generic_rs485_config(port, rs485);
456413058dfSJan Kiszka }
457413058dfSJan Kiszka 
458413058dfSJan Kiszka static const struct property_entry iot2040_gpio_properties[] = {
459a589e211SJan Kiszka 	PROPERTY_ENTRY_U32("exar,first-pin", 10),
460413058dfSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 1),
461413058dfSJan Kiszka 	{ }
462413058dfSJan Kiszka };
463413058dfSJan Kiszka 
464*81171e7dSHeikki Krogerus static const struct software_node iot2040_gpio_node = {
465*81171e7dSHeikki Krogerus 	.properties = iot2040_gpio_properties,
466*81171e7dSHeikki Krogerus };
467*81171e7dSHeikki Krogerus 
468413058dfSJan Kiszka static int iot2040_register_gpio(struct pci_dev *pcidev,
469413058dfSJan Kiszka 			      struct uart_8250_port *port)
470413058dfSJan Kiszka {
471413058dfSJan Kiszka 	u8 __iomem *p = port->port.membase;
472413058dfSJan Kiszka 
473413058dfSJan Kiszka 	writeb(IOT2040_UARTS_DEFAULT_MODE, p + UART_EXAR_MPIOLVL_7_0);
474413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_LO_MODE, p + UART_EXAR_MPIOSEL_7_0);
475413058dfSJan Kiszka 	writeb(IOT2040_UARTS_ENABLE, p + UART_EXAR_MPIOLVL_15_8);
476413058dfSJan Kiszka 	writeb(IOT2040_UARTS_GPIO_HI_MODE, p + UART_EXAR_MPIOSEL_15_8);
477413058dfSJan Kiszka 
478413058dfSJan Kiszka 	port->port.private_data =
479*81171e7dSHeikki Krogerus 		__xr17v35x_register_gpio(pcidev, &iot2040_gpio_node);
480413058dfSJan Kiszka 
481413058dfSJan Kiszka 	return 0;
482413058dfSJan Kiszka }
483413058dfSJan Kiszka 
484413058dfSJan Kiszka static const struct exar8250_platform iot2040_platform = {
485413058dfSJan Kiszka 	.rs485_config = iot2040_rs485_config,
486413058dfSJan Kiszka 	.register_gpio = iot2040_register_gpio,
487413058dfSJan Kiszka };
488413058dfSJan Kiszka 
4893e51ceeaSSu Bao Cheng /*
4903e51ceeaSSu Bao Cheng  * For SIMATIC IOT2000, only IOT2040 and its variants have the Exar device,
4913e51ceeaSSu Bao Cheng  * IOT2020 doesn't have. Therefore it is sufficient to match on the common
4923e51ceeaSSu Bao Cheng  * board name after the device was found.
4933e51ceeaSSu Bao Cheng  */
494413058dfSJan Kiszka static const struct dmi_system_id exar_platforms[] = {
495413058dfSJan Kiszka 	{
496413058dfSJan Kiszka 		.matches = {
497413058dfSJan Kiszka 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
498413058dfSJan Kiszka 		},
499413058dfSJan Kiszka 		.driver_data = (void *)&iot2040_platform,
500413058dfSJan Kiszka 	},
501413058dfSJan Kiszka 	{}
502413058dfSJan Kiszka };
503413058dfSJan Kiszka 
504d0aeaa83SSudip Mukherjee static int
505d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
506d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
507d0aeaa83SSudip Mukherjee {
5080d963ebfSJan Kiszka 	const struct exar8250_platform *platform;
509413058dfSJan Kiszka 	const struct dmi_system_id *dmi_match;
510d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
511d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
512d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
513d0aeaa83SSudip Mukherjee 	int ret;
514d0aeaa83SSudip Mukherjee 
515413058dfSJan Kiszka 	dmi_match = dmi_first_match(exar_platforms);
516413058dfSJan Kiszka 	if (dmi_match)
517413058dfSJan Kiszka 		platform = dmi_match->driver_data;
518413058dfSJan Kiszka 	else
5190d963ebfSJan Kiszka 		platform = &exar8250_default_platform;
5200d963ebfSJan Kiszka 
521d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
5220d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
5230d963ebfSJan Kiszka 
524d0aeaa83SSudip Mukherjee 	/*
525328c11f2SAndy Shevchenko 	 * Setup the UART clock for the devices on expansion slot to
526d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
527d0aeaa83SSudip Mukherjee 	 */
528328c11f2SAndy Shevchenko 	if (idx >= 8)
529d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
530d0aeaa83SSudip Mukherjee 
5315b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
5325b5f252dSJan Kiszka 	if (ret)
5335b5f252dSJan Kiszka 		return ret;
534d0aeaa83SSudip Mukherjee 
5355b5f252dSJan Kiszka 	p = port->port.membase;
536d0aeaa83SSudip Mukherjee 
537d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
538d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
539d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
540d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
541d0aeaa83SSudip Mukherjee 
5425b5f252dSJan Kiszka 	if (idx == 0) {
5435b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
544bea8be65SJan Kiszka 		setup_gpio(pcidev, p);
545d0aeaa83SSudip Mukherjee 
5460d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
5475b5f252dSJan Kiszka 	}
548d0aeaa83SSudip Mukherjee 
5490d963ebfSJan Kiszka 	return ret;
550d0aeaa83SSudip Mukherjee }
551d0aeaa83SSudip Mukherjee 
552d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
553d0aeaa83SSudip Mukherjee {
554d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
555d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
556d0aeaa83SSudip Mukherjee 	struct platform_device *pdev = port->port.private_data;
557d0aeaa83SSudip Mukherjee 
558*81171e7dSHeikki Krogerus 	device_remove_software_node(&pdev->dev);
559d0aeaa83SSudip Mukherjee 	platform_device_unregister(pdev);
560d0aeaa83SSudip Mukherjee 	port->port.private_data = NULL;
561d0aeaa83SSudip Mukherjee }
562d0aeaa83SSudip Mukherjee 
56372169e42SAaron Sierra static inline void exar_misc_clear(struct exar8250 *priv)
56472169e42SAaron Sierra {
56572169e42SAaron Sierra 	/* Clear all PCI interrupts by reading INT0. No effect on IIR */
56672169e42SAaron Sierra 	readb(priv->virt + UART_EXAR_INT0);
56772169e42SAaron Sierra 
56872169e42SAaron Sierra 	/* Clear INT0 for Expansion Interface slave ports, too */
56972169e42SAaron Sierra 	if (priv->board->num_ports > 8)
57072169e42SAaron Sierra 		readb(priv->virt + 0x2000 + UART_EXAR_INT0);
57172169e42SAaron Sierra }
57272169e42SAaron Sierra 
573c7e1b405SAaron Sierra /*
574c7e1b405SAaron Sierra  * These Exar UARTs have an extra interrupt indicator that could fire for a
575c7e1b405SAaron Sierra  * few interrupts that are not presented/cleared through IIR.  One of which is
576c7e1b405SAaron Sierra  * a wakeup interrupt when coming out of sleep.  These interrupts are only
577c7e1b405SAaron Sierra  * cleared by reading global INT0 or INT1 registers as interrupts are
578c7e1b405SAaron Sierra  * associated with channel 0. The INT[3:0] registers _are_ accessible from each
579c7e1b405SAaron Sierra  * channel's address space, but for the sake of bus efficiency we register a
580c7e1b405SAaron Sierra  * dedicated handler at the PCI device level to handle them.
581c7e1b405SAaron Sierra  */
582c7e1b405SAaron Sierra static irqreturn_t exar_misc_handler(int irq, void *data)
583c7e1b405SAaron Sierra {
58472169e42SAaron Sierra 	exar_misc_clear(data);
585c7e1b405SAaron Sierra 
586c7e1b405SAaron Sierra 	return IRQ_HANDLED;
587c7e1b405SAaron Sierra }
588c7e1b405SAaron Sierra 
589d0aeaa83SSudip Mukherjee static int
590d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
591d0aeaa83SSudip Mukherjee {
592d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
593d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
594d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
595d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
596d0aeaa83SSudip Mukherjee 	int rc;
597d0aeaa83SSudip Mukherjee 
598d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
599d0aeaa83SSudip Mukherjee 	if (!board)
600d0aeaa83SSudip Mukherjee 		return -EINVAL;
601d0aeaa83SSudip Mukherjee 
602d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
603d0aeaa83SSudip Mukherjee 	if (rc)
604d0aeaa83SSudip Mukherjee 		return rc;
605d0aeaa83SSudip Mukherjee 
606d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
607d0aeaa83SSudip Mukherjee 
608d0aeaa83SSudip Mukherjee 	nr_ports = board->num_ports ? board->num_ports : pcidev->device & 0x0f;
609d0aeaa83SSudip Mukherjee 
610df60a8afSAndy Shevchenko 	priv = devm_kzalloc(&pcidev->dev, struct_size(priv, line, nr_ports), GFP_KERNEL);
611d0aeaa83SSudip Mukherjee 	if (!priv)
612d0aeaa83SSudip Mukherjee 		return -ENOMEM;
613d0aeaa83SSudip Mukherjee 
614d0aeaa83SSudip Mukherjee 	priv->board = board;
615c7e1b405SAaron Sierra 	priv->virt = pcim_iomap(pcidev, bar, 0);
616c7e1b405SAaron Sierra 	if (!priv->virt)
617c7e1b405SAaron Sierra 		return -ENOMEM;
618d0aeaa83SSudip Mukherjee 
619172c33cbSJan Kiszka 	pci_set_master(pcidev);
620172c33cbSJan Kiszka 
621172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
622172c33cbSJan Kiszka 	if (rc < 0)
623172c33cbSJan Kiszka 		return rc;
624172c33cbSJan Kiszka 
625d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
6266be254c2SAndy Shevchenko 	uart.port.flags = UPF_SHARE_IRQ | UPF_EXAR_EFR | UPF_FIXED_TYPE | UPF_FIXED_PORT;
627172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
628d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
629d0aeaa83SSudip Mukherjee 
630c7e1b405SAaron Sierra 	rc = devm_request_irq(&pcidev->dev, uart.port.irq, exar_misc_handler,
631c7e1b405SAaron Sierra 			 IRQF_SHARED, "exar_uart", priv);
632c7e1b405SAaron Sierra 	if (rc)
633c7e1b405SAaron Sierra 		return rc;
634c7e1b405SAaron Sierra 
63572169e42SAaron Sierra 	/* Clear interrupts */
63672169e42SAaron Sierra 	exar_misc_clear(priv);
63772169e42SAaron Sierra 
638d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
639d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
640d0aeaa83SSudip Mukherjee 		if (rc) {
641d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
642d0aeaa83SSudip Mukherjee 			break;
643d0aeaa83SSudip Mukherjee 		}
644d0aeaa83SSudip Mukherjee 
645d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
646d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
647d0aeaa83SSudip Mukherjee 
648d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
649d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
650d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
651d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
652d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
653d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
654d0aeaa83SSudip Mukherjee 			break;
655d0aeaa83SSudip Mukherjee 		}
656d0aeaa83SSudip Mukherjee 	}
657d0aeaa83SSudip Mukherjee 	priv->nr = i;
658d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
659d0aeaa83SSudip Mukherjee 	return 0;
660d0aeaa83SSudip Mukherjee }
661d0aeaa83SSudip Mukherjee 
662d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
663d0aeaa83SSudip Mukherjee {
664d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
665d0aeaa83SSudip Mukherjee 	unsigned int i;
666d0aeaa83SSudip Mukherjee 
667d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
668d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
669d0aeaa83SSudip Mukherjee 
670d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
671d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
672d0aeaa83SSudip Mukherjee }
673d0aeaa83SSudip Mukherjee 
674d0aeaa83SSudip Mukherjee static int __maybe_unused exar_suspend(struct device *dev)
675d0aeaa83SSudip Mukherjee {
676d0aeaa83SSudip Mukherjee 	struct pci_dev *pcidev = to_pci_dev(dev);
677d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
678d0aeaa83SSudip Mukherjee 	unsigned int i;
679d0aeaa83SSudip Mukherjee 
680d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
681d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
682d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
683d0aeaa83SSudip Mukherjee 
684d0aeaa83SSudip Mukherjee 	/* Ensure that every init quirk is properly torn down */
685d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
686d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
687d0aeaa83SSudip Mukherjee 
688d0aeaa83SSudip Mukherjee 	return 0;
689d0aeaa83SSudip Mukherjee }
690d0aeaa83SSudip Mukherjee 
691d0aeaa83SSudip Mukherjee static int __maybe_unused exar_resume(struct device *dev)
692d0aeaa83SSudip Mukherjee {
69376b4106cSChuhong Yuan 	struct exar8250 *priv = dev_get_drvdata(dev);
694d0aeaa83SSudip Mukherjee 	unsigned int i;
695d0aeaa83SSudip Mukherjee 
69672169e42SAaron Sierra 	exar_misc_clear(priv);
69772169e42SAaron Sierra 
698d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
699d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
700d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
701d0aeaa83SSudip Mukherjee 
702d0aeaa83SSudip Mukherjee 	return 0;
703d0aeaa83SSudip Mukherjee }
704d0aeaa83SSudip Mukherjee 
705d0aeaa83SSudip Mukherjee static SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
706d0aeaa83SSudip Mukherjee 
70710c5ccc3SJay Dolan static const struct exar8250_board acces_com_2x = {
70810c5ccc3SJay Dolan 	.num_ports	= 2,
70910c5ccc3SJay Dolan 	.setup		= pci_xr17c154_setup,
71010c5ccc3SJay Dolan };
71110c5ccc3SJay Dolan 
71210c5ccc3SJay Dolan static const struct exar8250_board acces_com_4x = {
71310c5ccc3SJay Dolan 	.num_ports	= 4,
71410c5ccc3SJay Dolan 	.setup		= pci_xr17c154_setup,
71510c5ccc3SJay Dolan };
71610c5ccc3SJay Dolan 
71710c5ccc3SJay Dolan static const struct exar8250_board acces_com_8x = {
71810c5ccc3SJay Dolan 	.num_ports	= 8,
71910c5ccc3SJay Dolan 	.setup		= pci_xr17c154_setup,
72010c5ccc3SJay Dolan };
72110c5ccc3SJay Dolan 
72210c5ccc3SJay Dolan 
723fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
724fc6cc961SJan Kiszka 	.num_ports	= 2,
725fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
726fc6cc961SJan Kiszka };
727fc6cc961SJan Kiszka 
728fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
729fc6cc961SJan Kiszka 	.num_ports	= 4,
730fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
731fc6cc961SJan Kiszka };
732fc6cc961SJan Kiszka 
733fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
734fc6cc961SJan Kiszka 	.num_ports	= 8,
735fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
736fc6cc961SJan Kiszka };
737fc6cc961SJan Kiszka 
738d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_connect = {
739d0aeaa83SSudip Mukherjee 	.setup		= pci_connect_tech_setup,
740d0aeaa83SSudip Mukherjee };
741d0aeaa83SSudip Mukherjee 
742d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
743d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
744d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
745d0aeaa83SSudip Mukherjee };
746d0aeaa83SSudip Mukherjee 
747d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
748d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
749d0aeaa83SSudip Mukherjee };
750d0aeaa83SSudip Mukherjee 
751d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
752d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
753d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
754d0aeaa83SSudip Mukherjee };
755d0aeaa83SSudip Mukherjee 
756c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_2 = {
757c6b9e95dSValmer Huhn 	.num_ports	= 2,
758c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
759c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
760c6b9e95dSValmer Huhn };
761c6b9e95dSValmer Huhn 
762c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_4 = {
763c6b9e95dSValmer Huhn 	.num_ports	= 4,
764c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
765c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
766c6b9e95dSValmer Huhn };
767c6b9e95dSValmer Huhn 
768c6b9e95dSValmer Huhn static const struct exar8250_board pbn_fastcom35x_8 = {
769c6b9e95dSValmer Huhn 	.num_ports	= 8,
770c6b9e95dSValmer Huhn 	.setup		= pci_xr17v35x_setup,
771c6b9e95dSValmer Huhn 	.exit		= pci_xr17v35x_exit,
772c6b9e95dSValmer Huhn };
773c6b9e95dSValmer Huhn 
774d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
775d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
776d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
777d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
778d0aeaa83SSudip Mukherjee };
779d0aeaa83SSudip Mukherjee 
780d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
781d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
782d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
783d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
784d0aeaa83SSudip Mukherjee };
785d0aeaa83SSudip Mukherjee 
786d0aeaa83SSudip Mukherjee #define CONNECT_DEVICE(devid, sdevid, bd) {				\
787d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(							\
788d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,					\
789d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,				\
790d0aeaa83SSudip Mukherjee 		PCI_SUBVENDOR_ID_CONNECT_TECH,				\
791d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_##sdevid), 0, 0,	\
792d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd					\
793d0aeaa83SSudip Mukherjee 	}
794d0aeaa83SSudip Mukherjee 
79524637007SAndy Shevchenko #define EXAR_DEVICE(vend, devid, bd) { PCI_DEVICE_DATA(vend, devid, &bd) }
796d0aeaa83SSudip Mukherjee 
797d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
798d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
799d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
800d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
801d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_IBM,			\
802d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
803d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
804d0aeaa83SSudip Mukherjee 	}
805d0aeaa83SSudip Mukherjee 
8063637c460SArvind Yadav static const struct pci_device_id exar_pci_tbl[] = {
80724637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2S, acces_com_2x),
80824637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4S, acces_com_4x),
80924637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8S, acces_com_8x),
81024637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM232_8, acces_com_8x),
81124637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_2SM, acces_com_2x),
81224637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_4SM, acces_com_4x),
81324637007SAndy Shevchenko 	EXAR_DEVICE(ACCESSIO, COM_8SM, acces_com_8x),
81410c5ccc3SJay Dolan 
815d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_232, pbn_connect),
816d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_232, pbn_connect),
817d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_232, pbn_connect),
818d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_1_1, pbn_connect),
819d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_2_2, pbn_connect),
820d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_4_4, pbn_connect),
821d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2, pbn_connect),
822d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4, pbn_connect),
823d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8, pbn_connect),
824d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_485, pbn_connect),
825d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_485, pbn_connect),
826d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_485, pbn_connect),
827d0aeaa83SSudip Mukherjee 
828d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
829d0aeaa83SSudip Mukherjee 
830d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
83124637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C152, pbn_exar_XR17C15x),
83224637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C154, pbn_exar_XR17C15x),
83324637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17C158, pbn_exar_XR17C15x),
834d0aeaa83SSudip Mukherjee 
835d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
83624637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V352, pbn_exar_XR17V35x),
83724637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V354, pbn_exar_XR17V35x),
83824637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V358, pbn_exar_XR17V35x),
83924637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V4358, pbn_exar_XR17V4358),
84024637007SAndy Shevchenko 	EXAR_DEVICE(EXAR, XR17V8358, pbn_exar_XR17V8358),
841c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4222PCIE, pbn_fastcom35x_2),
842c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4224PCIE, pbn_fastcom35x_4),
843c6b9e95dSValmer Huhn 	EXAR_DEVICE(COMMTECH, 4228PCIE, pbn_fastcom35x_8),
844fc6cc961SJan Kiszka 
84524637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4222PCI335, pbn_fastcom335_2),
84624637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 4224PCI335, pbn_fastcom335_4),
84724637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2324PCI335, pbn_fastcom335_4),
84824637007SAndy Shevchenko 	EXAR_DEVICE(COMMTECH, 2328PCI335, pbn_fastcom335_8),
849d0aeaa83SSudip Mukherjee 	{ 0, }
850d0aeaa83SSudip Mukherjee };
851d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
852d0aeaa83SSudip Mukherjee 
853d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
854d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
855d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
856d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
857d0aeaa83SSudip Mukherjee 	.driver         = {
858d0aeaa83SSudip Mukherjee 		.pm     = &exar_pci_pm,
859d0aeaa83SSudip Mukherjee 	},
860d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
861d0aeaa83SSudip Mukherjee };
862d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
863d0aeaa83SSudip Mukherjee 
864d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
8652b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
866d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
867