pci_subr.c revision 1.13 1 1.13 cgd /* $NetBSD: pci_subr.c,v 1.13 1996/01/22 21:08:10 cgd Exp $ */
2 1.3 cgd
3 1.1 mycroft /*
4 1.13 cgd * Copyright (c) 1995, 1996 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.13 cgd pci_devinfo(id, class, 1, devinfo);
111 1.13 cgd printf("%s bus %d device %d: %s not configured\n",
112 1.13 cgd pcidev->dv_xname, bus, device, devinfo);
113 1.8 cgd return (0);
114 1.8 cgd }
115 1.1 mycroft
116 1.8 cgd return (1);
117 1.10 cgd }
118 1.10 cgd
119 1.10 cgd /*
120 1.10 cgd * Descriptions of known PCI classes and subclasses.
121 1.10 cgd *
122 1.10 cgd * Subclasses are described in the same way as classes, but have a
123 1.10 cgd * NULL subclass pointer.
124 1.10 cgd */
125 1.10 cgd struct pci_class {
126 1.10 cgd char *name;
127 1.10 cgd int val; /* as wide as pci_{,sub}class_t */
128 1.10 cgd struct pci_class *subclasses;
129 1.10 cgd };
130 1.10 cgd
131 1.10 cgd struct pci_class pci_subclass_prehistoric[] = {
132 1.10 cgd { "miscellaneous", PCI_SUBCLASS_PREHISTORIC_MISC, },
133 1.10 cgd { "VGA", PCI_SUBCLASS_PREHISTORIC_VGA, },
134 1.10 cgd { 0 }
135 1.10 cgd };
136 1.10 cgd
137 1.10 cgd struct pci_class pci_subclass_mass_storage[] = {
138 1.10 cgd { "SCSI", PCI_SUBCLASS_MASS_STORAGE_SCSI, },
139 1.10 cgd { "IDE", PCI_SUBCLASS_MASS_STORAGE_IDE, },
140 1.10 cgd { "floppy", PCI_SUBCLASS_MASS_STORAGE_FLOPPY, },
141 1.10 cgd { "IPI", PCI_SUBCLASS_MASS_STORAGE_IPI, },
142 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MASS_STORAGE_MISC, },
143 1.10 cgd { 0 },
144 1.10 cgd };
145 1.10 cgd
146 1.10 cgd struct pci_class pci_subclass_network[] = {
147 1.10 cgd { "ethernet", PCI_SUBCLASS_NETWORK_ETHERNET, },
148 1.10 cgd { "token ring", PCI_SUBCLASS_NETWORK_TOKENRING, },
149 1.10 cgd { "FDDI", PCI_SUBCLASS_NETWORK_FDDI, },
150 1.10 cgd { "miscellaneous", PCI_SUBCLASS_NETWORK_MISC, },
151 1.10 cgd { 0 },
152 1.10 cgd };
153 1.10 cgd
154 1.10 cgd struct pci_class pci_subclass_display[] = {
155 1.10 cgd { "VGA", PCI_SUBCLASS_DISPLAY_VGA, },
156 1.10 cgd { "XGA", PCI_SUBCLASS_DISPLAY_XGA, },
157 1.10 cgd { "miscellaneous", PCI_SUBCLASS_DISPLAY_MISC, },
158 1.10 cgd { 0 },
159 1.10 cgd };
160 1.10 cgd
161 1.10 cgd struct pci_class pci_subclass_multimedia[] = {
162 1.10 cgd { "video", PCI_SUBCLASS_MULTIMEDIA_VIDEO, },
163 1.10 cgd { "audio", PCI_SUBCLASS_MULTIMEDIA_AUDIO, },
164 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MULTIMEDIA_MISC, },
165 1.10 cgd { 0 },
166 1.10 cgd };
167 1.10 cgd
168 1.10 cgd struct pci_class pci_subclass_memory[] = {
169 1.10 cgd { "RAM", PCI_SUBCLASS_MEMORY_RAM, },
170 1.10 cgd { "flash", PCI_SUBCLASS_MEMORY_FLASH, },
171 1.10 cgd { "miscellaneous", PCI_SUBCLASS_MEMORY_MISC, },
172 1.10 cgd { 0 },
173 1.10 cgd };
174 1.10 cgd
175 1.10 cgd struct pci_class pci_subclass_bridge[] = {
176 1.10 cgd { "host", PCI_SUBCLASS_BRIDGE_HOST, },
177 1.10 cgd { "ISA", PCI_SUBCLASS_BRIDGE_ISA, },
178 1.10 cgd { "EISA", PCI_SUBCLASS_BRIDGE_EISA, },
179 1.10 cgd { "MicroChannel", PCI_SUBCLASS_BRIDGE_MC, },
180 1.10 cgd { "PCI", PCI_SUBCLASS_BRIDGE_PCI, },
181 1.10 cgd { "PCMCIA", PCI_SUBCLASS_BRIDGE_PCMCIA, },
182 1.10 cgd { "miscellaneous", PCI_SUBCLASS_BRIDGE_MISC, },
183 1.10 cgd { 0 },
184 1.10 cgd };
185 1.10 cgd
186 1.10 cgd struct pci_class pci_class[] = {
187 1.10 cgd { "prehistoric", PCI_CLASS_PREHISTORIC,
188 1.10 cgd pci_subclass_prehistoric, },
189 1.10 cgd { "mass storage", PCI_CLASS_MASS_STORAGE,
190 1.10 cgd pci_subclass_mass_storage, },
191 1.10 cgd { "network", PCI_CLASS_NETWORK,
192 1.10 cgd pci_subclass_network, },
193 1.10 cgd { "display", PCI_CLASS_DISPLAY,
194 1.11 cgd pci_subclass_display, },
195 1.10 cgd { "multimedia", PCI_CLASS_MULTIMEDIA,
196 1.10 cgd pci_subclass_multimedia, },
197 1.10 cgd { "memory", PCI_CLASS_MEMORY,
198 1.10 cgd pci_subclass_memory, },
199 1.10 cgd { "bridge", PCI_CLASS_BRIDGE,
200 1.10 cgd pci_subclass_bridge, },
201 1.10 cgd { "undefined", PCI_CLASS_UNDEFINED,
202 1.10 cgd 0, },
203 1.10 cgd { 0 },
204 1.10 cgd };
205 1.10 cgd
206 1.10 cgd #ifdef PCIVERBOSE
207 1.10 cgd /*
208 1.10 cgd * Descriptions of of known vendors and devices ("products").
209 1.10 cgd */
210 1.10 cgd struct pci_knowndev {
211 1.10 cgd pci_vendor_id_t vendor;
212 1.10 cgd pci_product_id_t product;
213 1.10 cgd int flags;
214 1.10 cgd char *vendorname, *productname;
215 1.10 cgd };
216 1.13 cgd #define PCI_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
217 1.10 cgd
218 1.10 cgd #include <dev/pci/pcidevs_data.h>
219 1.10 cgd #endif /* PCIVERBOSE */
220 1.10 cgd
221 1.10 cgd void
222 1.13 cgd pci_devinfo(id_reg, class_reg, showclass, cp)
223 1.10 cgd pcireg_t id_reg, class_reg;
224 1.13 cgd int showclass;
225 1.10 cgd char *cp;
226 1.10 cgd {
227 1.10 cgd pci_vendor_id_t vendor;
228 1.10 cgd pci_product_id_t product;
229 1.10 cgd pci_class_t class;
230 1.10 cgd pci_subclass_t subclass;
231 1.10 cgd pci_interface_t interface;
232 1.10 cgd pci_revision_t revision;
233 1.10 cgd char *vendor_namep, *product_namep;
234 1.10 cgd struct pci_class *classp, *subclassp;
235 1.10 cgd #ifdef PCIVERBOSE
236 1.10 cgd struct pci_knowndev *kdp;
237 1.10 cgd #endif
238 1.10 cgd
239 1.10 cgd vendor = PCI_VENDOR(id_reg);
240 1.10 cgd product = PCI_PRODUCT(id_reg);
241 1.10 cgd
242 1.10 cgd class = PCI_CLASS(class_reg);
243 1.10 cgd subclass = PCI_SUBCLASS(class_reg);
244 1.10 cgd interface = PCI_INTERFACE(class_reg);
245 1.10 cgd revision = PCI_REVISION(class_reg);
246 1.10 cgd
247 1.10 cgd #ifdef PCIVERBOSE
248 1.10 cgd kdp = pci_knowndevs;
249 1.10 cgd while (kdp->vendorname != NULL) { /* all have vendor name */
250 1.10 cgd if (kdp->vendor == vendor && (kdp->product == product ||
251 1.10 cgd (kdp->flags & PCI_KNOWNDEV_NOPROD) != 0))
252 1.10 cgd break;
253 1.10 cgd kdp++;
254 1.10 cgd }
255 1.13 cgd if (kdp->vendorname == NULL)
256 1.10 cgd vendor_namep = product_namep = NULL;
257 1.13 cgd else {
258 1.10 cgd vendor_namep = kdp->vendorname;
259 1.10 cgd product_namep = (kdp->flags & PCI_KNOWNDEV_NOPROD) == 0 ?
260 1.10 cgd kdp->productname : NULL;
261 1.10 cgd }
262 1.10 cgd #else /* PCIVERBOSE */
263 1.10 cgd vendor_namep = product_namep = NULL;
264 1.10 cgd #endif /* PCIVERBOSE */
265 1.10 cgd
266 1.10 cgd classp = pci_class;
267 1.10 cgd while (classp->name != NULL) {
268 1.10 cgd if (class == classp->val)
269 1.10 cgd break;
270 1.10 cgd classp++;
271 1.10 cgd }
272 1.10 cgd
273 1.10 cgd subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
274 1.10 cgd while (subclassp && subclassp->name != NULL) {
275 1.10 cgd if (subclass == subclassp->val)
276 1.10 cgd break;
277 1.10 cgd subclassp++;
278 1.10 cgd }
279 1.10 cgd
280 1.10 cgd if (vendor_namep == NULL)
281 1.10 cgd cp += sprintf(cp, "unknown vendor/product: 0x%04x/0x%04x",
282 1.10 cgd vendor, product);
283 1.10 cgd else if (product_namep != NULL)
284 1.10 cgd cp += sprintf(cp, "%s %s", vendor_namep, product_namep);
285 1.10 cgd else
286 1.10 cgd cp += sprintf(cp, "vendor: %s, unknown product: 0x%x",
287 1.10 cgd vendor_namep, product);
288 1.13 cgd if (showclass) {
289 1.13 cgd cp += sprintf(cp, " (");
290 1.13 cgd if (classp->name == NULL)
291 1.13 cgd cp += sprintf(cp,
292 1.13 cgd "unknown class/subclass: 0x%02x/0x%02x",
293 1.13 cgd class, subclass);
294 1.13 cgd else {
295 1.13 cgd cp += sprintf(cp, "class: %s, ", classp->name);
296 1.13 cgd if (subclassp == NULL || subclassp->name == NULL)
297 1.13 cgd cp += sprintf(cp, "unknown subclass: 0x%02x",
298 1.13 cgd subclass);
299 1.13 cgd else
300 1.13 cgd cp += sprintf(cp, "subclass: %s",
301 1.13 cgd subclassp->name);
302 1.13 cgd }
303 1.10 cgd #if 0 /* not very useful */
304 1.13 cgd cp += sprintf(cp, ", interface: 0x%02x", interface);
305 1.10 cgd #endif
306 1.13 cgd cp += sprintf(cp, ", revision: 0x%02x)", revision);
307 1.13 cgd }
308 1.1 mycroft }
309