2 * PCI Bus Services, see include/linux/pci.h for further explanation.
4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
7 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
9 * Copyright 2003 -- Eric Biederman <ebiederman@lnxi.com>
12 #include <console/console.h>
17 #include <device/device.h>
18 #include <device/pci.h>
19 #include <device/pci_ids.h>
20 #include <part/fallback_boot.h>
22 /** Given a device and register, read the size of the BAR for that register.
23 * @param dev Pointer to the device structure
24 * @param resource Pointer to the resource structure
25 * @param index Address of the pci configuration register
27 static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
29 uint32_t addr, size, base;
32 /* Initialize the resources to nothing */
39 resource->index = index;
41 addr = pci_read_config32(dev, index);
42 if (addr == 0xffffffffUL)
45 /* FIXME: more consideration for 64-bit PCI devices,
46 * we currently detect their size but otherwise
47 * treat them as 32-bit resources
50 pci_write_config32(dev, index, ~0);
51 size = pci_read_config32(dev, index);
53 /* get the minimum value the bar can be set to */
54 pci_write_config32(dev, index, 0);
55 base = pci_read_config32(dev, index);
58 pci_write_config32(dev, index, addr);
61 * some broken hardware has read-only registers that do not
62 * really size correctly. You can tell this if addr == size
63 * Example: the acer m7229 has BARs 1-4 normally read-only.
64 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
65 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
66 * violation of the spec.
67 * We catch this case and ignore it by settting size and type to 0.
68 * This incidentally catches the common case where registers
69 * read back as 0 for both address and size.
71 if ((addr == size) && (addr == base)) {
74 "PCI: %02x:%02x.%01x register %02x(%08x), read-only ignoring it\n",
76 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
81 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
82 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
83 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
84 /* BUG! Top 16 bits can be zero (or not)
85 * So set them to 0xffff so they go away ...
87 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
88 resource->align = log2(resource->size);
89 resource->gran = resource->align;
90 resource->flags = IORESOURCE_IO;
91 resource->limit = 0xffff;
94 /* A Memory mapped base address */
95 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
96 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
97 resource->align = log2(resource->size);
98 resource->gran = resource->align;
99 resource->flags = IORESOURCE_MEM;
100 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
101 resource->flags |= IORESOURCE_PREFETCH;
103 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
104 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
106 resource->limit = 0xffffffffUL;
108 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
110 resource->limit = 0x000fffffUL;
112 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
113 unsigned long index_hi;
115 * For now just treat this as a 32bit limit
117 index_hi = index + 4;
118 resource->limit = 0xffffffffUL;
119 resource->flags |= IORESOURCE_PCI64;
120 addr = pci_read_config32( dev, index_hi);
121 /* get the extended size */
122 pci_write_config32(dev, index_hi, 0xffffffffUL);
123 size = pci_read_config32( dev, index_hi);
125 /* get the minimum value the bar can be set to */
126 pci_write_config32(dev, index_hi, 0);
127 base = pci_read_config32(dev, index_hi);
130 pci_write_config32(dev, index_hi, addr);
132 if ((size == 0xffffffff) && (base == 0)) {
133 /* Clear the top half of the bar */
134 pci_write_config32(dev, index_hi, 0);
137 printk_err("PCI: %02x:%02x.%01x Unable to handle 64-bit address\n",
139 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
140 resource->flags = IORESOURCE_PCI64;
148 /* dev->size holds the flags... */
152 /** Read the base address registers for a given device.
153 * @param dev Pointer to the dev structure
154 * @param howmany How many registers to read (6 for device, 2 for bridge)
156 static void pci_read_bases(struct device *dev, unsigned int howmany)
161 reg = dev->resources;
162 for(index = PCI_BASE_ADDRESS_0;
163 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
164 struct resource *resource;
165 resource = &dev->resource[reg];
166 pci_get_resource(dev, resource, index);
167 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
168 index += (resource->flags & IORESOURCE_PCI64)?8:4;
170 dev->resources = reg;
174 static void pci_bridge_read_bases(struct device *dev)
176 unsigned int reg = dev->resources;
178 /* FIXME handle bridges without some of the optional resources */
180 /* Initialize the io space constraints on the current bus */
181 dev->resource[reg].base = 0;
182 dev->resource[reg].size = 0;
183 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
184 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
185 dev->resource[reg].limit = 0xffffUL;
186 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
187 dev->resource[reg].index = PCI_IO_BASE;
188 compute_allocate_resource(dev, &dev->resource[reg],
189 IORESOURCE_IO, IORESOURCE_IO);
192 /* Initiliaze the prefetchable memory constraints on the current bus */
193 dev->resource[reg].base = 0;
194 dev->resource[reg].size = 0;
195 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
196 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
197 dev->resource[reg].limit = 0xffffffffUL;
198 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
199 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
200 compute_allocate_resource(dev, &dev->resource[reg],
201 IORESOURCE_MEM | IORESOURCE_PREFETCH,
202 IORESOURCE_MEM | IORESOURCE_PREFETCH);
205 /* Initialize the memory resources on the current bus */
206 dev->resource[reg].base = 0;
207 dev->resource[reg].size = 0;
208 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
209 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
210 dev->resource[reg].limit = 0xffffffffUL;
211 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
212 dev->resource[reg].index = PCI_MEMORY_BASE;
213 compute_allocate_resource(dev, &dev->resource[reg],
214 IORESOURCE_MEM | IORESOURCE_PREFETCH,
218 dev->resources = reg;
222 void pci_dev_read_resources(struct device *dev)
226 memset(&dev->resource[0], 0, sizeof(dev->resource));
227 pci_read_bases(dev, 6);
228 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
229 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
232 void pci_bus_read_resources(struct device *dev)
236 memset(&dev->resource[0], 0, sizeof(dev->resource));
237 pci_bridge_read_bases(dev);
238 pci_read_bases(dev, 2);
240 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
241 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
246 static void pci_set_resource(struct device *dev, struct resource *resource)
248 unsigned long base, limit;
249 unsigned long bridge_align = PCI_MEM_BRIDGE_ALIGN;
250 unsigned char buf[10];
252 /* Make certain the resource has actually been set */
253 if (!(resource->flags & IORESOURCE_SET)) {
255 printk_err("ERROR: %02x:%02x.%01x %02x not allocated\n",
257 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
263 /* Only handle PCI memory and IO resources for now */
264 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
267 if (resource->flags & IORESOURCE_MEM) {
268 dev->command |= PCI_COMMAND_MEMORY;
269 bridge_align = PCI_MEM_BRIDGE_ALIGN;
271 if (resource->flags & IORESOURCE_IO) {
272 dev->command |= PCI_COMMAND_IO;
273 bridge_align = PCI_IO_BRIDGE_ALIGN;
275 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
276 dev->command |= PCI_COMMAND_MASTER;
278 /* Get the base address */
279 base = resource->base;
281 /* Get the limit (rounded up) */
282 limit = base + ((resource->size + bridge_align - 1UL) & ~(bridge_align -1)) -1UL;
284 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
286 * some chipsets allow us to set/clear the IO bit.
287 * (e.g. VIA 82c686a.) So set it to be safe)
289 limit = base + resource->size -1;
290 if (resource->flags & IORESOURCE_IO) {
291 base |= PCI_BASE_ADDRESS_SPACE_IO;
293 pci_write_config32(dev, resource->index, base & 0xffffffff);
294 if (resource->flags & IORESOURCE_PCI64) {
295 /* FIXME handle real 64bit base addresses */
296 pci_write_config32(dev, resource->index + 4, 0);
299 else if (resource->index == PCI_IO_BASE) {
301 * WARNING: we don't really do 32-bit addressing for IO yet!
303 compute_allocate_resource(dev, resource,
304 IORESOURCE_IO, IORESOURCE_IO);
305 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
306 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
307 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
308 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
310 else if (resource->index == PCI_MEMORY_BASE) {
311 /* set the memory range
313 compute_allocate_resource(dev, resource,
314 IORESOURCE_MEM | IORESOURCE_PREFETCH,
316 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
317 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
319 else if (resource->index == PCI_PREF_MEMORY_BASE) {
320 /* set the prefetchable memory range
321 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
323 compute_allocate_resource(dev, resource,
324 IORESOURCE_MEM | IORESOURCE_PREFETCH,
325 IORESOURCE_MEM | IORESOURCE_PREFETCH);
326 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
327 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
328 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
329 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
332 printk_err("ERROR: invalid resource->index %x\n",
336 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
337 sprintf(buf, "bus %d ", dev->secondary);
341 "PCI: %02x:%02x.%01x %02x <- [0x%08lx - 0x%08lx] %s%s\n",
343 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
345 resource->base, limit,
347 (resource->flags & IORESOURCE_IO)? "io":
348 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
352 void pci_dev_set_resources(struct device *dev)
354 struct resource *resource, *last;
357 last = &dev->resource[dev->resources];
359 for(resource = &dev->resource[0]; resource < last; resource++) {
360 pci_set_resource(dev, resource);
363 assign_resources(dev);
366 /* set a default latency timer */
367 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
369 /* set a default secondary latency timer */
370 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
371 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
374 /* zero the irq settings */
375 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
377 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
379 /* set the cache line size, so far 64 bytes is good for everyone */
380 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
383 struct device_operations default_pci_ops_dev = {
384 .read_resources = pci_dev_read_resources,
385 .set_resources = pci_dev_set_resources,
389 struct device_operations default_pci_ops_bus = {
390 .read_resources = pci_bus_read_resources,
391 .set_resources = pci_dev_set_resources,
393 .scan_bus = pci_scan_bridge,
395 static void set_pci_ops(struct device *dev)
397 struct pci_driver *driver;
401 /* Look through the list of setup drivers and find one for
404 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
405 if ((driver->vendor == dev->vendor) &&
406 (driver->device == dev->device)) {
407 dev->ops = driver->ops;
409 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] ops\n",
411 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
412 driver->vendor, driver->device
418 /* If I don't have a specific driver use the default operations */
419 switch(dev->hdr_type & 0x7f) { /* header type */
420 case PCI_HEADER_TYPE_NORMAL: /* standard header */
421 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
423 dev->ops = &default_pci_ops_dev;
425 case PCI_HEADER_TYPE_BRIDGE:
426 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
428 dev->ops = &default_pci_ops_bus;
432 printk_err("PCI: %02x:%02x.%01x [%04x/%04x/%06x] has unknown header "
433 "type %02x, ignoring.\n",
435 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
436 dev->vendor, dev->device,
437 dev->class >> 8, dev->hdr_type);
443 * Given a bus and a devfn number, find the device structure
444 * @param bus The bus structure
445 * @param devfn a device/function number
446 * @return pointer to the device structure
448 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
450 struct device *dev = 0;
451 for(; *list; list = &(*list)->sibling) {
452 if ((*list)->devfn == devfn) {
453 /* Unlink from the list */
455 *list = (*list)->sibling;
463 void assign_id_set_links(device_t dev, uint8_t *pos,
464 uint8_t *previous_pos, unsigned previous_unitid,
465 unsigned last_unitid, int *reset_needed,
466 struct device *bus, unsigned *next_unitid)
468 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
469 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
471 struct device last, prev_bus, previous;
473 uint8_t present_width_cap;
474 uint16_t present_freq_cap;
475 uint8_t upstream_width_cap;
476 uint16_t upstream_freq_cap;
477 uint8_t ln_upstream_width_in, ln_present_width_in;
478 uint8_t ln_upstream_width_out, ln_present_width_out;
482 uint8_t upstream_width, present_width;
485 flags = pci_read_config16(dev, (*pos) + PCI_CAP_FLAGS);
486 printk_debug("flags: 0x%04x\n", (unsigned)flags);
487 if ((flags >> 13) != 0)
488 return; /* Entry is a Host */
489 /* Entry is a Slave secondary */
490 flags &= ~0x1f; /* mask out base unit ID */
491 flags |= *next_unitid & 0x1f; /* assign ID */
492 count = (flags >> 5) & 0x1f; /* get unit count */
493 printk_debug("unitid: 0x%02x, count: 0x%02x\n",
494 *next_unitid, count);
495 pci_write_config16(dev, (*pos) + PCI_CAP_FLAGS, flags);
496 *next_unitid += count;
497 if (previous_unitid == 0) { /* the link is back to the host */
498 prev_bus.secondary = 0;
499 /* calculate the previous pos for the host */
500 *previous_pos = 0x80;
501 previous.bus = &prev_bus;
502 previous.devfn = 0x18 << 3;
503 #warning "FIXME we should not hard code this!"
506 previous.devfn = previous_unitid << 3;
509 last.devfn = last_unitid << 3;
510 /* Set link width and frequency */
511 present_freq_cap = pci_read_config16(&last,
512 (*pos) + PCI_HT_CAP_SLAVE_FREQ_CAP0);
513 present_width_cap = pci_read_config8(&last,
514 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0);
515 if(previous_unitid == 0) { /* the link is back to the host */
516 upstream_freq_cap = pci_read_config16(&previous,
517 (*previous_pos) + PCI_HT_CAP_HOST_FREQ_CAP);
518 upstream_width_cap = pci_read_config8(&previous,
519 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH);
521 else { /* The link is back up the chain */
522 upstream_freq_cap = pci_read_config16(&previous,
523 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ_CAP1);
524 upstream_width_cap = pci_read_config8(&previous,
525 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1);
527 /* Calculate the highest possible frequency */
528 /* Errata for 8131 - freq 5 has hardware problems don't support it */
529 freq = log2(present_freq_cap & upstream_freq_cap & 0x1f);
531 /* Calculate the highest width */
532 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
533 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
534 if (ln_upstream_width_in > ln_present_width_out) {
535 ln_upstream_width_in = ln_present_width_out;
537 upstream_width = pow2_to_link_width[ln_upstream_width_in];
538 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
540 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
541 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
542 if (ln_upstream_width_out > ln_present_width_in) {
543 ln_upstream_width_out = ln_present_width_in;
545 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
546 present_width |= pow2_to_link_width[ln_upstream_width_out];
548 /* set the present device */
549 old_freq = pci_read_config8(&last, (*pos) + PCI_HT_CAP_SLAVE_FREQ0);
550 if(old_freq != freq) {
551 pci_write_config8(&last,
552 (*pos) + PCI_HT_CAP_SLAVE_FREQ0, freq);
554 printk_debug("HyperT FreqP old %x new %x\n",old_freq,freq);
556 old_width = pci_read_config8(&last,
557 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0 + 1);
558 if(present_width != old_width) {
559 pci_write_config8(&last,
560 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0 + 1, present_width);
562 printk_debug("HyperT widthP old %x new %x\n",
563 old_width, present_width);
565 /* set the upstream device */
566 if(previous_unitid == 0) { /* the link is back to the host */
567 old_freq = pci_read_config8(&previous,
568 (*previous_pos) + PCI_HT_CAP_HOST_FREQ);
570 if(freq != old_freq) {
571 pci_write_config8(&previous,
572 (*previous_pos) + PCI_HT_CAP_HOST_FREQ, freq);
574 printk_debug("HyperT freqUH old %x new %x\n",
577 old_width = pci_read_config8(&previous,
578 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH + 1);
579 if(upstream_width != old_width) {
580 pci_write_config8(&previous,
581 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH + 1,
584 printk_debug("HyperT widthUH old %x new %x\n",
585 old_width, upstream_width);
588 else { /* The link is back up the chain */
589 old_freq = pci_read_config8(&previous,
590 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ1);
592 if(freq != old_freq) {
593 pci_write_config8(&previous,
594 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ1,
597 printk_debug("HyperT freqUL old %x new %x\n",
600 old_width = pci_read_config8(&previous,
601 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1 + 1);
602 if(upstream_width != old_width) {
603 pci_write_config8(&previous,
604 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1,
607 printk_debug("HyperT widthUL old %x new %x\n",
608 old_width, upstream_width);
611 *previous_pos = *pos;
615 #define HYPERTRANSPORT_SUPPORT 1
616 /** Scan the pci bus devices and bridges.
617 * @param pci_bus pointer to the bus structure
618 * @param max current bus number
619 * @return The maximum bus number found, after scanning all subordinate busses
621 unsigned int pci_scan_bus(struct device *bus, unsigned int max)
624 struct device *dev, **bus_last;
625 struct device *old_devices;
626 struct device *child;
627 #if HYPERTRANSPORT_SUPPORT
628 unsigned next_unitid, last_unitid, previous_unitid;
629 int reset_needed = 0;
630 uint8_t previous_pos;
633 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
635 old_devices = bus->children;
637 bus_last = &bus->children;
642 #if HYPERTRANSPORT_SUPPORT
643 /* Spin through the devices and collapse any early
644 * hypertransport enumeration.
646 for(devfn = 0; devfn <= 0xff; devfn += 8) {
649 uint8_t hdr_type, pos;
652 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
653 if (id == 0xffffffff || id == 0x00000000 ||
654 id == 0x0000ffff || id == 0xffff0000) {
657 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
659 switch(hdr_type & 0x7f) {
660 case PCI_HEADER_TYPE_NORMAL:
661 case PCI_HEADER_TYPE_BRIDGE:
662 pos = PCI_CAPABILITY_LIST;
665 if (pos > PCI_CAP_LIST_NEXT) {
666 pos = pci_read_config8(&dummy, pos);
670 cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID);
671 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
672 if (cap == PCI_CAP_ID_HT) {
674 flags = pci_read_config16(&dummy,
675 pos + PCI_CAP_FLAGS);
676 printk_debug("flags: 0x%04x\n",
678 if ((flags >> 13) == 0) {
679 /* Clear the unitid */
681 pci_write_config16(&dummy,
682 pos + PCI_CAP_FLAGS, flags);
686 pos = pci_read_config8(&dummy, pos + PCI_CAP_LIST_NEXT);
689 /* If present assign unitid to a hypertransport chain */
696 uint8_t hdr_type, pos;
698 previous_unitid = last_unitid;
699 last_unitid = next_unitid;
701 /* Read the next unassigned device off the stack */
704 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
705 /* If the chain is enumerated quit */
706 if (id == 0xffffffff || id == 0x00000000 ||
707 id == 0x0000ffff || id == 0xffff0000) {
710 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
712 switch(hdr_type & 0x7f) {
713 case PCI_HEADER_TYPE_NORMAL:
714 case PCI_HEADER_TYPE_BRIDGE:
715 pos = PCI_CAPABILITY_LIST;
718 if (pos > PCI_CAP_LIST_NEXT) {
719 pos = pci_read_config8(&dummy, pos);
721 while(pos != 0) { /* loop through the linked list */
723 cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID);
724 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
725 if (cap == PCI_CAP_ID_HT) {
726 assign_id_set_links(&dummy,&pos,&previous_pos,
727 previous_unitid, last_unitid,
732 pos = pci_read_config8(&dummy,
733 pos + PCI_CAP_LIST_NEXT);
735 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
736 #if HAVE_HARD_RESET == 1
738 printk_debug("HyperT reset needed\n");
742 printk_debug("HyperT reset not needed\n");
743 #endif /* HAVE_HARD_RESET */
744 #endif /* HYPERTRANSPORT_SUPPORT */
746 /* probe all devices on this bus with some optimization for non-existance and
747 single funcion devices */
748 for (devfn = 0; devfn <= 0xff; devfn++) {
753 /* First thing setup the device structure */
754 dev = pci_scan_get_dev(&old_devices, devfn);
758 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
759 /* some broken boards return 0 if a slot is empty: */
761 (id == 0xffffffff || id == 0x00000000 ||
762 id == 0x0000ffff || id == 0xffff0000)) {
763 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
764 if (PCI_FUNC(devfn) == 0x00) {
765 /* if this is a function 0 device and it is not present,
766 skip to next device */
769 /* multi function device, skip to next function */
772 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
773 class = pci_read_config32(&dummy, PCI_CLASS_REVISION);
776 if ((dev = malloc(sizeof(*dev))) == 0) {
777 printk_err("PCI: out of memory.\n");
780 memset(dev, 0, sizeof(*dev));
783 dev->vendor = id & 0xffff;
784 dev->device = (id >> 16) & 0xffff;
785 dev->hdr_type = hdr_type;
786 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
787 dev->class = class >> 8;
789 /* If we don't have prior information about this device enable it */
793 /* Look at the vendor and device id, or at least the
794 * header type and class and figure out which set of configuration
798 /* Kill the device if we don't have some pci operations for it */
804 /* Now run the magic enable/disable sequence for the device */
805 if (dev->ops && dev->ops->enable) {
806 dev->ops->enable(dev);
809 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] %s\n",
810 bus->secondary, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
811 dev->vendor, dev->device,
812 dev->enable?"enabled": "disabled");
814 /* Put it into the global device chain. */
817 /* Now insert it into the list of devices held by the parent bus. */
819 bus_last = &dev->sibling;
821 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
822 /* if this is not a multi function device, don't waste time probe
823 another function. Skip to next device. */
829 for(child = bus->children; child; child = child->sibling) {
830 if (!child->ops->scan_bus)
832 max = child->ops->scan_bus(child, max);
836 * We've scanned the bus and so we know all about what's on
837 * the other side of any bridges that may be on this bus plus
840 * Return how far we've got finding sub-buses.
842 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
847 /** Scan the bus, first for bridges and next for devices.
848 * @param pci_bus pointer to the bus structure
849 * @return The maximum bus number found, after scanning all subordinate busses
851 unsigned int pci_scan_bridge(struct device *bus, unsigned int max)
855 /* Set up the primary, secondary and subordinate bus numbers. We have
856 * no idea how many buses are behind this bridge yet, so we set the
857 * subordinate bus number to 0xff for the moment
859 bus->secondary = ++max;
860 bus->subordinate = 0xff;
862 /* Clear all status bits and turn off memory, I/O and master enables. */
863 cr = pci_read_config16(bus, PCI_COMMAND);
864 pci_write_config16(bus, PCI_COMMAND, 0x0000);
865 pci_write_config16(bus, PCI_STATUS, 0xffff);
868 * Read the existing primary/secondary/subordinate bus
869 * number configuration.
871 buses = pci_read_config32(bus, PCI_PRIMARY_BUS);
873 /* Configure the bus numbers for this bridge: the configuration
874 * transactions will not be propagated by the bridge if it is not
875 * correctly configured
878 buses |= (((unsigned int) (bus->bus->secondary) << 0) |
879 ((unsigned int) (bus->secondary) << 8) |
880 ((unsigned int) (bus->subordinate) << 16));
881 pci_write_config32(bus, PCI_PRIMARY_BUS, buses);
883 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
884 max = pci_scan_bus(bus, max);
886 /* We know the number of buses behind this bridge. Set the subordinate
887 * bus number to its real value
889 bus->subordinate = max;
890 buses = (buses & 0xff00ffff) |
891 ((unsigned int) (bus->subordinate) << 16);
892 pci_write_config32(bus, PCI_PRIMARY_BUS, buses);
893 pci_write_config16(bus, PCI_COMMAND, cr);
899 static void pci_root_read_resources(struct device *bus)
902 /* Initialize the system wide io space constraints */
903 bus->resource[res].base = 0x400;
904 bus->resource[res].size = 0;
905 bus->resource[res].align = 0;
906 bus->resource[res].gran = 0;
907 bus->resource[res].limit = 0xffffUL;
908 bus->resource[res].flags = IORESOURCE_IO;
909 bus->resource[res].index = PCI_IO_BASE;
910 compute_allocate_resource(bus, &bus->resource[res],
911 IORESOURCE_IO, IORESOURCE_IO);
914 /* Initialize the system wide memory resources constraints */
915 bus->resource[res].base = 0;
916 bus->resource[res].size = 0;
917 bus->resource[res].align = 0;
918 bus->resource[res].gran = 0;
919 bus->resource[res].limit = 0xffffffffUL;
920 bus->resource[res].flags = IORESOURCE_MEM;
921 bus->resource[res].index = PCI_MEMORY_BASE;
922 compute_allocate_resource(bus, &bus->resource[res],
923 IORESOURCE_MEM, IORESOURCE_MEM);
926 bus->resources = res;
928 static void pci_root_set_resources(struct device *bus)
930 compute_allocate_resource(bus,
931 &bus->resource[0], IORESOURCE_IO, IORESOURCE_IO);
932 compute_allocate_resource(bus,
933 &bus->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
934 assign_resources(bus);
937 struct device_operations default_pci_ops_root = {
938 .read_resources = pci_root_read_resources,
939 .set_resources = pci_root_set_resources,
941 .scan_bus = pci_scan_bus,