Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / boot / pc32 / libi386 / biospci.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/i386/libi386/biospci.c,v 1.2.2.1 2000/12/28 13:12:39 ps Exp $
27  */
28
29 /*
30  * PnP enumerator using the PCI BIOS.
31  */
32
33 #include <stand.h>
34 #include <machine/stdarg.h>
35 #include <bootstrap.h>
36 #include <isapnp.h>
37 #include <btxv86.h>
38
39 /*
40  * Stupid PCI BIOS interface doesn't let you simply enumerate everything
41  * that's there, instead you have to ask it if it has something.
42  *
43  * So we have to scan by class code, subclass code and sometimes programming
44  * interface.
45  */
46
47 struct pci_progif 
48 {
49     int         pi_code;
50     const char  *pi_name;
51 };
52
53 static struct pci_progif progif_null[] = {
54     {0x0,       NULL},
55     {-1,        NULL}
56 };
57
58 static struct pci_progif progif_display[] = {
59     {0x0,       "VGA"},
60     {0x1,       "8514"},
61     {-1,        NULL}
62 };
63
64 static struct pci_progif progif_ide[] = {
65     {0x00,      NULL},
66     {0x01,      NULL},
67     {0x02,      NULL},
68     {0x03,      NULL},
69     {0x04,      NULL},
70     {0x05,      NULL},
71     {0x06,      NULL},
72     {0x07,      NULL},
73     {0x08,      NULL},
74     {0x09,      NULL},
75     {0x0a,      NULL},
76     {0x0b,      NULL},
77     {0x0c,      NULL},
78     {0x0d,      NULL},
79     {0x0e,      NULL},
80     {0x0f,      NULL},
81     {0x80,      NULL},
82     {0x81,      NULL},
83     {0x82,      NULL},
84     {0x83,      NULL},
85     {0x84,      NULL},
86     {0x85,      NULL},
87     {0x86,      NULL},
88     {0x87,      NULL},
89     {0x88,      NULL},
90     {0x89,      NULL},
91     {0x8a,      NULL},
92     {0x8b,      NULL},
93     {0x8c,      NULL},
94     {0x8d,      NULL},
95     {0x8e,      NULL},
96     {0x8f,      NULL},
97     {-1,        NULL}
98 };
99
100 static struct pci_progif progif_serial[] = {
101     {0x0,       "8250"},
102     {0x1,       "16450"},
103     {0x2,       "16550"},
104     {-1,        NULL}
105 };
106
107 static struct pci_progif progif_parallel[] = {
108     {0x0,       "Standard"},
109     {0x1,       "Bidirectional"},
110     {0x2,       "ECP"},
111     {-1,        NULL}
112 };
113
114
115 struct pci_subclass 
116 {
117     int                 ps_subclass;
118     const char          *ps_name;
119     struct pci_progif   *ps_progif;     /* if set, use for programming interface value(s) */
120 };
121
122 static struct pci_subclass subclass_old[] = {
123     {0x0,       "Old non-VGA",          progif_null},
124     {0x1,       "Old VGA",              progif_null},
125     {-1,        NULL,                   NULL}
126 };
127
128 static struct pci_subclass subclass_mass[] = {
129     {0x0,       "SCSI",                 progif_null},
130     {0x1,       "IDE",                  progif_ide},
131     {0x2,       "Floppy disk",          progif_null},
132     {0x3,       "IPI",                  progif_null},
133     {0x4,       "RAID",                 progif_null},
134     {0x80,      "mass storage",         progif_null},
135     {-1,        NULL,                   NULL}
136 };
137
138 static struct pci_subclass subclass_net[] = {
139     {0x0,       "Ethernet",             progif_null},
140     {0x1,       "Token ring",           progif_null},
141     {0x2,       "FDDI",                 progif_null},
142     {0x3,       "ATM",                  progif_null},
143     {0x80,      "network",              progif_null},
144     {-1,        NULL,                   NULL}
145 };
146
147 static struct pci_subclass subclass_display[] = {
148     {0x0,       NULL,                   progif_display},
149     {0x1,       "XGA",                  progif_null},
150     {0x80,      "other",                progif_null},
151     {-1,        NULL,                   NULL}
152 };
153
154 static struct pci_subclass subclass_comms[] = {
155     {0x0,       "serial",               progif_serial},
156     {0x1,       "parallel",             progif_parallel},
157     {0x80,      "communications",       progif_null},
158     {-1,        NULL,                   NULL}
159 };
160
161 static struct pci_subclass subclass_serial[] = {
162     {0x0,       "Firewire",             progif_null},
163     {0x1,       "ACCESS.bus",           progif_null},
164     {0x2,       "SSA",                  progif_null},
165     {0x3,       "USB",                  progif_null},
166     {0x4,       "Fibrechannel",         progif_null},
167     {-1,        NULL,                   NULL}
168 };
169
170 static struct pci_class
171 {
172     int                 pc_class;
173     const char          *pc_name;
174     struct pci_subclass *pc_subclass;
175 } pci_classes[] = {
176     {0x0,       "device",       subclass_old},
177     {0x1,       "controller",   subclass_mass},
178     {0x2,       "controller",   subclass_net},
179     {0x3,       "display",      subclass_display},
180     {0x7,       "controller",   subclass_comms},
181     {0xc,       "controller",   subclass_serial},
182     {-1,        NULL,           NULL}
183 };
184
185
186 static void     biospci_enumerate(void);
187 static void     biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi);
188
189 static int      biospci_version;
190 static int      biospci_hwcap;
191
192 struct pnphandler biospcihandler =
193 {
194     "PCI BIOS",
195     biospci_enumerate
196 };
197
198 static void
199 biospci_enumerate(void)
200 {
201     int                 device_index, locator, devid;
202     struct pci_class    *pc;
203     struct pci_subclass *psc;
204     struct pci_progif   *ppi;
205
206     /* Find the PCI BIOS */
207     v86.ctl = V86_FLAGS;
208     v86.addr = 0x1a;
209     v86.eax = 0xb101;
210     v86.edi = 0x0;
211     v86int();
212
213     /* Check for OK response */
214     if ((v86.efl & 1) || ((v86.eax & 0xff00) != 0) || (v86.edx != 0x20494350))
215         return;
216
217     biospci_version = v86.ebx & 0xffff;
218     biospci_hwcap = v86.eax & 0xff;
219 #if 0
220     printf("PCI BIOS %d.%d%s%s\n", 
221            bcd2bin((biospci_version >> 8) & 0xf), bcd2bin(biospci_version & 0xf),
222            (biospci_hwcap & 1) ? " config1" : "", (biospci_hwcap & 2) ? " config2" : "");
223 #endif
224     /* Iterate over known classes */
225     for (pc = pci_classes; pc->pc_class >= 0; pc++) {
226         /* Iterate over subclasses */
227         for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
228             /* Iterate over programming interfaces */
229             for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
230
231                 /* Scan for matches */
232                 for (device_index = 0; ; device_index++) {
233
234                     /* Look for a match */
235                     v86.ctl = V86_FLAGS;
236                     v86.addr = 0x1a;
237                     v86.eax = 0xb103;
238                     v86.ecx = (pc->pc_class << 16) + (psc->ps_subclass << 8) + ppi->pi_code;
239                     v86.esi = device_index;
240                     v86int();
241                     /* error/end of matches */
242                     if ((v86.efl & 1) || (v86.eax & 0xff00))
243                         break;
244
245                     /* Got something */
246                     locator = v86.ebx;
247                     
248                     /* Read the device identifier from the nominated device */
249                     v86.ctl = V86_FLAGS;
250                     v86.addr = 0x1a;
251                     v86.eax = 0xb10a;
252                     v86.ebx = locator;
253                     v86.edi = 0x0;
254                     v86int();
255                     /* error */
256                     if ((v86.efl & 1) || (v86.eax & 0xff00))
257                         break;
258                     
259                     /* We have the device ID, create a PnP object and save everything */
260                     devid = v86.ecx;
261                     biospci_addinfo(devid, pc, psc, ppi);
262                 }
263             }
264         }
265     }
266 }
267
268 static void
269 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi) 
270 {
271     struct pnpinfo      *pi;
272     char                desc[80];
273     
274     
275     /* build the description */
276     desc[0] = 0;
277     if (ppi->pi_name != NULL) {
278         strcat(desc, ppi->pi_name);
279         strcat(desc, " ");
280     }
281     if (psc->ps_name != NULL) {
282         strcat(desc, psc->ps_name);
283         strcat(desc, " ");
284     }
285     if (pc->pc_name != NULL)
286         strcat(desc, pc->pc_name);
287
288     pi = pnp_allocinfo();
289     pi->pi_desc = strdup(desc);
290     sprintf(desc,"0x%08x", devid);
291     pnp_addident(pi, desc);
292     pnp_addinfo(pi);
293 }