xref: /linux/drivers/tty/serial/8250/8250_exar.c (revision 380b1e2f3a2f32bfe9c0aa85a68629eb99b043c0)
1d0aeaa83SSudip Mukherjee /*
2d0aeaa83SSudip Mukherjee  *  Probe module for 8250/16550-type Exar chips PCI serial ports.
3d0aeaa83SSudip Mukherjee  *
4d0aeaa83SSudip Mukherjee  *  Based on drivers/tty/serial/8250/8250_pci.c,
5d0aeaa83SSudip Mukherjee  *
6d0aeaa83SSudip Mukherjee  *  Copyright (C) 2017 Sudip Mukherjee, All Rights Reserved.
7d0aeaa83SSudip Mukherjee  *
8d0aeaa83SSudip Mukherjee  * This program is free software; you can redistribute it and/or modify
9d0aeaa83SSudip Mukherjee  * it under the terms of the GNU General Public License as published by
10d0aeaa83SSudip Mukherjee  * the Free Software Foundation; either version 2 of the License.
11d0aeaa83SSudip Mukherjee  */
124076cf08SJan Kiszka #include <linux/acpi.h>
13d0aeaa83SSudip Mukherjee #include <linux/io.h>
14d0aeaa83SSudip Mukherjee #include <linux/kernel.h>
15d0aeaa83SSudip Mukherjee #include <linux/module.h>
16d0aeaa83SSudip Mukherjee #include <linux/pci.h>
17*380b1e2fSJan Kiszka #include <linux/property.h>
18d0aeaa83SSudip Mukherjee #include <linux/serial_core.h>
19d0aeaa83SSudip Mukherjee #include <linux/serial_reg.h>
20d0aeaa83SSudip Mukherjee #include <linux/slab.h>
21d0aeaa83SSudip Mukherjee #include <linux/string.h>
22d0aeaa83SSudip Mukherjee #include <linux/tty.h>
23d0aeaa83SSudip Mukherjee #include <linux/8250_pci.h>
24d0aeaa83SSudip Mukherjee 
25d0aeaa83SSudip Mukherjee #include <asm/byteorder.h>
26d0aeaa83SSudip Mukherjee 
27d0aeaa83SSudip Mukherjee #include "8250.h"
28d0aeaa83SSudip Mukherjee 
29fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4224PCI335	0x0002
30fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_4222PCI335	0x0004
31fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2324PCI335	0x000a
32fc6cc961SJan Kiszka #define PCI_DEVICE_ID_COMMTECH_2328PCI335	0x000b
33d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4224PCIE		0x0020
34d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4228PCIE		0x0021
35d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_COMMTECH_4222PCIE		0x0022
36d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V4358		0x4358
37d0aeaa83SSudip Mukherjee #define PCI_DEVICE_ID_EXAR_XR17V8358		0x8358
38d0aeaa83SSudip Mukherjee 
397e12357eSJan Kiszka #define UART_EXAR_8XMODE	0x88	/* 8X sampling rate select */
407e12357eSJan Kiszka 
417e12357eSJan Kiszka #define UART_EXAR_FCTR		0x08	/* Feature Control Register */
427e12357eSJan Kiszka #define UART_FCTR_EXAR_IRDA	0x10	/* IrDa data encode select */
437e12357eSJan Kiszka #define UART_FCTR_EXAR_485	0x20	/* Auto 485 half duplex dir ctl */
447e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGA	0x00	/* FIFO trigger table A */
457e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGB	0x60	/* FIFO trigger table B */
467e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGC	0x80	/* FIFO trigger table C */
477e12357eSJan Kiszka #define UART_FCTR_EXAR_TRGD	0xc0	/* FIFO trigger table D programmable */
487e12357eSJan Kiszka 
497e12357eSJan Kiszka #define UART_EXAR_TXTRG		0x0a	/* Tx FIFO trigger level write-only */
507e12357eSJan Kiszka #define UART_EXAR_RXTRG		0x0b	/* Rx FIFO trigger level write-only */
517e12357eSJan Kiszka 
52d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_7_0	0x8f	/* MPIOINT[7:0] */
53d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_7_0	0x90	/* MPIOLVL[7:0] */
54d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_7_0	0x91	/* MPIO3T[7:0] */
55d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_7_0	0x92	/* MPIOINV[7:0] */
56d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_7_0	0x93	/* MPIOSEL[7:0] */
57d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_7_0	0x94	/* MPIOOD[7:0] */
58d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINT_15_8	0x95	/* MPIOINT[15:8] */
59d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOLVL_15_8	0x96	/* MPIOLVL[15:8] */
60d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIO3T_15_8	0x97	/* MPIO3T[15:8] */
61d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOINV_15_8	0x98	/* MPIOINV[15:8] */
62d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOSEL_15_8	0x99	/* MPIOSEL[15:8] */
63d0aeaa83SSudip Mukherjee #define UART_EXAR_MPIOOD_15_8	0x9a	/* MPIOOD[15:8] */
64d0aeaa83SSudip Mukherjee 
65d0aeaa83SSudip Mukherjee struct exar8250;
66d0aeaa83SSudip Mukherjee 
670d963ebfSJan Kiszka struct exar8250_platform {
680d963ebfSJan Kiszka 	int (*rs485_config)(struct uart_port *, struct serial_rs485 *);
690d963ebfSJan Kiszka 	int (*register_gpio)(struct pci_dev *, struct uart_8250_port *);
700d963ebfSJan Kiszka };
710d963ebfSJan Kiszka 
72d0aeaa83SSudip Mukherjee /**
73d0aeaa83SSudip Mukherjee  * struct exar8250_board - board information
74d0aeaa83SSudip Mukherjee  * @num_ports: number of serial ports
75d0aeaa83SSudip Mukherjee  * @reg_shift: describes UART register mapping in PCI memory
76d0aeaa83SSudip Mukherjee  */
77d0aeaa83SSudip Mukherjee struct exar8250_board {
78d0aeaa83SSudip Mukherjee 	unsigned int num_ports;
79d0aeaa83SSudip Mukherjee 	unsigned int reg_shift;
80d0aeaa83SSudip Mukherjee 	bool has_slave;
81d0aeaa83SSudip Mukherjee 	int	(*setup)(struct exar8250 *, struct pci_dev *,
82d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *, int);
83d0aeaa83SSudip Mukherjee 	void	(*exit)(struct pci_dev *pcidev);
84d0aeaa83SSudip Mukherjee };
85d0aeaa83SSudip Mukherjee 
86d0aeaa83SSudip Mukherjee struct exar8250 {
87d0aeaa83SSudip Mukherjee 	unsigned int		nr;
88d0aeaa83SSudip Mukherjee 	struct exar8250_board	*board;
89d0aeaa83SSudip Mukherjee 	int			line[0];
90d0aeaa83SSudip Mukherjee };
91d0aeaa83SSudip Mukherjee 
92d0aeaa83SSudip Mukherjee static int default_setup(struct exar8250 *priv, struct pci_dev *pcidev,
93d0aeaa83SSudip Mukherjee 			 int idx, unsigned int offset,
94d0aeaa83SSudip Mukherjee 			 struct uart_8250_port *port)
95d0aeaa83SSudip Mukherjee {
96d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
97d0aeaa83SSudip Mukherjee 	unsigned int bar = 0;
98d0aeaa83SSudip Mukherjee 
9924572af4SJan Kiszka 	if (!pcim_iomap_table(pcidev)[bar] && !pcim_iomap(pcidev, bar, 0))
10024572af4SJan Kiszka 		return -ENOMEM;
10124572af4SJan Kiszka 
102d0aeaa83SSudip Mukherjee 	port->port.iotype = UPIO_MEM;
103d0aeaa83SSudip Mukherjee 	port->port.mapbase = pci_resource_start(pcidev, bar) + offset;
104d0aeaa83SSudip Mukherjee 	port->port.membase = pcim_iomap_table(pcidev)[bar] + offset;
105d0aeaa83SSudip Mukherjee 	port->port.regshift = board->reg_shift;
106d0aeaa83SSudip Mukherjee 
107d0aeaa83SSudip Mukherjee 	return 0;
108d0aeaa83SSudip Mukherjee }
109d0aeaa83SSudip Mukherjee 
110d0aeaa83SSudip Mukherjee static int
111fc6cc961SJan Kiszka pci_fastcom335_setup(struct exar8250 *priv, struct pci_dev *pcidev,
112fc6cc961SJan Kiszka 		     struct uart_8250_port *port, int idx)
113fc6cc961SJan Kiszka {
114fc6cc961SJan Kiszka 	unsigned int offset = idx * 0x200;
115fc6cc961SJan Kiszka 	unsigned int baud = 1843200;
116fc6cc961SJan Kiszka 	u8 __iomem *p;
117fc6cc961SJan Kiszka 	int err;
118fc6cc961SJan Kiszka 
119fc6cc961SJan Kiszka 	port->port.flags |= UPF_EXAR_EFR;
120fc6cc961SJan Kiszka 	port->port.uartclk = baud * 16;
121fc6cc961SJan Kiszka 
122fc6cc961SJan Kiszka 	err = default_setup(priv, pcidev, idx, offset, port);
123fc6cc961SJan Kiszka 	if (err)
124fc6cc961SJan Kiszka 		return err;
125fc6cc961SJan Kiszka 
126fc6cc961SJan Kiszka 	p = port->port.membase;
127fc6cc961SJan Kiszka 
128fc6cc961SJan Kiszka 	writeb(0x00, p + UART_EXAR_8XMODE);
129fc6cc961SJan Kiszka 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
130fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_TXTRG);
131fc6cc961SJan Kiszka 	writeb(32, p + UART_EXAR_RXTRG);
132fc6cc961SJan Kiszka 
133fc6cc961SJan Kiszka 	/*
134fc6cc961SJan Kiszka 	 * Setup Multipurpose Input/Output pins.
135fc6cc961SJan Kiszka 	 */
136fc6cc961SJan Kiszka 	if (idx == 0) {
137fc6cc961SJan Kiszka 		switch (pcidev->device) {
138fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4222PCI335:
139fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_4224PCI335:
140fc6cc961SJan Kiszka 			writeb(0x78, p + UART_EXAR_MPIOLVL_7_0);
141fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
142fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
143fc6cc961SJan Kiszka 			break;
144fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2324PCI335:
145fc6cc961SJan Kiszka 		case PCI_DEVICE_ID_COMMTECH_2328PCI335:
146fc6cc961SJan Kiszka 			writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
147fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOINV_7_0);
148fc6cc961SJan Kiszka 			writeb(0xc0, p + UART_EXAR_MPIOSEL_7_0);
149fc6cc961SJan Kiszka 			break;
150fc6cc961SJan Kiszka 		}
151fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
152fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
153fc6cc961SJan Kiszka 		writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
154fc6cc961SJan Kiszka 	}
155fc6cc961SJan Kiszka 
156fc6cc961SJan Kiszka 	return 0;
157fc6cc961SJan Kiszka }
158fc6cc961SJan Kiszka 
159fc6cc961SJan Kiszka static int
160d0aeaa83SSudip Mukherjee pci_connect_tech_setup(struct exar8250 *priv, struct pci_dev *pcidev,
161d0aeaa83SSudip Mukherjee 		       struct uart_8250_port *port, int idx)
162d0aeaa83SSudip Mukherjee {
163d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
164d0aeaa83SSudip Mukherjee 	unsigned int baud = 1843200;
165d0aeaa83SSudip Mukherjee 
166d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
167d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
168d0aeaa83SSudip Mukherjee }
169d0aeaa83SSudip Mukherjee 
170d0aeaa83SSudip Mukherjee static int
171d0aeaa83SSudip Mukherjee pci_xr17c154_setup(struct exar8250 *priv, struct pci_dev *pcidev,
172d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
173d0aeaa83SSudip Mukherjee {
174d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x200;
175d0aeaa83SSudip Mukherjee 	unsigned int baud = 921600;
176d0aeaa83SSudip Mukherjee 
177d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
178d0aeaa83SSudip Mukherjee 	return default_setup(priv, pcidev, idx, offset, port);
179d0aeaa83SSudip Mukherjee }
180d0aeaa83SSudip Mukherjee 
181d0aeaa83SSudip Mukherjee static void setup_gpio(u8 __iomem *p)
182d0aeaa83SSudip Mukherjee {
183d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_7_0);
184d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_7_0);
185d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_7_0);
186d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_7_0);
187d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOSEL_7_0);
188d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_7_0);
189d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINT_15_8);
190d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOLVL_15_8);
191d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIO3T_15_8);
192d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOINV_15_8);
193d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOSEL_15_8);
194d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_MPIOOD_15_8);
195d0aeaa83SSudip Mukherjee }
196d0aeaa83SSudip Mukherjee 
197d0aeaa83SSudip Mukherjee static void *
198*380b1e2fSJan Kiszka __xr17v35x_register_gpio(struct pci_dev *pcidev,
199*380b1e2fSJan Kiszka 			 const struct property_entry *properties)
200d0aeaa83SSudip Mukherjee {
201d0aeaa83SSudip Mukherjee 	struct platform_device *pdev;
202d0aeaa83SSudip Mukherjee 
203d0aeaa83SSudip Mukherjee 	pdev = platform_device_alloc("gpio_exar", PLATFORM_DEVID_AUTO);
204d0aeaa83SSudip Mukherjee 	if (!pdev)
205d0aeaa83SSudip Mukherjee 		return NULL;
206d0aeaa83SSudip Mukherjee 
207d3936d74SJan Kiszka 	pdev->dev.parent = &pcidev->dev;
2084076cf08SJan Kiszka 	ACPI_COMPANION_SET(&pdev->dev, ACPI_COMPANION(&pcidev->dev));
209d3936d74SJan Kiszka 
210*380b1e2fSJan Kiszka 	if (platform_device_add_properties(pdev, properties) < 0 ||
211*380b1e2fSJan Kiszka 	    platform_device_add(pdev) < 0) {
212d0aeaa83SSudip Mukherjee 		platform_device_put(pdev);
213d0aeaa83SSudip Mukherjee 		return NULL;
214d0aeaa83SSudip Mukherjee 	}
215d0aeaa83SSudip Mukherjee 
216d0aeaa83SSudip Mukherjee 	return pdev;
217d0aeaa83SSudip Mukherjee }
218d0aeaa83SSudip Mukherjee 
219*380b1e2fSJan Kiszka static const struct property_entry exar_gpio_properties[] = {
220*380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("linux,first-pin", 0),
221*380b1e2fSJan Kiszka 	PROPERTY_ENTRY_U32("ngpios", 16),
222*380b1e2fSJan Kiszka 	{ }
223*380b1e2fSJan Kiszka };
224*380b1e2fSJan Kiszka 
2250d963ebfSJan Kiszka static int xr17v35x_register_gpio(struct pci_dev *pcidev,
2260d963ebfSJan Kiszka 				  struct uart_8250_port *port)
2270d963ebfSJan Kiszka {
2280d963ebfSJan Kiszka 	if (pcidev->vendor == PCI_VENDOR_ID_EXAR)
2290d963ebfSJan Kiszka 		port->port.private_data =
230*380b1e2fSJan Kiszka 			__xr17v35x_register_gpio(pcidev, exar_gpio_properties);
2310d963ebfSJan Kiszka 
2320d963ebfSJan Kiszka 	return 0;
2330d963ebfSJan Kiszka }
2340d963ebfSJan Kiszka 
2350d963ebfSJan Kiszka static const struct exar8250_platform exar8250_default_platform = {
2360d963ebfSJan Kiszka 	.register_gpio = xr17v35x_register_gpio,
2370d963ebfSJan Kiszka };
2380d963ebfSJan Kiszka 
239d0aeaa83SSudip Mukherjee static int
240d0aeaa83SSudip Mukherjee pci_xr17v35x_setup(struct exar8250 *priv, struct pci_dev *pcidev,
241d0aeaa83SSudip Mukherjee 		   struct uart_8250_port *port, int idx)
242d0aeaa83SSudip Mukherjee {
243d0aeaa83SSudip Mukherjee 	const struct exar8250_board *board = priv->board;
2440d963ebfSJan Kiszka 	const struct exar8250_platform *platform;
245d0aeaa83SSudip Mukherjee 	unsigned int offset = idx * 0x400;
246d0aeaa83SSudip Mukherjee 	unsigned int baud = 7812500;
247d0aeaa83SSudip Mukherjee 	u8 __iomem *p;
248d0aeaa83SSudip Mukherjee 	int ret;
249d0aeaa83SSudip Mukherjee 
2500d963ebfSJan Kiszka 	platform = &exar8250_default_platform;
2510d963ebfSJan Kiszka 
252d0aeaa83SSudip Mukherjee 	port->port.uartclk = baud * 16;
2530d963ebfSJan Kiszka 	port->port.rs485_config = platform->rs485_config;
2540d963ebfSJan Kiszka 
255d0aeaa83SSudip Mukherjee 	/*
256d0aeaa83SSudip Mukherjee 	 * Setup the uart clock for the devices on expansion slot to
257d0aeaa83SSudip Mukherjee 	 * half the clock speed of the main chip (which is 125MHz)
258d0aeaa83SSudip Mukherjee 	 */
259d0aeaa83SSudip Mukherjee 	if (board->has_slave && idx >= 8)
260d0aeaa83SSudip Mukherjee 		port->port.uartclk /= 2;
261d0aeaa83SSudip Mukherjee 
2625b5f252dSJan Kiszka 	ret = default_setup(priv, pcidev, idx, offset, port);
2635b5f252dSJan Kiszka 	if (ret)
2645b5f252dSJan Kiszka 		return ret;
265d0aeaa83SSudip Mukherjee 
2665b5f252dSJan Kiszka 	p = port->port.membase;
267d0aeaa83SSudip Mukherjee 
268d0aeaa83SSudip Mukherjee 	writeb(0x00, p + UART_EXAR_8XMODE);
269d0aeaa83SSudip Mukherjee 	writeb(UART_FCTR_EXAR_TRGD, p + UART_EXAR_FCTR);
270d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_TXTRG);
271d0aeaa83SSudip Mukherjee 	writeb(128, p + UART_EXAR_RXTRG);
272d0aeaa83SSudip Mukherjee 
2735b5f252dSJan Kiszka 	if (idx == 0) {
2745b5f252dSJan Kiszka 		/* Setup Multipurpose Input/Output pins. */
2755b5f252dSJan Kiszka 		setup_gpio(p);
276d0aeaa83SSudip Mukherjee 
2770d963ebfSJan Kiszka 		ret = platform->register_gpio(pcidev, port);
2785b5f252dSJan Kiszka 	}
279d0aeaa83SSudip Mukherjee 
2800d963ebfSJan Kiszka 	return ret;
281d0aeaa83SSudip Mukherjee }
282d0aeaa83SSudip Mukherjee 
283d0aeaa83SSudip Mukherjee static void pci_xr17v35x_exit(struct pci_dev *pcidev)
284d0aeaa83SSudip Mukherjee {
285d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
286d0aeaa83SSudip Mukherjee 	struct uart_8250_port *port = serial8250_get_port(priv->line[0]);
287d0aeaa83SSudip Mukherjee 	struct platform_device *pdev = port->port.private_data;
288d0aeaa83SSudip Mukherjee 
289d0aeaa83SSudip Mukherjee 	platform_device_unregister(pdev);
290d0aeaa83SSudip Mukherjee 	port->port.private_data = NULL;
291d0aeaa83SSudip Mukherjee }
292d0aeaa83SSudip Mukherjee 
293d0aeaa83SSudip Mukherjee static int
294d0aeaa83SSudip Mukherjee exar_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
295d0aeaa83SSudip Mukherjee {
296d0aeaa83SSudip Mukherjee 	unsigned int nr_ports, i, bar = 0, maxnr;
297d0aeaa83SSudip Mukherjee 	struct exar8250_board *board;
298d0aeaa83SSudip Mukherjee 	struct uart_8250_port uart;
299d0aeaa83SSudip Mukherjee 	struct exar8250 *priv;
300d0aeaa83SSudip Mukherjee 	int rc;
301d0aeaa83SSudip Mukherjee 
302d0aeaa83SSudip Mukherjee 	board = (struct exar8250_board *)ent->driver_data;
303d0aeaa83SSudip Mukherjee 	if (!board)
304d0aeaa83SSudip Mukherjee 		return -EINVAL;
305d0aeaa83SSudip Mukherjee 
306d0aeaa83SSudip Mukherjee 	rc = pcim_enable_device(pcidev);
307d0aeaa83SSudip Mukherjee 	if (rc)
308d0aeaa83SSudip Mukherjee 		return rc;
309d0aeaa83SSudip Mukherjee 
310d0aeaa83SSudip Mukherjee 	maxnr = pci_resource_len(pcidev, bar) >> (board->reg_shift + 3);
311d0aeaa83SSudip Mukherjee 
312d0aeaa83SSudip Mukherjee 	nr_ports = board->num_ports ? board->num_ports : pcidev->device & 0x0f;
313d0aeaa83SSudip Mukherjee 
314d0aeaa83SSudip Mukherjee 	priv = devm_kzalloc(&pcidev->dev, sizeof(*priv) +
315d0aeaa83SSudip Mukherjee 			    sizeof(unsigned int) * nr_ports,
316d0aeaa83SSudip Mukherjee 			    GFP_KERNEL);
317d0aeaa83SSudip Mukherjee 	if (!priv)
318d0aeaa83SSudip Mukherjee 		return -ENOMEM;
319d0aeaa83SSudip Mukherjee 
320d0aeaa83SSudip Mukherjee 	priv->board = board;
321d0aeaa83SSudip Mukherjee 
322172c33cbSJan Kiszka 	pci_set_master(pcidev);
323172c33cbSJan Kiszka 
324172c33cbSJan Kiszka 	rc = pci_alloc_irq_vectors(pcidev, 1, 1, PCI_IRQ_ALL_TYPES);
325172c33cbSJan Kiszka 	if (rc < 0)
326172c33cbSJan Kiszka 		return rc;
327172c33cbSJan Kiszka 
328d0aeaa83SSudip Mukherjee 	memset(&uart, 0, sizeof(uart));
329d0aeaa83SSudip Mukherjee 	uart.port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ
330d0aeaa83SSudip Mukherjee 			  | UPF_EXAR_EFR;
331172c33cbSJan Kiszka 	uart.port.irq = pci_irq_vector(pcidev, 0);
332d0aeaa83SSudip Mukherjee 	uart.port.dev = &pcidev->dev;
333d0aeaa83SSudip Mukherjee 
334d0aeaa83SSudip Mukherjee 	for (i = 0; i < nr_ports && i < maxnr; i++) {
335d0aeaa83SSudip Mukherjee 		rc = board->setup(priv, pcidev, &uart, i);
336d0aeaa83SSudip Mukherjee 		if (rc) {
337d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev, "Failed to setup port %u\n", i);
338d0aeaa83SSudip Mukherjee 			break;
339d0aeaa83SSudip Mukherjee 		}
340d0aeaa83SSudip Mukherjee 
341d0aeaa83SSudip Mukherjee 		dev_dbg(&pcidev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
342d0aeaa83SSudip Mukherjee 			uart.port.iobase, uart.port.irq, uart.port.iotype);
343d0aeaa83SSudip Mukherjee 
344d0aeaa83SSudip Mukherjee 		priv->line[i] = serial8250_register_8250_port(&uart);
345d0aeaa83SSudip Mukherjee 		if (priv->line[i] < 0) {
346d0aeaa83SSudip Mukherjee 			dev_err(&pcidev->dev,
347d0aeaa83SSudip Mukherjee 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
348d0aeaa83SSudip Mukherjee 				uart.port.iobase, uart.port.irq,
349d0aeaa83SSudip Mukherjee 				uart.port.iotype, priv->line[i]);
350d0aeaa83SSudip Mukherjee 			break;
351d0aeaa83SSudip Mukherjee 		}
352d0aeaa83SSudip Mukherjee 	}
353d0aeaa83SSudip Mukherjee 	priv->nr = i;
354d0aeaa83SSudip Mukherjee 	pci_set_drvdata(pcidev, priv);
355d0aeaa83SSudip Mukherjee 	return 0;
356d0aeaa83SSudip Mukherjee }
357d0aeaa83SSudip Mukherjee 
358d0aeaa83SSudip Mukherjee static void exar_pci_remove(struct pci_dev *pcidev)
359d0aeaa83SSudip Mukherjee {
360d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
361d0aeaa83SSudip Mukherjee 	unsigned int i;
362d0aeaa83SSudip Mukherjee 
363d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
364d0aeaa83SSudip Mukherjee 		serial8250_unregister_port(priv->line[i]);
365d0aeaa83SSudip Mukherjee 
366d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
367d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
368d0aeaa83SSudip Mukherjee }
369d0aeaa83SSudip Mukherjee 
370d0aeaa83SSudip Mukherjee static int __maybe_unused exar_suspend(struct device *dev)
371d0aeaa83SSudip Mukherjee {
372d0aeaa83SSudip Mukherjee 	struct pci_dev *pcidev = to_pci_dev(dev);
373d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
374d0aeaa83SSudip Mukherjee 	unsigned int i;
375d0aeaa83SSudip Mukherjee 
376d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
377d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
378d0aeaa83SSudip Mukherjee 			serial8250_suspend_port(priv->line[i]);
379d0aeaa83SSudip Mukherjee 
380d0aeaa83SSudip Mukherjee 	/* Ensure that every init quirk is properly torn down */
381d0aeaa83SSudip Mukherjee 	if (priv->board->exit)
382d0aeaa83SSudip Mukherjee 		priv->board->exit(pcidev);
383d0aeaa83SSudip Mukherjee 
384d0aeaa83SSudip Mukherjee 	return 0;
385d0aeaa83SSudip Mukherjee }
386d0aeaa83SSudip Mukherjee 
387d0aeaa83SSudip Mukherjee static int __maybe_unused exar_resume(struct device *dev)
388d0aeaa83SSudip Mukherjee {
389d0aeaa83SSudip Mukherjee 	struct pci_dev *pcidev = to_pci_dev(dev);
390d0aeaa83SSudip Mukherjee 	struct exar8250 *priv = pci_get_drvdata(pcidev);
391d0aeaa83SSudip Mukherjee 	unsigned int i;
392d0aeaa83SSudip Mukherjee 
393d0aeaa83SSudip Mukherjee 	for (i = 0; i < priv->nr; i++)
394d0aeaa83SSudip Mukherjee 		if (priv->line[i] >= 0)
395d0aeaa83SSudip Mukherjee 			serial8250_resume_port(priv->line[i]);
396d0aeaa83SSudip Mukherjee 
397d0aeaa83SSudip Mukherjee 	return 0;
398d0aeaa83SSudip Mukherjee }
399d0aeaa83SSudip Mukherjee 
400d0aeaa83SSudip Mukherjee static SIMPLE_DEV_PM_OPS(exar_pci_pm, exar_suspend, exar_resume);
401d0aeaa83SSudip Mukherjee 
402fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_2 = {
403fc6cc961SJan Kiszka 	.num_ports	= 2,
404fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
405fc6cc961SJan Kiszka };
406fc6cc961SJan Kiszka 
407fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_4 = {
408fc6cc961SJan Kiszka 	.num_ports	= 4,
409fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
410fc6cc961SJan Kiszka };
411fc6cc961SJan Kiszka 
412fc6cc961SJan Kiszka static const struct exar8250_board pbn_fastcom335_8 = {
413fc6cc961SJan Kiszka 	.num_ports	= 8,
414fc6cc961SJan Kiszka 	.setup		= pci_fastcom335_setup,
415fc6cc961SJan Kiszka };
416fc6cc961SJan Kiszka 
417d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_connect = {
418d0aeaa83SSudip Mukherjee 	.setup		= pci_connect_tech_setup,
419d0aeaa83SSudip Mukherjee };
420d0aeaa83SSudip Mukherjee 
421d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_ibm_saturn = {
422d0aeaa83SSudip Mukherjee 	.num_ports	= 1,
423d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
424d0aeaa83SSudip Mukherjee };
425d0aeaa83SSudip Mukherjee 
426d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17C15x = {
427d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17c154_setup,
428d0aeaa83SSudip Mukherjee };
429d0aeaa83SSudip Mukherjee 
430d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V35x = {
431d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
432d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
433d0aeaa83SSudip Mukherjee };
434d0aeaa83SSudip Mukherjee 
435d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V4358 = {
436d0aeaa83SSudip Mukherjee 	.num_ports	= 12,
437d0aeaa83SSudip Mukherjee 	.has_slave	= true,
438d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
439d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
440d0aeaa83SSudip Mukherjee };
441d0aeaa83SSudip Mukherjee 
442d0aeaa83SSudip Mukherjee static const struct exar8250_board pbn_exar_XR17V8358 = {
443d0aeaa83SSudip Mukherjee 	.num_ports	= 16,
444d0aeaa83SSudip Mukherjee 	.has_slave	= true,
445d0aeaa83SSudip Mukherjee 	.setup		= pci_xr17v35x_setup,
446d0aeaa83SSudip Mukherjee 	.exit		= pci_xr17v35x_exit,
447d0aeaa83SSudip Mukherjee };
448d0aeaa83SSudip Mukherjee 
449d0aeaa83SSudip Mukherjee #define CONNECT_DEVICE(devid, sdevid, bd) {				\
450d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(							\
451d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,					\
452d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,				\
453d0aeaa83SSudip Mukherjee 		PCI_SUBVENDOR_ID_CONNECT_TECH,				\
454d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_##sdevid), 0, 0,	\
455d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd					\
456d0aeaa83SSudip Mukherjee 	}
457d0aeaa83SSudip Mukherjee 
458d0aeaa83SSudip Mukherjee #define EXAR_DEVICE(vend, devid, bd) {					\
459d0aeaa83SSudip Mukherjee 	PCI_VDEVICE(vend, PCI_DEVICE_ID_##devid), (kernel_ulong_t)&bd	\
460d0aeaa83SSudip Mukherjee 	}
461d0aeaa83SSudip Mukherjee 
462d0aeaa83SSudip Mukherjee #define IBM_DEVICE(devid, sdevid, bd) {			\
463d0aeaa83SSudip Mukherjee 	PCI_DEVICE_SUB(					\
464d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_EXAR,			\
465d0aeaa83SSudip Mukherjee 		PCI_DEVICE_ID_EXAR_##devid,		\
466d0aeaa83SSudip Mukherjee 		PCI_VENDOR_ID_IBM,			\
467d0aeaa83SSudip Mukherjee 		PCI_SUBDEVICE_ID_IBM_##sdevid), 0, 0,	\
468d0aeaa83SSudip Mukherjee 		(kernel_ulong_t)&bd			\
469d0aeaa83SSudip Mukherjee 	}
470d0aeaa83SSudip Mukherjee 
471d0aeaa83SSudip Mukherjee static struct pci_device_id exar_pci_tbl[] = {
472d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_232, pbn_connect),
473d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_232, pbn_connect),
474d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_232, pbn_connect),
475d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_1_1, pbn_connect),
476d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_2_2, pbn_connect),
477d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_4_4, pbn_connect),
478d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2, pbn_connect),
479d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4, pbn_connect),
480d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8, pbn_connect),
481d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C152, UART_2_485, pbn_connect),
482d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C154, UART_4_485, pbn_connect),
483d0aeaa83SSudip Mukherjee 	CONNECT_DEVICE(XR17C158, UART_8_485, pbn_connect),
484d0aeaa83SSudip Mukherjee 
485d0aeaa83SSudip Mukherjee 	IBM_DEVICE(XR17C152, SATURN_SERIAL_ONE_PORT, pbn_exar_ibm_saturn),
486d0aeaa83SSudip Mukherjee 
487d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17C15[248] Dual/Quad/Octal UART */
488d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C152, pbn_exar_XR17C15x),
489d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C154, pbn_exar_XR17C15x),
490d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17C158, pbn_exar_XR17C15x),
491d0aeaa83SSudip Mukherjee 
492d0aeaa83SSudip Mukherjee 	/* Exar Corp. XR17V[48]35[248] Dual/Quad/Octal/Hexa PCIe UARTs */
493d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V352, pbn_exar_XR17V35x),
494d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V354, pbn_exar_XR17V35x),
495d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V358, pbn_exar_XR17V35x),
496d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V4358, pbn_exar_XR17V4358),
497d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(EXAR, EXAR_XR17V8358, pbn_exar_XR17V8358),
498d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4222PCIE, pbn_exar_XR17V35x),
499d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4224PCIE, pbn_exar_XR17V35x),
500d0aeaa83SSudip Mukherjee 	EXAR_DEVICE(COMMTECH, COMMTECH_4228PCIE, pbn_exar_XR17V35x),
501fc6cc961SJan Kiszka 
502fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_4222PCI335, pbn_fastcom335_2),
503fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_4224PCI335, pbn_fastcom335_4),
504fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_2324PCI335, pbn_fastcom335_4),
505fc6cc961SJan Kiszka 	EXAR_DEVICE(COMMTECH, COMMTECH_2328PCI335, pbn_fastcom335_8),
506d0aeaa83SSudip Mukherjee 	{ 0, }
507d0aeaa83SSudip Mukherjee };
508d0aeaa83SSudip Mukherjee MODULE_DEVICE_TABLE(pci, exar_pci_tbl);
509d0aeaa83SSudip Mukherjee 
510d0aeaa83SSudip Mukherjee static struct pci_driver exar_pci_driver = {
511d0aeaa83SSudip Mukherjee 	.name		= "exar_serial",
512d0aeaa83SSudip Mukherjee 	.probe		= exar_pci_probe,
513d0aeaa83SSudip Mukherjee 	.remove		= exar_pci_remove,
514d0aeaa83SSudip Mukherjee 	.driver         = {
515d0aeaa83SSudip Mukherjee 		.pm     = &exar_pci_pm,
516d0aeaa83SSudip Mukherjee 	},
517d0aeaa83SSudip Mukherjee 	.id_table	= exar_pci_tbl,
518d0aeaa83SSudip Mukherjee };
519d0aeaa83SSudip Mukherjee module_pci_driver(exar_pci_driver);
520d0aeaa83SSudip Mukherjee 
521d0aeaa83SSudip Mukherjee MODULE_LICENSE("GPL");
5222b57b7ffSAndy Shevchenko MODULE_DESCRIPTION("Exar Serial Driver");
523d0aeaa83SSudip Mukherjee MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
524