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/hard_reset.h>
21 #include <part/fallback_boot.h>
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 addr = pci_read_config32(dev, index);
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 "%s register %02x(%08x), read-only ignoring it\n",
79 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
80 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
81 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
82 /* BUG! Top 16 bits can be zero (or not)
83 * So set them to 0xffff so they go away ...
85 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
86 resource->align = log2(resource->size);
87 resource->gran = resource->align;
88 resource->flags = IORESOURCE_IO;
89 resource->limit = 0xffff;
92 /* A Memory mapped base address */
93 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
94 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
95 resource->align = log2(resource->size);
96 resource->gran = resource->align;
97 resource->flags = IORESOURCE_MEM;
98 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
99 resource->flags |= IORESOURCE_PREFETCH;
101 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
102 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
104 resource->limit = 0xffffffffUL;
106 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
108 resource->limit = 0x000fffffUL;
110 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
111 unsigned long index_hi;
113 * For now just treat this as a 32bit limit
115 index_hi = index + 4;
116 resource->limit = 0xffffffffUL;
117 resource->flags |= IORESOURCE_PCI64;
118 addr = pci_read_config32( dev, index_hi);
119 /* get the extended size */
120 pci_write_config32(dev, index_hi, 0xffffffffUL);
121 size = pci_read_config32( dev, index_hi);
123 /* get the minimum value the bar can be set to */
124 pci_write_config32(dev, index_hi, 0);
125 base = pci_read_config32(dev, index_hi);
128 pci_write_config32(dev, index_hi, addr);
130 if ((size == 0xffffffff) && (base == 0)) {
131 /* Clear the top half of the bar */
132 pci_write_config32(dev, index_hi, 0);
135 printk_err("%s Unable to handle 64-bit address\n",
137 resource->flags = IORESOURCE_PCI64;
145 /* dev->size holds the flags... */
149 /** Read the base address registers for a given device.
150 * @param dev Pointer to the dev structure
151 * @param howmany How many registers to read (6 for device, 2 for bridge)
153 static void pci_read_bases(struct device *dev, unsigned int howmany)
158 reg = dev->resources;
159 for(index = PCI_BASE_ADDRESS_0;
160 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
161 struct resource *resource;
162 resource = &dev->resource[reg];
163 pci_get_resource(dev, resource, index);
164 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
165 index += (resource->flags & IORESOURCE_PCI64)?8:4;
167 dev->resources = reg;
171 static void pci_bridge_read_bases(struct device *dev)
173 unsigned int reg = dev->resources;
175 /* FIXME handle bridges without some of the optional resources */
177 /* Initialize the io space constraints on the current bus */
178 dev->resource[reg].base = 0;
179 dev->resource[reg].size = 0;
180 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
181 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
182 dev->resource[reg].limit = 0xffffUL;
183 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
184 dev->resource[reg].index = PCI_IO_BASE;
185 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
186 IORESOURCE_IO, IORESOURCE_IO);
189 /* Initiliaze the prefetchable memory constraints on the current bus */
190 dev->resource[reg].base = 0;
191 dev->resource[reg].size = 0;
192 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
193 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
194 dev->resource[reg].limit = 0xffffffffUL;
195 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
196 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
197 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
198 IORESOURCE_MEM | IORESOURCE_PREFETCH,
199 IORESOURCE_MEM | IORESOURCE_PREFETCH);
202 /* Initialize the memory resources on the current bus */
203 dev->resource[reg].base = 0;
204 dev->resource[reg].size = 0;
205 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
206 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
207 dev->resource[reg].limit = 0xffffffffUL;
208 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
209 dev->resource[reg].index = PCI_MEMORY_BASE;
210 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
211 IORESOURCE_MEM | IORESOURCE_PREFETCH,
215 dev->resources = reg;
219 void pci_dev_read_resources(struct device *dev)
223 memset(&dev->resource[0], 0, sizeof(dev->resource));
224 pci_read_bases(dev, 6);
225 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
226 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
229 void pci_bus_read_resources(struct device *dev)
233 memset(&dev->resource, 0, sizeof(dev->resource));
234 pci_bridge_read_bases(dev);
235 pci_read_bases(dev, 2);
237 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
238 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
243 static void pci_set_resource(struct device *dev, struct resource *resource)
245 unsigned long base, limit;
246 unsigned char buf[10];
249 /* Make certain the resource has actually been set */
250 if (!(resource->flags & IORESOURCE_SET)) {
252 printk_err("ERROR: %s %02x not allocated\n",
253 dev_path(dev), resource->index);
258 /* Only handle PCI memory and IO resources for now */
259 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
262 if (resource->flags & IORESOURCE_MEM) {
263 dev->command |= PCI_COMMAND_MEMORY;
265 if (resource->flags & IORESOURCE_IO) {
266 dev->command |= PCI_COMMAND_IO;
268 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
269 dev->command |= PCI_COMMAND_MASTER;
271 /* Get the base address */
272 base = resource->base;
273 /* Get the resource alignment */
274 align = 1UL << resource->align;
276 /* Get the limit (rounded up) */
277 limit = base + ((resource->size + align - 1UL) & ~(align - 1UL)) -1UL;
279 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
281 * some chipsets allow us to set/clear the IO bit.
282 * (e.g. VIA 82c686a.) So set it to be safe)
284 limit = base + resource->size -1;
285 if (resource->flags & IORESOURCE_IO) {
286 base |= PCI_BASE_ADDRESS_SPACE_IO;
288 pci_write_config32(dev, resource->index, base & 0xffffffff);
289 if (resource->flags & IORESOURCE_PCI64) {
290 /* FIXME handle real 64bit base addresses */
291 pci_write_config32(dev, resource->index + 4, 0);
294 else if (resource->index == PCI_IO_BASE) {
296 * WARNING: we don't really do 32-bit addressing for IO yet!
298 compute_allocate_resource(&dev->link[0], resource,
299 IORESOURCE_IO, IORESOURCE_IO);
300 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
301 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
302 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
303 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
305 else if (resource->index == PCI_MEMORY_BASE) {
306 /* set the memory range
308 compute_allocate_resource(&dev->link[0], resource,
309 IORESOURCE_MEM | IORESOURCE_PREFETCH,
311 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
312 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
314 else if (resource->index == PCI_PREF_MEMORY_BASE) {
315 /* set the prefetchable memory range
316 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
318 compute_allocate_resource(&dev->link[0], resource,
319 IORESOURCE_MEM | IORESOURCE_PREFETCH,
320 IORESOURCE_MEM | IORESOURCE_PREFETCH);
321 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
322 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
323 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
324 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
327 printk_err("ERROR: invalid resource->index %x\n",
331 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
332 sprintf(buf, "bus %d ", dev->link[0].secondary);
336 "%s %02x <- [0x%08lx - 0x%08lx] %s%s\n",
339 resource->base, limit,
341 (resource->flags & IORESOURCE_IO)? "io":
342 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
346 void pci_dev_set_resources(struct device *dev)
348 struct resource *resource, *last;
352 last = &dev->resource[dev->resources];
354 for(resource = &dev->resource[0]; resource < last; resource++) {
355 pci_set_resource(dev, resource);
357 for(link = 0; link < dev->links; link++) {
359 bus = &dev->link[link];
361 assign_resources(bus);
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 void pci_dev_enable_resources(struct device *dev)
385 command = pci_read_config16(dev, PCI_COMMAND);
386 command |= dev->command;
387 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
388 pci_write_config16(dev, PCI_COMMAND, command);
390 enable_childrens_resources(dev);
393 void pci_bus_enable_resources(struct device *dev)
396 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
397 ctrl |= dev->link[0].bridge_ctrl;
398 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
399 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
401 pci_dev_enable_resources(dev);
404 struct device_operations default_pci_ops_dev = {
405 .read_resources = pci_dev_read_resources,
406 .set_resources = pci_dev_set_resources,
407 .enable_resources = pci_dev_enable_resources,
411 struct device_operations default_pci_ops_bus = {
412 .read_resources = pci_bus_read_resources,
413 .set_resources = pci_dev_set_resources,
414 .enable_resources = pci_bus_enable_resources,
416 .scan_bus = pci_scan_bridge,
418 static void set_pci_ops(struct device *dev)
420 struct pci_driver *driver;
424 /* Look through the list of setup drivers and find one for
427 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
428 if ((driver->vendor == dev->vendor) &&
429 (driver->device == dev->device)) {
430 dev->ops = driver->ops;
432 printk_debug("%s [%04x/%04x] %sops\n",
434 driver->vendor, driver->device,
435 (driver->ops->scan_bus?"bus ":"")
441 /* If I don't have a specific driver use the default operations */
442 switch(dev->hdr_type & 0x7f) { /* header type */
443 case PCI_HEADER_TYPE_NORMAL: /* standard header */
444 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
446 dev->ops = &default_pci_ops_dev;
448 case PCI_HEADER_TYPE_BRIDGE:
449 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
451 dev->ops = &default_pci_ops_bus;
455 printk_err("%s [%04x/%04x/%06x] has unknown header "
456 "type %02x, ignoring.\n",
458 dev->vendor, dev->device,
459 dev->class >> 8, dev->hdr_type);
465 * Given a bus and a devfn number, find the device structure
466 * @param bus The bus structure
467 * @param devfn a device/function number
468 * @return pointer to the device structure
470 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
472 struct device *dev = 0;
473 for(; *list; list = &(*list)->sibling) {
474 if ((*list)->path.u.pci.devfn == devfn) {
475 /* Unlink from the list */
477 *list = (*list)->sibling;
484 /* Find the last child of our parent */
485 for(child = dev->bus->children; child && child->sibling; ) {
486 child = child->sibling;
488 /* Place the device on the list of children of it's parent. */
490 child->sibling = dev;
492 dev->bus->children = dev;
499 /** Scan the pci bus devices and bridges.
500 * @param bus pointer to the bus structure
501 * @param min_devfn minimum devfn to look at in the scan usually 0x00
502 * @param max_devfn maximum devfn to look at in the scan usually 0xff
503 * @param max current bus number
504 * @return The maximum bus number found, after scanning all subordinate busses
506 unsigned int pci_scan_bus(struct bus *bus,
507 unsigned min_devfn, unsigned max_devfn,
512 device_t old_devices;
515 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
517 old_devices = bus->children;
523 /* probe all devices on this bus with some optimization for non-existance and
524 single funcion devices */
525 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
529 /* First thing setup the device structure */
530 dev = pci_scan_get_dev(&old_devices, devfn);
532 /* Detect if a device is present */
536 dummy.path.type = DEVICE_PATH_PCI;
537 dummy.path.u.pci.devfn = devfn;
538 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
539 /* some broken boards return 0 if a slot is empty: */
540 if ( (id == 0xffffffff) || (id == 0x00000000) ||
541 (id == 0x0000ffff) || (id == 0xffff0000))
543 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
544 if (PCI_FUNC(devfn) == 0x00) {
545 /* if this is a function 0 device and it is not present,
546 skip to next device */
549 /* multi function device, skip to next function */
552 dev = alloc_dev(bus, &dummy.path);
555 /* Run the magic enable/disable sequence for the device */
556 if (dev->ops && dev->ops->enable) {
557 dev->ops->enable(dev);
559 /* Now read the vendor and device id */
560 id = pci_read_config32(dev, PCI_VENDOR_ID);
563 /* Read the rest of the pci configuration information */
564 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
565 class = pci_read_config32(dev, PCI_CLASS_REVISION);
567 /* Store the interesting information in the device structure */
568 dev->vendor = id & 0xffff;
569 dev->device = (id >> 16) & 0xffff;
570 dev->hdr_type = hdr_type;
571 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
572 dev->class = class >> 8;
574 /* Look at the vendor and device id, or at least the
575 * header type and class and figure out which set of configuration
580 /* Error if we don't have some pci operations for it */
582 printk_err("%s No device operations\n",
586 /* Now run the magic enable/disable sequence for the device */
587 if (dev->ops && dev->ops->enable) {
588 dev->ops->enable(dev);
592 printk_debug("%s [%04x/%04x] %s\n",
594 dev->vendor, dev->device,
595 dev->enable?"enabled": "disabled");
597 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
598 /* if this is not a multi function device, don't waste time probe
599 another function. Skip to next device. */
605 for(child = bus->children; child; child = child->sibling) {
606 if (!child->ops->scan_bus) {
609 max = child->ops->scan_bus(child, max);
612 * We've scanned the bus and so we know all about what's on
613 * the other side of any bridges that may be on this bus plus
616 * Return how far we've got finding sub-buses.
618 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
623 /** Scan the bus, first for bridges and next for devices.
624 * @param pci_bus pointer to the bus structure
625 * @return The maximum bus number found, after scanning all subordinate busses
627 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
636 /* Set up the primary, secondary and subordinate bus numbers. We have
637 * no idea how many buses are behind this bridge yet, so we set the
638 * subordinate bus number to 0xff for the moment
640 bus->secondary = ++max;
641 bus->subordinate = 0xff;
643 /* Clear all status bits and turn off memory, I/O and master enables. */
644 cr = pci_read_config16(dev, PCI_COMMAND);
645 pci_write_config16(dev, PCI_COMMAND, 0x0000);
646 pci_write_config16(dev, PCI_STATUS, 0xffff);
649 * Read the existing primary/secondary/subordinate bus
650 * number configuration.
652 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
654 /* Configure the bus numbers for this bridge: the configuration
655 * transactions will not be propagated by the bridge if it is not
656 * correctly configured
659 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
660 ((unsigned int) (bus->secondary) << 8) |
661 ((unsigned int) (bus->subordinate) << 16));
662 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
664 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
665 max = pci_scan_bus(bus, 0x00, 0xff, max);
667 /* We know the number of buses behind this bridge. Set the subordinate
668 * bus number to its real value
670 bus->subordinate = max;
671 buses = (buses & 0xff00ffff) |
672 ((unsigned int) (bus->subordinate) << 16);
673 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
674 pci_write_config16(dev, PCI_COMMAND, cr);