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