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