]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/pci/host/pcie-rockchip.c
PCI: rockchip: Use normal register bank for config accessors
[karo-tx-linux.git] / drivers / pci / host / pcie-rockchip.c
index f2dca7bb0b39f8d02537ba985f958a19d1bfcf14..5acf8694fb23bc41d6000c27ae43c425fe478f8a 100644 (file)
 #include <linux/gpio/consumer.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
+#include <linux/iopoll.h>
 #include <linux/irq.h>
 #include <linux/irqchip/chained_irq.h>
 #include <linux/irqdomain.h>
 #include <linux/kernel.h>
 #include <linux/mfd/syscon.h>
+#include <linux/module.h>
 #include <linux/of_address.h>
 #include <linux/of_device.h>
 #include <linux/of_pci.h>
 #define   PCIE_CLIENT_MODE_RC            HIWORD_UPDATE_BIT(0x0040)
 #define   PCIE_CLIENT_GEN_SEL_1                  HIWORD_UPDATE(0x0080, 0)
 #define   PCIE_CLIENT_GEN_SEL_2                  HIWORD_UPDATE_BIT(0x0080)
+#define PCIE_CLIENT_DEBUG_OUT_0                (PCIE_CLIENT_BASE + 0x3c)
+#define   PCIE_CLIENT_DEBUG_LTSSM_MASK         GENMASK(5, 0)
+#define   PCIE_CLIENT_DEBUG_LTSSM_L1           0x18
+#define   PCIE_CLIENT_DEBUG_LTSSM_L2           0x19
 #define PCIE_CLIENT_BASIC_STATUS1      (PCIE_CLIENT_BASE + 0x48)
 #define   PCIE_CLIENT_LINK_STATUS_UP           0x00300000
 #define   PCIE_CLIENT_LINK_STATUS_MASK         0x00300000
 #define   PCIE_CORE_INT_CT                     BIT(11)
 #define   PCIE_CORE_INT_UTC                    BIT(18)
 #define   PCIE_CORE_INT_MMVC                   BIT(19)
+#define PCIE_CORE_CONFIG_VENDOR                (PCIE_CORE_CTRL_MGMT_BASE + 0x44)
 #define PCIE_CORE_INT_MASK             (PCIE_CORE_CTRL_MGMT_BASE + 0x210)
 #define PCIE_RC_BAR_CONF               (PCIE_CORE_CTRL_MGMT_BASE + 0x300)
 
                 PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \
                 PCIE_CORE_INT_MMVC)
 
+#define PCIE_RC_CONFIG_NORMAL_BASE     0x800000
 #define PCIE_RC_CONFIG_BASE            0xa00000
-#define PCIE_RC_CONFIG_VENDOR          (PCIE_RC_CONFIG_BASE + 0x00)
 #define PCIE_RC_CONFIG_RID_CCR         (PCIE_RC_CONFIG_BASE + 0x08)
 #define   PCIE_RC_CONFIG_SCC_SHIFT             16
 #define PCIE_RC_CONFIG_DCR             (PCIE_RC_CONFIG_BASE + 0xc4)
 #define   PCIE_RC_CONFIG_DCR_CSPL_SHIFT                18
 #define   PCIE_RC_CONFIG_DCR_CSPL_LIMIT                0xff
 #define   PCIE_RC_CONFIG_DCR_CPLS_SHIFT                26
+#define PCIE_RC_CONFIG_DCSR            (PCIE_RC_CONFIG_BASE + 0xc8)
+#define   PCIE_RC_CONFIG_DCSR_MPS_MASK         GENMASK(7, 5)
+#define   PCIE_RC_CONFIG_DCSR_MPS_256          (0x1 << 5)
+#define PCIE_RC_CONFIG_LINK_CAP                (PCIE_RC_CONFIG_BASE + 0xcc)
+#define   PCIE_RC_CONFIG_LINK_CAP_L0S          BIT(10)
 #define PCIE_RC_CONFIG_LCS             (PCIE_RC_CONFIG_BASE + 0xd0)
 #define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
 #define PCIE_RC_CONFIG_THP_CAP         (PCIE_RC_CONFIG_BASE + 0x274)
 #define IB_ROOT_PORT_REG_SIZE_SHIFT            3
 #define AXI_WRAPPER_IO_WRITE                   0x6
 #define AXI_WRAPPER_MEM_WRITE                  0x2
