Merge tag 'mmc-v4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[cascardo/linux.git] / drivers / pci / host / pcie-designware.c
index 12afce1..035f50c 100644 (file)
@@ -14,7 +14,6 @@
 #include <linux/irq.h>
 #include <linux/irqdomain.h>
 #include <linux/kernel.h>
-#include <linux/module.h>
 #include <linux/msi.h>
 #include <linux/of_address.h>
 #include <linux/of_pci.h>
 
 #include "pcie-designware.h"
 
-/* Synopsis specific PCIE configuration registers */
+/* Parameters for the waiting for link up routine */
+#define LINK_WAIT_MAX_RETRIES          10
+#define LINK_WAIT_USLEEP_MIN           90000
+#define LINK_WAIT_USLEEP_MAX           100000
+
+/* Parameters for the waiting for iATU enabled routine */
+#define LINK_WAIT_MAX_IATU_RETRIES     5
+#define LINK_WAIT_IATU_MIN             9000
+#define LINK_WAIT_IATU_MAX             10000
+
+/* Synopsys-specific PCIe configuration registers */
 #define PCIE_PORT_LINK_CONTROL         0x710
 #define PORT_LINK_MODE_MASK            (0x3f << 16)
 #define PORT_LINK_MODE_1_LANES         (0x1 << 16)
@@ -51,6 +60,7 @@
 #define PCIE_ATU_VIEWPORT              0x900
 #define PCIE_ATU_REGION_INBOUND                (0x1 << 31)
 #define PCIE_ATU_REGION_OUTBOUND       (0x0 << 31)
+#define PCIE_ATU_REGION_INDEX2         (0x2 << 0)
 #define PCIE_ATU_REGION_INDEX1         (0x1 << 0)
 #define PCIE_ATU_REGION_INDEX0         (0x0 << 0)
 #define PCIE_ATU_CR1                   0x904
 #define PCIE_ATU_FUNC(x)               (((x) & 0x7) << 16)
 #define PCIE_ATU_UPPER_TARGET          0x91C
 
+/*
+ * iATU Unroll-specific register definitions
+ * From 4.80 core version the address translation will be made by unroll
+ */
+#define PCIE_ATU_UNR_REGION_CTRL1      0x00
+#define PCIE_ATU_UNR_REGION_CTRL2      0x04
+#define PCIE_ATU_UNR_LOWER_BASE                0x08
+#define PCIE_ATU_UNR_UPPER_BASE                0x0C
+#define PCIE_ATU_UNR_LIMIT             0x10
+#define PCIE_ATU_UNR_LOWER_TARGET      0x14
+#define PCIE_ATU_UNR_UPPER_TARGET      0x18
+
+/* Register address builder */
+#define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region)  ((0x3 << 20) | (region << 9))
+
 /* PCIe Port Logic registers */
 #define PLR_OFFSET                     0x700
 #define PCIE_PHY_DEBUG_R1              (PLR_OFFSET + 0x2c)
-#define PCIE_PHY_DEBUG_R1_LINK_UP      0x00000010
+#define PCIE_PHY_DEBUG_R1_LINK_UP      (0x1 << 4)
+#define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING     (0x1 << 29)
 
 static struct pci_ops dw_pcie_ops;
 
@@ -115,22 +141,37 @@ int dw_pcie_cfg_write(void __iomem *addr, int size, u32 val)
        return PCIBIOS_SUCCESSFUL;
 }
 
-static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val)
+u32 dw_pcie_readl_rc(struct pcie_port *pp, u32 reg)
 {
        if (pp->ops->readl_rc)
-               pp->ops->readl_rc(pp, pp->dbi_base + reg, val);
-       else
-               *val = readl(pp->dbi_base + reg);
+               return pp->ops->readl_rc(pp, reg);
+
+       return readl(pp->dbi_base + reg);
 }
 
-static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg)
+void dw_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val)
 {
        if (pp->ops->writel_rc)
-               pp->ops->writel_rc(pp, val, pp->dbi_base + reg);
+               pp->ops->writel_rc(pp, reg, val);
        else
                writel(val, pp->dbi_base + reg);
 }
 
