pci.c revision 1.29.2.2 1 1.29.2.2 thorpej /* $NetBSD: pci.c,v 1.29.2.2 1997/09/01 20:29:42 thorpej Exp $ */
2 1.29.2.2 thorpej
3 1.29.2.2 thorpej /*
4 1.29.2.2 thorpej * Copyright (c) 1995, 1996, 1997
5 1.29.2.2 thorpej * Christopher G. Demetriou. All rights reserved.
6 1.29.2.2 thorpej * Copyright (c) 1994 Charles Hannum. All rights reserved.
7 1.29.2.2 thorpej *
8 1.29.2.2 thorpej * Redistribution and use in source and binary forms, with or without
9 1.29.2.2 thorpej * modification, are permitted provided that the following conditions
10 1.29.2.2 thorpej * are met:
11 1.29.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.29.2.2 thorpej * notice, this list of conditions and the following disclaimer.
13 1.29.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.29.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.29.2.2 thorpej * documentation and/or other materials provided with the distribution.
16 1.29.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
17 1.29.2.2 thorpej * must display the following acknowledgement:
18 1.29.2.2 thorpej * This product includes software developed by Charles Hannum.
19 1.29.2.2 thorpej * 4. The name of the author may not be used to endorse or promote products
20 1.29.2.2 thorpej * derived from this software without specific prior written permission.
21 1.29.2.2 thorpej *
22 1.29.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.29.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.29.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.29.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.29.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.29.2.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.29.2.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.29.2.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.29.2.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.29.2.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.29.2.2 thorpej */
33 1.29.2.2 thorpej
34 1.29.2.2 thorpej /*
35 1.29.2.2 thorpej * PCI bus autoconfiguration.
36 1.29.2.2 thorpej */
37 1.29.2.2 thorpej
38 1.29.2.2 thorpej #include <sys/param.h>
39 1.29.2.2 thorpej #include <sys/systm.h>
40 1.29.2.2 thorpej #include <sys/device.h>
41 1.29.2.2 thorpej
42 1.29.2.2 thorpej #include <dev/pci/pcireg.h>
43 1.29.2.2 thorpej #include <dev/pci/pcivar.h>
44 1.29.2.2 thorpej
45 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
46 1.29.2.2 thorpej int pcimatch __P((struct device *, void *, void *));
47 1.29.2.2 thorpej #else
48 1.29.2.2 thorpej int pcimatch __P((struct device *, struct cfdata *, void *));
49 1.29.2.2 thorpej #endif
50 1.29.2.2 thorpej void pciattach __P((struct device *, struct device *, void *));
51 1.29.2.2 thorpej
52 1.29.2.2 thorpej struct cfattach pci_ca = {
53 1.29.2.2 thorpej sizeof(struct device), pcimatch, pciattach
54 1.29.2.2 thorpej };
55 1.29.2.2 thorpej
56 1.29.2.2 thorpej struct cfdriver pci_cd = {
57 1.29.2.2 thorpej NULL, "pci", DV_DULL
58 1.29.2.2 thorpej };
59 1.29.2.2 thorpej
60 1.29.2.2 thorpej int pciprint __P((void *, const char *));
61 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
62 1.29.2.2 thorpej int pcisubmatch __P((struct device *, void *, void *));
63 1.29.2.2 thorpej #else
64 1.29.2.2 thorpej int pcisubmatch __P((struct device *, struct cfdata *, void *));
65 1.29.2.2 thorpej #endif
66 1.29.2.2 thorpej
67 1.29.2.2 thorpej /*
68 1.29.2.2 thorpej * Callback so that ISA/EISA bridges can attach their child busses
69 1.29.2.2 thorpej * after PCI configuration is done.
70 1.29.2.2 thorpej *
71 1.29.2.2 thorpej * This works because:
72 1.29.2.2 thorpej * (1) there can be at most one ISA/EISA bridge per PCI bus, and
73 1.29.2.2 thorpej * (2) any ISA/EISA bridges must be attached to primary PCI
74 1.29.2.2 thorpej * busses (i.e. bus zero).
75 1.29.2.2 thorpej *
76 1.29.2.2 thorpej * That boils down to: there can only be one of these outstanding
77 1.29.2.2 thorpej * at a time, it is cleared when configuring PCI bus 0 before any
78 1.29.2.2 thorpej * subdevices have been found, and it is run after all subdevices
79 1.29.2.2 thorpej * of PCI bus 0 have been found.
80 1.29.2.2 thorpej *
81 1.29.2.2 thorpej * This is needed because there are some (legacy) PCI devices which
82 1.29.2.2 thorpej * can show up as ISA/EISA devices as well (the prime example of which
83 1.29.2.2 thorpej * are VGA controllers). If you attach ISA from a PCI-ISA/EISA bridge,
84 1.29.2.2 thorpej * and the bridge is seen before the video board is, the board can show
85 1.29.2.2 thorpej * up as an ISA device, and that can (bogusly) complicate the PCI device's
86 1.29.2.2 thorpej * attach code, or make the PCI device not be properly attached at all.
87 1.29.2.2 thorpej */
88 1.29.2.2 thorpej static void (*pci_isa_bridge_callback) __P((void *));
89 1.29.2.2 thorpej static void *pci_isa_bridge_callback_arg;
90 1.29.2.2 thorpej
91 1.29.2.2 thorpej int
92 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
93 1.29.2.2 thorpej pcimatch(parent, match, aux)
94 1.29.2.2 thorpej #else
95 1.29.2.2 thorpej pcimatch(parent, cf, aux)
96 1.29.2.2 thorpej #endif
97 1.29.2.2 thorpej struct device *parent;
98 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
99 1.29.2.2 thorpej void *match;
100 1.29.2.2 thorpej #else
101 1.29.2.2 thorpej struct cfdata *cf;
102 1.29.2.2 thorpej #endif
103 1.29.2.2 thorpej void *aux;
104 1.29.2.2 thorpej {
105 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
106 1.29.2.2 thorpej struct cfdata *cf = match;
107 1.29.2.2 thorpej #endif
108 1.29.2.2 thorpej struct pcibus_attach_args *pba = aux;
109 1.29.2.2 thorpej
110 1.29.2.2 thorpej if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
111 1.29.2.2 thorpej return (0);
112 1.29.2.2 thorpej
113 1.29.2.2 thorpej /* Check the locators */
114 1.29.2.2 thorpej if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
115 1.29.2.2 thorpej cf->pcibuscf_bus != pba->pba_bus)
116 1.29.2.2 thorpej return (0);
117 1.29.2.2 thorpej
118 1.29.2.2 thorpej /* sanity */
119 1.29.2.2 thorpej if (pba->pba_bus < 0 || pba->pba_bus > 255)
120 1.29.2.2 thorpej return (0);
121 1.29.2.2 thorpej
122 1.29.2.2 thorpej /*
123 1.29.2.2 thorpej * XXX check other (hardware?) indicators
124 1.29.2.2 thorpej */
125 1.29.2.2 thorpej
126 1.29.2.2 thorpej return 1;
127 1.29.2.2 thorpej }
128 1.29.2.2 thorpej
129 1.29.2.2 thorpej void
130 1.29.2.2 thorpej pciattach(parent, self, aux)
131 1.29.2.2 thorpej struct device *parent, *self;
132 1.29.2.2 thorpej void *aux;
133 1.29.2.2 thorpej {
134 1.29.2.2 thorpej struct pcibus_attach_args *pba = aux;
135 1.29.2.2 thorpej bus_space_tag_t iot, memt;
136 1.29.2.2 thorpej pci_chipset_tag_t pc;
137 1.29.2.2 thorpej int bus, device, maxndevs, function, nfunctions;
138 1.29.2.2 thorpej
139 1.29.2.2 thorpej pci_attach_hook(parent, self, pba);
140 1.29.2.2 thorpej printf("\n");
141 1.29.2.2 thorpej
142 1.29.2.2 thorpej iot = pba->pba_iot;
143 1.29.2.2 thorpej memt = pba->pba_memt;
144 1.29.2.2 thorpej pc = pba->pba_pc;
145 1.29.2.2 thorpej bus = pba->pba_bus;
146 1.29.2.2 thorpej maxndevs = pci_bus_maxdevs(pc, bus);
147 1.29.2.2 thorpej
148 1.29.2.2 thorpej if (bus == 0)
149 1.29.2.2 thorpej pci_isa_bridge_callback = NULL;
150 1.29.2.2 thorpej
151 1.29.2.2 thorpej for (device = 0; device < maxndevs; device++) {
152 1.29.2.2 thorpej pcitag_t tag;
153 1.29.2.2 thorpej pcireg_t id, class, intr, bhlcr, csr;
154 1.29.2.2 thorpej struct pci_attach_args pa;
155 1.29.2.2 thorpej int pin;
156 1.29.2.2 thorpej
157 1.29.2.2 thorpej tag = pci_make_tag(pc, bus, device, 0);
158 1.29.2.2 thorpej id = pci_conf_read(pc, tag, PCI_ID_REG);
159 1.29.2.2 thorpej if (id == 0 || id == 0xffffffff)
160 1.29.2.2 thorpej continue;
161 1.29.2.2 thorpej
162 1.29.2.2 thorpej bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
163 1.29.2.2 thorpej nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
164 1.29.2.2 thorpej
165 1.29.2.2 thorpej for (function = 0; function < nfunctions; function++) {
166 1.29.2.2 thorpej tag = pci_make_tag(pc, bus, device, function);
167 1.29.2.2 thorpej id = pci_conf_read(pc, tag, PCI_ID_REG);
168 1.29.2.2 thorpej if (id == 0 || id == 0xffffffff)
169 1.29.2.2 thorpej continue;
170 1.29.2.2 thorpej csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
171 1.29.2.2 thorpej class = pci_conf_read(pc, tag, PCI_CLASS_REG);
172 1.29.2.2 thorpej intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
173 1.29.2.2 thorpej
174 1.29.2.2 thorpej pa.pa_iot = iot;
175 1.29.2.2 thorpej pa.pa_memt = memt;
176 1.29.2.2 thorpej pa.pa_dmat = pba->pba_dmat;
177 1.29.2.2 thorpej pa.pa_pc = pc;
178 1.29.2.2 thorpej pa.pa_device = device;
179 1.29.2.2 thorpej pa.pa_function = function;
180 1.29.2.2 thorpej pa.pa_tag = tag;
181 1.29.2.2 thorpej pa.pa_id = id;
182 1.29.2.2 thorpej pa.pa_class = class;
183 1.29.2.2 thorpej
184 1.29.2.2 thorpej /* set up memory and I/O enable flags as appropriate */
185 1.29.2.2 thorpej pa.pa_flags = 0;
186 1.29.2.2 thorpej if ((pba->pba_flags & PCI_FLAGS_IO_ENABLED) &&
187 1.29.2.2 thorpej (csr & PCI_COMMAND_IO_ENABLE))
188 1.29.2.2 thorpej pa.pa_flags |= PCI_FLAGS_IO_ENABLED;
189 1.29.2.2 thorpej if ((pba->pba_flags & PCI_FLAGS_MEM_ENABLED) &&
190 1.29.2.2 thorpej (csr & PCI_COMMAND_MEM_ENABLE))
191 1.29.2.2 thorpej pa.pa_flags |= PCI_FLAGS_MEM_ENABLED;
192 1.29.2.2 thorpej
193 1.29.2.2 thorpej if (bus == 0) {
194 1.29.2.2 thorpej pa.pa_intrswiz = 0;
195 1.29.2.2 thorpej pa.pa_intrtag = tag;
196 1.29.2.2 thorpej } else {
197 1.29.2.2 thorpej pa.pa_intrswiz = pba->pba_intrswiz + device;
198 1.29.2.2 thorpej pa.pa_intrtag = pba->pba_intrtag;
199 1.29.2.2 thorpej }
200 1.29.2.2 thorpej pin = PCI_INTERRUPT_PIN(intr);
201 1.29.2.2 thorpej if (pin == PCI_INTERRUPT_PIN_NONE) {
202 1.29.2.2 thorpej /* no interrupt */
203 1.29.2.2 thorpej pa.pa_intrpin = 0;
204 1.29.2.2 thorpej } else {
205 1.29.2.2 thorpej /*
206 1.29.2.2 thorpej * swizzle it based on the number of
207 1.29.2.2 thorpej * busses we're behind and our device
208 1.29.2.2 thorpej * number.
209 1.29.2.2 thorpej */
210 1.29.2.2 thorpej pa.pa_intrpin = /* XXX */
211 1.29.2.2 thorpej ((pin + pa.pa_intrswiz - 1) % 4) + 1;
212 1.29.2.2 thorpej }
213 1.29.2.2 thorpej pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
214 1.29.2.2 thorpej
215 1.29.2.2 thorpej config_found_sm(self, &pa, pciprint, pcisubmatch);
216 1.29.2.2 thorpej }
217 1.29.2.2 thorpej }
218 1.29.2.2 thorpej
219 1.29.2.2 thorpej if (bus == 0 && pci_isa_bridge_callback != NULL)
220 1.29.2.2 thorpej (*pci_isa_bridge_callback)(pci_isa_bridge_callback_arg);
221 1.29.2.2 thorpej }
222 1.29.2.2 thorpej
223 1.29.2.2 thorpej int
224 1.29.2.2 thorpej pciprint(aux, pnp)
225 1.29.2.2 thorpej void *aux;
226 1.29.2.2 thorpej const char *pnp;
227 1.29.2.2 thorpej {
228 1.29.2.2 thorpej register struct pci_attach_args *pa = aux;
229 1.29.2.2 thorpej char devinfo[256];
230 1.29.2.2 thorpej
231 1.29.2.2 thorpej if (pnp) {
232 1.29.2.2 thorpej pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
233 1.29.2.2 thorpej printf("%s at %s", devinfo, pnp);
234 1.29.2.2 thorpej }
235 1.29.2.2 thorpej printf(" dev %d function %d", pa->pa_device, pa->pa_function);
236 1.29.2.2 thorpej #if 0
237 1.29.2.2 thorpej printf(" (%si/o, %smem)",
238 1.29.2.2 thorpej pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "" : "no ",
239 1.29.2.2 thorpej pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "" : "no ");
240 1.29.2.2 thorpej #endif
241 1.29.2.2 thorpej return (UNCONF);
242 1.29.2.2 thorpej }
243 1.29.2.2 thorpej
244 1.29.2.2 thorpej int
245 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
246 1.29.2.2 thorpej pcisubmatch(parent, match, aux)
247 1.29.2.2 thorpej #else
248 1.29.2.2 thorpej pcisubmatch(parent, cf, aux)
249 1.29.2.2 thorpej #endif
250 1.29.2.2 thorpej struct device *parent;
251 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
252 1.29.2.2 thorpej void *match;
253 1.29.2.2 thorpej #else
254 1.29.2.2 thorpej struct cfdata *cf;
255 1.29.2.2 thorpej #endif
256 1.29.2.2 thorpej void *aux;
257 1.29.2.2 thorpej {
258 1.29.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
259 1.29.2.2 thorpej struct cfdata *cf = match;
260 1.29.2.2 thorpej #endif
261 1.29.2.2 thorpej struct pci_attach_args *pa = aux;
262 1.29.2.2 thorpej
263 1.29.2.2 thorpej if (cf->pcicf_dev != PCI_UNK_DEV &&
264 1.29.2.2 thorpej cf->pcicf_dev != pa->pa_device)
265 1.29.2.2 thorpej return 0;
266 1.29.2.2 thorpej if (cf->pcicf_function != PCI_UNK_FUNCTION &&
267 1.29.2.2 thorpej cf->pcicf_function != pa->pa_function)
268 1.29.2.2 thorpej return 0;
269 1.29.2.2 thorpej return ((*cf->cf_attach->ca_match)(parent, cf, aux));
270 1.29.2.2 thorpej }
271 1.29.2.2 thorpej
272 1.29.2.2 thorpej void
273 1.29.2.2 thorpej set_pci_isa_bridge_callback(fn, arg)
274 1.29.2.2 thorpej void (*fn) __P((void *));
275 1.29.2.2 thorpej void *arg;
276 1.29.2.2 thorpej {
277 1.29.2.2 thorpej
278 1.29.2.2 thorpej if (pci_isa_bridge_callback != NULL)
279 1.29.2.2 thorpej panic("set_pci_isa_bridge_callback");
280 1.29.2.2 thorpej pci_isa_bridge_callback = fn;
281 1.29.2.2 thorpej pci_isa_bridge_callback_arg = arg;
282 1.29.2.2 thorpej }
283