162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Low-level PCI config space access for OLPC systems who lack the VSA 462306a36Sopenharmony_ci * PCI virtualization software. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright © 2006 Advanced Micro Devices, Inc. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * The AMD Geode chipset (ie: GX2 processor, cs5536 I/O companion device) 962306a36Sopenharmony_ci * has some I/O functions (display, southbridge, sound, USB HCIs, etc) 1062306a36Sopenharmony_ci * that more or less behave like PCI devices, but the hardware doesn't 1162306a36Sopenharmony_ci * directly implement the PCI configuration space headers. AMD provides 1262306a36Sopenharmony_ci * "VSA" (Virtual System Architecture) software that emulates PCI config 1362306a36Sopenharmony_ci * space for these devices, by trapping I/O accesses to PCI config register 1462306a36Sopenharmony_ci * (CF8/CFC) and running some code in System Management Mode interrupt state. 1562306a36Sopenharmony_ci * On the OLPC platform, we don't want to use that VSA code because 1662306a36Sopenharmony_ci * (a) it slows down suspend/resume, and (b) recompiling it requires special 1762306a36Sopenharmony_ci * compilers that are hard to get. So instead of letting the complex VSA 1862306a36Sopenharmony_ci * code simulate the PCI config registers for the on-chip devices, we 1962306a36Sopenharmony_ci * just simulate them the easy way, by inserting the code into the 2062306a36Sopenharmony_ci * pci_write_config and pci_read_config path. Most of the config registers 2162306a36Sopenharmony_ci * are read-only anyway, so the bulk of the simulation is just table lookup. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#include <linux/pci.h> 2562306a36Sopenharmony_ci#include <linux/init.h> 2662306a36Sopenharmony_ci#include <asm/olpc.h> 2762306a36Sopenharmony_ci#include <asm/geode.h> 2862306a36Sopenharmony_ci#include <asm/pci_x86.h> 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/* 3162306a36Sopenharmony_ci * In the tables below, the first two line (8 longwords) are the 3262306a36Sopenharmony_ci * size masks that are used when the higher level PCI code determines 3362306a36Sopenharmony_ci * the size of the region by writing ~0 to a base address register 3462306a36Sopenharmony_ci * and reading back the result. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * The following lines are the values that are read during normal 3762306a36Sopenharmony_ci * PCI config access cycles, i.e. not after just having written 3862306a36Sopenharmony_ci * ~0 to a base address register. 3962306a36Sopenharmony_ci */ 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic const uint32_t lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */ 4262306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 4362306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci 0x281022, 0x2200005, 0x6000021, 0x80f808, /* AMD Vendor ID */ 4662306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, /* No virtual registers, hence no BAR */ 4762306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x28100b, 4862306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 4962306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 5062306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 5162306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 5262306a36Sopenharmony_ci}; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic const uint32_t gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */ 5562306a36Sopenharmony_ci 0xfffffffd, 0x0, 0x0, 0x0, 5662306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci 0x28100b, 0x2200005, 0x6000021, 0x80f808, /* NSC Vendor ID */ 5962306a36Sopenharmony_ci 0xac1d, 0x0, 0x0, 0x0, /* I/O BAR - base of virtual registers */ 6062306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x28100b, 6162306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 6262306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 6362306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 6462306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 6562306a36Sopenharmony_ci}; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_cistatic const uint32_t lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */ 6862306a36Sopenharmony_ci 0xff000008, 0xffffc000, 0xffffc000, 0xffffc000, 6962306a36Sopenharmony_ci 0xffffc000, 0x0, 0x0, 0x0, 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci 0x20811022, 0x2200003, 0x3000000, 0x0, /* AMD Vendor ID */ 7262306a36Sopenharmony_ci 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */ 7362306a36Sopenharmony_ci 0xfe00c000, 0x0, 0x0, 0x30100b, /* VIP */ 7462306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x10e, /* INTA, IRQ14 for graphics accel */ 7562306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 7662306a36Sopenharmony_ci 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */ 7762306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 7862306a36Sopenharmony_ci}; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cistatic const uint32_t gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */ 8162306a36Sopenharmony_ci 0xff800008, 0xffffc000, 0xffffc000, 0xffffc000, 8262306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci 0x30100b, 0x2200003, 0x3000000, 0x0, /* NSC Vendor ID */ 8562306a36Sopenharmony_ci 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */ 8662306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x30100b, 8762306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 8862306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 8962306a36Sopenharmony_ci 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */ 9062306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 9162306a36Sopenharmony_ci}; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_cistatic const uint32_t aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */ 9462306a36Sopenharmony_ci 0xffffc000, 0x0, 0x0, 0x0, 9562306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci 0x20821022, 0x2a00006, 0x10100000, 0x8, /* NSC Vendor ID */ 9862306a36Sopenharmony_ci 0xfe010000, 0x0, 0x0, 0x0, /* AES registers */ 9962306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x20821022, 10062306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 10162306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 10262306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 10362306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 10462306a36Sopenharmony_ci}; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_cistatic const uint32_t isa_hdr[] = { /* dev f function 0 - devfn = 78 */ 10862306a36Sopenharmony_ci 0xfffffff9, 0xffffff01, 0xffffffc1, 0xffffffe1, 10962306a36Sopenharmony_ci 0xffffff81, 0xffffffc1, 0x0, 0x0, 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci 0x20901022, 0x2a00049, 0x6010003, 0x802000, 11262306a36Sopenharmony_ci 0x18b1, 0x1001, 0x1801, 0x1881, /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */ 11362306a36Sopenharmony_ci 0x1401, 0x1841, 0x0, 0x20901022, /* PMS-128 ACPI-64 */ 11462306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 11562306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 11662306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0xaa5b, /* IRQ steering */ 11762306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 11862306a36Sopenharmony_ci}; 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_cistatic const uint32_t ac97_hdr[] = { /* dev f function 3 - devfn = 7b */ 12162306a36Sopenharmony_ci 0xffffff81, 0x0, 0x0, 0x0, 12262306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci 0x20931022, 0x2a00041, 0x4010001, 0x0, 12562306a36Sopenharmony_ci 0x1481, 0x0, 0x0, 0x0, /* I/O BAR-128 */ 12662306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x20931022, 12762306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x205, /* IntB, IRQ5 */ 12862306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 12962306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 13062306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 13162306a36Sopenharmony_ci}; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cistatic const uint32_t ohci_hdr[] = { /* dev f function 4 - devfn = 7c */ 13462306a36Sopenharmony_ci 0xfffff000, 0x0, 0x0, 0x0, 13562306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci 0x20941022, 0x2300006, 0xc031002, 0x0, 13862306a36Sopenharmony_ci 0xfe01a000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */ 13962306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x20941022, 14062306a36Sopenharmony_ci 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */ 14162306a36Sopenharmony_ci 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 14262306a36Sopenharmony_ci 44 is mask 8103 (power control) */ 14362306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 14462306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 14562306a36Sopenharmony_ci}; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_cistatic const uint32_t ehci_hdr[] = { /* dev f function 4 - devfn = 7d */ 14862306a36Sopenharmony_ci 0xfffff000, 0x0, 0x0, 0x0, 14962306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x0, 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci 0x20951022, 0x2300006, 0xc032002, 0x0, 15262306a36Sopenharmony_ci 0xfe01b000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */ 15362306a36Sopenharmony_ci 0x0, 0x0, 0x0, 0x20951022, 15462306a36Sopenharmony_ci 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */ 15562306a36Sopenharmony_ci 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 44 is 15662306a36Sopenharmony_ci mask 8103 (power control) */ 15762306a36Sopenharmony_ci#if 0 15862306a36Sopenharmony_ci 0x1, 0x40080000, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */ 15962306a36Sopenharmony_ci#endif 16062306a36Sopenharmony_ci 0x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */ 16162306a36Sopenharmony_ci 0x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O), 16262306a36Sopenharmony_ci 61 FLADJ (R/W), PORTWAKECAP */ 16362306a36Sopenharmony_ci}; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_cistatic uint32_t ff_loc = ~0; 16662306a36Sopenharmony_cistatic uint32_t zero_loc; 16762306a36Sopenharmony_cistatic int bar_probing; /* Set after a write of ~0 to a BAR */ 16862306a36Sopenharmony_cistatic int is_lx; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci#define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */ 17162306a36Sopenharmony_ci#define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */ 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_cistatic int is_simulated(unsigned int bus, unsigned int devfn) 17462306a36Sopenharmony_ci{ 17562306a36Sopenharmony_ci return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) || 17662306a36Sopenharmony_ci (PCI_SLOT(devfn) == SB_SLOT))); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_cistatic uint32_t *hdr_addr(const uint32_t *hdr, int reg) 18062306a36Sopenharmony_ci{ 18162306a36Sopenharmony_ci uint32_t addr; 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci /* 18462306a36Sopenharmony_ci * This is a little bit tricky. The header maps consist of 18562306a36Sopenharmony_ci * 0x20 bytes of size masks, followed by 0x70 bytes of header data. 18662306a36Sopenharmony_ci * In the normal case, when not probing a BAR's size, we want 18762306a36Sopenharmony_ci * to access the header data, so we add 0x20 to the reg offset, 18862306a36Sopenharmony_ci * thus skipping the size mask area. 18962306a36Sopenharmony_ci * In the BAR probing case, we want to access the size mask for 19062306a36Sopenharmony_ci * the BAR, so we subtract 0x10 (the config header offset for 19162306a36Sopenharmony_ci * BAR0), and don't skip the size mask area. 19262306a36Sopenharmony_ci */ 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20); 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci bar_probing = 0; 19762306a36Sopenharmony_ci return (uint32_t *)addr; 19862306a36Sopenharmony_ci} 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_cistatic int pci_olpc_read(unsigned int seg, unsigned int bus, 20162306a36Sopenharmony_ci unsigned int devfn, int reg, int len, uint32_t *value) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci uint32_t *addr; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci WARN_ON(seg); 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci /* Use the hardware mechanism for non-simulated devices */ 20862306a36Sopenharmony_ci if (!is_simulated(bus, devfn)) 20962306a36Sopenharmony_ci return pci_direct_conf1.read(seg, bus, devfn, reg, len, value); 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci /* 21262306a36Sopenharmony_ci * No device has config registers past 0x70, so we save table space 21362306a36Sopenharmony_ci * by not storing entries for the nonexistent registers 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci if (reg >= 0x70) 21662306a36Sopenharmony_ci addr = &zero_loc; 21762306a36Sopenharmony_ci else { 21862306a36Sopenharmony_ci switch (devfn) { 21962306a36Sopenharmony_ci case 0x8: 22062306a36Sopenharmony_ci addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg); 22162306a36Sopenharmony_ci break; 22262306a36Sopenharmony_ci case 0x9: 22362306a36Sopenharmony_ci addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg); 22462306a36Sopenharmony_ci break; 22562306a36Sopenharmony_ci case 0xa: 22662306a36Sopenharmony_ci addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc; 22762306a36Sopenharmony_ci break; 22862306a36Sopenharmony_ci case 0x78: 22962306a36Sopenharmony_ci addr = hdr_addr(isa_hdr, reg); 23062306a36Sopenharmony_ci break; 23162306a36Sopenharmony_ci case 0x7b: 23262306a36Sopenharmony_ci addr = hdr_addr(ac97_hdr, reg); 23362306a36Sopenharmony_ci break; 23462306a36Sopenharmony_ci case 0x7c: 23562306a36Sopenharmony_ci addr = hdr_addr(ohci_hdr, reg); 23662306a36Sopenharmony_ci break; 23762306a36Sopenharmony_ci case 0x7d: 23862306a36Sopenharmony_ci addr = hdr_addr(ehci_hdr, reg); 23962306a36Sopenharmony_ci break; 24062306a36Sopenharmony_ci default: 24162306a36Sopenharmony_ci addr = &ff_loc; 24262306a36Sopenharmony_ci break; 24362306a36Sopenharmony_ci } 24462306a36Sopenharmony_ci } 24562306a36Sopenharmony_ci switch (len) { 24662306a36Sopenharmony_ci case 1: 24762306a36Sopenharmony_ci *value = *(uint8_t *)addr; 24862306a36Sopenharmony_ci break; 24962306a36Sopenharmony_ci case 2: 25062306a36Sopenharmony_ci *value = *(uint16_t *)addr; 25162306a36Sopenharmony_ci break; 25262306a36Sopenharmony_ci case 4: 25362306a36Sopenharmony_ci *value = *addr; 25462306a36Sopenharmony_ci break; 25562306a36Sopenharmony_ci default: 25662306a36Sopenharmony_ci BUG(); 25762306a36Sopenharmony_ci } 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci return 0; 26062306a36Sopenharmony_ci} 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_cistatic int pci_olpc_write(unsigned int seg, unsigned int bus, 26362306a36Sopenharmony_ci unsigned int devfn, int reg, int len, uint32_t value) 26462306a36Sopenharmony_ci{ 26562306a36Sopenharmony_ci WARN_ON(seg); 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci /* Use the hardware mechanism for non-simulated devices */ 26862306a36Sopenharmony_ci if (!is_simulated(bus, devfn)) 26962306a36Sopenharmony_ci return pci_direct_conf1.write(seg, bus, devfn, reg, len, value); 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci /* XXX we may want to extend this to simulate EHCI power management */ 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci /* 27462306a36Sopenharmony_ci * Mostly we just discard writes, but if the write is a size probe 27562306a36Sopenharmony_ci * (i.e. writing ~0 to a BAR), we remember it and arrange to return 27662306a36Sopenharmony_ci * the appropriate size mask on the next read. This is cheating 27762306a36Sopenharmony_ci * to some extent, because it depends on the fact that the next 27862306a36Sopenharmony_ci * access after such a write will always be a read to the same BAR. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci if ((reg >= 0x10) && (reg < 0x2c)) { 28262306a36Sopenharmony_ci /* write is to a BAR */ 28362306a36Sopenharmony_ci if (value == ~0) 28462306a36Sopenharmony_ci bar_probing = 1; 28562306a36Sopenharmony_ci } else { 28662306a36Sopenharmony_ci /* 28762306a36Sopenharmony_ci * No warning on writes to ROM BAR, CMD, LATENCY_TIMER, 28862306a36Sopenharmony_ci * CACHE_LINE_SIZE, or PM registers. 28962306a36Sopenharmony_ci */ 29062306a36Sopenharmony_ci if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) && 29162306a36Sopenharmony_ci (reg != PCI_LATENCY_TIMER) && 29262306a36Sopenharmony_ci (reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44)) 29362306a36Sopenharmony_ci printk(KERN_WARNING "OLPC PCI: Config write to devfn" 29462306a36Sopenharmony_ci " %x reg %x value %x\n", devfn, reg, value); 29562306a36Sopenharmony_ci } 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci return 0; 29862306a36Sopenharmony_ci} 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_cistatic const struct pci_raw_ops pci_olpc_conf = { 30162306a36Sopenharmony_ci .read = pci_olpc_read, 30262306a36Sopenharmony_ci .write = pci_olpc_write, 30362306a36Sopenharmony_ci}; 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ciint __init pci_olpc_init(void) 30662306a36Sopenharmony_ci{ 30762306a36Sopenharmony_ci printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n"); 30862306a36Sopenharmony_ci raw_pci_ops = &pci_olpc_conf; 30962306a36Sopenharmony_ci is_lx = is_geode_lx(); 31062306a36Sopenharmony_ci return 0; 31162306a36Sopenharmony_ci} 312