pci_machdep.c revision 1.11 1 1.11 cgd /* $NetBSD: pci_machdep.c,v 1.11 2000/06/04 19:14:49 cgd Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.5 mycroft * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.5 mycroft * This product includes software developed by Charles M. Hannum.
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission.
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 tsubai * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.1 tsubai
33 1.1 tsubai /*
34 1.1 tsubai * Machine-specific functions for PCI autoconfiguration.
35 1.1 tsubai *
36 1.1 tsubai * On PCs, there are two methods of generating PCI configuration cycles.
37 1.1 tsubai * We try to detect the appropriate mechanism for this machine and set
38 1.1 tsubai * up a few function pointers to access the correct method directly.
39 1.1 tsubai *
40 1.1 tsubai * The configuration method can be hard-coded in the config file by
41 1.1 tsubai * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42 1.1 tsubai * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43 1.1 tsubai */
44 1.1 tsubai
45 1.1 tsubai #include <sys/types.h>
46 1.1 tsubai #include <sys/param.h>
47 1.1 tsubai #include <sys/time.h>
48 1.1 tsubai #include <sys/systm.h>
49 1.1 tsubai #include <sys/errno.h>
50 1.1 tsubai #include <sys/device.h>
51 1.1 tsubai
52 1.1 tsubai #include <vm/vm.h>
53 1.1 tsubai #include <vm/vm_kern.h>
54 1.1 tsubai
55 1.3 tsubai #define _MACPPC_BUS_DMA_PRIVATE
56 1.3 tsubai #include <machine/bus.h>
57 1.3 tsubai
58 1.1 tsubai #include <machine/bus.h>
59 1.1 tsubai #include <machine/pio.h>
60 1.1 tsubai #include <machine/intr.h>
61 1.1 tsubai
62 1.1 tsubai #include <dev/pci/pcivar.h>
63 1.1 tsubai #include <dev/pci/pcireg.h>
64 1.1 tsubai
65 1.10 tsubai #include <dev/ofw/openfirm.h>
66 1.10 tsubai #include <dev/ofw/ofw_pci.h>
67 1.10 tsubai
68 1.10 tsubai static void fixpci __P((int, pci_chipset_tag_t));
69 1.10 tsubai static int find_node_intr __P((int, u_int32_t *, u_int32_t *));
70 1.10 tsubai
71 1.3 tsubai /*
72 1.3 tsubai * PCI doesn't have any special needs; just use the generic versions
73 1.3 tsubai * of these functions.
74 1.3 tsubai */
75 1.3 tsubai struct macppc_bus_dma_tag pci_bus_dma_tag = {
76 1.3 tsubai 0, /* _bounce_thresh */
77 1.3 tsubai _bus_dmamap_create,
78 1.3 tsubai _bus_dmamap_destroy,
79 1.3 tsubai _bus_dmamap_load,
80 1.3 tsubai _bus_dmamap_load_mbuf,
81 1.3 tsubai _bus_dmamap_load_uio,
82 1.3 tsubai _bus_dmamap_load_raw,
83 1.3 tsubai _bus_dmamap_unload,
84 1.3 tsubai NULL, /* _dmamap_sync */
85 1.3 tsubai _bus_dmamem_alloc,
86 1.3 tsubai _bus_dmamem_free,
87 1.3 tsubai _bus_dmamem_map,
88 1.3 tsubai _bus_dmamem_unmap,
89 1.3 tsubai _bus_dmamem_mmap,
90 1.3 tsubai };
91 1.1 tsubai
92 1.1 tsubai void
93 1.1 tsubai pci_attach_hook(parent, self, pba)
94 1.1 tsubai struct device *parent, *self;
95 1.1 tsubai struct pcibus_attach_args *pba;
96 1.1 tsubai {
97 1.10 tsubai pci_chipset_tag_t pc = pba->pba_pc;
98 1.10 tsubai int bus = pba->pba_bus;
99 1.10 tsubai int node, nn, sz;
100 1.10 tsubai int32_t busrange[2];
101 1.10 tsubai
102 1.10 tsubai for (node = pc->node; node; node = nn) {
103 1.10 tsubai sz = OF_getprop(node, "bus-range", busrange, 8);
104 1.10 tsubai if (sz == 8 && busrange[0] == bus) {
105 1.10 tsubai fixpci(node, pc);
106 1.10 tsubai return;
107 1.10 tsubai }
108 1.10 tsubai if ((nn = OF_child(node)) != 0)
109 1.10 tsubai continue;
110 1.10 tsubai while ((nn = OF_peer(node)) == 0) {
111 1.10 tsubai node = OF_parent(node);
112 1.10 tsubai if (node == pc->node)
113 1.10 tsubai return; /* not found */
114 1.10 tsubai }
115 1.10 tsubai }
116 1.1 tsubai }
117 1.1 tsubai
118 1.1 tsubai int
119 1.1 tsubai pci_bus_maxdevs(pc, busno)
120 1.1 tsubai pci_chipset_tag_t pc;
121 1.1 tsubai int busno;
122 1.1 tsubai {
123 1.1 tsubai
124 1.1 tsubai /*
125 1.1 tsubai * Bus number is irrelevant. Configuration Mechanism 1 is in
126 1.1 tsubai * use, can have devices 0-32 (i.e. the `normal' range).
127 1.1 tsubai */
128 1.3 tsubai return 32;
129 1.1 tsubai }
130 1.1 tsubai
131 1.1 tsubai pcitag_t
132 1.1 tsubai pci_make_tag(pc, bus, device, function)
133 1.1 tsubai pci_chipset_tag_t pc;
134 1.1 tsubai int bus, device, function;
135 1.1 tsubai {
136 1.1 tsubai pcitag_t tag;
137 1.1 tsubai
138 1.1 tsubai if (bus >= 256 || device >= 32 || function >= 8)
139 1.1 tsubai panic("pci_make_tag: bad request");
140 1.1 tsubai
141 1.9 thorpej /* XXX magic number */
142 1.3 tsubai tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
143 1.1 tsubai
144 1.1 tsubai return tag;
145 1.1 tsubai }
146 1.1 tsubai
147 1.1 tsubai void
148 1.1 tsubai pci_decompose_tag(pc, tag, bp, dp, fp)
149 1.1 tsubai pci_chipset_tag_t pc;
150 1.1 tsubai pcitag_t tag;
151 1.1 tsubai int *bp, *dp, *fp;
152 1.1 tsubai {
153 1.9 thorpej
154 1.3 tsubai if (bp != NULL)
155 1.3 tsubai *bp = (tag >> 16) & 0xff;
156 1.3 tsubai if (dp != NULL)
157 1.3 tsubai *dp = (tag >> 11) & 0x1f;
158 1.3 tsubai if (fp != NULL)
159 1.9 thorpej *fp = (tag >> 8) & 0x07;
160 1.1 tsubai }
161 1.1 tsubai
162 1.1 tsubai pcireg_t
163 1.1 tsubai pci_conf_read(pc, tag, reg)
164 1.1 tsubai pci_chipset_tag_t pc;
165 1.1 tsubai pcitag_t tag;
166 1.1 tsubai int reg;
167 1.1 tsubai {
168 1.2 tsubai
169 1.10 tsubai return (*pc->conf_read)(pc, tag, reg);
170 1.1 tsubai }
171 1.1 tsubai
172 1.1 tsubai void
173 1.1 tsubai pci_conf_write(pc, tag, reg, data)
174 1.1 tsubai pci_chipset_tag_t pc;
175 1.1 tsubai pcitag_t tag;
176 1.1 tsubai int reg;
177 1.1 tsubai pcireg_t data;
178 1.1 tsubai {
179 1.2 tsubai
180 1.10 tsubai (*pc->conf_write)(pc, tag, reg, data);
181 1.1 tsubai }
182 1.1 tsubai
183 1.1 tsubai int
184 1.1 tsubai pci_intr_map(pc, intrtag, pin, line, ihp)
185 1.1 tsubai pci_chipset_tag_t pc;
186 1.1 tsubai pcitag_t intrtag;
187 1.1 tsubai int pin, line;
188 1.1 tsubai pci_intr_handle_t *ihp;
189 1.1 tsubai {
190 1.1 tsubai
191 1.1 tsubai if (pin == 0) {
192 1.1 tsubai /* No IRQ used. */
193 1.1 tsubai goto bad;
194 1.1 tsubai }
195 1.1 tsubai
196 1.1 tsubai if (pin > 4) {
197 1.1 tsubai printf("pci_intr_map: bad interrupt pin %d\n", pin);
198 1.1 tsubai goto bad;
199 1.1 tsubai }
200 1.1 tsubai
201 1.1 tsubai /*
202 1.1 tsubai * Section 6.2.4, `Miscellaneous Functions', says that 255 means
203 1.1 tsubai * `unknown' or `no connection' on a PC. We assume that a device with
204 1.1 tsubai * `no connection' either doesn't have an interrupt (in which case the
205 1.1 tsubai * pin number should be 0, and would have been noticed above), or
206 1.1 tsubai * wasn't configured by the BIOS (in which case we punt, since there's
207 1.1 tsubai * no real way we can know how the interrupt lines are mapped in the
208 1.1 tsubai * hardware).
209 1.1 tsubai *
210 1.1 tsubai * XXX
211 1.1 tsubai * Since IRQ 0 is only used by the clock, and we can't actually be sure
212 1.1 tsubai * that the BIOS did its job, we also recognize that as meaning that
213 1.1 tsubai * the BIOS has not configured the device.
214 1.1 tsubai */
215 1.1 tsubai if (line == 0 || line == 255) {
216 1.1 tsubai printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
217 1.1 tsubai goto bad;
218 1.1 tsubai } else {
219 1.1 tsubai if (line >= ICU_LEN) {
220 1.1 tsubai printf("pci_intr_map: bad interrupt line %d\n", line);
221 1.1 tsubai goto bad;
222 1.1 tsubai }
223 1.1 tsubai }
224 1.1 tsubai
225 1.1 tsubai *ihp = line;
226 1.1 tsubai return 0;
227 1.1 tsubai
228 1.1 tsubai bad:
229 1.1 tsubai *ihp = -1;
230 1.1 tsubai return 1;
231 1.1 tsubai }
232 1.1 tsubai
233 1.1 tsubai const char *
234 1.1 tsubai pci_intr_string(pc, ih)
235 1.1 tsubai pci_chipset_tag_t pc;
236 1.1 tsubai pci_intr_handle_t ih;
237 1.1 tsubai {
238 1.1 tsubai static char irqstr[8]; /* 4 + 2 + NULL + sanity */
239 1.1 tsubai
240 1.1 tsubai if (ih == 0 || ih >= ICU_LEN)
241 1.1 tsubai panic("pci_intr_string: bogus handle 0x%x\n", ih);
242 1.1 tsubai
243 1.1 tsubai sprintf(irqstr, "irq %d", ih);
244 1.1 tsubai return (irqstr);
245 1.1 tsubai
246 1.11 cgd }
247 1.11 cgd
248 1.11 cgd const struct evcnt *
249 1.11 cgd pci_intr_evcnt(pc, ih)
250 1.11 cgd pci_chipset_tag_t pc;
251 1.11 cgd pci_intr_handle_t ih;
252 1.11 cgd {
253 1.11 cgd
254 1.11 cgd /* XXX for now, no evcnt parent reported */
255 1.11 cgd return NULL;
256 1.1 tsubai }
257 1.1 tsubai
258 1.1 tsubai extern void * intr_establish();
259 1.1 tsubai extern void intr_disestablish();
260 1.1 tsubai
261 1.1 tsubai void *
262 1.1 tsubai pci_intr_establish(pc, ih, level, func, arg)
263 1.1 tsubai pci_chipset_tag_t pc;
264 1.1 tsubai pci_intr_handle_t ih;
265 1.1 tsubai int level, (*func) __P((void *));
266 1.1 tsubai void *arg;
267 1.1 tsubai {
268 1.1 tsubai
269 1.1 tsubai if (ih == 0 || ih >= ICU_LEN)
270 1.1 tsubai panic("pci_intr_establish: bogus handle 0x%x\n", ih);
271 1.1 tsubai
272 1.1 tsubai return intr_establish(ih, IST_LEVEL, level, func, arg);
273 1.1 tsubai }
274 1.1 tsubai
275 1.1 tsubai void
276 1.1 tsubai pci_intr_disestablish(pc, cookie)
277 1.1 tsubai pci_chipset_tag_t pc;
278 1.1 tsubai void *cookie;
279 1.1 tsubai {
280 1.1 tsubai
281 1.1 tsubai intr_disestablish(cookie);
282 1.10 tsubai }
283 1.10 tsubai
284 1.10 tsubai #define pcibus(x) \
285 1.10 tsubai (((x) & OFW_PCI_PHYS_HI_BUSMASK) >> OFW_PCI_PHYS_HI_BUSSHIFT)
286 1.10 tsubai #define pcidev(x) \
287 1.10 tsubai (((x) & OFW_PCI_PHYS_HI_DEVICEMASK) >> OFW_PCI_PHYS_HI_DEVICESHIFT)
288 1.10 tsubai #define pcifunc(x) \
289 1.10 tsubai (((x) & OFW_PCI_PHYS_HI_FUNCTIONMASK) >> OFW_PCI_PHYS_HI_FUNCTIONSHIFT)
290 1.10 tsubai
291 1.10 tsubai void
292 1.10 tsubai fixpci(parent, pc)
293 1.10 tsubai int parent;
294 1.10 tsubai pci_chipset_tag_t pc;
295 1.10 tsubai {
296 1.10 tsubai int node;
297 1.10 tsubai pcitag_t tag;
298 1.10 tsubai pcireg_t csr, intr;
299 1.10 tsubai int len, i;
300 1.10 tsubai int32_t irqs[4];
301 1.10 tsubai struct {
302 1.10 tsubai u_int32_t phys_hi, phys_mid, phys_lo;
303 1.10 tsubai u_int32_t size_hi, size_lo;
304 1.10 tsubai } addr[8];
305 1.10 tsubai
306 1.10 tsubai for (node = OF_child(parent); node; node = OF_peer(node)) {
307 1.10 tsubai len = OF_getprop(node, "assigned-addresses", addr,
308 1.10 tsubai sizeof(addr));
309 1.10 tsubai if (len < (int)sizeof(addr[0]))
310 1.10 tsubai continue;
311 1.10 tsubai
312 1.10 tsubai tag = pci_make_tag(pc, pcibus(addr[0].phys_hi),
313 1.10 tsubai pcidev(addr[0].phys_hi),
314 1.10 tsubai pcifunc(addr[0].phys_hi));
315 1.10 tsubai
316 1.10 tsubai /*
317 1.10 tsubai * Make sure the IO and MEM enable bits are set in the CSR.
318 1.10 tsubai */
319 1.10 tsubai csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
320 1.10 tsubai csr &= ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
321 1.10 tsubai
322 1.10 tsubai for (i = 0; i < len / sizeof(addr[0]); i++) {
323 1.10 tsubai switch (addr[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
324 1.10 tsubai case OFW_PCI_PHYS_HI_SPACE_IO:
325 1.10 tsubai csr |= PCI_COMMAND_IO_ENABLE;
326 1.10 tsubai break;
327 1.10 tsubai
328 1.10 tsubai case OFW_PCI_PHYS_HI_SPACE_MEM32:
329 1.10 tsubai csr |= PCI_COMMAND_MEM_ENABLE;
330 1.10 tsubai break;
331 1.10 tsubai }
332 1.10 tsubai }
333 1.10 tsubai
334 1.10 tsubai pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
335 1.10 tsubai
336 1.10 tsubai /*
337 1.10 tsubai * Make sure the line register is programmed with the
338 1.10 tsubai * interrupt mapping.
339 1.10 tsubai */
340 1.10 tsubai if (find_node_intr(node, &addr[0].phys_hi, irqs) == -1)
341 1.10 tsubai continue;
342 1.10 tsubai
343 1.10 tsubai intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
344 1.10 tsubai intr &= ~PCI_INTERRUPT_LINE_MASK;
345 1.10 tsubai intr |= irqs[0] & PCI_INTERRUPT_LINE_MASK;
346 1.10 tsubai pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
347 1.10 tsubai }
348 1.10 tsubai }
349 1.10 tsubai
350 1.10 tsubai /*
351 1.10 tsubai * Find PCI IRQ of the node from OF tree.
352 1.10 tsubai */
353 1.10 tsubai int
354 1.10 tsubai find_node_intr(node, addr, intr)
355 1.10 tsubai int node;
356 1.10 tsubai u_int32_t *addr, *intr;
357 1.10 tsubai {
358 1.10 tsubai int parent, len, mlen, iparent;
359 1.10 tsubai int match, i;
360 1.10 tsubai u_int32_t map[64], *mp;
361 1.10 tsubai u_int32_t imask[8], maskedaddr[8];
362 1.10 tsubai u_int32_t icells;
363 1.10 tsubai char name[32];
364 1.10 tsubai
365 1.10 tsubai len = OF_getprop(node, "AAPL,interrupts", intr, 4) ;
366 1.10 tsubai if (len == 4)
367 1.10 tsubai return len;
368 1.10 tsubai
369 1.10 tsubai parent = OF_parent(node);
370 1.10 tsubai len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
371 1.10 tsubai mlen = OF_getprop(parent, "interrupt-map-mask", imask, sizeof(imask));
372 1.10 tsubai
373 1.10 tsubai if (len == -1 || mlen == -1)
374 1.10 tsubai goto nomap;
375 1.10 tsubai
376 1.10 tsubai #ifdef DIAGNOSTIC
377 1.10 tsubai if (mlen == sizeof(imask)) {
378 1.10 tsubai printf("interrupt-map too long\n");
379 1.10 tsubai return -1;
380 1.10 tsubai }
381 1.10 tsubai #endif
382 1.10 tsubai
383 1.10 tsubai /* mask addr by "interrupt-map-mask" */
384 1.10 tsubai bcopy(addr, maskedaddr, mlen);
385 1.10 tsubai for (i = 0; i < mlen / 4; i++)
386 1.10 tsubai maskedaddr[i] &= imask[i];
387 1.10 tsubai
388 1.10 tsubai mp = map;
389 1.10 tsubai while (len > mlen) {
390 1.10 tsubai match = bcmp(maskedaddr, mp, mlen);
391 1.10 tsubai mp += mlen / 4;
392 1.10 tsubai len -= mlen;
393 1.10 tsubai
394 1.10 tsubai /*
395 1.10 tsubai * We must read "#interrupt-cells" for each time because
396 1.10 tsubai * interrupt-parent may be defferent.
397 1.10 tsubai *
398 1.10 tsubai * XXX assume #address-cells == 1
399 1.10 tsubai */
400 1.10 tsubai iparent = *mp++;
401 1.10 tsubai len -= 4;
402 1.10 tsubai if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
403 1.10 tsubai return -1;
404 1.10 tsubai
405 1.10 tsubai /* Found. */
406 1.10 tsubai if (match == 0) {
407 1.10 tsubai bcopy(mp, intr, icells * 4);
408 1.10 tsubai return icells * 4;
409 1.10 tsubai }
410 1.10 tsubai
411 1.10 tsubai mp += icells;
412 1.10 tsubai len -= icells * 4;
413 1.10 tsubai }
414 1.10 tsubai
415 1.10 tsubai nomap:
416 1.10 tsubai /*
417 1.10 tsubai * If the node has no interrupt property and the parent is a
418 1.10 tsubai * pci-bridge, use parent's interrupt. This occurs on a PCI
419 1.10 tsubai * slot. (e.g. AHA-3940)
420 1.10 tsubai */
421 1.10 tsubai bzero(name, sizeof(name));
422 1.10 tsubai OF_getprop(parent, "name", name, sizeof(name));
423 1.10 tsubai if (strcmp(name, "pci-bridge") == 0) {
424 1.10 tsubai len = OF_getprop(parent, "AAPL,interrupts", intr, 4) ;
425 1.10 tsubai if (len == 4)
426 1.10 tsubai return len;
427 1.10 tsubai }
428 1.10 tsubai
429 1.10 tsubai /* XXX This may be wrong... */
430 1.10 tsubai len = OF_getprop(node, "interrupts", intr, 4) ;
431 1.10 tsubai if (len == 4)
432 1.10 tsubai return len;
433 1.10 tsubai
434 1.10 tsubai return -1;
435 1.1 tsubai }
436