pci.c revision 1.8 1 /* $NetBSD: pci.c,v 1.8 1995/05/23 03:43:06 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * PCI autoconfiguration support
34 * XXX probably should be moved to pci_subr.c?
35 */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/device.h>
40
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
43
44 int
45 pciprint(aux, pci)
46 void *aux;
47 char *pci;
48 {
49 register struct pci_attach_args *pa = aux;
50
51 printf(" bus %d device %d", pa->pa_bus, pa->pa_device);
52 return (UNCONF);
53 }
54
55 int
56 pcisubmatch(parent, match, aux)
57 struct device *parent;
58 void *match, *aux;
59 {
60 struct cfdata *cf = match;
61 struct pci_attach_args *pa = aux;
62
63 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pa->pa_bus)
64 return 0;
65 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != pa->pa_device)
66 return 0;
67 return ((*cf->cf_driver->cd_match)(parent, match, aux));
68 }
69
70 /*
71 * Try to find and attach the PCI device at the give bus and device number.
72 * Return 1 if successful, 0 if unsuccessful.
73 */
74 int
75 pci_attach_subdev(pcidev, bus, device)
76 struct device *pcidev;
77 int bus, device;
78 {
79 pcitag_t tag;
80 pcireg_t id, class;
81 struct pci_attach_args pa;
82 struct cfdata *cf;
83
84 tag = pci_make_tag(bus, device, 0);
85 id = pci_conf_read(tag, PCI_ID_REG);
86 if (id == 0 || id == 0xffffffff)
87 return (0);
88 class = pci_conf_read(tag, PCI_CLASS_REG);
89
90 pa.pa_bus = bus;
91 pa.pa_device = device;
92 pa.pa_tag = tag;
93 pa.pa_id = id;
94 pa.pa_class = class;
95
96 if ((cf = config_search(pcisubmatch, pcidev, &pa)) != NULL)
97 config_attach(pcidev, cf, &pa, pciprint);
98 else {
99 printf("%s bus %d device %d: identifier %08x class %08x%s",
100 pcidev->dv_xname, bus, device, id, class,
101 " not configured\n");
102 return (0);
103 }
104
105 return (1);
106 }
107