+#define AXI_WRAPPER_TYPE0_CFG                  0xa
+#define AXI_WRAPPER_TYPE1_CFG                  0xb
+#define AXI_WRAPPER_NOR_MSG                    0xc
 
 #define MAX_AXI_IB_ROOTPORT_REGION_NUM         3
 #define MIN_AXI_ADDR_BITS_PASSED               8
+#define PCIE_RC_SEND_PME_OFF                   0x11960
 #define ROCKCHIP_VENDOR_ID                     0x1d87
 #define PCIE_ECAM_BUS(x)                       (((x) & 0xff) << 20)
 #define PCIE_ECAM_DEV(x)                       (((x) & 0x1f) << 15)
 #define PCIE_ECAM_ADDR(bus, dev, func, reg) \
          (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
           PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
+#define PCIE_LINK_IS_L2(x) \
+       (((x) & PCIE_CLIENT_DEBUG_LTSSM_MASK) == PCIE_CLIENT_DEBUG_LTSSM_L2)
+#define PCIE_LINK_UP(x) \
+       (((x) & PCIE_CLIENT_LINK_STATUS_MASK) == PCIE_CLIENT_LINK_STATUS_UP)
+#define PCIE_LINK_IS_GEN2(x) \
+       (((x) & PCIE_CORE_PL_CONF_SPEED_MASK) == PCIE_CORE_PL_CONF_SPEED_5G)
 
 #define RC_REGION_0_ADDR_TRANS_H               0x00000000
 #define RC_REGION_0_ADDR_TRANS_L               0x00000000
 #define RC_REGION_0_PASS_BITS                  (25 - 1)
+#define RC_REGION_0_TYPE_MASK                  GENMASK(3, 0)
 #define MAX_AXI_WRAPPER_REGION_NUM             33
 
 struct rockchip_pcie {
@@ -208,10 +231,14 @@ struct rockchip_pcie {
        int     link_gen;
        struct  device *dev;
        struct  irq_domain *irq_domain;
-       u32     io_size;
        int     offset;
+       struct pci_bus *root_bus;
+       struct resource *io;
        phys_addr_t io_bus_addr;
+       u32     io_size;
+       void    __iomem *msg_region;
        u32     mem_size;
+       phys_addr_t msg_bus_addr;
        phys_addr_t mem_bus_addr;
 };
 
@@ -275,7 +302,9 @@ static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
 static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
                                     int where, int size, u32 *val)
 {
-       void __iomem *addr = rockchip->apb_base + PCIE_RC_CONFIG_BASE + where;
+       void __iomem *addr;
+
+       addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + where;
 
        if (!IS_ALIGNED((uintptr_t)addr, size)) {
                *val = 0;
@@ -299,11 +328,13 @@ static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
                                     int where, int size, u32 val)
 {
        u32 mask, tmp, offset;
+       void __iomem *addr;
 
        offset = where & ~0x3;
+       addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + offset;
 
        if (size == 4) {
-               writel(val, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
+               writel(val, addr);
                return PCIBIOS_SUCCESSFUL;
        }
 
@@ -314,13 +345,33 @@ static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
         * corrupt RW1C bits in adjacent registers.  But the hardware
         * doesn't support smaller writes.
         */
-       tmp = readl(rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset) & mask;
+       tmp = readl(addr) & mask;
        tmp |= val << ((where & 0x3) * 8);
-       writel(tmp, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
+       writel(tmp, addr);
 
        return PCIBIOS_SUCCESSFUL;
 }
 
+static void rockchip_pcie_cfg_configuration_accesses(
+               struct rockchip_pcie *rockchip, u32 type)
+{
+       u32 ob_desc_0;
+
+       /* Configuration Accesses for region 0 */
+       rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
+
+       rockchip_pcie_write(rockchip,
+                           (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
+                           PCIE_CORE_OB_REGION_ADDR0);
+       rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
+                           PCIE_CORE_OB_REGION_ADDR1);
+       ob_desc_0 = rockchip_pcie_read(rockchip, PCIE_CORE_OB_REGION_DESC0);
+       ob_desc_0 &= ~(RC_REGION_0_TYPE_MASK);
+       ob_desc_0 |= (type | (0x1 << 23));
+       rockchip_pcie_write(rockchip, ob_desc_0, PCIE_CORE_OB_REGION_DESC0);
+       rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
+}
+
 static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
                                       struct pci_bus *bus, u32 devfn,
                                       int where, int size, u32 *val)
@@ -335,6 +386,13 @@ static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
                return PCIBIOS_BAD_REGISTER_NUMBER;
        }
 
+       if (bus->parent->number == rockchip->root_bus_nr)
+               rockchip_pcie_cfg_configuration_accesses(rockchip,
+                                               AXI_WRAPPER_TYPE0_CFG);
+       else
+               rockchip_pcie_cfg_configuration_accesses(rockchip,
+                                               AXI_WRAPPER_TYPE1_CFG);
+
        if (size == 4) {
                *val = readl(rockchip->reg_base + busdev);
        } else if (size == 2) {
@@ -359,6 +417,13 @@ static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
        if (!IS_ALIGNED(busdev, size))
                return PCIBIOS_BAD_REGISTER_NUMBER;
 
+       if (bus->parent->number == rockchip->root_bus_nr)
+               rockchip_pcie_cfg_configuration_accesses(rockchip,
+                                               AXI_WRAPPER_TYPE0_CFG);
+       else
+               rockchip_pcie_cfg_configuration_accesses(rockchip,
+                                               AXI_WRAPPER_TYPE1_CFG);
+
        if (size == 4)
                writel(val, rockchip->reg_base + busdev);
        else if (size == 2)
@@ -408,7 +473,8 @@ static struct pci_ops rockchip_pcie_ops = {
 
 static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
 {
-       u32 status, curr, scale, power;
+       int curr;
+       u32 status, scale, power;
 
        if (IS_ERR(rockchip->vpcie3v3))
                return;
@@ -420,24 +486,25 @@ static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
         * to the actual power supply.
         */
        curr = regulator_get_current_limit(rockchip->vpcie3v3);
-       if (curr > 0) {
-               scale = 3; /* 0.001x */
-               curr = curr / 1000; /* convert to mA */
-               power = (curr * 3300) / 1000; /* milliwatt */
-               while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
-                       if (!scale) {
-                               dev_warn(rockchip->dev, "invalid power supply\n");
-                               return;
-                       }
-                       scale--;
-                       power = power / 10;
-               }
+       if (curr <= 0)
+               return;
 
-               status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
-               status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
-                         (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
-               rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
+       scale = 3; /* 0.001x */
+       curr = curr / 1000; /* convert to mA */
+       power = (curr * 3300) / 1000; /* milliwatt */
+       while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
+               if (!scale) {
+                       dev_warn(rockchip->dev, "invalid power supply\n");
+                       return;
+               }
+               scale--;
+               power = power / 10;
        }
+
+       status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
+       status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
+                 (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
+       rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
 }
 
 /**
@@ -449,7 +516,6 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
        struct device *dev = rockchip->dev;
        int err;
        u32 status;
-       unsigned long timeout;
 
        gpiod_set_value(rockchip->ep_gpio, 0);
 
@@ -580,7 +646,12 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
 
        /* Set RC's clock architecture as common clock */
        status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
-       status |= PCI_EXP_LNKCTL_CCC;
+       status |= PCI_EXP_LNKSTA_SLC << 16;
+       rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
+
+       /* Set RC's RCB to 128 */
+       status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
+       status |= PCI_EXP_LNKCTL_RCB;
        rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
 
        /* Enable Gen1 training */
@@ -590,23 +661,12 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
        gpiod_set_value(rockchip->ep_gpio, 1);
 
        /* 500ms timeout value should be enough for Gen1/2 training */
-       timeout = jiffies + msecs_to_jiffies(500);
-
-       for (;;) {
-               status = rockchip_pcie_read(rockchip,
-                                           PCIE_CLIENT_BASIC_STATUS1);
-               if ((status & PCIE_CLIENT_LINK_STATUS_MASK) ==
-                   PCIE_CLIENT_LINK_STATUS_UP) {
-                       dev_dbg(dev, "PCIe link training gen1 pass!\n");
-                       break;
-               }
-
-               if (time_after(jiffies, timeout)) {
-                       dev_err(dev, "PCIe link training gen1 timeout!\n");
-                       return -ETIMEDOUT;
-               }
-
-               msleep(20);
+       err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_BASIC_STATUS1,
+                                status, PCIE_LINK_UP(status), 20,
+                                500 * USEC_PER_MSEC);
+       if (err) {
+               dev_err(dev, "PCIe link training gen1 timeout!\n");
+               return -ETIMEDOUT;
        }
 
        if (rockchip->link_gen == 2) {
@@ -618,22 +678,11 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
                status |= PCI_EXP_LNKCTL_RL;
                rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
 
-               timeout = jiffies + msecs_to_jiffies(500);
-               for (;;) {
-                       status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
-                       if ((status & PCIE_CORE_PL_CONF_SPEED_MASK) ==
-                           PCIE_CORE_PL_CONF_SPEED_5G) {
-                               dev_dbg(dev, "PCIe link training gen2 pass!\n");
-                               break;
-                       }
-
-                       if (time_after(jiffies, timeout)) {
-                               dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
-                               break;
-                       }
-
-                       msleep(20);
-               }
+               err = readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
+                                        status, PCIE_LINK_IS_GEN2(status), 20,
+                                        500 * USEC_PER_MSEC);
+               if (err)
+                       dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
        }
 
        /* Check the final link width from negotiated lane counter from MGMT */
@@ -643,7 +692,7 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
        dev_dbg(dev, "current link width is x%d\n", status);
 
        rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
-                           PCIE_RC_CONFIG_VENDOR);
+                           PCIE_CORE_CONFIG_VENDOR);
        rockchip_pcie_write(rockchip,
                            PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT,
                            PCIE_RC_CONFIG_RID_CCR);
@@ -653,15 +702,17 @@ static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
        status &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK;
        rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_THP_CAP);
 
-       rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
+       /* Clear L0s from RC's link cap */
+       if (of_property_read_bool(dev->of_node, "aspm-no-l0s")) {
+               status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LINK_CAP);
+               status &= ~PCIE_RC_CONFIG_LINK_CAP_L0S;
+               rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LINK_CAP);
+       }
 
