Path: blob/master/arch/arm/mach-integrator/pci_v3.c
10817 views
/*1* linux/arch/arm/mach-integrator/pci_v3.c2*3* PCI functions for V3 host PCI bridge4*5* Copyright (C) 1999 ARM Limited6* Copyright (C) 2000-2001 Deep Blue Solutions Ltd7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21*/22#include <linux/kernel.h>23#include <linux/pci.h>24#include <linux/ioport.h>25#include <linux/interrupt.h>26#include <linux/spinlock.h>27#include <linux/init.h>28#include <linux/io.h>2930#include <mach/hardware.h>31#include <mach/platform.h>32#include <asm/irq.h>33#include <asm/signal.h>34#include <asm/system.h>35#include <asm/mach/pci.h>36#include <asm/irq_regs.h>3738#include <asm/hardware/pci_v3.h>3940/*41* The V3 PCI interface chip in Integrator provides several windows from42* local bus memory into the PCI memory areas. Unfortunately, there43* are not really enough windows for our usage, therefore we reuse44* one of the windows for access to PCI configuration space. The45* memory map is as follows:46*47* Local Bus Memory Usage48*49* 40000000 - 4FFFFFFF PCI memory. 256M non-prefetchable50* 50000000 - 5FFFFFFF PCI memory. 256M prefetchable51* 60000000 - 60FFFFFF PCI IO. 16M52* 61000000 - 61FFFFFF PCI Configuration. 16M53*54* There are three V3 windows, each described by a pair of V3 registers.55* These are LB_BASE0/LB_MAP0, LB_BASE1/LB_MAP1 and LB_BASE2/LB_MAP2.56* Base0 and Base1 can be used for any type of PCI memory access. Base257* can be used either for PCI I/O or for I20 accesses. By default, uHAL58* uses this only for PCI IO space.59*60* Normally these spaces are mapped using the following base registers:61*62* Usage Local Bus Memory Base/Map registers used63*64* Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP065* Mem 50000000 - 5FFFFFFF LB_BASE1/LB_MAP166* IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP267* Cfg 61000000 - 61FFFFFF68*69* This means that I20 and PCI configuration space accesses will fail.70* When PCI configuration accesses are needed (via the uHAL PCI71* configuration space primitives) we must remap the spaces as follows:72*73* Usage Local Bus Memory Base/Map registers used74*75* Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP076* Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP077* IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP278* Cfg 61000000 - 61FFFFFF LB_BASE1/LB_MAP179*80* To make this work, the code depends on overlapping windows working.81* The V3 chip translates an address by checking its range within82* each of the BASE/MAP pairs in turn (in ascending register number83* order). It will use the first matching pair. So, for example,84* if the same address is mapped by both LB_BASE0/LB_MAP0 and85* LB_BASE1/LB_MAP1, the V3 will use the translation from86* LB_BASE0/LB_MAP0.87*88* To allow PCI Configuration space access, the code enlarges the89* window mapped by LB_BASE0/LB_MAP0 from 256M to 512M. This occludes90* the windows currently mapped by LB_BASE1/LB_MAP1 so that it can91* be remapped for use by configuration cycles.92*93* At the end of the PCI Configuration space accesses,94* LB_BASE1/LB_MAP1 is reset to map PCI Memory. Finally the window95* mapped by LB_BASE0/LB_MAP0 is reduced in size from 512M to 256M to96* reveal the now restored LB_BASE1/LB_MAP1 window.97*98* NOTE: We do not set up I2O mapping. I suspect that this is only99* for an intelligent (target) device. Using I2O disables most of100* the mappings into PCI memory.101*/102103// V3 access routines104#define v3_writeb(o,v) __raw_writeb(v, PCI_V3_VADDR + (unsigned int)(o))105#define v3_readb(o) (__raw_readb(PCI_V3_VADDR + (unsigned int)(o)))106107#define v3_writew(o,v) __raw_writew(v, PCI_V3_VADDR + (unsigned int)(o))108#define v3_readw(o) (__raw_readw(PCI_V3_VADDR + (unsigned int)(o)))109110#define v3_writel(o,v) __raw_writel(v, PCI_V3_VADDR + (unsigned int)(o))111#define v3_readl(o) (__raw_readl(PCI_V3_VADDR + (unsigned int)(o)))112113/*============================================================================114*115* routine: uHALir_PCIMakeConfigAddress()116*117* parameters: bus = which bus118* device = which device119* function = which function120* offset = configuration space register we are interested in121*122* description: this routine will generate a platform dependent config123* address.124*125* calls: none126*127* returns: configuration address to play on the PCI bus128*129* To generate the appropriate PCI configuration cycles in the PCI130* configuration address space, you present the V3 with the following pattern131* (which is very nearly a type 1 (except that the lower two bits are 00 and132* not 01). In order for this mapping to work you need to set up one of133* the local to PCI aperatures to 16Mbytes in length translating to134* PCI configuration space starting at 0x0000.0000.135*136* PCI configuration cycles look like this:137*138* Type 0:139*140* 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1141* 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0142* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+143* | | |D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|0|144* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+145*146* 31:11 Device select bit.147* 10:8 Function number148* 7:2 Register number149*150* Type 1:151*152* 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1153* 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0154* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+155* | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|156* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+157*158* 31:24 reserved159* 23:16 bus number (8 bits = 128 possible buses)160* 15:11 Device number (5 bits)161* 10:8 function number162* 7:2 register number163*164*/165static DEFINE_SPINLOCK(v3_lock);166167#define PCI_BUS_NONMEM_START 0x00000000168#define PCI_BUS_NONMEM_SIZE SZ_256M169170#define PCI_BUS_PREMEM_START PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE171#define PCI_BUS_PREMEM_SIZE SZ_256M172173#if PCI_BUS_NONMEM_START & 0x000fffff174#error PCI_BUS_NONMEM_START must be megabyte aligned175#endif176#if PCI_BUS_PREMEM_START & 0x000fffff177#error PCI_BUS_PREMEM_START must be megabyte aligned178#endif179180#undef V3_LB_BASE_PREFETCH181#define V3_LB_BASE_PREFETCH 0182183static unsigned long v3_open_config_window(struct pci_bus *bus,184unsigned int devfn, int offset)185{186unsigned int address, mapaddress, busnr;187188busnr = bus->number;189190/*191* Trap out illegal values192*/193if (offset > 255)194BUG();195if (busnr > 255)196BUG();197if (devfn > 255)198BUG();199200if (busnr == 0) {201int slot = PCI_SLOT(devfn);202203/*204* local bus segment so need a type 0 config cycle205*206* build the PCI configuration "address" with one-hot in207* A31-A11208*209* mapaddress:210* 3:1 = config cycle (101)211* 0 = PCI A1 & A0 are 0 (0)212*/213address = PCI_FUNC(devfn) << 8;214mapaddress = V3_LB_MAP_TYPE_CONFIG;215216if (slot > 12)217/*218* high order bits are handled by the MAP register219*/220mapaddress |= 1 << (slot - 5);221else222/*223* low order bits handled directly in the address224*/225address |= 1 << (slot + 11);226} else {227/*228* not the local bus segment so need a type 1 config cycle229*230* address:231* 23:16 = bus number232* 15:11 = slot number (7:3 of devfn)233* 10:8 = func number (2:0 of devfn)234*235* mapaddress:236* 3:1 = config cycle (101)237* 0 = PCI A1 & A0 from host bus (1)238*/239mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;240address = (busnr << 16) | (devfn << 8);241}242243/*244* Set up base0 to see all 512Mbytes of memory space (not245* prefetchable), this frees up base1 for re-use by246* configuration memory247*/248v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |249V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);250251/*252* Set up base1/map1 to point into configuration space.253*/254v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_CONFIG_BASE) |255V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);256v3_writew(V3_LB_MAP1, mapaddress);257258return PCI_CONFIG_VADDR + address + offset;259}260261static void v3_close_config_window(void)262{263/*264* Reassign base1 for use by prefetchable PCI memory265*/266v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |267V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |268V3_LB_BASE_ENABLE);269v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |270V3_LB_MAP_TYPE_MEM_MULTIPLE);271272/*273* And shrink base0 back to a 256M window (NOTE: MAP0 already correct)274*/275v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |276V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);277}278279static int v3_read_config(struct pci_bus *bus, unsigned int devfn, int where,280int size, u32 *val)281{282unsigned long addr;283unsigned long flags;284u32 v;285286spin_lock_irqsave(&v3_lock, flags);287addr = v3_open_config_window(bus, devfn, where);288289switch (size) {290case 1:291v = __raw_readb(addr);292break;293294case 2:295v = __raw_readw(addr);296break;297298default:299v = __raw_readl(addr);300break;301}302303v3_close_config_window();304spin_unlock_irqrestore(&v3_lock, flags);305306*val = v;307return PCIBIOS_SUCCESSFUL;308}309310static int v3_write_config(struct pci_bus *bus, unsigned int devfn, int where,311int size, u32 val)312{313unsigned long addr;314unsigned long flags;315316spin_lock_irqsave(&v3_lock, flags);317addr = v3_open_config_window(bus, devfn, where);318319switch (size) {320case 1:321__raw_writeb((u8)val, addr);322__raw_readb(addr);323break;324325case 2:326__raw_writew((u16)val, addr);327__raw_readw(addr);328break;329330case 4:331__raw_writel(val, addr);332__raw_readl(addr);333break;334}335336v3_close_config_window();337spin_unlock_irqrestore(&v3_lock, flags);338339return PCIBIOS_SUCCESSFUL;340}341342static struct pci_ops pci_v3_ops = {343.read = v3_read_config,344.write = v3_write_config,345};346347static struct resource non_mem = {348.name = "PCI non-prefetchable",349.start = PHYS_PCI_MEM_BASE + PCI_BUS_NONMEM_START,350.end = PHYS_PCI_MEM_BASE + PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE - 1,351.flags = IORESOURCE_MEM,352};353354static struct resource pre_mem = {355.name = "PCI prefetchable",356.start = PHYS_PCI_MEM_BASE + PCI_BUS_PREMEM_START,357.end = PHYS_PCI_MEM_BASE + PCI_BUS_PREMEM_START + PCI_BUS_PREMEM_SIZE - 1,358.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH,359};360361static int __init pci_v3_setup_resources(struct resource **resource)362{363if (request_resource(&iomem_resource, &non_mem)) {364printk(KERN_ERR "PCI: unable to allocate non-prefetchable "365"memory region\n");366return -EBUSY;367}368if (request_resource(&iomem_resource, &pre_mem)) {369release_resource(&non_mem);370printk(KERN_ERR "PCI: unable to allocate prefetchable "371"memory region\n");372return -EBUSY;373}374375/*376* bus->resource[0] is the IO resource for this bus377* bus->resource[1] is the mem resource for this bus378* bus->resource[2] is the prefetch mem resource for this bus379*/380resource[0] = &ioport_resource;381resource[1] = &non_mem;382resource[2] = &pre_mem;383384return 1;385}386387/*388* These don't seem to be implemented on the Integrator I have, which389* means I can't get additional information on the reason for the pm2fb390* problems. I suppose I'll just have to mind-meld with the machine. ;)391*/392#define SC_PCI IO_ADDRESS(INTEGRATOR_SC_PCIENABLE)393#define SC_LBFADDR IO_ADDRESS(INTEGRATOR_SC_BASE + 0x20)394#define SC_LBFCODE IO_ADDRESS(INTEGRATOR_SC_BASE + 0x24)395396static int397v3_pci_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)398{399unsigned long pc = instruction_pointer(regs);400unsigned long instr = *(unsigned long *)pc;401#if 0402char buf[128];403404sprintf(buf, "V3 fault: addr 0x%08lx, FSR 0x%03x, PC 0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x ISTAT=%02x\n",405addr, fsr, pc, instr, __raw_readl(SC_LBFADDR), __raw_readl(SC_LBFCODE) & 255,406v3_readb(V3_LB_ISTAT));407printk(KERN_DEBUG "%s", buf);408#endif409410v3_writeb(V3_LB_ISTAT, 0);411__raw_writel(3, SC_PCI);412413/*414* If the instruction being executed was a read,415* make it look like it read all-ones.416*/417if ((instr & 0x0c100000) == 0x04100000) {418int reg = (instr >> 12) & 15;419unsigned long val;420421if (instr & 0x00400000)422val = 255;423else424val = -1;425426regs->uregs[reg] = val;427regs->ARM_pc += 4;428return 0;429}430431if ((instr & 0x0e100090) == 0x00100090) {432int reg = (instr >> 12) & 15;433434regs->uregs[reg] = -1;435regs->ARM_pc += 4;436return 0;437}438439return 1;440}441442static irqreturn_t v3_irq(int dummy, void *devid)443{444#ifdef CONFIG_DEBUG_LL445struct pt_regs *regs = get_irq_regs();446unsigned long pc = instruction_pointer(regs);447unsigned long instr = *(unsigned long *)pc;448char buf[128];449extern void printascii(const char *);450451sprintf(buf, "V3 int %d: pc=0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x "452"ISTAT=%02x\n", IRQ_AP_V3INT, pc, instr,453__raw_readl(SC_LBFADDR),454__raw_readl(SC_LBFCODE) & 255,455v3_readb(V3_LB_ISTAT));456printascii(buf);457#endif458459v3_writew(V3_PCI_STAT, 0xf000);460v3_writeb(V3_LB_ISTAT, 0);461__raw_writel(3, SC_PCI);462463#ifdef CONFIG_DEBUG_LL464/*465* If the instruction being executed was a read,466* make it look like it read all-ones.467*/468if ((instr & 0x0c100000) == 0x04100000) {469int reg = (instr >> 16) & 15;470sprintf(buf, " reg%d = %08lx\n", reg, regs->uregs[reg]);471printascii(buf);472}473#endif474return IRQ_HANDLED;475}476477int __init pci_v3_setup(int nr, struct pci_sys_data *sys)478{479int ret = 0;480481if (nr == 0) {482sys->mem_offset = PHYS_PCI_MEM_BASE;483ret = pci_v3_setup_resources(sys->resource);484}485486return ret;487}488489struct pci_bus * __init pci_v3_scan_bus(int nr, struct pci_sys_data *sys)490{491return pci_scan_bus(sys->busnr, &pci_v3_ops, sys);492}493494/*495* V3_LB_BASE? - local bus address496* V3_LB_MAP? - pci bus address497*/498void __init pci_v3_preinit(void)499{500unsigned long flags;501unsigned int temp;502int ret;503504/*505* Hook in our fault handler for PCI errors506*/507hook_fault_code(4, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");508hook_fault_code(6, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");509hook_fault_code(8, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");510hook_fault_code(10, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");511512spin_lock_irqsave(&v3_lock, flags);513514/*515* Unlock V3 registers, but only if they were previously locked.516*/517if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)518v3_writew(V3_SYSTEM, 0xa05f);519520/*521* Setup window 0 - PCI non-prefetchable memory522* Local: 0x40000000 Bus: 0x00000000 Size: 256MB523*/524v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |525V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);526v3_writew(V3_LB_MAP0, v3_addr_to_lb_map(PCI_BUS_NONMEM_START) |527V3_LB_MAP_TYPE_MEM);528529/*530* Setup window 1 - PCI prefetchable memory531* Local: 0x50000000 Bus: 0x10000000 Size: 256MB532*/533v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |534V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |535V3_LB_BASE_ENABLE);536v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |537V3_LB_MAP_TYPE_MEM_MULTIPLE);538539/*540* Setup window 2 - PCI IO541*/542v3_writel(V3_LB_BASE2, v3_addr_to_lb_base2(PHYS_PCI_IO_BASE) |543V3_LB_BASE_ENABLE);544v3_writew(V3_LB_MAP2, v3_addr_to_lb_map2(0));545546/*547* Disable PCI to host IO cycles548*/549temp = v3_readw(V3_PCI_CFG) & ~V3_PCI_CFG_M_I2O_EN;550temp |= V3_PCI_CFG_M_IO_REG_DIS | V3_PCI_CFG_M_IO_DIS;551v3_writew(V3_PCI_CFG, temp);552553printk(KERN_DEBUG "FIFO_CFG: %04x FIFO_PRIO: %04x\n",554v3_readw(V3_FIFO_CFG), v3_readw(V3_FIFO_PRIORITY));555556/*557* Set the V3 FIFO such that writes have higher priority than558* reads, and local bus write causes local bus read fifo flush.559* Same for PCI.560*/561v3_writew(V3_FIFO_PRIORITY, 0x0a0a);562563/*564* Re-lock the system register.565*/566temp = v3_readw(V3_SYSTEM) | V3_SYSTEM_M_LOCK;567v3_writew(V3_SYSTEM, temp);568569/*570* Clear any error conditions, and enable write errors.571*/572v3_writeb(V3_LB_ISTAT, 0);573v3_writew(V3_LB_CFG, v3_readw(V3_LB_CFG) | (1 << 10));574v3_writeb(V3_LB_IMASK, 0x28);575__raw_writel(3, SC_PCI);576577/*578* Grab the PCI error interrupt.579*/580ret = request_irq(IRQ_AP_V3INT, v3_irq, 0, "V3", NULL);581if (ret)582printk(KERN_ERR "PCI: unable to grab PCI error "583"interrupt: %d\n", ret);584585spin_unlock_irqrestore(&v3_lock, flags);586}587588void __init pci_v3_postinit(void)589{590unsigned int pci_cmd;591592pci_cmd = PCI_COMMAND_MEMORY |593PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;594595v3_writew(V3_PCI_CMD, pci_cmd);596597v3_writeb(V3_LB_ISTAT, ~0x40);598v3_writeb(V3_LB_IMASK, 0x68);599600#if 0601ret = request_irq(IRQ_AP_LBUSTIMEOUT, lb_timeout, 0, "bus timeout", NULL);602if (ret)603printk(KERN_ERR "PCI: unable to grab local bus timeout "604"interrupt: %d\n", ret);605#endif606607register_isa_ports(PHYS_PCI_MEM_BASE, PHYS_PCI_IO_BASE, 0);608}609610611