2 * PCI autoconfiguration library
4 * Author: Matt Porter <mporter@mvista.com>
6 * Copyright 2000, 2001, 2002, 2003 MontaVista Software Inc.
7 * Copyright 2001 Bradley D. LaRonde <brad@ltc.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 * Modified for MIPS by Jun Sun, jsun@mvista.com
18 * . Simplify the interface between pci_auto and the rest: a single function.
19 * . Assign resources from low address to upper address.
20 * . change most int to u32.
22 * Further modified to include it as mips generic code, ppopov@mvista.com.
24 * 2001-10-26 Bradley D. LaRonde <brad@ltc.com>
25 * - Add a top_bus argument to the "early config" functions so that
26 * they can set a fake parent bus pointer to convince the underlying
27 * pci ops to use type 1 configuration for sub busses.
28 * - Set bridge base and limit registers correctly.
29 * - Align io and memory base properly before and after bridge setup.
30 * - Don't fall through to pci_setup_bars for bridge.
31 * - Reformat the debug output to look more like lspci's output.
33 * 2003-04-09 Yoichi Yuasa, Alice Hennessy, Jun Sun
34 * - Add cardbus bridge support, mostly copied from PPC
37 #include <linux/kernel.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/pci.h>
42 #include <asm/pci_channel.h>
46 #define DBG(x...) printk(x)
52 * These functions are used early on before PCI scanning is done
53 * and all of the pci_dev and pci_bus structures have been created.
55 static struct pci_dev *fake_pci_dev(struct pci_channel *hose,
56 int top_bus, int busnr, int devfn)
58 static struct pci_dev dev;
59 static struct pci_bus bus;
65 bus.ops = hose->pci_ops;
68 /* Fake a parent bus structure. */
76 #define EARLY_PCI_OP(rw, size, type) \
77 int early_##rw##_config_##size(struct pci_channel *hose, \
78 int top_bus, int bus, int devfn, int offset, type value) \
80 return pci_##rw##_config_##size( \
81 fake_pci_dev(hose, top_bus, bus, devfn), \
85 EARLY_PCI_OP(read, byte, u8 *)
86 EARLY_PCI_OP(read, word, u16 *)
87 EARLY_PCI_OP(read, dword, u32 *)
88 EARLY_PCI_OP(write, byte, u8)
89 EARLY_PCI_OP(write, word, u16)
90 EARLY_PCI_OP(write, dword, u32)
92 static struct resource *io_resource_inuse;
93 static struct resource *mem_resource_inuse;
95 static u32 pciauto_lower_iospc;
96 static u32 pciauto_upper_iospc;
98 static u32 pciauto_lower_memspc;
99 static u32 pciauto_upper_memspc;
102 pciauto_setup_bars(struct pci_channel *hose,
108 u32 bar_response, bar_size, bar_value;
109 u32 bar, addr_mask, bar_nr = 0;
114 for (bar = PCI_BASE_ADDRESS_0; bar <= bar_limit; bar+=4) {
115 /* Tickle the BAR and get the response */
116 early_write_config_dword(hose, top_bus,
121 early_read_config_dword(hose, top_bus,
127 /* If BAR is not implemented go to the next BAR */
132 * Workaround for a BAR that doesn't use its upper word,
133 * like the ALi 1535D+ PCI DC-97 Controller Modem (M5457).
136 if (!(bar_response & 0xffff0000))
137 bar_response |= 0xffff0000;
140 /* Check the BAR type and set our address mask */
141 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
142 addr_mask = PCI_BASE_ADDRESS_IO_MASK;
143 upper_limit = &pciauto_upper_iospc;
144 lower_limit = &pciauto_lower_iospc;
147 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
148 PCI_BASE_ADDRESS_MEM_TYPE_64)
151 addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
152 upper_limit = &pciauto_upper_memspc;
153 lower_limit = &pciauto_lower_memspc;
158 /* Calculate requested size */
159 bar_size = ~(bar_response & addr_mask) + 1;
161 /* Allocate a base address */
162 bar_value = ((*lower_limit - 1) & ~(bar_size - 1)) + bar_size;
164 if ((bar_value + bar_size) > *upper_limit) {
165 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
166 if (io_resource_inuse->child) {
168 io_resource_inuse->child;
169 pciauto_lower_iospc =
170 io_resource_inuse->start;
171 pciauto_upper_iospc =
172 io_resource_inuse->end + 1;
177 if (mem_resource_inuse->child) {
179 mem_resource_inuse->child;
180 pciauto_lower_memspc =
181 mem_resource_inuse->start;
182 pciauto_upper_memspc =
183 mem_resource_inuse->end + 1;
187 DBG(" unavailable -- skipping\n");
191 /* Write it out and update our limit */
192 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
195 *lower_limit = bar_value + bar_size;
198 * If we are a 64-bit decoder then increment to the
199 * upper 32 bits of the bar and force it to locate
200 * in the lower 4GB of memory.
204 early_write_config_dword(hose, top_bus,
211 DBG(" at 0x%.8x [size=0x%x]\n", bar_value, bar_size);
219 pciauto_prescan_setup_bridge(struct pci_channel *hose,
225 /* Configure bus number registers */
226 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
227 PCI_PRIMARY_BUS, current_bus);
228 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
229 PCI_SECONDARY_BUS, sub_bus + 1);
230 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
231 PCI_SUBORDINATE_BUS, 0xff);
233 /* Align memory and I/O to 1MB and 4KB boundaries. */
234 pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
236 pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
239 /* Set base (lower limit) of address range behind bridge. */
240 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
241 PCI_MEMORY_BASE, pciauto_lower_memspc >> 16);
242 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
243 PCI_IO_BASE, (pciauto_lower_iospc & 0x0000f000) >> 8);
244 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
245 PCI_IO_BASE_UPPER16, pciauto_lower_iospc >> 16);
247 /* We don't support prefetchable memory for now, so disable */
248 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
249 PCI_PREF_MEMORY_BASE, 0);
250 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
251 PCI_PREF_MEMORY_LIMIT, 0);
255 pciauto_postscan_setup_bridge(struct pci_channel *hose,
264 * [jsun] we always bump up baselines a little, so that if there
265 * nothing behind P2P bridge, we don't wind up overlapping IO/MEM
268 pciauto_lower_memspc += 1;
269 pciauto_lower_iospc += 1;
271 /* Configure bus number registers */
272 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
273 PCI_SUBORDINATE_BUS, sub_bus);
275 /* Set upper limit of address range behind bridge. */
276 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
277 PCI_MEMORY_LIMIT, pciauto_lower_memspc >> 16);
278 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
279 PCI_IO_LIMIT, (pciauto_lower_iospc & 0x0000f000) >> 8);
280 early_write_config_word(hose, top_bus, current_bus, pci_devfn,
281 PCI_IO_LIMIT_UPPER16, pciauto_lower_iospc >> 16);
283 /* Align memory and I/O to 1MB and 4KB boundaries. */
284 pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
286 pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
289 /* Enable memory and I/O accesses, enable bus master */
290 early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
292 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
293 PCI_COMMAND, temp | PCI_COMMAND_IO | PCI_COMMAND_MEMORY
294 | PCI_COMMAND_MASTER);
298 pciauto_prescan_setup_cardbus_bridge(struct pci_channel *hose,
304 /* Configure bus number registers */
305 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
306 PCI_PRIMARY_BUS, current_bus);
307 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
308 PCI_SECONDARY_BUS, sub_bus + 1);
309 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
310 PCI_SUBORDINATE_BUS, 0xff);
312 /* Align memory and I/O to 4KB and 4 byte boundaries. */
313 pciauto_lower_memspc = (pciauto_lower_memspc + (0x1000 - 1))
315 pciauto_lower_iospc = (pciauto_lower_iospc + (0x4 - 1))
318 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
319 PCI_CB_MEMORY_BASE_0, pciauto_lower_memspc);
320 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
321 PCI_CB_IO_BASE_0, pciauto_lower_iospc);
325 pciauto_postscan_setup_cardbus_bridge(struct pci_channel *hose,
334 * Configure subordinate bus number. The PCI subsystem
335 * bus scan will renumber buses (reserving three additional
336 * for this PCI<->CardBus bridge for the case where a CardBus
337 * adapter contains a P2P or CB2CB bridge.
340 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
341 PCI_SUBORDINATE_BUS, sub_bus);
344 * Reserve an additional 4MB for mem space and 16KB for
345 * I/O space. This should cover any additional space
346 * requirement of unusual CardBus devices with
347 * additional bridges that can consume more address space.
349 * Although pcmcia-cs currently will reprogram bridge
350 * windows, the goal is to add an option to leave them
351 * alone and use the bridge window ranges as the regions
352 * that are searched for free resources upon hot-insertion
353 * of a device. This will allow a PCI<->CardBus bridge
354 * configured by this routine to happily live behind a
355 * P2P bridge in a system.
357 pciauto_lower_memspc += 0x00400000;
358 pciauto_lower_iospc += 0x00004000;
360 /* Align memory and I/O to 4KB and 4 byte boundaries. */
361 pciauto_lower_memspc = (pciauto_lower_memspc + (0x1000 - 1))
363 pciauto_lower_iospc = (pciauto_lower_iospc + (0x4 - 1))
365 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
366 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
367 PCI_CB_MEMORY_LIMIT_0, pciauto_lower_memspc - 1);
368 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
369 PCI_CB_IO_LIMIT_0, pciauto_lower_iospc - 1);
371 /* Enable memory and I/O accesses, enable bus master */
372 early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
374 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
375 PCI_COMMAND, temp | PCI_COMMAND_IO | PCI_COMMAND_MEMORY
376 | PCI_COMMAND_MASTER);
379 #define PCIAUTO_IDE_MODE_MASK 0x05
382 pciauto_bus_scan(struct pci_channel *hose, int top_bus, int current_bus)
385 u32 pci_devfn, pci_class, cmdstat, found_multi=0;
386 unsigned short vid, did;
387 unsigned char header_type;
389 int devfn_stop = 0xff;
391 sub_bus = current_bus;
393 if (hose->first_devfn)
394 devfn_start = hose->first_devfn;
395 if (hose->last_devfn)
396 devfn_stop = hose->last_devfn;
398 for (pci_devfn=devfn_start; pci_devfn<devfn_stop; pci_devfn++) {
400 if (PCI_FUNC(pci_devfn) && !found_multi)
403 early_read_config_word(hose, top_bus, current_bus, pci_devfn,
404 PCI_VENDOR_ID, &vid);
406 if (vid == 0xffff) continue;
408 early_read_config_byte(hose, top_bus, current_bus, pci_devfn,
409 PCI_HEADER_TYPE, &header_type);
411 if (!PCI_FUNC(pci_devfn))
412 found_multi = header_type & 0x80;
414 early_read_config_word(hose, top_bus, current_bus, pci_devfn,
415 PCI_DEVICE_ID, &did);
417 early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
418 PCI_CLASS_REVISION, &pci_class);
420 DBG("%.2x:%.2x.%x Class %.4x: %.4x:%.4x",
421 current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn),
422 pci_class >> 16, vid, did);
423 if (pci_class & 0xff)
424 DBG(" (rev %.2x)", pci_class & 0xff);
427 if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
428 DBG(" Bridge: primary=%.2x, secondary=%.2x\n",
429 current_bus, sub_bus + 1);
430 pciauto_setup_bars(hose, top_bus, current_bus,
431 pci_devfn, PCI_BASE_ADDRESS_1);
432 pciauto_prescan_setup_bridge(hose, top_bus, current_bus,
434 DBG("Scanning sub bus %.2x, I/O 0x%.8x, Mem 0x%.8x\n",
436 pciauto_lower_iospc, pciauto_lower_memspc);
437 sub_bus = pciauto_bus_scan(hose, top_bus, sub_bus+1);
438 DBG("Back to bus %.2x\n", current_bus);
439 pciauto_postscan_setup_bridge(hose, top_bus, current_bus,
442 } else if ((pci_class >> 16) == PCI_CLASS_BRIDGE_CARDBUS) {
443 DBG(" CARDBUS Bridge: primary=%.2x, secondary=%.2x\n",
444 current_bus, sub_bus + 1);
445 DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
446 /* Place CardBus Socket/ExCA registers */
447 pciauto_setup_bars(hose, top_bus, current_bus, pci_devfn, PCI_BASE_ADDRESS_0);
449 pciauto_prescan_setup_cardbus_bridge(hose, top_bus,
450 current_bus, pci_devfn, sub_bus);
452 DBG("Scanning sub bus %.2x, I/O 0x%.8x, Mem 0x%.8x\n",
454 pciauto_lower_iospc, pciauto_lower_memspc);
455 sub_bus = pciauto_bus_scan(hose, top_bus, sub_bus+1);
456 DBG("Back to bus %.2x, sub_bus is %x\n", current_bus, sub_bus);
457 pciauto_postscan_setup_cardbus_bridge(hose, top_bus,
458 current_bus, pci_devfn, sub_bus);
460 } else if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
462 unsigned char prg_iface;
464 early_read_config_byte(hose, top_bus, current_bus,
465 pci_devfn, PCI_CLASS_PROG, &prg_iface);
466 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
467 DBG("Skipping legacy mode IDE controller\n");
473 * Found a peripheral, enable some standard
476 early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
477 PCI_COMMAND, &cmdstat);
478 early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
479 PCI_COMMAND, cmdstat | PCI_COMMAND_IO |
482 early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
483 PCI_LATENCY_TIMER, 0x80);
485 /* Allocate PCI I/O and/or memory space */
486 pciauto_setup_bars(hose, top_bus, current_bus, pci_devfn, PCI_BASE_ADDRESS_5);
492 pciauto_assign_resources(int busno, struct pci_channel *hose)
494 /* setup resource limits */
495 io_resource_inuse = hose->io_resource;
496 mem_resource_inuse = hose->mem_resource;
498 pciauto_lower_iospc = io_resource_inuse->start;
499 pciauto_upper_iospc = io_resource_inuse->end + 1;
500 pciauto_lower_memspc = mem_resource_inuse->start;
501 pciauto_upper_memspc = mem_resource_inuse->end + 1;
502 DBG("Autoconfig PCI channel 0x%p\n", hose);
503 DBG("Scanning bus %.2x, I/O 0x%.8x:0x%.8x, Mem 0x%.8x:0x%.8x\n",
504 busno, pciauto_lower_iospc, pciauto_upper_iospc,
505 pciauto_lower_memspc, pciauto_upper_memspc);
507 return pciauto_bus_scan(hose, busno, busno);