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 static unsigned int pci_scan_bridge(struct device *bus, unsigned int max);
23 /** Given a device and register, read the size of the BAR for that register.
24 * @param dev Pointer to the device structure
25 * @param resource Pointer to the resource structure
26 * @param index Address of the pci configuration register
28 static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
30 uint32_t addr, size, base;
33 /* Initialize the resources to nothing */
40 resource->index = index;
42 pci_read_config_dword(dev, index, &addr);
43 if (addr == 0xffffffffUL)
46 /* FIXME: more consideration for 64-bit PCI devices,
47 * we currently detect their size but otherwise
48 * treat them as 32-bit resources
51 pci_write_config_dword(dev, index, ~0);
52 pci_read_config_dword(dev, index, &size);
54 /* get the minimum value the bar can be set to */
55 pci_write_config_dword(dev, index, 0);
56 pci_read_config_dword(dev, index, &base);
59 pci_write_config_dword(dev, index, addr);
62 * some broken hardware has read-only registers that do not
63 * really size correctly. You can tell this if addr == size
64 * Example: the acer m7229 has BARs 1-4 normally read-only.
65 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
66 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
67 * violation of the spec.
68 * We catch this case and ignore it by settting size and type to 0.
69 * This incidentally catches the common case where registers
70 * read back as 0 for both address and size.
72 if ((addr == size) && (addr == base)) {
75 "PCI: %02x:%02x.%01x register %02x(%08x), read-only ignoring it\n",
77 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
82 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
83 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
84 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
85 /* BUG! Top 16 bits can be zero (or not)
86 * So set them to 0xffff so they go away ...
88 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
89 resource->align = log2(resource->size);
90 resource->gran = resource->align;
91 resource->flags = IORESOURCE_IO;
92 resource->limit = 0xffff;
95 /* A Memory mapped base address */
96 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
97 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
98 resource->align = log2(resource->size);
99 resource->gran = resource->align;
100 resource->flags = IORESOURCE_MEM;
101 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
102 resource->flags |= IORESOURCE_PREFETCH;
104 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
105 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
107 resource->limit = 0xffffffffUL;
109 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
111 resource->limit = 0x000fffffUL;
113 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
114 unsigned long index_hi;
116 * For now just treat this as a 32bit limit
118 index_hi = index + 4;
119 resource->limit = 0xffffffffUL;
120 resource->flags |= IORESOURCE_PCI64;
121 pci_read_config_dword( dev, index_hi, &addr);
122 /* get the extended size */
123 pci_write_config_dword(dev, index_hi, 0xffffffffUL);
124 pci_read_config_dword( dev, index_hi, &size);
126 /* get the minimum value the bar can be set to */
127 pci_write_config_dword(dev, index_hi, 0);
128 pci_read_config_dword(dev, index_hi, &base);
131 pci_write_config_dword(dev, index_hi, addr);
133 if ((size == 0xffffffff) && (base == 0)) {
134 /* Clear the top half of the bar */
135 pci_write_config_dword(dev, index_hi, 0);
138 printk_err("PCI: %02x:%02x.%01x Unable to handle 64-bit address\n",
140 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
141 resource->flags = IORESOURCE_PCI64;
149 /* dev->size holds the flags... */
153 /** Read the base address registers for a given device.
154 * @param dev Pointer to the dev structure
155 * @param howmany How many registers to read (6 for device, 2 for bridge)
157 static void pci_read_bases(struct device *dev, unsigned int howmany)
162 reg = dev->resources;
163 for(index = PCI_BASE_ADDRESS_0;
164 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
165 struct resource *resource;
166 resource = &dev->resource[reg];
167 pci_get_resource(dev, resource, index);
168 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
169 index += (resource->flags & IORESOURCE_PCI64)?8:4;
171 dev->resources = reg;
175 static void pci_bridge_read_bases(struct device *dev)
177 unsigned int reg = dev->resources;
179 /* FIXME handle bridges without some of the optional resources */
181 /* Initialize the io space constraints on the current bus */
182 dev->resource[reg].base = 0;
183 dev->resource[reg].size = 0;
184 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
185 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
186 dev->resource[reg].limit = 0xffffUL;
187 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
188 dev->resource[reg].index = PCI_IO_BASE;
189 compute_allocate_resource(dev, &dev->resource[reg],
190 IORESOURCE_IO, IORESOURCE_IO);
193 /* Initiliaze the prefetchable memory constraints on the current bus */
194 dev->resource[reg].base = 0;
195 dev->resource[reg].size = 0;
196 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
197 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
198 dev->resource[reg].limit = 0xffffffffUL;
199 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
200 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
201 compute_allocate_resource(dev, &dev->resource[reg],
202 IORESOURCE_MEM | IORESOURCE_PREFETCH,
203 IORESOURCE_MEM | IORESOURCE_PREFETCH);
206 /* Initialize the memory resources on the current bus */
207 dev->resource[reg].base = 0;
208 dev->resource[reg].size = 0;
209 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
210 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
211 dev->resource[reg].limit = 0xffffffffUL;
212 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
213 dev->resource[reg].index = PCI_MEMORY_BASE;
214 compute_allocate_resource(dev, &dev->resource[reg],
215 IORESOURCE_MEM | IORESOURCE_PREFETCH,
219 dev->resources = reg;
223 void pci_dev_read_resources(struct device *dev)
227 memset(&dev->resource[0], 0, sizeof(dev->resource));
228 pci_read_bases(dev, 6);
229 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr);
230 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
233 void pci_bus_read_resources(struct device *dev)
237 memset(&dev->resource[0], 0, sizeof(dev->resource));
238 pci_bridge_read_bases(dev);
239 pci_read_bases(dev, 2);
241 pci_read_config_dword(dev, PCI_ROM_ADDRESS1, &addr);
242 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
247 static void pci_set_resource(struct device *dev, struct resource *resource)
249 unsigned long base, limit;
250 unsigned long bridge_align = PCI_MEM_BRIDGE_ALIGN;
251 unsigned char buf[10];
253 /* Make certain the resource has actually been set */
254 if (!(resource->flags & IORESOURCE_SET)) {
256 printk_err("ERROR: %02x:%02x.%01x %02x not allocated\n",
258 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
264 /* Only handle PCI memory and IO resources for now */
265 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
268 if (resource->flags & IORESOURCE_MEM) {
269 dev->command |= PCI_COMMAND_MEMORY;
270 bridge_align = PCI_MEM_BRIDGE_ALIGN;
272 if (resource->flags & IORESOURCE_IO) {
273 dev->command |= PCI_COMMAND_IO;
274 bridge_align = PCI_IO_BRIDGE_ALIGN;
276 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
277 dev->command |= PCI_COMMAND_MASTER;
279 /* Get the base address */
280 base = resource->base;
282 /* Get the limit (rounded up) */
283 limit = base + ((resource->size + bridge_align - 1UL) & ~(bridge_align -1)) -1UL;
285 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
287 * some chipsets allow us to set/clear the IO bit.
288 * (e.g. VIA 82c686a.) So set it to be safe)
290 limit = base + resource->size -1;
291 if (resource->flags & IORESOURCE_IO) {
292 base |= PCI_BASE_ADDRESS_SPACE_IO;
294 pci_write_config_dword(dev, resource->index, base & 0xffffffff);
295 if (resource->flags & IORESOURCE_PCI64) {
296 /* FIXME handle real 64bit base addresses */
297 pci_write_config_dword(dev, resource->index + 4, 0);
300 else if (resource->index == PCI_IO_BASE) {
302 * WARNING: we don't really do 32-bit addressing for IO yet!
304 compute_allocate_resource(dev, resource,
305 IORESOURCE_IO, IORESOURCE_IO);
306 pci_write_config_byte(dev, PCI_IO_BASE, base >> 8);
307 pci_write_config_byte(dev, PCI_IO_LIMIT, limit >> 8);
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_config_word(dev, PCI_MEMORY_BASE, base >> 16);
316 pci_write_config_word(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_config_word(dev, PCI_PREF_MEMORY_BASE, base >> 16);
326 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
329 printk_err("ERROR: invalid resource->index %x\n",
333 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
334 sprintf(buf, "bus %d ", dev->secondary);
338 "PCI: %02x:%02x.%01x %02x <- [0x%08lx - 0x%08lx] %s%s\n",
340 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
342 resource->base, limit,
344 (resource->flags & IORESOURCE_IO)? "io":
345 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
349 void pci_dev_set_resources(struct device *dev)
351 struct resource *resource, *last;
354 last = &dev->resource[dev->resources];
356 for(resource = &dev->resource[0]; resource < last; resource++) {
357 pci_set_resource(dev, resource);
360 assign_resources(dev);
363 /* set a default latency timer */
364 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x40);
366 /* set a default secondary latency timer */
367 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
368 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 0x40);
371 /* zero the irq settings */
372 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &line);
374 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0);
376 /* set the cache line size, so far 64 bytes is good for everyone */
377 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
380 struct device_operations default_pci_ops_dev = {
381 .read_resources = pci_dev_read_resources,
382 .set_resources = pci_dev_set_resources,
386 struct device_operations default_pci_ops_bus = {
387 .read_resources = pci_bus_read_resources,
388 .set_resources = pci_dev_set_resources,
390 .scan_bus = pci_scan_bridge,
392 static void set_pci_ops(struct device *dev)
394 struct pci_driver *driver;
398 /* Look through the list of setup drivers and find one for
401 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
402 if ((driver->vendor == dev->vendor) &&
403 (driver->device == dev->device)) {
404 dev->ops = driver->ops;
406 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] ops\n",
408 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
409 driver->vendor, driver->device
415 /* If I don't have a specific driver use the default operations */
416 switch(dev->hdr_type & 0x7f) { /* header type */
417 case PCI_HEADER_TYPE_NORMAL: /* standard header */
418 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
420 dev->ops = &default_pci_ops_dev;
422 case PCI_HEADER_TYPE_BRIDGE:
423 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
425 dev->ops = &default_pci_ops_bus;
429 printk_err("PCI: %02x:%02x.%01x [%04x/%04x/%06x] has unknown header "
430 "type %02x, ignoring.\n",
432 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
433 dev->vendor, dev->device,
434 dev->class >> 8, dev->hdr_type);
440 * Given a bus and a devfn number, find the device structure
441 * @param bus The bus structure
442 * @param devfn a device/function number
443 * @return pointer to the device structure
445 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
447 struct device *dev = 0;
448 for(; *list; list = &(*list)->sibling) {
449 if ((*list)->devfn == devfn) {
450 /* Unlink from the list */
452 *list = (*list)->sibling;
461 #define HYPERTRANSPORT_SUPPORT 1
462 /** Scan the pci bus devices and bridges.
463 * @param pci_bus pointer to the bus structure
464 * @param max current bus number
465 * @return The maximum bus number found, after scanning all subordinate busses
467 unsigned int pci_scan_bus(struct device *bus, unsigned int max)
470 struct device *dev, **bus_last;
471 struct device *old_devices;
472 struct device *child;
473 #if HYPERTRANSPORT_SUPPORT
474 unsigned next_unitid, last_unitid;
477 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
479 old_devices = bus->children;
481 bus_last = &bus->children;
486 #if HYPERTRANSPORT_SUPPORT
487 /* If present assign unitid to a hypertransport chain */
492 uint8_t hdr_type, pos;
493 last_unitid = next_unitid;
497 pci_read_config_dword(&dummy, PCI_VENDOR_ID, &id);
498 if (id == 0xffffffff || id == 0x00000000 ||
499 id == 0x0000ffff || id == 0xffff0000) {
502 pci_read_config_byte(&dummy, PCI_HEADER_TYPE, &hdr_type);
504 switch(hdr_type & 0x7f) {
505 case PCI_HEADER_TYPE_NORMAL:
506 case PCI_HEADER_TYPE_BRIDGE:
507 pos = PCI_CAPABILITY_LIST;
510 if (pos > PCI_CAP_LIST_NEXT) {
511 pci_read_config_byte(&dummy, pos, &pos);
515 pci_read_config_byte(&dummy, pos + PCI_CAP_LIST_ID, &cap);
516 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
517 if (cap == PCI_CAP_ID_HT) {
519 pci_read_config_word(&dummy, pos + PCI_CAP_FLAGS, &flags);
520 printk_debug("flags: 0x%04x\n", (unsigned)flags);
521 if ((flags >> 13) == 0) {
524 flags |= next_unitid & 0x1f;
525 count = (flags >> 5) & 0x1f;
526 printk_debug("unitid: 0x%02x, count: 0x%02x\n",
528 pci_write_config_word(&dummy, pos + PCI_CAP_FLAGS, flags);
529 next_unitid += count;
533 pci_read_config_byte(&dummy, pos + PCI_CAP_LIST_NEXT, &pos);
535 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
536 #endif /* HYPERTRANSPORT_SUPPORT */
538 /* probe all devices on this bus with some optimization for non-existance and
539 single funcion devices */
540 for (devfn = 0; devfn <= 0xff; devfn++) {
543 uint8_t cmd, tmp, hdr_type;
545 /* First thing setup the device structure */
546 dev = pci_scan_get_dev(&old_devices, devfn);
550 pci_read_config_dword(&dummy, PCI_VENDOR_ID, &id);
551 /* some broken boards return 0 if a slot is empty: */
553 (id == 0xffffffff || id == 0x00000000 ||
554 id == 0x0000ffff || id == 0xffff0000)) {
555 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
556 if (PCI_FUNC(devfn) == 0x00) {
557 /* if this is a function 0 device and it is not present,
558 skip to next device */
561 /* multi function device, skip to next function */
564 pci_read_config_byte(&dummy, PCI_HEADER_TYPE, &hdr_type);
565 pci_read_config_dword(&dummy, PCI_CLASS_REVISION, &class);
568 if ((dev = malloc(sizeof(*dev))) == 0) {
569 printk_err("PCI: out of memory.\n");
572 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 /* non-destructively determine if device can be a master: */
584 pci_read_config_byte(dev, PCI_COMMAND, &cmd);
585 pci_write_config_byte(dev, PCI_COMMAND, cmd | PCI_COMMAND_MASTER);
586 pci_read_config_byte(dev, PCI_COMMAND, &tmp);
588 dev->master = ((tmp & PCI_COMMAND_MASTER) != 0);
589 pci_write_config_byte(dev, PCI_COMMAND, cmd);
591 /* Look at the vendor and device id, or at least the
592 * header type and class and figure out which set of configuration
596 /* Kill the device if we don't have some pci operations for it */
601 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x]\n",
602 bus->secondary, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
603 dev->vendor, dev->device);
605 /* Put it into the global device chain. */
608 /* Now insert it into the list of devices held by the parent bus. */
610 bus_last = &dev->sibling;
612 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
613 /* if this is not a multi function device, don't waste time probe
614 another function. Skip to next device. */
620 for(child = bus->children; child; child = child->sibling) {
621 if (!child->ops->scan_bus)
623 max = child->ops->scan_bus(child, max);
627 * We've scanned the bus and so we know all about what's on
628 * the other side of any bridges that may be on this bus plus
631 * Return how far we've got finding sub-buses.
633 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
638 /** Scan the bus, first for bridges and next for devices.
639 * @param pci_bus pointer to the bus structure
640 * @return The maximum bus number found, after scanning all subordinate busses
642 unsigned int pci_scan_bridge(struct device *bus, unsigned int max)
646 /* Set up the primary, secondary and subordinate bus numbers. We have
647 * no idea how many buses are behind this bridge yet, so we set the
648 * subordinate bus number to 0xff for the moment
650 bus->secondary = ++max;
651 bus->subordinate = 0xff;
653 /* Clear all status bits and turn off memory, I/O and master enables. */
654 pci_read_config_word(bus, PCI_COMMAND, &cr);
655 pci_write_config_word(bus, PCI_COMMAND, 0x0000);
656 pci_write_config_word(bus, PCI_STATUS, 0xffff);
659 * Read the existing primary/secondary/subordinate bus
660 * number configuration.
662 pci_read_config_dword(bus, PCI_PRIMARY_BUS, &buses);
664 /* Configure the bus numbers for this bridge: the configuration
665 * transactions will not be propagated by the bridge if it is not
666 * correctly configured
669 buses |= (((unsigned int) (bus->bus->secondary) << 0) |
670 ((unsigned int) (bus->secondary) << 8) |
671 ((unsigned int) (bus->subordinate) << 16));
672 pci_write_config_dword(bus, PCI_PRIMARY_BUS, buses);
674 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
675 max = pci_scan_bus(bus, max);
677 /* We know the number of buses behind this bridge. Set the subordinate
678 * bus number to its real value
680 bus->subordinate = max;
681 buses = (buses & 0xff00ffff) |
682 ((unsigned int) (bus->subordinate) << 16);
683 pci_write_config_dword(bus, PCI_PRIMARY_BUS, buses);
684 pci_write_config_word(bus, PCI_COMMAND, cr);
690 static void pci_root_read_resources(struct device *bus)
693 /* Initialize the system wide io space constraints */
694 bus->resource[res].base = 0x400;
695 bus->resource[res].size = 0;
696 bus->resource[res].align = 0;
697 bus->resource[res].gran = 0;
698 bus->resource[res].limit = 0xffffUL;
699 bus->resource[res].flags = IORESOURCE_IO;
700 bus->resource[res].index = PCI_IO_BASE;
701 compute_allocate_resource(bus, &bus->resource[res],
702 IORESOURCE_IO, IORESOURCE_IO);
705 /* Initialize the system wide memory resources constraints */
706 bus->resource[res].base = 0;
707 bus->resource[res].size = 0;
708 bus->resource[res].align = 0;
709 bus->resource[res].gran = 0;
710 bus->resource[res].limit = 0xffffffffUL;
711 bus->resource[res].flags = IORESOURCE_MEM;
712 bus->resource[res].index = PCI_MEMORY_BASE;
713 compute_allocate_resource(bus, &bus->resource[res],
714 IORESOURCE_MEM, IORESOURCE_MEM);
717 bus->resources = res;
719 static void pci_root_set_resources(struct device *bus)
721 compute_allocate_resource(bus,
722 &bus->resource[0], IORESOURCE_IO, IORESOURCE_IO);
723 compute_allocate_resource(bus,
724 &bus->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
725 assign_resources(bus);
728 struct device_operations default_pci_ops_root = {
729 .read_resources = pci_root_read_resources,
730 .set_resources = pci_root_set_resources,
732 .scan_bus = pci_scan_bus,