Merge branch 'for-1.3.2-ver2'
[oweals/u-boot.git] / cpu / ppc4xx / 4xx_pcie.c
index 7ac8ce0d765ba3a9e7d11b25ed8b8c396c6cd10a..3af9862bfff0dd8a5d313d7bb68c81a903035d5d 100644 (file)
@@ -21,7 +21,7 @@
  */
 
 /* define DEBUG for debugging output (obviously ;-)) */
-#if 1
+#if 0
 #define DEBUG
 #endif
 
@@ -31,7 +31,8 @@
 #include <common.h>
 #include <pci.h>
 
-#if defined(CONFIG_440SPE) && defined(CONFIG_PCI)
+#if (defined(CONFIG_440SPE) || defined(CONFIG_405EX)) && \
+    defined(CONFIG_PCI)
 
 #include <asm/4xx_pcie.h>
 
@@ -45,6 +46,20 @@ enum {
        LNKW_X8                 = 0x8
 };
 
+static int validate_endpoint(struct pci_controller *hose)
+{
+       if (hose->cfg_data == (u8 *)CFG_PCIE0_CFGBASE)
+               return (is_end_point(0));
+       else if (hose->cfg_data == (u8 *)CFG_PCIE1_CFGBASE)
+               return (is_end_point(1));
+#if CFG_PCIE_NR_PORTS > 2
+       else if (hose->cfg_data == (u8 *)CFG_PCIE2_CFGBASE)
+               return (is_end_point(2));
+#endif
+
+       return 0;
+}
+
 static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
 {
        u8 *base = (u8*)hose->cfg_data;
@@ -55,8 +70,10 @@ static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
                        base = (u8*)CFG_PCIE0_XCFGBASE;
                if (hose->cfg_data == (u8*)CFG_PCIE1_CFGBASE)
                        base = (u8*)CFG_PCIE1_XCFGBASE;
+#if CFG_PCIE_NR_PORTS > 2
                if (hose->cfg_data == (u8*)CFG_PCIE2_CFGBASE)
                        base = (u8*)CFG_PCIE2_XCFGBASE;
+#endif
        }
 
        return base;
@@ -68,8 +85,10 @@ static void pcie_dmer_disable(void)
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
        mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
+#if CFG_PCIE_NR_PORTS > 2
        mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
+#endif
 }
 
 static void pcie_dmer_enable(void)
@@ -78,8 +97,10 @@ static void pcie_dmer_enable(void)
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
        mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
+#if CFG_PCIE_NR_PORTS > 2
        mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
                mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
+#endif
 }
 
 static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
@@ -88,6 +109,9 @@ static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
        u8 *address;
        *val = 0;
 
+       if (validate_endpoint(hose))
+               return 0;               /* No upstream config access */
+
        /*
         * Bus numbers are relative to hose->first_busno
         */
@@ -120,6 +144,7 @@ static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
         */
        pcie_dmer_disable ();
 
+       debug("%s: cfg_data=%08x offset=%08x\n", __func__, hose->cfg_data, offset);
        switch (len) {
        case 1:
                *val = in_8(hose->cfg_data + offset);
@@ -142,6 +167,9 @@ static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
 
        u8 *address;
 
+       if (validate_endpoint(hose))
+               return 0;               /* No upstream config access */
+
        /*
         * Bus numbers are relative to hose->first_busno
         */
@@ -227,6 +255,7 @@ int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset
        return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
 }
 
+#if defined(CONFIG_440SPE)
 static void ppc4xx_setup_utl(u32 port) {
 
        volatile void *utl_base = NULL;
@@ -371,6 +400,57 @@ int ppc4xx_init_pcie(void)
        }
        return 0;
 }
