* published by the Free Software Foundation.
  */
 
-#include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 {
        struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
        u32 reg;
-       unsigned int retries;
 
        if (dw_pcie_link_up(pp)) {
                dev_err(pp->dev, "link is already up\n");
        reg |= LTSSM_EN;
        dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
 
-       for (retries = 0; retries < 1000; retries++) {
-               if (dw_pcie_link_up(pp))
-                       return 0;
-               usleep_range(10, 20);
-       }
-
-       dev_err(pp->dev, "link is not up\n");
-       return -EINVAL;
+       return dw_pcie_wait_for_link(pp);
 }
 
 static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
 
 {
        struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
        u32 val;
-       unsigned int retries;
 
        if (dw_pcie_link_up(pp)) {
                dev_err(pp->dev, "Link already up\n");
                          PCIE_APP_LTSSM_ENABLE);
 
        /* check if the link is up or not */
-       for (retries = 0; retries < 10; retries++) {
-               if (dw_pcie_link_up(pp)) {
-                       dev_info(pp->dev, "Link up\n");
-                       return 0;
-               }
-               mdelay(100);
-       }
+       if (!dw_pcie_wait_for_link(pp))
+               return 0;
 
        while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
                val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
        /* power off phy */
        exynos_pcie_power_off_phy(pp);
 
-       dev_err(pp->dev, "PCIe Link Fail\n");
-       return -EINVAL;
+       return -ETIMEDOUT;
 }
 
 static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
 
 
 static int imx6_pcie_wait_for_link(struct pcie_port *pp)
 {
-       unsigned int retries;
-
-       for (retries = 0; retries < 200; retries++) {
-               if (dw_pcie_link_up(pp))
-                       return 0;
-               usleep_range(100, 1000);
-       }
+       /* check if the link is up or not */
+       if (!dw_pcie_wait_for_link(pp))
+               return 0;
 
-       dev_err(pp->dev, "phy link never came up\n");
        dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
                readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
                readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
-       return -EINVAL;
+       return -ETIMEDOUT;
 }
 
 static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
 
                return 0;
        }
 
-       ks_dw_pcie_initiate_link_train(ks_pcie);
        /* check if the link is up or not */
-       for (retries = 0; retries < 200; retries++) {
-               if (dw_pcie_link_up(pp))
-                       return 0;
-               usleep_range(100, 1000);
+       for (retries = 0; retries < 5; retries++) {
                ks_dw_pcie_initiate_link_train(ks_pcie);
+               if (!dw_pcie_wait_for_link(pp))
+                       return 0;
        }
 
        dev_err(pp->dev, "phy link never came up\n");
-       return -EINVAL;
+       return -ETIMEDOUT;
 }
 
 static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
 
 #include <linux/pci_regs.h>
 #include <linux/platform_device.h>
 #include <linux/types.h>
+#include <linux/delay.h>
 
 #include "pcie-designware.h"
 
        .teardown_irq = dw_msi_teardown_irq,
 };
 
+int dw_pcie_wait_for_link(struct pcie_port *pp)
+{
+       int retries;
+
+       /* check if the link is up or not */
+       for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
+               if (dw_pcie_link_up(pp)) {
+                       dev_info(pp->dev, "link up\n");
+                       return 0;
+               }
+               usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
+       }
+
+       dev_err(pp->dev, "phy link never came up\n");
+
+       return -ETIMEDOUT;
+}
+
 int dw_pcie_link_up(struct pcie_port *pp)
 {
        if (pp->ops->link_up)
 
 #define MAX_MSI_IRQS                   32
 #define MAX_MSI_CTRLS                  (MAX_MSI_IRQS / 32)
 
+/* 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
+
 struct pcie_port {
        struct device           *dev;
        u8                      root_bus_nr;
 int dw_pcie_cfg_write(void __iomem *addr, int size, u32 val);
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp);
 void dw_pcie_msi_init(struct pcie_port *pp);
+int dw_pcie_wait_for_link(struct pcie_port *pp);
 int dw_pcie_link_up(struct pcie_port *pp);
 void dw_pcie_setup_rc(struct pcie_port *pp);
 int dw_pcie_host_init(struct pcie_port *pp);
 
 
 static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
 {
-       struct device *dev = pcie->dev;
-       unsigned int retries = 0;
        u32 val;
 
        if (dw_pcie_link_up(&pcie->pp))
        val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
        writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
 
-       do {
-               if (dw_pcie_link_up(&pcie->pp))
-                       return 0;
-               usleep_range(250, 1000);
-       } while (retries < 200);
-
-       dev_warn(dev, "phy link never came up\n");
-
-       return -ETIMEDOUT;
+       return dw_pcie_wait_for_link(&pcie->pp);
 }
 
 static int qcom_pcie_get_resources_v0(struct qcom_pcie *pcie)
 
  */
 
 #include <linux/clk.h>
-#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
        struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
        struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
        u32 exp_cap_off = EXP_CAP_ID_OFFSET;
-       unsigned int retries;
 
        if (dw_pcie_link_up(pp)) {
                dev_err(pp->dev, "link already up\n");
                        | ((u32)1 << REG_TRANSLATION_ENABLE),
                        &app_reg->app_ctrl_0);
 
-       /* check if the link is up or not */
-       for (retries = 0; retries < 10; retries++) {
-               if (dw_pcie_link_up(pp)) {
-                       dev_info(pp->dev, "link up\n");
-                       return 0;
-               }
-               mdelay(100);
-       }
-
-       dev_err(pp->dev, "link Fail\n");
-       return -EINVAL;
+       return dw_pcie_wait_for_link(pp);
 }
 
 static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)