coreboot-v3/southbridge/amd/amd8131/amd8131_bridge.c
<<
>>
Prefs
   1/*
   2 * This file is part of the coreboot project.
   3 *
   4 * Copyright (C) 2003-2004 Linux Networx
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; version 2 of the License.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  18 */
  19
  20#include <types.h>
  21#include <lib.h>
  22#include <console.h>
  23#include <device/pci.h>
  24#include <mc146818rtc.h>
  25#include <device/pcix.h>
  26#include <device/pci_ids.h>
  27
  28#define NMI_OFF 0
  29
  30static void amd8131_walk_children(struct bus *bus,
  31        void (*visit)(struct device * dev, void *ptr), void *ptr)
  32{
  33        struct device * child;
  34        for(child = bus->children; child; child = child->sibling)
  35        {
  36                if (child->path.type != DEVICE_PATH_PCI) {
  37                        continue;
  38                }
  39                if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  40                        amd8131_walk_children(&child->link[0], visit, ptr);
  41                }
  42                visit(child, ptr);
  43        }
  44}
  45
  46struct amd8131_bus_info {
  47        unsigned sstatus;
  48        unsigned rev;
  49        int errata_56;
  50        int master_devices;
  51        int max_func;
  52};
  53
  54static void amd8131_count_dev(struct device * dev, void *ptr)
  55{
  56        struct amd8131_bus_info *info = ptr;
  57        /* Don't count pci bridges */
  58        if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  59                info->master_devices++;
  60        }
  61        if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
  62                info->max_func = PCI_FUNC(dev->path.pci.devfn);
  63        }
  64}
  65
  66
  67static void amd8131_pcix_tune_dev(struct device * dev, void *ptr)
  68{
  69        struct amd8131_bus_info *info = ptr;
  70        unsigned cap;
  71        unsigned status, cmd, orig_cmd;
  72        unsigned max_read, max_tran;
  73        int sib_funcs, sibs;
  74        struct device * sib;
  75
  76        if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
  77                return;
  78        }
  79        cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  80        if (!cap) {
  81                return;
  82        }
  83        /* How many siblings does this device have? */
  84        sibs = info->master_devices - 1;
  85        /* Count how many sibling functions this device has */
  86        sib_funcs = 0;
  87        for(sib = dev->bus->children; sib; sib = sib->sibling) {
  88                if (sib == dev) {
  89                        continue;
  90                }
  91                if (PCI_SLOT(sib->path.pci.devfn) != PCI_SLOT(dev->path.pci.devfn)) {
  92                        continue;
  93                }
  94                sib_funcs++;
  95        }
  96
  97        printk(BIOS_DEBUG,"%s AMD8131 PCI-X tuning\n", dev_path(dev));
  98        status = pci_read_config32(dev, cap + PCI_X_STATUS);
  99        orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
 100
 101        max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
 102        max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
 103
 104        /* Errata #49 don't allow 4K transactions */
 105        if (max_read >= 2) {
 106                max_read = 2;
 107        }
 108
 109        /* Errata #37 Limit the number of split transactions to avoid starvation */
 110        if (sibs >= 2) {
 111                /* At most 2 outstanding split transactions when we have
 112                 * 3 or more bus master devices on the bus.
 113                 */
 114                if (max_tran > 1) {
 115                        max_tran = 1;
 116                }
 117        }
 118        else if (sibs == 1) {
 119                /* At most 4 outstanding split transactions when we have
 120                 * 2 bus master devices on the bus.
 121                 */
 122                if (max_tran > 3) {
 123                        max_tran = 3;
 124                }
 125        }
 126        else {
 127                /* At most 8 outstanding split transactions when we have
 128                 * only one bus master device on the bus.
 129                 */
 130                if (max_tran > 4) {
 131                        max_tran = 4;
 132                }
 133        }
 134        /* Errata #56 additional limits when the bus runs at 133Mhz */
 135        if (info->errata_56 &&
 136                (PCI_X_SSTATUS_MFREQ(info->sstatus) == PCI_X_SSTATUS_MODE1_133MHZ))
 137        {
 138                unsigned limit_read;
 139                /* Look at the number of siblings and compute the
 140                 * largest legal read size.
 141                 */
 142                if (sib_funcs == 0) {
 143                        /* 2k reads */
 144                        limit_read = 2;
 145                }
 146                else if (sib_funcs <= 1) {
 147                        /* 1k reads */
 148                        limit_read = 1;
 149                }
 150                else {
 151                        /* 512 byte reads */
 152                        limit_read = 0;
 153                }
 154                if (max_read > limit_read) {
 155                        max_read = limit_read;
 156                }
 157                /* Look at the read size and the nubmer of siblings
 158                 * and compute how many outstanding transactions I can have.
 159                 */
 160                if (max_read == 2) {
 161                        /* 2K reads */
 162                        if (max_tran > 0) {
 163                                /* Only 1 outstanding transaction allowed */
 164                                max_tran = 0;
 165                        }
 166                }
 167                else if (max_read == 1) {
 168                        /* 1K reads */
 169                        if (max_tran > (1 - sib_funcs)) {
 170                                /* At most 2 outstanding transactions */
 171                                max_tran = 1 - sib_funcs;
 172                        }
 173                }
 174                else {
 175                        /* 512 byte reads */
 176                        max_read = 0;
 177                        if (max_tran > (2 - sib_funcs)) {
 178                                /* At most 3 outstanding transactions */
 179                                max_tran = 2 - sib_funcs;
 180                        }
 181                }
 182        }
 183#if 0
 184        printk(BIOS_DEBUG, "%s max_read: %d max_tran: %d sibs: %d sib_funcs: %d\n",
 185                dev_path(dev), max_read, max_tran, sibs, sib_funcs, sib_funcs);
 186#endif
 187        if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
 188                cmd &= ~PCI_X_CMD_MAX_READ;
 189                cmd |= max_read << 2;
 190                }
 191        if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
 192                cmd &= ~PCI_X_CMD_MAX_SPLIT;
 193                cmd |= max_tran << 4;
 194        }
 195
 196        /* Don't attempt to handle PCI-X errors */
 197        cmd &= ~PCI_X_CMD_DPERR_E;
 198        /* The 8131 does not work properly with relax ordering enabled.
 199         * Errata #58
 200         */
 201        cmd &= ~PCI_X_CMD_ERO;
 202        if (orig_cmd != cmd) {
 203                pci_write_config16(dev, cap + PCI_X_CMD, cmd);
 204        }
 205}
 206static unsigned int amd8131_scan_bus(struct bus *bus,
 207        unsigned min_devfn, unsigned max_devfn, unsigned int max)
 208{
 209        struct amd8131_bus_info info;
 210        struct bus *pbus;
 211        unsigned pos;
 212
 213
 214        /* Find the children on the bus */
 215        max = pci_scan_bus(bus, min_devfn, max_devfn, max);
 216
 217        /* Find the revision of the 8131 */
 218        info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
 219
 220        /* See which errata apply */
 221        info.errata_56 = info.rev <= 0x12;
 222
 223        /* Find the pcix capability and get the secondary bus status */
 224        pos = pci_find_capability(bus->dev, PCI_CAP_ID_PCIX);
 225        info.sstatus = pci_read_config16(bus->dev, pos + PCI_X_SEC_STATUS);
 226
 227        /* Print the PCI-X bus speed */
 228        printk(BIOS_DEBUG, "PCI: %02x: %s\n", bus->secondary, pcix_speed(info.sstatus));
 229
 230
 231        /* Examine the bus and find out how loaded it is */
 232        info.max_func = 0;
 233        info.master_devices  = 0;
 234        amd8131_walk_children(bus, amd8131_count_dev, &info);
 235
 236        /* Disable the bus if there are no devices on it or
 237         * we are running at 133Mhz and have a 4 function device.
 238         * see errata #56
 239         */
 240        if (!bus->children ||
 241                (info.errata_56 &&
 242                        (info.max_func >= 3) &&
 243                        (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_MODE1_133MHZ)))
 244        {
 245                unsigned pcix_misc;
 246                /* Disable all of my children */
 247                disable_children(bus);
 248
 249                /* Remember the device is disabled */
 250                bus->dev->enabled = 0;
 251
 252                /* Disable the PCI-X clocks */
 253                pcix_misc = pci_read_config32(bus->dev, 0x40);
 254                pcix_misc &= ~(0x1f << 16);
 255                pci_write_config32(bus->dev, 0x40, pcix_misc);
 256
 257                return max;
 258        }
 259
 260        /* If we are in conventional PCI mode nothing more is necessary.
 261         */
 262        if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
 263                return max;
 264        }
 265
 266
 267        /* Tune the devices on the bus */
 268        amd8131_walk_children(bus, amd8131_pcix_tune_dev, &info);
 269
 270        /* Don't allow the 8131 or any of it's parent busses to
 271         * implement relaxed ordering.  Errata #58
 272         */
 273        for(pbus = bus; !pbus->disable_relaxed_ordering; pbus = pbus->dev->bus) {
 274                printk(BIOS_SPEW, "%s disabling relaxed ordering\n",
 275                        bus_path(pbus));
 276                pbus->disable_relaxed_ordering = 1;
 277        }
 278        return max;
 279}
 280
 281static unsigned int amd8131_scan_bridge(struct device * dev, unsigned int max)
 282{
 283        return do_pci_scan_bridge(dev, max, amd8131_scan_bus);
 284}
 285
 286
 287static void amd8131_pcix_init(struct device * dev)
 288{
 289        u32 dword;
 290        u16 word;
 291        u8 byte;
 292        int nmi_option;
 293
 294        /* Enable memory write and invalidate ??? */
 295        byte = pci_read_config8(dev, 0x04);
 296        byte |= 0x10;
 297        pci_write_config8(dev, 0x04, byte);
 298
 299        /* Set drive strength */
 300        word = pci_read_config16(dev, 0xe0);
 301        word = 0x0404;
 302        pci_write_config16(dev, 0xe0, word);
 303        word = pci_read_config16(dev, 0xe4);
 304        word = 0x0404;
 305        pci_write_config16(dev, 0xe4, word);
 306
 307        /* Set impedance */
 308        word = pci_read_config16(dev, 0xe8);
 309        word = 0x0404;
 310        pci_write_config16(dev, 0xe8, word);
 311
 312        /* Set discard unrequested prefetch data */
 313        /* Errata #51 */
 314        word = pci_read_config16(dev, 0x4c);
 315        word |= 1;
 316        pci_write_config16(dev, 0x4c, word);
 317
 318        /* Set split transaction limits */
 319        word = pci_read_config16(dev, 0xa8);
 320        pci_write_config16(dev, 0xaa, word);
 321        word = pci_read_config16(dev, 0xac);
 322        pci_write_config16(dev, 0xae, word);
 323
 324        /* Set up error reporting, enable all */
 325        /* system error enable */
 326        dword = pci_read_config32(dev, 0x04);
 327        dword |= (1<<8);
 328        pci_write_config32(dev, 0x04, dword);
 329
 330        /* system and error parity enable */
 331        dword = pci_read_config32(dev, 0x3c);
 332        dword |= (3<<16);
 333        pci_write_config32(dev, 0x3c, dword);
 334
 335        /* NMI enable */
 336        nmi_option = NMI_OFF;
 337        get_option(&nmi_option, "nmi");
 338        if(nmi_option) {
 339                dword = pci_read_config32(dev, 0x44);
 340                dword |= (1<<0);
 341                pci_write_config32(dev, 0x44, dword);
 342        }
 343
 344        /* Set up CRC flood enable */
 345        dword = pci_read_config32(dev, 0xc0);
 346        if(dword) {  /* do device A only */
 347                dword = pci_read_config32(dev, 0xc4);
 348                dword |= (1<<1);
 349                pci_write_config32(dev, 0xc4, dword);
 350                dword = pci_read_config32(dev, 0xc8);
 351                dword |= (1<<1);
 352                pci_write_config32(dev, 0xc8, dword);
 353        }
 354        return;
 355}
 356
 357struct device_operations amd8131_pcix = {
 358        .id = {.type = DEVICE_ID_PCI,
 359                {.pci = {.vendor = PCI_VENDOR_ID_AMD,
 360                         .device = PCI_DEVICE_ID_AMD_8131_PCIX}}},
 361        .constructor             = default_device_constructor,
 362        .reset_bus               = pci_bus_reset,
 363        .phase3_scan             = amd8131_scan_bridge,
 364        .phase4_read_resources   = pci_bus_read_resources,
 365        .phase4_set_resources    = pci_set_resources,
 366        .phase5_enable_resources = pci_bus_enable_resources,
 367        .phase6_init             = amd8131_pcix_init,
 368        .ops_pci                 = &pci_bus_ops_pci,
 369};
 370
 371static void ioapic_enable(struct device * dev)
 372{
 373        u32 value;
 374
 375        value = pci_read_config32(dev, 0x44);
 376        if (dev->enabled) {
 377                value |= ((1 << 1) | (1 << 0));
 378        } else {
 379                value &= ~((1 << 1) | (1 << 0));
 380        }
 381        pci_write_config32(dev, 0x44, value);
 382}
 383
 384static struct pci_operations pci_ops_pci_dev = {
 385        .set_subsystem    = pci_dev_set_subsystem,
 386};
 387
 388struct device_operations amd8131_apic = {
 389        .id = {.type = DEVICE_ID_PCI,
 390                {.pci = {.vendor = PCI_VENDOR_ID_AMD,
 391                         .device = PCI_DEVICE_ID_AMD_8131_IOAPIC}}},
 392        .constructor             = default_device_constructor,
 393        .phase3_scan             = 0,
 394        .phase3_chip_setup_dev   = ioapic_enable,
 395        .phase4_read_resources   = pci_dev_read_resources,
 396        .phase4_set_resources    = pci_set_resources,
 397        .phase5_enable_resources = pci_dev_enable_resources,
 398        .ops_pci                 = &pci_ops_pci_dev,
 399};
 400
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.