+static u32 dw_pcie_readl_unroll(struct pcie_port *pp, u32 index, u32 reg)
+{
+       u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
+
+       return dw_pcie_readl_rc(pp, offset + reg);
+}
+
+static void dw_pcie_writel_unroll(struct pcie_port *pp, u32 index, u32 reg,
+                                 u32 val)
+{
+       u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
+
+       dw_pcie_writel_rc(pp, offset + reg, val);
+}
+
 static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
                               u32 *val)
 {
@@ -152,24 +193,57 @@ static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
 static void dw_pcie_prog_outbound_atu(struct pcie_port *pp, int index,
                int type, u64 cpu_addr, u64 pci_addr, u32 size)
 {
-       u32 val;
-
-       dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | index,
-                         PCIE_ATU_VIEWPORT);
-       dw_pcie_writel_rc(pp, lower_32_bits(cpu_addr), PCIE_ATU_LOWER_BASE);
-       dw_pcie_writel_rc(pp, upper_32_bits(cpu_addr), PCIE_ATU_UPPER_BASE);
-       dw_pcie_writel_rc(pp, lower_32_bits(cpu_addr + size - 1),
-                         PCIE_ATU_LIMIT);
-       dw_pcie_writel_rc(pp, lower_32_bits(pci_addr), PCIE_ATU_LOWER_TARGET);
-       dw_pcie_writel_rc(pp, upper_32_bits(pci_addr), PCIE_ATU_UPPER_TARGET);
-       dw_pcie_writel_rc(pp, type, PCIE_ATU_CR1);
-       dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
+       u32 retries, val;
+
+       if (pp->iatu_unroll_enabled) {
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LOWER_BASE,
+                       lower_32_bits(cpu_addr));
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_UPPER_BASE,
+                       upper_32_bits(cpu_addr));
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LIMIT,
+                       lower_32_bits(cpu_addr + size - 1));
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LOWER_TARGET,
+                       lower_32_bits(pci_addr));
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_UPPER_TARGET,
+                       upper_32_bits(pci_addr));
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_REGION_CTRL1,
+                       type);
+               dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_REGION_CTRL2,
+                       PCIE_ATU_ENABLE);
+       } else {
+               dw_pcie_writel_rc(pp, PCIE_ATU_VIEWPORT,
+                                 PCIE_ATU_REGION_OUTBOUND | index);
+               dw_pcie_writel_rc(pp, PCIE_ATU_LOWER_BASE,
+                                 lower_32_bits(cpu_addr));
+               dw_pcie_writel_rc(pp, PCIE_ATU_UPPER_BASE,
+                                 upper_32_bits(cpu_addr));
+               dw_pcie_writel_rc(pp, PCIE_ATU_LIMIT,
+                                 lower_32_bits(cpu_addr + size - 1));
+               dw_pcie_writel_rc(pp, PCIE_ATU_LOWER_TARGET,
+                                 lower_32_bits(pci_addr));
+               dw_pcie_writel_rc(pp, PCIE_ATU_UPPER_TARGET,
+                                 upper_32_bits(pci_addr));
+               dw_pcie_writel_rc(pp, PCIE_ATU_CR1, type);
+               dw_pcie_writel_rc(pp, PCIE_ATU_CR2, PCIE_ATU_ENABLE);
+       }
 
        /*
         * Make sure ATU enable takes effect before any subsequent config
         * and I/O accesses.
         */
-       dw_pcie_readl_rc(pp, PCIE_ATU_CR2, &val);
+       for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
+               if (pp->iatu_unroll_enabled)
+                       val = dw_pcie_readl_unroll(pp, index,
+                                                  PCIE_ATU_UNR_REGION_CTRL2);
+               else
+                       val = dw_pcie_readl_rc(pp, PCIE_ATU_CR2);
+
+               if (val == PCIE_ATU_ENABLE)
+                       return;
+
+               usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
+       }
+       dev_err(pp->dev, "iATU is not being enabled\n");
 }
 
 static struct irq_chip dw_msi_irq_chip = {
@@ -412,7 +486,8 @@ int dw_pcie_link_up(struct pcie_port *pp)
                return pp->ops->link_up(pp);
 
        val = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
-       return val & PCIE_PHY_DEBUG_R1_LINK_UP;
+       return ((val & PCIE_PHY_DEBUG_R1_LINK_UP) &&
+               (!(val & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING)));
 }
 
 static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
