2 * PCI autoconfiguration library
4 * Author: Matt Porter <mporter@mvista.com>
6 * 2001 (c) MontaVista, Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
13 * The CardBus support is very preliminary. Preallocating space is
14 * the way to go but will require some change in card services to
15 * make it useful. Eventually this will ensure that we can put
16 * multiple CB bridges behind multiple P2P bridges. For now, at
17 * least it ensures that we place the CB bridge BAR and assigned
18 * initial bus numbers. I definitely need to do something about
19 * the lack of 16-bit I/O support. -MDP
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
26 #include <asm/pci-bridge.h>
28 #define PCIAUTO_IDE_MODE_MASK 0x05
33 #define DBG(x...) printk(x)
38 static int pciauto_upper_iospc
;
39 static int pciauto_upper_memspc
;
41 void __init
pciauto_setup_bars(struct pci_controller
*hose
,
46 int bar_response
, bar_size
, bar_value
;
51 DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
52 current_bus
, PCI_SLOT(pci_devfn
), PCI_FUNC(pci_devfn
) );
54 for (bar
= PCI_BASE_ADDRESS_0
; bar
<= bar_limit
; bar
+=4) {
55 /* Tickle the BAR and get the response */
56 early_write_config_dword(hose
,
61 early_read_config_dword(hose
,
67 /* If BAR is not implemented go to the next BAR */
71 /* Check the BAR type and set our address mask */
72 if (bar_response
& PCI_BASE_ADDRESS_SPACE
) {
73 addr_mask
= PCI_BASE_ADDRESS_IO_MASK
;
74 upper_limit
= &pciauto_upper_iospc
;
75 DBG("PCI Autoconfig: BAR 0x%x, I/O, ", bar
);
77 if ( (bar_response
& PCI_BASE_ADDRESS_MEM_TYPE_MASK
) ==
78 PCI_BASE_ADDRESS_MEM_TYPE_64
)
81 addr_mask
= PCI_BASE_ADDRESS_MEM_MASK
;
82 upper_limit
= &pciauto_upper_memspc
;
83 DBG("PCI Autoconfig: BAR 0x%x, Mem ", bar
);
86 /* Calculate requested size */
87 bar_size
= ~(bar_response
& addr_mask
) + 1;
89 /* Allocate a base address */
90 bar_value
= (*upper_limit
- bar_size
) & ~(bar_size
- 1);
92 /* Write it out and update our limit */
93 early_write_config_dword(hose
,
99 *upper_limit
= bar_value
;
102 * If we are a 64-bit decoder then increment to the
103 * upper 32 bits of the bar and force it to locate
104 * in the lower 4GB of memory.
108 early_write_config_dword(hose
,
116 DBG("size=0x%x, address=0x%x\n",
117 bar_size
, bar_value
);
122 void __init
pciauto_prescan_setup_bridge(struct pci_controller
*hose
,
129 /* Configure bus number registers */
130 early_write_config_byte(hose
,
135 early_write_config_byte(hose
,
140 early_write_config_byte(hose
,
146 /* Round memory allocator to 1MB boundary */
147 pciauto_upper_memspc
&= ~(0x100000 - 1);
148 *memsave
= pciauto_upper_memspc
;
150 /* Round I/O allocator to 4KB boundary */
151 pciauto_upper_iospc
&= ~(0x1000 - 1);
152 *iosave
= pciauto_upper_iospc
;
154 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
155 early_write_config_word(hose
,
159 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
160 early_write_config_byte(hose
,
164 ((pciauto_upper_iospc
- 1) & 0x0000f000) >> 8);
165 early_write_config_word(hose
,
168 PCI_IO_LIMIT_UPPER16
,
169 ((pciauto_upper_iospc
- 1) & 0xffff0000) >> 16);
171 /* Zero upper 32 bits of prefetchable base/limit */
172 early_write_config_dword(hose
,
175 PCI_PREF_BASE_UPPER32
,
177 early_write_config_dword(hose
,
180 PCI_PREF_LIMIT_UPPER32
,
184 void __init
pciauto_postscan_setup_bridge(struct pci_controller
*hose
,
193 /* Configure bus number registers */
194 early_write_config_byte(hose
,
201 * Round memory allocator to 1MB boundary.
202 * If no space used, allocate minimum.
204 pciauto_upper_memspc
&= ~(0x100000 - 1);
205 if (*memsave
== pciauto_upper_memspc
)
206 pciauto_upper_memspc
-= 0x00100000;
208 early_write_config_word(hose
,
212 pciauto_upper_memspc
>> 16);
214 /* Allocate 1MB for pre-fretch */
215 early_write_config_word(hose
,
218 PCI_PREF_MEMORY_LIMIT
,
219 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
221 pciauto_upper_memspc
-= 0x100000;
223 early_write_config_word(hose
,
226 PCI_PREF_MEMORY_BASE
,
227 pciauto_upper_memspc
>> 16);
229 /* Round I/O allocator to 4KB boundary */
230 pciauto_upper_iospc
&= ~(0x1000 - 1);
231 if (*iosave
== pciauto_upper_iospc
)
232 pciauto_upper_iospc
-= 0x1000;
234 early_write_config_byte(hose
,
238 (pciauto_upper_iospc
& 0x0000f000) >> 8);
239 early_write_config_word(hose
,
243 pciauto_upper_iospc
>> 16);
245 /* Enable memory and I/O accesses, enable bus master */
246 early_read_config_dword(hose
,
251 early_write_config_dword(hose
,
261 void __init
pciauto_prescan_setup_cardbus_bridge(struct pci_controller
*hose
,
268 /* Configure bus number registers */
269 early_write_config_byte(hose
,
274 early_write_config_byte(hose
,
279 early_write_config_byte(hose
,
285 /* Round memory allocator to 4KB boundary */
286 pciauto_upper_memspc
&= ~(0x1000 - 1);
287 *memsave
= pciauto_upper_memspc
;
289 /* Round I/O allocator to 4 byte boundary */
290 pciauto_upper_iospc
&= ~(0x4 - 1);
291 *iosave
= pciauto_upper_iospc
;
293 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
294 early_write_config_dword(hose
,
298 pciauto_upper_memspc
- 1);
299 early_write_config_dword(hose
,
303 pciauto_upper_iospc
- 1);
306 void __init
pciauto_postscan_setup_cardbus_bridge(struct pci_controller
*hose
,
316 * Configure subordinate bus number. The PCI subsystem
317 * bus scan will renumber buses (reserving three additional
318 * for this PCI<->CardBus bridge for the case where a CardBus
319 * adapter contains a P2P or CB2CB bridge.
321 early_write_config_byte(hose
,
328 * Reserve an additional 4MB for mem space and 16KB for
329 * I/O space. This should cover any additional space
330 * requirement of unusual CardBus devices with
331 * additional bridges that can consume more address space.
333 * Although pcmcia-cs currently will reprogram bridge
334 * windows, the goal is to add an option to leave them
335 * alone and use the bridge window ranges as the regions
336 * that are searched for free resources upon hot-insertion
337 * of a device. This will allow a PCI<->CardBus bridge
338 * configured by this routine to happily live behind a
339 * P2P bridge in a system.
341 pciauto_upper_memspc
-= 0x00400000;
342 pciauto_upper_iospc
-= 0x00004000;
344 /* Round memory allocator to 4KB boundary */
345 pciauto_upper_memspc
&= ~(0x1000 - 1);
347 early_write_config_dword(hose
,
351 pciauto_upper_memspc
);
353 /* Round I/O allocator to 4 byte boundary */
354 pciauto_upper_iospc
&= ~(0x4 - 1);
355 early_write_config_dword(hose
,
359 pciauto_upper_iospc
);
361 /* Enable memory and I/O accesses, enable bus master */
362 early_read_config_dword(hose
,
367 early_write_config_dword(hose
,
377 int __init
pciauto_bus_scan(struct pci_controller
*hose
, int current_bus
)
379 int sub_bus
, pci_devfn
, pci_class
, cmdstat
, found_multi
= 0;
381 unsigned char header_type
;
384 * Fetch our I/O and memory space upper boundaries used
385 * to allocated base addresses on this hose.
387 if (current_bus
== hose
->first_busno
) {
388 pciauto_upper_iospc
= hose
->io_space
.end
+ 1;
389 pciauto_upper_memspc
= hose
->mem_space
.end
+ 1;
392 sub_bus
= current_bus
;
394 for (pci_devfn
= 0; pci_devfn
< 0xff; pci_devfn
++) {
395 /* Skip our host bridge */
396 if ( (current_bus
== hose
->first_busno
) && (pci_devfn
== 0) )
399 if (PCI_FUNC(pci_devfn
) && !found_multi
)
402 /* If config space read fails from this device, move on */
403 if (early_read_config_byte(hose
,
410 if (!PCI_FUNC(pci_devfn
))
411 found_multi
= header_type
& 0x80;
413 early_read_config_word(hose
,
420 early_read_config_dword(hose
,
423 PCI_CLASS_REVISION
, &pci_class
);
424 if ( (pci_class
>> 16) == PCI_CLASS_BRIDGE_PCI
) {
427 DBG("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_SLOT(pci_devfn
));
428 /* Allocate PCI I/O and/or memory space */
429 pciauto_setup_bars(hose
,
434 pciauto_prescan_setup_bridge(hose
,
440 sub_bus
= pciauto_bus_scan(hose
, sub_bus
+1);
441 pciauto_postscan_setup_bridge(hose
,
447 } else if ((pci_class
>> 16) == PCI_CLASS_BRIDGE_CARDBUS
) {
450 DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn
), PCI_FUNC(pci_devfn
));
451 /* Place CardBus Socket/ExCA registers */
452 pciauto_setup_bars(hose
,
457 pciauto_prescan_setup_cardbus_bridge(hose
,
463 sub_bus
= pciauto_bus_scan(hose
, sub_bus
+1);
464 pciauto_postscan_setup_cardbus_bridge(hose
,
471 if ((pci_class
>> 16) == PCI_CLASS_STORAGE_IDE
) {
472 unsigned char prg_iface
;
474 early_read_config_byte(hose
,
479 if (!(prg_iface
& PCIAUTO_IDE_MODE_MASK
)) {
480 DBG("PCI Autoconfig: Skipping legacy mode IDE controller\n");
484 /* Allocate PCI I/O and/or memory space */
485 pciauto_setup_bars(hose
,
491 * Enable some standard settings
493 early_read_config_dword(hose
,
498 early_write_config_dword(hose
,
506 early_write_config_byte(hose
,