pci.c revision 1.26 1 1.26 cgd /* $NetBSD: pci.c,v 1.26 1996/12/05 01:25:30 cgd Exp $ */
2 1.3 cgd
3 1.1 mycroft /*
4 1.10 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 bus autoconfiguration.
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
44 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
45 1.10 cgd int pcimatch __P((struct device *, void *, void *));
46 1.26 cgd #else
47 1.26 cgd int pcimatch __P((struct device *, struct cfdata *, void *));
48 1.26 cgd #endif
49 1.10 cgd void pciattach __P((struct device *, struct device *, void *));
50 1.10 cgd
51 1.16 thorpej struct cfattach pci_ca = {
52 1.16 thorpej sizeof(struct device), pcimatch, pciattach
53 1.16 thorpej };
54 1.16 thorpej
55 1.16 thorpej struct cfdriver pci_cd = {
56 1.16 thorpej NULL, "pci", DV_DULL
57 1.10 cgd };
58 1.10 cgd
59 1.21 cgd int pciprint __P((void *, const char *));
60 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
61 1.10 cgd int pcisubmatch __P((struct device *, void *, void *));
62 1.26 cgd #else
63 1.26 cgd int pcisubmatch __P((struct device *, struct cfdata *, void *));
64 1.26 cgd #endif
65 1.10 cgd
66 1.25 cgd /*
67 1.25 cgd * Callback so that ISA/EISA bridges can attach their child busses
68 1.25 cgd * after PCI configuration is done.
69 1.25 cgd *
70 1.25 cgd * This works because:
71 1.25 cgd * (1) there can be at most one ISA/EISA bridge per PCI bus, and
72 1.25 cgd * (2) any ISA/EISA bridges must be attached to primary PCI
73 1.25 cgd * busses (i.e. bus zero).
74 1.25 cgd *
75 1.25 cgd * That boils down to: there can only be one of these outstanding
76 1.25 cgd * at a time, it is cleared when configuring PCI bus 0 before any
77 1.25 cgd * subdevices have been found, and it is run after all subdevices
78 1.25 cgd * of PCI bus 0 have been found.
79 1.25 cgd *
80 1.25 cgd * This is needed because there are some (legacy) PCI devices which
81 1.25 cgd * can show up as ISA/EISA devices as well (the prime example of which
82 1.25 cgd * are VGA controllers). If you attach ISA from a PCI-ISA/EISA bridge,
83 1.25 cgd * and the bridge is seen before the video board is, the board can show
84 1.25 cgd * up as an ISA device, and that can (bogusly) complicate the PCI device's
85 1.25 cgd * attach code, or make the PCI device not be properly attached at all.
86 1.25 cgd */
87 1.25 cgd static void (*pci_isa_bridge_callback) __P((void *));
88 1.25 cgd static void *pci_isa_bridge_callback_arg;
89 1.25 cgd
90 1.10 cgd int
91 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
92 1.10 cgd pcimatch(parent, match, aux)
93 1.26 cgd #else
94 1.26 cgd pcimatch(parent, cf, aux)
95 1.26 cgd #endif
96 1.10 cgd struct device *parent;
97 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
98 1.26 cgd void *match;
99 1.26 cgd #else
100 1.26 cgd struct cfdata *cf;
101 1.26 cgd #endif
102 1.26 cgd void *aux;
103 1.10 cgd {
104 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
105 1.10 cgd struct cfdata *cf = match;
106 1.26 cgd #endif
107 1.10 cgd struct pcibus_attach_args *pba = aux;
108 1.10 cgd
109 1.10 cgd if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
110 1.10 cgd return (0);
111 1.10 cgd
112 1.10 cgd /* Check the locators */
113 1.14 cgd if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
114 1.14 cgd cf->pcibuscf_bus != pba->pba_bus)
115 1.10 cgd return (0);
116 1.10 cgd
117 1.10 cgd /* sanity */
118 1.10 cgd if (pba->pba_bus < 0 || pba->pba_bus > 255)
119 1.10 cgd return (0);
120 1.10 cgd
121 1.10 cgd /*
122 1.10 cgd * XXX check other (hardware?) indicators
123 1.10 cgd */
124 1.10 cgd
125 1.10 cgd return 1;
126 1.10 cgd }
127 1.10 cgd
128 1.10 cgd void
129 1.10 cgd pciattach(parent, self, aux)
130 1.10 cgd struct device *parent, *self;
131 1.10 cgd void *aux;
132 1.10 cgd {
133 1.10 cgd struct pcibus_attach_args *pba = aux;
134 1.24 thorpej bus_space_tag_t iot, memt;
135 1.18 cgd pci_chipset_tag_t pc;
136 1.18 cgd int bus, device, maxndevs, function, nfunctions;
137 1.10 cgd
138 1.18 cgd pci_attach_hook(parent, self, pba);
139 1.23 christos printf("\n");
140 1.10 cgd
141 1.24 thorpej iot = pba->pba_iot;
142 1.24 thorpej memt = pba->pba_memt;
143 1.18 cgd pc = pba->pba_pc;
144 1.18 cgd bus = pba->pba_bus;
145 1.18 cgd maxndevs = pci_bus_maxdevs(pc, bus);
146 1.18 cgd
147 1.25 cgd if (bus == 0)
148 1.25 cgd pci_isa_bridge_callback = NULL;
149 1.25 cgd
150 1.18 cgd for (device = 0; device < maxndevs; device++) {
151 1.10 cgd pcitag_t tag;
152 1.18 cgd pcireg_t id, class, intr, bhlcr;
153 1.10 cgd struct pci_attach_args pa;
154 1.19 christos int pin;
155 1.10 cgd
156 1.18 cgd tag = pci_make_tag(pc, bus, device, 0);
157 1.18 cgd id = pci_conf_read(pc, tag, PCI_ID_REG);
158 1.10 cgd if (id == 0 || id == 0xffffffff)
159 1.10 cgd continue;
160 1.10 cgd
161 1.18 cgd bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
162 1.17 cgd nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
163 1.10 cgd
164 1.10 cgd for (function = 0; function < nfunctions; function++) {
165 1.18 cgd tag = pci_make_tag(pc, bus, device, function);
166 1.18 cgd id = pci_conf_read(pc, tag, PCI_ID_REG);
167 1.10 cgd if (id == 0 || id == 0xffffffff)
168 1.10 cgd continue;
169 1.18 cgd class = pci_conf_read(pc, tag, PCI_CLASS_REG);
170 1.18 cgd intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
171 1.10 cgd
172 1.24 thorpej pa.pa_iot = iot;
173 1.24 thorpej pa.pa_memt = memt;
174 1.18 cgd pa.pa_pc = pc;
175 1.10 cgd pa.pa_device = device;
176 1.10 cgd pa.pa_function = function;
177 1.10 cgd pa.pa_tag = tag;
178 1.10 cgd pa.pa_id = id;
179 1.10 cgd pa.pa_class = class;
180 1.10 cgd
181 1.18 cgd if (bus == 0) {
182 1.18 cgd pa.pa_intrswiz = 0;
183 1.18 cgd pa.pa_intrtag = tag;
184 1.18 cgd } else {
185 1.18 cgd pa.pa_intrswiz = pba->pba_intrswiz + device;
186 1.18 cgd pa.pa_intrtag = pba->pba_intrtag;
187 1.18 cgd }
188 1.18 cgd pin = PCI_INTERRUPT_PIN(intr);
189 1.18 cgd if (pin == PCI_INTERRUPT_PIN_NONE) {
190 1.18 cgd /* no interrupt */
191 1.18 cgd pa.pa_intrpin = 0;
192 1.18 cgd } else {
193 1.18 cgd /*
194 1.18 cgd * swizzle it based on the number of
195 1.18 cgd * busses we're behind and our device
196 1.18 cgd * number.
197 1.18 cgd */
198 1.18 cgd pa.pa_intrpin = /* XXX */
199 1.18 cgd ((pin + pa.pa_intrswiz - 1) % 4) + 1;
200 1.18 cgd }
201 1.18 cgd pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
202 1.18 cgd
203 1.10 cgd config_found_sm(self, &pa, pciprint, pcisubmatch);
204 1.10 cgd }
205 1.10 cgd }
206 1.25 cgd
207 1.25 cgd if (bus == 0 && pci_isa_bridge_callback != NULL)
208 1.25 cgd (*pci_isa_bridge_callback)(pci_isa_bridge_callback_arg);
209 1.10 cgd }
210 1.1 mycroft
211 1.1 mycroft int
212 1.10 cgd pciprint(aux, pnp)
213 1.1 mycroft void *aux;
214 1.21 cgd const char *pnp;
215 1.1 mycroft {
216 1.1 mycroft register struct pci_attach_args *pa = aux;
217 1.10 cgd char devinfo[256];
218 1.1 mycroft
219 1.10 cgd if (pnp) {
220 1.10 cgd pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
221 1.23 christos printf("%s at %s", devinfo, pnp);
222 1.10 cgd }
223 1.23 christos printf(" dev %d function %d", pa->pa_device, pa->pa_function);
224 1.6 mycroft return (UNCONF);
225 1.6 mycroft }
226 1.6 mycroft
227 1.6 mycroft int
228 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
229 1.6 mycroft pcisubmatch(parent, match, aux)
230 1.26 cgd #else
231 1.26 cgd pcisubmatch(parent, cf, aux)
232 1.26 cgd #endif
233 1.6 mycroft struct device *parent;
234 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
235 1.26 cgd void *match;
236 1.26 cgd #else
237 1.26 cgd struct cfdata *cf;
238 1.26 cgd #endif
239 1.26 cgd void *aux;
240 1.6 mycroft {
241 1.26 cgd #ifdef __BROKEN_INDIRECT_CONFIG
242 1.6 mycroft struct cfdata *cf = match;
243 1.26 cgd #endif
244 1.6 mycroft struct pci_attach_args *pa = aux;
245 1.6 mycroft
246 1.14 cgd if (cf->pcicf_dev != PCI_UNK_DEV &&
247 1.14 cgd cf->pcicf_dev != pa->pa_device)
248 1.6 mycroft return 0;
249 1.14 cgd if (cf->pcicf_function != PCI_UNK_FUNCTION &&
250 1.14 cgd cf->pcicf_function != pa->pa_function)
251 1.6 mycroft return 0;
252 1.26 cgd return ((*cf->cf_attach->ca_match)(parent, cf, aux));
253 1.18 cgd }
254 1.18 cgd
255 1.18 cgd int
256 1.18 cgd pci_io_find(pc, pcitag, reg, iobasep, iosizep)
257 1.18 cgd pci_chipset_tag_t pc;
258 1.18 cgd pcitag_t pcitag;
259 1.18 cgd int reg;
260 1.24 thorpej bus_addr_t *iobasep;
261 1.24 thorpej bus_size_t *iosizep;
262 1.18 cgd {
263 1.18 cgd pcireg_t addrdata, sizedata;
264 1.18 cgd int s;
265 1.18 cgd
266 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
267 1.18 cgd panic("pci_io_find: bad request");
268 1.18 cgd
269 1.18 cgd /* XXX?
270 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
271 1.18 cgd *
272 1.18 cgd * 1) The builtin software should have already mapped the device in a
273 1.18 cgd * reasonable way.
274 1.18 cgd *
275 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
276 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
277 1.18 cgd * what we get back.
278 1.18 cgd */
279 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
280 1.18 cgd
281 1.18 cgd s = splhigh();
282 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
283 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
284 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
285 1.18 cgd splx(s);
286 1.18 cgd
287 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) != PCI_MAPREG_TYPE_IO)
288 1.18 cgd panic("pci_io_find: not an I/O region");
289 1.18 cgd
290 1.18 cgd if (iobasep != NULL)
291 1.18 cgd *iobasep = PCI_MAPREG_IO_ADDR(addrdata);
292 1.18 cgd if (iosizep != NULL)
293 1.20 mycroft *iosizep = PCI_MAPREG_IO_SIZE(sizedata);
294 1.18 cgd
295 1.18 cgd return (0);
296 1.18 cgd }
297 1.18 cgd
298 1.18 cgd int
299 1.18 cgd pci_mem_find(pc, pcitag, reg, membasep, memsizep, cacheablep)
300 1.18 cgd pci_chipset_tag_t pc;
301 1.18 cgd pcitag_t pcitag;
302 1.18 cgd int reg;
303 1.24 thorpej bus_addr_t *membasep;
304 1.24 thorpej bus_size_t *memsizep;
305 1.18 cgd int *cacheablep;
306 1.18 cgd {
307 1.18 cgd pcireg_t addrdata, sizedata;
308 1.18 cgd int s;
309 1.18 cgd
310 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
311 1.18 cgd panic("pci_find_mem: bad request");
312 1.18 cgd
313 1.18 cgd /*
314 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
315 1.18 cgd *
316 1.18 cgd * 1) The builtin software should have already mapped the device in a
317 1.18 cgd * reasonable way.
318 1.18 cgd *
319 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
320 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
321 1.18 cgd * what we get back.
322 1.18 cgd */
323 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
324 1.18 cgd
325 1.18 cgd s = splhigh();
326 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
327 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
328 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
329 1.18 cgd splx(s);
330 1.18 cgd
331 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) == PCI_MAPREG_TYPE_IO)
332 1.18 cgd panic("pci_find_mem: I/O region");
333 1.18 cgd
334 1.18 cgd switch (PCI_MAPREG_MEM_TYPE(addrdata)) {
335 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT:
336 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT_1M:
337 1.18 cgd break;
338 1.18 cgd case PCI_MAPREG_MEM_TYPE_64BIT:
339 1.23 christos /* XXX */ printf("pci_find_mem: 64-bit region\n");
340 1.18 cgd /* XXX */ return (1);
341 1.18 cgd default:
342 1.23 christos printf("pci_find_mem: reserved region type\n");
343 1.18 cgd return (1);
344 1.18 cgd }
345 1.18 cgd
346 1.18 cgd if (membasep != NULL)
347 1.18 cgd *membasep = PCI_MAPREG_MEM_ADDR(addrdata); /* PCI addr */
348 1.18 cgd if (memsizep != NULL)
349 1.20 mycroft *memsizep = PCI_MAPREG_MEM_SIZE(sizedata);
350 1.18 cgd if (cacheablep != NULL)
351 1.18 cgd *cacheablep = PCI_MAPREG_MEM_CACHEABLE(addrdata);
352 1.18 cgd
353 1.18 cgd return 0;
354 1.25 cgd }
355 1.25 cgd
356 1.25 cgd void
357 1.25 cgd set_pci_isa_bridge_callback(fn, arg)
358 1.25 cgd void (*fn) __P((void *));
359 1.25 cgd void *arg;
360 1.25 cgd {
361 1.25 cgd
362 1.25 cgd if (pci_isa_bridge_callback != NULL)
363 1.25 cgd panic("set_pci_isa_bridge_callback");
364 1.25 cgd pci_isa_bridge_callback = fn;
365 1.25 cgd pci_isa_bridge_callback_arg = arg;
366 1.1 mycroft }
367