1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
2927d5dc18SMarcel Moolenaar #include <sys/systm.h>
3027d5dc18SMarcel Moolenaar #include <sys/bus.h>
3127d5dc18SMarcel Moolenaar #include <sys/conf.h>
3227d5dc18SMarcel Moolenaar #include <sys/kernel.h>
3327d5dc18SMarcel Moolenaar #include <sys/module.h>
3427d5dc18SMarcel Moolenaar #include <machine/bus.h>
3527d5dc18SMarcel Moolenaar #include <sys/rman.h>
3627d5dc18SMarcel Moolenaar #include <machine/resource.h>
3727d5dc18SMarcel Moolenaar
3827d5dc18SMarcel Moolenaar #include <dev/pci/pcivar.h>
399eb5fd35SWarner Losh #include <dev/pci/pcireg.h>
4027d5dc18SMarcel Moolenaar
4127d5dc18SMarcel Moolenaar #include <dev/uart/uart.h>
4227d5dc18SMarcel Moolenaar #include <dev/uart/uart_bus.h>
43ad93649dSColin Percival #include <dev/uart/uart_cpu.h>
4427d5dc18SMarcel Moolenaar
4524f9da03SMarcel Moolenaar #define DEFAULT_RCLK 1843200
4624f9da03SMarcel Moolenaar
4727d5dc18SMarcel Moolenaar static int uart_pci_probe(device_t dev);
48fb1d9b7fSBruce M Simpson static int uart_pci_attach(device_t dev);
49fb1d9b7fSBruce M Simpson static int uart_pci_detach(device_t dev);
5027d5dc18SMarcel Moolenaar
5127d5dc18SMarcel Moolenaar static device_method_t uart_pci_methods[] = {
5227d5dc18SMarcel Moolenaar /* Device interface */
5327d5dc18SMarcel Moolenaar DEVMETHOD(device_probe, uart_pci_probe),
54fb1d9b7fSBruce M Simpson DEVMETHOD(device_attach, uart_pci_attach),
55fb1d9b7fSBruce M Simpson DEVMETHOD(device_detach, uart_pci_detach),
565b23b1b9SAndriy Gapon DEVMETHOD(device_resume, uart_bus_resume),
57dec28016SMarius Strobl DEVMETHOD_END
5827d5dc18SMarcel Moolenaar };
5927d5dc18SMarcel Moolenaar
6027d5dc18SMarcel Moolenaar static driver_t uart_pci_driver = {
6127d5dc18SMarcel Moolenaar uart_driver_name,
6227d5dc18SMarcel Moolenaar uart_pci_methods,
6327d5dc18SMarcel Moolenaar sizeof(struct uart_softc),
6427d5dc18SMarcel Moolenaar };
6527d5dc18SMarcel Moolenaar
6627d5dc18SMarcel Moolenaar struct pci_id {
6724f9da03SMarcel Moolenaar uint16_t vendor;
6824f9da03SMarcel Moolenaar uint16_t device;
6924f9da03SMarcel Moolenaar uint16_t subven;
7024f9da03SMarcel Moolenaar uint16_t subdev;
7127d5dc18SMarcel Moolenaar const char *desc;
7227d5dc18SMarcel Moolenaar int rid;
7324f9da03SMarcel Moolenaar int rclk;
749308005eSMarcel Moolenaar int regshft;
7527d5dc18SMarcel Moolenaar };
7627d5dc18SMarcel Moolenaar
77ad93649dSColin Percival struct pci_unique_id {
78ad93649dSColin Percival uint16_t vendor;
79ad93649dSColin Percival uint16_t device;
80ad93649dSColin Percival };
81ad93649dSColin Percival
82bf400807SWarner Losh #define PCI_NO_MSI 0x40000000
83bf400807SWarner Losh #define PCI_RID_MASK 0x0000ffff
84bf400807SWarner Losh
85dec28016SMarius Strobl static const struct pci_id pci_ns8250_ids[] = {
86ab2b8832SMarcel Moolenaar { 0x1028, 0x0008, 0xffff, 0, "Dell Remote Access Card III", 0x14,
87ab2b8832SMarcel Moolenaar 128 * DEFAULT_RCLK },
88ab2b8832SMarcel Moolenaar { 0x1028, 0x0012, 0xffff, 0, "Dell RAC 4 Daughter Card Virtual UART", 0x14,
89ab2b8832SMarcel Moolenaar 128 * DEFAULT_RCLK },
9024f9da03SMarcel Moolenaar { 0x1033, 0x0074, 0x1033, 0x8014, "NEC RCV56ACF 56k Voice Modem", 0x10 },
9124f9da03SMarcel Moolenaar { 0x1033, 0x007d, 0x1033, 0x8012, "NEC RS232C", 0x10 },
92ab2b8832SMarcel Moolenaar { 0x103c, 0x1048, 0x103c, 0x1227, "HP Diva Serial [GSP] UART - Powerbar SP2",
93ab2b8832SMarcel Moolenaar 0x10 },
947db977fbSMarcel Moolenaar { 0x103c, 0x1048, 0x103c, 0x1301, "HP Diva RMP3", 0x14 },
95ab2b8832SMarcel Moolenaar { 0x103c, 0x1290, 0xffff, 0, "HP Auxiliary Diva Serial Port", 0x18 },
96b1a64a96SMarcel Moolenaar { 0x103c, 0x3301, 0xffff, 0, "HP iLO serial port", 0x10 },
9724f9da03SMarcel Moolenaar { 0x11c1, 0x0480, 0xffff, 0, "Agere Systems Venus Modem (V90, 56KFlex)", 0x14 },
9824f9da03SMarcel Moolenaar { 0x115d, 0x0103, 0xffff, 0, "Xircom Cardbus Ethernet + 56k Modem", 0x10 },
99059178b6SEd Maste { 0x125b, 0x9100, 0xa000, 0x1000,
100059178b6SEd Maste "ASIX AX99100 PCIe 1/2/3/4-port RS-232/422/485", 0x10 },
1012f8c04adSJohn Baldwin { 0x1282, 0x6585, 0xffff, 0, "Davicom 56PDV PCI Modem", 0x10 },
10224f9da03SMarcel Moolenaar { 0x12b9, 0x1008, 0xffff, 0, "3Com 56K FaxModem Model 5610", 0x10 },
10324f9da03SMarcel Moolenaar { 0x131f, 0x1000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x18 },
10424f9da03SMarcel Moolenaar { 0x131f, 0x1001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x18 },
10524f9da03SMarcel Moolenaar { 0x131f, 0x1002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x18 },
10624f9da03SMarcel Moolenaar { 0x131f, 0x2000, 0xffff, 0, "Siig CyberSerial (1-port) 16550", 0x10 },
10724f9da03SMarcel Moolenaar { 0x131f, 0x2001, 0xffff, 0, "Siig CyberSerial (1-port) 16650", 0x10 },
10824f9da03SMarcel Moolenaar { 0x131f, 0x2002, 0xffff, 0, "Siig CyberSerial (1-port) 16850", 0x10 },
109*41b30bbcSYoshihiro Takahashi { 0x135a, 0x0a61, 0xffff, 0, "Brainboxes UC-324", 0x18 },
110*41b30bbcSYoshihiro Takahashi { 0x135a, 0x0aa1, 0xffff, 0, "Brainboxes UC-246", 0x18 },
111*41b30bbcSYoshihiro Takahashi { 0x135a, 0x0aa2, 0xffff, 0, "Brainboxes UC-246", 0x18 },
112*41b30bbcSYoshihiro Takahashi { 0x135a, 0x0d60, 0xffff, 0, "Intashield IS-100", 0x18 },
113*41b30bbcSYoshihiro Takahashi { 0x135a, 0x0da0, 0xffff, 0, "Intashield IS-300", 0x18 },
114*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4000, 0xffff, 0, "Brainboxes PX-420", 0x10 },
115*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4001, 0xffff, 0, "Brainboxes PX-431", 0x10 },
116*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4002, 0xffff, 0, "Brainboxes PX-820", 0x10 },
117*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4003, 0xffff, 0, "Brainboxes PX-831", 0x10 },
118*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4004, 0xffff, 0, "Brainboxes PX-246", 0x10 },
119*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4005, 0xffff, 0, "Brainboxes PX-101", 0x10 },
120*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4006, 0xffff, 0, "Brainboxes PX-257", 0x10 },
121*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4008, 0xffff, 0, "Brainboxes PX-846", 0x10 },
122*41b30bbcSYoshihiro Takahashi { 0x135a, 0x4009, 0xffff, 0, "Brainboxes PX-857", 0x10 },
12324f9da03SMarcel Moolenaar { 0x135c, 0x0190, 0xffff, 0, "Quatech SSCLP-100", 0x18 },
12424f9da03SMarcel Moolenaar { 0x135c, 0x01c0, 0xffff, 0, "Quatech SSCLP-200/300", 0x18 },
125ab2b8832SMarcel Moolenaar { 0x135e, 0x7101, 0xffff, 0, "Sealevel Systems Single Port RS-232/422/485/530",
126ab2b8832SMarcel Moolenaar 0x18 },
12724f9da03SMarcel Moolenaar { 0x1407, 0x0110, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port A", 0x10 },
12824f9da03SMarcel Moolenaar { 0x1407, 0x0111, 0xffff, 0, "Lava Computer mfg DSerial-PCI Port B", 0x10 },
12964fc5491SKai Wang { 0x1407, 0x0510, 0xffff, 0, "Lava SP Serial 550 PCI", 0x10 },
130ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4025, "Timedia Technology Serial Port", 0x10,
131ab2b8832SMarcel Moolenaar 8 * DEFAULT_RCLK },
132ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4027, "Timedia Technology Serial Port", 0x10,
133ab2b8832SMarcel Moolenaar 8 * DEFAULT_RCLK },
134ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x4028, "Timedia Technology Serial Port", 0x10,
135ab2b8832SMarcel Moolenaar 8 * DEFAULT_RCLK },
136ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x5025, "Timedia Technology Serial Port", 0x10,
137ab2b8832SMarcel Moolenaar 8 * DEFAULT_RCLK },
138ab2b8832SMarcel Moolenaar { 0x1409, 0x7168, 0x1409, 0x5027, "Timedia Technology Serial Port", 0x10,
139ab2b8832SMarcel Moolenaar 8 * DEFAULT_RCLK },
140ab2b8832SMarcel Moolenaar { 0x1415, 0x950b, 0xffff, 0, "Oxford Semiconductor OXCB950 Cardbus 16950 UART",
141ab2b8832SMarcel Moolenaar 0x10, 16384000 },
1427e47312cSPeter Grehan { 0x1415, 0xc120, 0xffff, 0, "Oxford Semiconductor OXPCIe952 PCIe 16950 UART",
1437e47312cSPeter Grehan 0x10 },
1447bd6f3d0SMarcel Moolenaar { 0x14e4, 0x160a, 0xffff, 0, "Broadcom TruManage UART", 0x10,
1457bd6f3d0SMarcel Moolenaar 128 * DEFAULT_RCLK, 2},
146383227f1SEitan Adler { 0x14e4, 0x4344, 0xffff, 0, "Sony Ericsson GC89 PC Card", 0x10},
147ab2b8832SMarcel Moolenaar { 0x151f, 0x0000, 0xffff, 0, "TOPIC Semiconductor TP560 56k modem", 0x10 },
1481bfa40d2SColin Percival { 0x1d0f, 0x8250, 0x0000, 0, "Amazon PCI serial device", 0x10 },
149189bd7a9SColin Percival { 0x1d0f, 0x8250, 0x1d0f, 0, "Amazon PCI serial device", 0x10 },
15050c0e894SMarius Strobl { 0x1fd4, 0x1999, 0x1fd4, 0x0001, "Sunix SER5xxxx Serial Port", 0x10,
15150c0e894SMarius Strobl 8 * DEFAULT_RCLK },
1521f2776e1SHenrich Hartzer { 0x8086, 0x0c5f, 0xffff, 0, "Atom Processor S1200 UART",
1531f2776e1SHenrich Hartzer 0x10 | PCI_NO_MSI },
1549308005eSMarcel Moolenaar { 0x8086, 0x0f0a, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#1", 0x10,
1559308005eSMarcel Moolenaar 24 * DEFAULT_RCLK, 2 },
1569308005eSMarcel Moolenaar { 0x8086, 0x0f0c, 0xffff, 0, "Intel ValleyView LPIO1 HSUART#2", 0x10,
1579308005eSMarcel Moolenaar 24 * DEFAULT_RCLK, 2 },
158ef7161e7SEd Maste { 0x8086, 0x108f, 0xffff, 0, "Intel AMT - SOL", 0x10 },
159f37cc713SJustin Hibbits { 0x8086, 0x19d8, 0xffff, 0, "Intel Denverton UART", 0x10 },
16084f94128SKonstantin Belousov { 0x8086, 0x1c3d, 0xffff, 0, "Intel AMT - KT Controller", 0x10 },
1615b8b6b26SJose Luis Duran { 0x8086, 0x1d3d, 0xffff, 0, "Intel C600/X79 Series Chipset KT Controller",
1625b8b6b26SJose Luis Duran 0x10 },
1630a5c75e5SAlexander Motin { 0x8086, 0x1e3d, 0xffff, 0, "Intel Panther Point KT Controller", 0x10 },
164cb3c7e7fSSean Bruno { 0x8086, 0x228a, 0xffff, 0, "Intel Cherryview SIO HSUART#1", 0x10,
165cb3c7e7fSSean Bruno 24 * DEFAULT_RCLK, 2 },
166cb3c7e7fSSean Bruno { 0x8086, 0x228c, 0xffff, 0, "Intel Cherryview SIO HSUART#2", 0x10,
167cb3c7e7fSSean Bruno 24 * DEFAULT_RCLK, 2 },
168aa475e14SSean Bruno { 0x8086, 0x2a07, 0xffff, 0, "Intel AMT - PM965/GM965 KT Controller", 0x10 },
169a1d18b66SJohn Baldwin { 0x8086, 0x2a47, 0xffff, 0, "Mobile 4 Series Chipset KT Controller", 0x10 },
17077b5f5c8SKonstantin Belousov { 0x8086, 0x2e17, 0xffff, 0, "4 Series Chipset Serial KT Controller", 0x10 },
171eaf00819SKonstantin Belousov { 0x8086, 0x31bc, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 0", 0x10,
172eaf00819SKonstantin Belousov 24 * DEFAULT_RCLK, 2 },
173eaf00819SKonstantin Belousov { 0x8086, 0x31be, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 1", 0x10,
174eaf00819SKonstantin Belousov 24 * DEFAULT_RCLK, 2 },
175eaf00819SKonstantin Belousov { 0x8086, 0x31c0, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 2", 0x10,
176eaf00819SKonstantin Belousov 24 * DEFAULT_RCLK, 2 },
177eaf00819SKonstantin Belousov { 0x8086, 0x31ee, 0xffff, 0, "Intel Gemini Lake SIO/LPSS UART 3", 0x10,
178eaf00819SKonstantin Belousov 24 * DEFAULT_RCLK, 2 },
179f5793e6fSKonstantin Belousov { 0x8086, 0x3b67, 0xffff, 0, "5 Series/3400 Series Chipset KT Controller",
180f5793e6fSKonstantin Belousov 0x10 },
1818f156243SJose Luis Duran { 0x8086, 0x5abc, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 0", 0x10,
1828f156243SJose Luis Duran 24 * DEFAULT_RCLK, 2 },
1838f156243SJose Luis Duran { 0x8086, 0x5abe, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 1", 0x10,
1848f156243SJose Luis Duran 24 * DEFAULT_RCLK, 2 },
1858f156243SJose Luis Duran { 0x8086, 0x5ac0, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 2", 0x10,
1868f156243SJose Luis Duran 24 * DEFAULT_RCLK, 2 },
1878f156243SJose Luis Duran { 0x8086, 0x5aee, 0xffff, 0, "Intel Apollo Lake SIO/LPSS UART 3", 0x10,
1888f156243SJose Luis Duran 24 * DEFAULT_RCLK, 2 },
1891a74905fSKevin Lo { 0x8086, 0x8811, 0xffff, 0, "Intel EG20T Serial Port 0", 0x10 },
1901a74905fSKevin Lo { 0x8086, 0x8812, 0xffff, 0, "Intel EG20T Serial Port 1", 0x10 },
1911a74905fSKevin Lo { 0x8086, 0x8813, 0xffff, 0, "Intel EG20T Serial Port 2", 0x10 },
1921a74905fSKevin Lo { 0x8086, 0x8814, 0xffff, 0, "Intel EG20T Serial Port 3", 0x10 },
193f478527dSSean Bruno { 0x8086, 0x8c3d, 0xffff, 0, "Intel Lynx Point KT Controller", 0x10 },
194e67f3becSAlexander Motin { 0x8086, 0x8cbd, 0xffff, 0, "Intel Wildcat Point KT Controller", 0x10 },
19568ca3a15SStuart McLaren { 0x8086, 0x8d3d, 0xffff, 0,
19668ca3a15SStuart McLaren "Intel Corporation C610/X99 series chipset KT Controller", 0x10 },
1974abd2509SSean Bruno { 0x8086, 0x9c3d, 0xffff, 0, "Intel Lynx Point-LP HECI KT", 0x10 },
198fb640be4SSean Bruno { 0x8086, 0xa13d, 0xffff, 0,
199bf400807SWarner Losh "100 Series/C230 Series Chipset Family KT Redirection",
200bf400807SWarner Losh 0x10 | PCI_NO_MSI },
2017b4386c6SAlexander Motin { 0x9710, 0x9820, 0x1000, 1, "NetMos NM9820 Serial Port", 0x10 },
202843994aeSJohn Baldwin { 0x9710, 0x9835, 0x1000, 1, "NetMos NM9835 Serial Port", 0x10 },
2031d864e0dSMarcel Moolenaar { 0x9710, 0x9865, 0xa000, 0x1000, "NetMos NM9865 Serial Port", 0x10 },
20424f4fe9aSHans Petter Selasky { 0x9710, 0x9900, 0xa000, 0x1000,
20524f4fe9aSHans Petter Selasky "MosChip MCS9900 PCIe to Peripheral Controller", 0x10 },
206506b3c39SXin LI { 0x9710, 0x9901, 0xa000, 0x1000,
207506b3c39SXin LI "MosChip MCS9901 PCIe to Peripheral Controller", 0x10 },
208c8e72d0cSRemko Lodder { 0x9710, 0x9904, 0xa000, 0x1000,
209c8e72d0cSRemko Lodder "MosChip MCS9904 PCIe to Peripheral Controller", 0x10 },
21039367742SMarius Strobl { 0x9710, 0x9922, 0xa000, 0x1000,
21139367742SMarius Strobl "MosChip MCS9922 PCIe to Peripheral Controller", 0x10 },
21224f9da03SMarcel Moolenaar { 0xdeaf, 0x9051, 0xffff, 0, "Middle Digital PC Weasel Serial Port", 0x10 },
21324f9da03SMarcel Moolenaar { 0xffff, 0, 0xffff, 0, NULL, 0, 0}
21427d5dc18SMarcel Moolenaar };
21527d5dc18SMarcel Moolenaar
216dec28016SMarius Strobl const static struct pci_id *
uart_pci_match(device_t dev,const struct pci_id * id)217dec28016SMarius Strobl uart_pci_match(device_t dev, const struct pci_id *id)
21827d5dc18SMarcel Moolenaar {
21924f9da03SMarcel Moolenaar uint16_t device, subdev, subven, vendor;
22027d5dc18SMarcel Moolenaar
22124f9da03SMarcel Moolenaar vendor = pci_get_vendor(dev);
22224f9da03SMarcel Moolenaar device = pci_get_device(dev);
22324f9da03SMarcel Moolenaar while (id->vendor != 0xffff &&
22424f9da03SMarcel Moolenaar (id->vendor != vendor || id->device != device))
22527d5dc18SMarcel Moolenaar id++;
22624f9da03SMarcel Moolenaar if (id->vendor == 0xffff)
22724f9da03SMarcel Moolenaar return (NULL);
22824f9da03SMarcel Moolenaar if (id->subven == 0xffff)
22924f9da03SMarcel Moolenaar return (id);
23024f9da03SMarcel Moolenaar subven = pci_get_subvendor(dev);
23124f9da03SMarcel Moolenaar subdev = pci_get_subdevice(dev);
23224f9da03SMarcel Moolenaar while (id->vendor == vendor && id->device == device &&
23324f9da03SMarcel Moolenaar (id->subven != subven || id->subdev != subdev))
23424f9da03SMarcel Moolenaar id++;
23524f9da03SMarcel Moolenaar return ((id->vendor == vendor && id->device == device) ? id : NULL);
23627d5dc18SMarcel Moolenaar }
23727d5dc18SMarcel Moolenaar
238ad93649dSColin Percival extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
239ad93649dSColin Percival
240ad93649dSColin Percival /* PCI vendor/device pairs of devices guaranteed to be unique on a system. */
241ad93649dSColin Percival static const struct pci_unique_id pci_unique_devices[] = {
242ad93649dSColin Percival { 0x1d0f, 0x8250 } /* Amazon PCI serial device */
243ad93649dSColin Percival };
244ad93649dSColin Percival
245ad93649dSColin Percival /* Match a UART to a console if it's a PCI device known to be unique. */
246ad93649dSColin Percival static void
uart_pci_unique_console_match(device_t dev)247ad93649dSColin Percival uart_pci_unique_console_match(device_t dev)
248ad93649dSColin Percival {
249ad93649dSColin Percival struct uart_softc *sc;
250ad93649dSColin Percival struct uart_devinfo * sysdev;
251ad93649dSColin Percival const struct pci_unique_id * id;
252ad93649dSColin Percival uint16_t vendor, device;
253ad93649dSColin Percival
254ad93649dSColin Percival sc = device_get_softc(dev);
255ad93649dSColin Percival vendor = pci_get_vendor(dev);
256ad93649dSColin Percival device = pci_get_device(dev);
257ad93649dSColin Percival
258ad93649dSColin Percival /* Is this a device known to exist only once in a system? */
259ad93649dSColin Percival for (id = pci_unique_devices; ; id++) {
260ad93649dSColin Percival if (id == &pci_unique_devices[nitems(pci_unique_devices)])
261ad93649dSColin Percival return;
262ad93649dSColin Percival if (id->vendor == vendor && id->device == device)
263ad93649dSColin Percival break;
264ad93649dSColin Percival }
265ad93649dSColin Percival
266ad93649dSColin Percival /* If it matches a console, it must be the same device. */
267ad93649dSColin Percival SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
268ad93649dSColin Percival if (sysdev->pci_info.vendor == vendor &&
269ad93649dSColin Percival sysdev->pci_info.device == device) {
270ad93649dSColin Percival sc->sc_sysdev = sysdev;
271ad93649dSColin Percival sysdev->bas.rclk = sc->sc_bas.rclk;
272ad93649dSColin Percival }
273ad93649dSColin Percival }
274ad93649dSColin Percival }
275ad93649dSColin Percival
27627d5dc18SMarcel Moolenaar static int
uart_pci_probe(device_t dev)27727d5dc18SMarcel Moolenaar uart_pci_probe(device_t dev)
27827d5dc18SMarcel Moolenaar {
27927d5dc18SMarcel Moolenaar struct uart_softc *sc;
280dec28016SMarius Strobl const struct pci_id *id;
2819eb5fd35SWarner Losh struct pci_id cid = {
2829eb5fd35SWarner Losh .regshft = 0,
2839eb5fd35SWarner Losh .rclk = 0,
2849eb5fd35SWarner Losh .rid = 0x10 | PCI_NO_MSI,
2859eb5fd35SWarner Losh .desc = "Generic SimpleComm PCI device",
2869eb5fd35SWarner Losh };
287a177309fSMarcel Moolenaar int result;
28827d5dc18SMarcel Moolenaar
28927d5dc18SMarcel Moolenaar sc = device_get_softc(dev);
29027d5dc18SMarcel Moolenaar
29124f9da03SMarcel Moolenaar id = uart_pci_match(dev, pci_ns8250_ids);
29227d5dc18SMarcel Moolenaar if (id != NULL) {
29327d5dc18SMarcel Moolenaar sc->sc_class = &uart_ns8250_class;
29427d5dc18SMarcel Moolenaar goto match;
29527d5dc18SMarcel Moolenaar }
2969eb5fd35SWarner Losh if (pci_get_class(dev) == PCIC_SIMPLECOMM &&
2979eb5fd35SWarner Losh pci_get_subclass(dev) == PCIS_SIMPLECOMM_UART &&
2989eb5fd35SWarner Losh pci_get_progif(dev) < PCIP_SIMPLECOMM_UART_16550A) {
2999eb5fd35SWarner Losh /* XXX rclk what to do */
3009eb5fd35SWarner Losh id = &cid;
3019eb5fd35SWarner Losh sc->sc_class = &uart_ns8250_class;
3029eb5fd35SWarner Losh goto match;
3039eb5fd35SWarner Losh }
30427d5dc18SMarcel Moolenaar /* Add checks for non-ns8250 IDs here. */
30527d5dc18SMarcel Moolenaar return (ENXIO);
30627d5dc18SMarcel Moolenaar
30727d5dc18SMarcel Moolenaar match:
308bf400807SWarner Losh result = uart_bus_probe(dev, id->regshft, 0, id->rclk,
309bf400807SWarner Losh id->rid & PCI_RID_MASK, 0, 0);
310a177309fSMarcel Moolenaar /* Bail out on error. */
311a177309fSMarcel Moolenaar if (result > 0)
312a177309fSMarcel Moolenaar return (result);
313ad93649dSColin Percival /*
314ad93649dSColin Percival * If we haven't already matched this to a console, check if it's a
315ad93649dSColin Percival * PCI device which is known to only exist once in any given system
316ad93649dSColin Percival * and we can match it that way.
317ad93649dSColin Percival */
318ad93649dSColin Percival if (sc->sc_sysdev == NULL)
319ad93649dSColin Percival uart_pci_unique_console_match(dev);
320a177309fSMarcel Moolenaar /* Set/override the device description. */
32127d5dc18SMarcel Moolenaar if (id->desc)
32227d5dc18SMarcel Moolenaar device_set_desc(dev, id->desc);
323a177309fSMarcel Moolenaar return (result);
32427d5dc18SMarcel Moolenaar }
32527d5dc18SMarcel Moolenaar
326fb1d9b7fSBruce M Simpson static int
uart_pci_attach(device_t dev)327fb1d9b7fSBruce M Simpson uart_pci_attach(device_t dev)
328fb1d9b7fSBruce M Simpson {
329fb1d9b7fSBruce M Simpson struct uart_softc *sc;
330bf400807SWarner Losh const struct pci_id *id;
331fb1d9b7fSBruce M Simpson int count;
332fb1d9b7fSBruce M Simpson
333fb1d9b7fSBruce M Simpson sc = device_get_softc(dev);
334fb1d9b7fSBruce M Simpson
335fb1d9b7fSBruce M Simpson /*
336955b6109SWarner Losh * Use MSI in preference to legacy IRQ if available. However, experience
337955b6109SWarner Losh * suggests this is only reliable when one MSI vector is advertised.
338fb1d9b7fSBruce M Simpson */
339bf400807SWarner Losh id = uart_pci_match(dev, pci_ns8250_ids);
340bf400807SWarner Losh if ((id == NULL || (id->rid & PCI_NO_MSI) == 0) &&
341bf400807SWarner Losh pci_msi_count(dev) == 1) {
342fb1d9b7fSBruce M Simpson count = 1;
343fb1d9b7fSBruce M Simpson if (pci_alloc_msi(dev, &count) == 0) {
344fb1d9b7fSBruce M Simpson sc->sc_irid = 1;
345fb1d9b7fSBruce M Simpson device_printf(dev, "Using %d MSI message\n", count);
346fb1d9b7fSBruce M Simpson }
347fb1d9b7fSBruce M Simpson }
348fb1d9b7fSBruce M Simpson
349fb1d9b7fSBruce M Simpson return (uart_bus_attach(dev));
350fb1d9b7fSBruce M Simpson }
351fb1d9b7fSBruce M Simpson
352fb1d9b7fSBruce M Simpson static int
uart_pci_detach(device_t dev)353fb1d9b7fSBruce M Simpson uart_pci_detach(device_t dev)
354fb1d9b7fSBruce M Simpson {
355fb1d9b7fSBruce M Simpson struct uart_softc *sc;
356fb1d9b7fSBruce M Simpson
357fb1d9b7fSBruce M Simpson sc = device_get_softc(dev);
358fb1d9b7fSBruce M Simpson
359fb1d9b7fSBruce M Simpson if (sc->sc_irid != 0)
360fb1d9b7fSBruce M Simpson pci_release_msi(dev);
361fb1d9b7fSBruce M Simpson
362fb1d9b7fSBruce M Simpson return (uart_bus_detach(dev));
363fb1d9b7fSBruce M Simpson }
364fb1d9b7fSBruce M Simpson
365c90ea831SJohn Baldwin DRIVER_MODULE(uart, pci, uart_pci_driver, NULL, NULL);
366