ipq806x: reduce PCIe buffer size setting to fix potential data corruption issues
[openwrt.git] / target / linux / ipq806x / patches-4.1 / 114-pcie-add-ctlr-init.patch
1 --- a/drivers/pci/host/pcie-qcom.c
2 +++ b/drivers/pci/host/pcie-qcom.c
3 @@ -29,8 +29,53 @@
4  
5  #include "pcie-designware.h"
6  
7 +/* DBI registers */
8 +#define PCIE20_CAP                             0x70
9 +#define PCIE20_CAP_LINKCTRLSTATUS              (PCIE20_CAP + 0x10)
10 +
11 +#define PCIE20_AXI_MSTR_RESP_COMP_CTRL0                0x818
12 +#define PCIE20_AXI_MSTR_RESP_COMP_CTRL1                0x81c
13 +
14 +#define PCIE20_PLR_IATU_VIEWPORT               0x900
15 +#define PCIE20_PLR_IATU_REGION_OUTBOUND                (0x0 << 31)
16 +#define PCIE20_PLR_IATU_REGION_INDEX(x)                (x << 0)
17 +
18 +#define PCIE20_PLR_IATU_CTRL1                  0x904
19 +#define PCIE20_PLR_IATU_TYPE_CFG0              (0x4 << 0)
20 +#define PCIE20_PLR_IATU_TYPE_MEM               (0x0 << 0)
21 +
22 +#define PCIE20_PLR_IATU_CTRL2                  0x908
23 +#define PCIE20_PLR_IATU_ENABLE                 BIT(31)
24 +
25 +#define PCIE20_PLR_IATU_LBAR                   0x90C
26 +#define PCIE20_PLR_IATU_UBAR                   0x910
27 +#define PCIE20_PLR_IATU_LAR                    0x914
28 +#define PCIE20_PLR_IATU_LTAR                   0x918
29 +#define PCIE20_PLR_IATU_UTAR                   0x91c
30 +
31 +#define MSM_PCIE_DEV_CFG_ADDR                  0x01000000
32 +
33 +/* PARF registers */
34 +#define PCIE20_PARF_PCS_DEEMPH                 0x34
35 +#define PCS_DEEMPH_TX_DEEMPH_GEN1(x)           (x << 16)
36 +#define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x)     (x << 8)
37 +#define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x)       (x << 0)
38 +
39 +#define PCIE20_PARF_PCS_SWING                  0x38
40 +#define PCS_SWING_TX_SWING_FULL(x)             (x << 8)
41 +#define PCS_SWING_TX_SWING_LOW(x)              (x << 0)
42 +
43  #define PCIE20_PARF_PHY_CTRL                   0x40
44 +#define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK      (0x1f << 16)
45 +#define PHY_CTRL_PHY_TX0_TERM_OFFSET(x)                (x << 16)
46 +
47  #define PCIE20_PARF_PHY_REFCLK                 0x4C
48 +#define REF_SSP_EN                             BIT(16)
49 +#define REF_USE_PAD                            BIT(12)
50 +
51 +#define PCIE20_PARF_CONFIG_BITS                        0x50
52 +#define PHY_RX0_EQ(x)                          (x << 24)
53 +
54  #define PCIE20_PARF_DBI_BASE_ADDR              0x168
55  #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE                0x16c
56  #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT      0x178
57 @@ -39,9 +84,6 @@
58  #define PCIE20_ELBI_SYS_STTS                   0x08
59  #define XMLH_LINK_UP                           BIT(10)
60  
61 -#define PCIE20_CAP                             0x70
62 -#define PCIE20_CAP_LINKCTRLSTATUS              (PCIE20_CAP + 0x10)
63 -
64  #define PERST_DELAY_MIN_US                     1000
65  #define PERST_DELAY_MAX_US                     1005
66  
67 @@ -56,14 +98,18 @@ struct qcom_pcie_resources_v0 {
68         struct clk *iface_clk;
69         struct clk *core_clk;
70         struct clk *phy_clk;
71 +       struct clk *aux_clk;
72 +       struct clk *ref_clk;
73         struct reset_control *pci_reset;
74         struct reset_control *axi_reset;
75         struct reset_control *ahb_reset;
76         struct reset_control *por_reset;
77         struct reset_control *phy_reset;
78 +       struct reset_control *ext_reset;
79         struct regulator *vdda;
80         struct regulator *vdda_phy;
81         struct regulator *vdda_refclk;
82 +       uint8_t phy_tx0_term_offset;
83  };
84  
85  struct qcom_pcie_resources_v1 {
86 @@ -156,10 +202,13 @@ static void qcom_pcie_disable_resources_
87         reset_control_assert(res->axi_reset);
88         reset_control_assert(res->ahb_reset);
89         reset_control_assert(res->por_reset);
90 -       reset_control_assert(res->pci_reset);
91 +       reset_control_assert(res->phy_reset);
92 +       reset_control_assert(res->ext_reset);
93         clk_disable_unprepare(res->iface_clk);
94         clk_disable_unprepare(res->core_clk);
95         clk_disable_unprepare(res->phy_clk);
96 +       clk_disable_unprepare(res->aux_clk);
97 +       clk_disable_unprepare(res->ref_clk);
98         regulator_disable(res->vdda);
99         regulator_disable(res->vdda_phy);
100         regulator_disable(res->vdda_refclk);
101 @@ -201,6 +250,12 @@ static int qcom_pcie_enable_resources_v0
102                 goto err_vdda_phy;
103         }
104  
105 +       ret = reset_control_deassert(res->ext_reset);
106 +       if (ret) {
107 +               dev_err(dev, "cannot assert ext reset\n");
108 +               goto err_reset_ext;
109 +       }
110 +
111         ret = clk_prepare_enable(res->iface_clk);
112         if (ret) {
113                 dev_err(dev, "cannot prepare/enable iface clock\n");
114 @@ -219,6 +274,18 @@ static int qcom_pcie_enable_resources_v0
115                 goto err_clk_phy;
116         }
117  
118 +       ret = clk_prepare_enable(res->aux_clk);
119 +       if (ret) {
120 +               dev_err(dev, "cannot prepare/enable aux clock\n");
121 +               goto err_clk_aux;
122 +       }
123 +
124 +       ret = clk_prepare_enable(res->ref_clk);
125 +       if (ret) {
126 +               dev_err(dev, "cannot prepare/enable ref clock\n");
127 +               goto err_clk_ref;
128 +       }
129 +
130         ret = reset_control_deassert(res->ahb_reset);
131         if (ret) {
132                 dev_err(dev, "cannot deassert ahb reset\n");
133 @@ -228,12 +295,18 @@ static int qcom_pcie_enable_resources_v0
134         return 0;
135  
136  err_reset_ahb:
137 +       clk_disable_unprepare(res->ref_clk);
138 +err_clk_ref:
139 +       clk_disable_unprepare(res->aux_clk);
140 +err_clk_aux:
141         clk_disable_unprepare(res->phy_clk);
142  err_clk_phy:
143         clk_disable_unprepare(res->core_clk);
144  err_clk_core:
145         clk_disable_unprepare(res->iface_clk);
146  err_iface:
147 +       reset_control_assert(res->ext_reset);
148 +err_reset_ext:
149         regulator_disable(res->vdda_phy);
150  err_vdda_phy:
151         regulator_disable(res->vdda_refclk);
152 @@ -329,6 +402,14 @@ static int qcom_pcie_get_resources_v0(st
153         if (IS_ERR(res->phy_clk))
154                 return PTR_ERR(res->phy_clk);
155  
156 +       res->aux_clk = devm_clk_get(dev, "aux");
157 +       if (IS_ERR(res->aux_clk))
158 +               return PTR_ERR(res->aux_clk);
159 +
160 +       res->ref_clk = devm_clk_get(dev, "ref");
161 +       if (IS_ERR(res->ref_clk))
162 +               return PTR_ERR(res->ref_clk);
163 +
164         res->pci_reset = devm_reset_control_get(dev, "pci");
165         if (IS_ERR(res->pci_reset))
166                 return PTR_ERR(res->pci_reset);
167 @@ -349,6 +430,14 @@ static int qcom_pcie_get_resources_v0(st
168         if (IS_ERR(res->phy_reset))
169                 return PTR_ERR(res->phy_reset);
170  
171 +       res->ext_reset = devm_reset_control_get(dev, "ext");
172 +       if (IS_ERR(res->ext_reset))
173 +               return PTR_ERR(res->ext_reset);
174 +
175 +       if (of_property_read_u8(dev->of_node, "phy-tx0-term-offset",
176 +                               &res->phy_tx0_term_offset))
177 +               res->phy_tx0_term_offset = 0;
178 +
179         return 0;
180  }
181  
182 @@ -461,6 +550,57 @@ err_res:
183         qcom_pcie_disable_resources_v1(pcie);
184  }
185  
186 +static void qcom_pcie_prog_viewport_cfg0(struct qcom_pcie *pcie, u32 busdev)
187 +{
188 +       struct pcie_port *pp = &pcie->pp;
189 +
190 +       /*
191 +        * program and enable address translation region 0 (device config
192 +        * address space); region type config;
193 +        * axi config address range to device config address range
194 +        */
195 +       writel(PCIE20_PLR_IATU_REGION_OUTBOUND |
196 +              PCIE20_PLR_IATU_REGION_INDEX(0),
197 +              pcie->dbi + PCIE20_PLR_IATU_VIEWPORT);
198 +
199 +       writel(PCIE20_PLR_IATU_TYPE_CFG0, pcie->dbi + PCIE20_PLR_IATU_CTRL1);
200 +       writel(PCIE20_PLR_IATU_ENABLE, pcie->dbi + PCIE20_PLR_IATU_CTRL2);
201 +       writel(pp->cfg0_mod_base, pcie->dbi + PCIE20_PLR_IATU_LBAR);
202 +       writel((pp->cfg0_mod_base >> 32), pcie->dbi + PCIE20_PLR_IATU_UBAR);
203 +       writel((pp->cfg0_mod_base + pp->cfg0_size - 1),
204 +              pcie->dbi + PCIE20_PLR_IATU_LAR);
205 +       writel(busdev, pcie->dbi + PCIE20_PLR_IATU_LTAR);
206 +       writel(0, pcie->dbi + PCIE20_PLR_IATU_UTAR);
207 +}
208 +
209 +static void qcom_pcie_prog_viewport_mem2_outbound(struct qcom_pcie *pcie)
210 +{
211 +       struct pcie_port *pp = &pcie->pp;
212 +
213 +       /*
214 +        * program and enable address translation region 2 (device resource
215 +        * address space); region type memory;
216 +        * axi device bar address range to device bar address range
217 +        */
218 +       writel(PCIE20_PLR_IATU_REGION_OUTBOUND |
219 +              PCIE20_PLR_IATU_REGION_INDEX(2),
220 +              pcie->dbi + PCIE20_PLR_IATU_VIEWPORT);
221 +
222 +       writel(PCIE20_PLR_IATU_TYPE_MEM, pcie->dbi + PCIE20_PLR_IATU_CTRL1);
223 +       writel(PCIE20_PLR_IATU_ENABLE, pcie->dbi + PCIE20_PLR_IATU_CTRL2);
224 +       writel(pp->mem_mod_base, pcie->dbi + PCIE20_PLR_IATU_LBAR);
225 +       writel((pp->mem_mod_base >> 32), pcie->dbi + PCIE20_PLR_IATU_UBAR);
226 +       writel(pp->mem_mod_base + pp->mem_size - 1,
227 +              pcie->dbi + PCIE20_PLR_IATU_LAR);
228 +       writel(pp->mem_bus_addr, pcie->dbi + PCIE20_PLR_IATU_LTAR);
229 +       writel(upper_32_bits(pp->mem_bus_addr),
230 +              pcie->dbi + PCIE20_PLR_IATU_UTAR);
231 +
232 +       /* 256B PCIE buffer setting */
233 +       writel(0x1, pcie->dbi + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
234 +       writel(0x1, pcie->dbi + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
235 +}
236 +
237  static void qcom_pcie_host_init_v0(struct pcie_port *pp)
238  {
239         struct qcom_pcie *pcie = to_qcom_pcie(pp);
240 @@ -476,9 +616,26 @@ static void qcom_pcie_host_init_v0(struc
241  
242         writel_masked(pcie->parf + PCIE20_PARF_PHY_CTRL, BIT(0), 0);
243  
244 -       /* enable external reference clock */
245 -       writel_masked(pcie->parf + PCIE20_PARF_PHY_REFCLK, 0, BIT(16));
246 +       /* Set Tx termination offset */
247 +       writel_masked(pcie->parf + PCIE20_PARF_PHY_CTRL,
248 +                     PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK,
249 +                     PHY_CTRL_PHY_TX0_TERM_OFFSET(res->phy_tx0_term_offset));
250 +
251 +       /* PARF programming */
252 +       writel(PCS_DEEMPH_TX_DEEMPH_GEN1(0x18) |
253 +              PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(0x18) |
254 +              PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(0x22),
255 +              pcie->parf + PCIE20_PARF_PCS_DEEMPH);
256 +       writel(PCS_SWING_TX_SWING_FULL(0x78) |
257 +              PCS_SWING_TX_SWING_LOW(0x78),
258 +              pcie->parf + PCIE20_PARF_PCS_SWING);
259 +       writel(PHY_RX0_EQ(0x4), pcie->parf + PCIE20_PARF_CONFIG_BITS);
260 +
261 +       /* Enable reference clock */
262 +       writel_masked(pcie->parf + PCIE20_PARF_PHY_REFCLK,
263 +                     REF_USE_PAD, REF_SSP_EN);
264  
265 +       /* De-assert PHY, PCIe, POR and AXI resets */
266         ret = reset_control_deassert(res->phy_reset);
267         if (ret) {
268                 dev_err(dev, "cannot deassert phy reset\n");
269 @@ -517,6 +674,9 @@ static void qcom_pcie_host_init_v0(struc
270         if (ret)
271                 goto err;
272  
273 +       qcom_pcie_prog_viewport_cfg0(pcie, MSM_PCIE_DEV_CFG_ADDR);
274 +       qcom_pcie_prog_viewport_mem2_outbound(pcie);
275 +
276         return;
277  err:
278         qcom_ep_reset_assert(pcie);