Path: blob/master/arch/arm/mach-ixp4xx/common-pci.c
10817 views
/*1* arch/arm/mach-ixp4xx/common-pci.c2*3* IXP4XX PCI routines for all platforms4*5* Maintainer: Deepak Saxena <[email protected]>6*7* Copyright (C) 2002 Intel Corporation.8* Copyright (C) 2003 Greg Ungerer <[email protected]>9* Copyright (C) 2003-2004 MontaVista Software, Inc.10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License version 2 as13* published by the Free Software Foundation.14*15*/1617#include <linux/sched.h>18#include <linux/kernel.h>19#include <linux/pci.h>20#include <linux/interrupt.h>21#include <linux/mm.h>22#include <linux/init.h>23#include <linux/ioport.h>24#include <linux/slab.h>25#include <linux/delay.h>26#include <linux/device.h>27#include <linux/io.h>28#include <asm/dma-mapping.h>2930#include <asm/cputype.h>31#include <asm/irq.h>32#include <asm/sizes.h>33#include <asm/system.h>34#include <asm/mach/pci.h>35#include <mach/hardware.h>363738/*39* IXP4xx PCI read function is dependent on whether we are40* running A0 or B0 (AppleGate) silicon.41*/42int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);4344/*45* Base address for PCI regsiter region46*/47unsigned long ixp4xx_pci_reg_base = 0;4849/*50* PCI cfg an I/O routines are done by programming a51* command/byte enable register, and then read/writing52* the data from a data regsiter. We need to ensure53* these transactions are atomic or we will end up54* with corrupt data on the bus or in a driver.55*/56static DEFINE_SPINLOCK(ixp4xx_pci_lock);5758/*59* Read from PCI config space60*/61static void crp_read(u32 ad_cbe, u32 *data)62{63unsigned long flags;64spin_lock_irqsave(&ixp4xx_pci_lock, flags);65*PCI_CRP_AD_CBE = ad_cbe;66*data = *PCI_CRP_RDATA;67spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);68}6970/*71* Write to PCI config space72*/73static void crp_write(u32 ad_cbe, u32 data)74{75unsigned long flags;76spin_lock_irqsave(&ixp4xx_pci_lock, flags);77*PCI_CRP_AD_CBE = CRP_AD_CBE_WRITE | ad_cbe;78*PCI_CRP_WDATA = data;79spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);80}8182static inline int check_master_abort(void)83{84/* check Master Abort bit after access */85unsigned long isr = *PCI_ISR;8687if (isr & PCI_ISR_PFE) {88/* make sure the Master Abort bit is reset */89*PCI_ISR = PCI_ISR_PFE;90pr_debug("%s failed\n", __func__);91return 1;92}9394return 0;95}9697int ixp4xx_pci_read_errata(u32 addr, u32 cmd, u32* data)98{99unsigned long flags;100int retval = 0;101int i;102103spin_lock_irqsave(&ixp4xx_pci_lock, flags);104105*PCI_NP_AD = addr;106107/*108* PCI workaround - only works if NP PCI space reads have109* no side effects!!! Read 8 times. last one will be good.110*/111for (i = 0; i < 8; i++) {112*PCI_NP_CBE = cmd;113*data = *PCI_NP_RDATA;114*data = *PCI_NP_RDATA;115}116117if(check_master_abort())118retval = 1;119120spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);121return retval;122}123124int ixp4xx_pci_read_no_errata(u32 addr, u32 cmd, u32* data)125{126unsigned long flags;127int retval = 0;128129spin_lock_irqsave(&ixp4xx_pci_lock, flags);130131*PCI_NP_AD = addr;132133/* set up and execute the read */134*PCI_NP_CBE = cmd;135136/* the result of the read is now in NP_RDATA */137*data = *PCI_NP_RDATA;138139if(check_master_abort())140retval = 1;141142spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);143return retval;144}145146int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data)147{148unsigned long flags;149int retval = 0;150151spin_lock_irqsave(&ixp4xx_pci_lock, flags);152153*PCI_NP_AD = addr;154155/* set up the write */156*PCI_NP_CBE = cmd;157158/* execute the write by writing to NP_WDATA */159*PCI_NP_WDATA = data;160161if(check_master_abort())162retval = 1;163164spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);165return retval;166}167168static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)169{170u32 addr;171if (!bus_num) {172/* type 0 */173addr = BIT(32-PCI_SLOT(devfn)) | ((PCI_FUNC(devfn)) << 8) |174(where & ~3);175} else {176/* type 1 */177addr = (bus_num << 16) | ((PCI_SLOT(devfn)) << 11) |178((PCI_FUNC(devfn)) << 8) | (where & ~3) | 1;179}180return addr;181}182183/*184* Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.185* 0 and 3 are not valid indexes...186*/187static u32 bytemask[] = {188/*0*/ 0,189/*1*/ 0xff,190/*2*/ 0xffff,191/*3*/ 0,192/*4*/ 0xffffffff,193};194195static u32 local_byte_lane_enable_bits(u32 n, int size)196{197if (size == 1)198return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;199if (size == 2)200return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;201if (size == 4)202return 0;203return 0xffffffff;204}205206static int local_read_config(int where, int size, u32 *value)207{208u32 n, data;209pr_debug("local_read_config from %d size %d\n", where, size);210n = where % 4;211crp_read(where & ~3, &data);212*value = (data >> (8*n)) & bytemask[size];213pr_debug("local_read_config read %#x\n", *value);214return PCIBIOS_SUCCESSFUL;215}216217static int local_write_config(int where, int size, u32 value)218{219u32 n, byte_enables, data;220pr_debug("local_write_config %#x to %d size %d\n", value, where, size);221n = where % 4;222byte_enables = local_byte_lane_enable_bits(n, size);223if (byte_enables == 0xffffffff)224return PCIBIOS_BAD_REGISTER_NUMBER;225data = value << (8*n);226crp_write((where & ~3) | byte_enables, data);227return PCIBIOS_SUCCESSFUL;228}229230static u32 byte_lane_enable_bits(u32 n, int size)231{232if (size == 1)233return (0xf & ~BIT(n)) << 4;234if (size == 2)235return (0xf & ~(BIT(n) | BIT(n+1))) << 4;236if (size == 4)237return 0;238return 0xffffffff;239}240241static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)242{243u32 n, byte_enables, addr, data;244u8 bus_num = bus->number;245246pr_debug("read_config from %d size %d dev %d:%d:%d\n", where, size,247bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));248249*value = 0xffffffff;250n = where % 4;251byte_enables = byte_lane_enable_bits(n, size);252if (byte_enables == 0xffffffff)253return PCIBIOS_BAD_REGISTER_NUMBER;254255addr = ixp4xx_config_addr(bus_num, devfn, where);256if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_CONFIGREAD, &data))257return PCIBIOS_DEVICE_NOT_FOUND;258259*value = (data >> (8*n)) & bytemask[size];260pr_debug("read_config_byte read %#x\n", *value);261return PCIBIOS_SUCCESSFUL;262}263264static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)265{266u32 n, byte_enables, addr, data;267u8 bus_num = bus->number;268269pr_debug("write_config_byte %#x to %d size %d dev %d:%d:%d\n", value, where,270size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));271272n = where % 4;273byte_enables = byte_lane_enable_bits(n, size);274if (byte_enables == 0xffffffff)275return PCIBIOS_BAD_REGISTER_NUMBER;276277addr = ixp4xx_config_addr(bus_num, devfn, where);278data = value << (8*n);279if (ixp4xx_pci_write(addr, byte_enables | NP_CMD_CONFIGWRITE, data))280return PCIBIOS_DEVICE_NOT_FOUND;281282return PCIBIOS_SUCCESSFUL;283}284285struct pci_ops ixp4xx_ops = {286.read = ixp4xx_pci_read_config,287.write = ixp4xx_pci_write_config,288};289290/*291* PCI abort handler292*/293static int abort_handler(unsigned long addr, unsigned int fsr, struct pt_regs *regs)294{295u32 isr, status;296297isr = *PCI_ISR;298local_read_config(PCI_STATUS, 2, &status);299pr_debug("PCI: abort_handler addr = %#lx, isr = %#x, "300"status = %#x\n", addr, isr, status);301302/* make sure the Master Abort bit is reset */303*PCI_ISR = PCI_ISR_PFE;304status |= PCI_STATUS_REC_MASTER_ABORT;305local_write_config(PCI_STATUS, 2, status);306307/*308* If it was an imprecise abort, then we need to correct the309* return address to be _after_ the instruction.310*/311if (fsr & (1 << 10))312regs->ARM_pc += 4;313314return 0;315}316317318/*319* Setup DMA mask to 64MB on PCI devices. Ignore all other devices.320*/321static int ixp4xx_pci_platform_notify(struct device *dev)322{323if(dev->bus == &pci_bus_type) {324*dev->dma_mask = SZ_64M - 1;325dev->coherent_dma_mask = SZ_64M - 1;326dmabounce_register_dev(dev, 2048, 4096);327}328return 0;329}330331static int ixp4xx_pci_platform_notify_remove(struct device *dev)332{333if(dev->bus == &pci_bus_type) {334dmabounce_unregister_dev(dev);335}336return 0;337}338339int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)340{341return (dev->bus == &pci_bus_type ) && ((dma_addr + size) >= SZ_64M);342}343344void __init ixp4xx_pci_preinit(void)345{346unsigned long cpuid = read_cpuid_id();347348/*349* Determine which PCI read method to use.350* Rev 0 IXP425 requires workaround.351*/352if (!(cpuid & 0xf) && cpu_is_ixp42x()) {353printk("PCI: IXP42x A0 silicon detected - "354"PCI Non-Prefetch Workaround Enabled\n");355ixp4xx_pci_read = ixp4xx_pci_read_errata;356} else357ixp4xx_pci_read = ixp4xx_pci_read_no_errata;358359360/* hook in our fault handler for PCI errors */361hook_fault_code(16+6, abort_handler, SIGBUS, 0,362"imprecise external abort");363364pr_debug("setup PCI-AHB(inbound) and AHB-PCI(outbound) address mappings\n");365366/*367* We use identity AHB->PCI address translation368* in the 0x48000000 to 0x4bffffff address space369*/370*PCI_PCIMEMBASE = 0x48494A4B;371372/*373* We also use identity PCI->AHB address translation374* in 4 16MB BARs that begin at the physical memory start375*/376*PCI_AHBMEMBASE = (PHYS_OFFSET & 0xFF000000) +377((PHYS_OFFSET & 0xFF000000) >> 8) +378((PHYS_OFFSET & 0xFF000000) >> 16) +379((PHYS_OFFSET & 0xFF000000) >> 24) +3800x00010203;381382if (*PCI_CSR & PCI_CSR_HOST) {383printk("PCI: IXP4xx is host\n");384385pr_debug("setup BARs in controller\n");386387/*388* We configure the PCI inbound memory windows to be389* 1:1 mapped to SDRAM390*/391local_write_config(PCI_BASE_ADDRESS_0, 4, PHYS_OFFSET);392local_write_config(PCI_BASE_ADDRESS_1, 4, PHYS_OFFSET + SZ_16M);393local_write_config(PCI_BASE_ADDRESS_2, 4, PHYS_OFFSET + SZ_32M);394local_write_config(PCI_BASE_ADDRESS_3, 4, PHYS_OFFSET + SZ_48M);395396/*397* Enable CSR window at 64 MiB to allow PCI masters398* to continue prefetching past 64 MiB boundary.399*/400local_write_config(PCI_BASE_ADDRESS_4, 4, PHYS_OFFSET + SZ_64M);401402/*403* Enable the IO window to be way up high, at 0xfffffc00404*/405local_write_config(PCI_BASE_ADDRESS_5, 4, 0xfffffc01);406} else {407printk("PCI: IXP4xx is target - No bus scan performed\n");408}409410printk("PCI: IXP4xx Using %s access for memory space\n",411#ifndef CONFIG_IXP4XX_INDIRECT_PCI412"direct"413#else414"indirect"415#endif416);417418pr_debug("clear error bits in ISR\n");419*PCI_ISR = PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE;420421/*422* Set Initialize Complete in PCI Control Register: allow IXP4XX to423* respond to PCI configuration cycles. Specify that the AHB bus is424* operating in big endian mode. Set up byte lane swapping between425* little-endian PCI and the big-endian AHB bus426*/427#ifdef __ARMEB__428*PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;429#else430*PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE;431#endif432433pr_debug("DONE\n");434}435436int ixp4xx_setup(int nr, struct pci_sys_data *sys)437{438struct resource *res;439440if (nr >= 1)441return 0;442443res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);444if (res == NULL) {445/*446* If we're out of memory this early, something is wrong,447* so we might as well catch it here.448*/449panic("PCI: unable to allocate resources?\n");450}451452local_write_config(PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);453454res[0].name = "PCI I/O Space";455res[0].start = 0x00000000;456res[0].end = 0x0000ffff;457res[0].flags = IORESOURCE_IO;458459res[1].name = "PCI Memory Space";460res[1].start = PCIBIOS_MIN_MEM;461res[1].end = PCIBIOS_MAX_MEM;462res[1].flags = IORESOURCE_MEM;463464request_resource(&ioport_resource, &res[0]);465request_resource(&iomem_resource, &res[1]);466467sys->resource[0] = &res[0];468sys->resource[1] = &res[1];469sys->resource[2] = NULL;470471platform_notify = ixp4xx_pci_platform_notify;472platform_notify_remove = ixp4xx_pci_platform_notify_remove;473474return 1;475}476477struct pci_bus * __devinit ixp4xx_scan_bus(int nr, struct pci_sys_data *sys)478{479return pci_scan_bus(sys->busnr, &ixp4xx_ops, sys);480}481482int dma_set_coherent_mask(struct device *dev, u64 mask)483{484if (mask >= SZ_64M - 1)485return 0;486487return -EIO;488}489490EXPORT_SYMBOL(ixp4xx_pci_read);491EXPORT_SYMBOL(ixp4xx_pci_write);492EXPORT_SYMBOL(dma_set_coherent_mask);493494495