4 * Copyright (c) 2004 - 2009 Paul Mundt
5 * Copyright (c) 2002 M. R. Brown
7 * Modelled after arch/mips/pci/pci.c:
8 * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
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
14 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/dma-debug.h>
21 #include <linux/mutex.h>
23 unsigned long PCIBIOS_MIN_IO
= 0x0000;
24 unsigned long PCIBIOS_MIN_MEM
= 0;
27 * The PCI controller list.
29 static struct pci_channel
*hose_head
, **hose_tail
= &hose_head
;
31 static int pci_initialized
;
33 static void __devinit
pcibios_scanbus(struct pci_channel
*hose
)
35 static int next_busno
;
38 bus
= pci_scan_bus(next_busno
, hose
->pci_ops
, hose
);
40 next_busno
= bus
->subordinate
+ 1;
41 /* Don't allow 8-bit bus number overflow inside the hose -
42 reserve some space for bridges. */
46 pci_bus_size_bridges(bus
);
47 pci_bus_assign_resources(bus
);
48 pci_enable_bridges(bus
);
52 static DEFINE_MUTEX(pci_scan_mutex
);
54 void __devinit
register_pci_controller(struct pci_channel
*hose
)
56 request_resource(&iomem_resource
, hose
->mem_resource
);
57 request_resource(&ioport_resource
, hose
->io_resource
);
60 hose_tail
= &hose
->next
;
63 * Do not panic here but later - this might hapen before console init.
65 if (!hose
->io_map_base
) {
67 "registering PCI controller with io_map_base unset\n");
71 * Scan the bus if it is register after the PCI subsystem
74 if (pci_initialized
) {
75 mutex_lock(&pci_scan_mutex
);
76 pcibios_scanbus(hose
);
77 mutex_unlock(&pci_scan_mutex
);
81 static int __init
pcibios_init(void)
83 struct pci_channel
*hose
;
85 /* Scan all of the recorded PCI controllers. */
86 for (hose
= hose_head
; hose
; hose
= hose
->next
)
87 pcibios_scanbus(hose
);
89 pci_fixup_irqs(pci_common_swizzle
, pcibios_map_platform_irq
);
91 dma_debug_add_bus(&pci_bus_type
);
97 subsys_initcall(pcibios_init
);
99 static void pcibios_fixup_device_resources(struct pci_dev
*dev
,
102 /* Update device resources. */
103 struct pci_channel
*hose
= bus
->sysdata
;
104 unsigned long offset
= 0;
107 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
108 if (!dev
->resource
[i
].start
)
110 if (dev
->resource
[i
].flags
& IORESOURCE_PCI_FIXED
)
112 if (dev
->resource
[i
].flags
& IORESOURCE_IO
)
113 offset
= hose
->io_offset
;
114 else if (dev
->resource
[i
].flags
& IORESOURCE_MEM
)
115 offset
= hose
->mem_offset
;
117 dev
->resource
[i
].start
+= offset
;
118 dev
->resource
[i
].end
+= offset
;
123 * Called after each bus is probed, but before its children
126 void __devinit
pcibios_fixup_bus(struct pci_bus
*bus
)
128 struct pci_dev
*dev
= bus
->self
;
129 struct list_head
*ln
;
130 struct pci_channel
*chan
= bus
->sysdata
;
133 bus
->resource
[0] = chan
->io_resource
;
134 bus
->resource
[1] = chan
->mem_resource
;
137 for (ln
= bus
->devices
.next
; ln
!= &bus
->devices
; ln
= ln
->next
) {
140 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
141 pcibios_fixup_device_resources(dev
, bus
);
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
151 void pcibios_align_resource(void *data
, struct resource
*res
,
152 resource_size_t size
, resource_size_t align
)
154 struct pci_dev
*dev
= data
;
155 struct pci_channel
*chan
= dev
->sysdata
;
156 resource_size_t start
= res
->start
;
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
;
163 * Put everything into 0x00-0xff region modulo 0x400.
166 start
= (start
+ 0x3ff) & ~0x3ff;
169 } else if (res
->flags
& IORESOURCE_MEM
) {
170 if (start
< PCIBIOS_MIN_MEM
+ chan
->mem_resource
->start
)
171 start
= PCIBIOS_MIN_MEM
+ chan
->mem_resource
->start
;
177 void pcibios_resource_to_bus(struct pci_dev
*dev
, struct pci_bus_region
*region
,
178 struct resource
*res
)
180 struct pci_channel
*hose
= dev
->sysdata
;
181 unsigned long offset
= 0;
183 if (res
->flags
& IORESOURCE_IO
)
184 offset
= hose
->io_offset
;
185 else if (res
->flags
& IORESOURCE_MEM
)
186 offset
= hose
->mem_offset
;
188 region
->start
= res
->start
- offset
;
189 region
->end
= res
->end
- offset
;
193 pcibios_bus_to_resource(struct pci_dev
*dev
, struct resource
*res
,
194 struct pci_bus_region
*region
)
196 struct pci_channel
*hose
= dev
->sysdata
;
197 unsigned long offset
= 0;
199 if (res
->flags
& IORESOURCE_IO
)
200 offset
= hose
->io_offset
;
201 else if (res
->flags
& IORESOURCE_MEM
)
202 offset
= hose
->mem_offset
;
204 res
->start
= region
->start
+ offset
;
205 res
->end
= region
->end
+ offset
;
208 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
214 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
216 for (idx
=0; idx
< PCI_NUM_RESOURCES
; idx
++) {
217 /* Only set up the requested stuff */
218 if (!(mask
& (1<<idx
)))
221 r
= &dev
->resource
[idx
];
222 if (!(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
224 if ((idx
== PCI_ROM_RESOURCE
) &&
225 (!(r
->flags
& IORESOURCE_ROM_ENABLE
)))
227 if (!r
->start
&& r
->end
) {
228 printk(KERN_ERR
"PCI: Device %s not available "
229 "because of resource collisions\n",
233 if (r
->flags
& IORESOURCE_IO
)
234 cmd
|= PCI_COMMAND_IO
;
235 if (r
->flags
& IORESOURCE_MEM
)
236 cmd
|= PCI_COMMAND_MEMORY
;
238 if (cmd
!= old_cmd
) {
239 printk("PCI: Enabling device %s (%04x -> %04x)\n",
240 pci_name(dev
), old_cmd
, cmd
);
241 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
247 * If we set up a device for bus mastering, we need to check and set
248 * the latency timer as it may not be properly set.
250 static unsigned int pcibios_max_latency
= 255;
252 void pcibios_set_master(struct pci_dev
*dev
)
255 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
257 lat
= (64 <= pcibios_max_latency
) ? 64 : pcibios_max_latency
;
258 else if (lat
> pcibios_max_latency
)
259 lat
= pcibios_max_latency
;
262 printk(KERN_INFO
"PCI: Setting latency timer of device %s to %d\n",
264 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, lat
);
267 void __init
pcibios_update_irq(struct pci_dev
*dev
, int irq
)
269 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
272 char * __devinit
pcibios_setup(char *str
)
277 int pci_mmap_page_range(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
278 enum pci_mmap_state mmap_state
, int write_combine
)
281 * I/O space can be accessed via normal processor loads and stores on
282 * this platform but for now we elect not to do this and portable
283 * drivers should not do this anyway.
285 if (mmap_state
== pci_mmap_io
)
289 * Ignore write-combine; for now only return uncached mappings.
291 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
293 return remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
294 vma
->vm_end
- vma
->vm_start
,
298 #ifndef CONFIG_GENERIC_IOMAP
300 static void __iomem
*ioport_map_pci(struct pci_dev
*dev
,
301 unsigned long port
, unsigned int nr
)
303 struct pci_channel
*chan
= dev
->sysdata
;
305 if (!chan
->io_map_base
)
306 chan
->io_map_base
= generic_io_base
;
308 return (void __iomem
*)(chan
->io_map_base
+ port
);
311 void __iomem
*pci_iomap(struct pci_dev
*dev
, int bar
, unsigned long maxlen
)
313 resource_size_t start
= pci_resource_start(dev
, bar
);
314 resource_size_t len
= pci_resource_len(dev
, bar
);
315 unsigned long flags
= pci_resource_flags(dev
, bar
);
317 if (unlikely(!len
|| !start
))
319 if (maxlen
&& len
> maxlen
)
322 if (flags
& IORESOURCE_IO
)
323 return ioport_map_pci(dev
, start
, len
);
326 * Presently the IORESOURCE_MEM case is a bit special, most
327 * SH7751 style PCI controllers have PCI memory at a fixed
328 * location in the address space where no remapping is desired.
329 * With the IORESOURCE_MEM case more care has to be taken
330 * to inhibit page table mapping for legacy cores, but this is
331 * punted off to __ioremap().
334 if (flags
& IORESOURCE_MEM
) {
335 if (flags
& IORESOURCE_CACHEABLE
)
336 return ioremap(start
, len
);
338 return ioremap_nocache(start
, len
);
343 EXPORT_SYMBOL(pci_iomap
);
345 void pci_iounmap(struct pci_dev
*dev
, void __iomem
*addr
)
349 EXPORT_SYMBOL(pci_iounmap
);
351 #endif /* CONFIG_GENERIC_IOMAP */
353 #ifdef CONFIG_HOTPLUG
354 EXPORT_SYMBOL(pcibios_resource_to_bus
);
355 EXPORT_SYMBOL(pcibios_bus_to_resource
);
356 EXPORT_SYMBOL(PCIBIOS_MIN_IO
);
357 EXPORT_SYMBOL(PCIBIOS_MIN_MEM
);