xref: /freebsd/sys/dev/uart/uart_bus_pci.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Marcel Moolenaar
5  * Copyright (c) 2001 M. Warner Losh
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40 #include <machine/resource.h>
41 
42 #include <dev/pci/pcivar.h>
43 
44 #include <dev/uart/uart.h>
45 #include <dev/uart/uart_bus.h>
46 
47 #define	DEFAULT_RCLK	1843200
48 
49 static int uart_pci_probe(device_t dev);
50 static int uart_pci_attach(device_t dev);
51 static int uart_pci_detach(device_t dev);
52 
53 static device_method_t uart_pci_methods[] = {
54 	/* Device interface */
55 	DEVMETHOD(device_probe,		uart_pci_probe),
56 	DEVMETHOD(device_attach,	uart_pci_attach),
57 	DEVMETHOD(device_detach,	uart_pci_detach),
58 	DEVMETHOD(device_resume,	uart_bus_resume),
59 	DEVMETHOD_END
60 };
61 
62 static driver_t uart_pci_driver = {
63 	uart_driver_name,
64 	uart_pci_methods,
65 	sizeof(struct uart_softc),
66 };
67 
68 struct pci_id {
69 	uint16_t	vendor;
70 	uint16_t	device;
71 	uint16_t	subven;
72 	uint16_t	subdev;
73 	const char	*desc;
74 	int		rid;
75 	int		rclk;
76 	int		regshft;
77 };
78 
79 static const struct pci_id pci_ns8250_ids[] = {
80 { 0x1028, 0x0008, 0xffff, 0, "Dell Remote Access Card III", 0x14,
81 	128 * DEFAULT_RCLK },
82 { 0x1028, 0x0012, 0xffff, 0, "Dell RAC 4 Daughter Card Virtual UART", 0x14,
83 	128 * DEFAULT_RCLK },
84 { 0x1033, 0x0074, 0x1033, 0x8014, "NEC RCV56ACF 56k Voice Modem", 0x10 },
85 { 0x1033, 0x007d, 0x1033, 0x8012, "NEC RS232C", 0x10 },
86 { 0x103c, 0x1048, 0x103c, 0x1227, "HP Diva Serial [GSP] UART - Powerbar SP2",
87 	0x10 },
88 { 0x103c, 0x1048, 0x103c, 0x1301, "HP Diva RMP3", 0x14 },
89 { 0x103c, 0x1290, 0xffff, 0, "HP Auxiliary Diva Serial Port", 0x18 },
90 { 0x103c, 0x3301, 0xffff, 0, "HP iLO serial port", 0x10 },
91 { 0x11c1, 0x0480, 0xffff, 0, "Agere Systems Venus Modem (V90, 56KFlex)", 0x14 },
92 { 0x115d, 0x0103, 0xffff, 0, "Xircom Cardbus Ethernet + 56k Modem", 0x10 },
93 { 0x125b, 0x9100, 0xa000, 0x1000,
94 	"ASIX AX99100 PCIe 1/2/3/4-port RS-232/422/485", 0x10 },
95 { 0x1282, 0x6585, 0xffff, 0, "Davicom 56PDV PCI Modem", 0x10 },
96 { 0x12b9, 0x1008, 0xffff, 0, "3Com 56K FaxModem Model 5610", 0x10 },
97 { 0x131f, 0x1000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x18 },
98 { 0x131f, 0x1001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x18 },
99 { 0x131f, 0x1002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x18 },
100 { 0x131f, 0x2000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x10 },
101 { 0x131f, 0x2001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x10 },
102 { 0x131f, 0x2002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x10 },
103 { 0x135c, 0x0190, 0xffff, 0, "Quatech SSCLP-100", 0x18 },
104 { 0x135c, 0x01c0, 0xffff, 0, "Quatech SSCLP-200/300", 0x18 },
105 { 0x135e, 0x7101, 0xffff, 0, "Sealevel Systems Single Port RS-232/422/485/530",
106 	0x18 },
107 { 0x1407, 0x0110, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port A", 0x10 },
108 { 0x1407, 0x0111, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port B", 0x10 },
109 { 0x1407, 0x0510, 0xffff, 0, "Lava SP Serial 550 PCI", 0x10 },
110 { 0x1409, 0x7168, 0x1409, 0x4025, "Timedia Technology Serial Port", 0x10,
111 	8 * DEFAULT_RCLK },
112 { 0x1409, 0x7168, 0x1409, 0x4027, "Timedia Technology Serial Port", 0x10,
113 	8 * DEFAULT_RCLK },
114 { 0x1409, 0x7168, 0x1409, 0x4028, "Timedia Technology Serial Port", 0x10,
115 	8 * DEFAULT_RCLK },
116 { 0x1409, 0x7168, 0x1409, 0x5025, "Timedia Technology Serial Port", 0x10,
117 	8 * DEFAULT_RCLK },
118 { 0x1409, 0x7168, 0x1409, 0x5027, "Timedia Technology Serial Port", 0x10,
119 	8 * DEFAULT_RCLK },
120 { 0x1415, 0x950b, 0xffff, 0, "Oxford Semiconductor OXCB950 Cardbus 16950 UART",
121 	0x10, 16384000 },
122 { 0x1415, 0xc120, 0xffff, 0, "Oxford Semiconductor OXPCIe952 PCIe 16950 UART",
123 	0x10 },
124 { 0x14e4, 0x160a, 0xffff, 0, "Broadcom TruManage UART", 0x10,
125 	128 * DEFAULT_RCLK, 2},
126 { 0x14e4, 0x4344, 0xffff, 0, "Sony Ericsson GC89 PC Card", 0x10},
127 { 0x151f, 0x0000, 0xffff, 0, "TOPIC Semiconductor TP560 56k modem", 0x10 },
128 { 0x1d0f, 0x8250, 0x1d0f, 0, "Amazon PCI serial device", 0x10 },
129 { 0x1fd4, 0x1999, 0x1fd4, 0x0001, "Sunix SER5xxxx Serial Port", 0x10,
130 	8 * DEFAULT_RCLK },
131 { 0x8086, 0x0f0a, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#1", 0x10,
132 	24 * DEFAULT_RCLK, 2 },
133 { 0x8086, 0x0f0c, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#2", 0x10,
134 	24 * DEFAULT_RCLK, 2 },
135 { 0x8086, 0x108f, 0xffff, 0, "Intel AMT - SOL", 0x10 },
136 { 0x8086, 0x1c3d, 0xffff, 0, "Intel AMT - KT Controller", 0x10 },
137 { 0x8086, 0x1d3d, 0xffff, 0, "Intel C600/X79 Series Chipset KT Controller", 0x10 },
138 { 0x8086, 0x1e3d, 0xffff, 0, "Intel Panther Point KT Controller", 0x10 },
139 { 0x8086, 0x228a, 0xffff, 0, "Intel Cherryview SIO HSUART#1", 0x10,
140        24 * DEFAULT_RCLK, 2 },
141 { 0x8086, 0x228c, 0xffff, 0, "Intel Cherryview SIO HSUART#2", 0x10,
142        24 * DEFAULT_RCLK, 2 },
143 { 0x8086, 0x2a07, 0xffff, 0, "Intel AMT - PM965/GM965 KT Controller", 0x10 },
144 { 0x8086, 0x2a47, 0xffff, 0, "Mobile 4 Series Chipset KT Controller", 0x10 },
145 { 0x8086, 0x2e17, 0xffff, 0, "4 Series Chipset Serial KT Controller", 0x10 },
146 { 0x8086, 0x3b67, 0xffff, 0, "5 Series/3400 Series Chipset KT Controller",
147 	0x10 },
148 { 0x8086, 0x8811, 0xffff, 0, "Intel EG20T Serial Port 0", 0x10 },
149 { 0x8086, 0x8812, 0xffff, 0, "Intel EG20T Serial Port 1", 0x10 },
150 { 0x8086, 0x8813, 0xffff, 0, "Intel EG20T Serial Port 2", 0x10 },
151 { 0x8086, 0x8814, 0xffff, 0, "Intel EG20T Serial Port 3", 0x10 },
152 { 0x8086, 0x8c3d, 0xffff, 0, "Intel Lynx Point KT Controller", 0x10 },
153 { 0x8086, 0x8cbd, 0xffff, 0, "Intel Wildcat Point KT Controller", 0x10 },
154 { 0x8086, 0x9c3d, 0xffff, 0, "Intel Lynx Point-LP HECI KT", 0x10 },
155 { 0x9710, 0x9820, 0x1000, 1, "NetMos NM9820 Serial Port", 0x10 },
156 { 0x9710, 0x9835, 0x1000, 1, "NetMos NM9835 Serial Port", 0x10 },
157 { 0x9710, 0x9865, 0xa000, 0x1000, "NetMos NM9865 Serial Port", 0x10 },
158 { 0x9710, 0x9900, 0xa000, 0x1000,
159 	"MosChip MCS9900 PCIe to Peripheral Controller", 0x10 },
160 { 0x9710, 0x9901, 0xa000, 0x1000,
161 	"MosChip MCS9901 PCIe to Peripheral Controller", 0x10 },
162 { 0x9710, 0x9904, 0xa000, 0x1000,
163 	"MosChip MCS9904 PCIe to Peripheral Controller", 0x10 },
164 { 0x9710, 0x9922, 0xa000, 0x1000,
165 	"MosChip MCS9922 PCIe to Peripheral Controller", 0x10 },
166 { 0xdeaf, 0x9051, 0xffff, 0, "Middle Digital PC Weasel Serial Port", 0x10 },
167 { 0xffff, 0, 0xffff, 0, NULL, 0, 0}
168 };
169 
170 const static struct pci_id *
171 uart_pci_match(device_t dev, const struct pci_id *id)
172 {
173 	uint16_t device, subdev, subven, vendor;
174 
175 	vendor = pci_get_vendor(dev);
176 	device = pci_get_device(dev);
177 	while (id->vendor != 0xffff &&
178 	    (id->vendor != vendor || id->device != device))
179 		id++;
180 	if (id->vendor == 0xffff)
181 		return (NULL);
182 	if (id->subven == 0xffff)
183 		return (id);
184 	subven = pci_get_subvendor(dev);
185 	subdev = pci_get_subdevice(dev);
186 	while (id->vendor == vendor && id->device == device &&
187 	    (id->subven != subven || id->subdev != subdev))
188 		id++;
189 	return ((id->vendor == vendor && id->device == device) ? id : NULL);
190 }
191 
192 static int
193 uart_pci_probe(device_t dev)
194 {
195 	struct uart_softc *sc;
196 	const struct pci_id *id;
197 	int result;
198 
199 	sc = device_get_softc(dev);
200 
201 	id = uart_pci_match(dev, pci_ns8250_ids);
202 	if (id != NULL) {
203 		sc->sc_class = &uart_ns8250_class;
204 		goto match;
205 	}
206 	/* Add checks for non-ns8250 IDs here. */
207 	return (ENXIO);
208 
209  match:
210 	result = uart_bus_probe(dev, id->regshft, 0, id->rclk, id->rid, 0, 0);
211 	/* Bail out on error. */
212 	if (result > 0)
213 		return (result);
214 	/* Set/override the device description. */
215 	if (id->desc)
216 		device_set_desc(dev, id->desc);
217 	return (result);
218 }
219 
220 static int
221 uart_pci_attach(device_t dev)
222 {
223 	struct uart_softc *sc;
224 	int count;
225 
226 	sc = device_get_softc(dev);
227 
228 	/*
229 	 * Use MSI in preference to legacy IRQ if available.
230 	 * Whilst some PCIe UARTs support >1 MSI vector, use only the first.
231 	 */
232 	if (pci_msi_count(dev) > 0) {
233 		count = 1;
234 		if (pci_alloc_msi(dev, &count) == 0) {
235 			sc->sc_irid = 1;
236 			device_printf(dev, "Using %d MSI message\n", count);
237 		}
238 	}
239 
240 	return (uart_bus_attach(dev));
241 }
242 
243 static int
244 uart_pci_detach(device_t dev)
245 {
246 	struct uart_softc *sc;
247 
248 	sc = device_get_softc(dev);
249 
250 	if (sc->sc_irid != 0)
251 		pci_release_msi(dev);
252 
253 	return (uart_bus_detach(dev));
254 }
255 
256 DRIVER_MODULE(uart, pci, uart_pci_driver, uart_devclass, NULL, NULL);
257