coreboot-v3/device/pci_device.c
<<
>>
Prefs
   1/*
   2 * This file is part of the coreboot project.
   3 *
   4 * It was originally based on the Linux kernel (drivers/pci/pci.c).
   5 *
   6 * Modifications are:
   7 * Copyright (C) 2003-2004 Linux Networx
   8 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
   9 * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
  10 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
  11 * Copyright (C) 2005-2006 Tyan
  12 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
  13 * Copyright (C) 2005-2007 Stefan Reinauer <stepan@openbios.org>
  14 * Copyright (C) 2008 Myles Watson <mylesgw@gmail.com>
  15 */
  16
  17/*
  18 * PCI Bus Services, see include/linux/pci.h for further explanation.
  19 *
  20 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  21 * David Mosberger-Tang
  22 *
  23 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  24 */
  25
  26#include <types.h>
  27#include <io.h>
  28#include <string.h>
  29#include <lib.h>
  30#include <console.h>
  31#include <device/device.h>
  32#include <device/pci.h>
  33#include <device/pci_ids.h>
  34
  35#ifndef CONFIG_NO_PCIX_SUPPORT
  36#include <device/pcix.h>
  37#endif
  38#ifndef CONFIG_NO_AGP_SUPPORT
  39#include <device/agp.h>
  40#endif
  41#ifdef CONFIG_HYPERTRANSPORT_SUPPORT
  42#include <device/hypertransport.h>
  43#endif
  44#ifndef CONFIG_NO_PCIE_SUPPORT
  45#include <device/pcie.h>
  46#endif
  47#ifndef CONFIG_NO_CARDBUS_SUPPORT
  48#include <device/cardbus.h>
  49#endif
  50
  51#include <statictree.h>
  52
  53u8 pci_moving_config8(struct device *dev, unsigned int reg)
  54{
  55        u8 value, ones, zeroes;
  56        value = pci_read_config8(dev, reg);
  57
  58        pci_write_config8(dev, reg, 0xff);
  59        ones = pci_read_config8(dev, reg);
  60
  61        pci_write_config8(dev, reg, 0x00);
  62        zeroes = pci_read_config8(dev, reg);
  63
  64        pci_write_config8(dev, reg, value);
  65
  66        return ones ^ zeroes;
  67}
  68
  69u16 pci_moving_config16(struct device *dev, unsigned int reg)
  70{
  71        u16 value, ones, zeroes;
  72        value = pci_read_config16(dev, reg);
  73
  74        pci_write_config16(dev, reg, 0xffff);
  75        ones = pci_read_config16(dev, reg);
  76
  77        pci_write_config16(dev, reg, 0x0000);
  78        zeroes = pci_read_config16(dev, reg);
  79
  80        pci_write_config16(dev, reg, value);
  81
  82        return ones ^ zeroes;
  83}
  84
  85u32 pci_moving_config32(struct device *dev, unsigned int reg)
  86{
  87        u32 value, ones, zeroes;
  88        value = pci_read_config32(dev, reg);
  89
  90        pci_write_config32(dev, reg, 0xffffffff);
  91        ones = pci_read_config32(dev, reg);
  92
  93        pci_write_config32(dev, reg, 0x00000000);
  94        zeroes = pci_read_config32(dev, reg);
  95
  96        pci_write_config32(dev, reg, value);
  97
  98        return ones ^ zeroes;
  99}
 100
 101/**
 102 * Given a device, a capability type, and a last position, return the next
 103 * matching capability. Always start at the head of the list.
 104 *
 105 * @param dev Pointer to the device structure.
 106 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
 107 * @param last_pos Location of the PCI capability register to start from.
 108 */
 109unsigned int pci_find_next_capability(struct device *dev, unsigned int cap_type,
 110                                      unsigned int last_pos)
 111{
 112        unsigned int pos;
 113        unsigned int status;
 114        unsigned int reps = 48;
 115        pos = 0;
 116        status = pci_read_config16(dev, PCI_STATUS);
 117        if (!(status & PCI_STATUS_CAP_LIST)) {
 118                return 0;
 119        }
 120        switch (dev->hdr_type & 0x7f) {
 121        case PCI_HEADER_TYPE_NORMAL:
 122        case PCI_HEADER_TYPE_BRIDGE:
 123                pos = PCI_CAPABILITY_LIST;
 124                break;
 125        case PCI_HEADER_TYPE_CARDBUS:
 126                pos = PCI_CB_CAPABILITY_LIST;
 127                break;
 128        default:
 129                return 0;
 130        }
 131        pos = pci_read_config8(dev, pos);
 132        while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
 133                int this_cap_type;
 134                pos &= ~3;
 135                this_cap_type = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
 136                printk(BIOS_SPEW, "Capability: 0x%02x @ 0x%02x\n",
 137                        this_cap_type, pos);
 138                if (this_cap_type == 0xff) {
 139                        break;
 140                }
 141                if (!last_pos && (this_cap_type == cap_type)) {
 142                        return pos;
 143                }
 144                if (last_pos == pos) { /* Return the next match. */
 145                        last_pos = 0;
 146                }
 147                pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
 148        }
 149        return 0;
 150}
 151
 152/**
 153 * Given a device, and a capability type, return the next matching
 154 * capability. Always start at the head of the list.
 155 *
 156 * @param dev Pointer to the device structure.
 157 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
 158 */
 159unsigned int pci_find_capability(struct device *dev, unsigned int cap_type)
 160{
 161        return pci_find_next_capability(dev, cap_type, 0);
 162}
 163
 164/**
 165 * Given a device and register, read the size of the BAR for that register. 
 166 *
 167 * @param dev Pointer to the device structure.
 168 * @param index Address of the PCI configuration register.
 169 */
 170struct resource *pci_get_resource(struct device *dev, unsigned long index)
 171{
 172        struct resource *resource;
 173        unsigned long value, attr;
 174        resource_t moving, limit;
 175
 176        /* Initialize the resources to nothing. */
 177        resource = new_resource(dev, index);
 178
 179        /* Get the initial value. */
 180        value = pci_read_config32(dev, index);
 181
 182        /* See which bits move. */
 183        moving = pci_moving_config32(dev, index);
 184
 185        /* Initialize attr to the bits that do not move. */
 186        attr = value & ~moving;
 187
 188        /* If it is a 64bit resource look at the high half as well. */
 189        if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
 190            ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
 191             PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
 192                /* Find the high bits that move. */
 193                moving |=
 194                    ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
 195                resource->flags |= IORESOURCE_PCI64;
 196        }
 197
 198        /* Find the resource constraints.
 199         * Start by finding the bits that move. From there:
 200         * - Size is the least significant bit of the bits that move.
 201         * - Limit is all of the bits that move plus all of the lower bits.
 202         * See PCI Spec 6.2.5.1.
 203         */
 204        limit = 0;
 205        if (moving) {
 206                resource->size = 1;
 207                resource->align = resource->gran = 0;
 208                while (!(moving & resource->size)) {
 209                        resource->size <<= 1;
 210                        resource->align += 1;
 211                        resource->gran += 1;
 212                }
 213                resource->limit = limit = moving | (resource->size - 1);
 214        }
 215
 216        /* Some broken hardware has read-only registers that do not 
 217         * really size correctly.
 218         * Example: the Acer M7229 has BARs 1-4 normally read-only. 
 219         * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
 220         * by writing 0xffffffff to it, it will read back as 0x1f1 -- a 
 221         * violation of the spec. 
 222         * We catch this case and ignore it by observing which bits move,
 223         * This also catches the common case unimplemented registers
 224         * that always read back as 0.
 225         */
 226        if (moving == 0) {
 227                if (value != 0) {
 228                        printk(BIOS_DEBUG,
 229                               "%s register %02lx(%08lx), read-only ignoring it\n",
 230                               dev_path(dev), index, value);
 231                }
 232                /* resource->flags = 0;
 233                 * Shouldn't zero because we'll get off with 64-bit BARs.
 234                 * Are there any others to save?
 235                 */
 236                resource->flags &= IORESOURCE_PCI64;
 237        } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
 238                /* An I/O mapped base address. */
 239                attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
 240                resource->flags |= IORESOURCE_IO;
 241                /* I don't want to deal with 32bit I/O resources. */
 242                resource->limit = 0xffff;
 243        } else {
 244                /* A Memory mapped base address. */
 245                attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
 246                resource->flags |= IORESOURCE_MEM;
 247                if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 248                        resource->flags |= IORESOURCE_PREFETCH;
 249                }
 250                attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
 251                if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
 252                        /* 32bit limit. */
 253                        resource->limit = 0xffffffffUL;
 254                } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
 255                        /* 1MB limit. */
 256                        resource->limit = 0x000fffffUL;
 257                } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
 258                        /* 64bit limit. */
 259                        resource->limit = 0xffffffffffffffffULL;
 260                } else {
 261                        /* Invalid value. */
 262                        printk(BIOS_ERR,"Broken BAR with value %lx\n",attr);
 263                        printk(BIOS_ERR," on dev %s at index %02lx\n",dev->dtsname,index);
 264                        resource->flags = 0;
 265                }
 266        }
 267        /* Don't let the limit exceed which bits can move. */
 268        if (resource->limit > limit) {
 269                resource->limit = limit;
 270        }
 271
 272        return resource;
 273}
 274
 275/**
 276 * Given a device and an index, read the size of the BAR for that register. 
 277 *
 278 * @param dev Pointer to the device structure.
 279 * @param index Address of the PCI configuration register.
 280 */
 281static void pci_get_rom_resource(struct device *dev, unsigned long index)
 282{
 283        struct resource *resource;
 284        unsigned long value;
 285        resource_t moving, limit;
 286
 287        if ((dev->on_mainboard) && (dev->rom_address == 0)) {
 288                // Skip it if rom_address is not set in the mainboard's dts.
 289                return;
 290        }
 291
 292        /* Initialize the resources to nothing. */
 293        resource = new_resource(dev, index);
 294
 295        /* Get the initial value. */
 296        value = pci_read_config32(dev, index);
 297
 298        /* See which bits move. */
 299        moving = pci_moving_config32(dev, index);
 300
 301        /* Clear the Enable bit. */
 302        moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
 303
 304        /* Find the resource constraints. 
 305         * Start by finding the bits that move. From there:
 306         * - Size is the least significant bit of the bits that move.
 307         * - Limit is all of the bits that move plus all of the lower bits.
 308         * See PCI Spec 6.2.5.1.
 309         */
 310        limit = 0;
 311
 312        if (moving) {
 313                resource->size = 1;
 314                resource->align = resource->gran = 0;
 315                while (!(moving & resource->size)) {
 316                        resource->size <<= 1;
 317                        resource->align += 1;
 318                        resource->gran += 1;
 319                }
 320                resource->limit = limit = moving | (resource->size - 1);
 321                resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
 322        } else {
 323                if (value != 0) {
 324                        printk(BIOS_DEBUG,
 325                               "%s register %02lx(%08lx), read-only ignoring it\n",
 326                               dev_path(dev), index, value);
 327                }
 328                resource->flags = 0;
 329        }
 330
 331        /* For on board device with embedded ROM image, the ROM image is at
 332         * fixed address specified in the dts, the dev->rom_address is
 333         * inited by driver_pci_onboard_ops::enable_dev() */
 334        if ((dev->on_mainboard) && (dev->rom_address != 0)) {
 335                resource->base = dev->rom_address;
 336                resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
 337                    IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
 338        }
 339
 340        compact_resources(dev);
 341}
 342
 343/**
 344 * Read the base address registers for a given device.
 345 *
 346 * @param dev Pointer to the dev structure.
 347 * @param howmany How many registers to read (6 for device, 2 for bridge).
 348 */
 349static void pci_read_bases(struct device *dev, unsigned int howmany)
 350{
 351        unsigned long index;
 352
 353        for (index = PCI_BASE_ADDRESS_0;
 354             (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
 355                struct resource *resource;
 356                resource = pci_get_resource(dev, index);
 357                index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
 358        }
 359
 360        compact_resources(dev);
 361}
 362
 363static void pci_record_bridge_resource(struct device *dev, resource_t moving,
 364                                       unsigned int index, unsigned long type)
 365{
 366        /* Initialize the constraints on the current bus. */
 367        struct resource *resource;
 368        resource = NULL;
 369        if (moving) {
 370                unsigned long gran;
 371                resource_t step;
 372                resource = new_resource(dev, index);
 373                resource->size = 0;
 374                gran = 0;
 375                step = 1;
 376                while ((moving & step) == 0) {
 377                        gran += 1;
 378                        step <<= 1;
 379                }
 380                resource->gran = gran;
 381                resource->align = gran;
 382                resource->limit = moving | (step - 1);
 383                resource->flags = type | IORESOURCE_PCI_BRIDGE | IORESOURCE_BRIDGE;
 384        }
 385        return;
 386}
 387
 388static void pci_bridge_read_bases(struct device *dev)
 389{
 390        resource_t moving_base, moving_limit, moving;
 391
 392        /* See if the bridge I/O resources are implemented. */
 393        moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
 394        moving_base |=
 395            ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
 396
 397        moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
 398        moving_limit |=
 399            ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
 400
 401        moving = moving_base & moving_limit;
 402
 403        /* Initialize the I/O space constraints on the current bus. */
 404        pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
 405
 406        /* See if the bridge prefmem resources are implemented. */
 407        moving_base =
 408            ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
 409        moving_base |= ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
 410
 411        moving_limit = ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
 412        moving_limit |= ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
 413
 414        moving = moving_base & moving_limit;
 415        /* Initialize the prefetchable memory constraints on the current bus. */
 416        pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
 417                                   IORESOURCE_MEM | IORESOURCE_PREFETCH);
 418
 419        /* See if the bridge mem resources are implemented. */
 420        moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
 421        moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
 422
 423        moving = moving_base & moving_limit;
 424
 425        /* Initialize the memory resources on the current bus. */
 426        pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
 427                                   IORESOURCE_MEM);
 428
 429        compact_resources(dev);
 430}
 431
 432void pci_dev_read_resources(struct device *dev)
 433{
 434        pci_read_bases(dev, 6);
 435        pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
 436}
 437
 438void pci_bus_read_resources(struct device *dev)
 439{
 440        struct device *child;
 441
 442        printk(BIOS_DEBUG, "%s: %s bus %s\n",
 443               __func__, dev_path(dev), dev->bus? dev_path(dev->bus->dev):"NULL");
 444        pci_bridge_read_bases(dev);
 445        pci_read_bases(dev, 2);
 446        pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
 447        if (!dev->bus){
 448                printk(BIOS_ERR, "%s: %s bus %s\n",
 449                       __func__, dev_path(dev), dev->bus? dev_path(dev->bus->dev):"NULL");
 450        }
 451
 452        for (child = dev->link[0].children; child; child = child->sibling)
 453                if (child->ops && child->ops->phase4_read_resources)
 454                        child->ops->phase4_read_resources(child);
 455                else
 456                        printk(BIOS_ERR, "%s: %s missing Phase4\n",
 457                               __func__, dev_path(child));
 458}
 459
 460/**
 461 * Set resources for the PCI domain.
 462 *
 463 * A PCI domain contains the I/O and memory resource address space below it.
 464 * Set up basic global ranges for I/O and memory. Allocation of sub-resources
 465 * draws on these top-level resources in the usual hierarchical manner.
 466 *
 467 * @param dev The northbridge device.
 468 */
 469void pci_domain_read_resources(struct device *dev)
 470{
 471        struct resource *res;
 472
 473        /* Initialize the system-wide I/O space constraints. */
 474        res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
 475        res->limit = 0xffffUL;
 476        res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
 477                     IORESOURCE_ASSIGNED | IORESOURCE_BRIDGE;
 478
 479        /* Initialize the system-wide memory resources constraints. */
 480        res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
 481        res->limit = 0xffffffffULL;
 482        res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
 483                     IORESOURCE_ASSIGNED | IORESOURCE_BRIDGE;
 484}
 485
 486static void pci_set_resource(struct device *dev, struct resource *resource)
 487{
 488        resource_t base, end;
 489
 490        /* Make certain the resource has actually been assigned a value. */
 491        if (!(resource->flags & IORESOURCE_ASSIGNED) && resource->size!=0) {
 492                printk(BIOS_ERR,
 493                       "ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
 494                       dev_path(dev), resource->index, resource_type(resource),
 495                       resource->size);
 496                return;
 497        }
 498
 499        /* If I have already stored this resource don't worry about it. */
 500        if (resource->flags & IORESOURCE_STORED) {
 501                return;
 502        }
 503
 504        /* If the resource is subtractive don't worry about it. */
 505        if (resource->flags & IORESOURCE_SUBTRACTIVE) {
 506                return;
 507        }
 508
 509        /* Only handle PCI memory and I/O resources for now. */
 510        if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
 511                return;
 512
 513        /* Enable the resources in the command register. */
 514        if (resource->size) {
 515                if (resource->flags & IORESOURCE_MEM) {
 516                        dev->command |= PCI_COMMAND_MEMORY;
 517                }
 518                if (resource->flags & IORESOURCE_IO) {
 519                        dev->command |= PCI_COMMAND_IO;
 520                }
 521                if (resource->flags & IORESOURCE_PCI_BRIDGE) {
 522                        dev->command |= PCI_COMMAND_MASTER;
 523                }
 524        }
 525        /* Get the base address. */
 526        base = resource->base;
 527
 528        /* Get the end. */
 529        end = resource_end(resource);
 530
 531        /* Now store the resource. */
 532        resource->flags |= IORESOURCE_STORED;
 533        /* PCI Bridges have no enable bit.  They are disabled if the base of
 534         * the range is greater than the limit.  If the size is zero, disable
 535         * by setting the base = limit and end = limit - 2^gran.
 536         */
 537        if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
 538                base = resource->limit;
 539                end = resource->limit - (1<<resource->gran);
 540                resource->base = base;
 541        }
 542
 543        if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
 544                unsigned long base_lo, base_hi;
 545                /* Some chipsets allow us to set/clear the I/O bit
 546                 * (e.g. VIA 82c686a). So set it to be safe.
 547                 */
 548                base_lo = base & 0xffffffff;
 549                base_hi = (base >> 32) & 0xffffffff;
 550                if (resource->flags & IORESOURCE_IO) {
 551                        base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
 552                }
 553                pci_write_config32(dev, resource->index, base_lo);
 554                if (resource->flags & IORESOURCE_PCI64) {
 555                        pci_write_config32(dev, resource->index + 4, base_hi);
 556                }
 557        } else if (resource->index == PCI_IO_BASE) {
 558                /* Set the I/O ranges. */
 559                pci_write_config8(dev, PCI_IO_BASE, base >> 8);
 560                pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
 561                pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
 562                pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
 563        } else if (resource->index == PCI_MEMORY_BASE) {
 564                /* Set the memory range. */
 565                pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
 566                pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
 567        } else if (resource->index == PCI_PREF_MEMORY_BASE) {
 568                /* Set the prefetchable memory range. */
 569                pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
 570                pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
 571                pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
 572                pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
 573        } else {
 574                /* Don't let me think I stored the resource. */
 575                resource->flags &= ~IORESOURCE_STORED;
 576                printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
 577                       resource->index);
 578        }
 579        report_resource_stored(dev, resource, __func__);
 580        return;
 581}
 582
 583void pci_set_resources(struct device *dev)
 584{
 585        struct resource *resource, *last;
 586        unsigned int link;
 587        u8 line;
 588
 589        last = &dev->resource[dev->resources];
 590
 591        for (resource = &dev->resource[0]; resource < last; resource++) {
 592                pci_set_resource(dev, resource);
 593        }
 594        for (link = 0; link < dev->links; link++) {
 595                struct bus *bus;
 596                bus = &dev->link[link];
 597                if (bus->children) {
 598                        phase4_set_resources(bus);
 599                }
 600        }
 601
 602        /* Set a default latency timer. */
 603        pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
 604
 605        /* Set a default secondary latency timer. */
 606        if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
 607                pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
 608        }
 609
 610        /* Zero the IRQ settings. */
 611        line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
 612        if (line) {
 613                pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
 614        }
 615        /* Set the cache line size, so far 64 bytes is good for everyone. */
 616        pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
 617}
 618
 619/**
 620 * Create a RAM resource, by taking the passed-in size and creating
 621 * a resource record.
 622 *
 623 * @param dev The device.
 624 * @param index A resource index.
 625 * @param basek Base memory address in KB.
 626 * @param sizek Size of memory in KB.
 627 */
 628void ram_resource(struct device *dev, unsigned long index,
 629                  unsigned long basek, unsigned long sizek)
 630{
 631        struct resource *res;
 632
 633        if (!sizek)
 634                return;
 635
 636        res = new_resource(dev, index);
 637        res->base = ((resource_t) basek) << 10; /* Convert to bytes. */
 638        res->size = ((resource_t) sizek) << 10; /* Convert to bytes. */
 639        res->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
 640            IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
 641
 642        printk(BIOS_SPEW, "Adding RAM resource (%lld bytes)\n", res->size);
 643}
 644
 645void pci_dev_set_subsystem_wrapper(struct device *dev)
 646{
 647        const struct pci_operations *ops;
 648        u16 vendor = dev->id.pci.vendor;
 649        u16 device = dev->id.pci.device;
 650
 651        ops = ops_pci(dev);
 652
 653        /* If either vendor or device is zero, we leave it as is. */
 654        if (ops && ops->set_subsystem && vendor && device) {
 655                /* If there's no explicit subsystem ID for this device and the
 656                 * device is onboard, use the board defaults. */
 657                vendor = dev->subsystem_vendor;
 658                device = dev->subsystem_device;
 659
 660                /* Set the subsystem vendor and device ID for mainboard devices. */
 661                if (dev->on_mainboard) {
 662                        if (!vendor)
 663                                vendor = dev_root.subsystem_vendor;
 664                        if (!device)
 665                                device = dev_root.subsystem_device;
 666
 667                        printk(BIOS_DEBUG,
 668                               "%s: Setting subsystem VID/DID to %02x/%02x\n",
 669                               dev_path(dev), vendor, device);
 670
 671                        ops->set_subsystem(dev, vendor, device);
 672
 673                } else {
 674                        printk(BIOS_DEBUG, "%s: Device not on_mainboard\n",
 675                               dev_path(dev));
 676                }
 677        } 
 678                
 679}
 680
 681void pci_dev_enable_resources(struct device *dev)
 682{
 683        u16 command;
 684
 685        pci_dev_set_subsystem_wrapper(dev);
 686
 687        command = pci_read_config16(dev, PCI_COMMAND);
 688        command |= dev->command;
 689        command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
 690        printk(BIOS_DEBUG, "%s: %s (%s) cmd <- %02x\n", __func__, dev->dtsname,
 691               dev_path(dev), command);
 692        pci_write_config16(dev, PCI_COMMAND, command);
 693}
 694
 695void pci_bus_enable_resources(struct device *dev)
 696{
 697        u16 ctrl;
 698
 699        /* Enable I/O in command register if there is VGA card
 700         * connected with (even it does not claim I/O resource).
 701         */
 702        if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
 703                dev->command |= PCI_COMMAND_IO;
 704        ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
 705        ctrl |= dev->link[0].bridge_ctrl;
 706        ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); // Error check.
 707        printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
 708        pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
 709
 710        pci_dev_enable_resources(dev);
 711        enable_childrens_resources(dev);
 712}
 713
 714void pci_bus_reset(struct bus *bus)
 715{
 716        unsigned ctl;
 717        ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
 718        ctl |= PCI_BRIDGE_CTL_BUS_RESET;
 719        pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
 720        mdelay(10);
 721        ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
 722        pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
 723        delay(1);
 724}
 725
 726void pci_dev_set_subsystem(struct device *dev, u16 vendor, u16 device)
 727{
 728        pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
 729                           ((device & 0xffff) << 16) | (vendor & 0xffff));
 730}
 731
 732void pci_dev_init(struct device *dev)
 733{
 734        printk(BIOS_SPEW, "PCI: pci_dev_init %s\n", dev->dtsname);
 735#ifdef CONFIG_PCI_OPTION_ROM_RUN
 736        void run_bios(struct device *dev, unsigned long addr);
 737        struct rom_header *rom, *ram;
 738
 739        printk(BIOS_INFO, "Probing for option ROM\n");
 740        rom = pci_rom_probe(dev);
 741        if (rom == NULL)
 742                return;
 743        ram = pci_rom_load(dev, rom);
 744        if (ram == NULL)
 745                return;
 746        run_bios(dev, (unsigned long)ram);
 747#endif
 748}
 749
 750/** Default device operation for PCI devices. */
 751const struct pci_operations pci_dev_ops_pci = {
 752        .set_subsystem = pci_dev_set_subsystem,
 753};
 754
 755const struct device_operations default_pci_ops_dev = {
 756        .phase4_read_resources   = pci_dev_read_resources,
 757        .phase4_set_resources    = pci_set_resources,
 758        .phase5_enable_resources = pci_dev_enable_resources,
 759        .phase6_init             = pci_dev_init,
 760        .phase3_scan             = 0,
 761        .ops_pci                 = &pci_dev_ops_pci,
 762};
 763
 764/** Default device operations for PCI bridges. */
 765const struct pci_operations pci_bus_ops_pci = {
 766        .set_subsystem = 0,
 767};
 768
 769const struct device_operations default_pci_ops_bus = {
 770        .phase3_scan             = pci_scan_bridge,
 771        .phase4_read_resources   = pci_bus_read_resources,
 772        .phase4_set_resources    = pci_set_resources,
 773        .phase5_enable_resources = pci_bus_enable_resources,
 774        .phase6_init             = 0,
 775        .reset_bus               = pci_bus_reset,
 776        .ops_pci                 = &pci_bus_ops_pci,
 777};
 778
 779/**
 780 * Detect the type of downstream bridge.
 781 *
 782 * This function is a heuristic to detect which type of bus is downstream
 783 * of a PCI-to-PCI bridge. This functions by looking for various capability
 784 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
 785 * Hypertransport all seem to have appropriate capabilities.
 786 * 
 787 * When only a PCI-Express capability is found the type
 788 * is examined to see which type of bridge we have.
 789 *
 790 * @param dev Pointer to the device structure of the bridge.
 791 * @return Appropriate bridge operations.
 792 */
 793static const struct device_operations *get_pci_bridge_ops(struct device *dev)
 794{
 795#ifndef CONFIG_NO_PCIX_SUPPORT
 796        unsigned int pcix_pos;
 797        pcix_pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
 798        if (pcix_pos) {
 799                printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n",
 800                       dev_path(dev));
 801                return &default_pcix_ops_bus;
 802        }
 803#endif
 804#ifndef CONFIG_NO_AGP_SUPPORT
 805        /* How do I detect an PCI to AGP bridge? */
 806#warning AGP detection not implemented, so AGP bridge plugin not supported.
 807
 808#endif
 809#ifdef CONFIG_HYPERTRANSPORT_SUPPORT
 810        unsigned int ht_pos;
 811        ht_pos = 0;
 812        while ((ht_pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, ht_pos))) {
 813                unsigned int flags;
 814                flags = pci_read_config16(dev, ht_pos + PCI_CAP_FLAGS);
 815                if ((flags >> 13) == 1) {
 816                        /* Host or Secondary Interface. */
 817                        printk(BIOS_DEBUG,
 818                               "%s subordinate bus Hypertransport\n",
 819                               dev_path(dev));
 820                        return &default_ht_ops_bus;
 821                }
 822        }
 823#endif
 824#ifndef CONFIG_NO_PCIE_SUPPORT
 825        unsigned int pcie_pos;
 826        pcie_pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
 827        if (pcie_pos) {
 828                unsigned int flags;
 829                flags = pci_read_config16(dev, pcie_pos + PCI_EXP_FLAGS);
 830                switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
 831                case PCI_EXP_TYPE_ROOT_PORT:
 832                case PCI_EXP_TYPE_UPSTREAM:
 833                case PCI_EXP_TYPE_DOWNSTREAM:
 834                        printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
 835                               dev_path(dev));
 836                        return &default_pcie_ops_bus;
 837                case PCI_EXP_TYPE_PCI_BRIDGE:
 838                        printk(BIOS_DEBUG, "%s subordinate PCI\n",
 839                               dev_path(dev));
 840                        return &default_pci_ops_bus;
 841                default:
 842                        break;
 843                }
 844        }
 845#endif
 846        return &default_pci_ops_bus;
 847}
 848
 849/**
 850 * Set up PCI device operation.  Check if it already has a driver.  If not, use
 851 * find_device_operations, or set to a default based on type.
 852 *
 853 * @param dev Pointer to the device whose pci_ops you want to set.
 854 * @see pci_drivers
 855 */
 856static void set_pci_ops(struct device *dev)
 857{
 858        struct device_operations *c;
 859
 860        if (dev->ops) {
 861                printk(BIOS_SPEW, "%s: dev %s already has ops of type %x\n",
 862                       __func__, dev->dtsname, dev->ops->id.type);
 863                return;
 864        }
 865
 866        /* Look through the list of setup drivers and find one for
 867         * this PCI device.
 868         */
 869        c = find_device_operations(&dev->id);
 870        if (c) {
 871                dev->ops = c;
 872                printk(BIOS_SPEW, "%s id %s %sops\n",
 873                        dev_path(dev), dev_id_string(&dev->id), 
 874                        (dev->ops->phase3_scan ? "bus " : ""));
 875                return;
 876        }
 877
 878        /* If I don't have a specific driver use the default operations. */
 879        switch (dev->hdr_type & 0x7f) { /* Header type. */
 880        case PCI_HEADER_TYPE_NORMAL:    /* Standard header. */
 881                if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
 882                        printk(BIOS_ERR,
 883                               "%s [%s] hdr_type %02x doesn't match"
 884                               "class %06x, ignoring.\n", dev_path(dev),
 885                               dev_id_string(&dev->id), dev->class >> 8,
 886                               dev->hdr_type);
 887                else
 888                        dev->ops = &default_pci_ops_dev;
 889                break;
 890        case PCI_HEADER_TYPE_BRIDGE:
 891                if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
 892                        printk(BIOS_ERR,
 893                               "%s [%s] hdr_type %02x doesn't match"
 894                               "class %06x, ignoring.\n", dev_path(dev),
 895                               dev_id_string(&dev->id), dev->class >> 8,
 896                               dev->hdr_type);
 897                else
 898                        dev->ops = get_pci_bridge_ops(dev);
 899                break;
 900#ifndef CONFIG_NO_CARDBUS_SUPPORT
 901        case PCI_HEADER_TYPE_CARDBUS:
 902                dev->ops = &default_cardbus_ops_bus;
 903                break;
 904#endif
 905        default:
 906                if (dev->enabled) {
 907                        printk(BIOS_ERR,
 908                               "%s [%s/%06x] has unknown header "
 909                               "type %02x, ignoring.\n", dev_path(dev),
 910                               dev_id_string(&dev->id), dev->class >> 8,
 911                               dev->hdr_type);
 912                }
 913        }
 914        printk(BIOS_INFO, "%s: dev %s set ops to type %x\n", __func__,
 915               dev->dtsname, dev->ops? dev->ops->id.type : 0);
 916        return;
 917}
 918
 919/**
 920 * See if we have already allocated a device structure for a given devfn.
 921 *
 922 * Given a linked list of PCI device structures and a devfn number, find the
 923 * device structure correspond to the devfn, if present. This function also
 924 * removes the device structure from the linked list.
 925 *
 926 * @param list The device structure list.
 927 * @param devfn A device/function number.
 928 * @return Pointer to the device structure found or NULL of we have not
 929 *         allocated a device for this devfn yet.
 930 */
 931static struct device *pci_get_dev(struct device **list, unsigned int devfn)
 932{
 933        struct device *dev;
 934        dev = 0;
 935        printk(BIOS_SPEW, "%s: list is %sNULL, *list is %sNULL\n", __func__,
 936                list?"NOT ":"", *list?"NOT ":"");
 937        for (; *list; list = &(*list)->sibling) {
 938                printk(BIOS_SPEW, "%s: check dev %s \n", __func__,
 939                       (*list)->dtsname);
 940                if ((*list)->path.type != DEVICE_PATH_PCI) {
 941                        printk(BIOS_NOTICE,
 942                               "%s: child %s(%s) not a pci device: it's type %d\n",
 943                               __func__, (*list)->dtsname, dev_path(*list),
 944                               (*list)->path.type);
 945                        continue;
 946                }
 947                printk(BIOS_SPEW, "%s: check dev %s it has devfn 0x%02x\n",
 948                       __func__, (*list)->dtsname, (*list)->path.pci.devfn);
 949                if ((*list)->path.pci.devfn == devfn) {
 950                        /* Unlink from the list. */
 951                        dev = *list;
 952                        *list = (*list)->sibling;
 953                        dev->sibling = NULL;
 954                        break;
 955                }
 956        }
 957
 958        /* Just like alloc_dev() add the device to the list of devices on the
 959         * bus. When the list of devices was formed we removed all of the
 960         * parents children, and now we are interleaving static and dynamic
 961         * devices in order on the bus.
 962         */
 963        if (dev) {
 964                struct device *child;
 965                /* Find the last child of our parent. */
 966                for (child = dev->bus->children; child && child->sibling;) {
 967                        child = child->sibling;
 968                }
 969                /* Place the device on the list of children of its parent. */
 970                if (child) {
 971                        child->sibling = dev;
 972                } else {
 973                        dev->bus->children = dev;
 974                }
 975        }
 976
 977        return dev;
 978}
 979
 980/** 
 981 * Scan a PCI bus.
 982 *
 983 * Determine the existence of a given PCI device. Allocate a new struct device
 984 * if dev==NULL was passed in and the device exists in hardware.
 985 *
 986 * @param dev Pointer to the device structure if it already is in the device
 987 *         tree, i.e. was specified in the dts. It may not exist on hardware,
 988 *         however. When looking for hardware not yet in the device tree, this
 989 *         parameter is NULL.
 990 * @param bus Pointer to the bus structure.
 991 * @param devfn A device/function number.
 992 * @return The device structure for the device if it exists in hardware
 993 *         or the passed in device structure with enabled=0 if the device
 994 *         does not exist in hardware and only in the tree
 995 *         or NULL if no device is found and dev==NULL was passed in.
 996 */
 997struct device *pci_probe_dev(struct device *dev, struct bus *bus,
 998                             unsigned int devfn)
 999{
1000        u32 id, class;
1001        u8 hdr_type;
1002
1003        /* Detect if a device is present. */
1004        if (!dev) {
1005                struct device dummy;
1006                struct device_id devid;
1007                dummy.bus = bus;
1008                dummy.path.type = DEVICE_PATH_PCI;
1009                dummy.path.pci.devfn = devfn;
1010                id = pci_read_config32(&dummy, PCI_VENDOR_ID);
1011                /* Have we found something?
1012                 * Some broken boards return 0 if a slot is empty.
1013                 */
1014                if ((id == 0xffffffff) || (id == 0x00000000) ||
1015                    (id == 0x0000ffff) || (id == 0xffff0000)) {
1016                        printk(BIOS_SPEW, "PCI: devfn 0x%x, bad id 0x%x\n",
1017                               devfn, id);
1018                        return NULL;
1019                }
1020                devid.type = DEVICE_ID_PCI;
1021                devid.pci.vendor = id & 0xffff;
1022                devid.pci.device = id >> 16;
1023                dev = alloc_dev(bus, &dummy.path, &devid);
1024        } else {
1025                /* Enable/disable the device. Once we have found the device
1026                 * specific operations this operations we will disable the
1027                 * device with those as well.
1028                 * 
1029                 * This is geared toward devices that have subfunctions
1030                 * that do not show up by default.
1031                 * 
1032                 * If a device is a stuff option on the motherboard
1033                 * it may be absent and enable_dev() must cope.
1034                 */
1035                /* Run the magic enable sequence for the device. */
1036                if (dev->ops && dev->ops->phase3_chip_setup_dev) {
1037                        dev->ops->phase3_chip_setup_dev(dev);
1038                }
1039                /* Now read the vendor and device ID. */
1040                id = pci_read_config32(dev, PCI_VENDOR_ID);
1041
1042                /* If the device does not have a PCI ID disable it. Possibly
1043                 * this is because we have already disabled the device. But
1044                 * this also handles optional devices that may not always
1045                 * show up.
1046                 */
1047                if ((id == 0xffffffff) || (id == 0x00000000) ||
1048                    (id == 0x0000ffff) || (id == 0xffff0000)) {
1049                        if (dev->enabled) {
1050                                printk(BIOS_INFO,
1051                                       "Disabling static device: %s\n",
1052                                       dev_path(dev));
1053                                dev->enabled = 0;
1054                        }
1055                        return dev;
1056                }
1057        }
1058        /* Read the rest of the PCI configuration information. */
1059        /* Store the interesting information in the device structure. */
1060        hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1061        class = pci_read_config32(dev, PCI_CLASS_REVISION);
1062        dev->status = pci_read_config16(dev, PCI_STATUS);
1063        dev->revision = pci_read_config8(dev, PCI_REVISION_ID);
1064        dev->cache_line = pci_read_config8(dev, PCI_CACHE_LINE_SIZE);
1065        dev->irq_line = pci_read_config8(dev, PCI_INTERRUPT_LINE);
1066        dev->irq_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1067        dev->min_gnt = pci_read_config8(dev, PCI_MIN_GNT);
1068        dev->max_lat = pci_read_config8(dev, PCI_MAX_LAT);
1069
1070        /*Per-device subsystem ID should only be read from the device if none
1071         * has been specified for the device in the dts.
1072         */
1073        if (!dev->subsystem_vendor && !dev->subsystem_device) {
1074                dev->subsystem_vendor = 
1075                                pci_read_config16(dev, PCI_SUBSYSTEM_VENDOR_ID);
1076                dev->subsystem_device = 
1077                                pci_read_config16(dev, PCI_SUBSYSTEM_ID);
1078        }
1079
1080        dev->id.type = DEVICE_ID_PCI;
1081        dev->id.pci.vendor = id & 0xffff;
1082        dev->id.pci.device = (id >> 16) & 0xffff;
1083        dev->hdr_type = hdr_type;
1084
1085        /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
1086        dev->class = class >> 8;
1087
1088        /* Architectural/System devices always need to be bus masters. */
1089        if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
1090                dev->command |= PCI_COMMAND_MASTER;
1091        }
1092        /* Look at the vendor and device ID, or at least the header type and
1093         * class and figure out which set of configuration methods to use.
1094         * Unless we already have some PCI ops.
1095         */
1096        set_pci_ops(dev);
1097
1098        /* Now run the magic enable/disable sequence for the device. */
1099        if (dev->ops && dev->ops->phase3_enable) {
1100                dev->ops->phase3_enable(dev);
1101        }
1102
1103        /* Display the device. */
1104        printk(BIOS_DEBUG, "%s [%s] %s%s\n",
1105               dev_path(dev), dev_id_string(&dev->id),
1106               dev->enabled ? "enabled" : "disabled",
1107               dev->ops ? "" : " No operations");
1108
1109        return dev;
1110}
1111
1112/** 
1113 * Scan a PCI bus.
1114 *
1115 * Determine the existence of devices and bridges on a PCI bus. If there are
1116 * bridges on the bus, recursively scan the buses behind the bridges.
1117 *
1118 * This function is the default scan_bus() method for the root device
1119 * 'dev_root'.
1120 *
1121 * @param bus Pointer to the bus structure.
1122 * @param min_devfn Minimum devfn to look at in the scan usually 0x00.
1123 * @param max_devfn Maximum devfn to look at in the scan usually 0xff.
1124 * @param curr_bus Current bus number to be assigned.
1125 * @return The new curr_bus, after scanning all subordinate buses.
1126 */
1127unsigned int pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1128                          unsigned int max_devfn, unsigned int curr_bus)
1129{
1130        unsigned int devfn;
1131        struct device *old_devices;
1132        struct device *child;
1133
1134        printk(BIOS_DEBUG, "%s start bus->dev %s bus %x\n", __func__,
1135               bus->dev->dtsname, bus->link);
1136
1137#warning This check needs checking.
1138        if (bus->dev->path.type != DEVICE_PATH_PCI_BUS)
1139                printk(BIOS_ERR, "ERROR: pci_scan_bus called with incorrect "
1140                       "bus->dev->path.type, path is %s\n", dev_path(bus->dev));
1141
1142#if PCI_BUS_SEGN_BITS
1143        printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
1144               bus->secondary >> 8, bus->secondary & 0xff);
1145#else
1146        printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1147#endif
1148
1149        /* Remove children of the bus, so the enumeration starts empty. */
1150        old_devices = bus->children;
1151        printk(BIOS_DEBUG, "%s: old_devices %s, dev for this bus %s\n",
1152               __func__, old_devices?old_devices->dtsname:"NULL", bus->dev->dtsname);
1153        bus->children = NULL;
1154
1155        post_code(POST_STAGE2_PCISCANBUS_ENTER);
1156        printk(BIOS_SPEW, "PCI: scan devfn 0x%x to 0x%x\n", min_devfn,
1157               max_devfn);
1158        /* Probe all devices/functions on this bus with some optimization for
1159         * non-existence and single function devices.
1160         */
1161        for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1162                struct device *dev;
1163                printk(BIOS_SPEW, "PCI: devfn 0x%x\n", devfn);
1164
1165                /* First thing look in the device tree. */
1166                dev = pci_get_dev(&old_devices, devfn);
1167
1168                printk(BIOS_SPEW,
1169                       "PCI: pci_scan_bus pci_get_dev returns dev %s\n",
1170                       dev ? dev->dtsname : "None (no dev in tree yet)");
1171
1172                /* See if a device is present and setup the device structure. */
1173                dev = pci_probe_dev(dev, bus, devfn);
1174                printk(BIOS_SPEW,
1175                       "PCI: pci_scan_bus pci_probe_dev returns dev %s\n",
1176                       dev ? dev->dtsname : "None (no response)");
1177
1178                /* If this is not a multi function device, or the device is
1179                 * not present don't waste time probing another function. 
1180                 * Skip to next device.
1181                 */
1182                if ((PCI_FUNC(devfn) == 0x00) &&
1183                    (!dev
1184                     || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1185                        printk(BIOS_SPEW, "Not a multi function device, or the "
1186                               "device is not present. Skip to next device.\n");
1187                        devfn += 0x07;
1188                }
1189        }
1190        printk(BIOS_SPEW, "PCI: Done for loop\n");
1191        post_code(POST_STAGE2_PCISCANBUS_DONEFORLOOP);
1192
1193        /* Warn if any leftover static devices are are found.  
1194         * There's probably a problem in the device tree (dts).
1195         */
1196        if (old_devices) {
1197                struct device *left;
1198                printk(BIOS_INFO, "PCI: Left over static devices:\n");
1199                for (left = old_devices; left; left = left->sibling) {
1200                        printk(BIOS_INFO, "%s\n", left->dtsname);
1201                }
1202                printk(BIOS_INFO, "PCI: End of leftover list.\n");
1203        }
1204
1205        /* For all children that implement scan_bus() (i.e. bridges)
1206         * scan the bus behind that child.
1207         */
1208        for (child = bus->children; child; child = child->sibling) {
1209                curr_bus = dev_phase3_scan(child, curr_bus);
1210        }
1211
1212        /* We've scanned the bus and so we know all about what's on the other
1213         * side of any bridges that may be on this bus plus any devices.
1214         * Return how far we've got finding sub-buses.
1215         */
1216        printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with curr_bus=%03x\n",
1217                curr_bus);
1218        post_code(POST_STAGE2_PCISCANBUS_EXIT);
1219        return curr_bus;
1220}
1221
1222/**
1223 * Support for scan bus from the "tippy top" -- i.e. the PCI domain,
1224 * not the 0:0.0 device.
1225 *
1226 * This function works for almost all chipsets (AMD K8 is the exception).
1227 *
1228 * @param dev The PCI domain device.
1229 * @param max The current bus number to be assigned.
1230 * @return The current bus number returned by pci_scan_bus.
1231 */
1232unsigned int pci_domain_scan_bus(struct device *dev, unsigned int curr_bus)
1233{
1234        printk(BIOS_SPEW, "pci_domain_scan_bus: calling pci_scan_bus\n");
1235        /* There is only one link on this device, and it is always link 0. */
1236        return pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, curr_bus);
1237}
1238
1239/**
1240 * Scan a PCI bridge and the buses behind the bridge.
1241 *
1242 * Determine the existence of buses behind the bridge. Set up the bridge
1243 * according to the result of the scan.
1244 *
1245 * This function is the default scan_bus() method for PCI bridge devices.
1246 *
1247 * @param dev Pointer to the bridge device.
1248 * @param max The highest bus number assigned up to now.
1249 * @return The maximum bus number found, after scanning all subordinate buses.
1250 */
1251unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1252                                unsigned int (*do_scan_bus) (struct bus * bus,
1253                                                unsigned int min_devfn,
1254                                                unsigned int max_devfn,
1255                                                unsigned int max))
1256{
1257        struct bus *bus;
1258        u32 buses;
1259        u16 cr;
1260
1261        printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
1262
1263        bus = &dev->link[0];
1264        bus->dev = dev;
1265        dev->links = 1;
1266
1267        /* Set up the primary, secondary and subordinate bus numbers. We have
1268         * no idea how many buses are behind this bridge yet, so we set the
1269         * subordinate bus number to 0xff for the moment. 
1270         */
1271        bus->secondary = ++max;
1272        bus->subordinate = 0xff;
1273
1274        /* Clear all status bits and turn off memory, I/O and master enables. */
1275        cr = pci_read_config16(dev, PCI_COMMAND);
1276        pci_write_config16(dev, PCI_COMMAND, 0x0000);
1277        pci_write_config16(dev, PCI_STATUS, 0xffff);
1278
1279        /* Read the existing primary/secondary/subordinate bus
1280         * number configuration.
1281         */
1282        buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
1283
1284        /* Configure the bus numbers for this bridge: the configuration
1285         * transactions will not be propagated by the bridge if it is not
1286         * correctly configured.
1287         */
1288        buses &= 0xff000000;
1289        buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1290                  ((unsigned int)(bus->secondary) << 8) |
1291                  ((unsigned int)(bus->subordinate) << 16));
1292        pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1293
1294        /* Now we can scan all subordinate buses 
1295         * i.e. the bus behind the bridge.
1296         */
1297        max = do_scan_bus(bus, 0x00, 0xff, max);
1298
1299        /* We know the number of buses behind this bridge. Set the subordinate
1300         * bus number to its real value.
1301         */
1302        bus->subordinate = max;
1303        buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
1304        pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1305        pci_write_config16(dev, PCI_COMMAND, cr);
1306
1307        printk(BIOS_DEBUG, "%s DONE: [%x, %x, %x]\n", __func__, 
1308                dev->bus->secondary, bus->secondary, bus->subordinate);
1309        printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
1310        return max;
1311}
1312
1313/**
1314 * Scan a PCI bridge and the buses behind the bridge.
1315 *
1316 * Determine the existence of buses behind the bridge. Set up the bridge
1317 * according to the result of the scan.
1318 *
1319 * This function is the default scan_bus() method for PCI bridge devices.
1320 *
1321 * @param dev Pointer to the bridge device.
1322 * @param max The highest bus number assigned up to now.
1323 * @return The maximum bus number found, after scanning all subordinate buses.
1324 */
1325unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1326{
1327        printk(BIOS_SPEW, "pci_scan_bridge: calling pci_scan_bus\n");
1328        return do_pci_scan_bridge(dev, max, pci_scan_bus);
1329}
1330
1331/**
1332 * Tell the EISA int controller this int must be level triggered.
1333 *
1334 * THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
1335 *
1336 * @param intNum TODO
1337 */
1338void pci_level_irq(unsigned char intNum)
1339{
1340        unsigned short intBits = inb(0x4d0) | (((unsigned)inb(0x4d1)) << 8);
1341
1342        printk(BIOS_SPEW, "%s: current ints are 0x%x\n", __func__, intBits);
1343        intBits |= (1 << intNum);
1344
1345        printk(BIOS_SPEW, "%s: try to set ints 0x%x\n", __func__, intBits);
1346
1347        /* Write new values. */
1348        outb((unsigned char)intBits, 0x4d0);
1349        outb((unsigned char)(intBits >> 8), 0x4d1);
1350
1351        /* This seems like an error but is not. */
1352        if (inb(0x4d0) != (intBits & 0xff)) {
1353                printk(BIOS_ERR,
1354                       "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1355                       __func__, intBits & 0xff, inb(0x4d0));
1356        }
1357        if (inb(0x4d1) != ((intBits >> 8) & 0xff)) {
1358                printk(BIOS_ERR,
1359                       "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1360                       __func__, (intBits >> 8) & 0xff, inb(0x4d1));
1361        }
1362}
1363
1364/**
1365 * This function assigns IRQs for all functions contained within the
1366 * indicated device address. If the device does not exist or does not
1367 * require interrupts then this function has no effect.
1368 *
1369 * This function should be called for each PCI slot in your system.
1370 *
1371 * pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1372 * this slot.
1373 *
1374 * The particular irq #s that are passed in depend on the routing inside
1375 * your southbridge and on your motherboard.
1376 *
1377 * -kevinh@ispiri.com
1378 *
1379 * @param bus TODO
1380 * @param slot TODO
1381 * @param pIntAtoD TODO
1382 */
1383void pci_assign_irqs(unsigned int bus, unsigned int slot,
1384                     const unsigned char pIntAtoD[4])
1385{
1386        unsigned int functNum;
1387        struct device *pdev;
1388        unsigned char line;
1389        unsigned char irq;
1390        unsigned char readback;
1391
1392        /* Each slot may contain up to eight functions. */
1393        for (functNum = 0; functNum < 8; functNum++) {
1394                pdev = dev_find_slot(bus, (slot << 3) + functNum);
1395
1396                if (pdev) {
1397                        line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1398
1399                        // PCI spec says all other values are reserved.
1400                        if ((line >= 1) && (line <= 4)) {
1401                                irq = pIntAtoD[line - 1];
1402
1403                                printk(BIOS_DEBUG,
1404                                       "Assigning IRQ %d to %d:%x.%d\n", irq,
1405                                       bus, slot, functNum);
1406
1407                                pci_write_config8(pdev, PCI_INTERRUPT_LINE,
1408                                                  pIntAtoD[line - 1]);
1409
1410                                readback =
1411                                    pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1412                                printk(BIOS_DEBUG, "  Readback = %d\n",
1413                                       readback);
1414
1415                                // Change to level triggered.
1416                                pci_level_irq(pIntAtoD[line - 1]);
1417                        }
1418                }
1419        }
1420}
1421
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.