Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_pci.c
26481 views
/*1* PCI code for the Freescale MPC52xx embedded CPU.2*3* Copyright (C) 2006 Secret Lab Technologies Ltd.4* Grant Likely <[email protected]>5* Copyright (C) 2004 Sylvain Munaut <[email protected]>6*7* This file is licensed under the terms of the GNU General Public License8* version 2. This program is licensed "as is" without any warranty of any9* kind, whether express or implied.10*/1112#undef DEBUG1314#include <linux/pci.h>15#include <linux/of_address.h>16#include <asm/mpc52xx.h>17#include <asm/delay.h>18#include <asm/machdep.h>19#include <linux/kernel.h>202122/* ======================================================================== */23/* Structures mapping & Defines for PCI Unit */24/* ======================================================================== */2526#define MPC52xx_PCI_GSCR_BM 0x4000000027#define MPC52xx_PCI_GSCR_PE 0x2000000028#define MPC52xx_PCI_GSCR_SE 0x1000000029#define MPC52xx_PCI_GSCR_XLB2PCI_MASK 0x0700000030#define MPC52xx_PCI_GSCR_XLB2PCI_SHIFT 2431#define MPC52xx_PCI_GSCR_IPG2PCI_MASK 0x0007000032#define MPC52xx_PCI_GSCR_IPG2PCI_SHIFT 1633#define MPC52xx_PCI_GSCR_BME 0x0000400034#define MPC52xx_PCI_GSCR_PEE 0x0000200035#define MPC52xx_PCI_GSCR_SEE 0x0000100036#define MPC52xx_PCI_GSCR_PR 0x00000001373839#define MPC52xx_PCI_IWBTAR_TRANSLATION(proc_ad,pci_ad,size) \40( ( (proc_ad) & 0xff000000 ) | \41( (((size) - 1) >> 8) & 0x00ff0000 ) | \42( ((pci_ad) >> 16) & 0x0000ff00 ) )4344#define MPC52xx_PCI_IWCR_PACK(win0,win1,win2) (((win0) << 24) | \45((win1) << 16) | \46((win2) << 8))4748#define MPC52xx_PCI_IWCR_DISABLE 0x049#define MPC52xx_PCI_IWCR_ENABLE 0x150#define MPC52xx_PCI_IWCR_READ 0x051#define MPC52xx_PCI_IWCR_READ_LINE 0x252#define MPC52xx_PCI_IWCR_READ_MULTI 0x453#define MPC52xx_PCI_IWCR_MEM 0x054#define MPC52xx_PCI_IWCR_IO 0x85556#define MPC52xx_PCI_TCR_P 0x0100000057#define MPC52xx_PCI_TCR_LD 0x0001000058#define MPC52xx_PCI_TCR_WCT8 0x000000085960#define MPC52xx_PCI_TBATR_DISABLE 0x061#define MPC52xx_PCI_TBATR_ENABLE 0x16263struct mpc52xx_pci {64u32 idr; /* PCI + 0x00 */65u32 scr; /* PCI + 0x04 */66u32 ccrir; /* PCI + 0x08 */67u32 cr1; /* PCI + 0x0C */68u32 bar0; /* PCI + 0x10 */69u32 bar1; /* PCI + 0x14 */70u8 reserved1[16]; /* PCI + 0x18 */71u32 ccpr; /* PCI + 0x28 */72u32 sid; /* PCI + 0x2C */73u32 erbar; /* PCI + 0x30 */74u32 cpr; /* PCI + 0x34 */75u8 reserved2[4]; /* PCI + 0x38 */76u32 cr2; /* PCI + 0x3C */77u8 reserved3[32]; /* PCI + 0x40 */78u32 gscr; /* PCI + 0x60 */79u32 tbatr0; /* PCI + 0x64 */80u32 tbatr1; /* PCI + 0x68 */81u32 tcr; /* PCI + 0x6C */82u32 iw0btar; /* PCI + 0x70 */83u32 iw1btar; /* PCI + 0x74 */84u32 iw2btar; /* PCI + 0x78 */85u8 reserved4[4]; /* PCI + 0x7C */86u32 iwcr; /* PCI + 0x80 */87u32 icr; /* PCI + 0x84 */88u32 isr; /* PCI + 0x88 */89u32 arb; /* PCI + 0x8C */90u8 reserved5[104]; /* PCI + 0x90 */91u32 car; /* PCI + 0xF8 */92u8 reserved6[4]; /* PCI + 0xFC */93};9495/* MPC5200 device tree match tables */96const struct of_device_id mpc52xx_pci_ids[] __initconst = {97{ .type = "pci", .compatible = "fsl,mpc5200-pci", },98{ .type = "pci", .compatible = "mpc5200-pci", },99{}100};101102/* ======================================================================== */103/* PCI configuration access */104/* ======================================================================== */105106static int107mpc52xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,108int offset, int len, u32 *val)109{110struct pci_controller *hose = pci_bus_to_host(bus);111u32 value;112113if (ppc_md.pci_exclude_device)114if (ppc_md.pci_exclude_device(hose, bus->number, devfn))115return PCIBIOS_DEVICE_NOT_FOUND;116117out_be32(hose->cfg_addr,118(1 << 31) |119(bus->number << 16) |120(devfn << 8) |121(offset & 0xfc));122mb();123124#if defined(CONFIG_PPC_MPC5200_BUGFIX)125if (bus->number) {126/* workaround for the bug 435 of the MPC5200 (L25R);127* Don't do 32 bits config access during type-1 cycles */128switch (len) {129case 1:130value = in_8(((u8 __iomem *)hose->cfg_data) +131(offset & 3));132break;133case 2:134value = in_le16(((u16 __iomem *)hose->cfg_data) +135((offset>>1) & 1));136break;137138default:139value = in_le16((u16 __iomem *)hose->cfg_data) |140(in_le16(((u16 __iomem *)hose->cfg_data) + 1) << 16);141break;142}143}144else145#endif146{147value = in_le32(hose->cfg_data);148149if (len != 4) {150value >>= ((offset & 0x3) << 3);151value &= 0xffffffff >> (32 - (len << 3));152}153}154155*val = value;156157out_be32(hose->cfg_addr, 0);158mb();159160return PCIBIOS_SUCCESSFUL;161}162163static int164mpc52xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,165int offset, int len, u32 val)166{167struct pci_controller *hose = pci_bus_to_host(bus);168u32 value, mask;169170if (ppc_md.pci_exclude_device)171if (ppc_md.pci_exclude_device(hose, bus->number, devfn))172return PCIBIOS_DEVICE_NOT_FOUND;173174out_be32(hose->cfg_addr,175(1 << 31) |176(bus->number << 16) |177(devfn << 8) |178(offset & 0xfc));179mb();180181#if defined(CONFIG_PPC_MPC5200_BUGFIX)182if (bus->number) {183/* workaround for the bug 435 of the MPC5200 (L25R);184* Don't do 32 bits config access during type-1 cycles */185switch (len) {186case 1:187out_8(((u8 __iomem *)hose->cfg_data) +188(offset & 3), val);189break;190case 2:191out_le16(((u16 __iomem *)hose->cfg_data) +192((offset>>1) & 1), val);193break;194195default:196out_le16((u16 __iomem *)hose->cfg_data,197(u16)val);198out_le16(((u16 __iomem *)hose->cfg_data) + 1,199(u16)(val>>16));200break;201}202}203else204#endif205{206if (len != 4) {207value = in_le32(hose->cfg_data);208209offset = (offset & 0x3) << 3;210mask = (0xffffffff >> (32 - (len << 3)));211mask <<= offset;212213value &= ~mask;214val = value | ((val << offset) & mask);215}216217out_le32(hose->cfg_data, val);218}219mb();220221out_be32(hose->cfg_addr, 0);222mb();223224return PCIBIOS_SUCCESSFUL;225}226227static struct pci_ops mpc52xx_pci_ops = {228.read = mpc52xx_pci_read_config,229.write = mpc52xx_pci_write_config230};231232233/* ======================================================================== */234/* PCI setup */235/* ======================================================================== */236237static void __init238mpc52xx_pci_setup(struct pci_controller *hose,239struct mpc52xx_pci __iomem *pci_regs, phys_addr_t pci_phys)240{241struct resource *res;242u32 tmp;243int iwcr0 = 0, iwcr1 = 0, iwcr2 = 0;244245pr_debug("%s(hose=%p, pci_regs=%p)\n", __func__, hose, pci_regs);246247/* pci_process_bridge_OF_ranges() found all our addresses for us;248* now store them in the right places */249hose->cfg_addr = &pci_regs->car;250hose->cfg_data = hose->io_base_virt;251252/* Control regs */253tmp = in_be32(&pci_regs->scr);254tmp |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;255out_be32(&pci_regs->scr, tmp);256257/* Memory windows */258res = &hose->mem_resources[0];259if (res->flags) {260pr_debug("mem_resource[0] = %pr\n", res);261out_be32(&pci_regs->iw0btar,262MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,263resource_size(res)));264iwcr0 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;265if (res->flags & IORESOURCE_PREFETCH)266iwcr0 |= MPC52xx_PCI_IWCR_READ_MULTI;267else268iwcr0 |= MPC52xx_PCI_IWCR_READ;269}270271res = &hose->mem_resources[1];272if (res->flags) {273pr_debug("mem_resource[1] = %pr\n", res);274out_be32(&pci_regs->iw1btar,275MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,276resource_size(res)));277iwcr1 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;278if (res->flags & IORESOURCE_PREFETCH)279iwcr1 |= MPC52xx_PCI_IWCR_READ_MULTI;280else281iwcr1 |= MPC52xx_PCI_IWCR_READ;282}283284/* IO resources */285res = &hose->io_resource;286if (!res) {287printk(KERN_ERR "%s: Didn't find IO resources\n", __FILE__);288return;289}290pr_debug(".io_resource = %pr .io_base_phys=0x%pa\n",291res, &hose->io_base_phys);292out_be32(&pci_regs->iw2btar,293MPC52xx_PCI_IWBTAR_TRANSLATION(hose->io_base_phys,294res->start,295resource_size(res)));296iwcr2 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_IO;297298/* Set all the IWCR fields at once; they're in the same reg */299out_be32(&pci_regs->iwcr, MPC52xx_PCI_IWCR_PACK(iwcr0, iwcr1, iwcr2));300301/* Map IMMR onto PCI bus */302pci_phys &= 0xfffc0000; /* bar0 has only 14 significant bits */303out_be32(&pci_regs->tbatr0, MPC52xx_PCI_TBATR_ENABLE | pci_phys);304out_be32(&pci_regs->bar0, PCI_BASE_ADDRESS_MEM_PREFETCH | pci_phys);305306/* Map memory onto PCI bus */307out_be32(&pci_regs->tbatr1, MPC52xx_PCI_TBATR_ENABLE);308out_be32(&pci_regs->bar1, PCI_BASE_ADDRESS_MEM_PREFETCH);309310out_be32(&pci_regs->tcr, MPC52xx_PCI_TCR_LD | MPC52xx_PCI_TCR_WCT8);311312tmp = in_be32(&pci_regs->gscr);313#if 0314/* Reset the exteral bus ( internal PCI controller is NOT reset ) */315/* Not necessary and can be a bad thing if for example the bootloader316is displaying a splash screen or ... Just left here for317documentation purpose if anyone need it */318out_be32(&pci_regs->gscr, tmp | MPC52xx_PCI_GSCR_PR);319udelay(50);320#endif321322/* Make sure the PCI bridge is out of reset */323out_be32(&pci_regs->gscr, tmp & ~MPC52xx_PCI_GSCR_PR);324}325326static void327mpc52xx_pci_fixup_resources(struct pci_dev *dev)328{329struct resource *res;330331pr_debug("%s() %.4x:%.4x\n", __func__, dev->vendor, dev->device);332333/* We don't rely on boot loader for PCI and resets all334devices */335pci_dev_for_each_resource(dev, res) {336if (res->end > res->start) { /* Only valid resources */337res->end -= res->start;338res->start = 0;339res->flags |= IORESOURCE_UNSET;340}341}342343/* The PCI Host bridge of MPC52xx has a prefetch memory resource344fixed to 1Gb. Doesn't fit in the resource system so we remove it */345if ( (dev->vendor == PCI_VENDOR_ID_MOTOROLA) &&346( dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200347|| dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200B) ) {348struct resource *res = &dev->resource[1];349res->start = res->end = res->flags = 0;350}351}352353int __init354mpc52xx_add_bridge(struct device_node *node)355{356int len;357struct mpc52xx_pci __iomem *pci_regs;358struct pci_controller *hose;359const int *bus_range;360struct resource rsrc;361362pr_debug("Adding MPC52xx PCI host bridge %pOF\n", node);363364pci_add_flags(PCI_REASSIGN_ALL_BUS);365366if (of_address_to_resource(node, 0, &rsrc) != 0) {367printk(KERN_ERR "Can't get %pOF resources\n", node);368return -EINVAL;369}370371bus_range = of_get_property(node, "bus-range", &len);372if (bus_range == NULL || len < 2 * sizeof(int)) {373printk(KERN_WARNING "Can't get %pOF bus-range, assume bus 0\n",374node);375bus_range = NULL;376}377378/* There are some PCI quirks on the 52xx, register the hook to379* fix them. */380ppc_md.pcibios_fixup_resources = mpc52xx_pci_fixup_resources;381382/* Alloc and initialize the pci controller. Values in the device383* tree are needed to configure the 52xx PCI controller. Rather384* than parse the tree here, let pci_process_bridge_OF_ranges()385* do it for us and extract the values after the fact */386hose = pcibios_alloc_controller(node);387if (!hose)388return -ENOMEM;389390hose->first_busno = bus_range ? bus_range[0] : 0;391hose->last_busno = bus_range ? bus_range[1] : 0xff;392393hose->ops = &mpc52xx_pci_ops;394395pci_regs = ioremap(rsrc.start, resource_size(&rsrc));396if (!pci_regs)397return -ENOMEM;398399pci_process_bridge_OF_ranges(hose, node, 1);400401/* Finish setting up PCI using values obtained by402* pci_proces_bridge_OF_ranges */403mpc52xx_pci_setup(hose, pci_regs, rsrc.start);404405return 0;406}407408void __init mpc52xx_setup_pci(void)409{410struct device_node *pci;411412pci = of_find_matching_node(NULL, mpc52xx_pci_ids);413if (!pci)414return;415416mpc52xx_add_bridge(pci);417of_node_put(pci);418}419420421