+#else
+static void ppc4xx_setup_utl(u32 port)
+{
+       u32 utl_base;
+
+       /*
+        * Map UTL registers at 0xef4f_n000 (4K 0xfff mask) PEGPLn_REGMSK
+        */
+       switch (port) {
+       case 0:
+               mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x00000000);
+               mtdcr(DCRN_PEGPL_REGBAL(PCIE0), CFG_PCIE0_UTLBASE);
+               mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001); /* 4k region, valid */
+               mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
+               break;
+
+       case 1:
+               mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x00000000);
+               mtdcr(DCRN_PEGPL_REGBAL(PCIE1), CFG_PCIE1_UTLBASE);
+               mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001); /* 4k region, valid */
+               mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
+
+               break;
+       }
+       utl_base = (port==0) ? CFG_PCIE0_UTLBASE : CFG_PCIE1_UTLBASE;
+
+       /*
+        * Set buffer allocations and then assert VRB and TXE.
+        */
+       out_be32((u32 *)(utl_base + PEUTL_OUTTR),   0x02000000);
+       out_be32((u32 *)(utl_base + PEUTL_INTR),    0x02000000);
+       out_be32((u32 *)(utl_base + PEUTL_OPDBSZ),  0x04000000);
+       out_be32((u32 *)(utl_base + PEUTL_PBBSZ),   0x21000000);
+       out_be32((u32 *)(utl_base + PEUTL_IPHBSZ),  0x02000000);
+       out_be32((u32 *)(utl_base + PEUTL_IPDBSZ),  0x04000000);
+       out_be32((u32 *)(utl_base + PEUTL_RCIRQEN), 0x00f00000);
+       out_be32((u32 *)(utl_base + PEUTL_PCTL),    0x80800066);
+
+       out_be32((u32 *)(utl_base + PEUTL_PBCTL),   0x0800000c);
+       out_be32((u32 *)(utl_base + PEUTL_RCSTA),
+                in_be32((u32 *)(utl_base + PEUTL_RCSTA)) | 0x000040000);
+}
+
+int ppc4xx_init_pcie(void)
+{
+       /*
+        * Nothing to do on 405EX
+        */
+       return 0;
+}
+#endif
 
 /*
  * Board-specific pcie initialization
@@ -436,20 +516,14 @@ int __ppc4xx_init_pcie_port_hw(int port, int rootport)
 {
        u32 val;
 
-       /*
-        * test-only:
-        * This needs some testing and perhaps changes for
-        * endpoint configuration. Probably no PHY reset at all, etc.
-        * sr, 2007-10-03
-        */
        if (rootport)
                val = 0x00401000;
        else
                val = 0x00101000;
 
        SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
-       SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x20222222);
-       SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01110000);
+       SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x00000000);
+       SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01010000);
        SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
        SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
 
@@ -458,7 +532,10 @@ int __ppc4xx_init_pcie_port_hw(int port, int rootport)
        udelay(1000);
 
        /* deassert the PE0_hotreset */
-       SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
+       if (is_end_point(port))
+               SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01111000);
+       else
+               SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
 
        /* poll for phy !reset */
        while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
@@ -477,7 +554,7 @@ int __ppc4xx_init_pcie_port_hw(int port, int rootport)
 #endif /* CONFIG_405EX */
 
 int ppc4xx_init_pcie_port_hw(int port, int rootport)
-       __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
+__attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
 
 /*
  * We map PCI Express configuration access into the 512MB regions
@@ -577,9 +654,9 @@ int ppc4xx_init_pcie_port(int port, int rootport)
        u32 low, high;
 
        if (!core_init) {
-               ++core_init;
                if (ppc4xx_init_pcie())
                        return -1;
+               ++core_init;
        }
 
        /*
@@ -619,8 +696,8 @@ int ppc4xx_init_pcie_port(int port, int rootport)
         * We map PCI Express configuration access into the 512MB regions
         */
        addr = ppc4xx_get_cfgaddr(port);
-       low = (u32)(addr & 0x00000000ffffffff);
-       high = (u32)(addr >> 32);
+       low = U64_TO_U32_LOW(addr);
+       high = U64_TO_U32_HIGH(addr);
 
        switch (port) {
        case 0:
@@ -633,11 +710,13 @@ int ppc4xx_init_pcie_port(int port, int rootport)
                mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
                mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
                break;
+#if CFG_PCIE_NR_PORTS > 2
        case 2:
                mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
                mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
                mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
                break;
+#endif
        }
 
        /*
@@ -692,11 +771,13 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
                rmbase = (u32 *)CFG_PCIE1_CFGBASE;
                hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
                break;
+#if CFG_PCIE_NR_PORTS > 2
        case 2:
                mbase = (u32 *)CFG_PCIE2_XCFGBASE;
                rmbase = (u32 *)CFG_PCIE2_CFGBASE;
                hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
                break;
+#endif
        }
 
        /*
@@ -720,8 +801,8 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
 
        switch (port) {
        case 0:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
@@ -733,8 +814,8 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
                      mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
                break;
        case 1:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
@@ -745,9 +826,10 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
                      mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
                      mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
                break;
+#if CFG_PCIE_NR_PORTS > 2
        case 2:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
@@ -758,6 +840,7 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
                      mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
                      mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
                break;
+#endif
        }
 
        /* Set up 16GB inbound memory window at 0 */
@@ -770,8 +853,8 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
        out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
        out_le32(mbase + PECFG_PIM0LAL, 0);
        out_le32(mbase + PECFG_PIM0LAH, 0);
