0f2dab600b631d4d09c09723c5790e5069e090fd
[openwrt/openwrt.git] / target / linux / mvebu / patches-5.4 / 018-PCI-aardvark-Issue-PERST-via-GPIO.patch
1 From 5169a9851daaa2782a7bd2bb83d5b1bd224b2879 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <pali@kernel.org>
3 Date: Thu, 30 Apr 2020 10:06:18 +0200
4 Subject: [PATCH] PCI: aardvark: Issue PERST via GPIO
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add support for issuing PERST via GPIO specified in 'reset-gpios'
10 property (as described in PCI device tree bindings).
11
12 Some buggy cards (e.g. Compex WLE900VX or WLE1216) are not detected
13 after reboot when PERST is not issued during driver initialization.
14
15 If bootloader already enabled link training then issuing PERST has no
16 effect for some buggy cards (e.g. Compex WLE900VX) and these cards are
17 not detected. We therefore clear the LINK_TRAINING_EN register before.
18
19 It was observed that Compex WLE900VX card needs to be in PERST reset
20 for at least 10ms if bootloader enabled link training.
21
22 Tested on Turris MOX.
23
24 Link: https://lore.kernel.org/r/20200430080625.26070-6-pali@kernel.org
25 Tested-by: Tomasz Maciej Nowak <tmn505@gmail.com>
26 Signed-off-by: Pali Rohár <pali@kernel.org>
27 Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
28 Acked-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
29 ---
30 drivers/pci/controller/pci-aardvark.c | 43 ++++++++++++++++++++++++++-
31 1 file changed, 42 insertions(+), 1 deletion(-)
32
33 --- a/drivers/pci/controller/pci-aardvark.c
34 +++ b/drivers/pci/controller/pci-aardvark.c
35 @@ -9,6 +9,7 @@
36 */
37
38 #include <linux/delay.h>
39 +#include <linux/gpio.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
43 @@ -17,6 +18,7 @@
44 #include <linux/init.h>
45 #include <linux/platform_device.h>
46 #include <linux/of_address.h>
47 +#include <linux/of_gpio.h>
48 #include <linux/of_pci.h>
49
50 #include "../pci.h"
51 @@ -204,6 +206,7 @@ struct advk_pcie {
52 int root_bus_nr;
53 int link_gen;
54 struct pci_bridge_emul bridge;
55 + struct gpio_desc *reset_gpio;
56 };
57
58 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
59 @@ -330,10 +333,31 @@ err:
60 dev_err(dev, "link never came up\n");
61 }
62
63 +static void advk_pcie_issue_perst(struct advk_pcie *pcie)
64 +{
65 + u32 reg;
66 +
67 + if (!pcie->reset_gpio)
68 + return;
69 +
70 + /* PERST does not work for some cards when link training is enabled */
71 + reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
72 + reg &= ~LINK_TRAINING_EN;
73 + advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
74 +
75 + /* 10ms delay is needed for some cards */
76 + dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
77 + gpiod_set_value_cansleep(pcie->reset_gpio, 1);
78 + usleep_range(10000, 11000);
79 + gpiod_set_value_cansleep(pcie->reset_gpio, 0);
80 +}
81 +
82 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
83 {
84 u32 reg;
85
86 + advk_pcie_issue_perst(pcie);
87 +
88 /* Set to Direct mode */
89 reg = advk_readl(pcie, CTRL_CONFIG_REG);
90 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
91 @@ -406,7 +430,8 @@ static void advk_pcie_setup_hw(struct ad
92
93 /*
94 * PERST# signal could have been asserted by pinctrl subsystem before
95 - * probe() callback has been called, making the endpoint going into
96 + * probe() callback has been called or issued explicitly by reset gpio
97 + * function advk_pcie_issue_perst(), making the endpoint going into
98 * fundamental reset. As required by PCI Express spec a delay for at
99 * least 100ms after such a reset before link training is needed.
100 */
101 @@ -1124,6 +1149,22 @@ static int advk_pcie_probe(struct platfo
102 return ret;
103 }
104
105 + pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
106 + "reset-gpios", 0,
107 + GPIOD_OUT_LOW,
108 + "pcie1-reset");
109 + ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
110 + if (ret) {
111 + if (ret == -ENOENT) {
112 + pcie->reset_gpio = NULL;
113 + } else {
114 + if (ret != -EPROBE_DEFER)
115 + dev_err(dev, "Failed to get reset-gpio: %i\n",
116 + ret);
117 + return ret;
118 + }
119 + }
120 +
121 ret = of_pci_get_max_link_speed(dev->of_node);
122 if (ret <= 0 || ret > 3)
123 pcie->link_gen = 3;