#include <linux/configfs.h>
 #include <linux/delay.h>
+#include <linux/gpio/consumer.h>
 #include <linux/iopoll.h>
 #include <linux/kernel.h>
+#include <linux/irq.h>
 #include <linux/of.h>
 #include <linux/pci-epc.h>
 #include <linux/platform_device.h>
        u64                     irq_pci_addr;
        u8                      irq_pci_fn;
        u8                      irq_pending;
+       int                     perst_irq;
+       bool                    perst_asserted;
+       bool                    link_up;
        struct delayed_work     link_training;
 };
 
 
        rockchip_pcie_write(rockchip, cfg, PCIE_CORE_PHY_FUNC_CFG);
 
+       if (rockchip->perst_gpio)
+               enable_irq(ep->perst_irq);
+
        /* Enable configuration and start link training */
        rockchip_pcie_write(rockchip,
                            PCIE_CLIENT_LINK_TRAIN_ENABLE |
                            PCIE_CLIENT_CONF_ENABLE,
                            PCIE_CLIENT_CONFIG);
 
-       schedule_delayed_work(&ep->link_training, 0);
+       if (!rockchip->perst_gpio)
+               schedule_delayed_work(&ep->link_training, 0);
 
        return 0;
 }
        struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
        struct rockchip_pcie *rockchip = &ep->rockchip;
 
+       if (rockchip->perst_gpio) {
+               ep->perst_asserted = true;
+               disable_irq(ep->perst_irq);
+       }
+
        cancel_delayed_work_sync(&ep->link_training);
 
        /* Stop link training and disable configuration */
        if (!rockchip_pcie_ep_link_up(rockchip))
                goto again;
 
+       /*
+        * If PERST# was asserted while polling the link, do not notify
+        * the function.
+        */
+       if (ep->perst_asserted)
+               return;
+
        val = rockchip_pcie_read(rockchip, PCIE_CLIENT_BASIC_STATUS0);
        dev_info(dev,
                 "link up (negotiated speed: %sGT/s, width: x%lu)\n",
 
        /* Notify the function */
        pci_epc_linkup(ep->epc);
+       ep->link_up = true;
 
        return;
 
        schedule_delayed_work(&ep->link_training, msecs_to_jiffies(5));
 }
 
+static void rockchip_pcie_ep_perst_assert(struct rockchip_pcie_ep *ep)
+{
+       struct rockchip_pcie *rockchip = &ep->rockchip;
+
+       dev_dbg(rockchip->dev, "PERST# asserted, link down\n");
+
+       if (ep->perst_asserted)
+               return;
+
+       ep->perst_asserted = true;
+
+       cancel_delayed_work_sync(&ep->link_training);
+
+       if (ep->link_up) {
+               pci_epc_linkdown(ep->epc);
+               ep->link_up = false;
+       }
+}
+
+static void rockchip_pcie_ep_perst_deassert(struct rockchip_pcie_ep *ep)
+{
+       struct rockchip_pcie *rockchip = &ep->rockchip;
+
+       dev_dbg(rockchip->dev, "PERST# de-asserted, starting link training\n");
+
+       if (!ep->perst_asserted)
+               return;
+
+       ep->perst_asserted = false;
+
+       /* Enable link re-training */
+       rockchip_pcie_ep_retrain_link(rockchip);
+
+       /* Start link training */
+       schedule_delayed_work(&ep->link_training, 0);
+}
+
+static irqreturn_t rockchip_pcie_ep_perst_irq_thread(int irq, void *data)
+{
+       struct pci_epc *epc = data;
+       struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
+       struct rockchip_pcie *rockchip = &ep->rockchip;
+       u32 perst = gpiod_get_value(rockchip->perst_gpio);
+
+       if (perst)
+               rockchip_pcie_ep_perst_assert(ep);
+       else
+               rockchip_pcie_ep_perst_deassert(ep);
+
+       irq_set_irq_type(ep->perst_irq,
+                        (perst ? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW));
+
+       return IRQ_HANDLED;
+}
+
+static int rockchip_pcie_ep_setup_irq(struct pci_epc *epc)
+{
+       struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
+       struct rockchip_pcie *rockchip = &ep->rockchip;
+       struct device *dev = rockchip->dev;
+       int ret;
+
+       if (!rockchip->perst_gpio)
+               return 0;
+
+       /* PCIe reset interrupt */
+       ep->perst_irq = gpiod_to_irq(rockchip->perst_gpio);
+       if (ep->perst_irq < 0) {
+               dev_err(dev,
+                       "failed to get IRQ for PERST# GPIO: %d\n",
+                       ep->perst_irq);
+
+               return ep->perst_irq;
+       }
+
+       /*
+        * The perst_gpio is active low, so when it is inactive on start, it
+        * is high and will trigger the perst_irq handler. So treat this initial
+        * IRQ as a dummy one by faking the host asserting PERST#.
+        */
+       ep->perst_asserted = true;
+       irq_set_status_flags(ep->perst_irq, IRQ_NOAUTOEN);
+       ret = devm_request_threaded_irq(dev, ep->perst_irq, NULL,
+                                       rockchip_pcie_ep_perst_irq_thread,
+                                       IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
+                                       "pcie-ep-perst", epc);
+       if (ret) {
+               dev_err(dev,
+                       "failed to request IRQ for PERST# GPIO: %d\n",
+                       ret);
+
+               return ret;
+       }
+
+       return 0;
+}
+
 static const struct pci_epc_features rockchip_pcie_epc_features = {
        .linkup_notifier = true,
        .msi_capable = true,
 
        epc = devm_pci_epc_create(dev, &rockchip_pcie_epc_ops);
        if (IS_ERR(epc)) {
-               dev_err(dev, "failed to create epc device\n");
+               dev_err(dev, "failed to create EPC device\n");
                return PTR_ERR(epc);
        }
 
 
        pci_epc_init_notify(epc);
 
+       err = rockchip_pcie_ep_setup_irq(epc);
+       if (err < 0)
+               goto err_uninit_port;
+
        return 0;
-err_exit_ob_mem:
-       rockchip_pcie_ep_exit_ob_mem(ep);
+err_uninit_port:
+       rockchip_pcie_deinit_phys(rockchip);
 err_disable_clocks:
        rockchip_pcie_disable_clocks(rockchip);
+err_exit_ob_mem:
+       rockchip_pcie_ep_exit_ob_mem(ep);
        return err;
 }