xref: /openbmc/linux/arch/sh/drivers/pci/pci.c (revision b6c58b1d987a5795086c5c2babd8c7367d2fdb8c)
1 /*
2  * New-style PCI core.
3  *
4  * Copyright (c) 2004 - 2009  Paul Mundt
5  * Copyright (c) 2002  M. R. Brown
6  *
7  * Modelled after arch/mips/pci/pci.c:
8  *  Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file "COPYING" in the main directory of this archive
12  * for more details.
13  */
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/pci.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/dma-debug.h>
20 #include <linux/io.h>
21 #include <linux/mutex.h>
22 
23 unsigned long PCIBIOS_MIN_IO = 0x0000;
24 unsigned long PCIBIOS_MIN_MEM = 0;
25 
26 /*
27  * The PCI controller list.
28  */
29 static struct pci_channel *hose_head, **hose_tail = &hose_head;
30 
31 static int pci_initialized;
32 
33 static void __devinit pcibios_scanbus(struct pci_channel *hose)
34 {
35 	static int next_busno;
36 	static int need_domain_info;
37 	struct pci_bus *bus;
38 
39 	bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
40 	hose->bus = bus;
41 
42 	need_domain_info = need_domain_info || hose->index;
43 	hose->need_domain_info = need_domain_info;
44 	if (bus) {
45 		next_busno = bus->subordinate + 1;
46 		/* Don't allow 8-bit bus number overflow inside the hose -
47 		   reserve some space for bridges. */
48 		if (next_busno > 224) {
49 			next_busno = 0;
50 			need_domain_info = 1;
51 		}
52 
53 		pci_bus_size_bridges(bus);
54 		pci_bus_assign_resources(bus);
55 		pci_enable_bridges(bus);
56 	}
57 }
58 
59 static DEFINE_MUTEX(pci_scan_mutex);
60 
61 int __devinit register_pci_controller(struct pci_channel *hose)
62 {
63 	int i;
64 
65 	for (i = 0; i < hose->nr_resources; i++) {
66 		struct resource *res = hose->resources + i;
67 
68 		if (res->flags & IORESOURCE_IO) {
69 			if (request_resource(&ioport_resource, res) < 0)
70 				goto out;
71 		} else {
72 			if (request_resource(&iomem_resource, res) < 0)
73 				goto out;
74 		}
75 	}
76 
77 	*hose_tail = hose;
78 	hose_tail = &hose->next;
79 
80 	/*
81 	 * Do not panic here but later - this might hapen before console init.
82 	 */
83 	if (!hose->io_map_base) {
84 		printk(KERN_WARNING
85 		       "registering PCI controller with io_map_base unset\n");
86 	}
87 
88 	/*
89 	 * Setup the ERR/PERR and SERR timers, if available.
90 	 */
91 	pcibios_enable_timers(hose);
92 
93 	/*
94 	 * Scan the bus if it is register after the PCI subsystem
95 	 * initialization.
96 	 */
97 	if (pci_initialized) {
98 		mutex_lock(&pci_scan_mutex);
99 		pcibios_scanbus(hose);
100 		mutex_unlock(&pci_scan_mutex);
101 	}
102 
103 	return 0;
104 
105 out:
106 	for (--i; i >= 0; i--)
107 		release_resource(&hose->resources[i]);
108 
109 	printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
110 	return -1;
111 }
112 
113 static int __init pcibios_init(void)
114 {
115 	struct pci_channel *hose;
116 
117 	/* Scan all of the recorded PCI controllers.  */
118 	for (hose = hose_head; hose; hose = hose->next)
119 		pcibios_scanbus(hose);
120 
121 	pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
122 
123 	dma_debug_add_bus(&pci_bus_type);
124 
125 	pci_initialized = 1;
126 
127 	return 0;
128 }
129 subsys_initcall(pcibios_init);
130 
131 static void pcibios_fixup_device_resources(struct pci_dev *dev,
132 	struct pci_bus *bus)
133 {
134 	/* Update device resources.  */
135 	struct pci_channel *hose = bus->sysdata;
136 	unsigned long offset = 0;
137 	int i;
138 
139 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
140 		if (!dev->resource[i].start)
141 			continue;
142 		if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
143 			continue;
144 		if (dev->resource[i].flags & IORESOURCE_IO)
145 			offset = hose->io_offset;
146 		else if (dev->resource[i].flags & IORESOURCE_MEM)
147 			offset = hose->mem_offset;
148 
149 		dev->resource[i].start += offset;
150 		dev->resource[i].end += offset;
151 	}
152 }
153 
154 /*
155  *  Called after each bus is probed, but before its children
156  *  are examined.
157  */
158 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
159 {
160 	struct pci_dev *dev = bus->self;
161 	struct list_head *ln;
162 	struct pci_channel *hose = bus->sysdata;
163 
164 	if (!dev) {
165 		int i;
166 
167 		for (i = 0; i < hose->nr_resources; i++)
168 			bus->resource[i] = hose->resources + i;
169 	}
170 
171 	for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
172 		dev = pci_dev_b(ln);
173 
174 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
175 			pcibios_fixup_device_resources(dev, bus);
176 	}
177 }
178 
179 /*
180  * We need to avoid collisions with `mirrored' VGA ports
181  * and other strange ISA hardware, so we always want the
182  * addresses to be allocated in the 0x000-0x0ff region
183  * modulo 0x400.
184  */
185 void pcibios_align_resource(void *data, struct resource *res,
186 			    resource_size_t size, resource_size_t align)
187 {
188 	struct pci_dev *dev = data;
189 	struct pci_channel *hose = dev->sysdata;
190 	resource_size_t start = res->start;
191 
192 	if (res->flags & IORESOURCE_IO) {
193 		if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
194 			start = PCIBIOS_MIN_IO + hose->resources[0].start;
195 
196 		/*
197                  * Put everything into 0x00-0xff region modulo 0x400.
198 		 */
199 		if (start & 0x300)
200 			start = (start + 0x3ff) & ~0x3ff;
201 	}
202 
203 	res->start = start;
204 }
205 
206 void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
207 			 struct resource *res)
208 {
209 	struct pci_channel *hose = dev->sysdata;
210 	unsigned long offset = 0;
211 
212 	if (res->flags & IORESOURCE_IO)
213 		offset = hose->io_offset;
214 	else if (res->flags & IORESOURCE_MEM)
215 		offset = hose->mem_offset;
216 
217 	region->start = res->start - offset;
218 	region->end = res->end - offset;
219 }
220 
221 void __devinit
222 pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
223 			struct pci_bus_region *region)
224 {
225 	struct pci_channel *hose = dev->sysdata;
226 	unsigned long offset = 0;
227 
228 	if (res->flags & IORESOURCE_IO)
229 		offset = hose->io_offset;
230 	else if (res->flags & IORESOURCE_MEM)
231 		offset = hose->mem_offset;
232 
233 	res->start = region->start + offset;
234 	res->end = region->end + offset;
235 }
236 
237 int pcibios_enable_device(struct pci_dev *dev, int mask)
238 {
239 	u16 cmd, old_cmd;
240 	int idx;
241 	struct resource *r;
242 
243 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
244 	old_cmd = cmd;
245 	for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
246 		/* Only set up the requested stuff */
247 		if (!(mask & (1<<idx)))
248 			continue;
249 
250 		r = &dev->resource[idx];
251 		if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
252 			continue;
253 		if ((idx == PCI_ROM_RESOURCE) &&
254 				(!(r->flags & IORESOURCE_ROM_ENABLE)))
255 			continue;
256 		if (!r->start && r->end) {
257 			printk(KERN_ERR "PCI: Device %s not available "
258 			       "because of resource collisions\n",
259 			       pci_name(dev));
260 			return -EINVAL;
261 		}
262 		if (r->flags & IORESOURCE_IO)
263 			cmd |= PCI_COMMAND_IO;
264 		if (r->flags & IORESOURCE_MEM)
265 			cmd |= PCI_COMMAND_MEMORY;
266 	}
267 	if (cmd != old_cmd) {
268 		printk("PCI: Enabling device %s (%04x -> %04x)\n",
269 		       pci_name(dev), old_cmd, cmd);
270 		pci_write_config_word(dev, PCI_COMMAND, cmd);
271 	}
272 	return 0;
273 }
274 
275 /*
276  *  If we set up a device for bus mastering, we need to check and set
277  *  the latency timer as it may not be properly set.
278  */
279 static unsigned int pcibios_max_latency = 255;
280 
281 void pcibios_set_master(struct pci_dev *dev)
282 {
283 	u8 lat;
284 	pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
285 	if (lat < 16)
286 		lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
287 	else if (lat > pcibios_max_latency)
288 		lat = pcibios_max_latency;
289 	else
290 		return;
291 	printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
292 	       pci_name(dev), lat);
293 	pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
294 }
295 
296 void __init pcibios_update_irq(struct pci_dev *dev, int irq)
297 {
298 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
299 }
300 
301 char * __devinit pcibios_setup(char *str)
302 {
303 	return str;
304 }
305 
306 /*
307  * We can't use pci_find_device() here since we are
308  * called from interrupt context.
309  */
310 static void pcibios_bus_report_status(struct pci_bus *bus,
311 		unsigned int status_mask, int warn)
312 {
313 	struct pci_dev *dev;
314 
315 	list_for_each_entry(dev, &bus->devices, bus_list) {
316 		u16 status;
317 
318 		/*
319 		 * ignore host bridge - we handle
320 		 * that separately
321 		 */
322 		if (dev->bus->number == 0 && dev->devfn == 0)
323 			continue;
324 
325 		pci_read_config_word(dev, PCI_STATUS, &status);
326 		if (status == 0xffff)
327 			continue;
328 
329 		if ((status & status_mask) == 0)
330 			continue;
331 
332 		/* clear the status errors */
333 		pci_write_config_word(dev, PCI_STATUS, status & status_mask);
334 
335 		if (warn)
336 			printk("(%s: %04X) ", pci_name(dev), status);
337 	}
338 
339 	list_for_each_entry(dev, &bus->devices, bus_list)
340 		if (dev->subordinate)
341 			pcibios_bus_report_status(dev->subordinate, status_mask, warn);
342 }
343 
344 void pcibios_report_status(unsigned int status_mask, int warn)
345 {
346 	struct pci_channel *hose;
347 
348 	for (hose = hose_head; hose; hose = hose->next)
349 		pcibios_bus_report_status(hose->bus, status_mask, warn);
350 }
351 
352 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
353 			enum pci_mmap_state mmap_state, int write_combine)
354 {
355 	/*
356 	 * I/O space can be accessed via normal processor loads and stores on
357 	 * this platform but for now we elect not to do this and portable
358 	 * drivers should not do this anyway.
359 	 */
360 	if (mmap_state == pci_mmap_io)
361 		return -EINVAL;
362 
363 	/*
364 	 * Ignore write-combine; for now only return uncached mappings.
365 	 */
366 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
367 
368 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
369 			       vma->vm_end - vma->vm_start,
370 			       vma->vm_page_prot);
371 }
372 
373 #ifndef CONFIG_GENERIC_IOMAP
374 
375 static void __iomem *ioport_map_pci(struct pci_dev *dev,
376 				    unsigned long port, unsigned int nr)
377 {
378 	struct pci_channel *chan = dev->sysdata;
379 
380 	if (unlikely(!chan->io_map_base)) {
381 		chan->io_map_base = generic_io_base;
382 
383 		if (pci_domains_supported)
384 			panic("To avoid data corruption io_map_base MUST be "
385 			      "set with multiple PCI domains.");
386 	}
387 
388 
389 	return (void __iomem *)(chan->io_map_base + port);
390 }
391 
392 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
393 {
394 	resource_size_t start = pci_resource_start(dev, bar);
395 	resource_size_t len = pci_resource_len(dev, bar);
396 	unsigned long flags = pci_resource_flags(dev, bar);
397 
398 	if (unlikely(!len || !start))
399 		return NULL;
400 	if (maxlen && len > maxlen)
401 		len = maxlen;
402 
403 	if (flags & IORESOURCE_IO)
404 		return ioport_map_pci(dev, start, len);
405 	if (flags & IORESOURCE_MEM) {
406 		if (flags & IORESOURCE_CACHEABLE)
407 			return ioremap(start, len);
408 		return ioremap_nocache(start, len);
409 	}
410 
411 	return NULL;
412 }
413 EXPORT_SYMBOL(pci_iomap);
414 
415 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
416 {
417 	iounmap(addr);
418 }
419 EXPORT_SYMBOL(pci_iounmap);
420 
421 #endif /* CONFIG_GENERIC_IOMAP */
422 
423 #ifdef CONFIG_HOTPLUG
424 EXPORT_SYMBOL(pcibios_resource_to_bus);
425 EXPORT_SYMBOL(pcibios_bus_to_resource);
426 EXPORT_SYMBOL(PCIBIOS_MIN_IO);
427 EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
428 #endif
429