Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_pci.c
10818 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 <asm/pci.h>15#include <asm/mpc52xx.h>16#include <asm/delay.h>17#include <asm/machdep.h>18#include <linux/kernel.h>192021/* ======================================================================== */22/* Structures mapping & Defines for PCI Unit */23/* ======================================================================== */2425#define MPC52xx_PCI_GSCR_BM 0x4000000026#define MPC52xx_PCI_GSCR_PE 0x2000000027#define MPC52xx_PCI_GSCR_SE 0x1000000028#define MPC52xx_PCI_GSCR_XLB2PCI_MASK 0x0700000029#define MPC52xx_PCI_GSCR_XLB2PCI_SHIFT 2430#define MPC52xx_PCI_GSCR_IPG2PCI_MASK 0x0007000031#define MPC52xx_PCI_GSCR_IPG2PCI_SHIFT 1632#define MPC52xx_PCI_GSCR_BME 0x0000400033#define MPC52xx_PCI_GSCR_PEE 0x0000200034#define MPC52xx_PCI_GSCR_SEE 0x0000100035#define MPC52xx_PCI_GSCR_PR 0x00000001363738#define MPC52xx_PCI_IWBTAR_TRANSLATION(proc_ad,pci_ad,size) \39( ( (proc_ad) & 0xff000000 ) | \40( (((size) - 1) >> 8) & 0x00ff0000 ) | \41( ((pci_ad) >> 16) & 0x0000ff00 ) )4243#define MPC52xx_PCI_IWCR_PACK(win0,win1,win2) (((win0) << 24) | \44((win1) << 16) | \45((win2) << 8))4647#define MPC52xx_PCI_IWCR_DISABLE 0x048#define MPC52xx_PCI_IWCR_ENABLE 0x149#define MPC52xx_PCI_IWCR_READ 0x050#define MPC52xx_PCI_IWCR_READ_LINE 0x251#define MPC52xx_PCI_IWCR_READ_MULTI 0x452#define MPC52xx_PCI_IWCR_MEM 0x053#define MPC52xx_PCI_IWCR_IO 0x85455#define MPC52xx_PCI_TCR_P 0x0100000056#define MPC52xx_PCI_TCR_LD 0x0001000057#define MPC52xx_PCI_TCR_WCT8 0x000000085859#define MPC52xx_PCI_TBATR_DISABLE 0x060#define MPC52xx_PCI_TBATR_ENABLE 0x16162struct mpc52xx_pci {63u32 idr; /* PCI + 0x00 */64u32 scr; /* PCI + 0x04 */65u32 ccrir; /* PCI + 0x08 */66u32 cr1; /* PCI + 0x0C */67u32 bar0; /* PCI + 0x10 */68u32 bar1; /* PCI + 0x14 */69u8 reserved1[16]; /* PCI + 0x18 */70u32 ccpr; /* PCI + 0x28 */71u32 sid; /* PCI + 0x2C */72u32 erbar; /* PCI + 0x30 */73u32 cpr; /* PCI + 0x34 */74u8 reserved2[4]; /* PCI + 0x38 */75u32 cr2; /* PCI + 0x3C */76u8 reserved3[32]; /* PCI + 0x40 */77u32 gscr; /* PCI + 0x60 */78u32 tbatr0; /* PCI + 0x64 */79u32 tbatr1; /* PCI + 0x68 */80u32 tcr; /* PCI + 0x6C */81u32 iw0btar; /* PCI + 0x70 */82u32 iw1btar; /* PCI + 0x74 */83u32 iw2btar; /* PCI + 0x78 */84u8 reserved4[4]; /* PCI + 0x7C */85u32 iwcr; /* PCI + 0x80 */86u32 icr; /* PCI + 0x84 */87u32 isr; /* PCI + 0x88 */88u32 arb; /* PCI + 0x8C */89u8 reserved5[104]; /* PCI + 0x90 */90u32 car; /* PCI + 0xF8 */91u8 reserved6[4]; /* PCI + 0xFC */92};9394/* MPC5200 device tree match tables */95const struct of_device_id mpc52xx_pci_ids[] __initdata = {96{ .type = "pci", .compatible = "fsl,mpc5200-pci", },97{ .type = "pci", .compatible = "mpc5200-pci", },98{}99};100101/* ======================================================================== */102/* PCI configuration access */103/* ======================================================================== */104105static int106mpc52xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,107int offset, int len, u32 *val)108{109struct pci_controller *hose = pci_bus_to_host(bus);110u32 value;111112if (ppc_md.pci_exclude_device)113if (ppc_md.pci_exclude_device(hose, bus->number, devfn))114return PCIBIOS_DEVICE_NOT_FOUND;115116out_be32(hose->cfg_addr,117(1 << 31) |118(bus->number << 16) |119(devfn << 8) |120(offset & 0xfc));121mb();122123#if defined(CONFIG_PPC_MPC5200_BUGFIX)124if (bus->number) {125/* workaround for the bug 435 of the MPC5200 (L25R);126* Don't do 32 bits config access during type-1 cycles */127switch (len) {128case 1:129value = in_8(((u8 __iomem *)hose->cfg_data) +130(offset & 3));131break;132case 2:133value = in_le16(((u16 __iomem *)hose->cfg_data) +134((offset>>1) & 1));135break;136137default:138value = in_le16((u16 __iomem *)hose->cfg_data) |139(in_le16(((u16 __iomem *)hose->cfg_data) + 1) << 16);140break;141}142}143else144#endif145{146value = in_le32(hose->cfg_data);147148if (len != 4) {149value >>= ((offset & 0x3) << 3);150value &= 0xffffffff >> (32 - (len << 3));151}152}153154*val = value;155156out_be32(hose->cfg_addr, 0);157mb();158159return PCIBIOS_SUCCESSFUL;160}161162static int163mpc52xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,164int offset, int len, u32 val)165{166struct pci_controller *hose = pci_bus_to_host(bus);167u32 value, mask;168169if (ppc_md.pci_exclude_device)170if (ppc_md.pci_exclude_device(hose, bus->number, devfn))171return PCIBIOS_DEVICE_NOT_FOUND;172173out_be32(hose->cfg_addr,174(1 << 31) |175(bus->number << 16) |176(devfn << 8) |177(offset & 0xfc));178mb();179180#if defined(CONFIG_PPC_MPC5200_BUGFIX)181if (bus->number) {182/* workaround for the bug 435 of the MPC5200 (L25R);183* Don't do 32 bits config access during type-1 cycles */184switch (len) {185case 1:186out_8(((u8 __iomem *)hose->cfg_data) +187(offset & 3), val);188break;189case 2:190out_le16(((u16 __iomem *)hose->cfg_data) +191((offset>>1) & 1), val);192break;193194default:195out_le16((u16 __iomem *)hose->cfg_data,196(u16)val);197out_le16(((u16 __iomem *)hose->cfg_data) + 1,198(u16)(val>>16));199break;200}201}202else203#endif204{205if (len != 4) {206value = in_le32(hose->cfg_data);207208offset = (offset & 0x3) << 3;209mask = (0xffffffff >> (32 - (len << 3)));210mask <<= offset;211212value &= ~mask;213val = value | ((val << offset) & mask);214}215216out_le32(hose->cfg_data, val);217}218mb();219220out_be32(hose->cfg_addr, 0);221mb();222223return PCIBIOS_SUCCESSFUL;224}225226static struct pci_ops mpc52xx_pci_ops = {227.read = mpc52xx_pci_read_config,228.write = mpc52xx_pci_write_config229};230231232/* ======================================================================== */233/* PCI setup */234/* ======================================================================== */235236static void __init237mpc52xx_pci_setup(struct pci_controller *hose,238struct mpc52xx_pci __iomem *pci_regs, phys_addr_t pci_phys)239{240struct resource *res;241u32 tmp;242int iwcr0 = 0, iwcr1 = 0, iwcr2 = 0;243244pr_debug("mpc52xx_pci_setup(hose=%p, pci_regs=%p)\n", hose, pci_regs);245246/* pci_process_bridge_OF_ranges() found all our addresses for us;247* now store them in the right places */248hose->cfg_addr = &pci_regs->car;249hose->cfg_data = hose->io_base_virt;250251/* Control regs */252tmp = in_be32(&pci_regs->scr);253tmp |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;254out_be32(&pci_regs->scr, tmp);255256/* Memory windows */257res = &hose->mem_resources[0];258if (res->flags) {259pr_debug("mem_resource[0] = "260"{.start=%llx, .end=%llx, .flags=%llx}\n",261(unsigned long long)res->start,262(unsigned long long)res->end,263(unsigned long long)res->flags);264out_be32(&pci_regs->iw0btar,265MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,266res->end - res->start + 1));267iwcr0 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;268if (res->flags & IORESOURCE_PREFETCH)269iwcr0 |= MPC52xx_PCI_IWCR_READ_MULTI;270else271iwcr0 |= MPC52xx_PCI_IWCR_READ;272}273274res = &hose->mem_resources[1];275if (res->flags) {276pr_debug("mem_resource[1] = {.start=%x, .end=%x, .flags=%lx}\n",277res->start, res->end, res->flags);278out_be32(&pci_regs->iw1btar,279MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,280res->end - res->start + 1));281iwcr1 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;282if (res->flags & IORESOURCE_PREFETCH)283iwcr1 |= MPC52xx_PCI_IWCR_READ_MULTI;284else285iwcr1 |= MPC52xx_PCI_IWCR_READ;286}287288/* IO resources */289res = &hose->io_resource;290if (!res) {291printk(KERN_ERR "%s: Didn't find IO resources\n", __FILE__);292return;293}294pr_debug(".io_resource={.start=%llx,.end=%llx,.flags=%llx} "295".io_base_phys=0x%p\n",296(unsigned long long)res->start,297(unsigned long long)res->end,298(unsigned long long)res->flags, (void*)hose->io_base_phys);299out_be32(&pci_regs->iw2btar,300MPC52xx_PCI_IWBTAR_TRANSLATION(hose->io_base_phys,301res->start,302res->end - res->start + 1));303iwcr2 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_IO;304305/* Set all the IWCR fields at once; they're in the same reg */306out_be32(&pci_regs->iwcr, MPC52xx_PCI_IWCR_PACK(iwcr0, iwcr1, iwcr2));307308/* Map IMMR onto PCI bus */309pci_phys &= 0xfffc0000; /* bar0 has only 14 significant bits */310out_be32(&pci_regs->tbatr0, MPC52xx_PCI_TBATR_ENABLE | pci_phys);311out_be32(&pci_regs->bar0, PCI_BASE_ADDRESS_MEM_PREFETCH | pci_phys);312313/* Map memory onto PCI bus */314out_be32(&pci_regs->tbatr1, MPC52xx_PCI_TBATR_ENABLE);315out_be32(&pci_regs->bar1, PCI_BASE_ADDRESS_MEM_PREFETCH);316317out_be32(&pci_regs->tcr, MPC52xx_PCI_TCR_LD | MPC52xx_PCI_TCR_WCT8);318319tmp = in_be32(&pci_regs->gscr);320#if 0321/* Reset the exteral bus ( internal PCI controller is NOT resetted ) */322/* Not necessary and can be a bad thing if for example the bootloader323is displaying a splash screen or ... Just left here for324documentation purpose if anyone need it */325out_be32(&pci_regs->gscr, tmp | MPC52xx_PCI_GSCR_PR);326udelay(50);327#endif328329/* Make sure the PCI bridge is out of reset */330out_be32(&pci_regs->gscr, tmp & ~MPC52xx_PCI_GSCR_PR);331}332333static void334mpc52xx_pci_fixup_resources(struct pci_dev *dev)335{336int i;337338pr_debug("mpc52xx_pci_fixup_resources() %.4x:%.4x\n",339dev->vendor, dev->device);340341/* We don't rely on boot loader for PCI and resets all342devices */343for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {344struct resource *res = &dev->resource[i];345if (res->end > res->start) { /* Only valid resources */346res->end -= res->start;347res->start = 0;348res->flags |= IORESOURCE_UNSET;349}350}351352/* The PCI Host bridge of MPC52xx has a prefetch memory resource353fixed to 1Gb. Doesn't fit in the resource system so we remove it */354if ( (dev->vendor == PCI_VENDOR_ID_MOTOROLA) &&355( dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200356|| dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200B) ) {357struct resource *res = &dev->resource[1];358res->start = res->end = res->flags = 0;359}360}361362int __init363mpc52xx_add_bridge(struct device_node *node)364{365int len;366struct mpc52xx_pci __iomem *pci_regs;367struct pci_controller *hose;368const int *bus_range;369struct resource rsrc;370371pr_debug("Adding MPC52xx PCI host bridge %s\n", node->full_name);372373ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);374375if (of_address_to_resource(node, 0, &rsrc) != 0) {376printk(KERN_ERR "Can't get %s resources\n", node->full_name);377return -EINVAL;378}379380bus_range = of_get_property(node, "bus-range", &len);381if (bus_range == NULL || len < 2 * sizeof(int)) {382printk(KERN_WARNING "Can't get %s bus-range, assume bus 0\n",383node->full_name);384bus_range = NULL;385}386387/* There are some PCI quirks on the 52xx, register the hook to388* fix them. */389ppc_md.pcibios_fixup_resources = mpc52xx_pci_fixup_resources;390391/* Alloc and initialize the pci controller. Values in the device392* tree are needed to configure the 52xx PCI controller. Rather393* than parse the tree here, let pci_process_bridge_OF_ranges()394* do it for us and extract the values after the fact */395hose = pcibios_alloc_controller(node);396if (!hose)397return -ENOMEM;398399hose->first_busno = bus_range ? bus_range[0] : 0;400hose->last_busno = bus_range ? bus_range[1] : 0xff;401402hose->ops = &mpc52xx_pci_ops;403404pci_regs = ioremap(rsrc.start, rsrc.end - rsrc.start + 1);405if (!pci_regs)406return -ENOMEM;407408pci_process_bridge_OF_ranges(hose, node, 1);409410/* Finish setting up PCI using values obtained by411* pci_proces_bridge_OF_ranges */412mpc52xx_pci_setup(hose, pci_regs, rsrc.start);413414return 0;415}416417void __init mpc52xx_setup_pci(void)418{419struct device_node *pci;420421pci = of_find_matching_node(NULL, mpc52xx_pci_ids);422if (!pci)423return;424425mpc52xx_add_bridge(pci);426of_node_put(pci);427}428429430