1 /* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
2 * pci_common.c: PCI controller common support.
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
16 /* Pass "pci=irq_verbose" on the kernel command line to enable this. */
19 /* Fix self device of BUS and hook it into BUS->self.
20 * The pci_scan_bus does not do this for the host bridge.
22 void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
26 list_for_each_entry(pdev, &pbus->devices, bus_list) {
27 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
33 prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
37 /* Find the OBP PROM device tree node for a PCI device. */
38 static struct device_node * __init
39 find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
40 struct device_node *bus_node,
41 struct linux_prom_pci_registers **pregs,
44 struct device_node *dp;
49 * Return the PBM's PROM node in case we are it's PCI device,
50 * as the PBM's reg property is different to standard PCI reg
51 * properties. We would delete this device entry otherwise,
52 * which confuses XFree86's device probing...
54 if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
55 (pdev->vendor == PCI_VENDOR_ID_SUN) &&
56 (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
57 pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
58 pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
59 pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
60 pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
65 struct linux_prom_pci_registers *regs;
66 struct property *prop;
69 prop = of_find_property(dp, "reg", &len);
74 if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
76 *nregs = len / sizeof(struct linux_prom_pci_registers);
87 /* Older versions of OBP on PCI systems encode 64-bit MEM
88 * space assignments incorrectly, this fixes them up. We also
89 * take the opportunity here to hide other kinds of bogus
92 static void __init fixup_obp_assignments(struct pci_dev *pdev,
93 struct pcidev_cookie *pcp)
97 if (pdev->vendor == PCI_VENDOR_ID_AL &&
98 (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
99 pdev->device == PCI_DEVICE_ID_AL_M1533)) {
102 /* Zap all of the normal resources, they are
103 * meaningless and generate bogus resource collision
104 * messages. This is OpenBoot's ill-fated attempt to
105 * represent the implicit resources that these devices
108 pcp->num_prom_assignments = 0;
109 for (i = 0; i < 6; i++) {
110 pdev->resource[i].start =
111 pdev->resource[i].end =
112 pdev->resource[i].flags = 0;
114 pdev->resource[PCI_ROM_RESOURCE].start =
115 pdev->resource[PCI_ROM_RESOURCE].end =
116 pdev->resource[PCI_ROM_RESOURCE].flags = 0;
120 for (i = 0; i < pcp->num_prom_assignments; i++) {
121 struct linux_prom_pci_registers *ap;
124 ap = &pcp->prom_assignments[i];
125 space = ap->phys_hi >> 24;
126 if ((space & 0x3) == 2 &&
127 (space & 0x4) != 0) {
128 ap->phys_hi &= ~(0x7 << 24);
129 ap->phys_hi |= 0x3 << 24;
134 /* Fill in the PCI device cookie sysdata for the given
135 * PCI device. This cookie is the means by which one
136 * can get to OBP and PCI controller specific information
139 static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
140 struct pci_dev *pdev,
141 struct device_node *bus_node)
143 struct linux_prom_pci_registers *pregs = NULL;
144 struct pcidev_cookie *pcp;
145 struct device_node *dp;
146 struct property *prop;
149 dp = find_device_prom_node(pbm, pdev, bus_node,
152 /* If it is not in the OBP device tree then
153 * there must be a damn good reason for it.
155 * So what we do is delete the device from the
156 * PCI device tree completely. This scenario
157 * is seen, for example, on CP1500 for the
158 * second EBUS/HappyMeal pair if the external
159 * connector for it is not present.
161 pci_remove_bus_device(pdev);
165 pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
167 prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
172 memcpy(pcp->prom_regs, pregs,
173 nregs * sizeof(struct linux_prom_pci_registers));
174 pcp->num_prom_regs = nregs;
176 /* We can't have the pcidev_cookie assignments be just
177 * direct pointers into the property value, since they
178 * are potentially modified by the probing process.
180 prop = of_find_property(dp, "assigned-addresses", &len);
182 pcp->num_prom_assignments = 0;
184 memcpy(pcp->prom_assignments, prop->value, len);
185 pcp->num_prom_assignments =
186 (len / sizeof(pcp->prom_assignments[0]));
189 if (strcmp(dp->name, "ebus") == 0) {
190 struct linux_prom_ebus_ranges *erng;
193 /* EBUS is special... */
194 prop = of_find_property(dp, "ranges", &len);
196 prom_printf("EBUS: Fatal error, no range property\n");
200 len = (len / sizeof(erng[0]));
201 for (iter = 0; iter < len; iter++) {
202 struct linux_prom_ebus_ranges *ep = &erng[iter];
203 struct linux_prom_pci_registers *ap;
205 ap = &pcp->prom_assignments[iter];
207 ap->phys_hi = ep->parent_phys_hi;
208 ap->phys_mid = ep->parent_phys_mid;
209 ap->phys_lo = ep->parent_phys_lo;
211 ap->size_lo = ep->size;
213 pcp->num_prom_assignments = len;
216 fixup_obp_assignments(pdev, pcp);
221 void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
222 struct pci_pbm_info *pbm,
223 struct device_node *dp)
225 struct pci_dev *pdev, *pdev_next;
226 struct pci_bus *this_pbus, *pbus_next;
228 /* This must be _safe because the cookie fillin
229 routine can delete devices from the tree. */
230 list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
231 pdev_cookie_fillin(pbm, pdev, dp);
233 list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
234 struct pcidev_cookie *pcp = this_pbus->self->sysdata;
236 pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
240 static void __init bad_assignment(struct pci_dev *pdev,
241 struct linux_prom_pci_registers *ap,
242 struct resource *res,
245 prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
246 pdev->bus->number, pdev->devfn);
248 prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
249 ap->phys_hi, ap->phys_mid, ap->phys_lo,
250 ap->size_hi, ap->size_lo);
252 prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
253 res->start, res->end, res->flags);
258 static struct resource *
259 __init get_root_resource(struct linux_prom_pci_registers *ap,
260 struct pci_pbm_info *pbm)
262 int space = (ap->phys_hi >> 24) & 3;
266 /* Configuration space, silently ignore it. */
270 /* 16-bit IO space */
271 return &pbm->io_space;
274 /* 32-bit MEM space */
275 return &pbm->mem_space;
278 /* 64-bit MEM space, these are allocated out of
279 * the 32-bit mem_space range for the PBM, ie.
280 * we just zero out the upper 32-bits.
282 return &pbm->mem_space;
285 printk("PCI: What is resource space %x?\n", space);
290 static struct resource *
291 __init get_device_resource(struct linux_prom_pci_registers *ap,
292 struct pci_dev *pdev)
294 struct resource *res;
295 int breg = (ap->phys_hi & 0xff);
298 case PCI_ROM_ADDRESS:
299 /* Unfortunately I have seen several cases where
300 * buggy FCODE uses a space value of '1' (I/O space)
301 * in the register property for the ROM address
302 * so disable this sanity check for now.
306 int space = (ap->phys_hi >> 24) & 3;
308 /* It had better be MEM space. */
310 bad_assignment(pdev, ap, NULL, 0);
313 res = &pdev->resource[PCI_ROM_RESOURCE];
316 case PCI_BASE_ADDRESS_0:
317 case PCI_BASE_ADDRESS_1:
318 case PCI_BASE_ADDRESS_2:
319 case PCI_BASE_ADDRESS_3:
320 case PCI_BASE_ADDRESS_4:
321 case PCI_BASE_ADDRESS_5:
322 res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
326 bad_assignment(pdev, ap, NULL, 0);
334 static int __init pdev_resource_collisions_expected(struct pci_dev *pdev)
336 if (pdev->vendor != PCI_VENDOR_ID_SUN)
339 if (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS ||
340 pdev->device == PCI_DEVICE_ID_SUN_RIO_1394 ||
341 pdev->device == PCI_DEVICE_ID_SUN_RIO_USB)
347 static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
348 struct pci_dev *pdev)
350 struct pcidev_cookie *pcp = pdev->sysdata;
353 for (i = 0; i < pcp->num_prom_assignments; i++) {
354 struct linux_prom_pci_registers *ap;
355 struct resource *root, *res;
357 /* The format of this property is specified in
358 * the PCI Bus Binding to IEEE1275-1994.
360 ap = &pcp->prom_assignments[i];
361 root = get_root_resource(ap, pbm);
362 res = get_device_resource(ap, pdev);
363 if (root == NULL || res == NULL ||
367 /* Ok we know which resource this PROM assignment is
368 * for, sanity check it.
370 if ((res->start & 0xffffffffUL) != ap->phys_lo)
371 bad_assignment(pdev, ap, res, 1);
373 /* If it is a 64-bit MEM space assignment, verify that
374 * the resource is too and that the upper 32-bits match.
376 if (((ap->phys_hi >> 24) & 3) == 3) {
377 if (((res->flags & IORESOURCE_MEM) == 0) ||
378 ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
379 != PCI_BASE_ADDRESS_MEM_TYPE_64))
380 bad_assignment(pdev, ap, res, 1);
381 if ((res->start >> 32) != ap->phys_mid)
382 bad_assignment(pdev, ap, res, 1);
384 /* PBM cannot generate cpu initiated PIOs
385 * to the full 64-bit space. Therefore the
386 * upper 32-bits better be zero. If it is
387 * not, just skip it and we will assign it
388 * properly ourselves.
390 if ((res->start >> 32) != 0UL) {
391 printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
392 "%016lx for region %ld on device %s\n",
393 res->start, (res - &pdev->resource[0]), pci_name(pdev));
398 /* Adjust the resource into the physical address space
401 pbm->parent->resource_adjust(pdev, res, root);
403 if (request_resource(root, res) < 0) {
404 /* OK, there is some conflict. But this is fine
405 * since we'll reassign it in the fixup pass.
407 * We notify the user that OBP made an error if it
408 * is a case we don't expect.
410 if (!pdev_resource_collisions_expected(pdev)) {
411 printk(KERN_ERR "PCI: Address space collision on region %ld "
412 "[%016lx:%016lx] of device %s\n",
413 (res - &pdev->resource[0]),
414 res->start, res->end,
421 void __init pci_record_assignments(struct pci_pbm_info *pbm,
422 struct pci_bus *pbus)
427 list_for_each_entry(dev, &pbus->devices, bus_list)
428 pdev_record_assignments(pbm, dev);
430 list_for_each_entry(bus, &pbus->children, node)
431 pci_record_assignments(pbm, bus);
434 /* Return non-zero if PDEV has implicit I/O resources even
435 * though it may not have an I/O base address register
438 static int __init has_implicit_io(struct pci_dev *pdev)
440 int class = pdev->class >> 8;
442 if (class == PCI_CLASS_NOT_DEFINED ||
443 class == PCI_CLASS_NOT_DEFINED_VGA ||
444 class == PCI_CLASS_STORAGE_IDE ||
445 (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
451 static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
452 struct pci_dev *pdev)
456 int i, io_seen, mem_seen;
458 io_seen = mem_seen = 0;
459 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
460 struct resource *root, *res;
461 unsigned long size, min, max, align;
463 res = &pdev->resource[i];
465 if (res->flags & IORESOURCE_IO)
467 else if (res->flags & IORESOURCE_MEM)
470 /* If it is already assigned or the resource does
471 * not exist, there is nothing to do.
473 if (res->parent != NULL || res->flags == 0UL)
476 /* Determine the root we allocate from. */
477 if (res->flags & IORESOURCE_IO) {
478 root = &pbm->io_space;
479 min = root->start + 0x400UL;
482 root = &pbm->mem_space;
484 max = min + 0x80000000UL;
487 size = res->end - res->start;
489 if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
491 prom_printf("PCI: Failed to allocate resource %d for %s\n",
496 /* Update PCI config space. */
497 pbm->parent->base_address_update(pdev, i);
500 /* Special case, disable the ROM. Several devices
501 * act funny (ie. do not respond to memory space writes)
502 * when it is left enabled. A good example are Qlogic,ISP
505 pci_read_config_dword(pdev, PCI_ROM_ADDRESS, ®);
506 reg &= ~PCI_ROM_ADDRESS_ENABLE;
507 pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
509 /* If we saw I/O or MEM resources, enable appropriate
510 * bits in PCI command register.
512 if (io_seen || mem_seen) {
513 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
514 if (io_seen || has_implicit_io(pdev))
515 cmd |= PCI_COMMAND_IO;
517 cmd |= PCI_COMMAND_MEMORY;
518 pci_write_config_word(pdev, PCI_COMMAND, cmd);
521 /* If this is a PCI bridge or an IDE controller,
522 * enable bus mastering. In the former case also
523 * set the cache line size correctly.
525 if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
526 (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
527 ((pdev->class & 0x80) != 0))) {
528 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
529 cmd |= PCI_COMMAND_MASTER;
530 pci_write_config_word(pdev, PCI_COMMAND, cmd);
532 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
533 pci_write_config_byte(pdev,
539 void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
540 struct pci_bus *pbus)
545 list_for_each_entry(dev, &pbus->devices, bus_list)
546 pdev_assign_unassigned(pbm, dev);
548 list_for_each_entry(bus, &pbus->children, node)
549 pci_assign_unassigned(pbm, bus);
552 static inline unsigned int pci_slot_swivel(struct pci_pbm_info *pbm,
553 struct pci_dev *toplevel_pdev,
554 struct pci_dev *pdev,
555 unsigned int interrupt)
559 if (unlikely(interrupt < 1 || interrupt > 4)) {
560 printk("%s: Device %s interrupt value of %u is strange.\n",
561 pbm->name, pci_name(pdev), interrupt);
565 ret = ((interrupt - 1 + (PCI_SLOT(pdev->devfn) & 3)) & 3) + 1;
568 printk("%s: %s IRQ Swivel %s [%x:%x] -> [%x]\n",
569 pbm->name, pci_name(toplevel_pdev), pci_name(pdev),
570 interrupt, PCI_SLOT(pdev->devfn), ret);
575 static inline unsigned int pci_apply_intmap(struct pci_pbm_info *pbm,
576 struct pci_dev *toplevel_pdev,
577 struct pci_dev *pbus,
578 struct pci_dev *pdev,
579 unsigned int interrupt,
580 struct device_node **cnode)
582 struct linux_prom_pci_intmap *imap;
583 struct linux_prom_pci_intmask *imask;
584 struct pcidev_cookie *pbus_pcp = pbus->sysdata;
585 struct pcidev_cookie *pdev_pcp = pdev->sysdata;
586 struct linux_prom_pci_registers *pregs = pdev_pcp->prom_regs;
587 struct property *prop;
588 int plen, num_imap, i;
589 unsigned int hi, mid, lo, irq, orig_interrupt;
591 *cnode = pbus_pcp->prom_node;
593 prop = of_find_property(pbus_pcp->prom_node, "interrupt-map", &plen);
595 (plen % sizeof(struct linux_prom_pci_intmap)) != 0) {
596 printk("%s: Device %s interrupt-map has bad len %d\n",
597 pbm->name, pci_name(pbus), plen);
601 num_imap = plen / sizeof(struct linux_prom_pci_intmap);
603 prop = of_find_property(pbus_pcp->prom_node, "interrupt-map-mask", &plen);
605 (plen % sizeof(struct linux_prom_pci_intmask)) != 0) {
606 printk("%s: Device %s interrupt-map-mask has bad len %d\n",
607 pbm->name, pci_name(pbus), plen);
612 orig_interrupt = interrupt;
614 hi = pregs->phys_hi & imask->phys_hi;
615 mid = pregs->phys_mid & imask->phys_mid;
616 lo = pregs->phys_lo & imask->phys_lo;
617 irq = interrupt & imask->interrupt;
619 for (i = 0; i < num_imap; i++) {
620 if (imap[i].phys_hi == hi &&
621 imap[i].phys_mid == mid &&
622 imap[i].phys_lo == lo &&
623 imap[i].interrupt == irq) {
624 *cnode = of_find_node_by_phandle(imap[i].cnode);
625 interrupt = imap[i].cinterrupt;
630 printk("%s: %s MAP BUS %s DEV %s [%x] -> [%x]\n",
631 pbm->name, pci_name(toplevel_pdev),
632 pci_name(pbus), pci_name(pdev),
633 orig_interrupt, interrupt);
639 /* For each PCI bus on the way to the root:
640 * 1) If it has an interrupt-map property, apply it.
641 * 2) Else, swivel the interrupt number based upon the PCI device number.
643 * Return the "IRQ controller" node. If this is the PBM's device node,
644 * all interrupt translations are complete, else we should use that node's
645 * "reg" property to apply the PBM's "interrupt-{map,mask}" to the interrupt.
647 static struct device_node * __init
648 pci_intmap_match_to_root(struct pci_pbm_info *pbm,
649 struct pci_dev *pdev,
650 unsigned int *interrupt)
652 struct pci_dev *toplevel_pdev = pdev;
653 struct pcidev_cookie *toplevel_pcp = toplevel_pdev->sysdata;
654 struct device_node *cnode = toplevel_pcp->prom_node;
656 while (pdev->bus->number != pbm->pci_first_busno) {
657 struct pci_dev *pbus = pdev->bus->self;
658 struct pcidev_cookie *pcp = pbus->sysdata;
659 struct property *prop;
661 prop = of_find_property(pcp->prom_node, "interrupt-map", NULL);
663 *interrupt = pci_slot_swivel(pbm, toplevel_pdev,
665 cnode = pcp->prom_node;
667 *interrupt = pci_apply_intmap(pbm, toplevel_pdev,
671 while (pcp->prom_node != cnode &&
672 pbus->bus->number != pbm->pci_first_busno) {
673 pbus = pbus->bus->self;
679 if (cnode == pbm->prom_node)
686 static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
688 struct pcidev_cookie *dev_pcp = pdev->sysdata;
689 struct pci_pbm_info *pbm = dev_pcp->pbm;
690 struct linux_prom_pci_registers *reg;
691 struct device_node *cnode;
692 struct property *prop;
693 unsigned int hi, mid, lo, irq;
696 cnode = pci_intmap_match_to_root(pbm, pdev, interrupt);
697 if (cnode == pbm->prom_node)
700 prop = of_find_property(cnode, "reg", &plen);
702 (plen % sizeof(struct linux_prom_pci_registers)) != 0) {
703 printk("%s: OBP node %s reg property has bad len %d\n",
704 pbm->name, cnode->full_name, plen);
709 hi = reg[0].phys_hi & pbm->pbm_intmask->phys_hi;
710 mid = reg[0].phys_mid & pbm->pbm_intmask->phys_mid;
711 lo = reg[0].phys_lo & pbm->pbm_intmask->phys_lo;
712 irq = *interrupt & pbm->pbm_intmask->interrupt;
714 for (i = 0; i < pbm->num_pbm_intmap; i++) {
715 struct linux_prom_pci_intmap *intmap;
717 intmap = &pbm->pbm_intmap[i];
719 if (intmap->phys_hi == hi &&
720 intmap->phys_mid == mid &&
721 intmap->phys_lo == lo &&
722 intmap->interrupt == irq) {
723 *interrupt = intmap->cinterrupt;
733 printk("%s: Routing bus[%2x] slot[%2x] to INO[%02x]\n",
735 pdev->bus->number, PCI_SLOT(pdev->devfn),
740 static void __init pdev_fixup_irq(struct pci_dev *pdev)
742 struct pcidev_cookie *pcp = pdev->sysdata;
743 struct pci_pbm_info *pbm = pcp->pbm;
744 struct pci_controller_info *p = pbm->parent;
745 unsigned int portid = pbm->portid;
746 unsigned int prom_irq;
747 struct device_node *dp = pcp->prom_node;
748 struct property *prop;
750 /* If this is an empty EBUS device, sometimes OBP fails to
751 * give it a valid fully specified interrupts property.
752 * The EBUS hooked up to SunHME on PCI I/O boards of
753 * Ex000 systems is one such case.
755 * The interrupt is not important so just ignore it.
757 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
758 pdev->device == PCI_DEVICE_ID_SUN_EBUS &&
764 prop = of_find_property(dp, "interrupts", NULL);
769 prom_irq = *(unsigned int *) prop->value;
771 if (tlb_type != hypervisor) {
772 /* Fully specified already? */
773 if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
774 pdev->irq = p->irq_build(pbm, pdev, prom_irq);
778 /* An onboard device? (bit 5 set) */
779 if ((prom_irq & PCI_IRQ_INO) & 0x20) {
780 pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
785 /* Can we find a matching entry in the interrupt-map? */
786 if (pci_intmap_match(pdev, &prom_irq)) {
787 pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
791 /* Ok, we have to do it the hard way. */
793 unsigned int bus, slot, line;
795 bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
797 /* If we have a legal interrupt property, use it as
800 if (prom_irq > 0 && prom_irq < 5) {
801 line = ((prom_irq - 1) & 3);
805 /* Else just directly consult PCI config space. */
806 pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
807 line = ((pci_irq_line - 1) & 3);
810 /* Now figure out the slot.
812 * Basically, device number zero on the top-level bus is
813 * always the PCI host controller. Slot 0 is then device 1.
814 * PBM A supports two external slots (0 and 1), and PBM B
815 * supports 4 external slots (0, 1, 2, and 3). On-board PCI
816 * devices are wired to device numbers outside of these
819 if (pdev->bus->number == pbm->pci_first_busno) {
820 slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
822 struct pci_dev *bus_dev;
824 /* Underneath a bridge, use slot number of parent
825 * bridge which is closest to the PBM.
827 bus_dev = pdev->bus->self;
828 while (bus_dev->bus &&
829 bus_dev->bus->number != pbm->pci_first_busno)
830 bus_dev = bus_dev->bus->self;
832 slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
836 pdev->irq = p->irq_build(pbm, pdev,
837 ((portid << 6) & PCI_IRQ_IGN) |
838 (bus | slot | line));
842 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
843 pdev->irq & PCI_IRQ_INO);
846 void __init pci_fixup_irq(struct pci_pbm_info *pbm,
847 struct pci_bus *pbus)
852 list_for_each_entry(dev, &pbus->devices, bus_list)
855 list_for_each_entry(bus, &pbus->children, node)
856 pci_fixup_irq(pbm, bus);
859 static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
862 u8 hdr_type, min_gnt, ltimer;
864 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
865 cmd |= PCI_COMMAND_MASTER;
866 pci_write_config_word(pdev, PCI_COMMAND, cmd);
868 /* Read it back, if the mastering bit did not
869 * get set, the device does not support bus
870 * mastering so we have nothing to do here.
872 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
873 if ((cmd & PCI_COMMAND_MASTER) == 0)
876 /* Set correct cache line size, 64-byte on all
877 * Sparc64 PCI systems. Note that the value is
878 * measured in 32-bit words.
880 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
883 pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
885 if (hdr_type != PCI_HEADER_TYPE_NORMAL)
888 /* If the latency timer is already programmed with a non-zero
889 * value, assume whoever set it (OBP or whoever) knows what
892 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, <imer);
896 /* XXX Since I'm tipping off the min grant value to
897 * XXX choose a suitable latency timer value, I also
898 * XXX considered making use of the max latency value
899 * XXX as well. Unfortunately I've seen too many bogusly
900 * XXX low settings for it to the point where it lacks
901 * XXX any usefulness. In one case, an ethernet card
902 * XXX claimed a min grant of 10 and a max latency of 5.
903 * XXX Now, if I had two such cards on the same bus I
904 * XXX could not set the desired burst period (calculated
905 * XXX from min grant) without violating the max latency
908 * XXX I blame dumb PC bios implementors for stuff like
909 * XXX this, most of them don't even try to do something
910 * XXX sensible with latency timer values and just set some
911 * XXX default value (usually 32) into every device.
914 pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
917 /* If no min_gnt setting then use a default
932 /* Use a default value when the min_gnt value
933 * is erroneously high.
935 if (((unsigned int) min_gnt << shift_factor) > 512 ||
936 ((min_gnt << shift_factor) & 0xff) == 0) {
937 ltimer = 8 << shift_factor;
939 ltimer = min_gnt << shift_factor;
943 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
946 void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
947 struct pci_bus *pbus)
949 struct pci_dev *pdev;
953 if (pbm->is_66mhz_capable == 0) {
959 list_for_each_entry(pdev, &pbus->devices, bus_list) {
960 pci_read_config_word(pdev, PCI_STATUS, &status);
961 if (!(status & PCI_STATUS_66MHZ)) {
967 pbm->all_devs_66mhz = all_are_66mhz;
969 printk("PCI%d(PBM%c): Bus running at %dMHz\n",
971 (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
972 (all_are_66mhz ? 66 : 33));
975 void pci_setup_busmastering(struct pci_pbm_info *pbm,
976 struct pci_bus *pbus)
982 is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
984 list_for_each_entry(dev, &pbus->devices, bus_list)
985 pdev_setup_busmastering(dev, is_66mhz);
987 list_for_each_entry(bus, &pbus->children, node)
988 pci_setup_busmastering(pbm, bus);
991 void pci_register_legacy_regions(struct resource *io_res,
992 struct resource *mem_res)
997 p = kzalloc(sizeof(*p), GFP_KERNEL);
1001 p->name = "Video RAM area";
1002 p->start = mem_res->start + 0xa0000UL;
1003 p->end = p->start + 0x1ffffUL;
1004 p->flags = IORESOURCE_BUSY;
1005 request_resource(mem_res, p);
1007 p = kzalloc(sizeof(*p), GFP_KERNEL);
1011 p->name = "System ROM";
1012 p->start = mem_res->start + 0xf0000UL;
1013 p->end = p->start + 0xffffUL;
1014 p->flags = IORESOURCE_BUSY;
1015 request_resource(mem_res, p);
1017 p = kzalloc(sizeof(*p), GFP_KERNEL);
1021 p->name = "Video ROM";
1022 p->start = mem_res->start + 0xc0000UL;
1023 p->end = p->start + 0x7fffUL;
1024 p->flags = IORESOURCE_BUSY;
1025 request_resource(mem_res, p);
1028 /* Generic helper routines for PCI error reporting. */
1029 void pci_scan_for_target_abort(struct pci_controller_info *p,
1030 struct pci_pbm_info *pbm,
1031 struct pci_bus *pbus)
1033 struct pci_dev *pdev;
1034 struct pci_bus *bus;
1036 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1037 u16 status, error_bits;
1039 pci_read_config_word(pdev, PCI_STATUS, &status);
1041 (status & (PCI_STATUS_SIG_TARGET_ABORT |
1042 PCI_STATUS_REC_TARGET_ABORT));
1044 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1045 printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
1046 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1047 pci_name(pdev), status);
1051 list_for_each_entry(bus, &pbus->children, node)
1052 pci_scan_for_target_abort(p, pbm, bus);
1055 void pci_scan_for_master_abort(struct pci_controller_info *p,
1056 struct pci_pbm_info *pbm,
1057 struct pci_bus *pbus)
1059 struct pci_dev *pdev;
1060 struct pci_bus *bus;
1062 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1063 u16 status, error_bits;
1065 pci_read_config_word(pdev, PCI_STATUS, &status);
1067 (status & (PCI_STATUS_REC_MASTER_ABORT));
1069 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1070 printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
1071 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1072 pci_name(pdev), status);
1076 list_for_each_entry(bus, &pbus->children, node)
1077 pci_scan_for_master_abort(p, pbm, bus);
1080 void pci_scan_for_parity_error(struct pci_controller_info *p,
1081 struct pci_pbm_info *pbm,
1082 struct pci_bus *pbus)
1084 struct pci_dev *pdev;
1085 struct pci_bus *bus;
1087 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1088 u16 status, error_bits;
1090 pci_read_config_word(pdev, PCI_STATUS, &status);
1092 (status & (PCI_STATUS_PARITY |
1093 PCI_STATUS_DETECTED_PARITY));
1095 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1096 printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
1097 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1098 pci_name(pdev), status);
1102 list_for_each_entry(bus, &pbus->children, node)
1103 pci_scan_for_parity_error(p, pbm, bus);