-       out_le32(mbase + PECFG_PIM1LAL,  0x00000000);
-       out_le32(mbase + PECFG_PIM1LAH,  0x00000004);
+       out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
+       out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
        out_le32(mbase + PECFG_PIMEN, 0x1);
 
        /* Enable I/O, Mem, and Busmaster cycles */
@@ -780,28 +863,13 @@ void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
 
        /* Set Device and Vendor Id */
-       switch (port) {
-       case 0:
-               out_le16(mbase + 0x200, 0xaaa0);
-               out_le16(mbase + 0x202, 0xbed0);
-               break;
-       case 1:
-               out_le16(mbase + 0x200, 0xaaa1);
-               out_le16(mbase + 0x202, 0xbed1);
-               break;
-       case 2:
-               out_le16(mbase + 0x200, 0xaaa2);
-               out_le16(mbase + 0x202, 0xbed2);
-               break;
-       default:
-               out_le16(mbase + 0x200, 0xaaa3);
-               out_le16(mbase + 0x202, 0xbed3);
-       }
+       out_le16(mbase + 0x200, 0xaaa0 + port);
+       out_le16(mbase + 0x202, 0xbed0 + port);
 
        /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
        out_le32(mbase + 0x208, 0x06040001);
 
-       printf("PCIE:%d successfully set as rootpoint\n", port);
+       printf("PCIE%d: successfully set as root-complex\n", port);
 }
 
 int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
@@ -826,10 +894,12 @@ int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
                mbase = (u32 *)CFG_PCIE1_XCFGBASE;
                hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
                break;
+#if defined(CFG_PCIE2_CFGBASE)
        case 2:
                mbase = (u32 *)CFG_PCIE2_XCFGBASE;
                hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
                break;
+#endif
        }
 
        /*
@@ -843,46 +913,62 @@ int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
 
        switch (port) {
        case 0:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
                      ~(CFG_PCIE_MEMSIZE - 1) | 3);
                break;
        case 1:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
                      ~(CFG_PCIE_MEMSIZE - 1) | 3);
                break;
+#if CFG_PCIE_NR_PORTS > 2
        case 2:
-               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2),  0x0000000d);
-               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2),  CFG_PCIE_MEMBASE +
+               mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
+               mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
                      port * CFG_PCIE_MEMSIZE);
                mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
                mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
                      ~(CFG_PCIE_MEMSIZE - 1) | 3);
                break;
+#endif
        }
 
-       /* Set up 16GB inbound memory window at 0 */
+       /* Set up 64MB inbound memory window at 0 */
        out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
        out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
-       out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
-       out_le32(mbase + PECFG_BAR0LMPA, 0);
-       out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
-       out_le32(mbase + PECFG_PIM0LAH, 0x00000004);    /* pointing to SRAM */
+
+       out_le32(mbase + PECFG_PIM01SAH, 0xffffffff);
+       out_le32(mbase + PECFG_PIM01SAL, 0xfc000000);
+
+       /* Setup BAR0 */
+       out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffff);
+       out_le32(mbase + PECFG_BAR0LMPA, 0xfc000000 | PCI_BASE_ADDRESS_MEM_TYPE_64);
+
+       /* Disable BAR1 & BAR2 */
+       out_le32(mbase + PECFG_BAR1MPA, 0);
+       out_le32(mbase + PECFG_BAR2HMPA, 0);
+       out_le32(mbase + PECFG_BAR2LMPA, 0);
+
+       out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CFG_PCIE_INBOUND_BASE));
+       out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CFG_PCIE_INBOUND_BASE));
        out_le32(mbase + PECFG_PIMEN, 0x1);
 
        /* Enable I/O, Mem, and Busmaster cycles */
        out_le16((u16 *)(mbase + PCI_COMMAND),
                 in_le16((u16 *)(mbase + PCI_COMMAND)) |
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
-       out_le16(mbase + 0x200,0xcaad);                 /* Setting vendor ID */
-       out_le16(mbase + 0x202,0xfeed);                 /* Setting device ID */
+       out_le16(mbase + 0x200, 0xcaad);                /* Setting vendor ID */
+       out_le16(mbase + 0x202, 0xfeed);                /* Setting device ID */
+
+       /* Set Class Code to Processor/PPC */
+       out_le32(mbase + 0x208, 0x0b200001);
 
        attempts = 10;
        while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
@@ -893,7 +979,7 @@ int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
                mdelay(1000);
        }
 
-       printf("PCIE:%d successfully set as endpoint\n",port);
+       printf("PCIE%d: successfully set as endpoint\n", port);
 
        return 0;
 }