-       rockchip_pcie_write(rockchip,
-                           (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
-                           PCIE_CORE_OB_REGION_ADDR0);
-       rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
-                           PCIE_CORE_OB_REGION_ADDR1);
-       rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
-       rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
+       status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCSR);
+       status &= ~PCIE_RC_CONFIG_DCSR_MPS_MASK;
+       status |= PCIE_RC_CONFIG_DCSR_MPS_256;
+       rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCSR);
 
        return 0;
 }
@@ -821,7 +872,7 @@ static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
        regs = platform_get_resource_byname(pdev,
                                            IORESOURCE_MEM,
                                            "axi-base");
-       rockchip->reg_base = devm_ioremap_resource(dev, regs);
+       rockchip->reg_base = devm_pci_remap_cfg_resource(dev, regs);
        if (IS_ERR(rockchip->reg_base))
                return PTR_ERR(rockchip->reg_base);
 
@@ -1145,13 +1196,16 @@ static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
        return 0;
 }
 
-static int rockchip_cfg_atu(struct rockchip_pcie *rockchip)
+static int rockchip_pcie_cfg_atu(struct rockchip_pcie *rockchip)
 {
        struct device *dev = rockchip->dev;
        int offset;
        int err;
        int reg_no;
 
+       rockchip_pcie_cfg_configuration_accesses(rockchip,
+                                                AXI_WRAPPER_TYPE0_CFG);
+
        for (reg_no = 0; reg_no < (rockchip->mem_size >> 20); reg_no++) {
                err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
                                                AXI_WRAPPER_MEM_WRITE,
@@ -1186,7 +1240,119 @@ static int rockchip_cfg_atu(struct rockchip_pcie *rockchip)
                }
        }
 
