pci.c revision 1.18 1 1.18 cgd /* $NetBSD: pci.c,v 1.18 1996/03/27 04:08:24 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.10 cgd int pcimatch __P((struct device *, void *, void *));
45 1.10 cgd void pciattach __P((struct device *, struct device *, void *));
46 1.10 cgd
47 1.16 thorpej struct cfattach pci_ca = {
48 1.16 thorpej sizeof(struct device), pcimatch, pciattach
49 1.16 thorpej };
50 1.16 thorpej
51 1.16 thorpej struct cfdriver pci_cd = {
52 1.16 thorpej NULL, "pci", DV_DULL
53 1.10 cgd };
54 1.10 cgd
55 1.10 cgd int pciprint __P((void *, char *));
56 1.10 cgd int pcisubmatch __P((struct device *, void *, void *));
57 1.10 cgd
58 1.10 cgd int
59 1.10 cgd pcimatch(parent, match, aux)
60 1.10 cgd struct device *parent;
61 1.10 cgd void *match, *aux;
62 1.10 cgd {
63 1.10 cgd struct cfdata *cf = match;
64 1.10 cgd struct pcibus_attach_args *pba = aux;
65 1.10 cgd
66 1.10 cgd if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
67 1.10 cgd return (0);
68 1.10 cgd
69 1.10 cgd /* Check the locators */
70 1.14 cgd if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
71 1.14 cgd cf->pcibuscf_bus != pba->pba_bus)
72 1.10 cgd return (0);
73 1.10 cgd
74 1.10 cgd /* sanity */
75 1.10 cgd if (pba->pba_bus < 0 || pba->pba_bus > 255)
76 1.10 cgd return (0);
77 1.10 cgd
78 1.10 cgd /*
79 1.10 cgd * XXX check other (hardware?) indicators
80 1.10 cgd */
81 1.10 cgd
82 1.10 cgd return 1;
83 1.10 cgd }
84 1.10 cgd
85 1.10 cgd void
86 1.10 cgd pciattach(parent, self, aux)
87 1.10 cgd struct device *parent, *self;
88 1.10 cgd void *aux;
89 1.10 cgd {
90 1.10 cgd struct pcibus_attach_args *pba = aux;
91 1.13 cgd bus_chipset_tag_t bc;
92 1.18 cgd pci_chipset_tag_t pc;
93 1.18 cgd int bus, device, maxndevs, function, nfunctions;
94 1.10 cgd
95 1.18 cgd pci_attach_hook(parent, self, pba);
96 1.14 cgd printf("\n");
97 1.10 cgd
98 1.18 cgd bc = pba->pba_bc;
99 1.18 cgd pc = pba->pba_pc;
100 1.18 cgd bus = pba->pba_bus;
101 1.18 cgd maxndevs = pci_bus_maxdevs(pc, bus);
102 1.18 cgd
103 1.18 cgd for (device = 0; device < maxndevs; device++) {
104 1.10 cgd pcitag_t tag;
105 1.18 cgd pcireg_t id, class, intr, bhlcr;
106 1.10 cgd struct pci_attach_args pa;
107 1.10 cgd struct cfdata *cf;
108 1.18 cgd int supported, pin;
109 1.10 cgd
110 1.18 cgd tag = pci_make_tag(pc, bus, device, 0);
111 1.18 cgd id = pci_conf_read(pc, tag, PCI_ID_REG);
112 1.10 cgd if (id == 0 || id == 0xffffffff)
113 1.10 cgd continue;
114 1.10 cgd
115 1.18 cgd bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
116 1.17 cgd nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
117 1.10 cgd
118 1.10 cgd for (function = 0; function < nfunctions; function++) {
119 1.18 cgd tag = pci_make_tag(pc, bus, device, function);
120 1.18 cgd id = pci_conf_read(pc, tag, PCI_ID_REG);
121 1.10 cgd if (id == 0 || id == 0xffffffff)
122 1.10 cgd continue;
123 1.18 cgd class = pci_conf_read(pc, tag, PCI_CLASS_REG);
124 1.18 cgd intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
125 1.10 cgd
126 1.18 cgd pa.pa_bc = bc;
127 1.18 cgd pa.pa_pc = pc;
128 1.10 cgd pa.pa_device = device;
129 1.10 cgd pa.pa_function = function;
130 1.10 cgd pa.pa_tag = tag;
131 1.10 cgd pa.pa_id = id;
132 1.10 cgd pa.pa_class = class;
133 1.10 cgd
134 1.18 cgd if (bus == 0) {
135 1.18 cgd pa.pa_intrswiz = 0;
136 1.18 cgd pa.pa_intrtag = tag;
137 1.18 cgd } else {
138 1.18 cgd pa.pa_intrswiz = pba->pba_intrswiz + device;
139 1.18 cgd pa.pa_intrtag = pba->pba_intrtag;
140 1.18 cgd }
141 1.18 cgd pin = PCI_INTERRUPT_PIN(intr);
142 1.18 cgd if (pin == PCI_INTERRUPT_PIN_NONE) {
143 1.18 cgd /* no interrupt */
144 1.18 cgd pa.pa_intrpin = 0;
145 1.18 cgd } else {
146 1.18 cgd /*
147 1.18 cgd * swizzle it based on the number of
148 1.18 cgd * busses we're behind and our device
149 1.18 cgd * number.
150 1.18 cgd */
151 1.18 cgd pa.pa_intrpin = /* XXX */
152 1.18 cgd ((pin + pa.pa_intrswiz - 1) % 4) + 1;
153 1.18 cgd }
154 1.18 cgd pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
155 1.18 cgd
156 1.10 cgd config_found_sm(self, &pa, pciprint, pcisubmatch);
157 1.10 cgd }
158 1.10 cgd }
159 1.10 cgd }
160 1.1 mycroft
161 1.1 mycroft int
162 1.10 cgd pciprint(aux, pnp)
163 1.1 mycroft void *aux;
164 1.10 cgd char *pnp;
165 1.1 mycroft {
166 1.1 mycroft register struct pci_attach_args *pa = aux;
167 1.10 cgd char devinfo[256];
168 1.1 mycroft
169 1.10 cgd if (pnp) {
170 1.10 cgd pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
171 1.10 cgd printf("%s at %s", devinfo, pnp);
172 1.10 cgd }
173 1.10 cgd printf(" dev %d function %d", pa->pa_device, pa->pa_function);
174 1.6 mycroft return (UNCONF);
175 1.6 mycroft }
176 1.6 mycroft
177 1.6 mycroft int
178 1.6 mycroft pcisubmatch(parent, match, aux)
179 1.6 mycroft struct device *parent;
180 1.6 mycroft void *match, *aux;
181 1.6 mycroft {
182 1.6 mycroft struct cfdata *cf = match;
183 1.6 mycroft struct pci_attach_args *pa = aux;
184 1.6 mycroft
185 1.14 cgd if (cf->pcicf_dev != PCI_UNK_DEV &&
186 1.14 cgd cf->pcicf_dev != pa->pa_device)
187 1.6 mycroft return 0;
188 1.14 cgd if (cf->pcicf_function != PCI_UNK_FUNCTION &&
189 1.14 cgd cf->pcicf_function != pa->pa_function)
190 1.6 mycroft return 0;
191 1.16 thorpej return ((*cf->cf_attach->ca_match)(parent, match, aux));
192 1.18 cgd }
193 1.18 cgd
194 1.18 cgd int
195 1.18 cgd pci_io_find(pc, pcitag, reg, iobasep, iosizep)
196 1.18 cgd pci_chipset_tag_t pc;
197 1.18 cgd pcitag_t pcitag;
198 1.18 cgd int reg;
199 1.18 cgd bus_io_addr_t *iobasep;
200 1.18 cgd bus_io_size_t *iosizep;
201 1.18 cgd {
202 1.18 cgd pcireg_t addrdata, sizedata;
203 1.18 cgd int s;
204 1.18 cgd
205 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
206 1.18 cgd panic("pci_io_find: bad request");
207 1.18 cgd
208 1.18 cgd /* XXX?
209 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
210 1.18 cgd *
211 1.18 cgd * 1) The builtin software should have already mapped the device in a
212 1.18 cgd * reasonable way.
213 1.18 cgd *
214 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
215 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
216 1.18 cgd * what we get back.
217 1.18 cgd */
218 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
219 1.18 cgd
220 1.18 cgd s = splhigh();
221 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
222 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
223 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
224 1.18 cgd splx(s);
225 1.18 cgd
226 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) != PCI_MAPREG_TYPE_IO)
227 1.18 cgd panic("pci_io_find: not an I/O region");
228 1.18 cgd
229 1.18 cgd if (iobasep != NULL)
230 1.18 cgd *iobasep = PCI_MAPREG_IO_ADDR(addrdata);
231 1.18 cgd if (iosizep != NULL)
232 1.18 cgd *iosizep = ~PCI_MAPREG_IO_ADDR(sizedata) + 1;
233 1.18 cgd
234 1.18 cgd return (0);
235 1.18 cgd }
236 1.18 cgd
237 1.18 cgd int
238 1.18 cgd pci_mem_find(pc, pcitag, reg, membasep, memsizep, cacheablep)
239 1.18 cgd pci_chipset_tag_t pc;
240 1.18 cgd pcitag_t pcitag;
241 1.18 cgd int reg;
242 1.18 cgd bus_mem_addr_t *membasep;
243 1.18 cgd bus_mem_size_t *memsizep;
244 1.18 cgd int *cacheablep;
245 1.18 cgd {
246 1.18 cgd pcireg_t addrdata, sizedata;
247 1.18 cgd int s;
248 1.18 cgd
249 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
250 1.18 cgd panic("pci_find_mem: bad request");
251 1.18 cgd
252 1.18 cgd /*
253 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
254 1.18 cgd *
255 1.18 cgd * 1) The builtin software should have already mapped the device in a
256 1.18 cgd * reasonable way.
257 1.18 cgd *
258 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
259 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
260 1.18 cgd * what we get back.
261 1.18 cgd */
262 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
263 1.18 cgd
264 1.18 cgd s = splhigh();
265 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
266 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
267 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
268 1.18 cgd splx(s);
269 1.18 cgd
270 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) == PCI_MAPREG_TYPE_IO)
271 1.18 cgd panic("pci_find_mem: I/O region");
272 1.18 cgd
273 1.18 cgd switch (PCI_MAPREG_MEM_TYPE(addrdata)) {
274 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT:
275 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT_1M:
276 1.18 cgd break;
277 1.18 cgd case PCI_MAPREG_MEM_TYPE_64BIT:
278 1.18 cgd /* XXX */ printf("pci_find_mem: 64-bit region\n");
279 1.18 cgd /* XXX */ return (1);
280 1.18 cgd default:
281 1.18 cgd printf("pci_find_mem: reserved region type\n");
282 1.18 cgd return (1);
283 1.18 cgd }
284 1.18 cgd
285 1.18 cgd if (membasep != NULL)
286 1.18 cgd *membasep = PCI_MAPREG_MEM_ADDR(addrdata); /* PCI addr */
287 1.18 cgd if (memsizep != NULL)
288 1.18 cgd *memsizep = ~PCI_MAPREG_MEM_ADDR(sizedata) + 1;
289 1.18 cgd if (cacheablep != NULL)
290 1.18 cgd *cacheablep = PCI_MAPREG_MEM_CACHEABLE(addrdata);
291 1.18 cgd
292 1.18 cgd return 0;
293 1.1 mycroft }
294