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>
18 #include <device/device.h>
19 #include <device/pci.h>
20 #include <device/pci_ids.h>
21 #include <part/hard_reset.h>
22 #include <part/fallback_boot.h>
24 /** Given a device and register, read the size of the BAR for that register.
25 * @param dev Pointer to the device structure
26 * @param resource Pointer to the resource structure
27 * @param index Address of the pci configuration register
29 static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
31 uint32_t addr, size, base;
34 /* Initialize the resources to nothing */
41 resource->index = index;
43 addr = pci_read_config32(dev, index);
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 "%s register %02x(%08x), read-only ignoring it\n",
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("%s Unable to handle 64-bit address\n",
138 resource->flags = IORESOURCE_PCI64;
146 /* dev->size holds the flags... */
150 /** Read the base address registers for a given device.
151 * @param dev Pointer to the dev structure
152 * @param howmany How many registers to read (6 for device, 2 for bridge)
154 static void pci_read_bases(struct device *dev, unsigned int howmany)
159 reg = dev->resources;
160 for(index = PCI_BASE_ADDRESS_0;
161 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
162 struct resource *resource;
163 resource = &dev->resource[reg];
164 pci_get_resource(dev, resource, index);
165 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
166 index += (resource->flags & IORESOURCE_PCI64)?8:4;
168 dev->resources = reg;
172 static void pci_bridge_read_bases(struct device *dev)
174 unsigned int reg = dev->resources;
176 /* FIXME handle bridges without some of the optional resources */
178 printk_spew("%s: path %s\n", __FUNCTION__, dev_path(dev));
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->link[0], &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->link[0], &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->link[0], &dev->resource[reg],
213 IORESOURCE_MEM | IORESOURCE_PREFETCH,
217 dev->resources = reg;
218 printk_spew("DONE %s: path %s\n", __FUNCTION__, dev_path(dev));
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, 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 char buf[10];
252 /* Make certain the resource has actually been set */
253 if (!(resource->flags & IORESOURCE_SET)) {
255 printk_err("ERROR: %s %02x not allocated\n",
256 dev_path(dev), resource->index);
261 /* Only handle PCI memory and IO resources for now */
262 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
265 if (resource->flags & IORESOURCE_MEM) {
266 dev->command |= PCI_COMMAND_MEMORY;
268 if (resource->flags & IORESOURCE_IO) {
269 dev->command |= PCI_COMMAND_IO;
271 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
272 dev->command |= PCI_COMMAND_MASTER;
274 /* Get the base address */
275 base = resource->base;
276 /* Get the resource alignment */
277 align = 1UL << resource->align;
279 /* Get the limit (rounded up) */
280 limit = base + ((resource->size + align - 1UL) & ~(align - 1UL)) -1UL;
282 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
284 * some chipsets allow us to set/clear the IO bit.
285 * (e.g. VIA 82c686a.) So set it to be safe)
287 limit = base + resource->size -1;
288 if (resource->flags & IORESOURCE_IO) {
289 base |= PCI_BASE_ADDRESS_SPACE_IO;
291 pci_write_config32(dev, resource->index, base & 0xffffffff);
292 if (resource->flags & IORESOURCE_PCI64) {
293 /* FIXME handle real 64bit base addresses */
294 pci_write_config32(dev, resource->index + 4, 0);
297 else if (resource->index == PCI_IO_BASE) {
299 * WARNING: we don't really do 32-bit addressing for IO yet!
301 compute_allocate_resource(&dev->link[0], resource,
302 IORESOURCE_IO, IORESOURCE_IO);
303 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
304 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
305 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
306 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
308 else if (resource->index == PCI_MEMORY_BASE) {
309 /* set the memory range
311 compute_allocate_resource(&dev->link[0], resource,
312 IORESOURCE_MEM | IORESOURCE_PREFETCH,
314 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
315 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
317 else if (resource->index == PCI_PREF_MEMORY_BASE) {
318 /* set the prefetchable memory range
319 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
321 compute_allocate_resource(&dev->link[0], resource,
322 IORESOURCE_MEM | IORESOURCE_PREFETCH,
323 IORESOURCE_MEM | IORESOURCE_PREFETCH);
324 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
325 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
326 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
327 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
330 printk_err("ERROR: invalid resource->index %x\n",
334 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
335 sprintf(buf, "bus %d ", dev->link[0].secondary);
339 "%s %02x <- [0x%08lx - 0x%08lx] %s%s\n",
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;
355 last = &dev->resource[dev->resources];
357 for(resource = &dev->resource[0]; resource < last; resource++) {
358 pci_set_resource(dev, resource);
360 for(link = 0; link < dev->links; link++) {
362 bus = &dev->link[link];
364 assign_resources(bus);
368 /* set a default latency timer */
369 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
371 /* set a default secondary latency timer */
372 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
373 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
376 /* zero the irq settings */
377 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
379 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
381 /* set the cache line size, so far 64 bytes is good for everyone */
382 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
385 void pci_dev_enable_resources(struct device *dev)
388 command = pci_read_config16(dev, PCI_COMMAND);
389 command |= dev->command;
390 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
391 pci_write_config16(dev, PCI_COMMAND, command);
393 enable_childrens_resources(dev);
396 void pci_bus_enable_resources(struct device *dev)
399 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
400 ctrl |= dev->link[0].bridge_ctrl;
401 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
402 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
404 pci_dev_enable_resources(dev);
407 struct device_operations default_pci_ops_dev = {
408 .read_resources = pci_dev_read_resources,
409 .set_resources = pci_dev_set_resources,
410 .enable_resources = pci_dev_enable_resources,
414 struct device_operations default_pci_ops_bus = {
415 .read_resources = pci_bus_read_resources,
416 .set_resources = pci_dev_set_resources,
417 .enable_resources = pci_bus_enable_resources,
419 .scan_bus = pci_scan_bridge,
421 static void set_pci_ops(struct device *dev)
423 struct pci_driver *driver;
427 /* Look through the list of setup drivers and find one for
430 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
431 if ((driver->vendor == dev->vendor) &&
432 (driver->device == dev->device)) {
433 dev->ops = driver->ops;
435 printk_debug("%s [%04x/%04x] %sops\n",
437 driver->vendor, driver->device,
438 (driver->ops->scan_bus?"bus ":"")
444 /* If I don't have a specific driver use the default operations */
445 switch(dev->hdr_type & 0x7f) { /* header type */
446 case PCI_HEADER_TYPE_NORMAL: /* standard header */
447 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
449 dev->ops = &default_pci_ops_dev;
451 case PCI_HEADER_TYPE_BRIDGE:
452 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
454 dev->ops = &default_pci_ops_bus;
458 printk_err("%s [%04x/%04x/%06x] has unknown header "
459 "type %02x, ignoring.\n",
461 dev->vendor, dev->device,
462 dev->class >> 8, dev->hdr_type);
468 * Given a bus and a devfn number, find the device structure
469 * @param bus The bus structure
470 * @param devfn a device/function number
471 * @return pointer to the device structure
473 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
475 struct device *dev = 0;
476 for(; *list; list = &(*list)->sibling) {
477 if ((*list)->path.u.pci.devfn == devfn) {
478 /* Unlink from the list */
480 *list = (*list)->sibling;
487 /* Find the last child of our parent */
488 for(child = dev->bus->children; child && child->sibling; ) {
489 child = child->sibling;
491 /* Place the device on the list of children of it's parent. */
493 child->sibling = dev;
495 dev->bus->children = dev;
502 /** Scan the pci bus devices and bridges.
503 * @param bus pointer to the bus structure
504 * @param min_devfn minimum devfn to look at in the scan usually 0x00
505 * @param max_devfn maximum devfn to look at in the scan usually 0xff
506 * @param max current bus number
507 * @return The maximum bus number found, after scanning all subordinate busses
509 unsigned int pci_scan_bus(struct bus *bus,
510 unsigned min_devfn, unsigned max_devfn,
515 device_t old_devices;
518 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
520 old_devices = bus->children;
526 /* probe all devices on this bus with some optimization for non-existance and
527 single funcion devices */
528 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
532 /* First thing setup the device structure */
533 dev = pci_scan_get_dev(&old_devices, devfn);
535 /* Detect if a device is present */
539 dummy.path.type = DEVICE_PATH_PCI;
540 dummy.path.u.pci.devfn = devfn;
541 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
542 /* some broken boards return 0 if a slot is empty: */
543 if ( (id == 0xffffffff) || (id == 0x00000000) ||
544 (id == 0x0000ffff) || (id == 0xffff0000))
546 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
547 if (PCI_FUNC(devfn) == 0x00) {
548 /* if this is a function 0 device and it is not present,
549 skip to next device */
552 /* multi function device, skip to next function */
555 dev = alloc_dev(bus, &dummy.path);
558 /* Run the magic enable/disable sequence for the device */
559 if (dev->ops && dev->ops->enable) {
560 dev->ops->enable(dev);
562 /* Now read the vendor and device id */
563 id = pci_read_config32(dev, PCI_VENDOR_ID);
566 /* Read the rest of the pci configuration information */
567 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
568 class = pci_read_config32(dev, PCI_CLASS_REVISION);
570 /* Store the interesting information in the device structure */
571 dev->vendor = id & 0xffff;
572 dev->device = (id >> 16) & 0xffff;
573 dev->hdr_type = hdr_type;
574 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
575 dev->class = class >> 8;
577 /* Look at the vendor and device id, or at least the
578 * header type and class and figure out which set of configuration
583 /* Error if we don't have some pci operations for it */
585 printk_err("%s No device operations\n",
589 /* Now run the magic enable/disable sequence for the device */
590 if (dev->ops && dev->ops->enable) {
591 dev->ops->enable(dev);
595 printk_debug("%s [%04x/%04x] %s\n",
597 dev->vendor, dev->device,
598 dev->enable?"enabled": "disabled");
600 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
601 /* if this is not a multi function device, don't waste time probe
602 another function. Skip to next device. */
608 for(child = bus->children; child; child = child->sibling) {
609 if (!child->ops->scan_bus) {
612 max = child->ops->scan_bus(child, max);
615 * We've scanned the bus and so we know all about what's on
616 * the other side of any bridges that may be on this bus plus
619 * Return how far we've got finding sub-buses.
621 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
626 /** Scan the bus, first for bridges and next for devices.
627 * @param pci_bus pointer to the bus structure
628 * @return The maximum bus number found, after scanning all subordinate busses
630 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
636 printk_spew("%s: dev %p, max %d\n", __FUNCTION__, dev, max);
640 /* Set up the primary, secondary and subordinate bus numbers. We have
641 * no idea how many buses are behind this bridge yet, so we set the
642 * subordinate bus number to 0xff for the moment
644 bus->secondary = ++max;
645 bus->subordinate = 0xff;
647 /* Clear all status bits and turn off memory, I/O and master enables. */
648 cr = pci_read_config16(dev, PCI_COMMAND);
649 pci_write_config16(dev, PCI_COMMAND, 0x0000);
650 pci_write_config16(dev, PCI_STATUS, 0xffff);
653 * Read the existing primary/secondary/subordinate bus
654 * number configuration.
656 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
658 /* Configure the bus numbers for this bridge: the configuration
659 * transactions will not be propagated by the bridge if it is not
660 * correctly configured
663 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
664 ((unsigned int) (bus->secondary) << 8) |
665 ((unsigned int) (bus->subordinate) << 16));
666 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
668 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
669 max = pci_scan_bus(bus, 0x00, 0xff, max);
671 /* We know the number of buses behind this bridge. Set the subordinate
672 * bus number to its real value
674 bus->subordinate = max;
675 buses = (buses & 0xff00ffff) |
676 ((unsigned int) (bus->subordinate) << 16);
677 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
678 pci_write_config16(dev, PCI_COMMAND, cr);
680 printk_spew("%s returns max %d\n", __FUNCTION__, max);
684 Tell the EISA int controller this int must be level triggered
685 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
687 static void pci_level_irq(unsigned char intNum)
689 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
691 printk_spew("%s: current ints are 0x%x\n", __FUNCTION__, intBits);
692 intBits |= (1 << intNum);
694 printk_spew("%s: try to set ints 0x%x\n", __FUNCTION__, intBits);
697 outb((unsigned char) intBits, 0x4d0);
698 outb((unsigned char) (intBits >> 8), 0x4d1);
700 if (inb(0x4d0) != (intBits & 0xf)) {
701 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
702 __FUNCTION__, intBits &0xf, inb(0x4d0));
704 if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
705 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
706 __FUNCTION__, (intBits>>8) &0xf, inb(0x4d1));
712 This function assigns IRQs for all functions contained within
713 the indicated device address. If the device does not exist or does
714 not require interrupts then this function has no effect.
716 This function should be called for each PCI slot in your system.
718 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
720 The particular irq #s that are passed in depend on the routing inside
721 your southbridge and on your motherboard.
725 void pci_assign_irqs(unsigned bus, unsigned slot,
726 const unsigned char pIntAtoD[4])
732 unsigned char readback;
734 /* Each slot may contain up to eight functions */
735 for (functNum = 0; functNum < 8; functNum++) {
736 pdev = dev_find_slot(bus, (slot << 3) + functNum);
739 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
741 // PCI spec says all other values are reserved
742 if ((line >= 1) && (line <= 4)) {
743 irq = pIntAtoD[line - 1];
745 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
746 irq, bus, slot, functNum);
748 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
751 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
752 printk_debug(" Readback = %d\n", readback);
754 // Change to level triggered
755 pci_level_irq(pIntAtoD[line - 1]);