Skip to content

Commit 886bc5c

Browse files
Joao Pintobjorn-helgaas
authored andcommitted
PCI: designware: Add generic dw_pcie_wait_for_link()
Several DesignWare-based drivers (dra7xx, exynos, imx6, keystone, qcom, and spear13xx) had similar loops waiting for the link to come up. Add a generic dw_pcie_wait_for_link() for use by all these drivers so the waiting is done consistently, e.g., always using usleep_range() rather than mdelay() and using similar timeouts and retry counts. Note that this changes the Keystone link training/wait for link strategy, so we initiate link training, then wait longer for the link to come up before re-initiating link training. [bhelgaas: changelog, split into its own patch, update pci-keystone.c, pcie-qcom.c] Signed-off-by: Joao Pinto <jpinto@synopsys.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Acked-by: Pratyush Anand <pratyush.anand@gmail.com>
1 parent c1678ff commit 886bc5c

8 files changed

Lines changed: 39 additions & 59 deletions

File tree

‎drivers/pci/host/pci-dra7xx.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* published by the Free Software Foundation.
1111
*/
1212

13-
#include <linux/delay.h>
1413
#include <linux/err.h>
1514
#include <linux/interrupt.h>
1615
#include <linux/irq.h>
@@ -108,7 +107,6 @@ static int dra7xx_pcie_establish_link(struct pcie_port *pp)
108107
{
109108
struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
110109
u32 reg;
111-
unsigned int retries;
112110

113111
if (dw_pcie_link_up(pp)) {
114112
dev_err(pp->dev, "link is already up\n");
@@ -119,14 +117,7 @@ static int dra7xx_pcie_establish_link(struct pcie_port *pp)
119117
reg |= LTSSM_EN;
120118
dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
121119

122-
for (retries = 0; retries < 1000; retries++) {
123-
if (dw_pcie_link_up(pp))
124-
return 0;
125-
usleep_range(10, 20);
126-
}
127-
128-
dev_err(pp->dev, "link is not up\n");
129-
return -EINVAL;
120+
return dw_pcie_wait_for_link(pp);
130121
}
131122

132123
static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)

‎drivers/pci/host/pci-exynos.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ static int exynos_pcie_establish_link(struct pcie_port *pp)
318318
{
319319
struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
320320
u32 val;
321-
unsigned int retries;
322321

323322
if (dw_pcie_link_up(pp)) {
324323
dev_err(pp->dev, "Link already up\n");
@@ -357,13 +356,8 @@ static int exynos_pcie_establish_link(struct pcie_port *pp)
357356
PCIE_APP_LTSSM_ENABLE);
358357

359358
/* check if the link is up or not */
360-
for (retries = 0; retries < 10; retries++) {
361-
if (dw_pcie_link_up(pp)) {
362-
dev_info(pp->dev, "Link up\n");
363-
return 0;
364-
}
365-
mdelay(100);
366-
}
359+
if (!dw_pcie_wait_for_link(pp))
360+
return 0;
367361

368362
while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
369363
val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
@@ -372,8 +366,7 @@ static int exynos_pcie_establish_link(struct pcie_port *pp)
372366
/* power off phy */
373367
exynos_pcie_power_off_phy(pp);
374368

375-
dev_err(pp->dev, "PCIe Link Fail\n");
376-
return -EINVAL;
369+
return -ETIMEDOUT;
377370
}
378371

379372
static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)

‎drivers/pci/host/pci-imx6.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,14 @@ static void imx6_pcie_init_phy(struct pcie_port *pp)
330330

331331
static int imx6_pcie_wait_for_link(struct pcie_port *pp)
332332
{
333-
unsigned int retries;
334-
335-
for (retries = 0; retries < 200; retries++) {
336-
if (dw_pcie_link_up(pp))
337-
return 0;
338-
usleep_range(100, 1000);
339-
}
333+
/* check if the link is up or not */
334+
if (!dw_pcie_wait_for_link(pp))
335+
return 0;
340336

341-
dev_err(pp->dev, "phy link never came up\n");
342337
dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
343338
readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
344339
readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
345-
return -EINVAL;
340+
return -ETIMEDOUT;
346341
}
347342

348343
static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)

‎drivers/pci/host/pci-keystone.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,15 @@ static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
9797
return 0;
9898
}
9999

100-
ks_dw_pcie_initiate_link_train(ks_pcie);
101100
/* check if the link is up or not */
102-
for (retries = 0; retries < 200; retries++) {
103-
if (dw_pcie_link_up(pp))
104-
return 0;
105-
usleep_range(100, 1000);
101+
for (retries = 0; retries < 5; retries++) {
106102
ks_dw_pcie_initiate_link_train(ks_pcie);
103+
if (!dw_pcie_wait_for_link(pp))
104+
return 0;
107105
}
108106

109107
dev_err(pp->dev, "phy link never came up\n");
110-
return -EINVAL;
108+
return -ETIMEDOUT;
111109
}
112110

