2 * arch/ppc/syslib/pci_auto.c
4 * PCI autoconfiguration library
6 * Author: Matt Porter <mporter@mvista.com>
8 * 2001 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
15 * The CardBus support is very preliminary. Preallocating space is
16 * the way to go but will require some change in card services to
17 * make it useful. Eventually this will ensure that we can put
18 * multiple CB bridges behind multiple P2P bridges. For now, at
19 * least it ensures that we place the CB bridge BAR and assigned
20 * initial bus numbers. I definitely need to do something about
21 * the lack of 16-bit I/O support. -MDP
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/pci.h>
28 #include <asm/pci-bridge.h>
30 #define PCIAUTO_IDE_MODE_MASK 0x05
35 #define DBG(x...) printk(x)
40 static int pciauto_upper_iospc;
41 static int pciauto_upper_memspc;
43 void __init pciauto_setup_bars(struct pci_controller *hose,
48 int bar_response, bar_size, bar_value;
53 DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
54 current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn) );
56 for (bar = PCI_BASE_ADDRESS_0; bar <= bar_limit; bar+=4) {
57 /* Tickle the BAR and get the response */
58 early_write_config_dword(hose,
63 early_read_config_dword(hose,
69 /* If BAR is not implemented go to the next BAR */
73 /* Check the BAR type and set our address mask */
74 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
75 addr_mask = PCI_BASE_ADDRESS_IO_MASK;
76 upper_limit = &pciauto_upper_iospc;
77 DBG("PCI Autoconfig: BAR 0x%x, I/O, ", bar);
79 if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
80 PCI_BASE_ADDRESS_MEM_TYPE_64)
83 addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
84 upper_limit = &pciauto_upper_memspc;
85 DBG("PCI Autoconfig: BAR 0x%x, Mem ", bar);
88 /* Calculate requested size */
89 bar_size = ~(bar_response & addr_mask) + 1;
91 /* Allocate a base address */
92 bar_value = (*upper_limit - bar_size) & ~(bar_size - 1);
94 /* Write it out and update our limit */
95 early_write_config_dword(hose,
101 *upper_limit = bar_value;
104 * If we are a 64-bit decoder then increment to the
105 * upper 32 bits of the bar and force it to locate
106 * in the lower 4GB of memory.
110 early_write_config_dword(hose,
118 DBG("size=0x%x, address=0x%x\n",
119 bar_size, bar_value);
124 void __init pciauto_prescan_setup_bridge(struct pci_controller *hose,
131 /* Configure bus number registers */
132 early_write_config_byte(hose,
137 early_write_config_byte(hose,
142 early_write_config_byte(hose,
148 /* Round memory allocator to 1MB boundary */
149 pciauto_upper_memspc &= ~(0x100000 - 1);
150 *memsave = pciauto_upper_memspc;
152 /* Round I/O allocator to 4KB boundary */
153 pciauto_upper_iospc &= ~(0x1000 - 1);
154 *iosave = pciauto_upper_iospc;
156 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
157 early_write_config_word(hose,
161 ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
162 early_write_config_byte(hose,
166 ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
167 early_write_config_word(hose,
170 PCI_IO_LIMIT_UPPER16,
171 ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
173 /* Zero upper 32 bits of prefetchable base/limit */
174 early_write_config_dword(hose,
177 PCI_PREF_BASE_UPPER32,
179 early_write_config_dword(hose,
182 PCI_PREF_LIMIT_UPPER32,
186 void __init pciauto_postscan_setup_bridge(struct pci_controller *hose,
195 /* Configure bus number registers */
196 early_write_config_byte(hose,
203 * Round memory allocator to 1MB boundary.
204 * If no space used, allocate minimum.
206 pciauto_upper_memspc &= ~(0x100000 - 1);
207 if (*memsave == pciauto_upper_memspc)
208 pciauto_upper_memspc -= 0x00100000;
210 early_write_config_word(hose,
214 pciauto_upper_memspc >> 16);
216 /* Allocate 1MB for pre-fretch */
217 early_write_config_word(hose,
220 PCI_PREF_MEMORY_LIMIT,
221 ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
223 pciauto_upper_memspc -= 0x100000;
225 early_write_config_word(hose,
228 PCI_PREF_MEMORY_BASE,
229 pciauto_upper_memspc >> 16);
231 /* Round I/O allocator to 4KB boundary */
232 pciauto_upper_iospc &= ~(0x1000 - 1);
233 if (*iosave == pciauto_upper_iospc)
234 pciauto_upper_iospc -= 0x1000;
236 early_write_config_byte(hose,
240 (pciauto_upper_iospc & 0x0000f000) >> 8);
241 early_write_config_word(hose,
245 pciauto_upper_iospc >> 16);
247 /* Enable memory and I/O accesses, enable bus master */
248 early_read_config_dword(hose,
253 early_write_config_dword(hose,
263 void __init pciauto_prescan_setup_cardbus_bridge(struct pci_controller *hose,
270 /* Configure bus number registers */
271 early_write_config_byte(hose,
276 early_write_config_byte(hose,
281 early_write_config_byte(hose,
287 /* Round memory allocator to 4KB boundary */
288 pciauto_upper_memspc &= ~(0x1000 - 1);
289 *memsave = pciauto_upper_memspc;
291 /* Round I/O allocator to 4 byte boundary */
292 pciauto_upper_iospc &= ~(0x4 - 1);
293 *iosave = pciauto_upper_iospc;
295 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
296 early_write_config_dword(hose,
300 pciauto_upper_memspc - 1);
301 early_write_config_dword(hose,
305 pciauto_upper_iospc - 1);
308 void __init pciauto_postscan_setup_cardbus_bridge(struct pci_controller *hose,
318 * Configure subordinate bus number. The PCI subsystem
319 * bus scan will renumber buses (reserving three additional
320 * for this PCI<->CardBus bridge for the case where a CardBus
321 * adapter contains a P2P or CB2CB bridge.
323 early_write_config_byte(hose,
330 * Reserve an additional 4MB for mem space and 16KB for
331 * I/O space. This should cover any additional space
332 * requirement of unusual CardBus devices with
333 * additional bridges that can consume more address space.
335 * Although pcmcia-cs currently will reprogram bridge
336 * windows, the goal is to add an option to leave them
337 * alone and use the bridge window ranges as the regions
338 * that are searched for free resources upon hot-insertion
339 * of a device. This will allow a PCI<->CardBus bridge
340 * configured by this routine to happily live behind a
341 * P2P bridge in a system.
343 pciauto_upper_memspc -= 0x00400000;
344 pciauto_upper_iospc -= 0x00004000;
346 /* Round memory allocator to 4KB boundary */
347 pciauto_upper_memspc &= ~(0x1000 - 1);
349 early_write_config_dword(hose,
353 pciauto_upper_memspc);
355 /* Round I/O allocator to 4 byte boundary */
356 pciauto_upper_iospc &= ~(0x4 - 1);
357 early_write_config_dword(hose,
361 pciauto_upper_iospc);
363 /* Enable memory and I/O accesses, enable bus master */
364 early_read_config_dword(hose,
369 early_write_config_dword(hose,
379 int __init pciauto_bus_scan(struct pci_controller *hose, int current_bus)
381 int sub_bus, pci_devfn, pci_class, cmdstat, found_multi = 0;
383 unsigned char header_type;
386 * Fetch our I/O and memory space upper boundaries used
387 * to allocated base addresses on this hose.
389 if (current_bus == hose->first_busno) {
390 pciauto_upper_iospc = hose->io_space.end + 1;
391 pciauto_upper_memspc = hose->mem_space.end + 1;
394 sub_bus = current_bus;
396 for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
397 /* Skip our host bridge */
398 if ( (current_bus == hose->first_busno) && (pci_devfn == 0) )
401 if (PCI_FUNC(pci_devfn) && !found_multi)
404 /* If config space read fails from this device, move on */
405 if (early_read_config_byte(hose,
412 if (!PCI_FUNC(pci_devfn))
413 found_multi = header_type & 0x80;
415 early_read_config_word(hose,
422 early_read_config_dword(hose,
425 PCI_CLASS_REVISION, &pci_class);
426 if ( (pci_class >> 16) == PCI_CLASS_BRIDGE_PCI ) {
429 DBG("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_SLOT(pci_devfn));
430 /* Allocate PCI I/O and/or memory space */
431 pciauto_setup_bars(hose,
436 pciauto_prescan_setup_bridge(hose,
442 sub_bus = pciauto_bus_scan(hose, sub_bus+1);
443 pciauto_postscan_setup_bridge(hose,
449 } else if ((pci_class >> 16) == PCI_CLASS_BRIDGE_CARDBUS) {
452 DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
453 /* Place CardBus Socket/ExCA registers */
454 pciauto_setup_bars(hose,
459 pciauto_prescan_setup_cardbus_bridge(hose,
465 sub_bus = pciauto_bus_scan(hose, sub_bus+1);
466 pciauto_postscan_setup_cardbus_bridge(hose,
473 if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
474 unsigned char prg_iface;
476 early_read_config_byte(hose,
481 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
482 DBG("PCI Autoconfig: Skipping legacy mode IDE controller\n");
486 /* Allocate PCI I/O and/or memory space */
487 pciauto_setup_bars(hose,
493 * Enable some standard settings
495 early_read_config_dword(hose,
500 early_write_config_dword(hose,
508 early_write_config_byte(hose,