@@ -428,6 +503,17 @@ static const struct irq_domain_ops msi_domain_ops = {
        .map = dw_pcie_msi_map,
 };
 
+static u8 dw_pcie_iatu_unroll_enabled(struct pcie_port *pp)
+{
+       u32 val;
+
+       val = dw_pcie_readl_rc(pp, PCIE_ATU_VIEWPORT);
+       if (val == 0xffffffff)
+               return 1;
+
+       return 0;
+}
+
 int dw_pcie_host_init(struct pcie_port *pp)
 {
        struct device_node *np = pp->dev->of_node;
@@ -436,7 +522,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
        struct resource *cfg_res;
        int i, ret;
        LIST_HEAD(res);
-       struct resource_entry *win;
+       struct resource_entry *win, *tmp;
 
        cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
        if (cfg_res) {
@@ -457,17 +543,20 @@ int dw_pcie_host_init(struct pcie_port *pp)
                goto error;
 
        /* Get the I/O and memory ranges from DT */
-       resource_list_for_each_entry(win, &res) {
+       resource_list_for_each_entry_safe(win, tmp, &res) {
                switch (resource_type(win->res)) {
                case IORESOURCE_IO:
-                       pp->io = win->res;
-                       pp->io->name = "I/O";
-                       pp->io_size = resource_size(pp->io);
-                       pp->io_bus_addr = pp->io->start - win->offset;
-                       ret = pci_remap_iospace(pp->io, pp->io_base);
-                       if (ret)
+                       ret = pci_remap_iospace(win->res, pp->io_base);
+                       if (ret) {
                                dev_warn(pp->dev, "error %d: failed to map resource %pR\n",
-                                        ret, pp->io);
+                                        ret, win->res);
+                               resource_list_destroy_entry(win);
+                       } else {
+                               pp->io = win->res;
+                               pp->io->name = "I/O";
+                               pp->io_size = resource_size(pp->io);
+                               pp->io_bus_addr = pp->io->start - win->offset;
+                       }
                        break;
                case IORESOURCE_MEM:
                        pp->mem = win->res;
@@ -524,6 +613,10 @@ int dw_pcie_host_init(struct pcie_port *pp)
        if (ret)
                pp->lanes = 0;
 
+       ret = of_property_read_u32(np, "num-viewport", &pp->num_viewport);
+       if (ret)
+               pp->num_viewport = 2;
+
        if (IS_ENABLED(CONFIG_PCI_MSI)) {
                if (!pp->ops->msi_host_init) {
                        pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
@@ -544,6 +637,8 @@ int dw_pcie_host_init(struct pcie_port *pp)
                }
        }
 
+       pp->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pp);
+
        if (pp->ops->host_init)
                pp->ops->host_init(pp);
 
@@ -609,13 +704,14 @@ static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
                va_cfg_base = pp->va_cfg1_base;
        }
 
-       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
+       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
                                  type, cpu_addr,
                                  busdev, cfg_size);
        ret = dw_pcie_cfg_read(va_cfg_base + where, size, val);
-       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
-                                 PCIE_ATU_TYPE_IO, pp->io_base,
-                                 pp->io_bus_addr, pp->io_size);
+       if (pp->num_viewport <= 2)
+               dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
+                                         PCIE_ATU_TYPE_IO, pp->io_base,
+                                         pp->io_bus_addr, pp->io_size);
 
        return ret;
 }
@@ -646,19 +742,20 @@ static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
                va_cfg_base = pp->va_cfg1_base;
        }
 
-       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
+       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
                                  type, cpu_addr,
                                  busdev, cfg_size);
        ret = dw_pcie_cfg_write(va_cfg_base + where, size, val);
