pci_machdep.c revision 1.28.2.4 1 /* $NetBSD: pci_machdep.c,v 1.28.2.4 2008/01/21 09:37:29 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Machine-specific functions for PCI autoconfiguration.
35 *
36 * On PCs, there are two methods of generating PCI configuration cycles.
37 * We try to detect the appropriate mechanism for this machine and set
38 * up a few function pointers to access the correct method directly.
39 *
40 * The configuration method can be hard-coded in the config file by
41 * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42 * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.28.2.4 2008/01/21 09:37:29 yamt Exp $");
47
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/systm.h>
52 #include <sys/errno.h>
53 #include <sys/device.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #define _POWERPC_BUS_DMA_PRIVATE
58 #include <machine/bus.h>
59
60 #include <machine/autoconf.h>
61 #include <machine/intr.h>
62
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/ppbreg.h>
66 #include <dev/pci/pcidevs.h>
67
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofw_pci.h>
70
71 #include "opt_macppc.h"
72
73 static void fixpci(int, pci_chipset_tag_t);
74 static int find_node_intr(int, u_int32_t *, u_int32_t *);
75 static void fix_cardbus_bridge(int, pci_chipset_tag_t, pcitag_t);
76
77 #ifdef PB3400_CARDBUS_HACK
78 int cardbus_number = 2;
79 const char *pb3400_compat[] = {"AAPL,3400/2400", NULL};
80 #endif
81
82 pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
83 void genppc_pci_indirect_decompose_tag(void *, pcitag_t, int *, int *, int *);
84
85 void
86 macppc_pci_attach_hook(parent, self, pba)
87 struct device *parent, *self;
88 struct pcibus_attach_args *pba;
89 {
90 pci_chipset_tag_t pc = pba->pba_pc;
91 int bus = pba->pba_bus;
92 int node, nn, sz;
93 int32_t busrange[2];
94
95 for (node = pc->pc_node; node; node = nn) {
96 sz = OF_getprop(node, "bus-range", busrange, 8);
97 if (sz == 8 && busrange[0] == bus) {
98 fixpci(node, pc);
99 return;
100 }
101 if ((nn = OF_child(node)) != 0)
102 continue;
103 while ((nn = OF_peer(node)) == 0) {
104 node = OF_parent(node);
105 if (node == pc->pc_node)
106 return; /* not found */
107 }
108 }
109 }
110
111 void
112 macppc_pci_get_chipset_tag(pci_chipset_tag_t pc)
113 {
114
115 pc->pc_conf_v = (void *)pc;
116
117 pc->pc_attach_hook = macppc_pci_attach_hook;
118 pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
119
120 pc->pc_make_tag = genppc_pci_indirect_make_tag;
121 pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
122
123 pc->pc_intr_v = (void *)pc;
124
125 pc->pc_intr_map = genppc_pci_intr_map;
126 pc->pc_intr_string = genppc_pci_intr_string;
127 pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
128 pc->pc_intr_establish = genppc_pci_intr_establish;
129 pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
130
131 pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
132 pc->pc_conf_hook = genppc_pci_conf_hook;
133
134 pc->pc_bus = 0;
135 pc->pc_node = 0;
136 pc->pc_memt = 0;
137 pc->pc_iot = 0;
138 }
139
140 #define pcibus(x) \
141 (((x) & OFW_PCI_PHYS_HI_BUSMASK) >> OFW_PCI_PHYS_HI_BUSSHIFT)
142 #define pcidev(x) \
143 (((x) & OFW_PCI_PHYS_HI_DEVICEMASK) >> OFW_PCI_PHYS_HI_DEVICESHIFT)
144 #define pcifunc(x) \
145 (((x) & OFW_PCI_PHYS_HI_FUNCTIONMASK) >> OFW_PCI_PHYS_HI_FUNCTIONSHIFT)
146
147 static void
148 fixpci(int parent, pci_chipset_tag_t pc)
149 {
150 int node;
151 pcitag_t tag;
152 pcireg_t csr, intr, id, cr;
153 int len, i, ilen;
154 int32_t irqs[4];
155 struct {
156 u_int32_t phys_hi, phys_mid, phys_lo;
157 u_int32_t size_hi, size_lo;
158 } addr[8];
159 struct {
160 u_int32_t phys_hi, phys_mid, phys_lo;
161 u_int32_t icells[5];
162 } iaddr;
163
164 /*
165 * first hack - here we make the Ethernet portion of a
166 * UMAX E100 card work
167 */
168 #ifdef UMAX_E100_HACK
169 tag = pci_make_tag(pc, 0, 17, 0);
170 id = pci_conf_read(pc, tag, PCI_ID_REG);
171 if ((PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
172 (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21140)) {
173 /* this could be one */
174 pcireg_t isp, reg;
175 pcitag_t tag_isp = pci_make_tag(pc, 0, 13, 0);
176 /*
177 * here we go. We shouldn't encounter this anywhere else
178 * than on a UMAX S900 with an E100 board
179 * look at 00:0d:00 for a Qlogic ISP 1020 to
180 * make sure we really have an E100 here
181 */
182 aprint_debug("\nfound E100 candidate tlp");
183 isp = pci_conf_read(pc, tag_isp, PCI_ID_REG);
184 if ((PCI_VENDOR(isp) == PCI_VENDOR_QLOGIC) &&
185 (PCI_PRODUCT(isp) == PCI_PRODUCT_QLOGIC_ISP1020)) {
186
187 aprint_verbose("\nenabling UMAX E100 ethernet");
188
189 pci_conf_write(pc, tag, 0x14, 0x80000000);
190
191 /* now enable MMIO and busmastering */
192 reg = pci_conf_read(pc, tag,
193 PCI_COMMAND_STATUS_REG);
194 reg |= PCI_COMMAND_MEM_ENABLE |
195 PCI_COMMAND_MASTER_ENABLE;
196 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
197 reg);
198
199 /* and finally the interrupt */
200 reg = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
201 reg &= ~PCI_INTERRUPT_LINE_MASK;
202 reg |= 23;
203 pci_conf_write(pc, tag, PCI_INTERRUPT_REG, reg);
204 }
205 }
206 #endif
207
208 len = OF_getprop(parent, "#interrupt-cells", &ilen, sizeof(ilen));
209 if (len < 0)
210 ilen = 0;
211 for (node = OF_child(parent); node; node = OF_peer(node)) {
212 len = OF_getprop(node, "assigned-addresses", addr,
213 sizeof(addr));
214 if (len < (int)sizeof(addr[0]))
215 continue;
216
217 tag = pci_make_tag(pc, pcibus(addr[0].phys_hi),
218 pcidev(addr[0].phys_hi),
219 pcifunc(addr[0].phys_hi));
220
221 /*
222 * Make sure the IO and MEM enable bits are set in the CSR.
223 */
224 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
225 csr &= ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
226
227 for (i = 0; i < len / sizeof(addr[0]); i++) {
228 switch (addr[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
229 case OFW_PCI_PHYS_HI_SPACE_IO:
230 csr |= PCI_COMMAND_IO_ENABLE;
231 break;
232
233 case OFW_PCI_PHYS_HI_SPACE_MEM32:
234 case OFW_PCI_PHYS_HI_SPACE_MEM64:
235 csr |= PCI_COMMAND_MEM_ENABLE;
236 break;
237 }
238 }
239
240 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
241
242 /*
243 * Make sure the line register is programmed with the
244 * interrupt mapping.
245 */
246 if (ilen == 0) {
247 /*
248 * Early Apple OFW implementation don't handle
249 * interrupts as defined by the OFW PCI bindings.
250 */
251 len = OF_getprop(node, "AAPL,interrupts", irqs, 4);
252 } else {
253 iaddr.phys_hi = addr[0].phys_hi;
254 iaddr.phys_mid = addr[0].phys_mid;
255 iaddr.phys_lo = addr[0].phys_lo;
256 /*
257 * Thankfully, PCI can only have one entry in its
258 * "interrupts" property.
259 */
260 len = OF_getprop(node, "interrupts", &iaddr.icells[0],
261 4*ilen);
262 if (len != 4*ilen)
263 continue;
264 len = find_node_intr(node, &iaddr.phys_hi, irqs);
265 }
266 if (len <= 0) {
267 /*
268 * If we still don't have an interrupt, try one
269 * more time. This case covers devices behind the
270 * PCI-PCI bridge in a UMAX S900 or similar (9500?)
271 * system. These slots all share the bridge's
272 * interrupt.
273 */
274 len = find_node_intr(node, &addr[0].phys_hi, irqs);
275 if (len <= 0)
276 continue;
277 }
278
279 /*
280 * For PowerBook 2400, 3400 and original G3:
281 * check if we have a 2nd ohare PIC - if so frob the built-in
282 * tlp's IRQ to 60
283 * first see if we have something on bus 0 device 13 and if
284 * it's a DEC 21041
285 */
286 id = pci_conf_read(pc, tag, PCI_ID_REG);
287 if ((tag == pci_make_tag(pc, 0, 13, 0)) &&
288 (PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
289 (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21041)) {
290
291 /* now look for the 2nd ohare */
292 if (OF_finddevice("/bandit/pci106b,7") != -1) {
293
294 irqs[0] = 60;
295 aprint_verbose("\nohare: frobbing tlp IRQ to 60");
296 }
297 }
298
299 intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
300 intr &= ~PCI_INTERRUPT_LINE_MASK;
301 intr |= irqs[0] & PCI_INTERRUPT_LINE_MASK;
302 pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
303
304 /* fix secondary bus numbers on CardBus bridges */
305 cr = pci_conf_read(pc, tag, PCI_CLASS_REG);
306 if ((PCI_CLASS(cr) == PCI_CLASS_BRIDGE) &&
307 (PCI_SUBCLASS(cr) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
308 uint32_t bi, busid;
309
310 /*
311 * we found a CardBus bridge. Check if the bus number
312 * is sane
313 */
314 bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
315 busid = bi & 0xff;
316 if (busid == 0) {
317 fix_cardbus_bridge(node, pc, tag);
318 }
319 }
320 }
321 }
322
323 static void
324 fix_cardbus_bridge(int node, pci_chipset_tag_t pc, pcitag_t tag)
325 {
326 uint32_t bus_number = 0xffffffff;
327 pcireg_t bi;
328 int bus, dev, fn, ih, len;
329 char path[256];
330
331 #if PB3400_CARDBUS_HACK
332 int root_node;
333
334 root_node = OF_finddevice("/");
335 if (of_compatible(root_node, pb3400_compat) != -1) {
336
337 bus_number = cardbus_number;
338 cardbus_number++;
339 } else {
340 #endif
341 ih = OF_open(path);
342 OF_call_method("load-ata", ih, 0, 0);
343 OF_close(ih);
344
345 OF_getprop(node, "AAPL,bus-id", &bus_number,
346 sizeof(bus_number));
347 #if PB3400_CARDBUS_HACK
348 }
349 #endif
350 if (bus_number != 0xffffffff) {
351
352 len = OF_package_to_path(node, path, sizeof(path));
353 path[len] = 0;
354 aprint_verbose("\n%s: fixing bus number to %d", path, bus_number);
355 pci_decompose_tag(pc, tag, &bus, &dev, &fn);
356 bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
357 bi &= 0xff000000;
358 /* XXX subordinate is always 32 here */
359 bi |= (bus & 0xff) | (bus_number << 8) | 0x200000;
360 pci_conf_write(pc, tag, PPB_REG_BUSINFO, bi);
361 }
362 }
363
364 /*
365 * Find PCI IRQ of the node from OF tree.
366 */
367 static int
368 find_node_intr(int node, u_int32_t *addr, uint32_t *intr)
369 {
370 int parent, len, mlen, iparent;
371 int match, i;
372 u_int32_t map[160];
373 const u_int32_t *mp;
374 u_int32_t imapmask[8], maskedaddr[8];
375 u_int32_t acells, icells;
376 char name[32];
377
378 /* XXXSL: 1st check for a interrupt-parent property */
379 if (OF_getprop(node, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent))
380 {
381 /* How many cells to specify an interrupt ?? */
382 if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
383 return -1;
384
385 if (OF_getprop(node, "interrupts", &map, sizeof(map)) != (icells * 4))
386 return -1;
387
388 memcpy(intr, map, icells * 4);
389 return (icells * 4);
390 }
391
392 parent = OF_parent(node);
393 len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
394 mlen = OF_getprop(parent, "interrupt-map-mask", imapmask,
395 sizeof(imapmask));
396
397 if (mlen != -1)
398 memcpy(maskedaddr, addr, mlen);
399 again:
400 if (len == -1 || mlen == -1)
401 goto nomap;
402
403 #ifdef DIAGNOSTIC
404 if (mlen == sizeof(imapmask)) {
405 aprint_error("interrupt-map too long\n");
406 return -1;
407 }
408 #endif
409
410 /* mask addr by "interrupt-map-mask" */
411 for (i = 0; i < mlen / 4; i++)
412 maskedaddr[i] &= imapmask[i];
413
414 mp = map;
415 i = 0;
416 while (len > mlen) {
417 match = memcmp(maskedaddr, mp, mlen);
418 mp += mlen / 4;
419 len -= mlen;
420
421 /*
422 * We must read "#address-cells" and "#interrupt-cells" each
423 * time because each interrupt-parent may be different.
424 */
425 iparent = *mp++;
426 len -= 4;
427 i = OF_getprop(iparent, "#address-cells", &acells, 4);
428 if (i <= 0)
429 acells = 0;
430 else if (i != 4)
431 return -1;
432 if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
433 return -1;
434
435 /* Found. */
436 if (match == 0) {
437 /*
438 * We matched on address/interrupt, but are we done?
439 */
440 if (acells == 0) { /* XXX */
441 /*
442 * If we are at the interrupt controller,
443 * we are finally done. Save the result and
444 * return.
445 */
446 memcpy(intr, mp, icells * 4);
447 return icells * 4;
448 }
449 /*
450 * We are now at an intermedia interrupt node. We
451 * need to use its interrupt mask and map the
452 * supplied address/interrupt via its map.
453 */
454 mlen = OF_getprop(iparent, "interrupt-map-mask",
455 imapmask, sizeof(imapmask));
456 #ifdef DIAGNOSTIC
457 if (mlen != (acells + icells)*4) {
458 aprint_error("interrupt-map inconsistent (%d, %d)\n",
459 mlen, (acells + icells)*4);
460 return -1;
461 }
462 #endif
463 memcpy(maskedaddr, mp, mlen);
464 len = OF_getprop(iparent, "interrupt-map", map,
465 sizeof(map));
466 goto again;
467 }
468
469 mp += (acells + icells);
470 len -= (acells + icells) * 4;
471 }
472
473 nomap:
474 /*
475 * If the node has no interrupt property and the parent is a
476 * pci-bridge, use parent's interrupt. This occurs on a PCI
477 * slot. (e.g. AHA-3940)
478 */
479 memset(name, 0, sizeof(name));
480 OF_getprop(parent, "name", name, sizeof(name));
481 if (strcmp(name, "pci-bridge") == 0) {
482 len = OF_getprop(parent, "AAPL,interrupts", intr, 4) ;
483 if (len == 4)
484 return len;
485 #if 0
486 /*
487 * XXX I don't know what is the correct local address.
488 * XXX Use the first entry for now.
489 */
490 len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
491 if (len >= 36) {
492 addr = &map[5];
493 return find_node_intr(parent, addr, intr);
494 }
495 #endif
496 }
497
498 /*
499 * If all else fails, attempt to get AAPL, interrupts property.
500 * Grackle, at least, uses this instead of above in some cases.
501 */
502 len = OF_getprop(node, "AAPL,interrupts", intr, 4) ;
503 if (len == 4)
504 return len;
505
506 return -1;
507 }
508