1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2006 Marcel Moolenaar All rights reserved.
5 * Copyright (c) 2001 M. Warner Losh <imp@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <machine/bus.h>
35 #include <sys/rman.h>
36 #include <machine/resource.h>
37
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40
41 #include <dev/uart/uart.h>
42 #include <dev/uart/uart_bus.h>
43 #include <dev/uart/uart_cpu.h>
44
45 #define DEFAULT_RCLK 1843200
46
47 static int uart_pci_probe(device_t dev);
48 static int uart_pci_attach(device_t dev);
49 static int uart_pci_detach(device_t dev);
50
51 static device_method_t uart_pci_methods[] = {
52 /* Device interface */
53 DEVMETHOD(device_probe, uart_pci_probe),
54 DEVMETHOD(device_attach, uart_pci_attach),
55 DEVMETHOD(device_detach, uart_pci_detach),
56 DEVMETHOD(device_resume, uart_bus_resume),
57 DEVMETHOD_END
58 };
59
60 static driver_t uart_pci_driver = {
61 uart_driver_name,
62 uart_pci_methods,
63 sizeof(struct uart_softc),
64 };
65
66 struct pci_id {
67 uint16_t vendor;
68 uint16_t device;
69 uint16_t subven;
70 uint16_t subdev;
71 const char *desc;
72 int rid;
73 int rclk;
74 int regshft;
75 };
76
77 struct pci_unique_id {
78 uint16_t vendor;
79 uint16_t device;
80 };
81
82 #define PCI_NO_MSI 0x40000000
83 #define PCI_RID_MASK 0x0000ffff
84
85 static const struct pci_id pci_ns8250_ids[] = {
86 { 0x1028, 0x0008, 0xffff, 0, "Dell Remote Access Card III", 0x14,
87 128 * DEFAULT_RCLK },
88 { 0x1028, 0x0012, 0xffff, 0, "Dell RAC 4 Daughter Card Virtual UART", 0x14,
89 128 * DEFAULT_RCLK },
90 { 0x1033, 0x0074, 0x1033, 0x8014, "NEC RCV56ACF 56k Voice Modem", 0x10 },
91 { 0x1033, 0x007d, 0x1033, 0x8012, "NEC RS232C", 0x10 },
92 { 0x103c, 0x1048, 0x103c, 0x1227, "HP Diva Serial [GSP] UART - Powerbar SP2",
93 0x10 },
94 { 0x103c, 0x1048, 0x103c, 0x1301, "HP Diva RMP3", 0x14 },
95 { 0x103c, 0x1290, 0xffff, 0, "HP Auxiliary Diva Serial Port", 0x18 },
96 { 0x103c, 0x3301, 0xffff, 0, "HP iLO serial port", 0x10 },
97 { 0x11c1, 0x0480, 0xffff, 0, "Agere Systems Venus Modem (V90, 56KFlex)", 0x14 },
98 { 0x115d, 0x0103, 0xffff, 0, "Xircom Cardbus Ethernet + 56k Modem", 0x10 },
99 { 0x125b, 0x9100, 0xa000, 0x1000,
100 "ASIX AX99100 PCIe 1/2/3/4-port RS-232/422/485", 0x10 },
101 { 0x1282, 0x6585, 0xffff, 0, "Davicom 56PDV PCI Modem", 0x10 },
102 { 0x12b9, 0x1008, 0xffff, 0, "3Com 56K FaxModem Model 5610", 0x10 },
103 { 0x131f, 0x1000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x18 },
104 { 0x131f, 0x1001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x18 },
105 { 0x131f, 0x1002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x18 },
106 { 0x131f, 0x2000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x10 },
107 { 0x131f, 0x2001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x10 },
108 { 0x131f, 0x2002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x10 },
109 { 0x135a, 0x0a61, 0xffff, 0, "Brainboxes UC-324", 0x18 },
110 { 0x135a, 0x0aa1, 0xffff, 0, "Brainboxes UC-246", 0x18 },
111 { 0x135a, 0x0aa2, 0xffff, 0, "Brainboxes UC-246", 0x18 },
112 { 0x135a, 0x0d60, 0xffff, 0, "Intashield IS-100", 0x18 },
113 { 0x135a, 0x0da0, 0xffff, 0, "Intashield IS-300", 0x18 },
114 { 0x135a, 0x4000, 0xffff, 0, "Brainboxes PX-420", 0x10 },
115 { 0x135a, 0x4001, 0xffff, 0, "Brainboxes PX-431", 0x10 },
116 { 0x135a, 0x4002, 0xffff, 0, "Brainboxes PX-820", 0x10 },
117 { 0x135a, 0x4003, 0xffff, 0, "Brainboxes PX-831", 0x10 },
118 { 0x135a, 0x4004, 0xffff, 0, "Brainboxes PX-246", 0x10 },
119 { 0x135a, 0x4005, 0xffff, 0, "Brainboxes PX-101", 0x10 },
120 { 0x135a, 0x4006, 0xffff, 0, "Brainboxes PX-257", 0x10 },
121 { 0x135a, 0x4008, 0xffff, 0, "Brainboxes PX-846", 0x10 },
122 { 0x135a, 0x4009, 0xffff, 0, "Brainboxes PX-857", 0x10 },
123 { 0x135c, 0x0190, 0xffff, 0, "Quatech SSCLP-100", 0x18 },
124 { 0x135c, 0x01c0, 0xffff, 0, "Quatech SSCLP-200/300", 0x18 },
125 { 0x135e, 0x7101, 0xffff, 0, "Sealevel Systems Single Port RS-232/422/485/530",
126 0x18 },
127 { 0x1407, 0x0110, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port A", 0x10 },
128 { 0x1407, 0x0111, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port B", 0x10 },
129 { 0x1407, 0x0510, 0xffff, 0, "Lava SP Serial 550 PCI", 0x10 },
130 { 0x1409, 0x7168, 0x1409, 0x4025, "Timedia Technology Serial Port", 0x10,
131 8 * DEFAULT_RCLK },
132 { 0x1409, 0x7168, 0x1409, 0x4027, "Timedia Technology Serial Port", 0x10,
133 8 * DEFAULT_RCLK },
134 { 0x1409, 0x7168, 0x1409, 0x4028, "Timedia Technology Serial Port", 0x10,
135 8 * DEFAULT_RCLK },
136 { 0x1409, 0x7168, 0x1409, 0x5025, "Timedia Technology Serial Port", 0x10,
137 8 * DEFAULT_RCLK },
138 { 0x1409, 0x7168, 0x1409, 0x5027, "Timedia Technology Serial Port", 0x10,
139 8 * DEFAULT_RCLK },
140 { 0x1415, 0x950b, 0xffff, 0, "Oxford Semiconductor OXCB950 Cardbus 16950 UART",
141 0x10, 16384000 },
142 { 0x1415, 0xc120, 0xffff, 0, "Oxford Semiconductor OXPCIe952 PCIe 16950 UART",
143 0x10 },
144 { 0x14a1, 0x0008, 0x14a1, 0x0008, "Systembase SB16C1058",
145 0x10, 8 * DEFAULT_RCLK, },
146 { 0x14e4, 0x160a, 0xffff, 0, "Broadcom TruManage UART", 0x10,
147 128 * DEFAULT_RCLK, 2},
148 { 0x14e4, 0x4344, 0xffff, 0, "Sony Ericsson GC89 PC Card", 0x10},
149 { 0x151f, 0x0000, 0xffff, 0, "TOPIC Semiconductor TP560 56k modem", 0x10 },
150 { 0x1d0f, 0x8250, 0x0000, 0, "Amazon PCI serial device", 0x10 },
151 { 0x1d0f, 0x8250, 0x1d0f, 0, "Amazon PCI serial device", 0x10 },
152 { 0x1fd4, 0x1999, 0x1fd4, 0x0001, "Sunix SER5xxxx Serial Port", 0x10,
153 8 * DEFAULT_RCLK },
154 { 0x8086, 0x0c5f, 0xffff, 0, "Atom Processor S1200 UART",
155 0x10 | PCI_NO_MSI },
156 { 0x8086, 0x0f0a, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#1", 0x10,
157 24 * DEFAULT_RCLK, 2 },
158 { 0x8086, 0x0f0c, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#2", 0x10,
159 24 * DEFAULT_RCLK, 2 },
160 { 0x8086, 0x108f, 0xffff, 0, "Intel AMT - SOL", 0x10 },
161 { 0x8086, 0x19d8, 0xffff, 0, "Intel Denverton UART", 0x10 },
162 { 0x8086, 0x1c3d, 0xffff, 0, "Intel AMT - KT Controller", 0x10 },
163 { 0x8086, 0x1d3d, 0xffff, 0, "Intel C600/X79 Series Chipset KT Controller",
164 0x10 },
165 { 0x8086, 0x1e3d, 0xffff, 0, "Intel Panther Point KT Controller", 0x10 },
166 { 0x8086, 0x228a, 0xffff, 0, "Intel Cherryview SIO HSUART#1", 0x10,
167 24 * DEFAULT_RCLK, 2 },
168 { 0x8086, 0x228c, 0xffff, 0, "Intel Cherryview SIO HSUART#2", 0x10,
169 24 * DEFAULT_RCLK, 2 },
170 { 0x8086, 0x2a07, 0xffff, 0, "Intel AMT - PM965/GM965 KT Controller", 0x10 },
171 { 0x8086, 0x2a47, 0xffff, 0, "Mobile 4 Series Chipset KT Controller", 0x10 },
172 { 0x8086, 0x2e17, 0xffff, 0, "4 Series Chipset Serial KT Controller", 0x10 },
173 { 0x8086, 0x31bc, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 0", 0x10,
174 24 * DEFAULT_RCLK, 2 },
175 { 0x8086, 0x31be, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 1", 0x10,
176 24 * DEFAULT_RCLK, 2 },
177 { 0x8086, 0x31c0, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 2", 0x10,
178 24 * DEFAULT_RCLK, 2 },
179 { 0x8086, 0x31ee, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 3", 0x10,
180 24 * DEFAULT_RCLK, 2 },
181 { 0x8086, 0x3b67, 0xffff, 0, "5 Series/3400 Series Chipset KT Controller",
182 0x10 },
183 { 0x8086, 0x5abc, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 0", 0x10,
184 24 * DEFAULT_RCLK, 2 },
185 { 0x8086, 0x5abe, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 1", 0x10,
186 24 * DEFAULT_RCLK, 2 },
187 { 0x8086, 0x5ac0, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 2", 0x10,
188 24 * DEFAULT_RCLK, 2 },
189 { 0x8086, 0x5aee, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 3", 0x10,
190 24 * DEFAULT_RCLK, 2 },
191 { 0x8086, 0x8811, 0xffff, 0, "Intel EG20T Serial Port 0", 0x10 },
192 { 0x8086, 0x8812, 0xffff, 0, "Intel EG20T Serial Port 1", 0x10 },
193 { 0x8086, 0x8813, 0xffff, 0, "Intel EG20T Serial Port 2", 0x10 },
194 { 0x8086, 0x8814, 0xffff, 0, "Intel EG20T Serial Port 3", 0x10 },
195 { 0x8086, 0x8c3d, 0xffff, 0, "Intel Lynx Point KT Controller", 0x10 },
196 { 0x8086, 0x8cbd, 0xffff, 0, "Intel Wildcat Point KT Controller", 0x10 },
197 { 0x8086, 0x8d3d, 0xffff, 0,
198 "Intel Corporation C610/X99 series chipset KT Controller", 0x10 },
199 { 0x8086, 0x9c3d, 0xffff, 0, "Intel Lynx Point-LP HECI KT", 0x10 },
200 { 0x8086, 0xa13d, 0xffff, 0,
201 "100 Series/C230 Series Chipset Family KT Redirection",
202 0x10 | PCI_NO_MSI },
203 { 0x9710, 0x9820, 0x1000, 1, "NetMos NM9820 Serial Port", 0x10 },
204 { 0x9710, 0x9835, 0x1000, 1, "NetMos NM9835 Serial Port", 0x10 },
205 { 0x9710, 0x9865, 0xa000, 0x1000, "NetMos NM9865 Serial Port", 0x10 },
206 { 0x9710, 0x9900, 0xa000, 0x1000,
207 "MosChip MCS9900 PCIe to Peripheral Controller", 0x10 },
208 { 0x9710, 0x9901, 0xa000, 0x1000,
209 "MosChip MCS9901 PCIe to Peripheral Controller", 0x10 },
210 { 0x9710, 0x9904, 0xa000, 0x1000,
211 "MosChip MCS9904 PCIe to Peripheral Controller", 0x10 },
212 { 0x9710, 0x9922, 0xa000, 0x1000,
213 "MosChip MCS9922 PCIe to Peripheral Controller", 0x10 },
214 { 0xdeaf, 0x9051, 0xffff, 0, "Middle Digital PC Weasel Serial Port", 0x10 },
215 { 0xffff, 0, 0xffff, 0, NULL, 0, 0}
216 };
217
218 const static struct pci_id *
uart_pci_match(device_t dev,const struct pci_id * id)219 uart_pci_match(device_t dev, const struct pci_id *id)
220 {
221 uint16_t device, subdev, subven, vendor;
222
223 vendor = pci_get_vendor(dev);
224 device = pci_get_device(dev);
225 while (id->vendor != 0xffff &&
226 (id->vendor != vendor || id->device != device))
227 id++;
228 if (id->vendor == 0xffff)
229 return (NULL);
230 if (id->subven == 0xffff)
231 return (id);
232 subven = pci_get_subvendor(dev);
233 subdev = pci_get_subdevice(dev);
234 while (id->vendor == vendor && id->device == device &&
235 (id->subven != subven || id->subdev != subdev))
236 id++;
237 return ((id->vendor == vendor && id->device == device) ? id : NULL);
238 }
239
240 extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
241
242 /* PCI vendor/device pairs of devices guaranteed to be unique on a system. */
243 static const struct pci_unique_id pci_unique_devices[] = {
244 { 0x1d0f, 0x8250 } /* Amazon PCI serial device */
245 };
246
247 /* Match a UART to a console if it's a PCI device known to be unique. */
248 static void
uart_pci_unique_console_match(device_t dev)249 uart_pci_unique_console_match(device_t dev)
250 {
251 struct uart_softc *sc;
252 struct uart_devinfo * sysdev;
253 const struct pci_unique_id * id;
254 uint16_t vendor, device;
255
256 sc = device_get_softc(dev);
257 vendor = pci_get_vendor(dev);
258 device = pci_get_device(dev);
259
260 /* Is this a device known to exist only once in a system? */
261 for (id = pci_unique_devices; ; id++) {
262 if (id == &pci_unique_devices[nitems(pci_unique_devices)])
263 return;
264 if (id->vendor == vendor && id->device == device)
265 break;
266 }
267
268 /* If it matches a console, it must be the same device. */
269 SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
270 if (sysdev->pci_info.vendor == vendor &&
271 sysdev->pci_info.device == device) {
272 sc->sc_sysdev = sysdev;
273 sysdev->bas.rclk = sc->sc_bas.rclk;
274 }
275 }
276 }
277
278 static int
uart_pci_probe(device_t dev)279 uart_pci_probe(device_t dev)
280 {
281 struct uart_softc *sc;
282 const struct pci_id *id;
283 struct pci_id cid = {
284 .regshft = 0,
285 .rclk = 0,
286 .rid = 0x10 | PCI_NO_MSI,
287 .desc = "Generic SimpleComm PCI device",
288 };
289 int result;
290
291 sc = device_get_softc(dev);
292
293 id = uart_pci_match(dev, pci_ns8250_ids);
294 if (id != NULL) {
295 sc->sc_class = &uart_ns8250_class;
296 goto match;
297 }
298 if (pci_get_class(dev) == PCIC_SIMPLECOMM &&
299 pci_get_subclass(dev) == PCIS_SIMPLECOMM_UART &&
300 pci_get_progif(dev) < PCIP_SIMPLECOMM_UART_16550A) {
301 /* XXX rclk what to do */
302 id = &cid;
303 sc->sc_class = &uart_ns8250_class;
304 goto match;
305 }
306 /* Add checks for non-ns8250 IDs here. */
307 return (ENXIO);
308
309 match:
310 result = uart_bus_probe(dev, id->regshft, 0, id->rclk,
311 id->rid & PCI_RID_MASK, 0, 0);
312 /* Bail out on error. */
313 if (result > 0)
314 return (result);
315 /*
316 * If we haven't already matched this to a console, check if it's a
317 * PCI device which is known to only exist once in any given system
318 * and we can match it that way.
319 */
320 if (sc->sc_sysdev == NULL)
321 uart_pci_unique_console_match(dev);
322 /* Set/override the device description. */
323 if (id->desc)
324 device_set_desc(dev, id->desc);
325 return (result);
326 }
327
328 static int
uart_pci_attach(device_t dev)329 uart_pci_attach(device_t dev)
330 {
331 struct uart_softc *sc;
332 const struct pci_id *id;
333 int count;
334
335 sc = device_get_softc(dev);
336
337 /*
338 * Use MSI in preference to legacy IRQ if available. However, experience
339 * suggests this is only reliable when one MSI vector is advertised.
340 */
341 id = uart_pci_match(dev, pci_ns8250_ids);
342 if ((id == NULL || (id->rid & PCI_NO_MSI) == 0) &&
343 pci_msi_count(dev) == 1) {
344 count = 1;
345 if (pci_alloc_msi(dev, &count) == 0) {
346 sc->sc_irid = 1;
347 device_printf(dev, "Using %d MSI message\n", count);
348 }
349 }
350
351 return (uart_bus_attach(dev));
352 }
353
354 static int
uart_pci_detach(device_t dev)355 uart_pci_detach(device_t dev)
356 {
357 struct uart_softc *sc;
358
359 sc = device_get_softc(dev);
360
361 if (sc->sc_irid != 0)
362 pci_release_msi(dev);
363
364 return (uart_bus_detach(dev));
365 }
366
367 DRIVER_MODULE(uart, pci, uart_pci_driver, NULL, NULL);
368