xref: /openbmc/linux/arch/mips/pci/pci-legacy.c (revision 04c81c7293df875ca6a46e2c9a272c7ec72e5145)
1f8091a88SPaul Burton /*
2f8091a88SPaul Burton  * This program is free software; you can redistribute	it and/or modify it
3f8091a88SPaul Burton  * under  the terms of	the GNU General	 Public License as published by the
4f8091a88SPaul Burton  * Free Software Foundation;  either version 2 of the  License, or (at your
5f8091a88SPaul Burton  * option) any later version.
6f8091a88SPaul Burton  *
7f8091a88SPaul Burton  * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org)
8f8091a88SPaul Burton  * Copyright (C) 2011 Wind River Systems,
9f8091a88SPaul Burton  *   written by Ralf Baechle (ralf@linux-mips.org)
10f8091a88SPaul Burton  */
11f8091a88SPaul Burton #include <linux/bug.h>
12f8091a88SPaul Burton #include <linux/kernel.h>
13f8091a88SPaul Burton #include <linux/mm.h>
14f8091a88SPaul Burton #include <linux/bootmem.h>
15f8091a88SPaul Burton #include <linux/export.h>
16f8091a88SPaul Burton #include <linux/init.h>
17f8091a88SPaul Burton #include <linux/types.h>
18f8091a88SPaul Burton #include <linux/pci.h>
19f8091a88SPaul Burton #include <linux/of_address.h>
20f8091a88SPaul Burton 
21f8091a88SPaul Burton #include <asm/cpu-info.h>
22f8091a88SPaul Burton 
23f8091a88SPaul Burton /*
24f8091a88SPaul Burton  * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
25f8091a88SPaul Burton  * assignments.
26f8091a88SPaul Burton  */
27f8091a88SPaul Burton 
28f8091a88SPaul Burton /*
29f8091a88SPaul Burton  * The PCI controller list.
30f8091a88SPaul Burton  */
31f8091a88SPaul Burton static LIST_HEAD(controllers);
32f8091a88SPaul Burton 
33f8091a88SPaul Burton static int pci_initialized;
34f8091a88SPaul Burton 
35f8091a88SPaul Burton /*
36f8091a88SPaul Burton  * We need to avoid collisions with `mirrored' VGA ports
37f8091a88SPaul Burton  * and other strange ISA hardware, so we always want the
38f8091a88SPaul Burton  * addresses to be allocated in the 0x000-0x0ff region
39f8091a88SPaul Burton  * modulo 0x400.
40f8091a88SPaul Burton  *
41f8091a88SPaul Burton  * Why? Because some silly external IO cards only decode
42f8091a88SPaul Burton  * the low 10 bits of the IO address. The 0x00-0xff region
43f8091a88SPaul Burton  * is reserved for motherboard devices that decode all 16
44f8091a88SPaul Burton  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
45f8091a88SPaul Burton  * but we want to try to avoid allocating at 0x2900-0x2bff
46f8091a88SPaul Burton  * which might have be mirrored at 0x0100-0x03ff..
47f8091a88SPaul Burton  */
48f8091a88SPaul Burton resource_size_t
49f8091a88SPaul Burton pcibios_align_resource(void *data, const struct resource *res,
50f8091a88SPaul Burton 		       resource_size_t size, resource_size_t align)
51f8091a88SPaul Burton {
52f8091a88SPaul Burton 	struct pci_dev *dev = data;
53f8091a88SPaul Burton 	struct pci_controller *hose = dev->sysdata;
54f8091a88SPaul Burton 	resource_size_t start = res->start;
55f8091a88SPaul Burton 
56f8091a88SPaul Burton 	if (res->flags & IORESOURCE_IO) {
57f8091a88SPaul Burton 		/* Make sure we start at our min on all hoses */
58f8091a88SPaul Burton 		if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
59f8091a88SPaul Burton 			start = PCIBIOS_MIN_IO + hose->io_resource->start;
60f8091a88SPaul Burton 
61f8091a88SPaul Burton 		/*
62f8091a88SPaul Burton 		 * Put everything into 0x00-0xff region modulo 0x400
63f8091a88SPaul Burton 		 */
64f8091a88SPaul Burton 		if (start & 0x300)
65f8091a88SPaul Burton 			start = (start + 0x3ff) & ~0x3ff;
66f8091a88SPaul Burton 	} else if (res->flags & IORESOURCE_MEM) {
67f8091a88SPaul Burton 		/* Make sure we start at our min on all hoses */
68f8091a88SPaul Burton 		if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
69f8091a88SPaul Burton 			start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
70f8091a88SPaul Burton 	}
71f8091a88SPaul Burton 
72f8091a88SPaul Burton 	return start;
73f8091a88SPaul Burton }
74f8091a88SPaul Burton 
75f8091a88SPaul Burton static void pcibios_scanbus(struct pci_controller *hose)
76f8091a88SPaul Burton {
77f8091a88SPaul Burton 	static int next_busno;
78f8091a88SPaul Burton 	static int need_domain_info;
79f8091a88SPaul Burton 	LIST_HEAD(resources);
80f8091a88SPaul Burton 	struct pci_bus *bus;
81*04c81c72SLorenzo Pieralisi 	struct pci_host_bridge *bridge;
82*04c81c72SLorenzo Pieralisi 	int ret;
83*04c81c72SLorenzo Pieralisi 
84*04c81c72SLorenzo Pieralisi 	bridge = pci_alloc_host_bridge(0);
85*04c81c72SLorenzo Pieralisi 	if (!bridge)
86*04c81c72SLorenzo Pieralisi 		return;
87f8091a88SPaul Burton 
88f8091a88SPaul Burton 	if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY))
89f8091a88SPaul Burton 		next_busno = (*hose->get_busno)();
90f8091a88SPaul Burton 
91f8091a88SPaul Burton 	pci_add_resource_offset(&resources,
92f8091a88SPaul Burton 				hose->mem_resource, hose->mem_offset);
93f8091a88SPaul Burton 	pci_add_resource_offset(&resources,
94f8091a88SPaul Burton 				hose->io_resource, hose->io_offset);
957ee214b5SBjorn Helgaas 	pci_add_resource(&resources, hose->busn_resource);
96*04c81c72SLorenzo Pieralisi 	list_splice_init(&resources, &bridge->windows);
97*04c81c72SLorenzo Pieralisi 	bridge->dev.parent = NULL;
98*04c81c72SLorenzo Pieralisi 	bridge->sysdata = hose;
99*04c81c72SLorenzo Pieralisi 	bridge->busnr = next_busno;
100*04c81c72SLorenzo Pieralisi 	bridge->ops = hose->pci_ops;
101*04c81c72SLorenzo Pieralisi 	bridge->swizzle_irq = pci_common_swizzle;
102*04c81c72SLorenzo Pieralisi 	bridge->map_irq = pcibios_map_irq;
103*04c81c72SLorenzo Pieralisi 	ret = pci_scan_root_bus_bridge(bridge);
104*04c81c72SLorenzo Pieralisi 	if (ret) {
105*04c81c72SLorenzo Pieralisi 		pci_free_host_bridge(bridge);
106f8091a88SPaul Burton 		return;
107f8091a88SPaul Burton 	}
108f8091a88SPaul Burton 
109*04c81c72SLorenzo Pieralisi 	hose->bus = bus = bridge->bus;
110902d886dSLorenzo Pieralisi 
111902d886dSLorenzo Pieralisi 	need_domain_info = need_domain_info || pci_domain_nr(bus);
112902d886dSLorenzo Pieralisi 	set_pci_need_domain_info(hose, need_domain_info);
113902d886dSLorenzo Pieralisi 
114f8091a88SPaul Burton 	next_busno = bus->busn_res.end + 1;
115f8091a88SPaul Burton 	/* Don't allow 8-bit bus number overflow inside the hose -
116f8091a88SPaul Burton 	   reserve some space for bridges. */
117f8091a88SPaul Burton 	if (next_busno > 224) {
118f8091a88SPaul Burton 		next_busno = 0;
119f8091a88SPaul Burton 		need_domain_info = 1;
120f8091a88SPaul Burton 	}
121f8091a88SPaul Burton 
122f8091a88SPaul Burton 	/*
123f8091a88SPaul Burton 	 * We insert PCI resources into the iomem_resource and
124f8091a88SPaul Burton 	 * ioport_resource trees in either pci_bus_claim_resources()
125f8091a88SPaul Burton 	 * or pci_bus_assign_resources().
126f8091a88SPaul Burton 	 */
127f8091a88SPaul Burton 	if (pci_has_flag(PCI_PROBE_ONLY)) {
128f8091a88SPaul Burton 		pci_bus_claim_resources(bus);
129f8091a88SPaul Burton 	} else {
130f8091a88SPaul Burton 		pci_bus_size_bridges(bus);
131f8091a88SPaul Burton 		pci_bus_assign_resources(bus);
132f8091a88SPaul Burton 	}
133f8091a88SPaul Burton 	pci_bus_add_devices(bus);
134f8091a88SPaul Burton }
135f8091a88SPaul Burton 
136f8091a88SPaul Burton #ifdef CONFIG_OF
137f8091a88SPaul Burton void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node)
138f8091a88SPaul Burton {
139f8091a88SPaul Burton 	struct of_pci_range range;
140f8091a88SPaul Burton 	struct of_pci_range_parser parser;
141f8091a88SPaul Burton 
142f8091a88SPaul Burton 	pr_info("PCI host bridge %s ranges:\n", node->full_name);
143f8091a88SPaul Burton 	hose->of_node = node;
144f8091a88SPaul Burton 
145f8091a88SPaul Burton 	if (of_pci_range_parser_init(&parser, node))
146f8091a88SPaul Burton 		return;
147f8091a88SPaul Burton 
148f8091a88SPaul Burton 	for_each_of_pci_range(&parser, &range) {
149f8091a88SPaul Burton 		struct resource *res = NULL;
150f8091a88SPaul Burton 
151f8091a88SPaul Burton 		switch (range.flags & IORESOURCE_TYPE_BITS) {
152f8091a88SPaul Burton 		case IORESOURCE_IO:
153f8091a88SPaul Burton 			pr_info("  IO 0x%016llx..0x%016llx\n",
154f8091a88SPaul Burton 				range.cpu_addr,
155f8091a88SPaul Burton 				range.cpu_addr + range.size - 1);
156f8091a88SPaul Burton 			hose->io_map_base =
157f8091a88SPaul Burton 				(unsigned long)ioremap(range.cpu_addr,
158f8091a88SPaul Burton 						       range.size);
159f8091a88SPaul Burton 			res = hose->io_resource;
160f8091a88SPaul Burton 			break;
161f8091a88SPaul Burton 		case IORESOURCE_MEM:
162f8091a88SPaul Burton 			pr_info(" MEM 0x%016llx..0x%016llx\n",
163f8091a88SPaul Burton 				range.cpu_addr,
164f8091a88SPaul Burton 				range.cpu_addr + range.size - 1);
165f8091a88SPaul Burton 			res = hose->mem_resource;
166f8091a88SPaul Burton 			break;
167f8091a88SPaul Burton 		}
168f8091a88SPaul Burton 		if (res != NULL)
169f8091a88SPaul Burton 			of_pci_range_to_resource(&range, node, res);
170f8091a88SPaul Burton 	}
171f8091a88SPaul Burton }
172f8091a88SPaul Burton 
173f8091a88SPaul Burton struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
174f8091a88SPaul Burton {
175f8091a88SPaul Burton 	struct pci_controller *hose = bus->sysdata;
176f8091a88SPaul Burton 
177f8091a88SPaul Burton 	return of_node_get(hose->of_node);
178f8091a88SPaul Burton }
179f8091a88SPaul Burton #endif
180f8091a88SPaul Burton 
181f8091a88SPaul Burton static DEFINE_MUTEX(pci_scan_mutex);
182f8091a88SPaul Burton 
183f8091a88SPaul Burton void register_pci_controller(struct pci_controller *hose)
184f8091a88SPaul Burton {
185f8091a88SPaul Burton 	struct resource *parent;
186f8091a88SPaul Burton 
187f8091a88SPaul Burton 	parent = hose->mem_resource->parent;
188f8091a88SPaul Burton 	if (!parent)
189f8091a88SPaul Burton 		parent = &iomem_resource;
190f8091a88SPaul Burton 
191f8091a88SPaul Burton 	if (request_resource(parent, hose->mem_resource) < 0)
192f8091a88SPaul Burton 		goto out;
193f8091a88SPaul Burton 
194f8091a88SPaul Burton 	parent = hose->io_resource->parent;
195f8091a88SPaul Burton 	if (!parent)
196f8091a88SPaul Burton 		parent = &ioport_resource;
197f8091a88SPaul Burton 
198f8091a88SPaul Burton 	if (request_resource(parent, hose->io_resource) < 0) {
199f8091a88SPaul Burton 		release_resource(hose->mem_resource);
200f8091a88SPaul Burton 		goto out;
201f8091a88SPaul Burton 	}
202f8091a88SPaul Burton 
203f8091a88SPaul Burton 	INIT_LIST_HEAD(&hose->list);
204edb0b6a0SMathias Kresin 	list_add_tail(&hose->list, &controllers);
205f8091a88SPaul Burton 
206f8091a88SPaul Burton 	/*
207f8091a88SPaul Burton 	 * Do not panic here but later - this might happen before console init.
208f8091a88SPaul Burton 	 */
209f8091a88SPaul Burton 	if (!hose->io_map_base) {
210f8091a88SPaul Burton 		printk(KERN_WARNING
211f8091a88SPaul Burton 		       "registering PCI controller with io_map_base unset\n");
212f8091a88SPaul Burton 	}
213f8091a88SPaul Burton 
214f8091a88SPaul Burton 	/*
215f8091a88SPaul Burton 	 * Scan the bus if it is register after the PCI subsystem
216f8091a88SPaul Burton 	 * initialization.
217f8091a88SPaul Burton 	 */
218f8091a88SPaul Burton 	if (pci_initialized) {
219f8091a88SPaul Burton 		mutex_lock(&pci_scan_mutex);
220f8091a88SPaul Burton 		pcibios_scanbus(hose);
221f8091a88SPaul Burton 		mutex_unlock(&pci_scan_mutex);
222f8091a88SPaul Burton 	}
223f8091a88SPaul Burton 
224f8091a88SPaul Burton 	return;
225f8091a88SPaul Burton 
226f8091a88SPaul Burton out:
227f8091a88SPaul Burton 	printk(KERN_WARNING
228f8091a88SPaul Burton 	       "Skipping PCI bus scan due to resource conflict\n");
229f8091a88SPaul Burton }
230f8091a88SPaul Burton 
231f8091a88SPaul Burton static int __init pcibios_init(void)
232f8091a88SPaul Burton {
233f8091a88SPaul Burton 	struct pci_controller *hose;
234f8091a88SPaul Burton 
235f8091a88SPaul Burton 	/* Scan all of the recorded PCI controllers.  */
236f8091a88SPaul Burton 	list_for_each_entry(hose, &controllers, list)
237f8091a88SPaul Burton 		pcibios_scanbus(hose);
238f8091a88SPaul Burton 
239f8091a88SPaul Burton 	pci_initialized = 1;
240f8091a88SPaul Burton 
241f8091a88SPaul Burton 	return 0;
242f8091a88SPaul Burton }
243f8091a88SPaul Burton 
244f8091a88SPaul Burton subsys_initcall(pcibios_init);
245f8091a88SPaul Burton 
246f8091a88SPaul Burton static int pcibios_enable_resources(struct pci_dev *dev, int mask)
247f8091a88SPaul Burton {
248f8091a88SPaul Burton 	u16 cmd, old_cmd;
249f8091a88SPaul Burton 	int idx;
250f8091a88SPaul Burton 	struct resource *r;
251f8091a88SPaul Burton 
252f8091a88SPaul Burton 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
253f8091a88SPaul Burton 	old_cmd = cmd;
254f8091a88SPaul Burton 	for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
255f8091a88SPaul Burton 		/* Only set up the requested stuff */
256f8091a88SPaul Burton 		if (!(mask & (1<<idx)))
257f8091a88SPaul Burton 			continue;
258f8091a88SPaul Burton 
259f8091a88SPaul Burton 		r = &dev->resource[idx];
260f8091a88SPaul Burton 		if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
261f8091a88SPaul Burton 			continue;
262f8091a88SPaul Burton 		if ((idx == PCI_ROM_RESOURCE) &&
263f8091a88SPaul Burton 				(!(r->flags & IORESOURCE_ROM_ENABLE)))
264f8091a88SPaul Burton 			continue;
265f8091a88SPaul Burton 		if (!r->start && r->end) {
266f8091a88SPaul Burton 			printk(KERN_ERR "PCI: Device %s not available "
267f8091a88SPaul Burton 			       "because of resource collisions\n",
268f8091a88SPaul Burton 			       pci_name(dev));
269f8091a88SPaul Burton 			return -EINVAL;
270f8091a88SPaul Burton 		}
271f8091a88SPaul Burton 		if (r->flags & IORESOURCE_IO)
272f8091a88SPaul Burton 			cmd |= PCI_COMMAND_IO;
273f8091a88SPaul Burton 		if (r->flags & IORESOURCE_MEM)
274f8091a88SPaul Burton 			cmd |= PCI_COMMAND_MEMORY;
275f8091a88SPaul Burton 	}
276f8091a88SPaul Burton 	if (cmd != old_cmd) {
277f8091a88SPaul Burton 		printk("PCI: Enabling device %s (%04x -> %04x)\n",
278f8091a88SPaul Burton 		       pci_name(dev), old_cmd, cmd);
279f8091a88SPaul Burton 		pci_write_config_word(dev, PCI_COMMAND, cmd);
280f8091a88SPaul Burton 	}
281f8091a88SPaul Burton 	return 0;
282f8091a88SPaul Burton }
283f8091a88SPaul Burton 
284f8091a88SPaul Burton int pcibios_enable_device(struct pci_dev *dev, int mask)
285f8091a88SPaul Burton {
286f8091a88SPaul Burton 	int err;
287f8091a88SPaul Burton 
288f8091a88SPaul Burton 	if ((err = pcibios_enable_resources(dev, mask)) < 0)
289f8091a88SPaul Burton 		return err;
290f8091a88SPaul Burton 
291f8091a88SPaul Burton 	return pcibios_plat_dev_init(dev);
292f8091a88SPaul Burton }
293f8091a88SPaul Burton 
294f8091a88SPaul Burton void pcibios_fixup_bus(struct pci_bus *bus)
295f8091a88SPaul Burton {
296f8091a88SPaul Burton 	struct pci_dev *dev = bus->self;
297f8091a88SPaul Burton 
298f8091a88SPaul Burton 	if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
299f8091a88SPaul Burton 	    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
300f8091a88SPaul Burton 		pci_read_bridge_bases(bus);
301f8091a88SPaul Burton 	}
302f8091a88SPaul Burton }
303f8091a88SPaul Burton 
304f8091a88SPaul Burton char * (*pcibios_plat_setup)(char *str) __initdata;
305f8091a88SPaul Burton 
306f8091a88SPaul Burton char *__init pcibios_setup(char *str)
307f8091a88SPaul Burton {
308f8091a88SPaul Burton 	if (pcibios_plat_setup)
309f8091a88SPaul Burton 		return pcibios_plat_setup(str);
310f8091a88SPaul Burton 	return str;
311f8091a88SPaul Burton }
312