// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Low-level PCI config space access for OLPC systems who lack the VSA3* PCI virtualization software.4*5* Copyright © 2006 Advanced Micro Devices, Inc.6*7* The AMD Geode chipset (ie: GX2 processor, cs5536 I/O companion device)8* has some I/O functions (display, southbridge, sound, USB HCIs, etc)9* that more or less behave like PCI devices, but the hardware doesn't10* directly implement the PCI configuration space headers. AMD provides11* "VSA" (Virtual System Architecture) software that emulates PCI config12* space for these devices, by trapping I/O accesses to PCI config register13* (CF8/CFC) and running some code in System Management Mode interrupt state.14* On the OLPC platform, we don't want to use that VSA code because15* (a) it slows down suspend/resume, and (b) recompiling it requires special16* compilers that are hard to get. So instead of letting the complex VSA17* code simulate the PCI config registers for the on-chip devices, we18* just simulate them the easy way, by inserting the code into the19* pci_write_config and pci_read_config path. Most of the config registers20* are read-only anyway, so the bulk of the simulation is just table lookup.21*/2223#include <linux/pci.h>24#include <linux/init.h>25#include <asm/olpc.h>26#include <asm/geode.h>27#include <asm/pci_x86.h>2829/*30* In the tables below, the first two line (8 longwords) are the31* size masks that are used when the higher level PCI code determines32* the size of the region by writing ~0 to a base address register33* and reading back the result.34*35* The following lines are the values that are read during normal36* PCI config access cycles, i.e. not after just having written37* ~0 to a base address register.38*/3940static const uint32_t lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */410x0, 0x0, 0x0, 0x0,420x0, 0x0, 0x0, 0x0,43440x281022, 0x2200005, 0x6000021, 0x80f808, /* AMD Vendor ID */450x0, 0x0, 0x0, 0x0, /* No virtual registers, hence no BAR */460x0, 0x0, 0x0, 0x28100b,470x0, 0x0, 0x0, 0x0,480x0, 0x0, 0x0, 0x0,490x0, 0x0, 0x0, 0x0,500x0, 0x0, 0x0, 0x0,51};5253static const uint32_t gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */540xfffffffd, 0x0, 0x0, 0x0,550x0, 0x0, 0x0, 0x0,56570x28100b, 0x2200005, 0x6000021, 0x80f808, /* NSC Vendor ID */580xac1d, 0x0, 0x0, 0x0, /* I/O BAR - base of virtual registers */590x0, 0x0, 0x0, 0x28100b,600x0, 0x0, 0x0, 0x0,610x0, 0x0, 0x0, 0x0,620x0, 0x0, 0x0, 0x0,630x0, 0x0, 0x0, 0x0,64};6566static const uint32_t lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */670xff000008, 0xffffc000, 0xffffc000, 0xffffc000,680xffffc000, 0x0, 0x0, 0x0,69700x20811022, 0x2200003, 0x3000000, 0x0, /* AMD Vendor ID */710xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */720xfe00c000, 0x0, 0x0, 0x30100b, /* VIP */730x0, 0x0, 0x0, 0x10e, /* INTA, IRQ14 for graphics accel */740x0, 0x0, 0x0, 0x0,750x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */760x0, 0x0, 0x0, 0x0,77};7879static const uint32_t gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */800xff800008, 0xffffc000, 0xffffc000, 0xffffc000,810x0, 0x0, 0x0, 0x0,82830x30100b, 0x2200003, 0x3000000, 0x0, /* NSC Vendor ID */840xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */850x0, 0x0, 0x0, 0x30100b,860x0, 0x0, 0x0, 0x0,870x0, 0x0, 0x0, 0x0,880x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */890x0, 0x0, 0x0, 0x0,90};9192static const uint32_t aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */930xffffc000, 0x0, 0x0, 0x0,940x0, 0x0, 0x0, 0x0,95960x20821022, 0x2a00006, 0x10100000, 0x8, /* NSC Vendor ID */970xfe010000, 0x0, 0x0, 0x0, /* AES registers */980x0, 0x0, 0x0, 0x20821022,990x0, 0x0, 0x0, 0x0,1000x0, 0x0, 0x0, 0x0,1010x0, 0x0, 0x0, 0x0,1020x0, 0x0, 0x0, 0x0,103};104105106static const uint32_t isa_hdr[] = { /* dev f function 0 - devfn = 78 */1070xfffffff9, 0xffffff01, 0xffffffc1, 0xffffffe1,1080xffffff81, 0xffffffc1, 0x0, 0x0,1091100x20901022, 0x2a00049, 0x6010003, 0x802000,1110x18b1, 0x1001, 0x1801, 0x1881, /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */1120x1401, 0x1841, 0x0, 0x20901022, /* PMS-128 ACPI-64 */1130x0, 0x0, 0x0, 0x0,1140x0, 0x0, 0x0, 0x0,1150x0, 0x0, 0x0, 0xaa5b, /* IRQ steering */1160x0, 0x0, 0x0, 0x0,117};118119static const uint32_t ac97_hdr[] = { /* dev f function 3 - devfn = 7b */1200xffffff81, 0x0, 0x0, 0x0,1210x0, 0x0, 0x0, 0x0,1221230x20931022, 0x2a00041, 0x4010001, 0x0,1240x1481, 0x0, 0x0, 0x0, /* I/O BAR-128 */1250x0, 0x0, 0x0, 0x20931022,1260x0, 0x0, 0x0, 0x205, /* IntB, IRQ5 */1270x0, 0x0, 0x0, 0x0,1280x0, 0x0, 0x0, 0x0,1290x0, 0x0, 0x0, 0x0,130};131132static const uint32_t ohci_hdr[] = { /* dev f function 4 - devfn = 7c */1330xfffff000, 0x0, 0x0, 0x0,1340x0, 0x0, 0x0, 0x0,1351360x20941022, 0x2300006, 0xc031002, 0x0,1370xfe01a000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */1380x0, 0x0, 0x0, 0x20941022,1390x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */1400xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O,14144 is mask 8103 (power control) */1420x0, 0x0, 0x0, 0x0,1430x0, 0x0, 0x0, 0x0,144};145146static const uint32_t ehci_hdr[] = { /* dev f function 4 - devfn = 7d */1470xfffff000, 0x0, 0x0, 0x0,1480x0, 0x0, 0x0, 0x0,1491500x20951022, 0x2300006, 0xc032002, 0x0,1510xfe01b000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */1520x0, 0x0, 0x0, 0x20951022,1530x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */1540xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 44 is155mask 8103 (power control) */1560x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */1570x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O),15861 FLADJ (R/W), PORTWAKECAP */159};160161static uint32_t ff_loc = ~0;162static uint32_t zero_loc;163static int bar_probing; /* Set after a write of ~0 to a BAR */164static int is_lx;165166#define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */167#define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */168169static int is_simulated(unsigned int bus, unsigned int devfn)170{171return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) ||172(PCI_SLOT(devfn) == SB_SLOT)));173}174175static uint32_t *hdr_addr(const uint32_t *hdr, int reg)176{177uint32_t addr;178179/*180* This is a little bit tricky. The header maps consist of181* 0x20 bytes of size masks, followed by 0x70 bytes of header data.182* In the normal case, when not probing a BAR's size, we want183* to access the header data, so we add 0x20 to the reg offset,184* thus skipping the size mask area.185* In the BAR probing case, we want to access the size mask for186* the BAR, so we subtract 0x10 (the config header offset for187* BAR0), and don't skip the size mask area.188*/189190addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20);191192bar_probing = 0;193return (uint32_t *)addr;194}195196static int pci_olpc_read(unsigned int seg, unsigned int bus,197unsigned int devfn, int reg, int len, uint32_t *value)198{199uint32_t *addr;200201WARN_ON(seg);202203/* Use the hardware mechanism for non-simulated devices */204if (!is_simulated(bus, devfn))205return pci_direct_conf1.read(seg, bus, devfn, reg, len, value);206207/*208* No device has config registers past 0x70, so we save table space209* by not storing entries for the nonexistent registers210*/211if (reg >= 0x70)212addr = &zero_loc;213else {214switch (devfn) {215case 0x8:216addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg);217break;218case 0x9:219addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg);220break;221case 0xa:222addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc;223break;224case 0x78:225addr = hdr_addr(isa_hdr, reg);226break;227case 0x7b:228addr = hdr_addr(ac97_hdr, reg);229break;230case 0x7c:231addr = hdr_addr(ohci_hdr, reg);232break;233case 0x7d:234addr = hdr_addr(ehci_hdr, reg);235break;236default:237addr = &ff_loc;238break;239}240}241switch (len) {242case 1:243*value = *(uint8_t *)addr;244break;245case 2:246*value = *(uint16_t *)addr;247break;248case 4:249*value = *addr;250break;251default:252BUG();253}254255return 0;256}257258static int pci_olpc_write(unsigned int seg, unsigned int bus,259unsigned int devfn, int reg, int len, uint32_t value)260{261WARN_ON(seg);262263/* Use the hardware mechanism for non-simulated devices */264if (!is_simulated(bus, devfn))265return pci_direct_conf1.write(seg, bus, devfn, reg, len, value);266267/* XXX we may want to extend this to simulate EHCI power management */268269/*270* Mostly we just discard writes, but if the write is a size probe271* (i.e. writing ~0 to a BAR), we remember it and arrange to return272* the appropriate size mask on the next read. This is cheating273* to some extent, because it depends on the fact that the next274* access after such a write will always be a read to the same BAR.275*/276277if ((reg >= 0x10) && (reg < 0x2c)) {278/* write is to a BAR */279if (value == ~0)280bar_probing = 1;281} else {282/*283* No warning on writes to ROM BAR, CMD, LATENCY_TIMER,284* CACHE_LINE_SIZE, or PM registers.285*/286if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) &&287(reg != PCI_LATENCY_TIMER) &&288(reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44))289printk(KERN_WARNING "OLPC PCI: Config write to devfn"290" %x reg %x value %x\n", devfn, reg, value);291}292293return 0;294}295296static const struct pci_raw_ops pci_olpc_conf = {297.read = pci_olpc_read,298.write = pci_olpc_write,299};300301int __init pci_olpc_init(void)302{303printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n");304raw_pci_ops = &pci_olpc_conf;305is_lx = is_geode_lx();306return 0;307}308309310