2 * arch/ppc/kernel/pci_auto.c
4 * PCI autoconfiguration library
6 * Author: Matt Porter <mporter@mvista.com>
8 * Copyright 2001 MontaVista Software Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 * The CardBus support is very preliminary. Preallocating space is
18 * the way to go but will require some change in card services to
19 * make it useful. Eventually this will ensure that we can put
20 * multiple CB bridges behind multiple P2P bridges. For now, at
21 * least it ensures that we place the CB bridge BAR and assigned
22 * initial bus numbers. I definitely need to do something about
23 * the lack of 16-bit I/O support. -MDP
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
30 #include <asm/pci-bridge.h>
32 #define PCIAUTO_IDE_MODE_MASK 0x05
38 #define DBG(x...) printk(x)
43 static int pciauto_upper_iospc;
44 static int pciauto_upper_memspc;
46 void __init pciauto_setup_bars(struct pci_controller *hose,
51 int bar_response, bar_size, bar_value;
56 DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
57 current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn) );
59 for (bar = PCI_BASE_ADDRESS_0; bar <= bar_limit; bar+=4) {
60 /* Tickle the BAR and get the response */
61 early_write_config_dword(hose,
66 early_read_config_dword(hose,
72 /* If BAR is not implemented go to the next BAR */
76 /* Check the BAR type and set our address mask */
77 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
78 addr_mask = PCI_BASE_ADDRESS_IO_MASK;
79 upper_limit = &pciauto_upper_iospc;
80 DBG("PCI Autoconfig: BAR 0x%x, I/O, ", bar);
82 if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
83 PCI_BASE_ADDRESS_MEM_TYPE_64)
86 addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
87 upper_limit = &pciauto_upper_memspc;
88 DBG("PCI Autoconfig: BAR 0x%x, Mem ", bar);
91 /* Calculate requested size */
92 bar_size = ~(bar_response & addr_mask) + 1;
94 /* Allocate a base address */
95 bar_value = (*upper_limit - bar_size) & ~(bar_size - 1);
97 /* Write it out and update our limit */
98 early_write_config_dword(hose,
104 *upper_limit = bar_value;
107 * If we are a 64-bit decoder then increment to the
108 * upper 32 bits of the bar and force it to locate
109 * in the lower 4GB of memory.
113 early_write_config_dword(hose,
121 DBG("size=0x%x, address=0x%x\n",
122 bar_size, bar_value);
127 void __init pciauto_prescan_setup_bridge(struct pci_controller *hose,
134 /* Configure bus number registers */
135 early_write_config_byte(hose,
140 early_write_config_byte(hose,
145 early_write_config_byte(hose,
151 /* Round memory allocator to 1MB boundary */
152 pciauto_upper_memspc &= ~(0x100000 - 1);
153 *memsave = pciauto_upper_memspc;
155 /* Round I/O allocator to 4KB boundary */
156 pciauto_upper_iospc &= ~(0x1000 - 1);
157 *iosave = pciauto_upper_iospc;
159 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
160 early_write_config_word(hose,
164 ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
165 early_write_config_byte(hose,
169 ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
170 early_write_config_word(hose,
173 PCI_IO_LIMIT_UPPER16,
174 ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
176 /* Zero upper 32 bits of prefetchable base/limit */
177 early_write_config_dword(hose,
180 PCI_PREF_BASE_UPPER32,
182 early_write_config_dword(hose,
185 PCI_PREF_LIMIT_UPPER32,
189 void __init pciauto_postscan_setup_bridge(struct pci_controller *hose,
198 /* Configure bus number registers */
199 early_write_config_byte(hose,
206 * Round memory allocator to 1MB boundary.
207 * If no space used, allocate minimum.
209 pciauto_upper_memspc &= ~(0x100000 - 1);
210 if (*memsave == pciauto_upper_memspc)
211 pciauto_upper_memspc -= 0x00100000;
213 early_write_config_word(hose,
217 pciauto_upper_memspc >> 16);
219 /* Allocate 1MB for pre-fretch */
220 early_write_config_word(hose,
223 PCI_PREF_MEMORY_LIMIT,
224 ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
226 pciauto_upper_memspc -= 0x100000;
228 early_write_config_word(hose,
231 PCI_PREF_MEMORY_BASE,
232 pciauto_upper_memspc >> 16);
234 /* Round I/O allocator to 4KB boundary */
235 pciauto_upper_iospc &= ~(0x1000 - 1);
236 if (*iosave == pciauto_upper_iospc)
237 pciauto_upper_iospc -= 0x1000;
239 early_write_config_byte(hose,
243 (pciauto_upper_iospc & 0x0000f000) >> 8);
244 early_write_config_word(hose,
248 pciauto_upper_iospc >> 16);
250 /* Enable memory and I/O accesses, enable bus master */
251 early_read_config_dword(hose,
256 early_write_config_dword(hose,
266 void __init pciauto_prescan_setup_cardbus_bridge(struct pci_controller *hose,
273 /* Configure bus number registers */
274 early_write_config_byte(hose,
279 early_write_config_byte(hose,
284 early_write_config_byte(hose,
290 /* Round memory allocator to 4KB boundary */
291 pciauto_upper_memspc &= ~(0x1000 - 1);
292 *memsave = pciauto_upper_memspc;
294 /* Round I/O allocator to 4 byte boundary */
295 pciauto_upper_iospc &= ~(0x4 - 1);
296 *iosave = pciauto_upper_iospc;
298 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
299 early_write_config_dword(hose,
303 pciauto_upper_memspc - 1);
304 early_write_config_dword(hose,
308 pciauto_upper_iospc - 1);
311 void __init pciauto_postscan_setup_cardbus_bridge(struct pci_controller *hose,
321 * Configure subordinate bus number. The PCI subsystem
322 * bus scan will renumber buses (reserving three additional
323 * for this PCI<->CardBus bridge for the case where a CardBus
324 * adapter contains a P2P or CB2CB bridge.
326 early_write_config_byte(hose,
333 * Reserve an additional 4MB for mem space and 16KB for
334 * I/O space. This should cover any additional space
335 * requirement of unusual CardBus devices with
336 * additional bridges that can consume more address space.
338 * Although pcmcia-cs currently will reprogram bridge
339 * windows, the goal is to add an option to leave them
340 * alone and use the bridge window ranges as the regions
341 * that are searched for free resources upon hot-insertion
342 * of a device. This will allow a PCI<->CardBus bridge
343 * configured by this routine to happily live behind a
344 * P2P bridge in a system.
346 pciauto_upper_memspc -= 0x00400000;
347 pciauto_upper_iospc -= 0x00004000;
349 /* Round memory allocator to 4KB boundary */
350 pciauto_upper_memspc &= ~(0x1000 - 1);
352 early_write_config_dword(hose,
356 pciauto_upper_memspc);
358 /* Round I/O allocator to 4 byte boundary */
359 pciauto_upper_iospc &= ~(0x4 - 1);
360 early_write_config_dword(hose,
364 pciauto_upper_iospc);
366 /* Enable memory and I/O accesses, enable bus master */
367 early_read_config_dword(hose,
372 early_write_config_dword(hose,
382 int __init pciauto_bus_scan(struct pci_controller *hose, int current_bus)
384 int sub_bus, pci_devfn, pci_class, cmdstat, found_multi = 0;
386 unsigned char header_type;
389 * Fetch our I/O and memory space upper boundaries used
390 * to allocated base addresses on this hose.
392 if (current_bus == hose->first_busno) {
393 pciauto_upper_iospc = hose->io_space.end + 1;
394 pciauto_upper_memspc = hose->mem_space.end + 1;
397 sub_bus = current_bus;
399 for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
400 /* Skip our host bridge */
401 if ( (current_bus == hose->first_busno) && (pci_devfn == 0) )
404 if (PCI_FUNC(pci_devfn) && !found_multi)
407 /* If config space read fails from this device, move on */
408 if (early_read_config_byte(hose,
415 if (!PCI_FUNC(pci_devfn))
416 found_multi = header_type & 0x80;
418 early_read_config_word(hose,
425 early_read_config_dword(hose,
428 PCI_CLASS_REVISION, &pci_class);
429 if ( (pci_class >> 16) == PCI_CLASS_BRIDGE_PCI ) {
432 DBG("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_SLOT(pci_devfn));
433 /* Allocate PCI I/O and/or memory space */
434 pciauto_setup_bars(hose,
439 pciauto_prescan_setup_bridge(hose,
445 sub_bus = pciauto_bus_scan(hose, sub_bus+1);
446 pciauto_postscan_setup_bridge(hose,
452 } else if ((pci_class >> 16) == PCI_CLASS_BRIDGE_CARDBUS) {
455 DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
456 /* Place CardBus Socket/ExCA registers */
457 pciauto_setup_bars(hose,
462 pciauto_prescan_setup_cardbus_bridge(hose,
468 sub_bus = pciauto_bus_scan(hose, sub_bus+1);
469 pciauto_postscan_setup_cardbus_bridge(hose,
476 if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
477 unsigned char prg_iface;
479 early_read_config_byte(hose,
484 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
485 DBG("PCI Autoconfig: Skipping legacy mode IDE controller\n");
489 /* Allocate PCI I/O and/or memory space */
490 pciauto_setup_bars(hose,
496 * Enable some standard settings
498 early_read_config_dword(hose,
503 early_write_config_dword(hose,
511 early_write_config_byte(hose,