1 /* pci-frv.c: low-level PCI access routines
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from the i386 equivalent stuff
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/init.h>
17 #include <linux/ioport.h>
18 #include <linux/errno.h>
23 * We need to avoid collisions with `mirrored' VGA ports
24 * and other strange ISA hardware, so we always want the
25 * addresses to be allocated in the 0x000-0x0ff region
28 * Why? Because some silly external IO cards only decode
29 * the low 10 bits of the IO address. The 0x00-0xff region
30 * is reserved for motherboard devices that decode all 16
31 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
32 * but we want to try to avoid allocating at 0x2900-0x2bff
33 * which might have be mirrored at 0x0100-0x03ff..
36 pcibios_align_resource(void *data
, const struct resource
*res
,
37 resource_size_t size
, resource_size_t align
)
39 resource_size_t start
= res
->start
;
41 if ((res
->flags
& IORESOURCE_IO
) && (start
& 0x300))
42 start
= (start
+ 0x3ff) & ~0x3ff;
49 * Handle resources of PCI devices. If the world were perfect, we could
50 * just allocate all the resource regions and do nothing more. It isn't.
51 * On the other hand, we cannot just re-allocate all devices, as it would
52 * require us to know lots of host bridge internals. So we attempt to
53 * keep as much of the original configuration as possible, but tweak it
54 * when it's found to be wrong.
56 * Known BIOS problems we have to work around:
57 * - I/O or memory regions not configured
58 * - regions configured, but not enabled in the command register
59 * - bogus I/O addresses above 64K used
60 * - expansion ROMs left enabled (this may sound harmless, but given
61 * the fact the PCI specs explicitly allow address decoders to be
62 * shared between expansion ROMs and other resource regions, it's
66 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
67 * This gives us fixed barriers on where we can allocate.
68 * (2) Allocate resources for all enabled devices. If there is
69 * a collision, just mark the resource as unallocated. Also
70 * disable expansion ROMs during this step.
71 * (3) Try to allocate resources for disabled devices. If the
72 * resources were assigned correctly, everything goes well,
73 * if they weren't, they won't disturb allocation of other
75 * (4) Assign new addresses to resources which were either
76 * not configured at all or misconfigured. If explicitly
77 * requested by the user, configure expansion ROM address
81 static void __init
pcibios_allocate_bus_resources(struct list_head
*bus_list
)
89 /* Depth-First Search on bus tree */
90 for (ln
=bus_list
->next
; ln
!= bus_list
; ln
=ln
->next
) {
92 if ((dev
= bus
->self
)) {
93 for (idx
= PCI_BRIDGE_RESOURCES
; idx
< PCI_NUM_RESOURCES
; idx
++) {
94 r
= &dev
->resource
[idx
];
97 if (pci_claim_resource(dev
, idx
) < 0)
98 printk(KERN_ERR
"PCI: Cannot allocate resource region %d of bridge %s\n", idx
, pci_name(dev
));
101 pcibios_allocate_bus_resources(&bus
->children
);
105 static void __init
pcibios_allocate_resources(int pass
)
107 struct pci_dev
*dev
= NULL
;
112 for_each_pci_dev(dev
) {
113 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
114 for(idx
= 0; idx
< 6; idx
++) {
115 r
= &dev
->resource
[idx
];
116 if (r
->parent
) /* Already allocated */
118 if (!r
->start
) /* Address not assigned at all */
120 if (r
->flags
& IORESOURCE_IO
)
121 disabled
= !(command
& PCI_COMMAND_IO
);
123 disabled
= !(command
& PCI_COMMAND_MEMORY
);
124 if (pass
== disabled
) {
125 DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
126 r
->start
, r
->end
, r
->flags
, disabled
, pass
);
127 if (pci_claim_resource(dev
, idx
) < 0) {
128 printk(KERN_ERR
"PCI: Cannot allocate resource region %d of device %s\n", idx
, pci_name(dev
));
129 /* We'll assign a new address later */
136 r
= &dev
->resource
[PCI_ROM_RESOURCE
];
137 if (r
->flags
& IORESOURCE_ROM_ENABLE
) {
138 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
140 DBG("PCI: Switching off ROM of %s\n", pci_name(dev
));
141 r
->flags
&= ~IORESOURCE_ROM_ENABLE
;
142 pci_read_config_dword(dev
, dev
->rom_base_reg
, ®
);
143 pci_write_config_dword(dev
, dev
->rom_base_reg
, reg
& ~PCI_ROM_ADDRESS_ENABLE
);
149 static void __init
pcibios_assign_resources(void)
151 struct pci_dev
*dev
= NULL
;
155 for_each_pci_dev(dev
) {
156 int class = dev
->class >> 8;
158 /* Don't touch classless devices and host bridges */
159 if (!class || class == PCI_CLASS_BRIDGE_HOST
)
162 for(idx
=0; idx
<6; idx
++) {
163 r
= &dev
->resource
[idx
];
166 * Don't touch IDE controllers and I/O ports of video cards!
168 if ((class == PCI_CLASS_STORAGE_IDE
&& idx
< 4) ||
169 (class == PCI_CLASS_DISPLAY_VGA
&& (r
->flags
& IORESOURCE_IO
)))
173 * We shall assign a new address to this resource, either because
174 * the BIOS forgot to do so or because we have decided the old
175 * address was unusable for some reason.
177 if (!r
->start
&& r
->end
)
178 pci_assign_resource(dev
, idx
);
181 if (pci_probe
& PCI_ASSIGN_ROMS
) {
182 r
= &dev
->resource
[PCI_ROM_RESOURCE
];
186 pci_assign_resource(dev
, PCI_ROM_RESOURCE
);
191 void __init
pcibios_resource_survey(void)
193 DBG("PCI: Allocating resources\n");
194 pcibios_allocate_bus_resources(&pci_root_buses
);
195 pcibios_allocate_resources(0);
196 pcibios_allocate_resources(1);
197 pcibios_assign_resources();
201 * If we set up a device for bus mastering, we need to check the latency
202 * timer as certain crappy BIOSes forget to set it properly.
204 unsigned int pcibios_max_latency
= 255;
206 void pcibios_set_master(struct pci_dev
*dev
)
209 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
211 lat
= (64 <= pcibios_max_latency
) ? 64 : pcibios_max_latency
;
212 else if (lat
> pcibios_max_latency
)
213 lat
= pcibios_max_latency
;
216 printk(KERN_DEBUG
"PCI: Setting latency timer of device %s to %d\n", pci_name(dev
), lat
);
217 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, lat
);