+       /* assign message regions */
+       rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
+                                 AXI_WRAPPER_NOR_MSG,
+                                 20 - 1, 0, 0);
+
+       rockchip->msg_bus_addr = rockchip->mem_bus_addr +
+                                       ((reg_no + offset) << 20);
+       return err;
+}
+
+static int rockchip_pcie_wait_l2(struct rockchip_pcie *rockchip)
+{
+       u32 value;
+       int err;
+
+       /* send PME_TURN_OFF message */
+       writel(0x0, rockchip->msg_region + PCIE_RC_SEND_PME_OFF);
+
+       /* read LTSSM and wait for falling into L2 link state */
+       err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_DEBUG_OUT_0,
+                                value, PCIE_LINK_IS_L2(value), 20,
+                                jiffies_to_usecs(5 * HZ));
+       if (err) {
+               dev_err(rockchip->dev, "PCIe link enter L2 timeout!\n");
+               return err;
+       }
+
+       return 0;
+}
+
+static int __maybe_unused rockchip_pcie_suspend_noirq(struct device *dev)
+{
+       struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
+       int ret;
+
+       /* disable core and cli int since we don't need to ack PME_ACK */
+       rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) |
+                           PCIE_CLIENT_INT_CLI, PCIE_CLIENT_INT_MASK);
+       rockchip_pcie_write(rockchip, (u32)PCIE_CORE_INT, PCIE_CORE_INT_MASK);
+
+       ret = rockchip_pcie_wait_l2(rockchip);
+       if (ret) {
+               rockchip_pcie_enable_interrupts(rockchip);
+               return ret;
+       }
+
+       phy_power_off(rockchip->phy);
+       phy_exit(rockchip->phy);
+
+       clk_disable_unprepare(rockchip->clk_pcie_pm);
+       clk_disable_unprepare(rockchip->hclk_pcie);
+       clk_disable_unprepare(rockchip->aclk_perf_pcie);
+       clk_disable_unprepare(rockchip->aclk_pcie);
+
+       if (!IS_ERR(rockchip->vpcie0v9))
+               regulator_disable(rockchip->vpcie0v9);
+
+       return ret;
+}
+
+static int __maybe_unused rockchip_pcie_resume_noirq(struct device *dev)
+{
+       struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
+       int err;
+
+       if (!IS_ERR(rockchip->vpcie0v9)) {
+               err = regulator_enable(rockchip->vpcie0v9);
+               if (err) {
+                       dev_err(dev, "fail to enable vpcie0v9 regulator\n");
+                       return err;
+               }
+       }
+
+       err = clk_prepare_enable(rockchip->clk_pcie_pm);
+       if (err)
+               goto err_pcie_pm;
+
+       err = clk_prepare_enable(rockchip->hclk_pcie);
+       if (err)
+               goto err_hclk_pcie;
+
+       err = clk_prepare_enable(rockchip->aclk_perf_pcie);
+       if (err)
+               goto err_aclk_perf_pcie;
+
+       err = clk_prepare_enable(rockchip->aclk_pcie);
+       if (err)
+               goto err_aclk_pcie;
+
+       err = rockchip_pcie_init_port(rockchip);
+       if (err)
+               goto err_pcie_resume;
+
+       err = rockchip_pcie_cfg_atu(rockchip);
+       if (err)
+               goto err_pcie_resume;
+
+       /* Need this to enter L1 again */
+       rockchip_pcie_update_txcredit_mui(rockchip);
+       rockchip_pcie_enable_interrupts(rockchip);
+
        return 0;
+
+err_pcie_resume:
+       clk_disable_unprepare(rockchip->aclk_pcie);
+err_aclk_pcie:
+       clk_disable_unprepare(rockchip->aclk_perf_pcie);
+err_aclk_perf_pcie:
+       clk_disable_unprepare(rockchip->hclk_pcie);
+err_hclk_pcie:
+       clk_disable_unprepare(rockchip->clk_pcie_pm);
+err_pcie_pm:
+       return err;
 }
 
 static int rockchip_pcie_probe(struct platform_device *pdev)
