1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * PCIe host controller driver for the following SoCs
4 * Tegra194
5 * Tegra234
6 *
7 * Copyright (C) 2019-2022 NVIDIA Corporation.
8 *
9 * Author: Vidya Sagar <vidyas@nvidia.com>
10 */
11
12 #include <linux/clk.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/gpio.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_pci.h>
26 #include <linux/pci.h>
27 #include <linux/phy/phy.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/random.h>
32 #include <linux/reset.h>
33 #include <linux/resource.h>
34 #include <linux/types.h>
35 #include "pcie-designware.h"
36 #include <soc/tegra/bpmp.h>
37 #include <soc/tegra/bpmp-abi.h>
38 #include "../../pci.h"
39
40 #define TEGRA194_DWC_IP_VER 0x490A
41 #define TEGRA234_DWC_IP_VER 0x562A
42
43 #define APPL_PINMUX 0x0
44 #define APPL_PINMUX_PEX_RST BIT(0)
45 #define APPL_PINMUX_CLKREQ_OVERRIDE_EN BIT(2)
46 #define APPL_PINMUX_CLKREQ_OVERRIDE BIT(3)
47 #define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN BIT(4)
48 #define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE BIT(5)
49
50 #define APPL_CTRL 0x4
51 #define APPL_CTRL_SYS_PRE_DET_STATE BIT(6)
52 #define APPL_CTRL_LTSSM_EN BIT(7)
53 #define APPL_CTRL_HW_HOT_RST_EN BIT(20)
54 #define APPL_CTRL_HW_HOT_RST_MODE_MASK GENMASK(1, 0)
55 #define APPL_CTRL_HW_HOT_RST_MODE_SHIFT 22
56 #define APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST 0x1
57 #define APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST_LTSSM_EN 0x2
58
59 #define APPL_INTR_EN_L0_0 0x8
60 #define APPL_INTR_EN_L0_0_LINK_STATE_INT_EN BIT(0)
61 #define APPL_INTR_EN_L0_0_MSI_RCV_INT_EN BIT(4)
62 #define APPL_INTR_EN_L0_0_INT_INT_EN BIT(8)
63 #define APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN BIT(15)
64 #define APPL_INTR_EN_L0_0_CDM_REG_CHK_INT_EN BIT(19)
65 #define APPL_INTR_EN_L0_0_SYS_INTR_EN BIT(30)
66 #define APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN BIT(31)
67
68 #define APPL_INTR_STATUS_L0 0xC
69 #define APPL_INTR_STATUS_L0_LINK_STATE_INT BIT(0)
70 #define APPL_INTR_STATUS_L0_INT_INT BIT(8)
71 #define APPL_INTR_STATUS_L0_PCI_CMD_EN_INT BIT(15)
72 #define APPL_INTR_STATUS_L0_PEX_RST_INT BIT(16)
73 #define APPL_INTR_STATUS_L0_CDM_REG_CHK_INT BIT(18)
74
75 #define APPL_INTR_EN_L1_0_0 0x1C
76 #define APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN BIT(1)
77 #define APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN BIT(3)
78 #define APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN BIT(30)
79
80 #define APPL_INTR_STATUS_L1_0_0 0x20
81 #define APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED BIT(1)
82 #define APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED BIT(3)
83 #define APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE BIT(30)
84
85 #define APPL_INTR_STATUS_L1_1 0x2C
86 #define APPL_INTR_STATUS_L1_2 0x30
87 #define APPL_INTR_STATUS_L1_3 0x34
88 #define APPL_INTR_STATUS_L1_6 0x3C
89 #define APPL_INTR_STATUS_L1_7 0x40
90 #define APPL_INTR_STATUS_L1_15_CFG_BME_CHGED BIT(1)
91
92 #define APPL_INTR_EN_L1_8_0 0x44
93 #define APPL_INTR_EN_L1_8_BW_MGT_INT_EN BIT(2)
94 #define APPL_INTR_EN_L1_8_AUTO_BW_INT_EN BIT(3)
95 #define APPL_INTR_EN_L1_8_INTX_EN BIT(11)
96 #define APPL_INTR_EN_L1_8_AER_INT_EN BIT(15)
97
98 #define APPL_INTR_STATUS_L1_8_0 0x4C
99 #define APPL_INTR_STATUS_L1_8_0_EDMA_INT_MASK GENMASK(11, 6)
100 #define APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS BIT(2)
101 #define APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS BIT(3)
102
103 #define APPL_INTR_STATUS_L1_9 0x54
104 #define APPL_INTR_STATUS_L1_10 0x58
105 #define APPL_INTR_STATUS_L1_11 0x64
106 #define APPL_INTR_STATUS_L1_13 0x74
107 #define APPL_INTR_STATUS_L1_14 0x78
108 #define APPL_INTR_STATUS_L1_15 0x7C
109 #define APPL_INTR_STATUS_L1_17 0x88
110
111 #define APPL_INTR_EN_L1_18 0x90
112 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMPLT BIT(2)
113 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR BIT(1)
114 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR BIT(0)
115
116 #define APPL_INTR_STATUS_L1_18 0x94
117 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT BIT(2)
118 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR BIT(1)
119 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR BIT(0)
120
121 #define APPL_MSI_CTRL_1 0xAC
122
123 #define APPL_MSI_CTRL_2 0xB0
124
125 #define APPL_LEGACY_INTX 0xB8
126
127 #define APPL_LTR_MSG_1 0xC4
128 #define LTR_MSG_REQ BIT(15)
129 #define LTR_MST_NO_SNOOP_SHIFT 16
130
131 #define APPL_LTR_MSG_2 0xC8
132 #define APPL_LTR_MSG_2_LTR_MSG_REQ_STATE BIT(3)
133
134 #define APPL_LINK_STATUS 0xCC
135 #define APPL_LINK_STATUS_RDLH_LINK_UP BIT(0)
136
137 #define APPL_DEBUG 0xD0
138 #define APPL_DEBUG_PM_LINKST_IN_L2_LAT BIT(21)
139 #define APPL_DEBUG_PM_LINKST_IN_L0 0x11
140 #define APPL_DEBUG_LTSSM_STATE_MASK GENMASK(8, 3)
141 #define APPL_DEBUG_LTSSM_STATE_SHIFT 3
142 #define LTSSM_STATE_PRE_DETECT 5
143
144 #define APPL_RADM_STATUS 0xE4
145 #define APPL_PM_XMT_TURNOFF_STATE BIT(0)
146
147 #define APPL_DM_TYPE 0x100
148 #define APPL_DM_TYPE_MASK GENMASK(3, 0)
149 #define APPL_DM_TYPE_RP 0x4
150 #define APPL_DM_TYPE_EP 0x0
151
152 #define APPL_CFG_BASE_ADDR 0x104
153 #define APPL_CFG_BASE_ADDR_MASK GENMASK(31, 12)
154
155 #define APPL_CFG_IATU_DMA_BASE_ADDR 0x108
156 #define APPL_CFG_IATU_DMA_BASE_ADDR_MASK GENMASK(31, 18)
157
158 #define APPL_CFG_MISC 0x110
159 #define APPL_CFG_MISC_SLV_EP_MODE BIT(14)
160 #define APPL_CFG_MISC_ARCACHE_MASK GENMASK(13, 10)
161 #define APPL_CFG_MISC_ARCACHE_SHIFT 10
162 #define APPL_CFG_MISC_ARCACHE_VAL 3
163
164 #define APPL_CFG_SLCG_OVERRIDE 0x114
165 #define APPL_CFG_SLCG_OVERRIDE_SLCG_EN_MASTER BIT(0)
166
167 #define APPL_CAR_RESET_OVRD 0x12C
168 #define APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N BIT(0)
169
170 #define IO_BASE_IO_DECODE BIT(0)
171 #define IO_BASE_IO_DECODE_BIT8 BIT(8)
172
173 #define CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE BIT(0)
174 #define CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE BIT(16)
175
176 #define CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF 0x718
177 #define CFG_TIMER_CTRL_ACK_NAK_SHIFT (19)
178
179 #define N_FTS_VAL 52
180 #define FTS_VAL 52
181
182 #define GEN3_EQ_CONTROL_OFF 0x8a8
183 #define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT 8
184 #define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK GENMASK(23, 8)
185 #define GEN3_EQ_CONTROL_OFF_FB_MODE_MASK GENMASK(3, 0)
186
187 #define PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT 0x8D0
188 #define AMBA_ERROR_RESPONSE_CRS_SHIFT 3
189 #define AMBA_ERROR_RESPONSE_CRS_MASK GENMASK(1, 0)
190 #define AMBA_ERROR_RESPONSE_CRS_OKAY 0
191 #define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFFFFFF 1
192 #define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001 2
193
194 #define MSIX_ADDR_MATCH_LOW_OFF 0x940
195 #define MSIX_ADDR_MATCH_LOW_OFF_EN BIT(0)
196 #define MSIX_ADDR_MATCH_LOW_OFF_MASK GENMASK(31, 2)
197
198 #define MSIX_ADDR_MATCH_HIGH_OFF 0x944
199 #define MSIX_ADDR_MATCH_HIGH_OFF_MASK GENMASK(31, 0)
200
201 #define PORT_LOGIC_MSIX_DOORBELL 0x948
202
203 #define CAP_SPCIE_CAP_OFF 0x154
204 #define CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK GENMASK(3, 0)
205 #define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK GENMASK(11, 8)
206 #define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT 8
207
208 #define PME_ACK_TIMEOUT 10000
209
210 #define LTSSM_TIMEOUT 50000 /* 50ms */
211
212 #define GEN3_GEN4_EQ_PRESET_INIT 5
213
214 #define GEN1_CORE_CLK_FREQ 62500000
215 #define GEN2_CORE_CLK_FREQ 125000000
216 #define GEN3_CORE_CLK_FREQ 250000000
217 #define GEN4_CORE_CLK_FREQ 500000000
218
219 #define LTR_MSG_TIMEOUT (100 * 1000)
220
221 #define PERST_DEBOUNCE_TIME (5 * 1000)
222
223 #define EP_STATE_DISABLED 0
224 #define EP_STATE_ENABLED 1
225
226 static const unsigned int pcie_gen_freq[] = {
227 GEN1_CORE_CLK_FREQ,
228 GEN2_CORE_CLK_FREQ,
229 GEN3_CORE_CLK_FREQ,
230 GEN4_CORE_CLK_FREQ
231 };
232
233 struct tegra_pcie_dw_of_data {
234 u32 version;
235 enum dw_pcie_device_mode mode;
236 bool has_msix_doorbell_access_fix;
237 bool has_sbr_reset_fix;
238 bool has_l1ss_exit_fix;
239 bool has_ltr_req_fix;
240 u32 cdm_chk_int_en_bit;
241 u32 gen4_preset_vec;
242 u8 n_fts[2];
243 };
244
245 struct tegra_pcie_dw {
246 struct device *dev;
247 struct resource *appl_res;
248 struct resource *dbi_res;
249 struct resource *atu_dma_res;
250 void __iomem *appl_base;
251 struct clk *core_clk;
252 struct reset_control *core_apb_rst;
253 struct reset_control *core_rst;
254 struct dw_pcie pci;
255 struct tegra_bpmp *bpmp;
256
257 struct tegra_pcie_dw_of_data *of_data;
258
259 bool supports_clkreq;
260 bool enable_cdm_check;
261 bool enable_srns;
262 bool link_state;
263 bool update_fc_fixup;
264 bool enable_ext_refclk;
265 u8 init_link_width;
266 u32 msi_ctrl_int;
267 u32 num_lanes;
268 u32 cid;
269 u32 cfg_link_cap_l1sub;
270 u32 ras_des_cap;
271 u32 pcie_cap_base;
272 u32 aspm_cmrt;
273 u32 aspm_pwr_on_t;
274 u32 aspm_l0s_enter_lat;
275
276 struct regulator *pex_ctl_supply;
277 struct regulator *slot_ctl_3v3;
278 struct regulator *slot_ctl_12v;
279
280 unsigned int phy_count;
281 struct phy **phys;
282
283 struct dentry *debugfs;
284
285 /* Endpoint mode specific */
286 struct gpio_desc *pex_rst_gpiod;
287 struct gpio_desc *pex_refclk_sel_gpiod;
288 unsigned int pex_rst_irq;
289 int ep_state;
290 };
291
to_tegra_pcie(struct dw_pcie * pci)292 static inline struct tegra_pcie_dw *to_tegra_pcie(struct dw_pcie *pci)
293 {
294 return container_of(pci, struct tegra_pcie_dw, pci);
295 }
296
appl_writel(struct tegra_pcie_dw * pcie,const u32 value,const u32 reg)297 static inline void appl_writel(struct tegra_pcie_dw *pcie, const u32 value,
298 const u32 reg)
299 {
300 writel_relaxed(value, pcie->appl_base + reg);
301 }
302
appl_readl(struct tegra_pcie_dw * pcie,const u32 reg)303 static inline u32 appl_readl(struct tegra_pcie_dw *pcie, const u32 reg)
304 {
305 return readl_relaxed(pcie->appl_base + reg);
306 }
307
308 struct tegra_pcie_soc {
309 enum dw_pcie_device_mode mode;
310 };
311
apply_bad_link_workaround(struct dw_pcie_rp * pp)312 static void apply_bad_link_workaround(struct dw_pcie_rp *pp)
313 {
314 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
315 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
316 u32 current_link_width;
317 u16 val;
318
319 /*
320 * NOTE:- Since this scenario is uncommon and link as such is not
321 * stable anyway, not waiting to confirm if link is really
322 * transitioning to Gen-2 speed
323 */
324 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA);
325 if (val & PCI_EXP_LNKSTA_LBMS) {
326 current_link_width = (val & PCI_EXP_LNKSTA_NLW) >>
327 PCI_EXP_LNKSTA_NLW_SHIFT;
328 if (pcie->init_link_width > current_link_width) {
329 dev_warn(pci->dev, "PCIe link is bad, width reduced\n");
330 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
331 PCI_EXP_LNKCTL2);
332 val &= ~PCI_EXP_LNKCTL2_TLS;
333 val |= PCI_EXP_LNKCTL2_TLS_2_5GT;
334 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
335 PCI_EXP_LNKCTL2, val);
336
337 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
338 PCI_EXP_LNKCTL);
339 val |= PCI_EXP_LNKCTL_RL;
340 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
341 PCI_EXP_LNKCTL, val);
342 }
343 }
344 }
345
tegra_pcie_rp_irq_handler(int irq,void * arg)346 static irqreturn_t tegra_pcie_rp_irq_handler(int irq, void *arg)
347 {
348 struct tegra_pcie_dw *pcie = arg;
349 struct dw_pcie *pci = &pcie->pci;
350 struct dw_pcie_rp *pp = &pci->pp;
351 u32 val, status_l0, status_l1;
352 u16 val_w;
353
354 status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0);
355 if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) {
356 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0);
357 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0);
358 if (!pcie->of_data->has_sbr_reset_fix &&
359 status_l1 & APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED) {
360 /* SBR & Surprise Link Down WAR */
361 val = appl_readl(pcie, APPL_CAR_RESET_OVRD);
362 val &= ~APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N;
363 appl_writel(pcie, val, APPL_CAR_RESET_OVRD);
364 udelay(1);
365 val = appl_readl(pcie, APPL_CAR_RESET_OVRD);
366 val |= APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N;
367 appl_writel(pcie, val, APPL_CAR_RESET_OVRD);
368
369 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
370 val |= PORT_LOGIC_SPEED_CHANGE;
371 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
372 }
373 }
374
375 if (status_l0 & APPL_INTR_STATUS_L0_INT_INT) {
376 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_8_0);
377 if (status_l1 & APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS) {
378 appl_writel(pcie,
379 APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS,
380 APPL_INTR_STATUS_L1_8_0);
381 apply_bad_link_workaround(pp);
382 }
383 if (status_l1 & APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS) {
384 val_w = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
385 PCI_EXP_LNKSTA);
386 val_w |= PCI_EXP_LNKSTA_LBMS;
387 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
388 PCI_EXP_LNKSTA, val_w);
389
390 appl_writel(pcie,
391 APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS,
392 APPL_INTR_STATUS_L1_8_0);
393
394 val_w = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
395 PCI_EXP_LNKSTA);
396 dev_dbg(pci->dev, "Link Speed : Gen-%u\n", val_w &
397 PCI_EXP_LNKSTA_CLS);
398 }
399 }
400
401 if (status_l0 & APPL_INTR_STATUS_L0_CDM_REG_CHK_INT) {
402 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_18);
403 val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS);
404 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT) {
405 dev_info(pci->dev, "CDM check complete\n");
406 val |= PCIE_PL_CHK_REG_CHK_REG_COMPLETE;
407 }
408 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR) {
409 dev_err(pci->dev, "CDM comparison mismatch\n");
410 val |= PCIE_PL_CHK_REG_CHK_REG_COMPARISON_ERROR;
411 }
412 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR) {
413 dev_err(pci->dev, "CDM Logic error\n");
414 val |= PCIE_PL_CHK_REG_CHK_REG_LOGIC_ERROR;
415 }
416 dw_pcie_writel_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS, val);
417 val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_ERR_ADDR);
418 dev_err(pci->dev, "CDM Error Address Offset = 0x%08X\n", val);
419 }
420
421 return IRQ_HANDLED;
422 }
423
pex_ep_event_hot_rst_done(struct tegra_pcie_dw * pcie)424 static void pex_ep_event_hot_rst_done(struct tegra_pcie_dw *pcie)
425 {
426 u32 val;
427
428 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
429 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
430 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
431 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
432 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
433 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
434 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
435 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
436 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
437 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
438 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
439 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
440 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
441 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
442 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
443 appl_writel(pcie, 0xFFFFFFFF, APPL_MSI_CTRL_2);
444
445 val = appl_readl(pcie, APPL_CTRL);
446 val |= APPL_CTRL_LTSSM_EN;
447 appl_writel(pcie, val, APPL_CTRL);
448 }
449
tegra_pcie_ep_irq_thread(int irq,void * arg)450 static irqreturn_t tegra_pcie_ep_irq_thread(int irq, void *arg)
451 {
452 struct tegra_pcie_dw *pcie = arg;
453 struct dw_pcie *pci = &pcie->pci;
454 u32 val, speed;
455
456 speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) &
457 PCI_EXP_LNKSTA_CLS;
458 clk_set_rate(pcie->core_clk, pcie_gen_freq[speed - 1]);
459
460 if (pcie->of_data->has_ltr_req_fix)
461 return IRQ_HANDLED;
462
463 /* If EP doesn't advertise L1SS, just return */
464 val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub);
465 if (!(val & (PCI_L1SS_CAP_ASPM_L1_1 | PCI_L1SS_CAP_ASPM_L1_2)))
466 return IRQ_HANDLED;
467
468 /* Check if BME is set to '1' */
469 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
470 if (val & PCI_COMMAND_MASTER) {
471 ktime_t timeout;
472
473 /* 110us for both snoop and no-snoop */
474 val = 110 | (2 << PCI_LTR_SCALE_SHIFT) | LTR_MSG_REQ;
475 val |= (val << LTR_MST_NO_SNOOP_SHIFT);
476 appl_writel(pcie, val, APPL_LTR_MSG_1);
477
478 /* Send LTR upstream */
479 val = appl_readl(pcie, APPL_LTR_MSG_2);
480 val |= APPL_LTR_MSG_2_LTR_MSG_REQ_STATE;
481 appl_writel(pcie, val, APPL_LTR_MSG_2);
482
483 timeout = ktime_add_us(ktime_get(), LTR_MSG_TIMEOUT);
484 for (;;) {
485 val = appl_readl(pcie, APPL_LTR_MSG_2);
486 if (!(val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE))
487 break;
488 if (ktime_after(ktime_get(), timeout))
489 break;
490 usleep_range(1000, 1100);
491 }
492 if (val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE)
493 dev_err(pcie->dev, "Failed to send LTR message\n");
494 }
495
496 return IRQ_HANDLED;
497 }
498
tegra_pcie_ep_hard_irq(int irq,void * arg)499 static irqreturn_t tegra_pcie_ep_hard_irq(int irq, void *arg)
500 {
501 struct tegra_pcie_dw *pcie = arg;
502 struct dw_pcie_ep *ep = &pcie->pci.ep;
503 int spurious = 1;
504 u32 status_l0, status_l1, link_status;
505
506 status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0);
507 if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) {
508 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0);
509 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0);
510
511 if (status_l1 & APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE)
512 pex_ep_event_hot_rst_done(pcie);
513
514 if (status_l1 & APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED) {
515 link_status = appl_readl(pcie, APPL_LINK_STATUS);
516 if (link_status & APPL_LINK_STATUS_RDLH_LINK_UP) {
517 dev_dbg(pcie->dev, "Link is up with Host\n");
518 dw_pcie_ep_linkup(ep);
519 }
520 }
521
522 spurious = 0;
523 }
524
525 if (status_l0 & APPL_INTR_STATUS_L0_PCI_CMD_EN_INT) {
526 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_15);
527 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_15);
528
529 if (status_l1 & APPL_INTR_STATUS_L1_15_CFG_BME_CHGED)
530 return IRQ_WAKE_THREAD;
531
532 spurious = 0;
533 }
534
535 if (spurious) {
536 dev_warn(pcie->dev, "Random interrupt (STATUS = 0x%08X)\n",
537 status_l0);
538 appl_writel(pcie, status_l0, APPL_INTR_STATUS_L0);
539 }
540
541 return IRQ_HANDLED;
542 }
543
tegra_pcie_dw_rd_own_conf(struct pci_bus * bus,u32 devfn,int where,int size,u32 * val)544 static int tegra_pcie_dw_rd_own_conf(struct pci_bus *bus, u32 devfn, int where,
545 int size, u32 *val)
546 {
547 struct dw_pcie_rp *pp = bus->sysdata;
548 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
549 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
550
551 /*
552 * This is an endpoint mode specific register happen to appear even
553 * when controller is operating in root port mode and system hangs
554 * when it is accessed with link being in ASPM-L1 state.
555 * So skip accessing it altogether
556 */
557 if (!pcie->of_data->has_msix_doorbell_access_fix &&
558 !PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL) {
559 *val = 0x00000000;
560 return PCIBIOS_SUCCESSFUL;
561 }
562
563 return pci_generic_config_read(bus, devfn, where, size, val);
564 }
565
tegra_pcie_dw_wr_own_conf(struct pci_bus * bus,u32 devfn,int where,int size,u32 val)566 static int tegra_pcie_dw_wr_own_conf(struct pci_bus *bus, u32 devfn, int where,
567 int size, u32 val)
568 {
569 struct dw_pcie_rp *pp = bus->sysdata;
570 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
571 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
572
573 /*
574 * This is an endpoint mode specific register happen to appear even
575 * when controller is operating in root port mode and system hangs
576 * when it is accessed with link being in ASPM-L1 state.
577 * So skip accessing it altogether
578 */
579 if (!pcie->of_data->has_msix_doorbell_access_fix &&
580 !PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL)
581 return PCIBIOS_SUCCESSFUL;
582
583 return pci_generic_config_write(bus, devfn, where, size, val);
584 }
585
586 static struct pci_ops tegra_pci_ops = {
587 .map_bus = dw_pcie_own_conf_map_bus,
588 .read = tegra_pcie_dw_rd_own_conf,
589 .write = tegra_pcie_dw_wr_own_conf,
590 };
591
592 #if defined(CONFIG_PCIEASPM)
disable_aspm_l11(struct tegra_pcie_dw * pcie)593 static void disable_aspm_l11(struct tegra_pcie_dw *pcie)
594 {
595 u32 val;
596
597 val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub);
598 val &= ~PCI_L1SS_CAP_ASPM_L1_1;
599 dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val);
600 }
601
disable_aspm_l12(struct tegra_pcie_dw * pcie)602 static void disable_aspm_l12(struct tegra_pcie_dw *pcie)
603 {
604 u32 val;
605
606 val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub);
607 val &= ~PCI_L1SS_CAP_ASPM_L1_2;
608 dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val);
609 }
610
event_counter_prog(struct tegra_pcie_dw * pcie,u32 event)611 static inline u32 event_counter_prog(struct tegra_pcie_dw *pcie, u32 event)
612 {
613 u32 val;
614
615 val = dw_pcie_readl_dbi(&pcie->pci, pcie->ras_des_cap +
616 PCIE_RAS_DES_EVENT_COUNTER_CONTROL);
617 val &= ~(EVENT_COUNTER_EVENT_SEL_MASK << EVENT_COUNTER_EVENT_SEL_SHIFT);
618 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
619 val |= event << EVENT_COUNTER_EVENT_SEL_SHIFT;
620 val |= EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
621 dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
622 PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
623 val = dw_pcie_readl_dbi(&pcie->pci, pcie->ras_des_cap +
624 PCIE_RAS_DES_EVENT_COUNTER_DATA);
625
626 return val;
627 }
628
aspm_state_cnt(struct seq_file * s,void * data)629 static int aspm_state_cnt(struct seq_file *s, void *data)
630 {
631 struct tegra_pcie_dw *pcie = (struct tegra_pcie_dw *)
632 dev_get_drvdata(s->private);
633 u32 val;
634
635 seq_printf(s, "Tx L0s entry count : %u\n",
636 event_counter_prog(pcie, EVENT_COUNTER_EVENT_Tx_L0S));
637
638 seq_printf(s, "Rx L0s entry count : %u\n",
639 event_counter_prog(pcie, EVENT_COUNTER_EVENT_Rx_L0S));
640
641 seq_printf(s, "Link L1 entry count : %u\n",
642 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1));
643
644 seq_printf(s, "Link L1.1 entry count : %u\n",
645 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_1));
646
647 seq_printf(s, "Link L1.2 entry count : %u\n",
648 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_2));
649
650 /* Clear all counters */
651 dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
652 PCIE_RAS_DES_EVENT_COUNTER_CONTROL,
653 EVENT_COUNTER_ALL_CLEAR);
654
655 /* Re-enable counting */
656 val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
657 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
658 dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
659 PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
660
661 return 0;
662 }
663
init_host_aspm(struct tegra_pcie_dw * pcie)664 static void init_host_aspm(struct tegra_pcie_dw *pcie)
665 {
666 struct dw_pcie *pci = &pcie->pci;
667 u32 val;
668
669 val = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_L1SS);
670 pcie->cfg_link_cap_l1sub = val + PCI_L1SS_CAP;
671
672 pcie->ras_des_cap = dw_pcie_find_ext_capability(&pcie->pci,
673 PCI_EXT_CAP_ID_VNDR);
674
675 /* Enable ASPM counters */
676 val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
677 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
678 dw_pcie_writel_dbi(pci, pcie->ras_des_cap +
679 PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
680
681 /* Program T_cmrt and T_pwr_on values */
682 val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub);
683 val &= ~(PCI_L1SS_CAP_CM_RESTORE_TIME | PCI_L1SS_CAP_P_PWR_ON_VALUE);
684 val |= (pcie->aspm_cmrt << 8);
685 val |= (pcie->aspm_pwr_on_t << 19);
686 dw_pcie_writel_dbi(pci, pcie->cfg_link_cap_l1sub, val);
687
688 /* Program L0s and L1 entrance latencies */
689 val = dw_pcie_readl_dbi(pci, PCIE_PORT_AFR);
690 val &= ~PORT_AFR_L0S_ENTRANCE_LAT_MASK;
691 val |= (pcie->aspm_l0s_enter_lat << PORT_AFR_L0S_ENTRANCE_LAT_SHIFT);
692 val |= PORT_AFR_ENTER_ASPM;
693 dw_pcie_writel_dbi(pci, PCIE_PORT_AFR, val);
694 }
695
init_debugfs(struct tegra_pcie_dw * pcie)696 static void init_debugfs(struct tegra_pcie_dw *pcie)
697 {
698 debugfs_create_devm_seqfile(pcie->dev, "aspm_state_cnt", pcie->debugfs,
699 aspm_state_cnt);
700 }
701 #else
disable_aspm_l12(struct tegra_pcie_dw * pcie)702 static inline void disable_aspm_l12(struct tegra_pcie_dw *pcie) { return; }
disable_aspm_l11(struct tegra_pcie_dw * pcie)703 static inline void disable_aspm_l11(struct tegra_pcie_dw *pcie) { return; }
init_host_aspm(struct tegra_pcie_dw * pcie)704 static inline void init_host_aspm(struct tegra_pcie_dw *pcie) { return; }
init_debugfs(struct tegra_pcie_dw * pcie)705 static inline void init_debugfs(struct tegra_pcie_dw *pcie) { return; }
706 #endif
707
tegra_pcie_enable_system_interrupts(struct dw_pcie_rp * pp)708 static void tegra_pcie_enable_system_interrupts(struct dw_pcie_rp *pp)
709 {
710 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
711 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
712 u32 val;
713 u16 val_w;
714
715 val = appl_readl(pcie, APPL_INTR_EN_L0_0);
716 val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN;
717 appl_writel(pcie, val, APPL_INTR_EN_L0_0);
718
719 if (!pcie->of_data->has_sbr_reset_fix) {
720 val = appl_readl(pcie, APPL_INTR_EN_L1_0_0);
721 val |= APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN;
722 appl_writel(pcie, val, APPL_INTR_EN_L1_0_0);
723 }
724
725 if (pcie->enable_cdm_check) {
726 val = appl_readl(pcie, APPL_INTR_EN_L0_0);
727 val |= pcie->of_data->cdm_chk_int_en_bit;
728 appl_writel(pcie, val, APPL_INTR_EN_L0_0);
729
730 val = appl_readl(pcie, APPL_INTR_EN_L1_18);
731 val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR;
732 val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR;
733 appl_writel(pcie, val, APPL_INTR_EN_L1_18);
734 }
735
736 val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base +
737 PCI_EXP_LNKSTA);
738 pcie->init_link_width = (val_w & PCI_EXP_LNKSTA_NLW) >>
739 PCI_EXP_LNKSTA_NLW_SHIFT;
740
741 val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base +
742 PCI_EXP_LNKCTL);
743 val_w |= PCI_EXP_LNKCTL_LBMIE;
744 dw_pcie_writew_dbi(&pcie->pci, pcie->pcie_cap_base + PCI_EXP_LNKCTL,
745 val_w);
746 }
747
tegra_pcie_enable_legacy_interrupts(struct dw_pcie_rp * pp)748 static void tegra_pcie_enable_legacy_interrupts(struct dw_pcie_rp *pp)
749 {
750 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
751 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
752 u32 val;
753
754 /* Enable legacy interrupt generation */
755 val = appl_readl(pcie, APPL_INTR_EN_L0_0);
756 val |= APPL_INTR_EN_L0_0_SYS_INTR_EN;
757 val |= APPL_INTR_EN_L0_0_INT_INT_EN;
758 appl_writel(pcie, val, APPL_INTR_EN_L0_0);
759
760 val = appl_readl(pcie, APPL_INTR_EN_L1_8_0);
761 val |= APPL_INTR_EN_L1_8_INTX_EN;
762 val |= APPL_INTR_EN_L1_8_AUTO_BW_INT_EN;
763 val |= APPL_INTR_EN_L1_8_BW_MGT_INT_EN;
764 if (IS_ENABLED(CONFIG_PCIEAER))
765 val |= APPL_INTR_EN_L1_8_AER_INT_EN;
766 appl_writel(pcie, val, APPL_INTR_EN_L1_8_0);
767 }
768
tegra_pcie_enable_msi_interrupts(struct dw_pcie_rp * pp)769 static void tegra_pcie_enable_msi_interrupts(struct dw_pcie_rp *pp)
770 {
771 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
772 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
773 u32 val;
774
775 /* Enable MSI interrupt generation */
776 val = appl_readl(pcie, APPL_INTR_EN_L0_0);
777 val |= APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN;
778 val |= APPL_INTR_EN_L0_0_MSI_RCV_INT_EN;
779 appl_writel(pcie, val, APPL_INTR_EN_L0_0);
780 }
781
tegra_pcie_enable_interrupts(struct dw_pcie_rp * pp)782 static void tegra_pcie_enable_interrupts(struct dw_pcie_rp *pp)
783 {
784 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
785 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
786
787 /* Clear interrupt statuses before enabling interrupts */
788 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
789 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
790 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
791 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
792 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
793 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
794 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
795 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
796 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
797 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
798 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
799 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
800 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
801 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
802 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
803
804 tegra_pcie_enable_system_interrupts(pp);
805 tegra_pcie_enable_legacy_interrupts(pp);
806 if (IS_ENABLED(CONFIG_PCI_MSI))
807 tegra_pcie_enable_msi_interrupts(pp);
808 }
809
config_gen3_gen4_eq_presets(struct tegra_pcie_dw * pcie)810 static void config_gen3_gen4_eq_presets(struct tegra_pcie_dw *pcie)
811 {
812 struct dw_pcie *pci = &pcie->pci;
813 u32 val, offset, i;
814
815 /* Program init preset */
816 for (i = 0; i < pcie->num_lanes; i++) {
817 val = dw_pcie_readw_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2));
818 val &= ~CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK;
819 val |= GEN3_GEN4_EQ_PRESET_INIT;
820 val &= ~CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK;
821 val |= (GEN3_GEN4_EQ_PRESET_INIT <<
822 CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT);
823 dw_pcie_writew_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2), val);
824
825 offset = dw_pcie_find_ext_capability(pci,
826 PCI_EXT_CAP_ID_PL_16GT) +
827 PCI_PL_16GT_LE_CTRL;
828 val = dw_pcie_readb_dbi(pci, offset + i);
829 val &= ~PCI_PL_16GT_LE_CTRL_DSP_TX_PRESET_MASK;
830 val |= GEN3_GEN4_EQ_PRESET_INIT;
831 val &= ~PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK;
832 val |= (GEN3_GEN4_EQ_PRESET_INIT <<
833 PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT);
834 dw_pcie_writeb_dbi(pci, offset + i, val);
835 }
836
837 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
838 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
839 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
840
841 val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF);
842 val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK;
843 val |= (0x3ff << GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT);
844 val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK;
845 dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val);
846
847 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
848 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
849 val |= (0x1 << GEN3_RELATED_OFF_RATE_SHADOW_SEL_SHIFT);
850 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
851
852 val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF);
853 val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK;
854 val |= (pcie->of_data->gen4_preset_vec <<
855 GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT);
856 val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK;
857 dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val);
858
859 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
860 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
861 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
862 }
863
tegra_pcie_dw_host_init(struct dw_pcie_rp * pp)864 static int tegra_pcie_dw_host_init(struct dw_pcie_rp *pp)
865 {
866 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
867 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
868 u32 val;
869 u16 val_16;
870
871 pp->bridge->ops = &tegra_pci_ops;
872
873 if (!pcie->pcie_cap_base)
874 pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci,
875 PCI_CAP_ID_EXP);
876
877 val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL);
878 val_16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
879 val_16 |= PCI_EXP_DEVCTL_PAYLOAD_256B;
880 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL, val_16);
881
882 val = dw_pcie_readl_dbi(pci, PCI_IO_BASE);
883 val &= ~(IO_BASE_IO_DECODE | IO_BASE_IO_DECODE_BIT8);
884 dw_pcie_writel_dbi(pci, PCI_IO_BASE, val);
885
886 val = dw_pcie_readl_dbi(pci, PCI_PREF_MEMORY_BASE);
887 val |= CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE;
888 val |= CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE;
889 dw_pcie_writel_dbi(pci, PCI_PREF_MEMORY_BASE, val);
890
891 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
892
893 /* Enable as 0xFFFF0001 response for CRS */
894 val = dw_pcie_readl_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT);
895 val &= ~(AMBA_ERROR_RESPONSE_CRS_MASK << AMBA_ERROR_RESPONSE_CRS_SHIFT);
896 val |= (AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001 <<
897 AMBA_ERROR_RESPONSE_CRS_SHIFT);
898 dw_pcie_writel_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT, val);
899
900 /* Configure Max lane width from DT */
901 val = dw_pcie_readl_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP);
902 val &= ~PCI_EXP_LNKCAP_MLW;
903 val |= (pcie->num_lanes << PCI_EXP_LNKSTA_NLW_SHIFT);
904 dw_pcie_writel_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP, val);
905
906 /* Clear Slot Clock Configuration bit if SRNS configuration */
907 if (pcie->enable_srns) {
908 val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
909 PCI_EXP_LNKSTA);
910 val_16 &= ~PCI_EXP_LNKSTA_SLC;
911 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA,
912 val_16);
913 }
914
915 config_gen3_gen4_eq_presets(pcie);
916
917 init_host_aspm(pcie);
918
919 /* Disable ASPM-L1SS advertisement if there is no CLKREQ routing */
920 if (!pcie->supports_clkreq) {
921 disable_aspm_l11(pcie);
922 disable_aspm_l12(pcie);
923 }
924
925 if (!pcie->of_data->has_l1ss_exit_fix) {
926 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
927 val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL;
928 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
929 }
930
931 if (pcie->update_fc_fixup) {
932 val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF);
933 val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT;
934 dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val);
935 }
936
937 clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ);
938
939 return 0;
940 }
941
tegra_pcie_dw_start_link(struct dw_pcie * pci)942 static int tegra_pcie_dw_start_link(struct dw_pcie *pci)
943 {
944 u32 val, offset, speed, tmp;
945 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
946 struct dw_pcie_rp *pp = &pci->pp;
947 bool retry = true;
948
949 if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
950 enable_irq(pcie->pex_rst_irq);
951 return 0;
952 }
953
954 retry_link:
955 /* Assert RST */
956 val = appl_readl(pcie, APPL_PINMUX);
957 val &= ~APPL_PINMUX_PEX_RST;
958 appl_writel(pcie, val, APPL_PINMUX);
959
960 usleep_range(100, 200);
961
962 /* Enable LTSSM */
963 val = appl_readl(pcie, APPL_CTRL);
964 val |= APPL_CTRL_LTSSM_EN;
965 appl_writel(pcie, val, APPL_CTRL);
966
967 /* De-assert RST */
968 val = appl_readl(pcie, APPL_PINMUX);
969 val |= APPL_PINMUX_PEX_RST;
970 appl_writel(pcie, val, APPL_PINMUX);
971
972 msleep(100);
973
974 if (dw_pcie_wait_for_link(pci)) {
975 if (!retry)
976 return 0;
977 /*
978 * There are some endpoints which can't get the link up if
979 * root port has Data Link Feature (DLF) enabled.
980 * Refer Spec rev 4.0 ver 1.0 sec 3.4.2 & 7.7.4 for more info
981 * on Scaled Flow Control and DLF.
982 * So, need to confirm that is indeed the case here and attempt
983 * link up once again with DLF disabled.
984 */
985 val = appl_readl(pcie, APPL_DEBUG);
986 val &= APPL_DEBUG_LTSSM_STATE_MASK;
987 val >>= APPL_DEBUG_LTSSM_STATE_SHIFT;
988 tmp = appl_readl(pcie, APPL_LINK_STATUS);
989 tmp &= APPL_LINK_STATUS_RDLH_LINK_UP;
990 if (!(val == 0x11 && !tmp)) {
991 /* Link is down for all good reasons */
992 return 0;
993 }
994
995 dev_info(pci->dev, "Link is down in DLL");
996 dev_info(pci->dev, "Trying again with DLFE disabled\n");
997 /* Disable LTSSM */
998 val = appl_readl(pcie, APPL_CTRL);
999 val &= ~APPL_CTRL_LTSSM_EN;
1000 appl_writel(pcie, val, APPL_CTRL);
1001
1002 reset_control_assert(pcie->core_rst);
1003 reset_control_deassert(pcie->core_rst);
1004
1005 offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_DLF);
1006 val = dw_pcie_readl_dbi(pci, offset + PCI_DLF_CAP);
1007 val &= ~PCI_DLF_EXCHANGE_ENABLE;
1008 dw_pcie_writel_dbi(pci, offset + PCI_DLF_CAP, val);
1009
1010 tegra_pcie_dw_host_init(pp);
1011 dw_pcie_setup_rc(pp);
1012
1013 retry = false;
1014 goto retry_link;
1015 }
1016
1017 speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) &
1018 PCI_EXP_LNKSTA_CLS;
1019 clk_set_rate(pcie->core_clk, pcie_gen_freq[speed - 1]);
1020
1021 tegra_pcie_enable_interrupts(pp);
1022
1023 return 0;
1024 }
1025
tegra_pcie_dw_link_up(struct dw_pcie * pci)1026 static int tegra_pcie_dw_link_up(struct dw_pcie *pci)
1027 {
1028 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1029 u32 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA);
1030
1031 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1032 }
1033
tegra_pcie_dw_stop_link(struct dw_pcie * pci)1034 static void tegra_pcie_dw_stop_link(struct dw_pcie *pci)
1035 {
1036 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1037
1038 disable_irq(pcie->pex_rst_irq);
1039 }
1040
1041 static const struct dw_pcie_ops tegra_dw_pcie_ops = {
1042 .link_up = tegra_pcie_dw_link_up,
1043 .start_link = tegra_pcie_dw_start_link,
1044 .stop_link = tegra_pcie_dw_stop_link,
1045 };
1046
1047 static const struct dw_pcie_host_ops tegra_pcie_dw_host_ops = {
1048 .host_init = tegra_pcie_dw_host_init,
1049 };
1050
tegra_pcie_disable_phy(struct tegra_pcie_dw * pcie)1051 static void tegra_pcie_disable_phy(struct tegra_pcie_dw *pcie)
1052 {
1053 unsigned int phy_count = pcie->phy_count;
1054
1055 while (phy_count--) {
1056 phy_power_off(pcie->phys[phy_count]);
1057 phy_exit(pcie->phys[phy_count]);
1058 }
1059 }
1060
tegra_pcie_enable_phy(struct tegra_pcie_dw * pcie)1061 static int tegra_pcie_enable_phy(struct tegra_pcie_dw *pcie)
1062 {
1063 unsigned int i;
1064 int ret;
1065
1066 for (i = 0; i < pcie->phy_count; i++) {
1067 ret = phy_init(pcie->phys[i]);
1068 if (ret < 0)
1069 goto phy_power_off;
1070
1071 ret = phy_power_on(pcie->phys[i]);
1072 if (ret < 0)
1073 goto phy_exit;
1074 }
1075
1076 return 0;
1077
1078 phy_power_off:
1079 while (i--) {
1080 phy_power_off(pcie->phys[i]);
1081 phy_exit:
1082 phy_exit(pcie->phys[i]);
1083 }
1084
1085 return ret;
1086 }
1087
tegra_pcie_dw_parse_dt(struct tegra_pcie_dw * pcie)1088 static int tegra_pcie_dw_parse_dt(struct tegra_pcie_dw *pcie)
1089 {
1090 struct platform_device *pdev = to_platform_device(pcie->dev);
1091 struct device_node *np = pcie->dev->of_node;
1092 int ret;
1093
1094 pcie->dbi_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
1095 if (!pcie->dbi_res) {
1096 dev_err(pcie->dev, "Failed to find \"dbi\" region\n");
1097 return -ENODEV;
1098 }
1099
1100 ret = of_property_read_u32(np, "nvidia,aspm-cmrt-us", &pcie->aspm_cmrt);
1101 if (ret < 0) {
1102 dev_info(pcie->dev, "Failed to read ASPM T_cmrt: %d\n", ret);
1103 return ret;
1104 }
1105
1106 ret = of_property_read_u32(np, "nvidia,aspm-pwr-on-t-us",
1107 &pcie->aspm_pwr_on_t);
1108 if (ret < 0)
1109 dev_info(pcie->dev, "Failed to read ASPM Power On time: %d\n",
1110 ret);
1111
1112 ret = of_property_read_u32(np, "nvidia,aspm-l0s-entrance-latency-us",
1113 &pcie->aspm_l0s_enter_lat);
1114 if (ret < 0)
1115 dev_info(pcie->dev,
1116 "Failed to read ASPM L0s Entrance latency: %d\n", ret);
1117
1118 ret = of_property_read_u32(np, "num-lanes", &pcie->num_lanes);
1119 if (ret < 0) {
1120 dev_err(pcie->dev, "Failed to read num-lanes: %d\n", ret);
1121 return ret;
1122 }
1123
1124 ret = of_property_read_u32_index(np, "nvidia,bpmp", 1, &pcie->cid);
1125 if (ret) {
1126 dev_err(pcie->dev, "Failed to read Controller-ID: %d\n", ret);
1127 return ret;
1128 }
1129
1130 ret = of_property_count_strings(np, "phy-names");
1131 if (ret < 0) {
1132 dev_err(pcie->dev, "Failed to find PHY entries: %d\n",
1133 ret);
1134 return ret;
1135 }
1136 pcie->phy_count = ret;
1137
1138 if (of_property_read_bool(np, "nvidia,update-fc-fixup"))
1139 pcie->update_fc_fixup = true;
1140
1141 /* RP using an external REFCLK is supported only in Tegra234 */
1142 if (pcie->of_data->version == TEGRA194_DWC_IP_VER) {
1143 if (pcie->of_data->mode == DW_PCIE_EP_TYPE)
1144 pcie->enable_ext_refclk = true;
1145 } else {
1146 pcie->enable_ext_refclk =
1147 of_property_read_bool(pcie->dev->of_node,
1148 "nvidia,enable-ext-refclk");
1149 }
1150
1151 pcie->supports_clkreq =
1152 of_property_read_bool(pcie->dev->of_node, "supports-clkreq");
1153
1154 pcie->enable_cdm_check =
1155 of_property_read_bool(np, "snps,enable-cdm-check");
1156
1157 if (pcie->of_data->version == TEGRA234_DWC_IP_VER)
1158 pcie->enable_srns =
1159 of_property_read_bool(np, "nvidia,enable-srns");
1160
1161 if (pcie->of_data->mode == DW_PCIE_RC_TYPE)
1162 return 0;
1163
1164 /* Endpoint mode specific DT entries */
1165 pcie->pex_rst_gpiod = devm_gpiod_get(pcie->dev, "reset", GPIOD_IN);
1166 if (IS_ERR(pcie->pex_rst_gpiod)) {
1167 int err = PTR_ERR(pcie->pex_rst_gpiod);
1168 const char *level = KERN_ERR;
1169
1170 if (err == -EPROBE_DEFER)
1171 level = KERN_DEBUG;
1172
1173 dev_printk(level, pcie->dev,
1174 dev_fmt("Failed to get PERST GPIO: %d\n"),
1175 err);
1176 return err;
1177 }
1178
1179 pcie->pex_refclk_sel_gpiod = devm_gpiod_get(pcie->dev,
1180 "nvidia,refclk-select",
1181 GPIOD_OUT_HIGH);
1182 if (IS_ERR(pcie->pex_refclk_sel_gpiod)) {
1183 int err = PTR_ERR(pcie->pex_refclk_sel_gpiod);
1184 const char *level = KERN_ERR;
1185
1186 if (err == -EPROBE_DEFER)
1187 level = KERN_DEBUG;
1188
1189 dev_printk(level, pcie->dev,
1190 dev_fmt("Failed to get REFCLK select GPIOs: %d\n"),
1191 err);
1192 pcie->pex_refclk_sel_gpiod = NULL;
1193 }
1194
1195 return 0;
1196 }
1197
tegra_pcie_bpmp_set_ctrl_state(struct tegra_pcie_dw * pcie,bool enable)1198 static int tegra_pcie_bpmp_set_ctrl_state(struct tegra_pcie_dw *pcie,
1199 bool enable)
1200 {
1201 struct mrq_uphy_response resp;
1202 struct tegra_bpmp_message msg;
1203 struct mrq_uphy_request req;
1204
1205 /*
1206 * Controller-5 doesn't need to have its state set by BPMP-FW in
1207 * Tegra194
1208 */
1209 if (pcie->of_data->version == TEGRA194_DWC_IP_VER && pcie->cid == 5)
1210 return 0;
1211
1212 memset(&req, 0, sizeof(req));
1213 memset(&resp, 0, sizeof(resp));
1214
1215 req.cmd = CMD_UPHY_PCIE_CONTROLLER_STATE;
1216 req.controller_state.pcie_controller = pcie->cid;
1217 req.controller_state.enable = enable;
1218
1219 memset(&msg, 0, sizeof(msg));
1220 msg.mrq = MRQ_UPHY;
1221 msg.tx.data = &req;
1222 msg.tx.size = sizeof(req);
1223 msg.rx.data = &resp;
1224 msg.rx.size = sizeof(resp);
1225
1226 return tegra_bpmp_transfer(pcie->bpmp, &msg);
1227 }
1228
tegra_pcie_bpmp_set_pll_state(struct tegra_pcie_dw * pcie,bool enable)1229 static int tegra_pcie_bpmp_set_pll_state(struct tegra_pcie_dw *pcie,
1230 bool enable)
1231 {
1232 struct mrq_uphy_response resp;
1233 struct tegra_bpmp_message msg;
1234 struct mrq_uphy_request req;
1235
1236 memset(&req, 0, sizeof(req));
1237 memset(&resp, 0, sizeof(resp));
1238
1239 if (enable) {
1240 req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_INIT;
1241 req.ep_ctrlr_pll_init.ep_controller = pcie->cid;
1242 } else {
1243 req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_OFF;
1244 req.ep_ctrlr_pll_off.ep_controller = pcie->cid;
1245 }
1246
1247 memset(&msg, 0, sizeof(msg));
1248 msg.mrq = MRQ_UPHY;
1249 msg.tx.data = &req;
1250 msg.tx.size = sizeof(req);
1251 msg.rx.data = &resp;
1252 msg.rx.size = sizeof(resp);
1253
1254 return tegra_bpmp_transfer(pcie->bpmp, &msg);
1255 }
1256
tegra_pcie_downstream_dev_to_D0(struct tegra_pcie_dw * pcie)1257 static void tegra_pcie_downstream_dev_to_D0(struct tegra_pcie_dw *pcie)
1258 {
1259 struct dw_pcie_rp *pp = &pcie->pci.pp;
1260 struct pci_bus *child, *root_bus = NULL;
1261 struct pci_dev *pdev;
1262
1263 /*
1264 * link doesn't go into L2 state with some of the endpoints with Tegra
1265 * if they are not in D0 state. So, need to make sure that immediate
1266 * downstream devices are in D0 state before sending PME_TurnOff to put
1267 * link into L2 state.
1268 * This is as per PCI Express Base r4.0 v1.0 September 27-2017,
1269 * 5.2 Link State Power Management (Page #428).
1270 */
1271
1272 list_for_each_entry(child, &pp->bridge->bus->children, node) {
1273 /* Bring downstream devices to D0 if they are not already in */
1274 if (child->parent == pp->bridge->bus) {
1275 root_bus = child;
1276 break;
1277 }
1278 }
1279
1280 if (!root_bus) {
1281 dev_err(pcie->dev, "Failed to find downstream devices\n");
1282 return;
1283 }
1284
1285 list_for_each_entry(pdev, &root_bus->devices, bus_list) {
1286 if (PCI_SLOT(pdev->devfn) == 0) {
1287 if (pci_set_power_state(pdev, PCI_D0))
1288 dev_err(pcie->dev,
1289 "Failed to transition %s to D0 state\n",
1290 dev_name(&pdev->dev));
1291 }
1292 }
1293 }
1294
tegra_pcie_get_slot_regulators(struct tegra_pcie_dw * pcie)1295 static int tegra_pcie_get_slot_regulators(struct tegra_pcie_dw *pcie)
1296 {
1297 pcie->slot_ctl_3v3 = devm_regulator_get_optional(pcie->dev, "vpcie3v3");
1298 if (IS_ERR(pcie->slot_ctl_3v3)) {
1299 if (PTR_ERR(pcie->slot_ctl_3v3) != -ENODEV)
1300 return PTR_ERR(pcie->slot_ctl_3v3);
1301
1302 pcie->slot_ctl_3v3 = NULL;
1303 }
1304
1305 pcie->slot_ctl_12v = devm_regulator_get_optional(pcie->dev, "vpcie12v");
1306 if (IS_ERR(pcie->slot_ctl_12v)) {
1307 if (PTR_ERR(pcie->slot_ctl_12v) != -ENODEV)
1308 return PTR_ERR(pcie->slot_ctl_12v);
1309
1310 pcie->slot_ctl_12v = NULL;
1311 }
1312
1313 return 0;
1314 }
1315
tegra_pcie_enable_slot_regulators(struct tegra_pcie_dw * pcie)1316 static int tegra_pcie_enable_slot_regulators(struct tegra_pcie_dw *pcie)
1317 {
1318 int ret;
1319
1320 if (pcie->slot_ctl_3v3) {
1321 ret = regulator_enable(pcie->slot_ctl_3v3);
1322 if (ret < 0) {
1323 dev_err(pcie->dev,
1324 "Failed to enable 3.3V slot supply: %d\n", ret);
1325 return ret;
1326 }
1327 }
1328
1329 if (pcie->slot_ctl_12v) {
1330 ret = regulator_enable(pcie->slot_ctl_12v);
1331 if (ret < 0) {
1332 dev_err(pcie->dev,
1333 "Failed to enable 12V slot supply: %d\n", ret);
1334 goto fail_12v_enable;
1335 }
1336 }
1337
1338 /*
1339 * According to PCI Express Card Electromechanical Specification
1340 * Revision 1.1, Table-2.4, T_PVPERL (Power stable to PERST# inactive)
1341 * should be a minimum of 100ms.
1342 */
1343 if (pcie->slot_ctl_3v3 || pcie->slot_ctl_12v)
1344 msleep(100);
1345
1346 return 0;
1347
1348 fail_12v_enable:
1349 if (pcie->slot_ctl_3v3)
1350 regulator_disable(pcie->slot_ctl_3v3);
1351 return ret;
1352 }
1353
tegra_pcie_disable_slot_regulators(struct tegra_pcie_dw * pcie)1354 static void tegra_pcie_disable_slot_regulators(struct tegra_pcie_dw *pcie)
1355 {
1356 if (pcie->slot_ctl_12v)
1357 regulator_disable(pcie->slot_ctl_12v);
1358 if (pcie->slot_ctl_3v3)
1359 regulator_disable(pcie->slot_ctl_3v3);
1360 }
1361
tegra_pcie_config_controller(struct tegra_pcie_dw * pcie,bool en_hw_hot_rst)1362 static int tegra_pcie_config_controller(struct tegra_pcie_dw *pcie,
1363 bool en_hw_hot_rst)
1364 {
1365 int ret;
1366 u32 val;
1367
1368 ret = tegra_pcie_bpmp_set_ctrl_state(pcie, true);
1369 if (ret) {
1370 dev_err(pcie->dev,
1371 "Failed to enable controller %u: %d\n", pcie->cid, ret);
1372 return ret;
1373 }
1374
1375 if (pcie->enable_ext_refclk) {
1376 ret = tegra_pcie_bpmp_set_pll_state(pcie, true);
1377 if (ret) {
1378 dev_err(pcie->dev, "Failed to init UPHY: %d\n", ret);
1379 goto fail_pll_init;
1380 }
1381 }
1382
1383 ret = tegra_pcie_enable_slot_regulators(pcie);
1384 if (ret < 0)
1385 goto fail_slot_reg_en;
1386
1387 ret = regulator_enable(pcie->pex_ctl_supply);
1388 if (ret < 0) {
1389 dev_err(pcie->dev, "Failed to enable regulator: %d\n", ret);
1390 goto fail_reg_en;
1391 }
1392
1393 ret = clk_prepare_enable(pcie->core_clk);
1394 if (ret) {
1395 dev_err(pcie->dev, "Failed to enable core clock: %d\n", ret);
1396 goto fail_core_clk;
1397 }
1398
1399 ret = reset_control_deassert(pcie->core_apb_rst);
1400 if (ret) {
1401 dev_err(pcie->dev, "Failed to deassert core APB reset: %d\n",
1402 ret);
1403 goto fail_core_apb_rst;
1404 }
1405
1406 if (en_hw_hot_rst || pcie->of_data->has_sbr_reset_fix) {
1407 /* Enable HW_HOT_RST mode */
1408 val = appl_readl(pcie, APPL_CTRL);
1409 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
1410 APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
1411 val |= (APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST_LTSSM_EN <<
1412 APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
1413 val |= APPL_CTRL_HW_HOT_RST_EN;
1414 appl_writel(pcie, val, APPL_CTRL);
1415 }
1416
1417 ret = tegra_pcie_enable_phy(pcie);
1418 if (ret) {
1419 dev_err(pcie->dev, "Failed to enable PHY: %d\n", ret);
1420 goto fail_phy;
1421 }
1422
1423 /* Update CFG base address */
1424 appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK,
1425 APPL_CFG_BASE_ADDR);
1426
1427 /* Configure this core for RP mode operation */
1428 appl_writel(pcie, APPL_DM_TYPE_RP, APPL_DM_TYPE);
1429
1430 appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE);
1431
1432 val = appl_readl(pcie, APPL_CTRL);
1433 appl_writel(pcie, val | APPL_CTRL_SYS_PRE_DET_STATE, APPL_CTRL);
1434
1435 val = appl_readl(pcie, APPL_CFG_MISC);
1436 val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT);
1437 appl_writel(pcie, val, APPL_CFG_MISC);
1438
1439 if (pcie->enable_srns || pcie->enable_ext_refclk) {
1440 /*
1441 * When Tegra PCIe RP is using external clock, it cannot supply
1442 * same clock to its downstream hierarchy. Hence, gate PCIe RP
1443 * REFCLK out pads when RP & EP are using separate clocks or RP
1444 * is using an external REFCLK.
1445 */
1446 val = appl_readl(pcie, APPL_PINMUX);
1447 val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1448 val &= ~APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1449 appl_writel(pcie, val, APPL_PINMUX);
1450 }
1451
1452 if (!pcie->supports_clkreq) {
1453 val = appl_readl(pcie, APPL_PINMUX);
1454 val |= APPL_PINMUX_CLKREQ_OVERRIDE_EN;
1455 val &= ~APPL_PINMUX_CLKREQ_OVERRIDE;
1456 appl_writel(pcie, val, APPL_PINMUX);
1457 }
1458
1459 /* Update iATU_DMA base address */
1460 appl_writel(pcie,
1461 pcie->atu_dma_res->start & APPL_CFG_IATU_DMA_BASE_ADDR_MASK,
1462 APPL_CFG_IATU_DMA_BASE_ADDR);
1463
1464 reset_control_deassert(pcie->core_rst);
1465
1466 return ret;
1467
1468 fail_phy:
1469 reset_control_assert(pcie->core_apb_rst);
1470 fail_core_apb_rst:
1471 clk_disable_unprepare(pcie->core_clk);
1472 fail_core_clk:
1473 regulator_disable(pcie->pex_ctl_supply);
1474 fail_reg_en:
1475 tegra_pcie_disable_slot_regulators(pcie);
1476 fail_slot_reg_en:
1477 if (pcie->enable_ext_refclk)
1478 tegra_pcie_bpmp_set_pll_state(pcie, false);
1479 fail_pll_init:
1480 tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1481
1482 return ret;
1483 }
1484
tegra_pcie_unconfig_controller(struct tegra_pcie_dw * pcie)1485 static void tegra_pcie_unconfig_controller(struct tegra_pcie_dw *pcie)
1486 {
1487 int ret;
1488
1489 ret = reset_control_assert(pcie->core_rst);
1490 if (ret)
1491 dev_err(pcie->dev, "Failed to assert \"core\" reset: %d\n", ret);
1492
1493 tegra_pcie_disable_phy(pcie);
1494
1495 ret = reset_control_assert(pcie->core_apb_rst);
1496 if (ret)
1497 dev_err(pcie->dev, "Failed to assert APB reset: %d\n", ret);
1498
1499 clk_disable_unprepare(pcie->core_clk);
1500
1501 ret = regulator_disable(pcie->pex_ctl_supply);
1502 if (ret)
1503 dev_err(pcie->dev, "Failed to disable regulator: %d\n", ret);
1504
1505 tegra_pcie_disable_slot_regulators(pcie);
1506
1507 if (pcie->enable_ext_refclk) {
1508 ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1509 if (ret)
1510 dev_err(pcie->dev, "Failed to deinit UPHY: %d\n", ret);
1511 }
1512
1513 ret = tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1514 if (ret)
1515 dev_err(pcie->dev, "Failed to disable controller %d: %d\n",
1516 pcie->cid, ret);
1517 }
1518
tegra_pcie_init_controller(struct tegra_pcie_dw * pcie)1519 static int tegra_pcie_init_controller(struct tegra_pcie_dw *pcie)
1520 {
1521 struct dw_pcie *pci = &pcie->pci;
1522 struct dw_pcie_rp *pp = &pci->pp;
1523 int ret;
1524
1525 ret = tegra_pcie_config_controller(pcie, false);
1526 if (ret < 0)
1527 return ret;
1528
1529 pp->ops = &tegra_pcie_dw_host_ops;
1530
1531 ret = dw_pcie_host_init(pp);
1532 if (ret < 0) {
1533 dev_err(pcie->dev, "Failed to add PCIe port: %d\n", ret);
1534 goto fail_host_init;
1535 }
1536
1537 return 0;
1538
1539 fail_host_init:
1540 tegra_pcie_unconfig_controller(pcie);
1541 return ret;
1542 }
1543
tegra_pcie_try_link_l2(struct tegra_pcie_dw * pcie)1544 static int tegra_pcie_try_link_l2(struct tegra_pcie_dw *pcie)
1545 {
1546 u32 val;
1547
1548 if (!tegra_pcie_dw_link_up(&pcie->pci))
1549 return 0;
1550
1551 val = appl_readl(pcie, APPL_RADM_STATUS);
1552 val |= APPL_PM_XMT_TURNOFF_STATE;
1553 appl_writel(pcie, val, APPL_RADM_STATUS);
1554
1555 return readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG, val,
1556 val & APPL_DEBUG_PM_LINKST_IN_L2_LAT,
1557 1, PME_ACK_TIMEOUT);
1558 }
1559
tegra_pcie_dw_pme_turnoff(struct tegra_pcie_dw * pcie)1560 static void tegra_pcie_dw_pme_turnoff(struct tegra_pcie_dw *pcie)
1561 {
1562 u32 data;
1563 int err;
1564
1565 if (!tegra_pcie_dw_link_up(&pcie->pci)) {
1566 dev_dbg(pcie->dev, "PCIe link is not up...!\n");
1567 return;
1568 }
1569
1570 /*
1571 * PCIe controller exits from L2 only if reset is applied, so
1572 * controller doesn't handle interrupts. But in cases where
1573 * L2 entry fails, PERST# is asserted which can trigger surprise
1574 * link down AER. However this function call happens in
1575 * suspend_noirq(), so AER interrupt will not be processed.
1576 * Disable all interrupts to avoid such a scenario.
1577 */
1578 appl_writel(pcie, 0x0, APPL_INTR_EN_L0_0);
1579
1580 if (tegra_pcie_try_link_l2(pcie)) {
1581 dev_info(pcie->dev, "Link didn't transition to L2 state\n");
1582 /*
1583 * TX lane clock freq will reset to Gen1 only if link is in L2
1584 * or detect state.
1585 * So apply pex_rst to end point to force RP to go into detect
1586 * state
1587 */
1588 data = appl_readl(pcie, APPL_PINMUX);
1589 data &= ~APPL_PINMUX_PEX_RST;
1590 appl_writel(pcie, data, APPL_PINMUX);
1591
1592 /*
1593 * Some cards do not go to detect state even after de-asserting
1594 * PERST#. So, de-assert LTSSM to bring link to detect state.
1595 */
1596 data = readl(pcie->appl_base + APPL_CTRL);
1597 data &= ~APPL_CTRL_LTSSM_EN;
1598 writel(data, pcie->appl_base + APPL_CTRL);
1599
1600 err = readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG,
1601 data,
1602 ((data &
1603 APPL_DEBUG_LTSSM_STATE_MASK) >>
1604 APPL_DEBUG_LTSSM_STATE_SHIFT) ==
1605 LTSSM_STATE_PRE_DETECT,
1606 1, LTSSM_TIMEOUT);
1607 if (err)
1608 dev_info(pcie->dev, "Link didn't go to detect state\n");
1609 }
1610 /*
1611 * DBI registers may not be accessible after this as PLL-E would be
1612 * down depending on how CLKREQ is pulled by end point
1613 */
1614 data = appl_readl(pcie, APPL_PINMUX);
1615 data |= (APPL_PINMUX_CLKREQ_OVERRIDE_EN | APPL_PINMUX_CLKREQ_OVERRIDE);
1616 /* Cut REFCLK to slot */
1617 data |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1618 data &= ~APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1619 appl_writel(pcie, data, APPL_PINMUX);
1620 }
1621
tegra_pcie_deinit_controller(struct tegra_pcie_dw * pcie)1622 static void tegra_pcie_deinit_controller(struct tegra_pcie_dw *pcie)
1623 {
1624 tegra_pcie_downstream_dev_to_D0(pcie);
1625 dw_pcie_host_deinit(&pcie->pci.pp);
1626 tegra_pcie_dw_pme_turnoff(pcie);
1627 tegra_pcie_unconfig_controller(pcie);
1628 }
1629
tegra_pcie_config_rp(struct tegra_pcie_dw * pcie)1630 static int tegra_pcie_config_rp(struct tegra_pcie_dw *pcie)
1631 {
1632 struct device *dev = pcie->dev;
1633 char *name;
1634 int ret;
1635
1636 pm_runtime_enable(dev);
1637
1638 ret = pm_runtime_get_sync(dev);
1639 if (ret < 0) {
1640 dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n",
1641 ret);
1642 goto fail_pm_get_sync;
1643 }
1644
1645 ret = pinctrl_pm_select_default_state(dev);
1646 if (ret < 0) {
1647 dev_err(dev, "Failed to configure sideband pins: %d\n", ret);
1648 goto fail_pm_get_sync;
1649 }
1650
1651 ret = tegra_pcie_init_controller(pcie);
1652 if (ret < 0) {
1653 dev_err(dev, "Failed to initialize controller: %d\n", ret);
1654 goto fail_pm_get_sync;
1655 }
1656
1657 pcie->link_state = tegra_pcie_dw_link_up(&pcie->pci);
1658 if (!pcie->link_state) {
1659 ret = -ENOMEDIUM;
1660 goto fail_host_init;
1661 }
1662
1663 name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
1664 if (!name) {
1665 ret = -ENOMEM;
1666 goto fail_host_init;
1667 }
1668
1669 pcie->debugfs = debugfs_create_dir(name, NULL);
1670 init_debugfs(pcie);
1671
1672 return ret;
1673
1674 fail_host_init:
1675 tegra_pcie_deinit_controller(pcie);
1676 fail_pm_get_sync:
1677 pm_runtime_put_sync(dev);
1678 pm_runtime_disable(dev);
1679 return ret;
1680 }
1681
pex_ep_event_pex_rst_assert(struct tegra_pcie_dw * pcie)1682 static void pex_ep_event_pex_rst_assert(struct tegra_pcie_dw *pcie)
1683 {
1684 u32 val;
1685 int ret;
1686
1687 if (pcie->ep_state == EP_STATE_DISABLED)
1688 return;
1689
1690 /* Disable LTSSM */
1691 val = appl_readl(pcie, APPL_CTRL);
1692 val &= ~APPL_CTRL_LTSSM_EN;
1693 appl_writel(pcie, val, APPL_CTRL);
1694
1695 ret = readl_poll_timeout(pcie->appl_base + APPL_DEBUG, val,
1696 ((val & APPL_DEBUG_LTSSM_STATE_MASK) >>
1697 APPL_DEBUG_LTSSM_STATE_SHIFT) ==
1698 LTSSM_STATE_PRE_DETECT,
1699 1, LTSSM_TIMEOUT);
1700 if (ret)
1701 dev_err(pcie->dev, "Failed to go Detect state: %d\n", ret);
1702
1703 reset_control_assert(pcie->core_rst);
1704
1705 tegra_pcie_disable_phy(pcie);
1706
1707 reset_control_assert(pcie->core_apb_rst);
1708
1709 clk_disable_unprepare(pcie->core_clk);
1710
1711 pm_runtime_put_sync(pcie->dev);
1712
1713 if (pcie->enable_ext_refclk) {
1714 ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1715 if (ret)
1716 dev_err(pcie->dev, "Failed to turn off UPHY: %d\n",
1717 ret);
1718 }
1719
1720 ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1721 if (ret)
1722 dev_err(pcie->dev, "Failed to turn off UPHY: %d\n", ret);
1723
1724 pcie->ep_state = EP_STATE_DISABLED;
1725 dev_dbg(pcie->dev, "Uninitialization of endpoint is completed\n");
1726 }
1727
pex_ep_event_pex_rst_deassert(struct tegra_pcie_dw * pcie)1728 static void pex_ep_event_pex_rst_deassert(struct tegra_pcie_dw *pcie)
1729 {
1730 struct dw_pcie *pci = &pcie->pci;
1731 struct dw_pcie_ep *ep = &pci->ep;
1732 struct device *dev = pcie->dev;
1733 u32 val;
1734 int ret;
1735 u16 val_16;
1736
1737 if (pcie->ep_state == EP_STATE_ENABLED)
1738 return;
1739
1740 ret = pm_runtime_resume_and_get(dev);
1741 if (ret < 0) {
1742 dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n",
1743 ret);
1744 return;
1745 }
1746
1747 ret = tegra_pcie_bpmp_set_ctrl_state(pcie, true);
1748 if (ret) {
1749 dev_err(pcie->dev, "Failed to enable controller %u: %d\n",
1750 pcie->cid, ret);
1751 goto fail_set_ctrl_state;
1752 }
1753
1754 if (pcie->enable_ext_refclk) {
1755 ret = tegra_pcie_bpmp_set_pll_state(pcie, true);
1756 if (ret) {
1757 dev_err(dev, "Failed to init UPHY for PCIe EP: %d\n",
1758 ret);
1759 goto fail_pll_init;
1760 }
1761 }
1762
1763 ret = clk_prepare_enable(pcie->core_clk);
1764 if (ret) {
1765 dev_err(dev, "Failed to enable core clock: %d\n", ret);
1766 goto fail_core_clk_enable;
1767 }
1768
1769 ret = reset_control_deassert(pcie->core_apb_rst);
1770 if (ret) {
1771 dev_err(dev, "Failed to deassert core APB reset: %d\n", ret);
1772 goto fail_core_apb_rst;
1773 }
1774
1775 ret = tegra_pcie_enable_phy(pcie);
1776 if (ret) {
1777 dev_err(dev, "Failed to enable PHY: %d\n", ret);
1778 goto fail_phy;
1779 }
1780
1781 /* Clear any stale interrupt statuses */
1782 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
1783 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
1784 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
1785 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
1786 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
1787 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
1788 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
1789 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
1790 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
1791 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
1792 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
1793 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
1794 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
1795 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
1796 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
1797
1798 /* configure this core for EP mode operation */
1799 val = appl_readl(pcie, APPL_DM_TYPE);
1800 val &= ~APPL_DM_TYPE_MASK;
1801 val |= APPL_DM_TYPE_EP;
1802 appl_writel(pcie, val, APPL_DM_TYPE);
1803
1804 appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE);
1805
1806 val = appl_readl(pcie, APPL_CTRL);
1807 val |= APPL_CTRL_SYS_PRE_DET_STATE;
1808 val |= APPL_CTRL_HW_HOT_RST_EN;
1809 appl_writel(pcie, val, APPL_CTRL);
1810
1811 val = appl_readl(pcie, APPL_CFG_MISC);
1812 val |= APPL_CFG_MISC_SLV_EP_MODE;
1813 val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT);
1814 appl_writel(pcie, val, APPL_CFG_MISC);
1815
1816 val = appl_readl(pcie, APPL_PINMUX);
1817 val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1818 val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1819 appl_writel(pcie, val, APPL_PINMUX);
1820
1821 appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK,
1822 APPL_CFG_BASE_ADDR);
1823
1824 appl_writel(pcie, pcie->atu_dma_res->start &
1825 APPL_CFG_IATU_DMA_BASE_ADDR_MASK,
1826 APPL_CFG_IATU_DMA_BASE_ADDR);
1827
1828 val = appl_readl(pcie, APPL_INTR_EN_L0_0);
1829 val |= APPL_INTR_EN_L0_0_SYS_INTR_EN;
1830 val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN;
1831 val |= APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN;
1832 appl_writel(pcie, val, APPL_INTR_EN_L0_0);
1833
1834 val = appl_readl(pcie, APPL_INTR_EN_L1_0_0);
1835 val |= APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN;
1836 val |= APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN;
1837 appl_writel(pcie, val, APPL_INTR_EN_L1_0_0);
1838
1839 reset_control_deassert(pcie->core_rst);
1840
1841 if (pcie->update_fc_fixup) {
1842 val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF);
1843 val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT;
1844 dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val);
1845 }
1846
1847 config_gen3_gen4_eq_presets(pcie);
1848
1849 init_host_aspm(pcie);
1850
1851 /* Disable ASPM-L1SS advertisement if there is no CLKREQ routing */
1852 if (!pcie->supports_clkreq) {
1853 disable_aspm_l11(pcie);
1854 disable_aspm_l12(pcie);
1855 }
1856
1857 if (!pcie->of_data->has_l1ss_exit_fix) {
1858 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
1859 val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL;
1860 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
1861 }
1862
1863 pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci,
1864 PCI_CAP_ID_EXP);
1865
1866 val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL);
1867 val_16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
1868 val_16 |= PCI_EXP_DEVCTL_PAYLOAD_256B;
1869 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL, val_16);
1870
1871 /* Clear Slot Clock Configuration bit if SRNS configuration */
1872 if (pcie->enable_srns) {
1873 val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
1874 PCI_EXP_LNKSTA);
1875 val_16 &= ~PCI_EXP_LNKSTA_SLC;
1876 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA,
1877 val_16);
1878 }
1879
1880 clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ);
1881
1882 val = (ep->msi_mem_phys & MSIX_ADDR_MATCH_LOW_OFF_MASK);
1883 val |= MSIX_ADDR_MATCH_LOW_OFF_EN;
1884 dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_LOW_OFF, val);
1885 val = (upper_32_bits(ep->msi_mem_phys) & MSIX_ADDR_MATCH_HIGH_OFF_MASK);
1886 dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_HIGH_OFF, val);
1887
1888 ret = dw_pcie_ep_init_complete(ep);
1889 if (ret) {
1890 dev_err(dev, "Failed to complete initialization: %d\n", ret);
1891 goto fail_init_complete;
1892 }
1893
1894 dw_pcie_ep_init_notify(ep);
1895
1896 /* Program the private control to allow sending LTR upstream */
1897 if (pcie->of_data->has_ltr_req_fix) {
1898 val = appl_readl(pcie, APPL_LTR_MSG_2);
1899 val |= APPL_LTR_MSG_2_LTR_MSG_REQ_STATE;
1900 appl_writel(pcie, val, APPL_LTR_MSG_2);
1901 }
1902
1903 /* Enable LTSSM */
1904 val = appl_readl(pcie, APPL_CTRL);
1905 val |= APPL_CTRL_LTSSM_EN;
1906 appl_writel(pcie, val, APPL_CTRL);
1907
1908 pcie->ep_state = EP_STATE_ENABLED;
1909 dev_dbg(dev, "Initialization of endpoint is completed\n");
1910
1911 return;
1912
1913 fail_init_complete:
1914 reset_control_assert(pcie->core_rst);
1915 tegra_pcie_disable_phy(pcie);
1916 fail_phy:
1917 reset_control_assert(pcie->core_apb_rst);
1918 fail_core_apb_rst:
1919 clk_disable_unprepare(pcie->core_clk);
1920 fail_core_clk_enable:
1921 tegra_pcie_bpmp_set_pll_state(pcie, false);
1922 fail_pll_init:
1923 tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1924 fail_set_ctrl_state:
1925 pm_runtime_put_sync(dev);
1926 }
1927
tegra_pcie_ep_pex_rst_irq(int irq,void * arg)1928 static irqreturn_t tegra_pcie_ep_pex_rst_irq(int irq, void *arg)
1929 {
1930 struct tegra_pcie_dw *pcie = arg;
1931
1932 if (gpiod_get_value(pcie->pex_rst_gpiod))
1933 pex_ep_event_pex_rst_assert(pcie);
1934 else
1935 pex_ep_event_pex_rst_deassert(pcie);
1936
1937 return IRQ_HANDLED;
1938 }
1939
tegra_pcie_ep_raise_legacy_irq(struct tegra_pcie_dw * pcie,u16 irq)1940 static int tegra_pcie_ep_raise_legacy_irq(struct tegra_pcie_dw *pcie, u16 irq)
1941 {
1942 /* Tegra194 supports only INTA */
1943 if (irq > 1)
1944 return -EINVAL;
1945
1946 appl_writel(pcie, 1, APPL_LEGACY_INTX);
1947 usleep_range(1000, 2000);
1948 appl_writel(pcie, 0, APPL_LEGACY_INTX);
1949 return 0;
1950 }
1951
tegra_pcie_ep_raise_msi_irq(struct tegra_pcie_dw * pcie,u16 irq)1952 static int tegra_pcie_ep_raise_msi_irq(struct tegra_pcie_dw *pcie, u16 irq)
1953 {
1954 if (unlikely(irq > 31))
1955 return -EINVAL;
1956
1957 appl_writel(pcie, BIT(irq), APPL_MSI_CTRL_1);
1958
1959 return 0;
1960 }
1961
tegra_pcie_ep_raise_msix_irq(struct tegra_pcie_dw * pcie,u16 irq)1962 static int tegra_pcie_ep_raise_msix_irq(struct tegra_pcie_dw *pcie, u16 irq)
1963 {
1964 struct dw_pcie_ep *ep = &pcie->pci.ep;
1965
1966 writel(irq, ep->msi_mem);
1967
1968 return 0;
1969 }
1970
tegra_pcie_ep_raise_irq(struct dw_pcie_ep * ep,u8 func_no,enum pci_epc_irq_type type,u16 interrupt_num)1971 static int tegra_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
1972 enum pci_epc_irq_type type,
1973 u16 interrupt_num)
1974 {
1975 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1976 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1977
1978 switch (type) {
1979 case PCI_EPC_IRQ_LEGACY:
1980 return tegra_pcie_ep_raise_legacy_irq(pcie, interrupt_num);
1981
1982 case PCI_EPC_IRQ_MSI:
1983 return tegra_pcie_ep_raise_msi_irq(pcie, interrupt_num);
1984
1985 case PCI_EPC_IRQ_MSIX:
1986 return tegra_pcie_ep_raise_msix_irq(pcie, interrupt_num);
1987
1988 default:
1989 dev_err(pci->dev, "Unknown IRQ type\n");
1990 return -EPERM;
1991 }
1992
1993 return 0;
1994 }
1995
1996 static const struct pci_epc_features tegra_pcie_epc_features = {
1997 .linkup_notifier = true,
1998 .core_init_notifier = true,
1999 .msi_capable = false,
2000 .msix_capable = false,
2001 .reserved_bar = 1 << BAR_2 | 1 << BAR_3 | 1 << BAR_4 | 1 << BAR_5,
2002 .bar_fixed_64bit = 1 << BAR_0,
2003 .bar_fixed_size[0] = SZ_1M,
2004 };
2005
2006 static const struct pci_epc_features*
tegra_pcie_ep_get_features(struct dw_pcie_ep * ep)2007 tegra_pcie_ep_get_features(struct dw_pcie_ep *ep)
2008 {
2009 return &tegra_pcie_epc_features;
2010 }
2011
2012 static const struct dw_pcie_ep_ops pcie_ep_ops = {
2013 .raise_irq = tegra_pcie_ep_raise_irq,
2014 .get_features = tegra_pcie_ep_get_features,
2015 };
2016
tegra_pcie_config_ep(struct tegra_pcie_dw * pcie,struct platform_device * pdev)2017 static int tegra_pcie_config_ep(struct tegra_pcie_dw *pcie,
2018 struct platform_device *pdev)
2019 {
2020 struct dw_pcie *pci = &pcie->pci;
2021 struct device *dev = pcie->dev;
2022 struct dw_pcie_ep *ep;
2023 char *name;
2024 int ret;
2025
2026 ep = &pci->ep;
2027 ep->ops = &pcie_ep_ops;
2028
2029 ep->page_size = SZ_64K;
2030
2031 ret = gpiod_set_debounce(pcie->pex_rst_gpiod, PERST_DEBOUNCE_TIME);
2032 if (ret < 0) {
2033 dev_err(dev, "Failed to set PERST GPIO debounce time: %d\n",
2034 ret);
2035 return ret;
2036 }
2037
2038 ret = gpiod_to_irq(pcie->pex_rst_gpiod);
2039 if (ret < 0) {
2040 dev_err(dev, "Failed to get IRQ for PERST GPIO: %d\n", ret);
2041 return ret;
2042 }
2043 pcie->pex_rst_irq = (unsigned int)ret;
2044
2045 name = devm_kasprintf(dev, GFP_KERNEL, "tegra_pcie_%u_pex_rst_irq",
2046 pcie->cid);
2047 if (!name) {
2048 dev_err(dev, "Failed to create PERST IRQ string\n");
2049 return -ENOMEM;
2050 }
2051
2052 irq_set_status_flags(pcie->pex_rst_irq, IRQ_NOAUTOEN);
2053
2054 pcie->ep_state = EP_STATE_DISABLED;
2055
2056 ret = devm_request_threaded_irq(dev, pcie->pex_rst_irq, NULL,
2057 tegra_pcie_ep_pex_rst_irq,
2058 IRQF_TRIGGER_RISING |
2059 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2060 name, (void *)pcie);
2061 if (ret < 0) {
2062 dev_err(dev, "Failed to request IRQ for PERST: %d\n", ret);
2063 return ret;
2064 }
2065
2066 pm_runtime_enable(dev);
2067
2068 ret = dw_pcie_ep_init(ep);
2069 if (ret) {
2070 dev_err(dev, "Failed to initialize DWC Endpoint subsystem: %d\n",
2071 ret);
2072 pm_runtime_disable(dev);
2073 return ret;
2074 }
2075
2076 return 0;
2077 }
2078
tegra_pcie_dw_probe(struct platform_device * pdev)2079 static int tegra_pcie_dw_probe(struct platform_device *pdev)
2080 {
2081 const struct tegra_pcie_dw_of_data *data;
2082 struct device *dev = &pdev->dev;
2083 struct resource *atu_dma_res;
2084 struct tegra_pcie_dw *pcie;
2085 struct dw_pcie_rp *pp;
2086 struct dw_pcie *pci;
2087 struct phy **phys;
2088 char *name;
2089 int ret;
2090 u32 i;
2091
2092 data = of_device_get_match_data(dev);
2093
2094 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
2095 if (!pcie)
2096 return -ENOMEM;
2097
2098 pci = &pcie->pci;
2099 pci->dev = &pdev->dev;
2100 pci->ops = &tegra_dw_pcie_ops;
2101 pcie->dev = &pdev->dev;
2102 pcie->of_data = (struct tegra_pcie_dw_of_data *)data;
2103 pci->n_fts[0] = pcie->of_data->n_fts[0];
2104 pci->n_fts[1] = pcie->of_data->n_fts[1];
2105 pp = &pci->pp;
2106 pp->num_vectors = MAX_MSI_IRQS;
2107
2108 ret = tegra_pcie_dw_parse_dt(pcie);
2109 if (ret < 0) {
2110 const char *level = KERN_ERR;
2111
2112 if (ret == -EPROBE_DEFER)
2113 level = KERN_DEBUG;
2114
2115 dev_printk(level, dev,
2116 dev_fmt("Failed to parse device tree: %d\n"),
2117 ret);
2118 return ret;
2119 }
2120
2121 ret = tegra_pcie_get_slot_regulators(pcie);
2122 if (ret < 0) {
2123 const char *level = KERN_ERR;
2124
2125 if (ret == -EPROBE_DEFER)
2126 level = KERN_DEBUG;
2127
2128 dev_printk(level, dev,
2129 dev_fmt("Failed to get slot regulators: %d\n"),
2130 ret);
2131 return ret;
2132 }
2133
2134 if (pcie->pex_refclk_sel_gpiod)
2135 gpiod_set_value(pcie->pex_refclk_sel_gpiod, 1);
2136
2137 pcie->pex_ctl_supply = devm_regulator_get(dev, "vddio-pex-ctl");
2138 if (IS_ERR(pcie->pex_ctl_supply)) {
2139 ret = PTR_ERR(pcie->pex_ctl_supply);
2140 if (ret != -EPROBE_DEFER)
2141 dev_err(dev, "Failed to get regulator: %ld\n",
2142 PTR_ERR(pcie->pex_ctl_supply));
2143 return ret;
2144 }
2145
2146 pcie->core_clk = devm_clk_get(dev, "core");
2147 if (IS_ERR(pcie->core_clk)) {
2148 dev_err(dev, "Failed to get core clock: %ld\n",
2149 PTR_ERR(pcie->core_clk));
2150 return PTR_ERR(pcie->core_clk);
2151 }
2152
2153 pcie->appl_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2154 "appl");
2155 if (!pcie->appl_res) {
2156 dev_err(dev, "Failed to find \"appl\" region\n");
2157 return -ENODEV;
2158 }
2159
2160 pcie->appl_base = devm_ioremap_resource(dev, pcie->appl_res);
2161 if (IS_ERR(pcie->appl_base))
2162 return PTR_ERR(pcie->appl_base);
2163
2164 pcie->core_apb_rst = devm_reset_control_get(dev, "apb");
2165 if (IS_ERR(pcie->core_apb_rst)) {
2166 dev_err(dev, "Failed to get APB reset: %ld\n",
2167 PTR_ERR(pcie->core_apb_rst));
2168 return PTR_ERR(pcie->core_apb_rst);
2169 }
2170
2171 phys = devm_kcalloc(dev, pcie->phy_count, sizeof(*phys), GFP_KERNEL);
2172 if (!phys)
2173 return -ENOMEM;
2174
2175 for (i = 0; i < pcie->phy_count; i++) {
2176 name = kasprintf(GFP_KERNEL, "p2u-%u", i);
2177 if (!name) {
2178 dev_err(dev, "Failed to create P2U string\n");
2179 return -ENOMEM;
2180 }
2181 phys[i] = devm_phy_get(dev, name);
2182 kfree(name);
2183 if (IS_ERR(phys[i])) {
2184 ret = PTR_ERR(phys[i]);
2185 if (ret != -EPROBE_DEFER)
2186 dev_err(dev, "Failed to get PHY: %d\n", ret);
2187 return ret;
2188 }
2189 }
2190
2191 pcie->phys = phys;
2192
2193 atu_dma_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2194 "atu_dma");
2195 if (!atu_dma_res) {
2196 dev_err(dev, "Failed to find \"atu_dma\" region\n");
2197 return -ENODEV;
2198 }
2199 pcie->atu_dma_res = atu_dma_res;
2200
2201 pci->atu_size = resource_size(atu_dma_res);
2202 pci->atu_base = devm_ioremap_resource(dev, atu_dma_res);
2203 if (IS_ERR(pci->atu_base))
2204 return PTR_ERR(pci->atu_base);
2205
2206 pcie->core_rst = devm_reset_control_get(dev, "core");
2207 if (IS_ERR(pcie->core_rst)) {
2208 dev_err(dev, "Failed to get core reset: %ld\n",
2209 PTR_ERR(pcie->core_rst));
2210 return PTR_ERR(pcie->core_rst);
2211 }
2212
2213 pp->irq = platform_get_irq_byname(pdev, "intr");
2214 if (pp->irq < 0)
2215 return pp->irq;
2216
2217 pcie->bpmp = tegra_bpmp_get(dev);
2218 if (IS_ERR(pcie->bpmp))
2219 return PTR_ERR(pcie->bpmp);
2220
2221 platform_set_drvdata(pdev, pcie);
2222
2223 switch (pcie->of_data->mode) {
2224 case DW_PCIE_RC_TYPE:
2225 ret = devm_request_irq(dev, pp->irq, tegra_pcie_rp_irq_handler,
2226 IRQF_SHARED, "tegra-pcie-intr", pcie);
2227 if (ret) {
2228 dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq,
2229 ret);
2230 goto fail;
2231 }
2232
2233 ret = tegra_pcie_config_rp(pcie);
2234 if (ret && ret != -ENOMEDIUM)
2235 goto fail;
2236 else
2237 return 0;
2238 break;
2239
2240 case DW_PCIE_EP_TYPE:
2241 ret = devm_request_threaded_irq(dev, pp->irq,
2242 tegra_pcie_ep_hard_irq,
2243 tegra_pcie_ep_irq_thread,
2244 IRQF_SHARED | IRQF_ONESHOT,
2245 "tegra-pcie-ep-intr", pcie);
2246 if (ret) {
2247 dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq,
2248 ret);
2249 goto fail;
2250 }
2251
2252 ret = tegra_pcie_config_ep(pcie, pdev);
2253 if (ret < 0)
2254 goto fail;
2255 break;
2256
2257 default:
2258 dev_err(dev, "Invalid PCIe device type %d\n",
2259 pcie->of_data->mode);
2260 }
2261
2262 fail:
2263 tegra_bpmp_put(pcie->bpmp);
2264 return ret;
2265 }
2266
tegra_pcie_dw_remove(struct platform_device * pdev)2267 static int tegra_pcie_dw_remove(struct platform_device *pdev)
2268 {
2269 struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev);
2270
2271 if (pcie->of_data->mode == DW_PCIE_RC_TYPE) {
2272 if (!pcie->link_state)
2273 return 0;
2274
2275 debugfs_remove_recursive(pcie->debugfs);
2276 tegra_pcie_deinit_controller(pcie);
2277 pm_runtime_put_sync(pcie->dev);
2278 } else {
2279 disable_irq(pcie->pex_rst_irq);
2280 pex_ep_event_pex_rst_assert(pcie);
2281 }
2282
2283 pm_runtime_disable(pcie->dev);
2284 tegra_bpmp_put(pcie->bpmp);
2285 if (pcie->pex_refclk_sel_gpiod)
2286 gpiod_set_value(pcie->pex_refclk_sel_gpiod, 0);
2287
2288 return 0;
2289 }
2290
tegra_pcie_dw_suspend_late(struct device * dev)2291 static int tegra_pcie_dw_suspend_late(struct device *dev)
2292 {
2293 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2294 u32 val;
2295
2296 if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
2297 dev_err(dev, "Failed to Suspend as Tegra PCIe is in EP mode\n");
2298 return -EPERM;
2299 }
2300
2301 if (!pcie->link_state)
2302 return 0;
2303
2304 /* Enable HW_HOT_RST mode */
2305 if (!pcie->of_data->has_sbr_reset_fix) {
2306 val = appl_readl(pcie, APPL_CTRL);
2307 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
2308 APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
2309 val |= APPL_CTRL_HW_HOT_RST_EN;
2310 appl_writel(pcie, val, APPL_CTRL);
2311 }
2312
2313 return 0;
2314 }
2315
tegra_pcie_dw_suspend_noirq(struct device * dev)2316 static int tegra_pcie_dw_suspend_noirq(struct device *dev)
2317 {
2318 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2319
2320 if (!pcie->link_state)
2321 return 0;
2322
2323 tegra_pcie_downstream_dev_to_D0(pcie);
2324 tegra_pcie_dw_pme_turnoff(pcie);
2325 tegra_pcie_unconfig_controller(pcie);
2326
2327 return 0;
2328 }
2329
tegra_pcie_dw_resume_noirq(struct device * dev)2330 static int tegra_pcie_dw_resume_noirq(struct device *dev)
2331 {
2332 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2333 int ret;
2334
2335 if (!pcie->link_state)
2336 return 0;
2337
2338 ret = tegra_pcie_config_controller(pcie, true);
2339 if (ret < 0)
2340 return ret;
2341
2342 ret = tegra_pcie_dw_host_init(&pcie->pci.pp);
2343 if (ret < 0) {
2344 dev_err(dev, "Failed to init host: %d\n", ret);
2345 goto fail_host_init;
2346 }
2347
2348 dw_pcie_setup_rc(&pcie->pci.pp);
2349
2350 ret = tegra_pcie_dw_start_link(&pcie->pci);
2351 if (ret < 0)
2352 goto fail_host_init;
2353
2354 return 0;
2355
2356 fail_host_init:
2357 tegra_pcie_unconfig_controller(pcie);
2358 return ret;
2359 }
2360
tegra_pcie_dw_resume_early(struct device * dev)2361 static int tegra_pcie_dw_resume_early(struct device *dev)
2362 {
2363 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2364 u32 val;
2365
2366 if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
2367 dev_err(dev, "Suspend is not supported in EP mode");
2368 return -ENOTSUPP;
2369 }
2370
2371 if (!pcie->link_state)
2372 return 0;
2373
2374 /* Disable HW_HOT_RST mode */
2375 if (!pcie->of_data->has_sbr_reset_fix) {
2376 val = appl_readl(pcie, APPL_CTRL);
2377 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
2378 APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
2379 val |= APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST <<
2380 APPL_CTRL_HW_HOT_RST_MODE_SHIFT;
2381 val &= ~APPL_CTRL_HW_HOT_RST_EN;
2382 appl_writel(pcie, val, APPL_CTRL);
2383 }
2384
2385 return 0;
2386 }
2387
tegra_pcie_dw_shutdown(struct platform_device * pdev)2388 static void tegra_pcie_dw_shutdown(struct platform_device *pdev)
2389 {
2390 struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev);
2391
2392 if (pcie->of_data->mode == DW_PCIE_RC_TYPE) {
2393 if (!pcie->link_state)
2394 return;
2395
2396 debugfs_remove_recursive(pcie->debugfs);
2397 tegra_pcie_downstream_dev_to_D0(pcie);
2398
2399 disable_irq(pcie->pci.pp.irq);
2400 if (IS_ENABLED(CONFIG_PCI_MSI))
2401 disable_irq(pcie->pci.pp.msi_irq[0]);
2402
2403 tegra_pcie_dw_pme_turnoff(pcie);
2404 tegra_pcie_unconfig_controller(pcie);
2405 pm_runtime_put_sync(pcie->dev);
2406 } else {
2407 disable_irq(pcie->pex_rst_irq);
2408 pex_ep_event_pex_rst_assert(pcie);
2409 }
2410 }
2411
2412 static const struct tegra_pcie_dw_of_data tegra194_pcie_dw_rc_of_data = {
2413 .version = TEGRA194_DWC_IP_VER,
2414 .mode = DW_PCIE_RC_TYPE,
2415 .cdm_chk_int_en_bit = BIT(19),
2416 /* Gen4 - 5, 6, 8 and 9 presets enabled */
2417 .gen4_preset_vec = 0x360,
2418 .n_fts = { 52, 52 },
2419 };
2420
2421 static const struct tegra_pcie_dw_of_data tegra194_pcie_dw_ep_of_data = {
2422 .version = TEGRA194_DWC_IP_VER,
2423 .mode = DW_PCIE_EP_TYPE,
2424 .cdm_chk_int_en_bit = BIT(19),
2425 /* Gen4 - 5, 6, 8 and 9 presets enabled */
2426 .gen4_preset_vec = 0x360,
2427 .n_fts = { 52, 52 },
2428 };
2429
2430 static const struct tegra_pcie_dw_of_data tegra234_pcie_dw_rc_of_data = {
2431 .version = TEGRA234_DWC_IP_VER,
2432 .mode = DW_PCIE_RC_TYPE,
2433 .has_msix_doorbell_access_fix = true,
2434 .has_sbr_reset_fix = true,
2435 .has_l1ss_exit_fix = true,
2436 .cdm_chk_int_en_bit = BIT(18),
2437 /* Gen4 - 6, 8 and 9 presets enabled */
2438 .gen4_preset_vec = 0x340,
2439 .n_fts = { 52, 80 },
2440 };
2441
2442 static const struct tegra_pcie_dw_of_data tegra234_pcie_dw_ep_of_data = {
2443 .version = TEGRA234_DWC_IP_VER,
2444 .mode = DW_PCIE_EP_TYPE,
2445 .has_l1ss_exit_fix = true,
2446 .has_ltr_req_fix = true,
2447 .cdm_chk_int_en_bit = BIT(18),
2448 /* Gen4 - 6, 8 and 9 presets enabled */
2449 .gen4_preset_vec = 0x340,
2450 .n_fts = { 52, 80 },
2451 };
2452
2453 static const struct of_device_id tegra_pcie_dw_of_match[] = {
2454 {
2455 .compatible = "nvidia,tegra194-pcie",
2456 .data = &tegra194_pcie_dw_rc_of_data,
2457 },
2458 {
2459 .compatible = "nvidia,tegra194-pcie-ep",
2460 .data = &tegra194_pcie_dw_ep_of_data,
2461 },
2462 {
2463 .compatible = "nvidia,tegra234-pcie",
2464 .data = &tegra234_pcie_dw_rc_of_data,
2465 },
2466 {
2467 .compatible = "nvidia,tegra234-pcie-ep",
2468 .data = &tegra234_pcie_dw_ep_of_data,
2469 },
2470 {}
2471 };
2472
2473 static const struct dev_pm_ops tegra_pcie_dw_pm_ops = {
2474 .suspend_late = tegra_pcie_dw_suspend_late,
2475 .suspend_noirq = tegra_pcie_dw_suspend_noirq,
2476 .resume_noirq = tegra_pcie_dw_resume_noirq,
2477 .resume_early = tegra_pcie_dw_resume_early,
2478 };
2479
2480 static struct platform_driver tegra_pcie_dw_driver = {
2481 .probe = tegra_pcie_dw_probe,
2482 .remove = tegra_pcie_dw_remove,
2483 .shutdown = tegra_pcie_dw_shutdown,
2484 .driver = {
2485 .name = "tegra194-pcie",
2486 .pm = &tegra_pcie_dw_pm_ops,
2487 .of_match_table = tegra_pcie_dw_of_match,
2488 },
2489 };
2490 module_platform_driver(tegra_pcie_dw_driver);
2491
2492 MODULE_DEVICE_TABLE(of, tegra_pcie_dw_of_match);
2493
2494 MODULE_AUTHOR("Vidya Sagar <vidyas@nvidia.com>");
2495 MODULE_DESCRIPTION("NVIDIA PCIe host controller driver");
2496 MODULE_LICENSE("GPL v2");
2497