2 * Port for PPC64 David Engebretsen, IBM Corp.
3 * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/init.h>
17 #include <linux/capability.h>
18 #include <linux/sched.h>
19 #include <linux/errno.h>
20 #include <linux/bootmem.h>
22 #include <asm/processor.h>
25 #include <asm/pci-bridge.h>
26 #include <asm/byteorder.h>
28 #include <asm/uaccess.h>
29 #include <asm/flight_recorder.h>
30 #include <asm/ppcdebug.h>
32 #include <asm/pci_dma.h>
33 #include <asm/machdep.h>
37 /* pci_io_base -- the base address from which io bars are offsets.
38 * This is the lowest I/O base address (so bar values are always positive),
39 * and it *must* be the start of ISA space if an ISA bus exists because
40 * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
41 * page is mapped and isa_io_limit prevents access to it.
43 unsigned long isa_io_base = 0; /* NULL if no ISA bus */
44 unsigned long pci_io_base = 0;
45 unsigned long isa_mem_base = 0;
46 unsigned long pci_dram_offset = 0;
48 /******************************************************************
49 * Forward declare of prototypes
50 ******************************************************************/
51 static void pcibios_fixup_resources(struct pci_dev* dev);
52 static void fixup_broken_pcnet32(struct pci_dev* dev);
53 static void fixup_windbond_82c105(struct pci_dev* dev);
54 void fixup_resources(struct pci_dev* dev);
56 void iSeries_pcibios_init(void);
57 void pSeries_pcibios_init(void);
59 int pci_assign_all_busses = 0;
60 struct pci_controller* hose_head;
61 struct pci_controller** hose_tail = &hose_head;
63 LIST_HEAD(iSeries_Global_Device_List);
65 /*******************************************************************
66 * Counters and control flags.
67 *******************************************************************/
68 long Pci_Io_Read_Count = 0;
69 long Pci_Io_Write_Count = 0;
70 long Pci_Cfg_Read_Count = 0;
71 long Pci_Cfg_Write_Count= 0;
72 long Pci_Error_Count = 0;
74 int Pci_Retry_Max = 7; /* Retry set to 7 times */
75 int Pci_Error_Flag = 1; /* Set Retry Error on. */
76 int Pci_Trace_Flag = 0;
78 /******************************************************************
80 ******************************************************************/
81 int global_phb_number = 0; /* Global phb counter */
82 int Pci_Large_Bus_System = 0;
83 int Pci_Set_IOA_Address = 0;
84 int Pci_Manage_Phb_Space = 0;
85 struct pci_controller *phbtab[PCI_MAX_PHB];
87 static int pci_bus_count;
89 /* Cached ISA bridge dev. */
90 struct pci_dev *ppc64_isabridge_dev = NULL;
92 struct pci_fixup pcibios_fixups[] = {
93 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32 },
94 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, fixup_windbond_82c105 },
95 { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },
99 static void fixup_broken_pcnet32(struct pci_dev* dev)
101 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
102 dev->vendor = PCI_VENDOR_ID_AMD;
103 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
104 pci_name_device(dev);
108 static void fixup_windbond_82c105(struct pci_dev* dev)
110 /* Assume the windbond 82c105 is the IDE controller on a
111 * p610. We should probably be more careful in case
112 * someone tries to plug in a similar adapter.
116 printk("Using INTC for W82c105 IDE controller.\n");
117 pci_read_config_dword(dev, 0x40, ®);
118 /* Enable LEGIRQ to use INTC instead of ISA interrupts */
119 pci_write_config_dword(dev, 0x40, reg | (1<<11));
123 /* Given an mmio phys address, find a pci device that implements
124 * this address. This is of course expensive, but only used
125 * for device initialization or error paths.
126 * For io BARs it is assumed the pci_io_base has already been added
129 * Bridges are ignored although they could be used to optimize the search.
131 struct pci_dev *pci_find_dev_by_addr(unsigned long addr)
135 unsigned long ioaddr;
137 ioaddr = (addr > _IO_BASE) ? addr - _IO_BASE : 0;
139 pci_for_each_dev(dev) {
140 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
142 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
143 unsigned long start = pci_resource_start(dev,i);
144 unsigned long end = pci_resource_end(dev,i);
145 unsigned int flags = pci_resource_flags(dev,i);
146 if (start == 0 || ~start == 0 ||
147 end == 0 || ~end == 0)
149 if ((flags & IORESOURCE_IO) &&
150 (ioaddr >= start && ioaddr <= end))
152 else if ((flags & IORESOURCE_MEM) &&
153 (addr >= start && addr <= end))
160 void pcibios_fixup_pbus_ranges(struct pci_bus *pbus,
161 struct pbus_set_ranges_data *pranges)
166 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
167 struct resource *res, int resource)
171 struct pci_controller* hose = PCI_GET_PHB_PTR(dev);
174 if (hose && res->flags & IORESOURCE_MEM)
175 new -= hose->pci_mem_offset;
176 new |= (res->flags & PCI_REGION_FLAG_MASK);
178 reg = PCI_BASE_ADDRESS_0 + 4*resource;
179 } else if (resource == PCI_ROM_RESOURCE) {
180 res->flags |= PCI_ROM_ADDRESS_ENABLE;
181 reg = dev->rom_base_reg;
183 /* Somebody might have asked allocation of a non-standard resource */
187 pci_write_config_dword(dev, reg, new);
188 pci_read_config_dword(dev, reg, &check);
189 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
190 printk(KERN_ERR "PCI: Error while updating region "
191 "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
197 pcibios_fixup_resources(struct pci_dev* dev)
199 fixup_resources(dev);
203 * We need to avoid collisions with `mirrored' VGA ports
204 * and other strange ISA hardware, so we always want the
205 * addresses to be allocated in the 0x000-0x0ff region
208 * Why? Because some silly external IO cards only decode
209 * the low 10 bits of the IO address. The 0x00-0xff region
210 * is reserved for motherboard devices that decode all 16
211 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
212 * but we want to try to avoid allocating at 0x2900-0x2bff
213 * which might have be mirrored at 0x0100-0x03ff..
216 pcibios_align_resource(void *data, struct resource *res, unsigned long size,
220 struct pci_dev *dev = data;
222 if (res->flags & IORESOURCE_IO) {
223 unsigned long start = res->start;
226 printk(KERN_ERR "PCI: Can not align I/O Region %s %s because size %ld is too large.\n",
227 dev->slot_name, res->name, size);
231 start = (start + 0x3ff) & ~0x3ff;
238 * Handle resources of PCI devices. If the world were perfect, we could
239 * just allocate all the resource regions and do nothing more. It isn't.
240 * On the other hand, we cannot just re-allocate all devices, as it would
241 * require us to know lots of host bridge internals. So we attempt to
242 * keep as much of the original configuration as possible, but tweak it
243 * when it's found to be wrong.
245 * Known BIOS problems we have to work around:
246 * - I/O or memory regions not configured
247 * - regions configured, but not enabled in the command register
248 * - bogus I/O addresses above 64K used
249 * - expansion ROMs left enabled (this may sound harmless, but given
250 * the fact the PCI specs explicitly allow address decoders to be
251 * shared between expansion ROMs and other resource regions, it's
252 * at least dangerous)
255 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
256 * This gives us fixed barriers on where we can allocate.
257 * (2) Allocate resources for all enabled devices. If there is
258 * a collision, just mark the resource as unallocated. Also
259 * disable expansion ROMs during this step.
260 * (3) Try to allocate resources for disabled devices. If the
261 * resources were assigned correctly, everything goes well,
262 * if they weren't, they won't disturb allocation of other
264 * (4) Assign new addresses to resources which were either
265 * not configured at all or misconfigured. If explicitly
266 * requested by the user, configure expansion ROM address
271 pcibios_allocate_bus_resources(struct list_head *bus_list)
273 struct list_head *ln;
276 struct resource *res, *pr;
278 /* Depth-First Search on bus tree */
279 for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
281 for (i = 0; i < 4; ++i) {
282 if ((res = bus->resource[i]) == NULL || !res->flags)
284 if (bus->parent == NULL)
285 pr = (res->flags & IORESOURCE_IO)?
286 &ioport_resource: &iomem_resource;
288 pr = pci_find_parent_resource(bus->self, res);
291 continue; /* transparent bus or undefined */
292 if (pr && request_resource(pr, res) == 0)
294 printk(KERN_ERR "PCI: Cannot allocate resource region "
295 "%d of PCI bridge %x\n", i, bus->number);
296 printk(KERN_ERR "PCI: resource is %lx..%lx (%lx), parent %p\n",
297 res->start, res->end, res->flags, pr);
299 pcibios_allocate_bus_resources(&bus->children);
304 pcibios_allocate_resources(int pass)
309 struct resource *r, *pr;
311 pci_for_each_dev(dev) {
312 pci_read_config_word(dev, PCI_COMMAND, &command);
313 for(idx = 0; idx < 6; idx++) {
314 r = &dev->resource[idx];
315 if (r->parent) /* Already allocated */
317 if (!r->start) /* Address not assigned at all */
320 if (r->flags & IORESOURCE_IO)
321 disabled = !(command & PCI_COMMAND_IO);
323 disabled = !(command & PCI_COMMAND_MEMORY);
324 if (pass == disabled) {
325 PPCDBG(PPCDBG_PHBINIT,
326 "PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
327 r->start, r->end, r->flags, disabled, pass);
328 pr = pci_find_parent_resource(dev, r);
329 if (!pr || request_resource(pr, r) < 0) {
330 PPCDBG(PPCDBG_PHBINIT,
331 "PCI: Cannot allocate resource region %d of device %s, pr = 0x%lx\n", idx, dev->slot_name, pr);
333 PPCDBG(PPCDBG_PHBINIT,
334 "PCI: Cannot allocate resource 0x%lx\n", request_resource(pr,r));
336 /* We'll assign a new address later */
343 r = &dev->resource[PCI_ROM_RESOURCE];
344 if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
345 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
347 r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
348 pci_read_config_dword(dev, dev->rom_base_reg, ®);
349 pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
356 pcibios_assign_resources(void)
362 pci_for_each_dev(dev) {
363 int class = dev->class >> 8;
365 /* Don't touch classless devices and host bridges */
366 if (!class || class == PCI_CLASS_BRIDGE_HOST)
369 for(idx=0; idx<6; idx++) {
370 r = &dev->resource[idx];
373 * Don't touch IDE controllers and I/O ports of video cards!
375 if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
376 (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
380 * We shall assign a new address to this resource, either because
381 * the BIOS forgot to do so or because we have decided the old
382 * address was unusable for some reason.
384 if (!r->start && r->end && ppc_md.pcibios_enable_device_hook &&
385 !ppc_md.pcibios_enable_device_hook(dev, 1))
386 pci_assign_resource(dev, idx);
389 if (0) { /* don't assign ROMs */
390 r = &dev->resource[PCI_ROM_RESOURCE];
394 pci_assign_resource(dev, PCI_ROM_RESOURCE);
401 pcibios_enable_resources(struct pci_dev *dev, int mask)
407 pci_read_config_word(dev, PCI_COMMAND, &cmd);
409 for(idx=0; idx<6; idx++) {
410 if(!(mask & (1<<idx)))
412 r = &dev->resource[idx];
413 if (!r->start && r->end) {
414 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
417 if (r->flags & IORESOURCE_IO)
418 cmd |= PCI_COMMAND_IO;
419 if (r->flags & IORESOURCE_MEM)
420 cmd |= PCI_COMMAND_MEMORY;
422 if (dev->resource[PCI_ROM_RESOURCE].start)
423 cmd |= PCI_COMMAND_MEMORY;
424 if (cmd != old_cmd) {
425 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
426 pci_write_config_word(dev, PCI_COMMAND, cmd);
432 * Allocate pci_controller(phb) initialized common variables.
434 struct pci_controller * __init
435 pci_alloc_pci_controller(char *model, enum phb_types controller_type)
437 struct pci_controller *hose;
438 PPCDBG(PPCDBG_PHBINIT, "PCI: Allocate pci_controller for %s\n",model);
439 hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
441 printk(KERN_ERR "PCI: Allocate pci_controller failed.\n");
444 memset(hose, 0, sizeof(struct pci_controller));
445 if(strlen(model) < 8)
446 strcpy(hose->what,model);
448 memcpy(hose->what,model,7);
449 hose->type = controller_type;
450 hose->global_number = global_phb_number;
451 phbtab[global_phb_number++] = hose;
454 hose_tail = &hose->next;
459 * This fixup is arch independent and probably should go somewhere else.
462 pcibios_generic_fixup(void)
466 /* Fix miss-identified vendor AMD pcnet32 adapters. */
468 while ((dev = pci_find_device(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE, dev)) != NULL &&
469 dev->class == (PCI_CLASS_NETWORK_ETHERNET << 8))
470 dev->vendor = PCI_VENDOR_ID_AMD;
475 /***********************************************************************
479 ***********************************************************************/
483 struct pci_controller *hose;
487 #ifndef CONFIG_PPC_ISERIES
488 pSeries_pcibios_init();
490 iSeries_pcibios_init();
493 ppc64_boot_msg(0x40, "PCI Probe");
494 printk("PCI: Probing PCI hardware\n");
495 PPCDBG(PPCDBG_BUSWALK,"PCI: Probing PCI hardware\n");
497 /* Scan all of the recorded PCI controllers. */
498 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
499 hose->last_busno = 0xff;
500 bus = pci_scan_bus(hose->first_busno, hose->ops, hose->arch_data);
502 hose->last_busno = bus->subordinate;
503 if (pci_assign_all_busses || next_busno <= hose->last_busno)
504 next_busno = hose->last_busno+1;
506 pci_bus_count = next_busno;
508 /* Call machine dependant fixup */
509 if (ppc_md.pcibios_fixup) {
510 ppc_md.pcibios_fixup();
514 pcibios_generic_fixup();
516 /* Allocate and assign resources */
517 pcibios_allocate_bus_resources(&pci_root_buses);
518 pcibios_allocate_resources(0);
519 pcibios_allocate_resources(1);
520 pcibios_assign_resources();
522 #ifndef CONFIG_PPC_ISERIES
523 pci_fix_bus_sysdata();
526 PPCDBG(PPCDBG_BUSWALK,"pSeries create_tce_tables()\n");
529 /* Cache the location of the ISA bridge (if we have one) */
530 ppc64_isabridge_dev = pci_find_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
531 if (ppc64_isabridge_dev != NULL )
532 printk("ISA bridge at %s\n", ppc64_isabridge_dev->slot_name);
534 printk("PCI: Probing PCI hardware done\n");
535 PPCDBG(PPCDBG_BUSWALK,"PCI: Probing PCI hardware done.\n");
536 ppc64_boot_msg(0x41, "PCI Done");
541 pcibios_assign_all_busses(void)
543 return pci_assign_all_busses;
546 unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
547 unsigned long start, unsigned long size)
552 void __init pcibios_fixup_bus(struct pci_bus *bus)
554 #ifndef CONFIG_PPC_ISERIES
555 struct pci_controller *phb = PCI_GET_PHB_PTR(bus);
556 struct resource *res;
559 if (bus->parent == NULL) {
560 /* This is a host bridge - fill in its resources */
562 bus->resource[0] = res = &phb->io_resource;
564 BUG(); /* No I/O resource for this PHB? */
566 for (i = 0; i < 3; ++i) {
567 res = &phb->mem_resources[i];
570 BUG(); /* No memory resource for this PHB? */
572 bus->resource[i+1] = res;
575 /* This is a subordinate bridge */
576 pci_read_bridge_bases(bus);
578 for (i = 0; i < 4; ++i) {
579 if ((res = bus->resource[i]) == NULL)
583 if (res == pci_find_parent_resource(bus->self, res)) {
584 /* Transparent resource -- don't try to "fix" it. */
587 if (res->flags & IORESOURCE_IO) {
588 unsigned long offset = (unsigned long)phb->io_base_virt - pci_io_base;
589 res->start += offset;
591 } else if (phb->pci_mem_offset
592 && (res->flags & IORESOURCE_MEM)) {
593 if (res->start < phb->pci_mem_offset) {
594 res->start += phb->pci_mem_offset;
595 res->end += phb->pci_mem_offset;
601 if ( ppc_md.pcibios_fixup_bus )
602 ppc_md.pcibios_fixup_bus(bus);
605 char __init *pcibios_setup(char *str)
610 int pcibios_enable_device(struct pci_dev *dev, int mask)
616 PPCDBG(PPCDBG_BUSWALK,"PCI: %s for device %s \n",__FUNCTION__,dev->slot_name);
617 if (ppc_md.pcibios_enable_device_hook)
618 if (ppc_md.pcibios_enable_device_hook(dev, 0))
621 pci_read_config_word(dev, PCI_COMMAND, &cmd);
623 for (idx=0; idx<6; idx++) {
624 r = &dev->resource[idx];
625 if (!r->start && r->end) {
626 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
629 if (r->flags & IORESOURCE_IO)
630 cmd |= PCI_COMMAND_IO;
631 if (r->flags & IORESOURCE_MEM)
632 cmd |= PCI_COMMAND_MEMORY;
634 if (cmd != old_cmd) {
635 printk("PCI: Enabling device %s (%04x -> %04x)\n",
636 dev->slot_name, old_cmd, cmd);
637 PPCDBG(PPCDBG_BUSWALK,"PCI: Enabling device %s \n",dev->slot_name);
638 pci_write_config_word(dev, PCI_COMMAND, cmd);
643 struct pci_controller*
644 pci_bus_to_hose(int bus)
646 struct pci_controller* hose = hose_head;
648 for (; hose; hose = hose->next)
649 if (bus >= hose->first_busno && bus <= hose->last_busno)
655 pci_bus_io_base(unsigned int bus)
657 struct pci_controller *hose;
659 hose = pci_bus_to_hose(bus);
662 return hose->io_base_virt;
666 pci_bus_io_base_phys(unsigned int bus)
668 struct pci_controller *hose;
670 hose = pci_bus_to_hose(bus);
673 return hose->io_base_phys;
677 pci_bus_mem_base_phys(unsigned int bus)
679 struct pci_controller *hose;
681 hose = pci_bus_to_hose(bus);
684 return hose->pci_mem_offset;
688 * Return the index of the PCI controller for device pdev.
690 int pci_controller_num(struct pci_dev *dev)
692 struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
694 return hose->global_number;
698 * Platform support for /proc/bus/pci/X/Y mmap()s,
699 * modelled on the sparc64 implementation by Dave Miller.
704 * Adjust vm_pgoff of VMA such that it is the physical page offset
705 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
707 * Basically, the user finds the base address for his device which he wishes
708 * to mmap. They read the 32-bit value from the config space base register,
709 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
710 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
712 * Returns negative error code on failure, zero on success.
714 static __inline__ int
715 __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
716 enum pci_mmap_state mmap_state)
718 struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
719 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
720 unsigned long io_offset = 0;
724 return -EINVAL; /* should never happen */
726 /* If memory, add on the PCI bridge address offset */
727 if (mmap_state == pci_mmap_mem) {
728 offset += hose->pci_mem_offset;
729 res_bit = IORESOURCE_MEM;
731 io_offset = (unsigned long)hose->io_base_virt;
733 res_bit = IORESOURCE_IO;
737 * Check that the offset requested corresponds to one of the
738 * resources of the device.
740 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
741 struct resource *rp = &dev->resource[i];
742 int flags = rp->flags;
744 /* treat ROM as memory (should be already) */
745 if (i == PCI_ROM_RESOURCE)
746 flags |= IORESOURCE_MEM;
748 /* Active and same type? */
749 if ((flags & res_bit) == 0)
752 /* In the range of this resource? */
753 if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
756 /* found it! construct the final physical address */
757 if (mmap_state == pci_mmap_io)
758 offset += hose->io_base_phys - io_offset;
760 vma->vm_pgoff = offset >> PAGE_SHIFT;
768 * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
771 static __inline__ void
772 __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
773 enum pci_mmap_state mmap_state)
775 vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
779 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
782 static __inline__ void
783 __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
784 enum pci_mmap_state mmap_state, int write_combine)
786 long prot = pgprot_val(vma->vm_page_prot);
788 /* XXX would be nice to have a way to ask for write-through */
789 prot |= _PAGE_NO_CACHE;
790 prot |= _PAGE_GUARDED;
791 vma->vm_page_prot = __pgprot(prot);
795 * Perform the actual remap of the pages for a PCI device mapping, as
796 * appropriate for this architecture. The region in the process to map
797 * is described by vm_start and vm_end members of VMA, the base physical
798 * address is found in vm_pgoff.
799 * The pci device structure is provided so that architectures may make mapping
800 * decisions on a per-device or per-bus basis.
802 * Returns a negative error code on failure, zero on success.
804 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
805 enum pci_mmap_state mmap_state,
810 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
814 __pci_mmap_set_flags(dev, vma, mmap_state);
815 __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
817 ret = remap_page_range(vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
818 vma->vm_end - vma->vm_start, vma->vm_page_prot);
823 /* Provide information on locations of various I/O regions in physical
824 * memory. Do this on a per-card basis so that we choose the right
826 * Note that the returned IO or memory base is a physical address
830 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
832 struct pci_controller* hose = pci_bus_to_hose(bus);
833 long result = -EOPNOTSUPP;
839 case IOBASE_BRIDGE_NUMBER:
840 return (long)hose->first_busno;
842 return (long)hose->pci_mem_offset;
844 return (long)hose->io_base_phys;
846 return (long)isa_io_base;
848 return (long)isa_mem_base;
853 /************************************************************************/
854 /* Formats the device information and location for service. */
855 /* - Pass in pci_dev* pointer to the device. */
856 /* - Pass in buffer to place the data. Danger here is the buffer must */
857 /* be as big as the client says it is. Should be at least 128 bytes.*/
858 /* Return will the length of the string data put in the buffer. */
859 /* The brand specific method device_Location is called. */
861 /* PCI: Bus 0, Device 26, Vendor 0x12AE Frame 1, Card C10 Ethernet */
862 /* PCI: Bus 0, Device 26, Vendor 0x12AE Location U0.3-P1-I8 Ethernet */
863 /* For pSeries, see the Product Topology in the RS/6000 Architecture. */
864 /* For iSeries, see the Service Manuals. */
865 /************************************************************************/
866 int format_device_location(struct pci_dev* PciDev,char* BufPtr, int BufferSize)
868 struct device_node* DevNode = (struct device_node*)PciDev->sysdata;
870 if (DevNode != NULL && BufferSize >= 128) {
871 LineLen += device_Location(PciDev,BufPtr+LineLen);
872 LineLen += sprintf(BufPtr+LineLen," %12s",pci_class_name(PciDev->class >> 8) );
876 /************************************************************************
877 * Saves the config registers for a device. *
878 ************************************************************************
879 * Note: This does byte reads so the data may appear byte swapped, *
880 * The data returned in the pci_config_reg_save_area structure can be *
881 * used to the restore of the data. If the save failed, the data *
882 * will not be restore. Yes I know, you are most likey toast. *
883 ************************************************************************/
884 int pci_save_config_regs(struct pci_dev* PciDev,struct pci_config_reg_save_area* SaveArea)
886 memset(SaveArea,0x00,sizeof(struct pci_config_reg_save_area) );
887 SaveArea->PciDev = PciDev;
889 SaveArea->Register = 0;
890 /******************************************************************
891 * Save All the Regs, NOTE: restore skips the first 16 bytes. *
892 ******************************************************************/
893 while (SaveArea->Register < REG_SAVE_SIZE && SaveArea->RCode == 0) {
894 SaveArea->RCode = pci_read_config_byte(PciDev, SaveArea->Register, &SaveArea->Regs[SaveArea->Register]);
895 ++SaveArea->Register;
897 if (SaveArea->RCode != 0) { /* Ouch */
898 SaveArea->Flags = 0x80;
899 printk("PCI: pci_restore_save_regs failed! %p\n 0x%04X",PciDev,SaveArea->RCode);
900 PCIFR( "pci_restore_save_regs failed! %p\n 0x%04X",PciDev,SaveArea->RCode);
903 SaveArea->Flags = 0x01;
905 return SaveArea->RCode;
908 /************************************************************************
909 * Restores the registers saved via the save function. See the save *
910 * function for details. *
911 ************************************************************************/
912 int pci_restore_config_regs(struct pci_dev* PciDev,struct pci_config_reg_save_area* SaveArea)
914 if (SaveArea->PciDev != PciDev || SaveArea->Flags == 0x80 || SaveArea->RCode != 0) {
915 printk("PCI: pci_restore_config_regs failed! %p\n",PciDev);
918 /******************************************************************
919 * Don't touch the Cmd or BIST regs, user must restore those. *
920 * Restore PCI_VENDOR_ID & PCI_DEVICE_ID *
921 * Restore PCI_CACHE_LINE_SIZE & PCI_LATENCY_TIMER *
922 * Restore Saved Regs from 0x10 to 0x3F *
923 ******************************************************************/
924 SaveArea->Register = 0;
925 while(SaveArea->Register < REG_SAVE_SIZE && SaveArea->RCode == 0) {
926 SaveArea->RCode = pci_write_config_byte(PciDev,SaveArea->Register,SaveArea->Regs[SaveArea->Register]);
927 ++SaveArea->Register;
928 if ( SaveArea->Register == PCI_COMMAND) SaveArea->Register = PCI_CACHE_LINE_SIZE;
929 else if (SaveArea->Register == PCI_HEADER_TYPE) SaveArea->Register = PCI_BASE_ADDRESS_0;
931 if (SaveArea->RCode != 0) {
932 printk("PCI: pci_restore_config_regs failed! %p\n 0x%04X",PciDev,SaveArea->RCode);
933 PCIFR( "pci_restore_config_regs failed! %p\n 0x%04X",PciDev,SaveArea->RCode);
935 return SaveArea->RCode;
938 /************************************************************************/
939 /* Interface to toggle the reset line */
940 /* Time is in .1 seconds, need for seconds. */
941 /************************************************************************/
942 int pci_reset_device(struct pci_dev* PciDev, int AssertTime, int DelayTime)
944 unsigned long AssertDelay, WaitDelay;
946 /********************************************************************
947 * Set defaults, Assert is .5 second, Wait is 3 seconds.
948 ********************************************************************/
949 if (AssertTime == 0) AssertDelay = ( 5 * HZ)/10;
950 else AssertDelay = (AssertTime*HZ)/10;
951 if (WaitDelay == 0) WaitDelay = (30 * HZ)/10;
952 else WaitDelay = (DelayTime* HZ)/10;
954 /********************************************************************
955 * Assert reset, wait, de-assert reset, wait for IOA to reset.
956 * - Don't waste the CPU time on jiffies.
957 ********************************************************************/
958 RtnCode = pci_set_reset(PciDev,1);
960 set_current_state(TASK_UNINTERRUPTIBLE);
961 schedule_timeout(AssertDelay); /* Sleep for the time */
962 RtnCode = pci_set_reset(PciDev,0);
963 set_current_state(TASK_UNINTERRUPTIBLE);
964 schedule_timeout(WaitDelay);
967 PCIFR( "Bus%3d, Device%3d, Reset\n",PciDev->bus->number,PCI_SLOT(PciDev->devfn) );
970 printk("PCI: Bus%3d, Device%3d, Reset Failed:0x%04X\n",PciDev->bus->number,PCI_SLOT(PciDev->devfn),RtnCode );
971 PCIFR( "Bus%3d, Device%3d, Reset Failed:0x%04X\n",PciDev->bus->number,PCI_SLOT(PciDev->devfn),RtnCode );
976 /*****************************************************
977 * Dump Resource information
978 *****************************************************/
979 void dumpResources(struct resource* Resource)
981 if(Resource != NULL) {
982 int Flags = 0x00000F00 & Resource->flags;
983 if(Resource->start == 0 && Resource->end == 0) return;
984 else if(Resource->start == Resource->end ) return;
986 if (Flags == IORESOURCE_IO) udbg_printf("IO.:");
987 else if(Flags == IORESOURCE_MEM) udbg_printf("MEM:");
988 else if(Flags == IORESOURCE_IRQ) udbg_printf("IRQ:");
989 else udbg_printf("0x%02X:",Resource->flags);
992 udbg_printf("0x%016LX / 0x%016LX (0x%08X)\n",
993 Resource->start, Resource->end, Resource->end - Resource->start);
997 int resourceSize(struct resource* Resource)
999 if(Resource->start == 0 && Resource->end == 0) return 0;
1000 else if(Resource->start == Resource->end ) return 0;
1001 else return (Resource->end-1)-Resource->start;
1005 /*****************************************************
1006 * Dump PHB information for Debug
1007 *****************************************************/
1008 void dumpPci_Controller(struct pci_controller* phb)
1010 udbg_printf("\tpci_controller= 0x%016LX\n", phb);
1012 udbg_printf("\twhat & type = %s 0x%02X\n ",phb->what,phb->type);
1013 udbg_printf("\tbus = ");
1014 if (phb->bus != NULL) udbg_printf("0x%02X\n", phb->bus->number);
1015 else udbg_printf("<NULL>\n");
1016 udbg_printf("\tarch_data = 0x%016LX\n", phb->arch_data);
1017 udbg_printf("\tfirst_busno = 0x%02X\n", phb->first_busno);
1018 udbg_printf("\tlast_busno = 0x%02X\n", phb->last_busno);
1019 udbg_printf("\tio_base_virt* = 0x%016LX\n", phb->io_base_virt);
1020 udbg_printf("\tio_base_phys = 0x%016LX\n", phb->io_base_phys);
1021 udbg_printf("\tpci_mem_offset= 0x%016LX\n", phb->pci_mem_offset);
1022 udbg_printf("\tpci_io_offset = 0x%016LX\n", phb->pci_io_offset);
1024 udbg_printf("\tcfg_addr = 0x%016LX\n", phb->cfg_addr);
1025 udbg_printf("\tcfg_data = 0x%016LX\n", phb->cfg_data);
1026 udbg_printf("\tphb_regs = 0x%016LX\n", phb->phb_regs);
1027 udbg_printf("\tchip_regs = 0x%016LX\n", phb->chip_regs);
1030 udbg_printf("\tResources\n");
1031 dumpResources(&phb->io_resource);
1032 if (phb->mem_resource_count > 0) dumpResources(&phb->mem_resources[0]);
1033 if (phb->mem_resource_count > 1) dumpResources(&phb->mem_resources[1]);
1034 if (phb->mem_resource_count > 2) dumpResources(&phb->mem_resources[2]);
1036 udbg_printf("\tglobal_num = 0x%02X\n", phb->global_number);
1037 udbg_printf("\tlocal_num = 0x%02X\n", phb->local_number);
1041 /*****************************************************
1042 * Dump PHB information for Debug
1043 *****************************************************/
1044 void dumpPci_Bus(struct pci_bus* Pci_Bus)
1047 udbg_printf("\tpci_bus = 0x%016LX \n",Pci_Bus);
1048 if (Pci_Bus != NULL) {
1050 udbg_printf("\tnumber = 0x%02X \n",Pci_Bus->number);
1051 udbg_printf("\tprimary = 0x%02X \n",Pci_Bus->primary);
1052 udbg_printf("\tsecondary = 0x%02X \n",Pci_Bus->secondary);
1053 udbg_printf("\tsubordinate = 0x%02X \n",Pci_Bus->subordinate);
1056 if(Pci_Bus->resource[i] == NULL) continue;
1057 if(Pci_Bus->resource[i]->start == 0 && Pci_Bus->resource[i]->end == 0) break;
1058 udbg_printf("\tResources[%d]",i);
1059 dumpResources(Pci_Bus->resource[i]);
1064 /*****************************************************
1065 * Dump Device information for Debug
1066 *****************************************************/
1067 void dumpPci_Dev(struct pci_dev* Pci_Dev)
1070 udbg_printf("\tpci_dev* = 0x%p\n",Pci_Dev);
1071 if ( Pci_Dev == NULL ) return;
1072 udbg_printf("\tname = %s \n",Pci_Dev->name);
1073 udbg_printf("\tbus* = 0x%p\n",Pci_Dev->bus);
1074 udbg_printf("\tsysdata* = 0x%p\n",Pci_Dev->sysdata);
1075 udbg_printf("\tDevice = 0x%4X%02X:%02X.%02X 0x%04X:%04X\n",
1076 PCI_GET_PHB_NUMBER(Pci_Dev),
1077 PCI_GET_BUS_NUMBER(Pci_Dev),
1078 PCI_SLOT(Pci_Dev->devfn),
1079 PCI_FUNC(Pci_Dev->devfn),
1082 udbg_printf("\tHdr/Irq = 0x%02X/0x%02X \n",Pci_Dev->hdr_type,Pci_Dev->irq);
1083 for (i=0;i<DEVICE_COUNT_RESOURCE;++i) {
1084 if (Pci_Dev->resource[i].start == 0 && Pci_Dev->resource[i].end == 0) continue;
1085 udbg_printf("\tResources[%d] ",i);
1086 dumpResources(&Pci_Dev->resource[i]);
1088 dumpResources(&Pci_Dev->resource[i]);