-       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
-                                 PCIE_ATU_TYPE_IO, pp->io_base,
-                                 pp->io_bus_addr, pp->io_size);
+       if (pp->num_viewport <= 2)
+               dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
+                                         PCIE_ATU_TYPE_IO, pp->io_base,
+                                         pp->io_bus_addr, pp->io_size);
 
        return ret;
 }
 
-static int dw_pcie_valid_config(struct pcie_port *pp,
-                               struct pci_bus *bus, int dev)
+static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
+                               int dev)
 {
        /* If there is no link, then there is no device */
        if (bus->number != pp->root_bus_nr) {
@@ -670,13 +767,6 @@ static int dw_pcie_valid_config(struct pcie_port *pp,
        if (bus->number == pp->root_bus_nr && dev > 0)
                return 0;
 
-       /*
-        * do not read more than one device on the bus directly attached
-        * to RC's (Virtual Bridge's) DS side.
-        */
-       if (bus->primary == pp->root_bus_nr && dev > 0)
-               return 0;
-
        return 1;
 }
 
@@ -685,7 +775,7 @@ static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
 {
        struct pcie_port *pp = bus->sysdata;
 
-       if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
+       if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
                *val = 0xffffffff;
                return PCIBIOS_DEVICE_NOT_FOUND;
        }
@@ -701,7 +791,7 @@ static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
 {
        struct pcie_port *pp = bus->sysdata;
 
-       if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
+       if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
                return PCIBIOS_DEVICE_NOT_FOUND;
 
        if (bus->number == pp->root_bus_nr)
@@ -720,7 +810,7 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
        u32 val;
 
        /* set the number of lanes */
-       dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
+       val = dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL);
        val &= ~PORT_LINK_MODE_MASK;
        switch (pp->lanes) {
        case 1:
@@ -739,10 +829,10 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
                dev_err(pp->dev, "num-lanes %u: invalid value\n", pp->lanes);
                return;
        }
-       dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
+       dw_pcie_writel_rc(pp, PCIE_PORT_LINK_CONTROL, val);
 
        /* set link width speed control register */
-       dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
+       val = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
        val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
        switch (pp->lanes) {
        case 1:
@@ -758,40 +848,45 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
                val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
                break;
        }
-       dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
+       dw_pcie_writel_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
 
        /* setup RC BARs */
-       dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
-       dw_pcie_writel_rc(pp, 0x00000000, PCI_BASE_ADDRESS_1);
+       dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, 0x00000004);
+       dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_1, 0x00000000);
 
        /* setup interrupt pins */
-       dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
+       val = dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE);
        val &= 0xffff00ff;
        val |= 0x00000100;
-       dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
+       dw_pcie_writel_rc(pp, PCI_INTERRUPT_LINE, val);
 
        /* setup bus numbers */
-       dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
+       val = dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS);
        val &= 0xff000000;
        val |= 0x00010100;
-       dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
+       dw_pcie_writel_rc(pp, PCI_PRIMARY_BUS, val);
 
        /* setup command register */
-       dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
+       val = dw_pcie_readl_rc(pp, PCI_COMMAND);
        val &= 0xffff0000;
        val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
-       dw_pcie_writel_rc(pp, val, PCI_COMMAND);
+       dw_pcie_writel_rc(pp, PCI_COMMAND, val);
 
        /*
         * If the platform provides ->rd_other_conf, it means the platform
         * uses its own address translation component rather than ATU, so
         * we should not program the ATU here.
         */
-       if (!pp->ops->rd_other_conf)
-               dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
+       if (!pp->ops->rd_other_conf) {
+               dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
                                          PCIE_ATU_TYPE_MEM, pp->mem_base,
                                          pp->mem_bus_addr, pp->mem_size);
+               if (pp->num_viewport > 2)
+                       dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX2,
+                                                 PCIE_ATU_TYPE_IO, pp->io_base,
+                                                 pp->io_bus_addr, pp->io_size);
+       }
 
        dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
 
@@ -802,7 +897,3 @@ void dw_pcie_setup_rc(struct pcie_port *pp)
        val |= PORT_LOGIC_SPEED_CHANGE;
        dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
 }
-
-MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
-MODULE_DESCRIPTION("Designware PCIe host controller driver");
-MODULE_LICENSE("GPL v2");