pci_subr.c revision 1.11 1 1.11 cgd /* $NetBSD: pci_subr.c,v 1.11 1995/06/21 03:56:09 cgd Exp $ */
2 1.3 cgd
3 1.1 mycroft /*
4 1.10 cgd * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
5 1.1 mycroft * Copyright (c) 1994 Charles Hannum. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by Charles Hannum.
18 1.1 mycroft * 4. The name of the author may not be used to endorse or promote products
19 1.1 mycroft * derived from this software without specific prior written permission.
20 1.1 mycroft *
21 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mycroft */
32 1.1 mycroft
33 1.1 mycroft /*
34 1.10 cgd * PCI autoconfiguration support functions.
35 1.1 mycroft */
36 1.1 mycroft
37 1.1 mycroft #include <sys/param.h>
38 1.10 cgd #include <sys/systm.h>
39 1.1 mycroft #include <sys/device.h>
40 1.1 mycroft
41 1.10 cgd #include <dev/pci/pcireg.h>
42 1.7 cgd #include <dev/pci/pcivar.h>
43 1.10 cgd #ifdef PCIVERBOSE
44 1.10 cgd #include <dev/pci/pcidevs.h>
45 1.10 cgd #endif
46 1.1 mycroft
47 1.1 mycroft int
48 1.1 mycroft pciprint(aux, pci)
49 1.1 mycroft void *aux;
50 1.1 mycroft char *pci;
51 1.1 mycroft {
52 1.1 mycroft register struct pci_attach_args *pa = aux;
53 1.1 mycroft
54 1.6 mycroft printf(" bus %d device %d", pa->pa_bus, pa->pa_device);
55 1.6 mycroft return (UNCONF);
56 1.6 mycroft }
57 1.6 mycroft
58 1.6 mycroft int
59 1.6 mycroft pcisubmatch(parent, match, aux)
60 1.6 mycroft struct device *parent;
61 1.6 mycroft void *match, *aux;
62 1.6 mycroft {
63 1.6 mycroft struct cfdata *cf = match;
64 1.6 mycroft struct pci_attach_args *pa = aux;
65 1.6 mycroft
66 1.6 mycroft if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pa->pa_bus)
67 1.6 mycroft return 0;
68 1.6 mycroft if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != pa->pa_device)
69 1.6 mycroft return 0;
70 1.6 mycroft return ((*cf->cf_driver->cd_match)(parent, match, aux));
71 1.1 mycroft }
72 1.1 mycroft
73 1.8 cgd /*
74 1.8 cgd * Try to find and attach the PCI device at the give bus and device number.
75 1.8 cgd * Return 1 if successful, 0 if unsuccessful.
76 1.8 cgd */
77 1.8 cgd int
78 1.8 cgd pci_attach_subdev(pcidev, bus, device)
79 1.10 cgd struct device *pcidev;
80 1.10 cgd int bus, device;
81 1.1 mycroft {
82 1.8 cgd pcitag_t tag;
83 1.8 cgd pcireg_t id, class;
84 1.8 cgd struct pci_attach_args pa;
85 1.8 cgd struct cfdata *cf;
86 1.10 cgd int supported;
87 1.10 cgd char devinfo[256];
88 1.8 cgd
89 1.8 cgd tag = pci_make_tag(bus, device, 0);
90 1.8 cgd id = pci_conf_read(tag, PCI_ID_REG);
91 1.8 cgd if (id == 0 || id == 0xffffffff)
92 1.8 cgd return (0);
93 1.8 cgd class = pci_conf_read(tag, PCI_CLASS_REG);
94 1.8 cgd
95 1.8 cgd pa.pa_bus = bus;
96 1.8 cgd pa.pa_device = device;
97 1.8 cgd pa.pa_tag = tag;
98 1.8 cgd pa.pa_id = id;
99 1.8 cgd pa.pa_class = class;
100 1.8 cgd
101 1.10 cgd #if defined(PCIVERBOSE) && 0 /* _too_ verbose */
102 1.10 cgd pci_devinfo(id, class, devinfo, NULL);
103 1.10 cgd printf("%s bus %d device %d: %s\n", pcidev->dv_xname, bus,
104 1.10 cgd device, devinfo);
105 1.10 cgd #endif /* _too_ verbose */
106 1.10 cgd
107 1.8 cgd if ((cf = config_search(pcisubmatch, pcidev, &pa)) != NULL)
108 1.8 cgd config_attach(pcidev, cf, &pa, pciprint);
109 1.8 cgd else {
110 1.10 cgd pci_devinfo(id, class, devinfo, &supported);
111 1.10 cgd printf("%s bus %d device %d: %s not %s\n", pcidev->dv_xname,
112 1.10 cgd bus, device, devinfo,
113 1.10 cgd supported ? "configured" : "supported");
114 1.8 cgd return (0);
115 1.8 cgd }
116 1.1 mycroft
117 1.8 cgd return (1);
118 1.10 cgd }
119 1.10 cgd
120 1.10 cgd /*
121 1.10 cgd * Descriptions of known PCI classes and subclasses.
122 1.10 cgd *
123 1.10 cgd * Subclasses are described in the same way as classes, but have a
124 1.10 cgd * NULL subclass pointer.
125 1.10 cgd */
126 1.10 cgd struct pci_class {
127 1.10 cgd char *name;
128 1.10 cgd int val; /* as wide as pci_{,sub}class_t */
129 1.10 cgd struct pci_class *subclasses;
130 1.10 cgd };
131 1.10 cgd
132 1.10 cgd struct pci_class pci_subclass_prehistoric[] = {
133 1.10 cgd { "miscellaneous", PCI_SUBCLASS_PREHISTORIC_MISC, },
134 1.10 cgd { "VGA", PCI_SUBCLASS_PREHISTORIC_VGA, },
135 1.10 cgd { 0 }
136 1.10 cgd };
137 1.10 cgd
138 1.10 cgd struct pci_class pci_subclass_mass_storage[] = {
139 1.10 cgd { "SCSI", PCI_SUBCLASS_MASS_STORAGE_SCSI, },
140 1.10 cgd { "IDE", PCI_SUBCLASS_MASS_STORAGE_IDE, },
141 1.10 cgd { "floppy", PCI_SUBCLASS_MASS_STORAGE_FLOPPY, },
142 1.10 cgd { "IPI", PCI_SUBCLASS_MASS_STORAGE_IPI, },
143 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MASS_STORAGE_MISC, },
144 1.10 cgd { 0 },
145 1.10 cgd };
146 1.10 cgd
147 1.10 cgd struct pci_class pci_subclass_network[] = {
148 1.10 cgd { "ethernet", PCI_SUBCLASS_NETWORK_ETHERNET, },
149 1.10 cgd { "token ring", PCI_SUBCLASS_NETWORK_TOKENRING, },
150 1.10 cgd { "FDDI", PCI_SUBCLASS_NETWORK_FDDI, },
151 1.10 cgd { "miscellaneous", PCI_SUBCLASS_NETWORK_MISC, },
152 1.10 cgd { 0 },
153 1.10 cgd };
154 1.10 cgd
155 1.10 cgd struct pci_class pci_subclass_display[] = {
156 1.10 cgd { "VGA", PCI_SUBCLASS_DISPLAY_VGA, },
157 1.10 cgd { "XGA", PCI_SUBCLASS_DISPLAY_XGA, },
158 1.10 cgd { "miscellaneous", PCI_SUBCLASS_DISPLAY_MISC, },
159 1.10 cgd { 0 },
160 1.10 cgd };
161 1.10 cgd
162 1.10 cgd struct pci_class pci_subclass_multimedia[] = {
163 1.10 cgd { "video", PCI_SUBCLASS_MULTIMEDIA_VIDEO, },
164 1.10 cgd { "audio", PCI_SUBCLASS_MULTIMEDIA_AUDIO, },
165 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MULTIMEDIA_MISC, },
166 1.10 cgd { 0 },
167 1.10 cgd };
168 1.10 cgd
169 1.10 cgd struct pci_class pci_subclass_memory[] = {
170 1.10 cgd { "RAM", PCI_SUBCLASS_MEMORY_RAM, },
171 1.10 cgd { "flash", PCI_SUBCLASS_MEMORY_FLASH, },
172 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MEMORY_MISC, },
173 1.10 cgd { 0 },
174 1.10 cgd };
175 1.10 cgd
176 1.10 cgd struct pci_class pci_subclass_bridge[] = {
177 1.10 cgd { "host", PCI_SUBCLASS_BRIDGE_HOST, },
178 1.10 cgd { "ISA", PCI_SUBCLASS_BRIDGE_ISA, },
179 1.10 cgd { "EISA", PCI_SUBCLASS_BRIDGE_EISA, },
180 1.10 cgd { "MicroChannel", PCI_SUBCLASS_BRIDGE_MC, },
181 1.10 cgd { "PCI", PCI_SUBCLASS_BRIDGE_PCI, },
182 1.10 cgd { "PCMCIA", PCI_SUBCLASS_BRIDGE_PCMCIA, },
183 1.10 cgd { "miscellaneous", PCI_SUBCLASS_BRIDGE_MISC, },
184 1.10 cgd { 0 },
185 1.10 cgd };
186 1.10 cgd
187 1.10 cgd struct pci_class pci_class[] = {
188 1.10 cgd { "prehistoric", PCI_CLASS_PREHISTORIC,
189 1.10 cgd pci_subclass_prehistoric, },
190 1.10 cgd { "mass storage", PCI_CLASS_MASS_STORAGE,
191 1.10 cgd pci_subclass_mass_storage, },
192 1.10 cgd { "network", PCI_CLASS_NETWORK,
193 1.10 cgd pci_subclass_network, },
194 1.10 cgd { "display", PCI_CLASS_DISPLAY,
195 1.11 cgd pci_subclass_display, },
196 1.10 cgd { "multimedia", PCI_CLASS_MULTIMEDIA,
197 1.10 cgd pci_subclass_multimedia, },
198 1.10 cgd { "memory", PCI_CLASS_MEMORY,
199 1.10 cgd pci_subclass_memory, },
200 1.10 cgd { "bridge", PCI_CLASS_BRIDGE,
201 1.10 cgd pci_subclass_bridge, },
202 1.10 cgd { "undefined", PCI_CLASS_UNDEFINED,
203 1.10 cgd 0, },
204 1.10 cgd { 0 },
205 1.10 cgd };
206 1.10 cgd
207 1.10 cgd #ifdef PCIVERBOSE
208 1.10 cgd /*
209 1.10 cgd * Descriptions of of known vendors and devices ("products").
210 1.10 cgd */
211 1.10 cgd struct pci_knowndev {
212 1.10 cgd pci_vendor_id_t vendor;
213 1.10 cgd pci_product_id_t product;
214 1.10 cgd int flags;
215 1.10 cgd char *vendorname, *productname;
216 1.10 cgd };
217 1.10 cgd #define PCI_KNOWNDEV_UNSUPP 0x01 /* unsupported device */
218 1.10 cgd #define PCI_KNOWNDEV_NOPROD 0x02 /* match on vendor only */
219 1.10 cgd
220 1.10 cgd #include <dev/pci/pcidevs_data.h>
221 1.10 cgd #endif /* PCIVERBOSE */
222 1.10 cgd
223 1.10 cgd void
224 1.10 cgd pci_devinfo(id_reg, class_reg, cp, supp)
225 1.10 cgd pcireg_t id_reg, class_reg;
226 1.10 cgd char *cp;
227 1.10 cgd int *supp;
228 1.10 cgd {
229 1.10 cgd pci_vendor_id_t vendor;
230 1.10 cgd pci_product_id_t product;
231 1.10 cgd pci_class_t class;
232 1.10 cgd pci_subclass_t subclass;
233 1.10 cgd pci_interface_t interface;
234 1.10 cgd pci_revision_t revision;
235 1.10 cgd char *vendor_namep, *product_namep;
236 1.10 cgd struct pci_class *classp, *subclassp;
237 1.10 cgd #ifdef PCIVERBOSE
238 1.10 cgd struct pci_knowndev *kdp;
239 1.10 cgd #endif
240 1.10 cgd
241 1.10 cgd vendor = PCI_VENDOR(id_reg);
242 1.10 cgd product = PCI_PRODUCT(id_reg);
243 1.10 cgd
244 1.10 cgd class = PCI_CLASS(class_reg);
245 1.10 cgd subclass = PCI_SUBCLASS(class_reg);
246 1.10 cgd interface = PCI_INTERFACE(class_reg);
247 1.10 cgd revision = PCI_REVISION(class_reg);
248 1.10 cgd
249 1.10 cgd #ifdef PCIVERBOSE
250 1.10 cgd kdp = pci_knowndevs;
251 1.10 cgd while (kdp->vendorname != NULL) { /* all have vendor name */
252 1.10 cgd if (kdp->vendor == vendor && (kdp->product == product ||
253 1.10 cgd (kdp->flags & PCI_KNOWNDEV_NOPROD) != 0))
254 1.10 cgd break;
255 1.10 cgd kdp++;
256 1.10 cgd }
257 1.10 cgd if (kdp->vendorname == NULL) {
258 1.10 cgd vendor_namep = product_namep = NULL;
259 1.10 cgd if (supp != NULL)
260 1.10 cgd *supp = 0;
261 1.10 cgd } else {
262 1.10 cgd vendor_namep = kdp->vendorname;
263 1.10 cgd product_namep = (kdp->flags & PCI_KNOWNDEV_NOPROD) == 0 ?
264 1.10 cgd kdp->productname : NULL;
265 1.10 cgd if (supp != NULL)
266 1.10 cgd *supp = (kdp->flags & PCI_KNOWNDEV_UNSUPP) == 0;
267 1.10 cgd }
268 1.10 cgd #else /* PCIVERBOSE */
269 1.10 cgd vendor_namep = product_namep = NULL;
270 1.10 cgd if (supp != NULL)
271 1.10 cgd *supp = 1; /* always say 'not configured' */
272 1.10 cgd #endif /* PCIVERBOSE */
273 1.10 cgd
274 1.10 cgd classp = pci_class;
275 1.10 cgd while (classp->name != NULL) {
276 1.10 cgd if (class == classp->val)
277 1.10 cgd break;
278 1.10 cgd classp++;
279 1.10 cgd }
280 1.10 cgd
281 1.10 cgd subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
282 1.10 cgd while (subclassp && subclassp->name != NULL) {
283 1.10 cgd if (subclass == subclassp->val)
284 1.10 cgd break;
285 1.10 cgd subclassp++;
286 1.10 cgd }
287 1.10 cgd
288 1.10 cgd if (vendor_namep == NULL)
289 1.10 cgd cp += sprintf(cp, "unknown vendor/product: 0x%04x/0x%04x",
290 1.10 cgd vendor, product);
291 1.10 cgd else if (product_namep != NULL)
292 1.10 cgd cp += sprintf(cp, "%s %s", vendor_namep, product_namep);
293 1.10 cgd else
294 1.10 cgd cp += sprintf(cp, "vendor: %s, unknown product: 0x%x",
295 1.10 cgd vendor_namep, product);
296 1.10 cgd cp += sprintf(cp, " (");
297 1.10 cgd if (classp->name == NULL)
298 1.10 cgd cp += sprintf(cp, "unknown class/subclass: 0x%02x/0x%02x",
299 1.10 cgd class, subclass);
300 1.10 cgd else {
301 1.10 cgd cp += sprintf(cp, "class: %s, ", classp->name);
302 1.10 cgd if (subclassp->name == NULL)
303 1.10 cgd cp += sprintf(cp, "unknown subclass: 0x%02x", subclass);
304 1.10 cgd else
305 1.10 cgd cp += sprintf(cp, "subclass: %s", subclassp->name);
306 1.10 cgd }
307 1.10 cgd #if 0 /* not very useful */
308 1.10 cgd cp += sprintf(cp, ", interface: 0x%02x", interface);
309 1.10 cgd #endif
310 1.10 cgd cp += sprintf(cp, ", revision: 0x%02x)", revision);
311 1.1 mycroft }
312