@@ -1194,6 +1360,7 @@ static int rockchip_pcie_probe(struct platform_device *pdev)
        struct rockchip_pcie *rockchip;
        struct device *dev = &pdev->dev;
        struct pci_bus *bus, *child;
+       struct pci_host_bridge *bridge;
        struct resource_entry *win;
        resource_size_t io_base;
        struct resource *mem;
@@ -1205,10 +1372,13 @@ static int rockchip_pcie_probe(struct platform_device *pdev)
        if (!dev->of_node)
                return -ENODEV;
 
-       rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
-       if (!rockchip)
+       bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rockchip));
+       if (!bridge)
                return -ENOMEM;
 
+       rockchip = pci_host_bridge_priv(bridge);
+
+       platform_set_drvdata(pdev, rockchip);
        rockchip->dev = dev;
 
        err = rockchip_pcie_parse_dt(rockchip);
@@ -1262,7 +1432,7 @@ static int rockchip_pcie_probe(struct platform_device *pdev)
 
        err = devm_request_pci_bus_resources(dev, &res);
        if (err)
-               goto err_vpcie;
+               goto err_free_res;
 
        /* Get the I/O and memory ranges from DT */
        resource_list_for_each_entry(win, &res) {
@@ -1278,6 +1448,7 @@ static int rockchip_pcie_probe(struct platform_device *pdev)
                                         err, io);
                                continue;
                        }
