2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2 of the License, or (at your
5 * option) any later version.
7 * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
9 #include <linux/kernel.h>
11 #include <linux/bootmem.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/pci.h>
18 * Indicate whether we respect the PCI setup left by the firmware.
20 * Make this long-lived so that we know when shutting down
21 * whether we probed only or not.
25 #define PCI_ASSIGN_ALL_BUSSES 1
27 unsigned int pci_probe
= PCI_ASSIGN_ALL_BUSSES
;
30 * The PCI controller list.
33 static struct pci_controller
*hose_head
, **hose_tail
= &hose_head
;
35 unsigned long PCIBIOS_MIN_IO
;
36 unsigned long PCIBIOS_MIN_MEM
;
38 static int pci_initialized
;
41 * We need to avoid collisions with `mirrored' VGA ports
42 * and other strange ISA hardware, so we always want the
43 * addresses to be allocated in the 0x000-0x0ff region
46 * Why? Because some silly external IO cards only decode
47 * the low 10 bits of the IO address. The 0x00-0xff region
48 * is reserved for motherboard devices that decode all 16
49 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
50 * but we want to try to avoid allocating at 0x2900-0x2bff
51 * which might have be mirrored at 0x0100-0x03ff..
54 pcibios_align_resource(void *data
, const struct resource
*res
,
55 resource_size_t size
, resource_size_t align
)
57 struct pci_dev
*dev
= data
;
58 struct pci_controller
*hose
= dev
->sysdata
;
59 resource_size_t start
= res
->start
;
61 if (res
->flags
& IORESOURCE_IO
) {
62 /* Make sure we start at our min on all hoses */
63 if (start
< PCIBIOS_MIN_IO
+ hose
->io_resource
->start
)
64 start
= PCIBIOS_MIN_IO
+ hose
->io_resource
->start
;
67 * Put everything into 0x00-0xff region modulo 0x400
70 start
= (start
+ 0x3ff) & ~0x3ff;
71 } else if (res
->flags
& IORESOURCE_MEM
) {
72 /* Make sure we start at our min on all hoses */
73 if (start
< PCIBIOS_MIN_MEM
+ hose
->mem_resource
->start
)
74 start
= PCIBIOS_MIN_MEM
+ hose
->mem_resource
->start
;
80 static void __devinit
pcibios_scanbus(struct pci_controller
*hose
)
82 static int next_busno
;
83 static int need_domain_info
;
87 PCI_DMA_BUS_IS_PHYS
= 1;
89 if (hose
->get_busno
&& pci_probe_only
)
90 next_busno
= (*hose
->get_busno
)();
92 bus
= pci_scan_bus(next_busno
, hose
->pci_ops
, hose
);
95 need_domain_info
= need_domain_info
|| hose
->index
;
96 hose
->need_domain_info
= need_domain_info
;
98 next_busno
= bus
->subordinate
+ 1;
99 /* Don't allow 8-bit bus number overflow inside the hose -
100 reserve some space for bridges. */
101 if (next_busno
> 224) {
103 need_domain_info
= 1;
106 if (!pci_probe_only
) {
107 pci_bus_size_bridges(bus
);
108 pci_bus_assign_resources(bus
);
109 pci_enable_bridges(bus
);
114 static DEFINE_MUTEX(pci_scan_mutex
);
116 void __devinit
register_pci_controller(struct pci_controller
*hose
)
118 if (request_resource(&iomem_resource
, hose
->mem_resource
) < 0)
120 if (request_resource(&ioport_resource
, hose
->io_resource
) < 0) {
121 release_resource(hose
->mem_resource
);
126 hose_tail
= &hose
->next
;
129 * Do not panic here but later - this might happen before console init.
131 if (!hose
->io_map_base
) {
133 "registering PCI controller with io_map_base unset\n");
137 * Scan the bus if it is register after the PCI subsystem
140 if (pci_initialized
) {
141 mutex_lock(&pci_scan_mutex
);
142 pcibios_scanbus(hose
);
143 mutex_unlock(&pci_scan_mutex
);
150 "Skipping PCI bus scan due to resource conflict\n");
153 static int __init
pcibios_init(void)
155 struct pci_controller
*hose
;
157 /* Scan all of the recorded PCI controllers. */
158 for (hose
= hose_head
; hose
; hose
= hose
->next
)
159 pcibios_scanbus(hose
);
161 pci_fixup_irqs(pci_common_swizzle
, pcibios_map_irq
);
168 subsys_initcall(pcibios_init
);
170 static int pcibios_enable_resources(struct pci_dev
*dev
, int mask
)
176 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
178 for (idx
=0; idx
< PCI_NUM_RESOURCES
; idx
++) {
179 /* Only set up the requested stuff */
180 if (!(mask
& (1<<idx
)))
183 r
= &dev
->resource
[idx
];
184 if (!(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
186 if ((idx
== PCI_ROM_RESOURCE
) &&
187 (!(r
->flags
& IORESOURCE_ROM_ENABLE
)))
189 if (!r
->start
&& r
->end
) {
190 printk(KERN_ERR
"PCI: Device %s not available "
191 "because of resource collisions\n",
195 if (r
->flags
& IORESOURCE_IO
)
196 cmd
|= PCI_COMMAND_IO
;
197 if (r
->flags
& IORESOURCE_MEM
)
198 cmd
|= PCI_COMMAND_MEMORY
;
200 if (cmd
!= old_cmd
) {
201 printk("PCI: Enabling device %s (%04x -> %04x)\n",
202 pci_name(dev
), old_cmd
, cmd
);
203 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
209 * If we set up a device for bus mastering, we need to check the latency
210 * timer as certain crappy BIOSes forget to set it properly.
212 static unsigned int pcibios_max_latency
= 255;
214 void pcibios_set_master(struct pci_dev
*dev
)
217 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
219 lat
= (64 <= pcibios_max_latency
) ? 64 : pcibios_max_latency
;
220 else if (lat
> pcibios_max_latency
)
221 lat
= pcibios_max_latency
;
224 printk(KERN_DEBUG
"PCI: Setting latency timer of device %s to %d\n",
226 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, lat
);
229 unsigned int pcibios_assign_all_busses(void)
231 return (pci_probe
& PCI_ASSIGN_ALL_BUSSES
) ? 1 : 0;
234 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
238 if ((err
= pcibios_enable_resources(dev
, mask
)) < 0)
241 return pcibios_plat_dev_init(dev
);
244 static void pcibios_fixup_device_resources(struct pci_dev
*dev
,
247 /* Update device resources. */
248 struct pci_controller
*hose
= (struct pci_controller
*)bus
->sysdata
;
249 unsigned long offset
= 0;
252 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
253 if (!dev
->resource
[i
].start
)
255 if (dev
->resource
[i
].flags
& IORESOURCE_IO
)
256 offset
= hose
->io_offset
;
257 else if (dev
->resource
[i
].flags
& IORESOURCE_MEM
)
258 offset
= hose
->mem_offset
;
260 dev
->resource
[i
].start
+= offset
;
261 dev
->resource
[i
].end
+= offset
;
265 void __devinit
pcibios_fixup_bus(struct pci_bus
*bus
)
267 /* Propagate hose info into the subordinate devices. */
269 struct pci_controller
*hose
= bus
->sysdata
;
270 struct list_head
*ln
;
271 struct pci_dev
*dev
= bus
->self
;
274 bus
->resource
[0] = hose
->io_resource
;
275 bus
->resource
[1] = hose
->mem_resource
;
276 } else if (pci_probe_only
&&
277 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
278 pci_read_bridge_bases(bus
);
279 pcibios_fixup_device_resources(dev
, bus
);
282 for (ln
= bus
->devices
.next
; ln
!= &bus
->devices
; ln
= ln
->next
) {
285 if ((dev
->class >> 8) != PCI_CLASS_BRIDGE_PCI
)
286 pcibios_fixup_device_resources(dev
, bus
);
291 pcibios_update_irq(struct pci_dev
*dev
, int irq
)
293 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
296 void pcibios_resource_to_bus(struct pci_dev
*dev
, struct pci_bus_region
*region
,
297 struct resource
*res
)
299 struct pci_controller
*hose
= (struct pci_controller
*)dev
->sysdata
;
300 unsigned long offset
= 0;
302 if (res
->flags
& IORESOURCE_IO
)
303 offset
= hose
->io_offset
;
304 else if (res
->flags
& IORESOURCE_MEM
)
305 offset
= hose
->mem_offset
;
307 region
->start
= res
->start
- offset
;
308 region
->end
= res
->end
- offset
;
312 pcibios_bus_to_resource(struct pci_dev
*dev
, struct resource
*res
,
313 struct pci_bus_region
*region
)
315 struct pci_controller
*hose
= (struct pci_controller
*)dev
->sysdata
;
316 unsigned long offset
= 0;
318 if (res
->flags
& IORESOURCE_IO
)
319 offset
= hose
->io_offset
;
320 else if (res
->flags
& IORESOURCE_MEM
)
321 offset
= hose
->mem_offset
;
323 res
->start
= region
->start
+ offset
;
324 res
->end
= region
->end
+ offset
;
327 #ifdef CONFIG_HOTPLUG
328 EXPORT_SYMBOL(pcibios_resource_to_bus
);
329 EXPORT_SYMBOL(pcibios_bus_to_resource
);
330 EXPORT_SYMBOL(PCIBIOS_MIN_IO
);
331 EXPORT_SYMBOL(PCIBIOS_MIN_MEM
);
334 int pci_mmap_page_range(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
335 enum pci_mmap_state mmap_state
, int write_combine
)
340 * I/O space can be accessed via normal processor loads and stores on
341 * this platform but for now we elect not to do this and portable
342 * drivers should not do this anyway.
344 if (mmap_state
== pci_mmap_io
)
348 * Ignore write-combine; for now only return uncached mappings.
350 prot
= pgprot_val(vma
->vm_page_prot
);
351 prot
= (prot
& ~_CACHE_MASK
) | _CACHE_UNCACHED
;
352 vma
->vm_page_prot
= __pgprot(prot
);
354 return remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
355 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
358 char * (*pcibios_plat_setup
)(char *str
) __devinitdata
;
360 char *__devinit
pcibios_setup(char *str
)
362 if (pcibios_plat_setup
)
363 return pcibios_plat_setup(str
);