xref: /netbsd-src/sys/dev/pci/pci_subr.c (revision 6ea46cb5e46c49111a6ecf3bcbe3c7e2730fe9f6)
1 /*
2  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *	This product includes software developed by Charles Hannum.
15  * 4. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *	$Id: pci_subr.c,v 1.2 1994/08/10 04:37:52 mycroft Exp $
30  */
31 
32 /*
33  * PCI autoconfiguration support
34  */
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/device.h>
39 
40 #include <i386/pci/pcivar.h>
41 #include <i386/pci/pcireg.h>
42 
43 #if defined(i386) && !defined(NEWCONFIG)
44 #include <i386/isa/isa_device.h>
45 #endif
46 
47 int pciprobe();
48 void pciattach();
49 
50 struct cfdriver pcicd = {
51 	NULL, "pci", pciprobe, pciattach, DV_DULL, sizeof(struct device)
52 };
53 
54 int
55 pciprobe(parent, cf, aux)
56 	struct device *parent;
57 	struct cfdata *cf;
58 	void *aux;
59 {
60 
61 #ifdef i386
62 	if (pci_mode_detect() == 0)
63 		return 0;
64 #endif
65 	return 1;
66 }
67 
68 int
69 pciprint(aux, pci)
70 	void *aux;
71 	char *pci;
72 {
73 	register struct pci_attach_args *pa = aux;
74 
75 	printf("%s bus %d device %d", pci ? pci : "", pa->pa_bus,
76 	    pa->pa_device);
77 	if (pci)
78 		printf(": identifier %08x class %08x", pa->pa_id,
79 		    pa->pa_class);
80 	return UNCONF;
81 }
82 
83 void
84 pciattach(parent, self, aux)
85 	struct device *parent, *self;
86 	void *aux;
87 {
88 	int bus, device;
89 
90 #ifdef i386
91 	printf(": configuration mode %d\n", pci_mode);
92 #else
93 	printf("\n");
94 #endif
95 
96 #if 0
97 	for (bus = 0; bus <= 255; bus++) {
98 #else
99 	/*
100 	 * XXX
101 	 * Some current chipsets do wacky things with bus numbers > 0.
102 	 * This seems like a violation of protocol, but the PCI BIOS does
103 	 * allow one to query the maximum bus number, and eventually we
104 	 * should do so.
105 	 */
106 	for (bus = 0; bus <= 0; bus++) {
107 #endif
108 #ifdef i386
109 		for (device = 0; device <= (pci_mode == 2 ? 15 : 31);
110 		    device++) {
111 #else
112 		for (device = 0; device <= 31; device++) {
113 #endif
114 			pcitag_t tag = pci_make_tag(bus, device, 0);
115 			pcireg_t id = pci_conf_read(tag, PCI_ID_REG),
116 				 class = pci_conf_read(tag, PCI_CLASS_REG);
117 			struct pci_attach_args pa;
118 
119 			if (id == 0 || id == 0xffffffff)
120 				continue;
121 
122 			pa.pa_bus = bus;
123 			pa.pa_device = device;
124 			pa.pa_tag = tag;
125 			pa.pa_id = id;
126 			pa.pa_class = class;
127 
128 			(void)config_found(self, (void *)&pa, pciprint);
129 		}
130 	}
131 }
132 
133 int
134 pci_targmatch(cf, pa)
135 	struct cfdata *cf;
136 	struct pci_attach_args *pa;
137 {
138 #if !defined(i386) || defined(NEWCONFIG)
139 
140 #define	cf_bus		cf_loc[0]
141 #define	cf_device	cf_loc[1]
142 	if (cf->cf_bus != -1 && cf->cf_bus != pa->pa_bus)
143 		return 0;
144 	if (cf->cf_device != -1 && cf->cf_device != pa->pa_device)
145 		return 0;
146 #undef cf_device
147 #undef cf_bus
148 #else
149 	struct isa_device *id = (void *)cf->cf_loc;
150 
151 	if (id->id_physid != -1 &&
152 	    id->id_physid != (pa->pa_bus << 5) | pa->pa_device)
153 		return 0;
154 #endif
155 
156 	return 1;
157 }
158