+                       rockchip->io = io;
                        break;
                case IORESOURCE_MEM:
                        mem = win->res;
@@ -1293,23 +1464,42 @@ static int rockchip_pcie_probe(struct platform_device *pdev)
                }
        }
 
-       err = rockchip_cfg_atu(rockchip);
+       err = rockchip_pcie_cfg_atu(rockchip);
        if (err)
-               goto err_vpcie;
-       bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
-       if (!bus) {
+               goto err_free_res;
+
+       rockchip->msg_region = devm_ioremap(dev, rockchip->msg_bus_addr, SZ_1M);
+       if (!rockchip->msg_region) {
                err = -ENOMEM;
-               goto err_vpcie;
+               goto err_free_res;
        }
 
+       list_splice_init(&res, &bridge->windows);
+       bridge->dev.parent = dev;
+       bridge->sysdata = rockchip;
+       bridge->busnr = 0;
+       bridge->ops = &rockchip_pcie_ops;
+       bridge->map_irq = of_irq_parse_and_map_pci;
+       bridge->swizzle_irq = pci_common_swizzle;
+
+       err = pci_scan_root_bus_bridge(bridge);
+       if (!err)
+               goto err_free_res;
+
+       bus = bridge->bus;
+
+       rockchip->root_bus = bus;
+
        pci_bus_size_bridges(bus);
        pci_bus_assign_resources(bus);
        list_for_each_entry(child, &bus->children, node)
                pcie_bus_configure_settings(child);
 
        pci_bus_add_devices(bus);
-       return err;
+       return 0;
 
+err_free_res:
+       pci_free_resource_list(&res);
 err_vpcie:
        if (!IS_ERR(rockchip->vpcie3v3))
                regulator_disable(rockchip->vpcie3v3);
@@ -1329,17 +1519,56 @@ err_aclk_pcie:
        return err;
 }
 
+static int rockchip_pcie_remove(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
+
+       pci_stop_root_bus(rockchip->root_bus);
+       pci_remove_root_bus(rockchip->root_bus);
+       pci_unmap_iospace(rockchip->io);
+       irq_domain_remove(rockchip->irq_domain);
+
+       phy_power_off(rockchip->phy);
+       phy_exit(rockchip->phy);
+
+       clk_disable_unprepare(rockchip->clk_pcie_pm);
+       clk_disable_unprepare(rockchip->hclk_pcie);
+       clk_disable_unprepare(rockchip->aclk_perf_pcie);
+       clk_disable_unprepare(rockchip->aclk_pcie);
+
+       if (!IS_ERR(rockchip->vpcie3v3))
+               regulator_disable(rockchip->vpcie3v3);
+       if (!IS_ERR(rockchip->vpcie1v8))
+               regulator_disable(rockchip->vpcie1v8);
+       if (!IS_ERR(rockchip->vpcie0v9))
+               regulator_disable(rockchip->vpcie0v9);
+
+       return 0;
+}
+
+static const struct dev_pm_ops rockchip_pcie_pm_ops = {
+       SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_pcie_suspend_noirq,
+                                     rockchip_pcie_resume_noirq)
+};
+
 static const struct of_device_id rockchip_pcie_of_match[] = {
        { .compatible = "rockchip,rk3399-pcie", },
        {}
 };
+MODULE_DEVICE_TABLE(of, rockchip_pcie_of_match);
 
 static struct platform_driver rockchip_pcie_driver = {
        .driver = {
                .name = "rockchip-pcie",
                .of_match_table = rockchip_pcie_of_match,
+               .pm = &rockchip_pcie_pm_ops,
        },
        .probe = rockchip_pcie_probe,
-
+       .remove = rockchip_pcie_remove,
 };
-builtin_platform_driver(rockchip_pcie_driver);
+module_platform_driver(rockchip_pcie_driver);
+
+MODULE_AUTHOR("Rockchip Inc");
+MODULE_DESCRIPTION("Rockchip AXI PCIe driver");
+MODULE_LICENSE("GPL v2");