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>
21 /** Given a device and register, read the size of the BAR for that register.
22 * @param dev Pointer to the device structure
23 * @param resource Pointer to the resource structure
24 * @param index Address of the pci configuration register
26 static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
28 uint32_t addr, size, base;
31 /* Initialize the resources to nothing */
38 resource->index = index;
40 addr = pci_read_config32(dev, index);
41 if (addr == 0xffffffffUL)
44 /* FIXME: more consideration for 64-bit PCI devices,
45 * we currently detect their size but otherwise
46 * treat them as 32-bit resources
49 pci_write_config32(dev, index, ~0);
50 size = pci_read_config32(dev, index);
52 /* get the minimum value the bar can be set to */
53 pci_write_config32(dev, index, 0);
54 base = pci_read_config32(dev, index);
57 pci_write_config32(dev, index, addr);
60 * some broken hardware has read-only registers that do not
61 * really size correctly. You can tell this if addr == size
62 * Example: the acer m7229 has BARs 1-4 normally read-only.
63 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
64 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
65 * violation of the spec.
66 * We catch this case and ignore it by settting size and type to 0.
67 * This incidentally catches the common case where registers
68 * read back as 0 for both address and size.
70 if ((addr == size) && (addr == base)) {
73 "PCI: %02x:%02x.%01x register %02x(%08x), read-only ignoring it\n",
75 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
80 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
81 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
82 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
83 /* BUG! Top 16 bits can be zero (or not)
84 * So set them to 0xffff so they go away ...
86 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
87 resource->align = log2(resource->size);
88 resource->gran = resource->align;
89 resource->flags = IORESOURCE_IO;
90 resource->limit = 0xffff;
93 /* A Memory mapped base address */
94 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
95 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
96 resource->align = log2(resource->size);
97 resource->gran = resource->align;
98 resource->flags = IORESOURCE_MEM;
99 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
100 resource->flags |= IORESOURCE_PREFETCH;
102 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
103 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
105 resource->limit = 0xffffffffUL;
107 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
109 resource->limit = 0x000fffffUL;
111 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
112 unsigned long index_hi;
114 * For now just treat this as a 32bit limit
116 index_hi = index + 4;
117 resource->limit = 0xffffffffUL;
118 resource->flags |= IORESOURCE_PCI64;
119 addr = pci_read_config32( dev, index_hi);
120 /* get the extended size */
121 pci_write_config32(dev, index_hi, 0xffffffffUL);
122 size = pci_read_config32( dev, index_hi);
124 /* get the minimum value the bar can be set to */
125 pci_write_config32(dev, index_hi, 0);
126 base = pci_read_config32(dev, index_hi);
129 pci_write_config32(dev, index_hi, addr);
131 if ((size == 0xffffffff) && (base == 0)) {
132 /* Clear the top half of the bar */
133 pci_write_config32(dev, index_hi, 0);
136 printk_err("PCI: %02x:%02x.%01x Unable to handle 64-bit address\n",
138 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
139 resource->flags = IORESOURCE_PCI64;
147 /* dev->size holds the flags... */
151 /** Read the base address registers for a given device.
152 * @param dev Pointer to the dev structure
153 * @param howmany How many registers to read (6 for device, 2 for bridge)
155 static void pci_read_bases(struct device *dev, unsigned int howmany)
160 reg = dev->resources;
161 for(index = PCI_BASE_ADDRESS_0;
162 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
163 struct resource *resource;
164 resource = &dev->resource[reg];
165 pci_get_resource(dev, resource, index);
166 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
167 index += (resource->flags & IORESOURCE_PCI64)?8:4;
169 dev->resources = reg;
173 static void pci_bridge_read_bases(struct device *dev)
175 unsigned int reg = dev->resources;
177 /* FIXME handle bridges without some of the optional resources */
179 /* Initialize the io space constraints on the current bus */
180 dev->resource[reg].base = 0;
181 dev->resource[reg].size = 0;
182 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
183 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
184 dev->resource[reg].limit = 0xffffUL;
185 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
186 dev->resource[reg].index = PCI_IO_BASE;
187 compute_allocate_resource(dev, &dev->resource[reg],
188 IORESOURCE_IO, IORESOURCE_IO);
191 /* Initiliaze the prefetchable memory constraints on the current bus */
192 dev->resource[reg].base = 0;
193 dev->resource[reg].size = 0;
194 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
195 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
196 dev->resource[reg].limit = 0xffffffffUL;
197 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
198 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
199 compute_allocate_resource(dev, &dev->resource[reg],
200 IORESOURCE_MEM | IORESOURCE_PREFETCH,
201 IORESOURCE_MEM | IORESOURCE_PREFETCH);
204 /* Initialize the memory resources on the current bus */
205 dev->resource[reg].base = 0;
206 dev->resource[reg].size = 0;
207 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
208 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
209 dev->resource[reg].limit = 0xffffffffUL;
210 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
211 dev->resource[reg].index = PCI_MEMORY_BASE;
212 compute_allocate_resource(dev, &dev->resource[reg],
213 IORESOURCE_MEM | IORESOURCE_PREFETCH,
217 dev->resources = reg;
221 void pci_dev_read_resources(struct device *dev)
225 memset(&dev->resource[0], 0, sizeof(dev->resource));
226 pci_read_bases(dev, 6);
227 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
228 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
231 void pci_bus_read_resources(struct device *dev)
235 memset(&dev->resource[0], 0, sizeof(dev->resource));
236 pci_bridge_read_bases(dev);
237 pci_read_bases(dev, 2);
239 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
240 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
245 static void pci_set_resource(struct device *dev, struct resource *resource)
247 unsigned long base, limit;
248 unsigned long bridge_align = PCI_MEM_BRIDGE_ALIGN;
249 unsigned char buf[10];
251 /* Make certain the resource has actually been set */
252 if (!(resource->flags & IORESOURCE_SET)) {
254 printk_err("ERROR: %02x:%02x.%01x %02x not allocated\n",
256 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
262 /* Only handle PCI memory and IO resources for now */
263 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
266 if (resource->flags & IORESOURCE_MEM) {
267 dev->command |= PCI_COMMAND_MEMORY;
268 bridge_align = PCI_MEM_BRIDGE_ALIGN;
270 if (resource->flags & IORESOURCE_IO) {
271 dev->command |= PCI_COMMAND_IO;
272 bridge_align = PCI_IO_BRIDGE_ALIGN;
274 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
275 dev->command |= PCI_COMMAND_MASTER;
277 /* Get the base address */
278 base = resource->base;
280 /* Get the limit (rounded up) */
281 limit = base + ((resource->size + bridge_align - 1UL) & ~(bridge_align -1)) -1UL;
283 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
285 * some chipsets allow us to set/clear the IO bit.
286 * (e.g. VIA 82c686a.) So set it to be safe)
288 limit = base + resource->size -1;
289 if (resource->flags & IORESOURCE_IO) {
290 base |= PCI_BASE_ADDRESS_SPACE_IO;
292 pci_write_config32(dev, resource->index, base & 0xffffffff);
293 if (resource->flags & IORESOURCE_PCI64) {
294 /* FIXME handle real 64bit base addresses */
295 pci_write_config32(dev, resource->index + 4, 0);
298 else if (resource->index == PCI_IO_BASE) {
300 * WARNING: we don't really do 32-bit addressing for IO yet!
302 compute_allocate_resource(dev, resource,
303 IORESOURCE_IO, IORESOURCE_IO);
304 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
305 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
306 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
307 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
309 else if (resource->index == PCI_MEMORY_BASE) {
310 /* set the memory range
312 compute_allocate_resource(dev, resource,
313 IORESOURCE_MEM | IORESOURCE_PREFETCH,
315 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
316 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
318 else if (resource->index == PCI_PREF_MEMORY_BASE) {
319 /* set the prefetchable memory range
320 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
322 compute_allocate_resource(dev, resource,
323 IORESOURCE_MEM | IORESOURCE_PREFETCH,
324 IORESOURCE_MEM | IORESOURCE_PREFETCH);
325 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
326 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
327 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
328 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
331 printk_err("ERROR: invalid resource->index %x\n",
335 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
336 sprintf(buf, "bus %d ", dev->secondary);
340 "PCI: %02x:%02x.%01x %02x <- [0x%08lx - 0x%08lx] %s%s\n",
342 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
344 resource->base, limit,
346 (resource->flags & IORESOURCE_IO)? "io":
347 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
351 void pci_dev_set_resources(struct device *dev)
353 struct resource *resource, *last;
356 last = &dev->resource[dev->resources];
358 for(resource = &dev->resource[0]; resource < last; resource++) {
359 pci_set_resource(dev, resource);
362 assign_resources(dev);
365 /* set a default latency timer */
366 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
368 /* set a default secondary latency timer */
369 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
370 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
373 /* zero the irq settings */
374 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
376 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
378 /* set the cache line size, so far 64 bytes is good for everyone */
379 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
382 struct device_operations default_pci_ops_dev = {
383 .read_resources = pci_dev_read_resources,
384 .set_resources = pci_dev_set_resources,
388 struct device_operations default_pci_ops_bus = {
389 .read_resources = pci_bus_read_resources,
390 .set_resources = pci_dev_set_resources,
392 .scan_bus = pci_scan_bridge,
394 static void set_pci_ops(struct device *dev)
396 struct pci_driver *driver;
400 /* Look through the list of setup drivers and find one for
403 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
404 if ((driver->vendor == dev->vendor) &&
405 (driver->device == dev->device)) {
406 dev->ops = driver->ops;
408 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] ops\n",
410 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
411 driver->vendor, driver->device
417 /* If I don't have a specific driver use the default operations */
418 switch(dev->hdr_type & 0x7f) { /* header type */
419 case PCI_HEADER_TYPE_NORMAL: /* standard header */
420 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
422 dev->ops = &default_pci_ops_dev;
424 case PCI_HEADER_TYPE_BRIDGE:
425 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
427 dev->ops = &default_pci_ops_bus;
431 printk_err("PCI: %02x:%02x.%01x [%04x/%04x/%06x] has unknown header "
432 "type %02x, ignoring.\n",
434 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
435 dev->vendor, dev->device,
436 dev->class >> 8, dev->hdr_type);
442 * Given a bus and a devfn number, find the device structure
443 * @param bus The bus structure
444 * @param devfn a device/function number
445 * @return pointer to the device structure
447 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
449 struct device *dev = 0;
450 for(; *list; list = &(*list)->sibling) {
451 if ((*list)->devfn == devfn) {
452 /* Unlink from the list */
454 *list = (*list)->sibling;
463 #define HYPERTRANSPORT_SUPPORT 1
464 /** Scan the pci bus devices and bridges.
465 * @param pci_bus pointer to the bus structure
466 * @param max current bus number
467 * @return The maximum bus number found, after scanning all subordinate busses
469 unsigned int pci_scan_bus(struct device *bus, unsigned int max)
472 struct device *dev, **bus_last;
473 struct device *old_devices;
474 struct device *child;
475 #if HYPERTRANSPORT_SUPPORT
476 unsigned next_unitid, last_unitid;
479 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
481 old_devices = bus->children;
483 bus_last = &bus->children;
488 #if HYPERTRANSPORT_SUPPORT
489 /* If present assign unitid to a hypertransport chain */
494 uint8_t hdr_type, pos;
495 last_unitid = next_unitid;
499 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
500 if (id == 0xffffffff || id == 0x00000000 ||
501 id == 0x0000ffff || id == 0xffff0000) {
504 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
506 switch(hdr_type & 0x7f) {
507 case PCI_HEADER_TYPE_NORMAL:
508 case PCI_HEADER_TYPE_BRIDGE:
509 pos = PCI_CAPABILITY_LIST;
512 if (pos > PCI_CAP_LIST_NEXT) {
513 pos = pci_read_config8(&dummy, pos);
517 cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID);
518 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
519 if (cap == PCI_CAP_ID_HT) {
521 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
522 printk_debug("flags: 0x%04x\n", (unsigned)flags);
523 if ((flags >> 13) == 0) {
526 flags |= next_unitid & 0x1f;
527 count = (flags >> 5) & 0x1f;
528 printk_debug("unitid: 0x%02x, count: 0x%02x\n",
530 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
531 next_unitid += count;
535 pos = pci_read_config8(&dummy, pos + PCI_CAP_LIST_NEXT);
537 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
538 #endif /* HYPERTRANSPORT_SUPPORT */
540 /* probe all devices on this bus with some optimization for non-existance and
541 single funcion devices */
542 for (devfn = 0; devfn <= 0xff; devfn++) {
545 uint8_t cmd, tmp, hdr_type;
547 /* First thing setup the device structure */
548 dev = pci_scan_get_dev(&old_devices, devfn);
552 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
553 /* some broken boards return 0 if a slot is empty: */
555 (id == 0xffffffff || id == 0x00000000 ||
556 id == 0x0000ffff || id == 0xffff0000)) {
557 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
558 if (PCI_FUNC(devfn) == 0x00) {
559 /* if this is a function 0 device and it is not present,
560 skip to next device */
563 /* multi function device, skip to next function */
566 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
567 class = pci_read_config32(&dummy, PCI_CLASS_REVISION);
570 if ((dev = malloc(sizeof(*dev))) == 0) {
571 printk_err("PCI: out of memory.\n");
574 memset(dev, 0, sizeof(*dev));
577 dev->vendor = id & 0xffff;
578 dev->device = (id >> 16) & 0xffff;
579 dev->hdr_type = hdr_type;
580 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
581 dev->class = class >> 8;
583 /* If we don't have prior information about this device enable it */
588 /* Look at the vendor and device id, or at least the
589 * header type and class and figure out which set of configuration
593 /* Kill the device if we don't have some pci operations for it */
599 /* Now run the magic enable/disable sequence for the device */
600 if (dev->ops && dev->ops->enable) {
601 dev->ops->enable(dev);
604 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] %s\n",
605 bus->secondary, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
606 dev->vendor, dev->device,
607 dev->enable?"enabled": "disabled");
609 /* Put it into the global device chain. */
612 /* Now insert it into the list of devices held by the parent bus. */
614 bus_last = &dev->sibling;
616 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
617 /* if this is not a multi function device, don't waste time probe
618 another function. Skip to next device. */
624 for(child = bus->children; child; child = child->sibling) {
625 if (!child->ops->scan_bus)
627 max = child->ops->scan_bus(child, max);
631 * We've scanned the bus and so we know all about what's on
632 * the other side of any bridges that may be on this bus plus
635 * Return how far we've got finding sub-buses.
637 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
642 /** Scan the bus, first for bridges and next for devices.
643 * @param pci_bus pointer to the bus structure
644 * @return The maximum bus number found, after scanning all subordinate busses
646 unsigned int pci_scan_bridge(struct device *bus, unsigned int max)
650 /* Set up the primary, secondary and subordinate bus numbers. We have
651 * no idea how many buses are behind this bridge yet, so we set the
652 * subordinate bus number to 0xff for the moment
654 bus->secondary = ++max;
655 bus->subordinate = 0xff;
657 /* Clear all status bits and turn off memory, I/O and master enables. */
658 cr = pci_read_config16(bus, PCI_COMMAND);
659 pci_write_config16(bus, PCI_COMMAND, 0x0000);
660 pci_write_config16(bus, PCI_STATUS, 0xffff);
663 * Read the existing primary/secondary/subordinate bus
664 * number configuration.
666 buses = pci_read_config32(bus, PCI_PRIMARY_BUS);
668 /* Configure the bus numbers for this bridge: the configuration
669 * transactions will not be propagated by the bridge if it is not
670 * correctly configured
673 buses |= (((unsigned int) (bus->bus->secondary) << 0) |
674 ((unsigned int) (bus->secondary) << 8) |
675 ((unsigned int) (bus->subordinate) << 16));
676 pci_write_config32(bus, PCI_PRIMARY_BUS, buses);
678 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
679 max = pci_scan_bus(bus, max);
681 /* We know the number of buses behind this bridge. Set the subordinate
682 * bus number to its real value
684 bus->subordinate = max;
685 buses = (buses & 0xff00ffff) |
686 ((unsigned int) (bus->subordinate) << 16);
687 pci_write_config32(bus, PCI_PRIMARY_BUS, buses);
688 pci_write_config16(bus, PCI_COMMAND, cr);
694 static void pci_root_read_resources(struct device *bus)
697 /* Initialize the system wide io space constraints */
698 bus->resource[res].base = 0x400;
699 bus->resource[res].size = 0;
700 bus->resource[res].align = 0;
701 bus->resource[res].gran = 0;
702 bus->resource[res].limit = 0xffffUL;
703 bus->resource[res].flags = IORESOURCE_IO;
704 bus->resource[res].index = PCI_IO_BASE;
705 compute_allocate_resource(bus, &bus->resource[res],
706 IORESOURCE_IO, IORESOURCE_IO);
709 /* Initialize the system wide memory resources constraints */
710 bus->resource[res].base = 0;
711 bus->resource[res].size = 0;
712 bus->resource[res].align = 0;
713 bus->resource[res].gran = 0;
714 bus->resource[res].limit = 0xffffffffUL;
715 bus->resource[res].flags = IORESOURCE_MEM;
716 bus->resource[res].index = PCI_MEMORY_BASE;
717 compute_allocate_resource(bus, &bus->resource[res],
718 IORESOURCE_MEM, IORESOURCE_MEM);
721 bus->resources = res;
723 static void pci_root_set_resources(struct device *bus)
725 compute_allocate_resource(bus,
726 &bus->resource[0], IORESOURCE_IO, IORESOURCE_IO);
727 compute_allocate_resource(bus,
728 &bus->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
729 assign_resources(bus);
732 struct device_operations default_pci_ops_root = {
733 .read_resources = pci_root_read_resources,
734 .set_resources = pci_root_set_resources,
736 .scan_bus = pci_scan_bus,