/*1* Low-level PCI config space access for OLPC systems who lack the VSA2* PCI virtualization software.3*4* Copyright © 2006 Advanced Micro Devices, Inc.5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* The AMD Geode chipset (ie: GX2 processor, cs5536 I/O companion device)12* has some I/O functions (display, southbridge, sound, USB HCIs, etc)13* that more or less behave like PCI devices, but the hardware doesn't14* directly implement the PCI configuration space headers. AMD provides15* "VSA" (Virtual System Architecture) software that emulates PCI config16* space for these devices, by trapping I/O accesses to PCI config register17* (CF8/CFC) and running some code in System Management Mode interrupt state.18* On the OLPC platform, we don't want to use that VSA code because19* (a) it slows down suspend/resume, and (b) recompiling it requires special20* compilers that are hard to get. So instead of letting the complex VSA21* code simulate the PCI config registers for the on-chip devices, we22* just simulate them the easy way, by inserting the code into the23* pci_write_config and pci_read_config path. Most of the config registers24* are read-only anyway, so the bulk of the simulation is just table lookup.25*/2627#include <linux/pci.h>28#include <linux/init.h>29#include <asm/olpc.h>30#include <asm/geode.h>31#include <asm/pci_x86.h>3233/*34* In the tables below, the first two line (8 longwords) are the35* size masks that are used when the higher level PCI code determines36* the size of the region by writing ~0 to a base address register37* and reading back the result.38*39* The following lines are the values that are read during normal40* PCI config access cycles, i.e. not after just having written41* ~0 to a base address register.42*/4344static const uint32_t lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */450x0, 0x0, 0x0, 0x0,460x0, 0x0, 0x0, 0x0,47480x281022, 0x2200005, 0x6000021, 0x80f808, /* AMD Vendor ID */490x0, 0x0, 0x0, 0x0, /* No virtual registers, hence no BAR */500x0, 0x0, 0x0, 0x28100b,510x0, 0x0, 0x0, 0x0,520x0, 0x0, 0x0, 0x0,530x0, 0x0, 0x0, 0x0,540x0, 0x0, 0x0, 0x0,55};5657static const uint32_t gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */580xfffffffd, 0x0, 0x0, 0x0,590x0, 0x0, 0x0, 0x0,60610x28100b, 0x2200005, 0x6000021, 0x80f808, /* NSC Vendor ID */620xac1d, 0x0, 0x0, 0x0, /* I/O BAR - base of virtual registers */630x0, 0x0, 0x0, 0x28100b,640x0, 0x0, 0x0, 0x0,650x0, 0x0, 0x0, 0x0,660x0, 0x0, 0x0, 0x0,670x0, 0x0, 0x0, 0x0,68};6970static const uint32_t lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */710xff000008, 0xffffc000, 0xffffc000, 0xffffc000,720xffffc000, 0x0, 0x0, 0x0,73740x20811022, 0x2200003, 0x3000000, 0x0, /* AMD Vendor ID */750xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */760xfe00c000, 0x0, 0x0, 0x30100b, /* VIP */770x0, 0x0, 0x0, 0x10e, /* INTA, IRQ14 for graphics accel */780x0, 0x0, 0x0, 0x0,790x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */800x0, 0x0, 0x0, 0x0,81};8283static const uint32_t gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */840xff800008, 0xffffc000, 0xffffc000, 0xffffc000,850x0, 0x0, 0x0, 0x0,86870x30100b, 0x2200003, 0x3000000, 0x0, /* NSC Vendor ID */880xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */890x0, 0x0, 0x0, 0x30100b,900x0, 0x0, 0x0, 0x0,910x0, 0x0, 0x0, 0x0,920x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */930x0, 0x0, 0x0, 0x0,94};9596static const uint32_t aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */970xffffc000, 0x0, 0x0, 0x0,980x0, 0x0, 0x0, 0x0,991000x20821022, 0x2a00006, 0x10100000, 0x8, /* NSC Vendor ID */1010xfe010000, 0x0, 0x0, 0x0, /* AES registers */1020x0, 0x0, 0x0, 0x20821022,1030x0, 0x0, 0x0, 0x0,1040x0, 0x0, 0x0, 0x0,1050x0, 0x0, 0x0, 0x0,1060x0, 0x0, 0x0, 0x0,107};108109110static const uint32_t isa_hdr[] = { /* dev f function 0 - devfn = 78 */1110xfffffff9, 0xffffff01, 0xffffffc1, 0xffffffe1,1120xffffff81, 0xffffffc1, 0x0, 0x0,1131140x20901022, 0x2a00049, 0x6010003, 0x802000,1150x18b1, 0x1001, 0x1801, 0x1881, /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */1160x1401, 0x1841, 0x0, 0x20901022, /* PMS-128 ACPI-64 */1170x0, 0x0, 0x0, 0x0,1180x0, 0x0, 0x0, 0x0,1190x0, 0x0, 0x0, 0xaa5b, /* IRQ steering */1200x0, 0x0, 0x0, 0x0,121};122123static const uint32_t ac97_hdr[] = { /* dev f function 3 - devfn = 7b */1240xffffff81, 0x0, 0x0, 0x0,1250x0, 0x0, 0x0, 0x0,1261270x20931022, 0x2a00041, 0x4010001, 0x0,1280x1481, 0x0, 0x0, 0x0, /* I/O BAR-128 */1290x0, 0x0, 0x0, 0x20931022,1300x0, 0x0, 0x0, 0x205, /* IntB, IRQ5 */1310x0, 0x0, 0x0, 0x0,1320x0, 0x0, 0x0, 0x0,1330x0, 0x0, 0x0, 0x0,134};135136static const uint32_t ohci_hdr[] = { /* dev f function 4 - devfn = 7c */1370xfffff000, 0x0, 0x0, 0x0,1380x0, 0x0, 0x0, 0x0,1391400x20941022, 0x2300006, 0xc031002, 0x0,1410xfe01a000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */1420x0, 0x0, 0x0, 0x20941022,1430x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */1440xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O,14544 is mask 8103 (power control) */1460x0, 0x0, 0x0, 0x0,1470x0, 0x0, 0x0, 0x0,148};149150static const uint32_t ehci_hdr[] = { /* dev f function 4 - devfn = 7d */1510xfffff000, 0x0, 0x0, 0x0,1520x0, 0x0, 0x0, 0x0,1531540x20951022, 0x2300006, 0xc032002, 0x0,1550xfe01b000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */1560x0, 0x0, 0x0, 0x20951022,1570x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */1580xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 44 is159mask 8103 (power control) */160#if 01610x1, 0x40080000, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */162#endif1630x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */1640x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O),16561 FLADJ (R/W), PORTWAKECAP */166};167168static uint32_t ff_loc = ~0;169static uint32_t zero_loc;170static int bar_probing; /* Set after a write of ~0 to a BAR */171static int is_lx;172173#define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */174#define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */175176static int is_simulated(unsigned int bus, unsigned int devfn)177{178return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) ||179(PCI_SLOT(devfn) == SB_SLOT)));180}181182static uint32_t *hdr_addr(const uint32_t *hdr, int reg)183{184uint32_t addr;185186/*187* This is a little bit tricky. The header maps consist of188* 0x20 bytes of size masks, followed by 0x70 bytes of header data.189* In the normal case, when not probing a BAR's size, we want190* to access the header data, so we add 0x20 to the reg offset,191* thus skipping the size mask area.192* In the BAR probing case, we want to access the size mask for193* the BAR, so we subtract 0x10 (the config header offset for194* BAR0), and don't skip the size mask area.195*/196197addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20);198199bar_probing = 0;200return (uint32_t *)addr;201}202203static int pci_olpc_read(unsigned int seg, unsigned int bus,204unsigned int devfn, int reg, int len, uint32_t *value)205{206uint32_t *addr;207208/* Use the hardware mechanism for non-simulated devices */209if (!is_simulated(bus, devfn))210return pci_direct_conf1.read(seg, bus, devfn, reg, len, value);211212/*213* No device has config registers past 0x70, so we save table space214* by not storing entries for the nonexistent registers215*/216if (reg >= 0x70)217addr = &zero_loc;218else {219switch (devfn) {220case 0x8:221addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg);222break;223case 0x9:224addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg);225break;226case 0xa:227addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc;228break;229case 0x78:230addr = hdr_addr(isa_hdr, reg);231break;232case 0x7b:233addr = hdr_addr(ac97_hdr, reg);234break;235case 0x7c:236addr = hdr_addr(ohci_hdr, reg);237break;238case 0x7d:239addr = hdr_addr(ehci_hdr, reg);240break;241default:242addr = &ff_loc;243break;244}245}246switch (len) {247case 1:248*value = *(uint8_t *)addr;249break;250case 2:251*value = *(uint16_t *)addr;252break;253case 4:254*value = *addr;255break;256default:257BUG();258}259260return 0;261}262263static int pci_olpc_write(unsigned int seg, unsigned int bus,264unsigned int devfn, int reg, int len, uint32_t value)265{266/* Use the hardware mechanism for non-simulated devices */267if (!is_simulated(bus, devfn))268return pci_direct_conf1.write(seg, bus, devfn, reg, len, value);269270/* XXX we may want to extend this to simulate EHCI power management */271272/*273* Mostly we just discard writes, but if the write is a size probe274* (i.e. writing ~0 to a BAR), we remember it and arrange to return275* the appropriate size mask on the next read. This is cheating276* to some extent, because it depends on the fact that the next277* access after such a write will always be a read to the same BAR.278*/279280if ((reg >= 0x10) && (reg < 0x2c)) {281/* write is to a BAR */282if (value == ~0)283bar_probing = 1;284} else {285/*286* No warning on writes to ROM BAR, CMD, LATENCY_TIMER,287* CACHE_LINE_SIZE, or PM registers.288*/289if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) &&290(reg != PCI_LATENCY_TIMER) &&291(reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44))292printk(KERN_WARNING "OLPC PCI: Config write to devfn"293" %x reg %x value %x\n", devfn, reg, value);294}295296return 0;297}298299static struct pci_raw_ops pci_olpc_conf = {300.read = pci_olpc_read,301.write = pci_olpc_write,302};303304int __init pci_olpc_init(void)305{306printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n");307raw_pci_ops = &pci_olpc_conf;308is_lx = is_geode_lx();309return 0;310}311312313