pci.c revision 1.24 1 1.24 thorpej /* $NetBSD: pci.c,v 1.24 1996/10/21 22:56:55 thorpej 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.21 cgd int pciprint __P((void *, const 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.24 thorpej bus_space_tag_t iot, memt;
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.23 christos printf("\n");
97 1.10 cgd
98 1.24 thorpej iot = pba->pba_iot;
99 1.24 thorpej memt = pba->pba_memt;
100 1.18 cgd pc = pba->pba_pc;
101 1.18 cgd bus = pba->pba_bus;
102 1.18 cgd maxndevs = pci_bus_maxdevs(pc, bus);
103 1.18 cgd
104 1.18 cgd for (device = 0; device < maxndevs; device++) {
105 1.10 cgd pcitag_t tag;
106 1.18 cgd pcireg_t id, class, intr, bhlcr;
107 1.10 cgd struct pci_attach_args pa;
108 1.19 christos int 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.24 thorpej pa.pa_iot = iot;
127 1.24 thorpej pa.pa_memt = memt;
128 1.18 cgd pa.pa_pc = pc;
129 1.10 cgd pa.pa_device = device;
130 1.10 cgd pa.pa_function = function;
131 1.10 cgd pa.pa_tag = tag;
132 1.10 cgd pa.pa_id = id;
133 1.10 cgd pa.pa_class = class;
134 1.10 cgd
135 1.18 cgd if (bus == 0) {
136 1.18 cgd pa.pa_intrswiz = 0;
137 1.18 cgd pa.pa_intrtag = tag;
138 1.18 cgd } else {
139 1.18 cgd pa.pa_intrswiz = pba->pba_intrswiz + device;
140 1.18 cgd pa.pa_intrtag = pba->pba_intrtag;
141 1.18 cgd }
142 1.18 cgd pin = PCI_INTERRUPT_PIN(intr);
143 1.18 cgd if (pin == PCI_INTERRUPT_PIN_NONE) {
144 1.18 cgd /* no interrupt */
145 1.18 cgd pa.pa_intrpin = 0;
146 1.18 cgd } else {
147 1.18 cgd /*
148 1.18 cgd * swizzle it based on the number of
149 1.18 cgd * busses we're behind and our device
150 1.18 cgd * number.
151 1.18 cgd */
152 1.18 cgd pa.pa_intrpin = /* XXX */
153 1.18 cgd ((pin + pa.pa_intrswiz - 1) % 4) + 1;
154 1.18 cgd }
155 1.18 cgd pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
156 1.18 cgd
157 1.10 cgd config_found_sm(self, &pa, pciprint, pcisubmatch);
158 1.10 cgd }
159 1.10 cgd }
160 1.10 cgd }
161 1.1 mycroft
162 1.1 mycroft int
163 1.10 cgd pciprint(aux, pnp)
164 1.1 mycroft void *aux;
165 1.21 cgd const char *pnp;
166 1.1 mycroft {
167 1.1 mycroft register struct pci_attach_args *pa = aux;
168 1.10 cgd char devinfo[256];
169 1.1 mycroft
170 1.10 cgd if (pnp) {
171 1.10 cgd pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
172 1.23 christos printf("%s at %s", devinfo, pnp);
173 1.10 cgd }
174 1.23 christos printf(" dev %d function %d", pa->pa_device, pa->pa_function);
175 1.6 mycroft return (UNCONF);
176 1.6 mycroft }
177 1.6 mycroft
178 1.6 mycroft int
179 1.6 mycroft pcisubmatch(parent, match, aux)
180 1.6 mycroft struct device *parent;
181 1.6 mycroft void *match, *aux;
182 1.6 mycroft {
183 1.6 mycroft struct cfdata *cf = match;
184 1.6 mycroft struct pci_attach_args *pa = aux;
185 1.6 mycroft
186 1.14 cgd if (cf->pcicf_dev != PCI_UNK_DEV &&
187 1.14 cgd cf->pcicf_dev != pa->pa_device)
188 1.6 mycroft return 0;
189 1.14 cgd if (cf->pcicf_function != PCI_UNK_FUNCTION &&
190 1.14 cgd cf->pcicf_function != pa->pa_function)
191 1.6 mycroft return 0;
192 1.16 thorpej return ((*cf->cf_attach->ca_match)(parent, match, aux));
193 1.18 cgd }
194 1.18 cgd
195 1.18 cgd int
196 1.18 cgd pci_io_find(pc, pcitag, reg, iobasep, iosizep)
197 1.18 cgd pci_chipset_tag_t pc;
198 1.18 cgd pcitag_t pcitag;
199 1.18 cgd int reg;
200 1.24 thorpej bus_addr_t *iobasep;
201 1.24 thorpej bus_size_t *iosizep;
202 1.18 cgd {
203 1.18 cgd pcireg_t addrdata, sizedata;
204 1.18 cgd int s;
205 1.18 cgd
206 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
207 1.18 cgd panic("pci_io_find: bad request");
208 1.18 cgd
209 1.18 cgd /* XXX?
210 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
211 1.18 cgd *
212 1.18 cgd * 1) The builtin software should have already mapped the device in a
213 1.18 cgd * reasonable way.
214 1.18 cgd *
215 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
216 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
217 1.18 cgd * what we get back.
218 1.18 cgd */
219 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
220 1.18 cgd
221 1.18 cgd s = splhigh();
222 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
223 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
224 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
225 1.18 cgd splx(s);
226 1.18 cgd
227 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) != PCI_MAPREG_TYPE_IO)
228 1.18 cgd panic("pci_io_find: not an I/O region");
229 1.18 cgd
230 1.18 cgd if (iobasep != NULL)
231 1.18 cgd *iobasep = PCI_MAPREG_IO_ADDR(addrdata);
232 1.18 cgd if (iosizep != NULL)
233 1.20 mycroft *iosizep = PCI_MAPREG_IO_SIZE(sizedata);
234 1.18 cgd
235 1.18 cgd return (0);
236 1.18 cgd }
237 1.18 cgd
238 1.18 cgd int
239 1.18 cgd pci_mem_find(pc, pcitag, reg, membasep, memsizep, cacheablep)
240 1.18 cgd pci_chipset_tag_t pc;
241 1.18 cgd pcitag_t pcitag;
242 1.18 cgd int reg;
243 1.24 thorpej bus_addr_t *membasep;
244 1.24 thorpej bus_size_t *memsizep;
245 1.18 cgd int *cacheablep;
246 1.18 cgd {
247 1.18 cgd pcireg_t addrdata, sizedata;
248 1.18 cgd int s;
249 1.18 cgd
250 1.18 cgd if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
251 1.18 cgd panic("pci_find_mem: bad request");
252 1.18 cgd
253 1.18 cgd /*
254 1.18 cgd * Section 6.2.5.1, `Address Maps', tells us that:
255 1.18 cgd *
256 1.18 cgd * 1) The builtin software should have already mapped the device in a
257 1.18 cgd * reasonable way.
258 1.18 cgd *
259 1.18 cgd * 2) A device which wants 2^n bytes of memory will hardwire the bottom
260 1.18 cgd * n bits of the address to 0. As recommended, we write all 1s and see
261 1.18 cgd * what we get back.
262 1.18 cgd */
263 1.18 cgd addrdata = pci_conf_read(pc, pcitag, reg);
264 1.18 cgd
265 1.18 cgd s = splhigh();
266 1.18 cgd pci_conf_write(pc, pcitag, reg, 0xffffffff);
267 1.18 cgd sizedata = pci_conf_read(pc, pcitag, reg);
268 1.18 cgd pci_conf_write(pc, pcitag, reg, addrdata);
269 1.18 cgd splx(s);
270 1.18 cgd
271 1.18 cgd if (PCI_MAPREG_TYPE(addrdata) == PCI_MAPREG_TYPE_IO)
272 1.18 cgd panic("pci_find_mem: I/O region");
273 1.18 cgd
274 1.18 cgd switch (PCI_MAPREG_MEM_TYPE(addrdata)) {
275 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT:
276 1.18 cgd case PCI_MAPREG_MEM_TYPE_32BIT_1M:
277 1.18 cgd break;
278 1.18 cgd case PCI_MAPREG_MEM_TYPE_64BIT:
279 1.23 christos /* XXX */ printf("pci_find_mem: 64-bit region\n");
280 1.18 cgd /* XXX */ return (1);
281 1.18 cgd default:
282 1.23 christos printf("pci_find_mem: reserved region type\n");
283 1.18 cgd return (1);
284 1.18 cgd }
285 1.18 cgd
286 1.18 cgd if (membasep != NULL)
287 1.18 cgd *membasep = PCI_MAPREG_MEM_ADDR(addrdata); /* PCI addr */
288 1.18 cgd if (memsizep != NULL)
289 1.20 mycroft *memsizep = PCI_MAPREG_MEM_SIZE(sizedata);
290 1.18 cgd if (cacheablep != NULL)
291 1.18 cgd *cacheablep = PCI_MAPREG_MEM_CACHEABLE(addrdata);
292 1.18 cgd
293 1.18 cgd return 0;
294 1.1 mycroft }
295