2 * Common pmac/prep/chrp pci routines. -- Cort
5 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
18 #include <asm/processor.h>
21 #include <asm/sections.h>
22 #include <asm/pci-bridge.h>
23 #include <asm/ppc-pci.h>
24 #include <asm/byteorder.h>
25 #include <asm/uaccess.h>
26 #include <asm/machdep.h>
30 unsigned long isa_io_base
= 0;
31 unsigned long pci_dram_offset
= 0;
32 int pcibios_assign_bus_offset
= 1;
34 void pcibios_make_OF_bus_map(void);
36 static void fixup_cpc710_pci64(struct pci_dev
* dev
);
38 static u8
* pci_to_OF_bus_map
;
41 /* By default, we don't re-assign bus numbers. We do this only on
44 static int pci_assign_all_buses
;
46 static int pci_bus_count
;
48 /* This will remain NULL for now, until isa-bridge.c is made common
49 * to both 32-bit and 64-bit.
51 struct pci_dev
*isa_bridge_pcidev
;
52 EXPORT_SYMBOL_GPL(isa_bridge_pcidev
);
55 fixup_hide_host_resource_fsl(struct pci_dev
*dev
)
57 int i
, class = dev
->class >> 8;
59 if ((class == PCI_CLASS_PROCESSOR_POWERPC
||
60 class == PCI_CLASS_BRIDGE_OTHER
) &&
61 (dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
) &&
62 (dev
->bus
->parent
== NULL
)) {
63 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
64 dev
->resource
[i
].start
= 0;
65 dev
->resource
[i
].end
= 0;
66 dev
->resource
[i
].flags
= 0;
70 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA
, PCI_ANY_ID
, fixup_hide_host_resource_fsl
);
71 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE
, PCI_ANY_ID
, fixup_hide_host_resource_fsl
);
74 fixup_cpc710_pci64(struct pci_dev
* dev
)
76 /* Hide the PCI64 BARs from the kernel as their content doesn't
77 * fit well in the resource management
79 dev
->resource
[0].start
= dev
->resource
[0].end
= 0;
80 dev
->resource
[0].flags
= 0;
81 dev
->resource
[1].start
= dev
->resource
[1].end
= 0;
82 dev
->resource
[1].flags
= 0;
84 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_IBM_CPC710_PCI64
, fixup_cpc710_pci64
);
88 * Functions below are used on OpenFirmware machines.
91 make_one_node_map(struct device_node
* node
, u8 pci_bus
)
96 if (pci_bus
>= pci_bus_count
)
98 bus_range
= of_get_property(node
, "bus-range", &len
);
99 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
100 printk(KERN_WARNING
"Can't get bus-range for %s, "
101 "assuming it starts at 0\n", node
->full_name
);
102 pci_to_OF_bus_map
[pci_bus
] = 0;
104 pci_to_OF_bus_map
[pci_bus
] = bus_range
[0];
106 for_each_child_of_node(node
, node
) {
108 const unsigned int *class_code
, *reg
;
110 class_code
= of_get_property(node
, "class-code", NULL
);
111 if (!class_code
|| ((*class_code
>> 8) != PCI_CLASS_BRIDGE_PCI
&&
112 (*class_code
>> 8) != PCI_CLASS_BRIDGE_CARDBUS
))
114 reg
= of_get_property(node
, "reg", NULL
);
117 dev
= pci_get_bus_and_slot(pci_bus
, ((reg
[0] >> 8) & 0xff));
118 if (!dev
|| !dev
->subordinate
) {
122 make_one_node_map(node
, dev
->subordinate
->number
);
128 pcibios_make_OF_bus_map(void)
131 struct pci_controller
*hose
, *tmp
;
132 struct property
*map_prop
;
133 struct device_node
*dn
;
135 pci_to_OF_bus_map
= kmalloc(pci_bus_count
, GFP_KERNEL
);
136 if (!pci_to_OF_bus_map
) {
137 printk(KERN_ERR
"Can't allocate OF bus map !\n");
141 /* We fill the bus map with invalid values, that helps
144 for (i
=0; i
<pci_bus_count
; i
++)
145 pci_to_OF_bus_map
[i
] = 0xff;
147 /* For each hose, we begin searching bridges */
148 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
149 struct device_node
* node
= hose
->dn
;
153 make_one_node_map(node
, hose
->first_busno
);
155 dn
= of_find_node_by_path("/");
156 map_prop
= of_find_property(dn
, "pci-OF-bus-map", NULL
);
158 BUG_ON(pci_bus_count
> map_prop
->length
);
159 memcpy(map_prop
->value
, pci_to_OF_bus_map
, pci_bus_count
);
163 printk("PCI->OF bus map:\n");
164 for (i
=0; i
<pci_bus_count
; i
++) {
165 if (pci_to_OF_bus_map
[i
] == 0xff)
167 printk("%d -> %d\n", i
, pci_to_OF_bus_map
[i
]);
172 typedef int (*pci_OF_scan_iterator
)(struct device_node
* node
, void* data
);
174 static struct device_node
*
175 scan_OF_pci_childs(struct device_node
*parent
, pci_OF_scan_iterator filter
, void* data
)
177 struct device_node
*node
;
178 struct device_node
* sub_node
;
180 for_each_child_of_node(parent
, node
) {
181 const unsigned int *class_code
;
183 if (filter(node
, data
)) {
188 /* For PCI<->PCI bridges or CardBus bridges, we go down
189 * Note: some OFs create a parent node "multifunc-device" as
190 * a fake root for all functions of a multi-function device,
191 * we go down them as well.
193 class_code
= of_get_property(node
, "class-code", NULL
);
194 if ((!class_code
|| ((*class_code
>> 8) != PCI_CLASS_BRIDGE_PCI
&&
195 (*class_code
>> 8) != PCI_CLASS_BRIDGE_CARDBUS
)) &&
196 strcmp(node
->name
, "multifunc-device"))
198 sub_node
= scan_OF_pci_childs(node
, filter
, data
);
207 static struct device_node
*scan_OF_for_pci_dev(struct device_node
*parent
,
210 struct device_node
*np
, *cnp
;
214 for_each_child_of_node(parent
, np
) {
215 reg
= of_get_property(np
, "reg", &psize
);
216 if (reg
&& psize
>= 4 && ((reg
[0] >> 8) & 0xff) == devfn
)
219 /* Note: some OFs create a parent node "multifunc-device" as
220 * a fake root for all functions of a multi-function device,
221 * we go down them as well. */
222 if (!strcmp(np
->name
, "multifunc-device")) {
223 cnp
= scan_OF_for_pci_dev(np
, devfn
);
232 static struct device_node
*scan_OF_for_pci_bus(struct pci_bus
*bus
)
234 struct device_node
*parent
, *np
;
236 /* Are we a root bus ? */
237 if (bus
->self
== NULL
|| bus
->parent
== NULL
) {
238 struct pci_controller
*hose
= pci_bus_to_host(bus
);
241 return of_node_get(hose
->dn
);
244 /* not a root bus, we need to get our parent */
245 parent
= scan_OF_for_pci_bus(bus
->parent
);
249 /* now iterate for children for a match */
250 np
= scan_OF_for_pci_dev(parent
, bus
->self
->devfn
);
257 * Scans the OF tree for a device node matching a PCI device
260 pci_busdev_to_OF_node(struct pci_bus
*bus
, int devfn
)
262 struct device_node
*parent
, *np
;
264 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus
->number
, devfn
);
265 parent
= scan_OF_for_pci_bus(bus
);
268 pr_debug(" parent is %s\n", parent
? parent
->full_name
: "<NULL>");
269 np
= scan_OF_for_pci_dev(parent
, devfn
);
271 pr_debug(" result is %s\n", np
? np
->full_name
: "<NULL>");
273 /* XXX most callers don't release the returned node
274 * mostly because ppc64 doesn't increase the refcount,
275 * we need to fix that.
279 EXPORT_SYMBOL(pci_busdev_to_OF_node
);
282 pci_device_to_OF_node(struct pci_dev
*dev
)
284 return pci_busdev_to_OF_node(dev
->bus
, dev
->devfn
);
286 EXPORT_SYMBOL(pci_device_to_OF_node
);
289 find_OF_pci_device_filter(struct device_node
* node
, void* data
)
291 return ((void *)node
== data
);
295 * Returns the PCI device matching a given OF node
298 pci_device_from_OF_node(struct device_node
* node
, u8
* bus
, u8
* devfn
)
300 const unsigned int *reg
;
301 struct pci_controller
* hose
;
302 struct pci_dev
* dev
= NULL
;
304 /* Make sure it's really a PCI device */
305 hose
= pci_find_hose_for_OF_device(node
);
306 if (!hose
|| !hose
->dn
)
308 if (!scan_OF_pci_childs(hose
->dn
,
309 find_OF_pci_device_filter
, (void *)node
))
311 reg
= of_get_property(node
, "reg", NULL
);
314 *bus
= (reg
[0] >> 16) & 0xff;
315 *devfn
= ((reg
[0] >> 8) & 0xff);
317 /* Ok, here we need some tweak. If we have already renumbered
318 * all busses, we can't rely on the OF bus number any more.
319 * the pci_to_OF_bus_map is not enough as several PCI busses
320 * may match the same OF bus number.
322 if (!pci_to_OF_bus_map
)
325 for_each_pci_dev(dev
)
326 if (pci_to_OF_bus_map
[dev
->bus
->number
] == *bus
&&
327 dev
->devfn
== *devfn
) {
328 *bus
= dev
->bus
->number
;
335 EXPORT_SYMBOL(pci_device_from_OF_node
);
337 /* We create the "pci-OF-bus-map" property now so it appears in the
341 pci_create_OF_bus_map(void)
343 struct property
* of_prop
;
344 struct device_node
*dn
;
346 of_prop
= (struct property
*) alloc_bootmem(sizeof(struct property
) + 256);
349 dn
= of_find_node_by_path("/");
351 memset(of_prop
, -1, sizeof(struct property
) + 256);
352 of_prop
->name
= "pci-OF-bus-map";
353 of_prop
->length
= 256;
354 of_prop
->value
= &of_prop
[1];
355 prom_add_property(dn
, of_prop
);
360 #else /* CONFIG_PPC_OF */
361 void pcibios_make_OF_bus_map(void)
364 #endif /* CONFIG_PPC_OF */
366 static void __devinit
pcibios_scan_phb(struct pci_controller
*hose
)
369 struct device_node
*node
= hose
->dn
;
370 unsigned long io_offset
;
371 struct resource
*res
= &hose
->io_resource
;
373 pr_debug("PCI: Scanning PHB %s\n",
374 node
? node
->full_name
: "<NO NAME>");
376 /* Create an empty bus for the toplevel */
377 bus
= pci_create_bus(hose
->parent
, hose
->first_busno
, hose
->ops
, hose
);
379 printk(KERN_ERR
"Failed to create bus for PCI domain %04x\n",
380 hose
->global_number
);
383 bus
->secondary
= hose
->first_busno
;
386 /* Fixup IO space offset */
387 io_offset
= (unsigned long)hose
->io_base_virt
- isa_io_base
;
388 res
->start
= (res
->start
+ io_offset
) & 0xffffffffu
;
389 res
->end
= (res
->end
+ io_offset
) & 0xffffffffu
;
391 /* Wire up PHB bus resources */
392 pcibios_setup_phb_resources(hose
);
395 hose
->last_busno
= bus
->subordinate
= pci_scan_child_bus(bus
);
398 static int __init
pcibios_init(void)
400 struct pci_controller
*hose
, *tmp
;
403 printk(KERN_INFO
"PCI: Probing PCI hardware\n");
405 if (ppc_pci_flags
& PPC_PCI_REASSIGN_ALL_BUS
)
406 pci_assign_all_buses
= 1;
408 /* Scan all of the recorded PCI controllers. */
409 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
) {
410 if (pci_assign_all_buses
)
411 hose
->first_busno
= next_busno
;
412 hose
->last_busno
= 0xff;
413 pcibios_scan_phb(hose
);
414 pci_bus_add_devices(hose
->bus
);
415 if (pci_assign_all_buses
|| next_busno
<= hose
->last_busno
)
416 next_busno
= hose
->last_busno
+ pcibios_assign_bus_offset
;
418 pci_bus_count
= next_busno
;
420 /* OpenFirmware based machines need a map of OF bus
421 * numbers vs. kernel bus numbers since we may have to
424 if (pci_assign_all_buses
)
425 pcibios_make_OF_bus_map();
427 /* Call common code to handle resource allocation */
428 pcibios_resource_survey();
430 /* Call machine dependent post-init code */
431 if (ppc_md
.pcibios_after_init
)
432 ppc_md
.pcibios_after_init();
437 subsys_initcall(pcibios_init
);
439 static struct pci_controller
*
440 pci_bus_to_hose(int bus
)
442 struct pci_controller
*hose
, *tmp
;
444 list_for_each_entry_safe(hose
, tmp
, &hose_list
, list_node
)
445 if (bus
>= hose
->first_busno
&& bus
<= hose
->last_busno
)
450 /* Provide information on locations of various I/O regions in physical
451 * memory. Do this on a per-card basis so that we choose the right
453 * Note that the returned IO or memory base is a physical address
456 long sys_pciconfig_iobase(long which
, unsigned long bus
, unsigned long devfn
)
458 struct pci_controller
* hose
;
459 long result
= -EOPNOTSUPP
;
461 hose
= pci_bus_to_hose(bus
);
466 case IOBASE_BRIDGE_NUMBER
:
467 return (long)hose
->first_busno
;
469 return (long)hose
->pci_mem_offset
;
471 return (long)hose
->io_base_phys
;
473 return (long)isa_io_base
;
475 return (long)isa_mem_base
;
482 * Null PCI config access functions, for the case when we can't
485 #define NULL_PCI_OP(rw, size, type) \
487 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
489 return PCIBIOS_DEVICE_NOT_FOUND; \
493 null_read_config(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
496 return PCIBIOS_DEVICE_NOT_FOUND
;
500 null_write_config(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
503 return PCIBIOS_DEVICE_NOT_FOUND
;
506 static struct pci_ops null_pci_ops
=
508 .read
= null_read_config
,
509 .write
= null_write_config
,
513 * These functions are used early on before PCI scanning is done
514 * and all of the pci_dev and pci_bus structures have been created.
516 static struct pci_bus
*
517 fake_pci_bus(struct pci_controller
*hose
, int busnr
)
519 static struct pci_bus bus
;
522 hose
= pci_bus_to_hose(busnr
);
524 printk(KERN_ERR
"Can't find hose for PCI bus %d!\n", busnr
);
528 bus
.ops
= hose
? hose
->ops
: &null_pci_ops
;
532 #define EARLY_PCI_OP(rw, size, type) \
533 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
534 int devfn, int offset, type value) \
536 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
537 devfn, offset, value); \
540 EARLY_PCI_OP(read
, byte
, u8
*)
541 EARLY_PCI_OP(read
, word
, u16
*)
542 EARLY_PCI_OP(read
, dword
, u32
*)
543 EARLY_PCI_OP(write
, byte
, u8
)
544 EARLY_PCI_OP(write
, word
, u16
)
545 EARLY_PCI_OP(write
, dword
, u32
)
547 extern int pci_bus_find_capability (struct pci_bus
*bus
, unsigned int devfn
, int cap
);
548 int early_find_capability(struct pci_controller
*hose
, int bus
, int devfn
,
551 return pci_bus_find_capability(fake_pci_bus(hose
, bus
), devfn
, cap
);