blob: 45a15cab01df0b0d92225a33c0424b7b90960c8b [file] [log] [blame]
Paul Mundt4c5107e2009-04-20 15:43:36 +09001/*
2 * New-style PCI core.
3 *
Paul Mundt4c5107e2009-04-20 15:43:36 +09004 * Copyright (c) 2004 - 2009 Paul Mundt
Paul Mundt35bcfff2009-04-20 21:51:19 +09005 * 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)
Paul Mundt4c5107e2009-04-20 15:43:36 +09009 *
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>
Paul Mundt35bcfff2009-04-20 21:51:19 +090015#include <linux/mm.h>
Paul Mundt4c5107e2009-04-20 15:43:36 +090016#include <linux/pci.h>
17#include <linux/init.h>
Paul Mundt35bcfff2009-04-20 21:51:19 +090018#include <linux/types.h>
Paul Mundt4c5107e2009-04-20 15:43:36 +090019#include <linux/dma-debug.h>
20#include <linux/io.h>
Paul Mundte79066a2009-04-20 18:29:22 +090021#include <linux/mutex.h>
22
Paul Mundt35bcfff2009-04-20 21:51:19 +090023unsigned long PCIBIOS_MIN_IO = 0x0000;
24unsigned long PCIBIOS_MIN_MEM = 0;
25
Paul Mundte79066a2009-04-20 18:29:22 +090026/*
27 * The PCI controller list.
28 */
29static struct pci_channel *hose_head, **hose_tail = &hose_head;
30
31static int pci_initialized;
32
33static void __devinit pcibios_scanbus(struct pci_channel *hose)
34{
35 static int next_busno;
Paul Mundt320e68d2010-01-29 22:38:13 +090036 static int need_domain_info;
Paul Mundte79066a2009-04-20 18:29:22 +090037 struct pci_bus *bus;
38
Paul Mundte79066a2009-04-20 18:29:22 +090039 bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
Paul Mundt320e68d2010-01-29 22:38:13 +090040 hose->bus = bus;
41
42 need_domain_info = need_domain_info || hose->index;
43 hose->need_domain_info = need_domain_info;
Paul Mundte79066a2009-04-20 18:29:22 +090044 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. */
Paul Mundt320e68d2010-01-29 22:38:13 +090048 if (next_busno > 224) {
Paul Mundte79066a2009-04-20 18:29:22 +090049 next_busno = 0;
Paul Mundt320e68d2010-01-29 22:38:13 +090050 need_domain_info = 1;
51 }
Paul Mundte79066a2009-04-20 18:29:22 +090052
53 pci_bus_size_bridges(bus);
54 pci_bus_assign_resources(bus);
55 pci_enable_bridges(bus);
56 }
57}
58
59static DEFINE_MUTEX(pci_scan_mutex);
60
61void __devinit register_pci_controller(struct pci_channel *hose)
62{
Paul Mundtac8ab542010-01-29 22:22:27 +090063 if (request_resource(&iomem_resource, hose->mem_resource) < 0)
64 goto out;
65 if (request_resource(&ioport_resource, hose->io_resource) < 0) {
66 release_resource(hose->mem_resource);
67 goto out;
68 }
Paul Mundte79066a2009-04-20 18:29:22 +090069
70 *hose_tail = hose;
71 hose_tail = &hose->next;
72
73 /*
74 * Do not panic here but later - this might hapen before console init.
75 */
76 if (!hose->io_map_base) {
77 printk(KERN_WARNING
78 "registering PCI controller with io_map_base unset\n");
79 }
80
81 /*
82 * Scan the bus if it is register after the PCI subsystem
83 * initialization.
84 */
85 if (pci_initialized) {
86 mutex_lock(&pci_scan_mutex);
87 pcibios_scanbus(hose);
88 mutex_unlock(&pci_scan_mutex);
89 }
Paul Mundtac8ab542010-01-29 22:22:27 +090090
91out:
92 printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
Paul Mundte79066a2009-04-20 18:29:22 +090093}
Paul Mundt4c5107e2009-04-20 15:43:36 +090094
95static int __init pcibios_init(void)
96{
Paul Mundte79066a2009-04-20 18:29:22 +090097 struct pci_channel *hose;
Paul Mundt4c5107e2009-04-20 15:43:36 +090098
Paul Mundte79066a2009-04-20 18:29:22 +090099 /* Scan all of the recorded PCI controllers. */
100 for (hose = hose_head; hose; hose = hose->next)
101 pcibios_scanbus(hose);
Paul Mundt4c5107e2009-04-20 15:43:36 +0900102
103 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
104
105 dma_debug_add_bus(&pci_bus_type);
106
Paul Mundte79066a2009-04-20 18:29:22 +0900107 pci_initialized = 1;
108
Paul Mundt4c5107e2009-04-20 15:43:36 +0900109 return 0;
110}
111subsys_initcall(pcibios_init);
112
113static void pcibios_fixup_device_resources(struct pci_dev *dev,
114 struct pci_bus *bus)
115{
116 /* Update device resources. */
Paul Mundt09cfeb12009-04-20 18:42:00 +0900117 struct pci_channel *hose = bus->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900118 unsigned long offset = 0;
119 int i;
120
121 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
122 if (!dev->resource[i].start)
123 continue;
124 if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
125 continue;
126 if (dev->resource[i].flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900127 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900128 else if (dev->resource[i].flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900129 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900130
131 dev->resource[i].start += offset;
132 dev->resource[i].end += offset;
133 }
134}
135
Paul Mundt4c5107e2009-04-20 15:43:36 +0900136/*
137 * Called after each bus is probed, but before its children
138 * are examined.
139 */
Paul Mundt35bcfff2009-04-20 21:51:19 +0900140void __devinit pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt4c5107e2009-04-20 15:43:36 +0900141{
142 struct pci_dev *dev = bus->self;
143 struct list_head *ln;
144 struct pci_channel *chan = bus->sysdata;
145
146 if (!dev) {
147 bus->resource[0] = chan->io_resource;
148 bus->resource[1] = chan->mem_resource;
149 }
150
151 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
152 dev = pci_dev_b(ln);
153
154 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
155 pcibios_fixup_device_resources(dev, bus);
156 }
157}
Paul Mundt35bcfff2009-04-20 21:51:19 +0900158
159/*
160 * We need to avoid collisions with `mirrored' VGA ports
161 * and other strange ISA hardware, so we always want the
162 * addresses to be allocated in the 0x000-0x0ff region
163 * modulo 0x400.
164 */
165void pcibios_align_resource(void *data, struct resource *res,
166 resource_size_t size, resource_size_t align)
167{
168 struct pci_dev *dev = data;
169 struct pci_channel *chan = dev->sysdata;
170 resource_size_t start = res->start;
171
172 if (res->flags & IORESOURCE_IO) {
173 if (start < PCIBIOS_MIN_IO + chan->io_resource->start)
174 start = PCIBIOS_MIN_IO + chan->io_resource->start;
175
176 /*
177 * Put everything into 0x00-0xff region modulo 0x400.
178 */
Paul Mundt84959352010-01-28 18:15:05 +0900179 if (start & 0x300)
Paul Mundt35bcfff2009-04-20 21:51:19 +0900180 start = (start + 0x3ff) & ~0x3ff;
Paul Mundt35bcfff2009-04-20 21:51:19 +0900181 } else if (res->flags & IORESOURCE_MEM) {
182 if (start < PCIBIOS_MIN_MEM + chan->mem_resource->start)
183 start = PCIBIOS_MIN_MEM + chan->mem_resource->start;
184 }
185
186 res->start = start;
187}
188
189void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
190 struct resource *res)
191{
192 struct pci_channel *hose = dev->sysdata;
193 unsigned long offset = 0;
194
195 if (res->flags & IORESOURCE_IO)
196 offset = hose->io_offset;
197 else if (res->flags & IORESOURCE_MEM)
198 offset = hose->mem_offset;
199
200 region->start = res->start - offset;
201 region->end = res->end - offset;
202}
203
204void __devinit
205pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
206 struct pci_bus_region *region)
207{
208 struct pci_channel *hose = dev->sysdata;
209 unsigned long offset = 0;
210
211 if (res->flags & IORESOURCE_IO)
212 offset = hose->io_offset;
213 else if (res->flags & IORESOURCE_MEM)
214 offset = hose->mem_offset;
215
216 res->start = region->start + offset;
217 res->end = region->end + offset;
218}
219
220int pcibios_enable_device(struct pci_dev *dev, int mask)
221{
222 u16 cmd, old_cmd;
223 int idx;
224 struct resource *r;
225
226 pci_read_config_word(dev, PCI_COMMAND, &cmd);
227 old_cmd = cmd;
228 for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
229 /* Only set up the requested stuff */
230 if (!(mask & (1<<idx)))
231 continue;
232
233 r = &dev->resource[idx];
234 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
235 continue;
236 if ((idx == PCI_ROM_RESOURCE) &&
237 (!(r->flags & IORESOURCE_ROM_ENABLE)))
238 continue;
239 if (!r->start && r->end) {
240 printk(KERN_ERR "PCI: Device %s not available "
241 "because of resource collisions\n",
242 pci_name(dev));
243 return -EINVAL;
244 }
245 if (r->flags & IORESOURCE_IO)
246 cmd |= PCI_COMMAND_IO;
247 if (r->flags & IORESOURCE_MEM)
248 cmd |= PCI_COMMAND_MEMORY;
249 }
250 if (cmd != old_cmd) {
251 printk("PCI: Enabling device %s (%04x -> %04x)\n",
252 pci_name(dev), old_cmd, cmd);
253 pci_write_config_word(dev, PCI_COMMAND, cmd);
254 }
255 return 0;
256}
257
258/*
259 * If we set up a device for bus mastering, we need to check and set
260 * the latency timer as it may not be properly set.
261 */
262static unsigned int pcibios_max_latency = 255;
263
264void pcibios_set_master(struct pci_dev *dev)
265{
266 u8 lat;
267 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
268 if (lat < 16)
269 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
270 else if (lat > pcibios_max_latency)
271 lat = pcibios_max_latency;
272 else
273 return;
274 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
275 pci_name(dev), lat);
276 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
277}
278
279void __init pcibios_update_irq(struct pci_dev *dev, int irq)
280{
281 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
282}
283
284char * __devinit pcibios_setup(char *str)
285{
286 return str;
287}
288
289int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
290 enum pci_mmap_state mmap_state, int write_combine)
291{
292 /*
293 * I/O space can be accessed via normal processor loads and stores on
294 * this platform but for now we elect not to do this and portable
295 * drivers should not do this anyway.
296 */
297 if (mmap_state == pci_mmap_io)
298 return -EINVAL;
299
300 /*
301 * Ignore write-combine; for now only return uncached mappings.
302 */
303 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
304
305 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
306 vma->vm_end - vma->vm_start,
307 vma->vm_page_prot);
308}
309
David McKay15444a82009-08-24 16:10:40 +0900310#ifndef CONFIG_GENERIC_IOMAP
311
Paul Mundt35bcfff2009-04-20 21:51:19 +0900312static void __iomem *ioport_map_pci(struct pci_dev *dev,
313 unsigned long port, unsigned int nr)
314{
315 struct pci_channel *chan = dev->sysdata;
316
Paul Mundt320e68d2010-01-29 22:38:13 +0900317 if (unlikely(!chan->io_map_base)) {
Paul Mundt35bcfff2009-04-20 21:51:19 +0900318 chan->io_map_base = generic_io_base;
319
Paul Mundt320e68d2010-01-29 22:38:13 +0900320 if (pci_domains_supported)
321 panic("To avoid data corruption io_map_base MUST be "
322 "set with multiple PCI domains.");
323 }
324
325
Paul Mundt35bcfff2009-04-20 21:51:19 +0900326 return (void __iomem *)(chan->io_map_base + port);
327}
328
329void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
330{
331 resource_size_t start = pci_resource_start(dev, bar);
332 resource_size_t len = pci_resource_len(dev, bar);
333 unsigned long flags = pci_resource_flags(dev, bar);
334
335 if (unlikely(!len || !start))
336 return NULL;
337 if (maxlen && len > maxlen)
338 len = maxlen;
339
340 if (flags & IORESOURCE_IO)
341 return ioport_map_pci(dev, start, len);
Paul Mundt35bcfff2009-04-20 21:51:19 +0900342 if (flags & IORESOURCE_MEM) {
343 if (flags & IORESOURCE_CACHEABLE)
344 return ioremap(start, len);
Paul Mundt35bcfff2009-04-20 21:51:19 +0900345 return ioremap_nocache(start, len);
346 }
347
348 return NULL;
349}
350EXPORT_SYMBOL(pci_iomap);
351
352void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
353{
354 iounmap(addr);
355}
356EXPORT_SYMBOL(pci_iounmap);
357
David McKay15444a82009-08-24 16:10:40 +0900358#endif /* CONFIG_GENERIC_IOMAP */
359
Paul Mundt35bcfff2009-04-20 21:51:19 +0900360#ifdef CONFIG_HOTPLUG
361EXPORT_SYMBOL(pcibios_resource_to_bus);
362EXPORT_SYMBOL(pcibios_bus_to_resource);
363EXPORT_SYMBOL(PCIBIOS_MIN_IO);
364EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
365#endif