blob: 191075e91cda5a70bdc0b14cd62a220ae7923ee4 [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;
36 struct pci_bus *bus;
37
Paul Mundte79066a2009-04-20 18:29:22 +090038 bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
39 if (bus) {
40 next_busno = bus->subordinate + 1;
41 /* Don't allow 8-bit bus number overflow inside the hose -
42 reserve some space for bridges. */
43 if (next_busno > 224)
44 next_busno = 0;
45
46 pci_bus_size_bridges(bus);
47 pci_bus_assign_resources(bus);
48 pci_enable_bridges(bus);
49 }
50}
51
52static DEFINE_MUTEX(pci_scan_mutex);
53
54void __devinit register_pci_controller(struct pci_channel *hose)
55{
Paul Mundt8c6b44d2009-06-16 06:01:58 +090056 request_resource(&iomem_resource, hose->mem_resource);
57 request_resource(&ioport_resource, hose->io_resource);
Paul Mundte79066a2009-04-20 18:29:22 +090058
59 *hose_tail = hose;
60 hose_tail = &hose->next;
61
62 /*
63 * Do not panic here but later - this might hapen before console init.
64 */
65 if (!hose->io_map_base) {
66 printk(KERN_WARNING
67 "registering PCI controller with io_map_base unset\n");
68 }
69
70 /*
71 * Scan the bus if it is register after the PCI subsystem
72 * initialization.
73 */
74 if (pci_initialized) {
75 mutex_lock(&pci_scan_mutex);
76 pcibios_scanbus(hose);
77 mutex_unlock(&pci_scan_mutex);
78 }
Paul Mundte79066a2009-04-20 18:29:22 +090079}
Paul Mundt4c5107e2009-04-20 15:43:36 +090080
81static int __init pcibios_init(void)
82{
Paul Mundte79066a2009-04-20 18:29:22 +090083 struct pci_channel *hose;
Paul Mundt4c5107e2009-04-20 15:43:36 +090084
Paul Mundte79066a2009-04-20 18:29:22 +090085 /* Scan all of the recorded PCI controllers. */
86 for (hose = hose_head; hose; hose = hose->next)
87 pcibios_scanbus(hose);
Paul Mundt4c5107e2009-04-20 15:43:36 +090088
89 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
90
91 dma_debug_add_bus(&pci_bus_type);
92
Paul Mundte79066a2009-04-20 18:29:22 +090093 pci_initialized = 1;
94
Paul Mundt4c5107e2009-04-20 15:43:36 +090095 return 0;
96}
97subsys_initcall(pcibios_init);
98
99static void pcibios_fixup_device_resources(struct pci_dev *dev,
100 struct pci_bus *bus)
101{
102 /* Update device resources. */
Paul Mundt09cfeb12009-04-20 18:42:00 +0900103 struct pci_channel *hose = bus->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900104 unsigned long offset = 0;
105 int i;
106
107 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
108 if (!dev->resource[i].start)
109 continue;
110 if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
111 continue;
112 if (dev->resource[i].flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900113 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900114 else if (dev->resource[i].flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900115 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900116
117 dev->resource[i].start += offset;
118 dev->resource[i].end += offset;
119 }
120}
121
Paul Mundt4c5107e2009-04-20 15:43:36 +0900122/*
123 * Called after each bus is probed, but before its children
124 * are examined.
125 */
Paul Mundt35bcfff2009-04-20 21:51:19 +0900126void __devinit pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt4c5107e2009-04-20 15:43:36 +0900127{
128 struct pci_dev *dev = bus->self;
129 struct list_head *ln;
130 struct pci_channel *chan = bus->sysdata;
131
132 if (!dev) {
133 bus->resource[0] = chan->io_resource;
134 bus->resource[1] = chan->mem_resource;
135 }
136
137 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
138 dev = pci_dev_b(ln);
139
140 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
141 pcibios_fixup_device_resources(dev, bus);
142 }
143}
Paul Mundt35bcfff2009-04-20 21:51:19 +0900144
145/*
146 * We need to avoid collisions with `mirrored' VGA ports
147 * and other strange ISA hardware, so we always want the
148 * addresses to be allocated in the 0x000-0x0ff region
149 * modulo 0x400.
150 */
151void pcibios_align_resource(void *data, struct resource *res,
152 resource_size_t size, resource_size_t align)
153{
154 struct pci_dev *dev = data;
155 struct pci_channel *chan = dev->sysdata;
156 resource_size_t start = res->start;
157
158 if (res->flags & IORESOURCE_IO) {
159 if (start < PCIBIOS_MIN_IO + chan->io_resource->start)
160 start = PCIBIOS_MIN_IO + chan->io_resource->start;
161
162 /*
163 * Put everything into 0x00-0xff region modulo 0x400.
164 */
Paul Mundt84959352010-01-28 18:15:05 +0900165 if (start & 0x300)
Paul Mundt35bcfff2009-04-20 21:51:19 +0900166 start = (start + 0x3ff) & ~0x3ff;
Paul Mundt35bcfff2009-04-20 21:51:19 +0900167 } else if (res->flags & IORESOURCE_MEM) {
168 if (start < PCIBIOS_MIN_MEM + chan->mem_resource->start)
169 start = PCIBIOS_MIN_MEM + chan->mem_resource->start;
170 }
171
172 res->start = start;
173}
174
175void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
176 struct resource *res)
177{
178 struct pci_channel *hose = dev->sysdata;
179 unsigned long offset = 0;
180
181 if (res->flags & IORESOURCE_IO)
182 offset = hose->io_offset;
183 else if (res->flags & IORESOURCE_MEM)
184 offset = hose->mem_offset;
185
186 region->start = res->start - offset;
187 region->end = res->end - offset;
188}
189
190void __devinit
191pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
192 struct pci_bus_region *region)
193{
194 struct pci_channel *hose = dev->sysdata;
195 unsigned long offset = 0;
196
197 if (res->flags & IORESOURCE_IO)
198 offset = hose->io_offset;
199 else if (res->flags & IORESOURCE_MEM)
200 offset = hose->mem_offset;
201
202 res->start = region->start + offset;
203 res->end = region->end + offset;
204}
205
206int pcibios_enable_device(struct pci_dev *dev, int mask)
207{
208 u16 cmd, old_cmd;
209 int idx;
210 struct resource *r;
211
212 pci_read_config_word(dev, PCI_COMMAND, &cmd);
213 old_cmd = cmd;
214 for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
215 /* Only set up the requested stuff */
216 if (!(mask & (1<<idx)))
217 continue;
218
219 r = &dev->resource[idx];
220 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
221 continue;
222 if ((idx == PCI_ROM_RESOURCE) &&
223 (!(r->flags & IORESOURCE_ROM_ENABLE)))
224 continue;
225 if (!r->start && r->end) {
226 printk(KERN_ERR "PCI: Device %s not available "
227 "because of resource collisions\n",
228 pci_name(dev));
229 return -EINVAL;
230 }
231 if (r->flags & IORESOURCE_IO)
232 cmd |= PCI_COMMAND_IO;
233 if (r->flags & IORESOURCE_MEM)
234 cmd |= PCI_COMMAND_MEMORY;
235 }
236 if (cmd != old_cmd) {
237 printk("PCI: Enabling device %s (%04x -> %04x)\n",
238 pci_name(dev), old_cmd, cmd);
239 pci_write_config_word(dev, PCI_COMMAND, cmd);
240 }
241 return 0;
242}
243
244/*
245 * If we set up a device for bus mastering, we need to check and set
246 * the latency timer as it may not be properly set.
247 */
248static unsigned int pcibios_max_latency = 255;
249
250void pcibios_set_master(struct pci_dev *dev)
251{
252 u8 lat;
253 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
254 if (lat < 16)
255 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
256 else if (lat > pcibios_max_latency)
257 lat = pcibios_max_latency;
258 else
259 return;
260 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
261 pci_name(dev), lat);
262 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
263}
264
265void __init pcibios_update_irq(struct pci_dev *dev, int irq)
266{
267 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
268}
269
270char * __devinit pcibios_setup(char *str)
271{
272 return str;
273}
274
275int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
276 enum pci_mmap_state mmap_state, int write_combine)
277{
278 /*
279 * I/O space can be accessed via normal processor loads and stores on
280 * this platform but for now we elect not to do this and portable
281 * drivers should not do this anyway.
282 */
283 if (mmap_state == pci_mmap_io)
284 return -EINVAL;
285
286 /*
287 * Ignore write-combine; for now only return uncached mappings.
288 */
289 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
290
291 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
292 vma->vm_end - vma->vm_start,
293 vma->vm_page_prot);
294}
295
David McKay15444a82009-08-24 16:10:40 +0900296#ifndef CONFIG_GENERIC_IOMAP
297
Paul Mundt35bcfff2009-04-20 21:51:19 +0900298static void __iomem *ioport_map_pci(struct pci_dev *dev,
299 unsigned long port, unsigned int nr)
300{
301 struct pci_channel *chan = dev->sysdata;
302
303 if (!chan->io_map_base)
304 chan->io_map_base = generic_io_base;
305
306 return (void __iomem *)(chan->io_map_base + port);
307}
308
309void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
310{
311 resource_size_t start = pci_resource_start(dev, bar);
312 resource_size_t len = pci_resource_len(dev, bar);
313 unsigned long flags = pci_resource_flags(dev, bar);
314
315 if (unlikely(!len || !start))
316 return NULL;
317 if (maxlen && len > maxlen)
318 len = maxlen;
319
320 if (flags & IORESOURCE_IO)
321 return ioport_map_pci(dev, start, len);
322
323 /*
324 * Presently the IORESOURCE_MEM case is a bit special, most
325 * SH7751 style PCI controllers have PCI memory at a fixed
326 * location in the address space where no remapping is desired.
327 * With the IORESOURCE_MEM case more care has to be taken
328 * to inhibit page table mapping for legacy cores, but this is
329 * punted off to __ioremap().
330 * -- PFM.
331 */
332 if (flags & IORESOURCE_MEM) {
333 if (flags & IORESOURCE_CACHEABLE)
334 return ioremap(start, len);
335
336 return ioremap_nocache(start, len);
337 }
338
339 return NULL;
340}
341EXPORT_SYMBOL(pci_iomap);
342
343void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
344{
345 iounmap(addr);
346}
347EXPORT_SYMBOL(pci_iounmap);
348
David McKay15444a82009-08-24 16:10:40 +0900349#endif /* CONFIG_GENERIC_IOMAP */
350
Paul Mundt35bcfff2009-04-20 21:51:19 +0900351#ifdef CONFIG_HOTPLUG
352EXPORT_SYMBOL(pcibios_resource_to_bus);
353EXPORT_SYMBOL(pcibios_bus_to_resource);
354EXPORT_SYMBOL(PCIBIOS_MIN_IO);
355EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
356#endif