xref: /openbmc/linux/arch/sh/drivers/pci/pci.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1ff4a7481SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2cf242007SPaul Mundt /*
3cf242007SPaul Mundt  * New-style PCI core.
4cf242007SPaul Mundt  *
5cf242007SPaul Mundt  * Copyright (c) 2004 - 2009  Paul Mundt
6cf242007SPaul Mundt  * Copyright (c) 2002  M. R. Brown
7cf242007SPaul Mundt  *
8cf242007SPaul Mundt  * Modelled after arch/mips/pci/pci.c:
9cf242007SPaul Mundt  *  Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
10cf242007SPaul Mundt  */
11cf242007SPaul Mundt #include <linux/kernel.h>
12cf242007SPaul Mundt #include <linux/mm.h>
13cf242007SPaul Mundt #include <linux/pci.h>
14cf242007SPaul Mundt #include <linux/init.h>
15cf242007SPaul Mundt #include <linux/types.h>
16cf242007SPaul Mundt #include <linux/io.h>
17cf242007SPaul Mundt #include <linux/mutex.h>
1839a90865SPaul Mundt #include <linux/spinlock.h>
19f7be3455SPaul Gortmaker #include <linux/export.h>
20cf242007SPaul Mundt 
21cf242007SPaul Mundt unsigned long PCIBIOS_MIN_IO = 0x0000;
22cf242007SPaul Mundt unsigned long PCIBIOS_MIN_MEM = 0;
23cf242007SPaul Mundt 
24cf242007SPaul Mundt /*
25cf242007SPaul Mundt  * The PCI controller list.
26cf242007SPaul Mundt  */
27cf242007SPaul Mundt static struct pci_channel *hose_head, **hose_tail = &hose_head;
28cf242007SPaul Mundt 
29cf242007SPaul Mundt static int pci_initialized;
30cf242007SPaul Mundt 
pcibios_scanbus(struct pci_channel * hose)31b881bc46SGreg Kroah-Hartman static void pcibios_scanbus(struct pci_channel *hose)
32cf242007SPaul Mundt {
33cf242007SPaul Mundt 	static int next_busno;
34320e68daSPaul Mundt 	static int need_domain_info;
356f17dd1bSBjorn Helgaas 	LIST_HEAD(resources);
367fa6a50eSBjorn Helgaas 	struct resource *res;
377fa6a50eSBjorn Helgaas 	resource_size_t offset;
3820d69322SLorenzo Pieralisi 	int i, ret;
3920d69322SLorenzo Pieralisi 	struct pci_host_bridge *bridge;
4020d69322SLorenzo Pieralisi 
4120d69322SLorenzo Pieralisi 	bridge = pci_alloc_host_bridge(0);
4220d69322SLorenzo Pieralisi 	if (!bridge)
4320d69322SLorenzo Pieralisi 		return;
44cf242007SPaul Mundt 
457fa6a50eSBjorn Helgaas 	for (i = 0; i < hose->nr_resources; i++) {
467fa6a50eSBjorn Helgaas 		res = hose->resources + i;
477fa6a50eSBjorn Helgaas 		offset = 0;
483aeb93a0SThomas Petazzoni 		if (res->flags & IORESOURCE_DISABLED)
493aeb93a0SThomas Petazzoni 			continue;
507fa6a50eSBjorn Helgaas 		if (res->flags & IORESOURCE_IO)
517fa6a50eSBjorn Helgaas 			offset = hose->io_offset;
527fa6a50eSBjorn Helgaas 		else if (res->flags & IORESOURCE_MEM)
537fa6a50eSBjorn Helgaas 			offset = hose->mem_offset;
547fa6a50eSBjorn Helgaas 		pci_add_resource_offset(&resources, res, offset);
557fa6a50eSBjorn Helgaas 	}
566f17dd1bSBjorn Helgaas 
5720d69322SLorenzo Pieralisi 	list_splice_init(&resources, &bridge->windows);
5820d69322SLorenzo Pieralisi 	bridge->dev.parent = NULL;
5920d69322SLorenzo Pieralisi 	bridge->sysdata = hose;
6020d69322SLorenzo Pieralisi 	bridge->busnr = next_busno;
6120d69322SLorenzo Pieralisi 	bridge->ops = hose->pci_ops;
6220d69322SLorenzo Pieralisi 	bridge->swizzle_irq = pci_common_swizzle;
6320d69322SLorenzo Pieralisi 	bridge->map_irq = pcibios_map_platform_irq;
6420d69322SLorenzo Pieralisi 
6520d69322SLorenzo Pieralisi 	ret = pci_scan_root_bus_bridge(bridge);
6620d69322SLorenzo Pieralisi 	if (ret) {
6720d69322SLorenzo Pieralisi 		pci_free_host_bridge(bridge);
6820d69322SLorenzo Pieralisi 		return;
6920d69322SLorenzo Pieralisi 	}
7020d69322SLorenzo Pieralisi 
7120d69322SLorenzo Pieralisi 	hose->bus = bridge->bus;
72320e68daSPaul Mundt 
73320e68daSPaul Mundt 	need_domain_info = need_domain_info || hose->index;
74320e68daSPaul Mundt 	hose->need_domain_info = need_domain_info;
759e808eb6SBjorn Helgaas 
7620d69322SLorenzo Pieralisi 	next_busno = hose->bus->busn_res.end + 1;
77cf242007SPaul Mundt 	/* Don't allow 8-bit bus number overflow inside the hose -
78cf242007SPaul Mundt 	   reserve some space for bridges. */
79320e68daSPaul Mundt 	if (next_busno > 224) {
80cf242007SPaul Mundt 		next_busno = 0;
81320e68daSPaul Mundt 		need_domain_info = 1;
82320e68daSPaul Mundt 	}
83cf242007SPaul Mundt 
8420d69322SLorenzo Pieralisi 	pci_bus_size_bridges(hose->bus);
8520d69322SLorenzo Pieralisi 	pci_bus_assign_resources(hose->bus);
8620d69322SLorenzo Pieralisi 	pci_bus_add_devices(hose->bus);
87cf242007SPaul Mundt }
88cf242007SPaul Mundt 
8939a90865SPaul Mundt /*
9039a90865SPaul Mundt  * This interrupt-safe spinlock protects all accesses to PCI
9139a90865SPaul Mundt  * configuration space.
9239a90865SPaul Mundt  */
9339a90865SPaul Mundt DEFINE_RAW_SPINLOCK(pci_config_lock);
94cf242007SPaul Mundt static DEFINE_MUTEX(pci_scan_mutex);
95cf242007SPaul Mundt 
register_pci_controller(struct pci_channel * hose)96b881bc46SGreg Kroah-Hartman int register_pci_controller(struct pci_channel *hose)
97cf242007SPaul Mundt {
98b6c58b1dSPaul Mundt 	int i;
99b6c58b1dSPaul Mundt 
100b6c58b1dSPaul Mundt 	for (i = 0; i < hose->nr_resources; i++) {
101b6c58b1dSPaul Mundt 		struct resource *res = hose->resources + i;
102b6c58b1dSPaul Mundt 
1033aeb93a0SThomas Petazzoni 		if (res->flags & IORESOURCE_DISABLED)
1043aeb93a0SThomas Petazzoni 			continue;
1053aeb93a0SThomas Petazzoni 
106b6c58b1dSPaul Mundt 		if (res->flags & IORESOURCE_IO) {
107b6c58b1dSPaul Mundt 			if (request_resource(&ioport_resource, res) < 0)
108ac8ab54aSPaul Mundt 				goto out;
109b6c58b1dSPaul Mundt 		} else {
110b6c58b1dSPaul Mundt 			if (request_resource(&iomem_resource, res) < 0)
111ac8ab54aSPaul Mundt 				goto out;
112ac8ab54aSPaul Mundt 		}
113b6c58b1dSPaul Mundt 	}
114cf242007SPaul Mundt 
115cf242007SPaul Mundt 	*hose_tail = hose;
116cf242007SPaul Mundt 	hose_tail = &hose->next;
117cf242007SPaul Mundt 
118cf242007SPaul Mundt 	/*
11925985edcSLucas De Marchi 	 * Do not panic here but later - this might happen before console init.
120cf242007SPaul Mundt 	 */
121cf242007SPaul Mundt 	if (!hose->io_map_base) {
122*601bf18bSGeert Uytterhoeven 		pr_warn("registering PCI controller with io_map_base unset\n");
123cf242007SPaul Mundt 	}
124cf242007SPaul Mundt 
125cf242007SPaul Mundt 	/*
126ef407beeSPaul Mundt 	 * Setup the ERR/PERR and SERR timers, if available.
127ef407beeSPaul Mundt 	 */
128ef407beeSPaul Mundt 	pcibios_enable_timers(hose);
129ef407beeSPaul Mundt 
130ef407beeSPaul Mundt 	/*
131cf242007SPaul Mundt 	 * Scan the bus if it is register after the PCI subsystem
132cf242007SPaul Mundt 	 * initialization.
133cf242007SPaul Mundt 	 */
134cf242007SPaul Mundt 	if (pci_initialized) {
135cf242007SPaul Mundt 		mutex_lock(&pci_scan_mutex);
136cf242007SPaul Mundt 		pcibios_scanbus(hose);
137cf242007SPaul Mundt 		mutex_unlock(&pci_scan_mutex);
138cf242007SPaul Mundt 	}
139ac8ab54aSPaul Mundt 
140bcf39352SPaul Mundt 	return 0;
14185b59f5bSPaul Mundt 
142ac8ab54aSPaul Mundt out:
143b6c58b1dSPaul Mundt 	for (--i; i >= 0; i--)
144b6c58b1dSPaul Mundt 		release_resource(&hose->resources[i]);
145b6c58b1dSPaul Mundt 
146*601bf18bSGeert Uytterhoeven 	pr_warn("Skipping PCI bus scan due to resource conflict\n");
147bcf39352SPaul Mundt 	return -1;
148cf242007SPaul Mundt }
149cf242007SPaul Mundt 
pcibios_init(void)150cf242007SPaul Mundt static int __init pcibios_init(void)
151cf242007SPaul Mundt {
152cf242007SPaul Mundt 	struct pci_channel *hose;
153cf242007SPaul Mundt 
154cf242007SPaul Mundt 	/* Scan all of the recorded PCI controllers.  */
155cf242007SPaul Mundt 	for (hose = hose_head; hose; hose = hose->next)
156cf242007SPaul Mundt 		pcibios_scanbus(hose);
157cf242007SPaul Mundt 
158cf242007SPaul Mundt 	pci_initialized = 1;
159cf242007SPaul Mundt 
160cf242007SPaul Mundt 	return 0;
161cf242007SPaul Mundt }
162cf242007SPaul Mundt subsys_initcall(pcibios_init);
163cf242007SPaul Mundt 
164cf242007SPaul Mundt /*
165cf242007SPaul Mundt  * We need to avoid collisions with `mirrored' VGA ports
166cf242007SPaul Mundt  * and other strange ISA hardware, so we always want the
167cf242007SPaul Mundt  * addresses to be allocated in the 0x000-0x0ff region
168cf242007SPaul Mundt  * modulo 0x400.
169cf242007SPaul Mundt  */
pcibios_align_resource(void * data,const struct resource * res,resource_size_t size,resource_size_t align)1703b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
171cf242007SPaul Mundt 				resource_size_t size, resource_size_t align)
172cf242007SPaul Mundt {
173cf242007SPaul Mundt 	struct pci_dev *dev = data;
174b6c58b1dSPaul Mundt 	struct pci_channel *hose = dev->sysdata;
175cf242007SPaul Mundt 	resource_size_t start = res->start;
176cf242007SPaul Mundt 
177cf242007SPaul Mundt 	if (res->flags & IORESOURCE_IO) {
178b6c58b1dSPaul Mundt 		if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
179b6c58b1dSPaul Mundt 			start = PCIBIOS_MIN_IO + hose->resources[0].start;
180cf242007SPaul Mundt 
181cf242007SPaul Mundt 		/*
182cf242007SPaul Mundt                  * Put everything into 0x00-0xff region modulo 0x400.
183cf242007SPaul Mundt 		 */
18484959359SPaul Mundt 		if (start & 0x300)
185cf242007SPaul Mundt 			start = (start + 0x3ff) & ~0x3ff;
186cf242007SPaul Mundt 	}
187cf242007SPaul Mundt 
188b26b2d49SDominik Brodowski 	return start;
189cf242007SPaul Mundt }
190cf242007SPaul Mundt 
1919ad62ec4SPaul Mundt static void __init
pcibios_bus_report_status_early(struct pci_channel * hose,int top_bus,int current_bus,unsigned int status_mask,int warn)1929ad62ec4SPaul Mundt pcibios_bus_report_status_early(struct pci_channel *hose,
1939ad62ec4SPaul Mundt 				int top_bus, int current_bus,
1949ad62ec4SPaul Mundt 				unsigned int status_mask, int warn)
1959ad62ec4SPaul Mundt {
1969ad62ec4SPaul Mundt 	unsigned int pci_devfn;
1979ad62ec4SPaul Mundt 	u16 status;
1989ad62ec4SPaul Mundt 	int ret;
1999ad62ec4SPaul Mundt 
2009ad62ec4SPaul Mundt 	for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
2019ad62ec4SPaul Mundt 		if (PCI_FUNC(pci_devfn))
2029ad62ec4SPaul Mundt 			continue;
2039ad62ec4SPaul Mundt 		ret = early_read_config_word(hose, top_bus, current_bus,
2049ad62ec4SPaul Mundt 					     pci_devfn, PCI_STATUS, &status);
2059ad62ec4SPaul Mundt 		if (ret != PCIBIOS_SUCCESSFUL)
2069ad62ec4SPaul Mundt 			continue;
2079ad62ec4SPaul Mundt 		if (status == 0xffff)
2089ad62ec4SPaul Mundt 			continue;
2099ad62ec4SPaul Mundt 
2109ad62ec4SPaul Mundt 		early_write_config_word(hose, top_bus, current_bus,
2119ad62ec4SPaul Mundt 					pci_devfn, PCI_STATUS,
2129ad62ec4SPaul Mundt 					status & status_mask);
2139ad62ec4SPaul Mundt 		if (warn)
214*601bf18bSGeert Uytterhoeven 			pr_cont("(%02x:%02x: %04X) ", current_bus, pci_devfn,
215*601bf18bSGeert Uytterhoeven 				status);
2169ad62ec4SPaul Mundt 	}
2179ad62ec4SPaul Mundt }
2189ad62ec4SPaul Mundt 
219ef407beeSPaul Mundt /*
220ef407beeSPaul Mundt  * We can't use pci_find_device() here since we are
221ef407beeSPaul Mundt  * called from interrupt context.
222ef407beeSPaul Mundt  */
223bd721ea7SFabian Frederick static void __ref
pcibios_bus_report_status(struct pci_bus * bus,unsigned int status_mask,int warn)2249ad62ec4SPaul Mundt pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
2259ad62ec4SPaul Mundt 			  int warn)
226ef407beeSPaul Mundt {
227ef407beeSPaul Mundt 	struct pci_dev *dev;
228ef407beeSPaul Mundt 
229ef407beeSPaul Mundt 	list_for_each_entry(dev, &bus->devices, bus_list) {
230ef407beeSPaul Mundt 		u16 status;
231ef407beeSPaul Mundt 
232ef407beeSPaul Mundt 		/*
233ef407beeSPaul Mundt 		 * ignore host bridge - we handle
234ef407beeSPaul Mundt 		 * that separately
235ef407beeSPaul Mundt 		 */
236ef407beeSPaul Mundt 		if (dev->bus->number == 0 && dev->devfn == 0)
237ef407beeSPaul Mundt 			continue;
238ef407beeSPaul Mundt 
239ef407beeSPaul Mundt 		pci_read_config_word(dev, PCI_STATUS, &status);
240ef407beeSPaul Mundt 		if (status == 0xffff)
241ef407beeSPaul Mundt 			continue;
242ef407beeSPaul Mundt 
243ef407beeSPaul Mundt 		if ((status & status_mask) == 0)
244ef407beeSPaul Mundt 			continue;
245ef407beeSPaul Mundt 
246ef407beeSPaul Mundt 		/* clear the status errors */
247ef407beeSPaul Mundt 		pci_write_config_word(dev, PCI_STATUS, status & status_mask);
248ef407beeSPaul Mundt 
249ef407beeSPaul Mundt 		if (warn)
250*601bf18bSGeert Uytterhoeven 			pr_cont("(%s: %04X) ", pci_name(dev), status);
251ef407beeSPaul Mundt 	}
252ef407beeSPaul Mundt 
253ef407beeSPaul Mundt 	list_for_each_entry(dev, &bus->devices, bus_list)
254ef407beeSPaul Mundt 		if (dev->subordinate)
255ef407beeSPaul Mundt 			pcibios_bus_report_status(dev->subordinate, status_mask, warn);
256ef407beeSPaul Mundt }
257ef407beeSPaul Mundt 
pcibios_report_status(unsigned int status_mask,int warn)258bd721ea7SFabian Frederick void __ref pcibios_report_status(unsigned int status_mask, int warn)
259ef407beeSPaul Mundt {
260ef407beeSPaul Mundt 	struct pci_channel *hose;
261ef407beeSPaul Mundt 
2629ad62ec4SPaul Mundt 	for (hose = hose_head; hose; hose = hose->next) {
2639ad62ec4SPaul Mundt 		if (unlikely(!hose->bus))
2649ad62ec4SPaul Mundt 			pcibios_bus_report_status_early(hose, hose_head->index,
2659ad62ec4SPaul Mundt 					hose->index, status_mask, warn);
2669ad62ec4SPaul Mundt 		else
267ef407beeSPaul Mundt 			pcibios_bus_report_status(hose->bus, status_mask, warn);
268ef407beeSPaul Mundt 	}
2699ad62ec4SPaul Mundt }
270ef407beeSPaul Mundt 
27115444a89SDavid McKay #ifndef CONFIG_GENERIC_IOMAP
27215444a89SDavid McKay 
__pci_ioport_map(struct pci_dev * dev,unsigned long port,unsigned int nr)2731e05b62aSMichael S. Tsirkin void __iomem *__pci_ioport_map(struct pci_dev *dev,
274cf242007SPaul Mundt 			       unsigned long port, unsigned int nr)
275cf242007SPaul Mundt {
276cf242007SPaul Mundt 	struct pci_channel *chan = dev->sysdata;
277cf242007SPaul Mundt 
278320e68daSPaul Mundt 	if (unlikely(!chan->io_map_base)) {
27937b7a978SPaul Mundt 		chan->io_map_base = sh_io_port_base;
280cf242007SPaul Mundt 
281320e68daSPaul Mundt 		if (pci_domains_supported)
282320e68daSPaul Mundt 			panic("To avoid data corruption io_map_base MUST be "
283320e68daSPaul Mundt 			      "set with multiple PCI domains.");
284320e68daSPaul Mundt 	}
285320e68daSPaul Mundt 
286cf242007SPaul Mundt 	return (void __iomem *)(chan->io_map_base + port);
287cf242007SPaul Mundt }
288cf242007SPaul Mundt 
pci_iounmap(struct pci_dev * dev,void __iomem * addr)289cf242007SPaul Mundt void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
290cf242007SPaul Mundt {
291cf242007SPaul Mundt 	iounmap(addr);
292cf242007SPaul Mundt }
293cf242007SPaul Mundt EXPORT_SYMBOL(pci_iounmap);
294cf242007SPaul Mundt 
29515444a89SDavid McKay #endif /* CONFIG_GENERIC_IOMAP */
29615444a89SDavid McKay 
297cf242007SPaul Mundt EXPORT_SYMBOL(PCIBIOS_MIN_IO);
298cf242007SPaul Mundt EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
299