162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright(c) 2010 Intel Corporation. All rights reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Contact Information: 662306a36Sopenharmony_ci * Intel Corporation 762306a36Sopenharmony_ci * 2200 Mission College Blvd. 862306a36Sopenharmony_ci * Santa Clara, CA 97052 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * This provides access methods for PCI registers that mis-behave on 1162306a36Sopenharmony_ci * the CE4100. Each register can be assigned a private init, read and 1262306a36Sopenharmony_ci * write routine. The exception to this is the bridge device. The 1362306a36Sopenharmony_ci * bridge device is the only device on bus zero (0) that requires any 1462306a36Sopenharmony_ci * fixup so it is a special case ATM 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <linux/kernel.h> 1862306a36Sopenharmony_ci#include <linux/pci.h> 1962306a36Sopenharmony_ci#include <linux/init.h> 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#include <asm/ce4100.h> 2262306a36Sopenharmony_ci#include <asm/pci_x86.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistruct sim_reg { 2562306a36Sopenharmony_ci u32 value; 2662306a36Sopenharmony_ci u32 mask; 2762306a36Sopenharmony_ci}; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistruct sim_dev_reg { 3062306a36Sopenharmony_ci int dev_func; 3162306a36Sopenharmony_ci int reg; 3262306a36Sopenharmony_ci void (*init)(struct sim_dev_reg *reg); 3362306a36Sopenharmony_ci void (*read)(struct sim_dev_reg *reg, u32 *value); 3462306a36Sopenharmony_ci void (*write)(struct sim_dev_reg *reg, u32 value); 3562306a36Sopenharmony_ci struct sim_reg sim_reg; 3662306a36Sopenharmony_ci}; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistruct sim_reg_op { 3962306a36Sopenharmony_ci void (*init)(struct sim_dev_reg *reg); 4062306a36Sopenharmony_ci void (*read)(struct sim_dev_reg *reg, u32 value); 4162306a36Sopenharmony_ci void (*write)(struct sim_dev_reg *reg, u32 value); 4262306a36Sopenharmony_ci}; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#define MB (1024 * 1024) 4562306a36Sopenharmony_ci#define KB (1024) 4662306a36Sopenharmony_ci#define SIZE_TO_MASK(size) (~(size - 1)) 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci#define DEFINE_REG(device, func, offset, size, init_op, read_op, write_op)\ 4962306a36Sopenharmony_ci{ PCI_DEVFN(device, func), offset, init_op, read_op, write_op,\ 5062306a36Sopenharmony_ci {0, SIZE_TO_MASK(size)} }, 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/* 5362306a36Sopenharmony_ci * All read/write functions are called with pci_config_lock held. 5462306a36Sopenharmony_ci */ 5562306a36Sopenharmony_cistatic void reg_init(struct sim_dev_reg *reg) 5662306a36Sopenharmony_ci{ 5762306a36Sopenharmony_ci pci_direct_conf1.read(0, 1, reg->dev_func, reg->reg, 4, 5862306a36Sopenharmony_ci ®->sim_reg.value); 5962306a36Sopenharmony_ci} 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_cistatic void reg_read(struct sim_dev_reg *reg, u32 *value) 6262306a36Sopenharmony_ci{ 6362306a36Sopenharmony_ci *value = reg->sim_reg.value; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic void reg_write(struct sim_dev_reg *reg, u32 value) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci reg->sim_reg.value = (value & reg->sim_reg.mask) | 6962306a36Sopenharmony_ci (reg->sim_reg.value & ~reg->sim_reg.mask); 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic void sata_reg_init(struct sim_dev_reg *reg) 7362306a36Sopenharmony_ci{ 7462306a36Sopenharmony_ci pci_direct_conf1.read(0, 1, PCI_DEVFN(14, 0), 0x10, 4, 7562306a36Sopenharmony_ci ®->sim_reg.value); 7662306a36Sopenharmony_ci reg->sim_reg.value += 0x400; 7762306a36Sopenharmony_ci} 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cistatic void ehci_reg_read(struct sim_dev_reg *reg, u32 *value) 8062306a36Sopenharmony_ci{ 8162306a36Sopenharmony_ci reg_read(reg, value); 8262306a36Sopenharmony_ci if (*value != reg->sim_reg.mask) 8362306a36Sopenharmony_ci *value |= 0x100; 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic void sata_revid_init(struct sim_dev_reg *reg) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci reg->sim_reg.value = 0x01060100; 8962306a36Sopenharmony_ci reg->sim_reg.mask = 0; 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_cistatic void sata_revid_read(struct sim_dev_reg *reg, u32 *value) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci reg_read(reg, value); 9562306a36Sopenharmony_ci} 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_cistatic void reg_noirq_read(struct sim_dev_reg *reg, u32 *value) 9862306a36Sopenharmony_ci{ 9962306a36Sopenharmony_ci /* force interrupt pin value to 0 */ 10062306a36Sopenharmony_ci *value = reg->sim_reg.value & 0xfff00ff; 10162306a36Sopenharmony_ci} 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_cistatic struct sim_dev_reg bus1_fixups[] = { 10462306a36Sopenharmony_ci DEFINE_REG(2, 0, 0x10, (16*MB), reg_init, reg_read, reg_write) 10562306a36Sopenharmony_ci DEFINE_REG(2, 0, 0x14, (256), reg_init, reg_read, reg_write) 10662306a36Sopenharmony_ci DEFINE_REG(2, 1, 0x10, (64*KB), reg_init, reg_read, reg_write) 10762306a36Sopenharmony_ci DEFINE_REG(3, 0, 0x10, (64*KB), reg_init, reg_read, reg_write) 10862306a36Sopenharmony_ci DEFINE_REG(4, 0, 0x10, (128*KB), reg_init, reg_read, reg_write) 10962306a36Sopenharmony_ci DEFINE_REG(4, 1, 0x10, (128*KB), reg_init, reg_read, reg_write) 11062306a36Sopenharmony_ci DEFINE_REG(6, 0, 0x10, (512*KB), reg_init, reg_read, reg_write) 11162306a36Sopenharmony_ci DEFINE_REG(6, 1, 0x10, (512*KB), reg_init, reg_read, reg_write) 11262306a36Sopenharmony_ci DEFINE_REG(6, 2, 0x10, (64*KB), reg_init, reg_read, reg_write) 11362306a36Sopenharmony_ci DEFINE_REG(8, 0, 0x10, (1*MB), reg_init, reg_read, reg_write) 11462306a36Sopenharmony_ci DEFINE_REG(8, 1, 0x10, (64*KB), reg_init, reg_read, reg_write) 11562306a36Sopenharmony_ci DEFINE_REG(8, 2, 0x10, (64*KB), reg_init, reg_read, reg_write) 11662306a36Sopenharmony_ci DEFINE_REG(9, 0, 0x10 , (1*MB), reg_init, reg_read, reg_write) 11762306a36Sopenharmony_ci DEFINE_REG(9, 0, 0x14, (64*KB), reg_init, reg_read, reg_write) 11862306a36Sopenharmony_ci DEFINE_REG(10, 0, 0x10, (256), reg_init, reg_read, reg_write) 11962306a36Sopenharmony_ci DEFINE_REG(10, 0, 0x14, (256*MB), reg_init, reg_read, reg_write) 12062306a36Sopenharmony_ci DEFINE_REG(11, 0, 0x10, (256), reg_init, reg_read, reg_write) 12162306a36Sopenharmony_ci DEFINE_REG(11, 0, 0x14, (256), reg_init, reg_read, reg_write) 12262306a36Sopenharmony_ci DEFINE_REG(11, 1, 0x10, (256), reg_init, reg_read, reg_write) 12362306a36Sopenharmony_ci DEFINE_REG(11, 2, 0x10, (256), reg_init, reg_read, reg_write) 12462306a36Sopenharmony_ci DEFINE_REG(11, 2, 0x14, (256), reg_init, reg_read, reg_write) 12562306a36Sopenharmony_ci DEFINE_REG(11, 2, 0x18, (256), reg_init, reg_read, reg_write) 12662306a36Sopenharmony_ci DEFINE_REG(11, 3, 0x10, (256), reg_init, reg_read, reg_write) 12762306a36Sopenharmony_ci DEFINE_REG(11, 3, 0x14, (256), reg_init, reg_read, reg_write) 12862306a36Sopenharmony_ci DEFINE_REG(11, 4, 0x10, (256), reg_init, reg_read, reg_write) 12962306a36Sopenharmony_ci DEFINE_REG(11, 5, 0x10, (64*KB), reg_init, reg_read, reg_write) 13062306a36Sopenharmony_ci DEFINE_REG(11, 6, 0x10, (256), reg_init, reg_read, reg_write) 13162306a36Sopenharmony_ci DEFINE_REG(11, 7, 0x10, (64*KB), reg_init, reg_read, reg_write) 13262306a36Sopenharmony_ci DEFINE_REG(11, 7, 0x3c, 256, reg_init, reg_noirq_read, reg_write) 13362306a36Sopenharmony_ci DEFINE_REG(12, 0, 0x10, (128*KB), reg_init, reg_read, reg_write) 13462306a36Sopenharmony_ci DEFINE_REG(12, 0, 0x14, (256), reg_init, reg_read, reg_write) 13562306a36Sopenharmony_ci DEFINE_REG(12, 1, 0x10, (1024), reg_init, reg_read, reg_write) 13662306a36Sopenharmony_ci DEFINE_REG(13, 0, 0x10, (32*KB), reg_init, ehci_reg_read, reg_write) 13762306a36Sopenharmony_ci DEFINE_REG(13, 1, 0x10, (32*KB), reg_init, ehci_reg_read, reg_write) 13862306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x8, 0, sata_revid_init, sata_revid_read, 0) 13962306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x10, 0, reg_init, reg_read, reg_write) 14062306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x14, 0, reg_init, reg_read, reg_write) 14162306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x18, 0, reg_init, reg_read, reg_write) 14262306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x1C, 0, reg_init, reg_read, reg_write) 14362306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x20, 0, reg_init, reg_read, reg_write) 14462306a36Sopenharmony_ci DEFINE_REG(14, 0, 0x24, (0x200), sata_reg_init, reg_read, reg_write) 14562306a36Sopenharmony_ci DEFINE_REG(15, 0, 0x10, (64*KB), reg_init, reg_read, reg_write) 14662306a36Sopenharmony_ci DEFINE_REG(15, 0, 0x14, (64*KB), reg_init, reg_read, reg_write) 14762306a36Sopenharmony_ci DEFINE_REG(16, 0, 0x10, (64*KB), reg_init, reg_read, reg_write) 14862306a36Sopenharmony_ci DEFINE_REG(16, 0, 0x14, (64*MB), reg_init, reg_read, reg_write) 14962306a36Sopenharmony_ci DEFINE_REG(16, 0, 0x18, (64*MB), reg_init, reg_read, reg_write) 15062306a36Sopenharmony_ci DEFINE_REG(16, 0, 0x3c, 256, reg_init, reg_noirq_read, reg_write) 15162306a36Sopenharmony_ci DEFINE_REG(17, 0, 0x10, (128*KB), reg_init, reg_read, reg_write) 15262306a36Sopenharmony_ci DEFINE_REG(18, 0, 0x10, (1*KB), reg_init, reg_read, reg_write) 15362306a36Sopenharmony_ci DEFINE_REG(18, 0, 0x3c, 256, reg_init, reg_noirq_read, reg_write) 15462306a36Sopenharmony_ci}; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_cistatic void __init init_sim_regs(void) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci int i; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bus1_fixups); i++) { 16162306a36Sopenharmony_ci if (bus1_fixups[i].init) 16262306a36Sopenharmony_ci bus1_fixups[i].init(&bus1_fixups[i]); 16362306a36Sopenharmony_ci } 16462306a36Sopenharmony_ci} 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cistatic inline void extract_bytes(u32 *value, int reg, int len) 16762306a36Sopenharmony_ci{ 16862306a36Sopenharmony_ci uint32_t mask; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci *value >>= ((reg & 3) * 8); 17162306a36Sopenharmony_ci mask = 0xFFFFFFFF >> ((4 - len) * 8); 17262306a36Sopenharmony_ci *value &= mask; 17362306a36Sopenharmony_ci} 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_cistatic int bridge_read(unsigned int devfn, int reg, int len, u32 *value) 17662306a36Sopenharmony_ci{ 17762306a36Sopenharmony_ci u32 av_bridge_base, av_bridge_limit; 17862306a36Sopenharmony_ci int retval = 0; 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci switch (reg) { 18162306a36Sopenharmony_ci /* Make BARs appear to not request any memory. */ 18262306a36Sopenharmony_ci case PCI_BASE_ADDRESS_0: 18362306a36Sopenharmony_ci case PCI_BASE_ADDRESS_0 + 1: 18462306a36Sopenharmony_ci case PCI_BASE_ADDRESS_0 + 2: 18562306a36Sopenharmony_ci case PCI_BASE_ADDRESS_0 + 3: 18662306a36Sopenharmony_ci *value = 0; 18762306a36Sopenharmony_ci break; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci /* Since subordinate bus number register is hardwired 19062306a36Sopenharmony_ci * to zero and read only, so do the simulation. 19162306a36Sopenharmony_ci */ 19262306a36Sopenharmony_ci case PCI_PRIMARY_BUS: 19362306a36Sopenharmony_ci if (len == 4) 19462306a36Sopenharmony_ci *value = 0x00010100; 19562306a36Sopenharmony_ci break; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci case PCI_SUBORDINATE_BUS: 19862306a36Sopenharmony_ci *value = 1; 19962306a36Sopenharmony_ci break; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci case PCI_MEMORY_BASE: 20262306a36Sopenharmony_ci case PCI_MEMORY_LIMIT: 20362306a36Sopenharmony_ci /* Get the A/V bridge base address. */ 20462306a36Sopenharmony_ci pci_direct_conf1.read(0, 0, devfn, 20562306a36Sopenharmony_ci PCI_BASE_ADDRESS_0, 4, &av_bridge_base); 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci av_bridge_limit = av_bridge_base + (512*MB - 1); 20862306a36Sopenharmony_ci av_bridge_limit >>= 16; 20962306a36Sopenharmony_ci av_bridge_limit &= 0xFFF0; 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci av_bridge_base >>= 16; 21262306a36Sopenharmony_ci av_bridge_base &= 0xFFF0; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci if (reg == PCI_MEMORY_LIMIT) 21562306a36Sopenharmony_ci *value = av_bridge_limit; 21662306a36Sopenharmony_ci else if (len == 2) 21762306a36Sopenharmony_ci *value = av_bridge_base; 21862306a36Sopenharmony_ci else 21962306a36Sopenharmony_ci *value = (av_bridge_limit << 16) | av_bridge_base; 22062306a36Sopenharmony_ci break; 22162306a36Sopenharmony_ci /* Make prefetchable memory limit smaller than prefetchable 22262306a36Sopenharmony_ci * memory base, so not claim prefetchable memory space. 22362306a36Sopenharmony_ci */ 22462306a36Sopenharmony_ci case PCI_PREF_MEMORY_BASE: 22562306a36Sopenharmony_ci *value = 0xFFF0; 22662306a36Sopenharmony_ci break; 22762306a36Sopenharmony_ci case PCI_PREF_MEMORY_LIMIT: 22862306a36Sopenharmony_ci *value = 0x0; 22962306a36Sopenharmony_ci break; 23062306a36Sopenharmony_ci /* Make IO limit smaller than IO base, so not claim IO space. */ 23162306a36Sopenharmony_ci case PCI_IO_BASE: 23262306a36Sopenharmony_ci *value = 0xF0; 23362306a36Sopenharmony_ci break; 23462306a36Sopenharmony_ci case PCI_IO_LIMIT: 23562306a36Sopenharmony_ci *value = 0; 23662306a36Sopenharmony_ci break; 23762306a36Sopenharmony_ci default: 23862306a36Sopenharmony_ci retval = 1; 23962306a36Sopenharmony_ci } 24062306a36Sopenharmony_ci return retval; 24162306a36Sopenharmony_ci} 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_cistatic int ce4100_bus1_read(unsigned int devfn, int reg, int len, u32 *value) 24462306a36Sopenharmony_ci{ 24562306a36Sopenharmony_ci unsigned long flags; 24662306a36Sopenharmony_ci int i; 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bus1_fixups); i++) { 24962306a36Sopenharmony_ci if (bus1_fixups[i].dev_func == devfn && 25062306a36Sopenharmony_ci bus1_fixups[i].reg == (reg & ~3) && 25162306a36Sopenharmony_ci bus1_fixups[i].read) { 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci raw_spin_lock_irqsave(&pci_config_lock, flags); 25462306a36Sopenharmony_ci bus1_fixups[i].read(&(bus1_fixups[i]), value); 25562306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pci_config_lock, flags); 25662306a36Sopenharmony_ci extract_bytes(value, reg, len); 25762306a36Sopenharmony_ci return 0; 25862306a36Sopenharmony_ci } 25962306a36Sopenharmony_ci } 26062306a36Sopenharmony_ci return -1; 26162306a36Sopenharmony_ci} 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_cistatic int ce4100_conf_read(unsigned int seg, unsigned int bus, 26462306a36Sopenharmony_ci unsigned int devfn, int reg, int len, u32 *value) 26562306a36Sopenharmony_ci{ 26662306a36Sopenharmony_ci WARN_ON(seg); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci if (bus == 1 && !ce4100_bus1_read(devfn, reg, len, value)) 26962306a36Sopenharmony_ci return 0; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci if (bus == 0 && (PCI_DEVFN(1, 0) == devfn) && 27262306a36Sopenharmony_ci !bridge_read(devfn, reg, len, value)) 27362306a36Sopenharmony_ci return 0; 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci return pci_direct_conf1.read(seg, bus, devfn, reg, len, value); 27662306a36Sopenharmony_ci} 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_cistatic int ce4100_bus1_write(unsigned int devfn, int reg, int len, u32 value) 27962306a36Sopenharmony_ci{ 28062306a36Sopenharmony_ci unsigned long flags; 28162306a36Sopenharmony_ci int i; 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bus1_fixups); i++) { 28462306a36Sopenharmony_ci if (bus1_fixups[i].dev_func == devfn && 28562306a36Sopenharmony_ci bus1_fixups[i].reg == (reg & ~3) && 28662306a36Sopenharmony_ci bus1_fixups[i].write) { 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci raw_spin_lock_irqsave(&pci_config_lock, flags); 28962306a36Sopenharmony_ci bus1_fixups[i].write(&(bus1_fixups[i]), value); 29062306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pci_config_lock, flags); 29162306a36Sopenharmony_ci return 0; 29262306a36Sopenharmony_ci } 29362306a36Sopenharmony_ci } 29462306a36Sopenharmony_ci return -1; 29562306a36Sopenharmony_ci} 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_cistatic int ce4100_conf_write(unsigned int seg, unsigned int bus, 29862306a36Sopenharmony_ci unsigned int devfn, int reg, int len, u32 value) 29962306a36Sopenharmony_ci{ 30062306a36Sopenharmony_ci WARN_ON(seg); 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci if (bus == 1 && !ce4100_bus1_write(devfn, reg, len, value)) 30362306a36Sopenharmony_ci return 0; 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci /* Discard writes to A/V bridge BAR. */ 30662306a36Sopenharmony_ci if (bus == 0 && PCI_DEVFN(1, 0) == devfn && 30762306a36Sopenharmony_ci ((reg & ~3) == PCI_BASE_ADDRESS_0)) 30862306a36Sopenharmony_ci return 0; 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci return pci_direct_conf1.write(seg, bus, devfn, reg, len, value); 31162306a36Sopenharmony_ci} 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_cistatic const struct pci_raw_ops ce4100_pci_conf = { 31462306a36Sopenharmony_ci .read = ce4100_conf_read, 31562306a36Sopenharmony_ci .write = ce4100_conf_write, 31662306a36Sopenharmony_ci}; 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ciint __init ce4100_pci_init(void) 31962306a36Sopenharmony_ci{ 32062306a36Sopenharmony_ci init_sim_regs(); 32162306a36Sopenharmony_ci raw_pci_ops = &ce4100_pci_conf; 32262306a36Sopenharmony_ci /* Indicate caller that it should invoke pci_legacy_init() */ 32362306a36Sopenharmony_ci return 1; 32462306a36Sopenharmony_ci} 325