Debian Codegraph @

drivers/pci/controller/dwc/pcie-tegra194.c

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