xref: /freebsd/sys/dev/uart/uart_bus_pci.c (revision bf40080762e939b44d4dd0fbfac5127f2a2562ce)
1098ca2bdSWarner Losh /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 2006 Marcel Moolenaar All rights reserved.
5f86e6000SWarner Losh  * Copyright (c) 2001 M. Warner Losh <imp@FreeBSD.org>
627d5dc18SMarcel Moolenaar  *
727d5dc18SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
827d5dc18SMarcel Moolenaar  * modification, are permitted provided that the following conditions
927d5dc18SMarcel Moolenaar  * are met:
1027d5dc18SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1127d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1227d5dc18SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1327d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1427d5dc18SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1527d5dc18SMarcel Moolenaar  *
1627d5dc18SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1727d5dc18SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1827d5dc18SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1927d5dc18SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2027d5dc18SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2127d5dc18SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2227d5dc18SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2327d5dc18SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2427d5dc18SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2527d5dc18SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2627d5dc18SMarcel Moolenaar  */
2727d5dc18SMarcel Moolenaar 
2827d5dc18SMarcel Moolenaar #include <sys/cdefs.h>
2927d5dc18SMarcel Moolenaar __FBSDID("$FreeBSD$");
3027d5dc18SMarcel Moolenaar 
3127d5dc18SMarcel Moolenaar #include <sys/param.h>
3227d5dc18SMarcel Moolenaar #include <sys/systm.h>
3327d5dc18SMarcel Moolenaar #include <sys/bus.h>
3427d5dc18SMarcel Moolenaar #include <sys/conf.h>
3527d5dc18SMarcel Moolenaar #include <sys/kernel.h>
3627d5dc18SMarcel Moolenaar #include <sys/module.h>
3727d5dc18SMarcel Moolenaar #include <machine/bus.h>
3827d5dc18SMarcel Moolenaar #include <sys/rman.h>
3927d5dc18SMarcel Moolenaar #include <machine/resource.h>
4027d5dc18SMarcel Moolenaar 
4127d5dc18SMarcel Moolenaar #include <dev/pci/pcivar.h>
4227d5dc18SMarcel Moolenaar 
4327d5dc18SMarcel Moolenaar #include <dev/uart/uart.h>
4427d5dc18SMarcel Moolenaar #include <dev/uart/uart_bus.h>
4527d5dc18SMarcel Moolenaar 
4624f9da03SMarcel Moolenaar #define	DEFAULT_RCLK	1843200
4724f9da03SMarcel Moolenaar 
4827d5dc18SMarcel Moolenaar static int uart_pci_probe(device_t dev);
49fb1d9b7fSBruce M Simpson static int uart_pci_attach(device_t dev);
50fb1d9b7fSBruce M Simpson static int uart_pci_detach(device_t dev);
5127d5dc18SMarcel Moolenaar 
5227d5dc18SMarcel Moolenaar static device_method_t uart_pci_methods[] = {
5327d5dc18SMarcel Moolenaar 	/* Device interface */
5427d5dc18SMarcel Moolenaar 	DEVMETHOD(device_probe,		uart_pci_probe),
55fb1d9b7fSBruce M Simpson 	DEVMETHOD(device_attach,	uart_pci_attach),
56fb1d9b7fSBruce M Simpson 	DEVMETHOD(device_detach,	uart_pci_detach),
575b23b1b9SAndriy Gapon 	DEVMETHOD(device_resume,	uart_bus_resume),
58dec28016SMarius Strobl 	DEVMETHOD_END
5927d5dc18SMarcel Moolenaar };
6027d5dc18SMarcel Moolenaar 
6127d5dc18SMarcel Moolenaar static driver_t uart_pci_driver = {
6227d5dc18SMarcel Moolenaar 	uart_driver_name,
6327d5dc18SMarcel Moolenaar 	uart_pci_methods,
6427d5dc18SMarcel Moolenaar 	sizeof(struct uart_softc),
6527d5dc18SMarcel Moolenaar };
6627d5dc18SMarcel Moolenaar 
6727d5dc18SMarcel Moolenaar struct pci_id {
6824f9da03SMarcel Moolenaar 	uint16_t	vendor;
6924f9da03SMarcel Moolenaar 	uint16_t	device;
7024f9da03SMarcel Moolenaar 	uint16_t	subven;
7124f9da03SMarcel Moolenaar 	uint16_t	subdev;
7227d5dc18SMarcel Moolenaar 	const char	*desc;
7327d5dc18SMarcel Moolenaar 	int		rid;
7424f9da03SMarcel Moolenaar 	int		rclk;
759308005eSMarcel Moolenaar 	int		regshft;
7627d5dc18SMarcel Moolenaar };
7727d5dc18SMarcel Moolenaar 
78*bf400807SWarner Losh #define PCI_NO_MSI	0x40000000
79*bf400807SWarner Losh #define PCI_RID_MASK	0x0000ffff
80*bf400807SWarner Losh 
81dec28016SMarius Strobl static const struct pci_id pci_ns8250_ids[] = {
82ab2b8832SMarcel Moolenaar { 0x1028, 0x0008, 0xffff, 0, "Dell Remote Access Card III", 0x14,
83ab2b8832SMarcel Moolenaar 	128 * DEFAULT_RCLK },
84ab2b8832SMarcel Moolenaar { 0x1028, 0x0012, 0xffff, 0, "Dell RAC 4 Daughter Card Virtual UART", 0x14,
85ab2b8832SMarcel Moolenaar 	128 * DEFAULT_RCLK },
8624f9da03SMarcel Moolenaar { 0x1033, 0x0074, 0x1033, 0x8014, "NEC RCV56ACF 56k Voice Modem", 0x10 },
8724f9da03SMarcel Moolenaar { 0x1033, 0x007d, 0x1033, 0x8012, "NEC RS232C", 0x10 },
88ab2b8832SMarcel Moolenaar { 0x103c, 0x1048, 0x103c, 0x1227, "HP Diva Serial [GSP] UART - Powerbar SP2",
89ab2b8832SMarcel Moolenaar 	0x10 },
907db977fbSMarcel Moolenaar { 0x103c, 0x1048, 0x103c, 0x1301, "HP Diva RMP3", 0x14 },
91ab2b8832SMarcel Moolenaar { 0x103c, 0x1290, 0xffff, 0, "HP Auxiliary Diva Serial Port", 0x18 },
92b1a64a96SMarcel Moolenaar { 0x103c, 0x3301, 0xffff, 0, "HP iLO serial port", 0x10 },
9324f9da03SMarcel Moolenaar { 0x11c1, 0x0480, 0xffff, 0, "Agere Systems Venus Modem (V90, 56KFlex)", 0x14 },
9424f9da03SMarcel Moolenaar { 0x115d, 0x0103, 0xffff, 0, "Xircom Cardbus Ethernet + 56k Modem", 0x10 },
95059178b6SEd Maste { 0x125b, 0x9100, 0xa000, 0x1000,
96059178b6SEd Maste 	"ASIX AX99100 PCIe 1/2/3/4-port RS-232/422/485", 0x10 },
972f8c04adSJohn Baldwin { 0x1282, 0x6585, 0xffff, 0, "Davicom 56PDV PCI Modem", 0x10 },
9824f9da03SMarcel Moolenaar { 0x12b9, 0x1008, 0xffff, 0, "3Com 56K FaxModem Model 5610", 0x10 },
9924f9da03SMarcel Moolenaar { 0x131f, 0x1000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x18 },
10024f9da03SMarcel Moolenaar { 0x131f, 0x1001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x18 },
10124f9da03SMarcel Moolenaar { 0x131f, 0x1002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x18 },
10224f9da03SMarcel Moolenaar { 0x131f, 0x2000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x10 },
10324f9da03SMarcel Moolenaar { 0x131f, 0x2001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x10 },
10424f9da03SMarcel Moolenaar { 0x131f, 0x2002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x10 },
10524f9da03SMarcel Moolenaar { 0x135c, 0x0190, 0xffff, 0, "Quatech SSCLP-100", 0x18 },
10624f9da03SMarcel Moolenaar { 0x135c, 0x01c0, 0xffff, 0, "Quatech SSCLP-200/300", 0x18 },
107ab2b8832SMarcel Moolenaar { 0x135e, 0x7101, 0xffff, 0, "Sealevel Systems Single Port RS-232/422/485/530",
108ab2b8832SMarcel Moolenaar 	0x18 },
10924f9da03SMarcel Moolenaar { 0x1407, 0x0110, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port A", 0x10 },
11024f9da03SMarcel Moolenaar { 0x1407, 0x0111, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port B", 0x10 },
11164fc5491SKai Wang { 0x1407, 0x0510, 0xffff, 0, "Lava SP Serial 550 PCI", 0x10 },
112ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4025, "Timedia Technology Serial Port", 0x10,
113ab2b8832SMarcel Moolenaar 	8 * DEFAULT_RCLK },
114ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4027, "Timedia Technology Serial Port", 0x10,
115ab2b8832SMarcel Moolenaar 	8 * DEFAULT_RCLK },
116ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4028, "Timedia Technology Serial Port", 0x10,
117ab2b8832SMarcel Moolenaar 	8 * DEFAULT_RCLK },
118ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x5025, "Timedia Technology Serial Port", 0x10,
119ab2b8832SMarcel Moolenaar 	8 * DEFAULT_RCLK },
120ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x5027, "Timedia Technology Serial Port", 0x10,
121ab2b8832SMarcel Moolenaar 	8 * DEFAULT_RCLK },
122ab2b8832SMarcel Moolenaar { 0x1415, 0x950b, 0xffff, 0, "Oxford Semiconductor OXCB950 Cardbus 16950 UART",
123ab2b8832SMarcel Moolenaar 	0x10, 16384000 },
1247e47312cSPeter Grehan { 0x1415, 0xc120, 0xffff, 0, "Oxford Semiconductor OXPCIe952 PCIe 16950 UART",
1257e47312cSPeter Grehan 	0x10 },
1267bd6f3d0SMarcel Moolenaar { 0x14e4, 0x160a, 0xffff, 0, "Broadcom TruManage UART", 0x10,
1277bd6f3d0SMarcel Moolenaar 	128 * DEFAULT_RCLK, 2},
128383227f1SEitan Adler { 0x14e4, 0x4344, 0xffff, 0, "Sony Ericsson GC89 PC Card", 0x10},
129ab2b8832SMarcel Moolenaar { 0x151f, 0x0000, 0xffff, 0, "TOPIC Semiconductor TP560 56k modem", 0x10 },
1301bfa40d2SColin Percival { 0x1d0f, 0x8250, 0x0000, 0, "Amazon PCI serial device", 0x10 },
131189bd7a9SColin Percival { 0x1d0f, 0x8250, 0x1d0f, 0, "Amazon PCI serial device", 0x10 },
13250c0e894SMarius Strobl { 0x1fd4, 0x1999, 0x1fd4, 0x0001, "Sunix SER5xxxx Serial Port", 0x10,
13350c0e894SMarius Strobl 	8 * DEFAULT_RCLK },
1349308005eSMarcel Moolenaar { 0x8086, 0x0f0a, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#1", 0x10,
1359308005eSMarcel Moolenaar 	24 * DEFAULT_RCLK, 2 },
1369308005eSMarcel Moolenaar { 0x8086, 0x0f0c, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#2", 0x10,
1379308005eSMarcel Moolenaar 	24 * DEFAULT_RCLK, 2 },
138ef7161e7SEd Maste { 0x8086, 0x108f, 0xffff, 0, "Intel AMT - SOL", 0x10 },
139f37cc713SJustin Hibbits { 0x8086, 0x19d8, 0xffff, 0, "Intel Denverton UART", 0x10 },
14084f94128SKonstantin Belousov { 0x8086, 0x1c3d, 0xffff, 0, "Intel AMT - KT Controller", 0x10 },
1415b8b6b26SJose Luis Duran { 0x8086, 0x1d3d, 0xffff, 0, "Intel C600/X79 Series Chipset KT Controller",
1425b8b6b26SJose Luis Duran 	0x10 },
1430a5c75e5SAlexander Motin { 0x8086, 0x1e3d, 0xffff, 0, "Intel Panther Point KT Controller", 0x10 },
144cb3c7e7fSSean Bruno { 0x8086, 0x228a, 0xffff, 0, "Intel Cherryview SIO HSUART#1", 0x10,
145cb3c7e7fSSean Bruno 	24 * DEFAULT_RCLK, 2 },
146cb3c7e7fSSean Bruno { 0x8086, 0x228c, 0xffff, 0, "Intel Cherryview SIO HSUART#2", 0x10,
147cb3c7e7fSSean Bruno 	24 * DEFAULT_RCLK, 2 },
148aa475e14SSean Bruno { 0x8086, 0x2a07, 0xffff, 0, "Intel AMT - PM965/GM965 KT Controller", 0x10 },
149a1d18b66SJohn Baldwin { 0x8086, 0x2a47, 0xffff, 0, "Mobile 4 Series Chipset KT Controller", 0x10 },
15077b5f5c8SKonstantin Belousov { 0x8086, 0x2e17, 0xffff, 0, "4 Series Chipset Serial KT Controller", 0x10 },
151eaf00819SKonstantin Belousov { 0x8086, 0x31bc, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 0", 0x10,
152eaf00819SKonstantin Belousov 	24 * DEFAULT_RCLK, 2 },
153eaf00819SKonstantin Belousov { 0x8086, 0x31be, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 1", 0x10,
154eaf00819SKonstantin Belousov 	24 * DEFAULT_RCLK, 2 },
155eaf00819SKonstantin Belousov { 0x8086, 0x31c0, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 2", 0x10,
156eaf00819SKonstantin Belousov 	24 * DEFAULT_RCLK, 2 },
157eaf00819SKonstantin Belousov { 0x8086, 0x31ee, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 3", 0x10,
158eaf00819SKonstantin Belousov 	24 * DEFAULT_RCLK, 2 },
159f5793e6fSKonstantin Belousov { 0x8086, 0x3b67, 0xffff, 0, "5 Series/3400 Series Chipset KT Controller",
160f5793e6fSKonstantin Belousov 	0x10 },
1618f156243SJose Luis Duran { 0x8086, 0x5abc, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 0", 0x10,
1628f156243SJose Luis Duran 	24 * DEFAULT_RCLK, 2 },
1638f156243SJose Luis Duran { 0x8086, 0x5abe, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 1", 0x10,
1648f156243SJose Luis Duran 	24 * DEFAULT_RCLK, 2 },
1658f156243SJose Luis Duran { 0x8086, 0x5ac0, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 2", 0x10,
1668f156243SJose Luis Duran 	24 * DEFAULT_RCLK, 2 },
1678f156243SJose Luis Duran { 0x8086, 0x5aee, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 3", 0x10,
1688f156243SJose Luis Duran 	24 * DEFAULT_RCLK, 2 },
1691a74905fSKevin Lo { 0x8086, 0x8811, 0xffff, 0, "Intel EG20T Serial Port 0", 0x10 },
1701a74905fSKevin Lo { 0x8086, 0x8812, 0xffff, 0, "Intel EG20T Serial Port 1", 0x10 },
1711a74905fSKevin Lo { 0x8086, 0x8813, 0xffff, 0, "Intel EG20T Serial Port 2", 0x10 },
1721a74905fSKevin Lo { 0x8086, 0x8814, 0xffff, 0, "Intel EG20T Serial Port 3", 0x10 },
173f478527dSSean Bruno { 0x8086, 0x8c3d, 0xffff, 0, "Intel Lynx Point KT Controller", 0x10 },
174e67f3becSAlexander Motin { 0x8086, 0x8cbd, 0xffff, 0, "Intel Wildcat Point KT Controller", 0x10 },
1754abd2509SSean Bruno { 0x8086, 0x9c3d, 0xffff, 0, "Intel Lynx Point-LP HECI KT", 0x10 },
176fb640be4SSean Bruno { 0x8086, 0xa13d, 0xffff, 0,
177*bf400807SWarner Losh 	"100 Series/C230 Series Chipset Family KT Redirection",
178*bf400807SWarner Losh 	0x10 | PCI_NO_MSI },
1797b4386c6SAlexander Motin { 0x9710, 0x9820, 0x1000, 1, "NetMos NM9820 Serial Port", 0x10 },
180843994aeSJohn Baldwin { 0x9710, 0x9835, 0x1000, 1, "NetMos NM9835 Serial Port", 0x10 },
1811d864e0dSMarcel Moolenaar { 0x9710, 0x9865, 0xa000, 0x1000, "NetMos NM9865 Serial Port", 0x10 },
18224f4fe9aSHans Petter Selasky { 0x9710, 0x9900, 0xa000, 0x1000,
18324f4fe9aSHans Petter Selasky 	"MosChip MCS9900 PCIe to Peripheral Controller", 0x10 },
184506b3c39SXin LI { 0x9710, 0x9901, 0xa000, 0x1000,
185506b3c39SXin LI 	"MosChip MCS9901 PCIe to Peripheral Controller", 0x10 },
186c8e72d0cSRemko Lodder { 0x9710, 0x9904, 0xa000, 0x1000,
187c8e72d0cSRemko Lodder 	"MosChip MCS9904 PCIe to Peripheral Controller", 0x10 },
18839367742SMarius Strobl { 0x9710, 0x9922, 0xa000, 0x1000,
18939367742SMarius Strobl 	"MosChip MCS9922 PCIe to Peripheral Controller", 0x10 },
19024f9da03SMarcel Moolenaar { 0xdeaf, 0x9051, 0xffff, 0, "Middle Digital PC Weasel Serial Port", 0x10 },
19124f9da03SMarcel Moolenaar { 0xffff, 0, 0xffff, 0, NULL, 0, 0}
19227d5dc18SMarcel Moolenaar };
19327d5dc18SMarcel Moolenaar 
194dec28016SMarius Strobl const static struct pci_id *
195dec28016SMarius Strobl uart_pci_match(device_t dev, const struct pci_id *id)
19627d5dc18SMarcel Moolenaar {
19724f9da03SMarcel Moolenaar 	uint16_t device, subdev, subven, vendor;
19827d5dc18SMarcel Moolenaar 
19924f9da03SMarcel Moolenaar 	vendor = pci_get_vendor(dev);
20024f9da03SMarcel Moolenaar 	device = pci_get_device(dev);
20124f9da03SMarcel Moolenaar 	while (id->vendor != 0xffff &&
20224f9da03SMarcel Moolenaar 	    (id->vendor != vendor || id->device != device))
20327d5dc18SMarcel Moolenaar 		id++;
20424f9da03SMarcel Moolenaar 	if (id->vendor == 0xffff)
20524f9da03SMarcel Moolenaar 		return (NULL);
20624f9da03SMarcel Moolenaar 	if (id->subven == 0xffff)
20724f9da03SMarcel Moolenaar 		return (id);
20824f9da03SMarcel Moolenaar 	subven = pci_get_subvendor(dev);
20924f9da03SMarcel Moolenaar 	subdev = pci_get_subdevice(dev);
21024f9da03SMarcel Moolenaar 	while (id->vendor == vendor && id->device == device &&
21124f9da03SMarcel Moolenaar 	    (id->subven != subven || id->subdev != subdev))
21224f9da03SMarcel Moolenaar 		id++;
21324f9da03SMarcel Moolenaar 	return ((id->vendor == vendor && id->device == device) ? id : NULL);
21427d5dc18SMarcel Moolenaar }
21527d5dc18SMarcel Moolenaar 
21627d5dc18SMarcel Moolenaar static int
21727d5dc18SMarcel Moolenaar uart_pci_probe(device_t dev)
21827d5dc18SMarcel Moolenaar {
21927d5dc18SMarcel Moolenaar 	struct uart_softc *sc;
220dec28016SMarius Strobl 	const struct pci_id *id;
221a177309fSMarcel Moolenaar 	int result;
22227d5dc18SMarcel Moolenaar 
22327d5dc18SMarcel Moolenaar 	sc = device_get_softc(dev);
22427d5dc18SMarcel Moolenaar 
22524f9da03SMarcel Moolenaar 	id = uart_pci_match(dev, pci_ns8250_ids);
22627d5dc18SMarcel Moolenaar 	if (id != NULL) {
22727d5dc18SMarcel Moolenaar 		sc->sc_class = &uart_ns8250_class;
22827d5dc18SMarcel Moolenaar 		goto match;
22927d5dc18SMarcel Moolenaar 	}
23027d5dc18SMarcel Moolenaar 	/* Add checks for non-ns8250 IDs here. */
23127d5dc18SMarcel Moolenaar 	return (ENXIO);
23227d5dc18SMarcel Moolenaar 
23327d5dc18SMarcel Moolenaar  match:
234*bf400807SWarner Losh 	result = uart_bus_probe(dev, id->regshft, 0, id->rclk,
235*bf400807SWarner Losh 	    id->rid & PCI_RID_MASK, 0, 0);
236a177309fSMarcel Moolenaar 	/* Bail out on error. */
237a177309fSMarcel Moolenaar 	if (result > 0)
238a177309fSMarcel Moolenaar 		return (result);
239a177309fSMarcel Moolenaar 	/* Set/override the device description. */
24027d5dc18SMarcel Moolenaar 	if (id->desc)
24127d5dc18SMarcel Moolenaar 		device_set_desc(dev, id->desc);
242a177309fSMarcel Moolenaar 	return (result);
24327d5dc18SMarcel Moolenaar }
24427d5dc18SMarcel Moolenaar 
245fb1d9b7fSBruce M Simpson static int
246fb1d9b7fSBruce M Simpson uart_pci_attach(device_t dev)
247fb1d9b7fSBruce M Simpson {
248fb1d9b7fSBruce M Simpson 	struct uart_softc *sc;
249*bf400807SWarner Losh 	const struct pci_id *id;
250fb1d9b7fSBruce M Simpson 	int count;
251fb1d9b7fSBruce M Simpson 
252fb1d9b7fSBruce M Simpson 	sc = device_get_softc(dev);
253fb1d9b7fSBruce M Simpson 
254fb1d9b7fSBruce M Simpson 	/*
255955b6109SWarner Losh 	 * Use MSI in preference to legacy IRQ if available. However, experience
256955b6109SWarner Losh 	 * suggests this is only reliable when one MSI vector is advertised.
257fb1d9b7fSBruce M Simpson 	 */
258*bf400807SWarner Losh 	id = uart_pci_match(dev, pci_ns8250_ids);
259*bf400807SWarner Losh 	if ((id == NULL || (id->rid & PCI_NO_MSI) == 0) &&
260*bf400807SWarner Losh 	    pci_msi_count(dev) == 1) {
261fb1d9b7fSBruce M Simpson 		count = 1;
262fb1d9b7fSBruce M Simpson 		if (pci_alloc_msi(dev, &count) == 0) {
263fb1d9b7fSBruce M Simpson 			sc->sc_irid = 1;
264fb1d9b7fSBruce M Simpson 			device_printf(dev, "Using %d MSI message\n", count);
265fb1d9b7fSBruce M Simpson 		}
266fb1d9b7fSBruce M Simpson 	}
267fb1d9b7fSBruce M Simpson 
268fb1d9b7fSBruce M Simpson 	return (uart_bus_attach(dev));
269fb1d9b7fSBruce M Simpson }
270fb1d9b7fSBruce M Simpson 
271fb1d9b7fSBruce M Simpson static int
272fb1d9b7fSBruce M Simpson uart_pci_detach(device_t dev)
273fb1d9b7fSBruce M Simpson {
274fb1d9b7fSBruce M Simpson 	struct uart_softc *sc;
275fb1d9b7fSBruce M Simpson 
276fb1d9b7fSBruce M Simpson 	sc = device_get_softc(dev);
277fb1d9b7fSBruce M Simpson 
278fb1d9b7fSBruce M Simpson 	if (sc->sc_irid != 0)
279fb1d9b7fSBruce M Simpson 		pci_release_msi(dev);
280fb1d9b7fSBruce M Simpson 
281fb1d9b7fSBruce M Simpson 	return (uart_bus_detach(dev));
282fb1d9b7fSBruce M Simpson }
283fb1d9b7fSBruce M Simpson 
284dec28016SMarius Strobl DRIVER_MODULE(uart, pci, uart_pci_driver, uart_devclass, NULL, NULL);
285