113111
static void ks_pcie_msi_irq_handler(struct irq_desc *desc)

‎drivers/pci/host/pcie-designware.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/pci_regs.h>
2323
#include <linux/platform_device.h>
2424
#include <linux/types.h>
25+
#include <linux/delay.h>
2526

2627
#include "pcie-designware.h"
2728

@@ -380,6 +381,24 @@ static struct msi_controller dw_pcie_msi_chip = {
380381
.teardown_irq = dw_msi_teardown_irq,
381382
};
382383

384+
int dw_pcie_wait_for_link(struct pcie_port *pp)
385+
{
386+
int retries;
387+
388+
/* check if the link is up or not */
389+
for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
390+
if (dw_pcie_link_up(pp)) {
391+
dev_info(pp->dev, "link up\n");
392+
return 0;
393+
}
394+
usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
395+
}
396+
397+
dev_err(pp->dev, "phy link never came up\n");
398+
399+
return -ETIMEDOUT;
400+
}
401+
383402
int dw_pcie_link_up(struct pcie_port *pp)
384403
{
385404
if (pp->ops->link_up)

‎drivers/pci/host/pcie-designware.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#define MAX_MSI_IRQS 32
2323
#define MAX_MSI_CTRLS (MAX_MSI_IRQS / 32)
2424

25+
/* Parameters for the waiting for link up routine */
26+
#define LINK_WAIT_MAX_RETRIES 10
27+
#define LINK_WAIT_USLEEP_MIN 90000
28+
#define LINK_WAIT_USLEEP_MAX 100000
29+
2530
struct pcie_port {
2631
struct device *dev;
2732
u8 root_bus_nr;
@@ -76,6 +81,7 @@ int dw_pcie_cfg_read(void __iomem *addr, int size, u32 *val);
7681
int dw_pcie_cfg_write(void __iomem *addr, int size, u32 val);
7782
irqreturn_t dw_handle_msi_irq(struct pcie_port *pp);
7883
void dw_pcie_msi_init(struct pcie_port *pp);
84+
int dw_pcie_wait_for_link(struct pcie_port *pp);
7985
int dw_pcie_link_up(struct pcie_port *pp);
8086
void dw_pcie_setup_rc(struct pcie_port *pp);
8187
int dw_pcie_host_init(struct pcie_port *pp);

‎drivers/pci/host/pcie-qcom.c‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ static irqreturn_t qcom_pcie_msi_irq_handler(int irq, void *arg)
116116

117117
static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
118118
{
119-
struct device *dev = pcie->dev;
120-
unsigned int retries = 0;
121119
u32 val;
122120

123121
if (dw_pcie_link_up(&pcie->pp))
@@ -128,15 +126,7 @@ static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
128126
val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
129127
writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
130128

131-
do {
132-
if (dw_pcie_link_up(&pcie->pp))
133-
return 0;
134-
usleep_range(250, 1000);
135-
} while (retries < 200);
136-
137-
dev_warn(dev, "phy link never came up\n");
138-
139-
return -ETIMEDOUT;
129+
return dw_pcie_wait_for_link(&pcie->pp);
140130
}
141131

142132
static int qcom_pcie_get_resources_v0(struct qcom_pcie *pcie)

‎drivers/pci/host/pcie-spear13xx.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414

1515
#include <linux/clk.h>
16-
#include <linux/delay.h>
1716
#include <linux/interrupt.h>
1817
#include <linux/kernel.h>
1918
#include <linux/module.h>
@@ -149,7 +148,6 @@ static int spear13xx_pcie_establish_link(struct pcie_port *pp)
149148
struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
150149
struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
151150
u32 exp_cap_off = EXP_CAP_ID_OFFSET;
152-
unsigned int retries;
153151

154152
if (dw_pcie_link_up(pp)) {
155153
dev_err(pp->dev, "link already up\n");
@@ -200,17 +198,7 @@ static int spear13xx_pcie_establish_link(struct pcie_port *pp)
200198
| ((u32)1 << REG_TRANSLATION_ENABLE),
201199
&app_reg->app_ctrl_0);
202200

203-
/* check if the link is up or not */
204-
for (retries = 0; retries < 10; retries++) {
205-
if (dw_pcie_link_up(pp)) {
206-
dev_info(pp->dev, "link up\n");
207-
return 0;
208-
}
209-
mdelay(100);
210-
}
211-
212-
dev_err(pp->dev, "link Fail\n");
213-
return -EINVAL;
201+
return dw_pcie_wait_for_link(pp);
214202
}
215203

216204
static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)

0 commit comments

Comments
 (0)