xref: /openbmc/linux/arch/powerpc/kernel/pci_32.c (revision 6b82b3e4)
1e05b3b4aSPaul Mackerras /*
2e05b3b4aSPaul Mackerras  * Common pmac/prep/chrp pci routines. -- Cort
3e05b3b4aSPaul Mackerras  */
4e05b3b4aSPaul Mackerras 
5e05b3b4aSPaul Mackerras #include <linux/kernel.h>
6e05b3b4aSPaul Mackerras #include <linux/pci.h>
7e05b3b4aSPaul Mackerras #include <linux/delay.h>
8e05b3b4aSPaul Mackerras #include <linux/string.h>
9e05b3b4aSPaul Mackerras #include <linux/init.h>
10e05b3b4aSPaul Mackerras #include <linux/capability.h>
11e05b3b4aSPaul Mackerras #include <linux/sched.h>
12e05b3b4aSPaul Mackerras #include <linux/errno.h>
13e05b3b4aSPaul Mackerras #include <linux/bootmem.h>
146e99e458SBenjamin Herrenschmidt #include <linux/irq.h>
15f90bb153SBenjamin Herrenschmidt #include <linux/list.h>
1666524b22SStephen Rothwell #include <linux/of.h>
17e05b3b4aSPaul Mackerras 
18e05b3b4aSPaul Mackerras #include <asm/processor.h>
19e05b3b4aSPaul Mackerras #include <asm/io.h>
20e05b3b4aSPaul Mackerras #include <asm/prom.h>
21e05b3b4aSPaul Mackerras #include <asm/sections.h>
22e05b3b4aSPaul Mackerras #include <asm/pci-bridge.h>
23e05b3b4aSPaul Mackerras #include <asm/byteorder.h>
24e05b3b4aSPaul Mackerras #include <asm/uaccess.h>
25e05b3b4aSPaul Mackerras #include <asm/machdep.h>
26e05b3b4aSPaul Mackerras 
27e05b3b4aSPaul Mackerras #undef DEBUG
28e05b3b4aSPaul Mackerras 
29e05b3b4aSPaul Mackerras unsigned long isa_io_base     = 0;
30e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0;
31e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1;
32e05b3b4aSPaul Mackerras 
33e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void);
34e05b3b4aSPaul Mackerras 
35e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev);
36e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev);
37e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
38e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map;
39e05b3b4aSPaul Mackerras #endif
40e05b3b4aSPaul Mackerras 
41e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on
42e05b3b4aSPaul Mackerras  * some pmacs
43e05b3b4aSPaul Mackerras  */
44fc3fb71cSBenjamin Herrenschmidt static int pci_assign_all_buses;
45e05b3b4aSPaul Mackerras 
46a4c9e328SKumar Gala LIST_HEAD(hose_list);
47e05b3b4aSPaul Mackerras 
48e05b3b4aSPaul Mackerras static int pci_bus_count;
49e05b3b4aSPaul Mackerras 
507b6b574cSBenjamin Herrenschmidt /* This will remain NULL for now, until isa-bridge.c is made common
517b6b574cSBenjamin Herrenschmidt  * to both 32-bit and 64-bit.
527b6b574cSBenjamin Herrenschmidt  */
537b6b574cSBenjamin Herrenschmidt struct pci_dev *isa_bridge_pcidev;
547b6b574cSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
557b6b574cSBenjamin Herrenschmidt 
56e05b3b4aSPaul Mackerras static void
572052d6d2SKumar Gala fixup_hide_host_resource_fsl(struct pci_dev *dev)
582052d6d2SKumar Gala {
592052d6d2SKumar Gala 	int i, class = dev->class >> 8;
602052d6d2SKumar Gala 
614a015c37SJohn Rigby 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
624a015c37SJohn Rigby 	     class == PCI_CLASS_BRIDGE_OTHER) &&
632052d6d2SKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
642052d6d2SKumar Gala 		(dev->bus->parent == NULL)) {
652052d6d2SKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
662052d6d2SKumar Gala 			dev->resource[i].start = 0;
672052d6d2SKumar Gala 			dev->resource[i].end = 0;
682052d6d2SKumar Gala 			dev->resource[i].flags = 0;
692052d6d2SKumar Gala 		}
702052d6d2SKumar Gala 	}
712052d6d2SKumar Gala }
722052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
732052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
742052d6d2SKumar Gala 
752052d6d2SKumar Gala static void
76e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev)
77e05b3b4aSPaul Mackerras {
78e05b3b4aSPaul Mackerras 	if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
79e05b3b4aSPaul Mackerras 		dev->vendor = PCI_VENDOR_ID_AMD;
80e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
81e05b3b4aSPaul Mackerras 	}
82e05b3b4aSPaul Mackerras }
83e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT,	PCI_ANY_ID,			fixup_broken_pcnet32);
84e05b3b4aSPaul Mackerras 
85e05b3b4aSPaul Mackerras static void
86e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev)
87e05b3b4aSPaul Mackerras {
88e05b3b4aSPaul Mackerras 	/* Hide the PCI64 BARs from the kernel as their content doesn't
89e05b3b4aSPaul Mackerras 	 * fit well in the resource management
90e05b3b4aSPaul Mackerras 	 */
91e05b3b4aSPaul Mackerras 	dev->resource[0].start = dev->resource[0].end = 0;
92e05b3b4aSPaul Mackerras 	dev->resource[0].flags = 0;
93e05b3b4aSPaul Mackerras 	dev->resource[1].start = dev->resource[1].end = 0;
94e05b3b4aSPaul Mackerras 	dev->resource[1].flags = 0;
95e05b3b4aSPaul Mackerras }
96e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,	PCI_DEVICE_ID_IBM_CPC710_PCI64,	fixup_cpc710_pci64);
97e05b3b4aSPaul Mackerras 
98e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
99e05b3b4aSPaul Mackerras /*
100e05b3b4aSPaul Mackerras  * Functions below are used on OpenFirmware machines.
101e05b3b4aSPaul Mackerras  */
102e05b3b4aSPaul Mackerras static void
103e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus)
104e05b3b4aSPaul Mackerras {
105a7f67bdfSJeremy Kerr 	const int *bus_range;
106e05b3b4aSPaul Mackerras 	int len;
107e05b3b4aSPaul Mackerras 
108e05b3b4aSPaul Mackerras 	if (pci_bus >= pci_bus_count)
109e05b3b4aSPaul Mackerras 		return;
110e2eb6392SStephen Rothwell 	bus_range = of_get_property(node, "bus-range", &len);
111e05b3b4aSPaul Mackerras 	if (bus_range == NULL || len < 2 * sizeof(int)) {
112e05b3b4aSPaul Mackerras 		printk(KERN_WARNING "Can't get bus-range for %s, "
113e05b3b4aSPaul Mackerras 		       "assuming it starts at 0\n", node->full_name);
114e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = 0;
115e05b3b4aSPaul Mackerras 	} else
116e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = bus_range[0];
117e05b3b4aSPaul Mackerras 
11866524b22SStephen Rothwell 	for_each_child_of_node(node, node) {
119e05b3b4aSPaul Mackerras 		struct pci_dev* dev;
120a7f67bdfSJeremy Kerr 		const unsigned int *class_code, *reg;
121e05b3b4aSPaul Mackerras 
122e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
123e05b3b4aSPaul Mackerras 		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
124e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
125e05b3b4aSPaul Mackerras 			continue;
126e2eb6392SStephen Rothwell 		reg = of_get_property(node, "reg", NULL);
127e05b3b4aSPaul Mackerras 		if (!reg)
128e05b3b4aSPaul Mackerras 			continue;
129ab462768SAlan Cox 		dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
130ab462768SAlan Cox 		if (!dev || !dev->subordinate) {
131ab462768SAlan Cox 			pci_dev_put(dev);
132e05b3b4aSPaul Mackerras 			continue;
133ab462768SAlan Cox 		}
134e05b3b4aSPaul Mackerras 		make_one_node_map(node, dev->subordinate->number);
135ab462768SAlan Cox 		pci_dev_put(dev);
136e05b3b4aSPaul Mackerras 	}
137e05b3b4aSPaul Mackerras }
138e05b3b4aSPaul Mackerras 
139e05b3b4aSPaul Mackerras void
140e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void)
141e05b3b4aSPaul Mackerras {
142e05b3b4aSPaul Mackerras 	int i;
143a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
144a7f67bdfSJeremy Kerr 	struct property *map_prop;
1458c8dc322SStephen Rothwell 	struct device_node *dn;
146e05b3b4aSPaul Mackerras 
1475cbded58SRobert P. J. Day 	pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
148e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map) {
149e05b3b4aSPaul Mackerras 		printk(KERN_ERR "Can't allocate OF bus map !\n");
150e05b3b4aSPaul Mackerras 		return;
151e05b3b4aSPaul Mackerras 	}
152e05b3b4aSPaul Mackerras 
153e05b3b4aSPaul Mackerras 	/* We fill the bus map with invalid values, that helps
154e05b3b4aSPaul Mackerras 	 * debugging.
155e05b3b4aSPaul Mackerras 	 */
156e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++)
157e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[i] = 0xff;
158e05b3b4aSPaul Mackerras 
159e05b3b4aSPaul Mackerras 	/* For each hose, we begin searching bridges */
160a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
16144ef3390SStephen Rothwell 		struct device_node* node = hose->dn;
16244ef3390SStephen Rothwell 
163e05b3b4aSPaul Mackerras 		if (!node)
164e05b3b4aSPaul Mackerras 			continue;
165e05b3b4aSPaul Mackerras 		make_one_node_map(node, hose->first_busno);
166e05b3b4aSPaul Mackerras 	}
1678c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
1688c8dc322SStephen Rothwell 	map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
169a7f67bdfSJeremy Kerr 	if (map_prop) {
170a7f67bdfSJeremy Kerr 		BUG_ON(pci_bus_count > map_prop->length);
171a7f67bdfSJeremy Kerr 		memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
172a7f67bdfSJeremy Kerr 	}
1738c8dc322SStephen Rothwell 	of_node_put(dn);
174e05b3b4aSPaul Mackerras #ifdef DEBUG
175e05b3b4aSPaul Mackerras 	printk("PCI->OF bus map:\n");
176e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++) {
177e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[i] == 0xff)
178e05b3b4aSPaul Mackerras 			continue;
179e05b3b4aSPaul Mackerras 		printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
180e05b3b4aSPaul Mackerras 	}
181e05b3b4aSPaul Mackerras #endif
182e05b3b4aSPaul Mackerras }
183e05b3b4aSPaul Mackerras 
184e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
185e05b3b4aSPaul Mackerras 
186e05b3b4aSPaul Mackerras static struct device_node*
18766524b22SStephen Rothwell scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
188e05b3b4aSPaul Mackerras {
18966524b22SStephen Rothwell 	struct device_node *node;
190e05b3b4aSPaul Mackerras 	struct device_node* sub_node;
191e05b3b4aSPaul Mackerras 
19266524b22SStephen Rothwell 	for_each_child_of_node(parent, node) {
193a7f67bdfSJeremy Kerr 		const unsigned int *class_code;
194e05b3b4aSPaul Mackerras 
19566524b22SStephen Rothwell 		if (filter(node, data)) {
19666524b22SStephen Rothwell 			of_node_put(node);
197e05b3b4aSPaul Mackerras 			return node;
19866524b22SStephen Rothwell 		}
199e05b3b4aSPaul Mackerras 
200e05b3b4aSPaul Mackerras 		/* For PCI<->PCI bridges or CardBus bridges, we go down
201e05b3b4aSPaul Mackerras 		 * Note: some OFs create a parent node "multifunc-device" as
202e05b3b4aSPaul Mackerras 		 * a fake root for all functions of a multi-function device,
203e05b3b4aSPaul Mackerras 		 * we go down them as well.
204e05b3b4aSPaul Mackerras 		 */
205e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
206e05b3b4aSPaul Mackerras 		if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
207e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
208e05b3b4aSPaul Mackerras 			strcmp(node->name, "multifunc-device"))
209e05b3b4aSPaul Mackerras 			continue;
21066524b22SStephen Rothwell 		sub_node = scan_OF_pci_childs(node, filter, data);
21166524b22SStephen Rothwell 		if (sub_node) {
21266524b22SStephen Rothwell 			of_node_put(node);
213e05b3b4aSPaul Mackerras 			return sub_node;
214e05b3b4aSPaul Mackerras 		}
21566524b22SStephen Rothwell 	}
216e05b3b4aSPaul Mackerras 	return NULL;
217e05b3b4aSPaul Mackerras }
218e05b3b4aSPaul Mackerras 
219dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
220dae4828dSBenjamin Herrenschmidt 					       unsigned int devfn)
221e05b3b4aSPaul Mackerras {
22285e99b9fSStephen Rothwell 	struct device_node *np;
223dae4828dSBenjamin Herrenschmidt 	const u32 *reg;
224dae4828dSBenjamin Herrenschmidt 	unsigned int psize;
225e05b3b4aSPaul Mackerras 
22685e99b9fSStephen Rothwell 	for_each_child_of_node(parent, np) {
227e2eb6392SStephen Rothwell 		reg = of_get_property(np, "reg", &psize);
228dae4828dSBenjamin Herrenschmidt 		if (reg == NULL || psize < 4)
229dae4828dSBenjamin Herrenschmidt 			continue;
230dae4828dSBenjamin Herrenschmidt 		if (((reg[0] >> 8) & 0xff) == devfn)
231dae4828dSBenjamin Herrenschmidt 			return np;
232dae4828dSBenjamin Herrenschmidt 	}
233dae4828dSBenjamin Herrenschmidt 	return NULL;
234e05b3b4aSPaul Mackerras }
235e05b3b4aSPaul Mackerras 
236e05b3b4aSPaul Mackerras 
237dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
238dae4828dSBenjamin Herrenschmidt {
239dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
240dae4828dSBenjamin Herrenschmidt 
241dae4828dSBenjamin Herrenschmidt 	/* Are we a root bus ? */
242dae4828dSBenjamin Herrenschmidt 	if (bus->self == NULL || bus->parent == NULL) {
2430b1d40c4SKumar Gala 		struct pci_controller *hose = pci_bus_to_host(bus);
244dae4828dSBenjamin Herrenschmidt 		if (hose == NULL)
245dae4828dSBenjamin Herrenschmidt 			return NULL;
24644ef3390SStephen Rothwell 		return of_node_get(hose->dn);
247dae4828dSBenjamin Herrenschmidt 	}
248dae4828dSBenjamin Herrenschmidt 
249dae4828dSBenjamin Herrenschmidt 	/* not a root bus, we need to get our parent */
250dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus->parent);
251dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
252dae4828dSBenjamin Herrenschmidt 		return NULL;
253dae4828dSBenjamin Herrenschmidt 
254dae4828dSBenjamin Herrenschmidt 	/* now iterate for children for a match */
255dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, bus->self->devfn);
256dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
257dae4828dSBenjamin Herrenschmidt 
258dae4828dSBenjamin Herrenschmidt 	return np;
259e05b3b4aSPaul Mackerras }
260e05b3b4aSPaul Mackerras 
261e05b3b4aSPaul Mackerras /*
262e05b3b4aSPaul Mackerras  * Scans the OF tree for a device node matching a PCI device
263e05b3b4aSPaul Mackerras  */
264e05b3b4aSPaul Mackerras struct device_node *
265e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
266e05b3b4aSPaul Mackerras {
267dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
268e05b3b4aSPaul Mackerras 
269b0494bc8SBenjamin Herrenschmidt 	pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
270dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus);
271dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
272e05b3b4aSPaul Mackerras 		return NULL;
273b0494bc8SBenjamin Herrenschmidt 	pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
274dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, devfn);
275dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
276b0494bc8SBenjamin Herrenschmidt 	pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
277e05b3b4aSPaul Mackerras 
278dae4828dSBenjamin Herrenschmidt 	/* XXX most callers don't release the returned node
279dae4828dSBenjamin Herrenschmidt 	 * mostly because ppc64 doesn't increase the refcount,
280dae4828dSBenjamin Herrenschmidt 	 * we need to fix that.
281e05b3b4aSPaul Mackerras 	 */
282dae4828dSBenjamin Herrenschmidt 	return np;
283e05b3b4aSPaul Mackerras }
284e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node);
285e05b3b4aSPaul Mackerras 
286e05b3b4aSPaul Mackerras struct device_node*
287e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev)
288e05b3b4aSPaul Mackerras {
289e05b3b4aSPaul Mackerras 	return pci_busdev_to_OF_node(dev->bus, dev->devfn);
290e05b3b4aSPaul Mackerras }
291e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node);
292e05b3b4aSPaul Mackerras 
293e05b3b4aSPaul Mackerras static int
294e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data)
295e05b3b4aSPaul Mackerras {
296e05b3b4aSPaul Mackerras 	return ((void *)node == data);
297e05b3b4aSPaul Mackerras }
298e05b3b4aSPaul Mackerras 
299e05b3b4aSPaul Mackerras /*
300e05b3b4aSPaul Mackerras  * Returns the PCI device matching a given OF node
301e05b3b4aSPaul Mackerras  */
302e05b3b4aSPaul Mackerras int
303e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
304e05b3b4aSPaul Mackerras {
305a7f67bdfSJeremy Kerr 	const unsigned int *reg;
306e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
307e05b3b4aSPaul Mackerras 	struct pci_dev* dev = NULL;
308e05b3b4aSPaul Mackerras 
309e05b3b4aSPaul Mackerras 	/* Make sure it's really a PCI device */
310e05b3b4aSPaul Mackerras 	hose = pci_find_hose_for_OF_device(node);
31144ef3390SStephen Rothwell 	if (!hose || !hose->dn)
312e05b3b4aSPaul Mackerras 		return -ENODEV;
31366524b22SStephen Rothwell 	if (!scan_OF_pci_childs(hose->dn,
314e05b3b4aSPaul Mackerras 			find_OF_pci_device_filter, (void *)node))
315e05b3b4aSPaul Mackerras 		return -ENODEV;
316e2eb6392SStephen Rothwell 	reg = of_get_property(node, "reg", NULL);
317e05b3b4aSPaul Mackerras 	if (!reg)
318e05b3b4aSPaul Mackerras 		return -ENODEV;
319e05b3b4aSPaul Mackerras 	*bus = (reg[0] >> 16) & 0xff;
320e05b3b4aSPaul Mackerras 	*devfn = ((reg[0] >> 8) & 0xff);
321e05b3b4aSPaul Mackerras 
322e05b3b4aSPaul Mackerras 	/* Ok, here we need some tweak. If we have already renumbered
323e05b3b4aSPaul Mackerras 	 * all busses, we can't rely on the OF bus number any more.
324e05b3b4aSPaul Mackerras 	 * the pci_to_OF_bus_map is not enough as several PCI busses
325e05b3b4aSPaul Mackerras 	 * may match the same OF bus number.
326e05b3b4aSPaul Mackerras 	 */
327e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map)
328e05b3b4aSPaul Mackerras 		return 0;
329e05b3b4aSPaul Mackerras 
330e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev)
331e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
332e05b3b4aSPaul Mackerras 				dev->devfn == *devfn) {
333e05b3b4aSPaul Mackerras 			*bus = dev->bus->number;
334e05b3b4aSPaul Mackerras 			pci_dev_put(dev);
335e05b3b4aSPaul Mackerras 			return 0;
336e05b3b4aSPaul Mackerras 		}
337e05b3b4aSPaul Mackerras 
338e05b3b4aSPaul Mackerras 	return -ENODEV;
339e05b3b4aSPaul Mackerras }
340e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node);
341e05b3b4aSPaul Mackerras 
342e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the
343e05b3b4aSPaul Mackerras  * /proc device tree
344e05b3b4aSPaul Mackerras  */
345e05b3b4aSPaul Mackerras void __init
346e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void)
347e05b3b4aSPaul Mackerras {
348e05b3b4aSPaul Mackerras 	struct property* of_prop;
3498c8dc322SStephen Rothwell 	struct device_node *dn;
350e05b3b4aSPaul Mackerras 
351e05b3b4aSPaul Mackerras 	of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
3528c8dc322SStephen Rothwell 	if (!of_prop)
3538c8dc322SStephen Rothwell 		return;
3548c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
3558c8dc322SStephen Rothwell 	if (dn) {
356e05b3b4aSPaul Mackerras 		memset(of_prop, -1, sizeof(struct property) + 256);
357e05b3b4aSPaul Mackerras 		of_prop->name = "pci-OF-bus-map";
358e05b3b4aSPaul Mackerras 		of_prop->length = 256;
3591a38147eSStephen Rothwell 		of_prop->value = &of_prop[1];
3608c8dc322SStephen Rothwell 		prom_add_property(dn, of_prop);
3618c8dc322SStephen Rothwell 		of_node_put(dn);
362e05b3b4aSPaul Mackerras 	}
363e05b3b4aSPaul Mackerras }
364e05b3b4aSPaul Mackerras 
365e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */
366e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void)
367e05b3b4aSPaul Mackerras {
368e05b3b4aSPaul Mackerras }
369e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */
370e05b3b4aSPaul Mackerras 
37153280323SBenjamin Herrenschmidt static void __devinit pcibios_scan_phb(struct pci_controller *hose)
37253280323SBenjamin Herrenschmidt {
37353280323SBenjamin Herrenschmidt 	struct pci_bus *bus;
37453280323SBenjamin Herrenschmidt 	struct device_node *node = hose->dn;
37553280323SBenjamin Herrenschmidt 	unsigned long io_offset;
37653280323SBenjamin Herrenschmidt 	struct resource *res = &hose->io_resource;
37753280323SBenjamin Herrenschmidt 
37853280323SBenjamin Herrenschmidt 	pr_debug("PCI: Scanning PHB %s\n",
37953280323SBenjamin Herrenschmidt 		 node ? node->full_name : "<NO NAME>");
38053280323SBenjamin Herrenschmidt 
38153280323SBenjamin Herrenschmidt 	/* Create an empty bus for the toplevel */
38253280323SBenjamin Herrenschmidt 	bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
38353280323SBenjamin Herrenschmidt 	if (bus == NULL) {
38453280323SBenjamin Herrenschmidt 		printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
38553280323SBenjamin Herrenschmidt 		       hose->global_number);
38653280323SBenjamin Herrenschmidt 		return;
38753280323SBenjamin Herrenschmidt 	}
38853280323SBenjamin Herrenschmidt 	bus->secondary = hose->first_busno;
38953280323SBenjamin Herrenschmidt 	hose->bus = bus;
39053280323SBenjamin Herrenschmidt 
39153280323SBenjamin Herrenschmidt 	/* Fixup IO space offset */
39253280323SBenjamin Herrenschmidt 	io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
39353280323SBenjamin Herrenschmidt 	res->start = (res->start + io_offset) & 0xffffffffu;
39453280323SBenjamin Herrenschmidt 	res->end = (res->end + io_offset) & 0xffffffffu;
39553280323SBenjamin Herrenschmidt 
39653280323SBenjamin Herrenschmidt 	/* Wire up PHB bus resources */
39753280323SBenjamin Herrenschmidt 	pcibios_setup_phb_resources(hose);
39853280323SBenjamin Herrenschmidt 
39953280323SBenjamin Herrenschmidt 	/* Scan children */
40053280323SBenjamin Herrenschmidt 	hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
40153280323SBenjamin Herrenschmidt }
40253280323SBenjamin Herrenschmidt 
4033fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void)
404e05b3b4aSPaul Mackerras {
405a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
406a4c9e328SKumar Gala 	int next_busno = 0;
407e05b3b4aSPaul Mackerras 
408e05b3b4aSPaul Mackerras 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
409e05b3b4aSPaul Mackerras 
410fc3fb71cSBenjamin Herrenschmidt 	if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
411fc3fb71cSBenjamin Herrenschmidt 		pci_assign_all_buses = 1;
412fc3fb71cSBenjamin Herrenschmidt 
413e05b3b4aSPaul Mackerras 	/* Scan all of the recorded PCI controllers.  */
414a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
415e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses)
416e05b3b4aSPaul Mackerras 			hose->first_busno = next_busno;
417e05b3b4aSPaul Mackerras 		hose->last_busno = 0xff;
41853280323SBenjamin Herrenschmidt 		pcibios_scan_phb(hose);
41953280323SBenjamin Herrenschmidt 		pci_bus_add_devices(hose->bus);
420e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses || next_busno <= hose->last_busno)
421e05b3b4aSPaul Mackerras 			next_busno = hose->last_busno + pcibios_assign_bus_offset;
422e05b3b4aSPaul Mackerras 	}
423e05b3b4aSPaul Mackerras 	pci_bus_count = next_busno;
424e05b3b4aSPaul Mackerras 
425e05b3b4aSPaul Mackerras 	/* OpenFirmware based machines need a map of OF bus
426e05b3b4aSPaul Mackerras 	 * numbers vs. kernel bus numbers since we may have to
427e05b3b4aSPaul Mackerras 	 * remap them.
428e05b3b4aSPaul Mackerras 	 */
4296b82b3e4SAnton Vorontsov 	if (pci_assign_all_buses)
430e05b3b4aSPaul Mackerras 		pcibios_make_OF_bus_map();
431e05b3b4aSPaul Mackerras 
4323fd94c6bSBenjamin Herrenschmidt 	/* Call common code to handle resource allocation */
4333fd94c6bSBenjamin Herrenschmidt 	pcibios_resource_survey();
434e05b3b4aSPaul Mackerras 
435e05b3b4aSPaul Mackerras 	/* Call machine dependent post-init code */
436e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_after_init)
437e05b3b4aSPaul Mackerras 		ppc_md.pcibios_after_init();
438e05b3b4aSPaul Mackerras 
439e05b3b4aSPaul Mackerras 	return 0;
440e05b3b4aSPaul Mackerras }
441e05b3b4aSPaul Mackerras 
442e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init);
443e05b3b4aSPaul Mackerras 
444e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */
445e05b3b4aSPaul Mackerras void __init
446e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq)
447e05b3b4aSPaul Mackerras {
448e05b3b4aSPaul Mackerras 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
449e05b3b4aSPaul Mackerras 	/* XXX FIXME - update OF device tree node interrupt property */
450e05b3b4aSPaul Mackerras }
451e05b3b4aSPaul Mackerras 
4520b1d40c4SKumar Gala static struct pci_controller*
453e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus)
454e05b3b4aSPaul Mackerras {
455a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
456e05b3b4aSPaul Mackerras 
457a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
458e05b3b4aSPaul Mackerras 		if (bus >= hose->first_busno && bus <= hose->last_busno)
459e05b3b4aSPaul Mackerras 			return hose;
460e05b3b4aSPaul Mackerras 	return NULL;
461e05b3b4aSPaul Mackerras }
462e05b3b4aSPaul Mackerras 
463e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical
464e05b3b4aSPaul Mackerras  * memory.  Do this on a per-card basis so that we choose the right
465e05b3b4aSPaul Mackerras  * root bridge.
466e05b3b4aSPaul Mackerras  * Note that the returned IO or memory base is a physical address
467e05b3b4aSPaul Mackerras  */
468e05b3b4aSPaul Mackerras 
469e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
470e05b3b4aSPaul Mackerras {
471e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
472e05b3b4aSPaul Mackerras 	long result = -EOPNOTSUPP;
473e05b3b4aSPaul Mackerras 
474e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
475e05b3b4aSPaul Mackerras 	if (!hose)
476e05b3b4aSPaul Mackerras 		return -ENODEV;
477e05b3b4aSPaul Mackerras 
478e05b3b4aSPaul Mackerras 	switch (which) {
479e05b3b4aSPaul Mackerras 	case IOBASE_BRIDGE_NUMBER:
480e05b3b4aSPaul Mackerras 		return (long)hose->first_busno;
481e05b3b4aSPaul Mackerras 	case IOBASE_MEMORY:
482e05b3b4aSPaul Mackerras 		return (long)hose->pci_mem_offset;
483e05b3b4aSPaul Mackerras 	case IOBASE_IO:
484e05b3b4aSPaul Mackerras 		return (long)hose->io_base_phys;
485e05b3b4aSPaul Mackerras 	case IOBASE_ISA_IO:
486e05b3b4aSPaul Mackerras 		return (long)isa_io_base;
487e05b3b4aSPaul Mackerras 	case IOBASE_ISA_MEM:
488e05b3b4aSPaul Mackerras 		return (long)isa_mem_base;
489e05b3b4aSPaul Mackerras 	}
490e05b3b4aSPaul Mackerras 
491e05b3b4aSPaul Mackerras 	return result;
492e05b3b4aSPaul Mackerras }
493e05b3b4aSPaul Mackerras 
494e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address)
495e05b3b4aSPaul Mackerras {
496a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
497e05b3b4aSPaul Mackerras 
498a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
499e05b3b4aSPaul Mackerras 		unsigned int size = hose->io_resource.end -
500e05b3b4aSPaul Mackerras 			hose->io_resource.start + 1;
501e05b3b4aSPaul Mackerras 		if (address >= hose->io_base_phys &&
502e05b3b4aSPaul Mackerras 		    address < (hose->io_base_phys + size)) {
503e05b3b4aSPaul Mackerras 			unsigned long base =
504e05b3b4aSPaul Mackerras 				(unsigned long)hose->io_base_virt - _IO_BASE;
505e05b3b4aSPaul Mackerras 			return base + (address - hose->io_base_phys);
506e05b3b4aSPaul Mackerras 		}
507e05b3b4aSPaul Mackerras 	}
508e05b3b4aSPaul Mackerras 	return (unsigned int)-1;
509e05b3b4aSPaul Mackerras }
510e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio);
511e05b3b4aSPaul Mackerras 
512e05b3b4aSPaul Mackerras /*
513e05b3b4aSPaul Mackerras  * Null PCI config access functions, for the case when we can't
514e05b3b4aSPaul Mackerras  * find a hose.
515e05b3b4aSPaul Mackerras  */
516e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type)					\
517e05b3b4aSPaul Mackerras static int								\
518e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
519e05b3b4aSPaul Mackerras {									\
520e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
521e05b3b4aSPaul Mackerras }
522e05b3b4aSPaul Mackerras 
523e05b3b4aSPaul Mackerras static int
524e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
525e05b3b4aSPaul Mackerras 		 int len, u32 *val)
526e05b3b4aSPaul Mackerras {
527e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
528e05b3b4aSPaul Mackerras }
529e05b3b4aSPaul Mackerras 
530e05b3b4aSPaul Mackerras static int
531e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
532e05b3b4aSPaul Mackerras 		  int len, u32 val)
533e05b3b4aSPaul Mackerras {
534e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
535e05b3b4aSPaul Mackerras }
536e05b3b4aSPaul Mackerras 
537e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops =
538e05b3b4aSPaul Mackerras {
5396127d1c0SNathan Lynch 	.read = null_read_config,
5406127d1c0SNathan Lynch 	.write = null_write_config,
541e05b3b4aSPaul Mackerras };
542e05b3b4aSPaul Mackerras 
543e05b3b4aSPaul Mackerras /*
544e05b3b4aSPaul Mackerras  * These functions are used early on before PCI scanning is done
545e05b3b4aSPaul Mackerras  * and all of the pci_dev and pci_bus structures have been created.
546e05b3b4aSPaul Mackerras  */
547e05b3b4aSPaul Mackerras static struct pci_bus *
548e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr)
549e05b3b4aSPaul Mackerras {
550e05b3b4aSPaul Mackerras 	static struct pci_bus bus;
551e05b3b4aSPaul Mackerras 
552e05b3b4aSPaul Mackerras 	if (hose == 0) {
553e05b3b4aSPaul Mackerras 		hose = pci_bus_to_hose(busnr);
554e05b3b4aSPaul Mackerras 		if (hose == 0)
555e05b3b4aSPaul Mackerras 			printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
556e05b3b4aSPaul Mackerras 	}
557e05b3b4aSPaul Mackerras 	bus.number = busnr;
558e05b3b4aSPaul Mackerras 	bus.sysdata = hose;
559e05b3b4aSPaul Mackerras 	bus.ops = hose? hose->ops: &null_pci_ops;
560e05b3b4aSPaul Mackerras 	return &bus;
561e05b3b4aSPaul Mackerras }
562e05b3b4aSPaul Mackerras 
563e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type)					\
564e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
565e05b3b4aSPaul Mackerras 			       int devfn, int offset, type value)	\
566e05b3b4aSPaul Mackerras {									\
567e05b3b4aSPaul Mackerras 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
568e05b3b4aSPaul Mackerras 					    devfn, offset, value);	\
569e05b3b4aSPaul Mackerras }
570e05b3b4aSPaul Mackerras 
571e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *)
572e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *)
573e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *)
574e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8)
575e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16)
576e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32)
57738805e5fSKumar Gala 
57838805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
57938805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
58038805e5fSKumar Gala 			  int cap)
58138805e5fSKumar Gala {
58238805e5fSKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
58338805e5fSKumar Gala }
584