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