xref: /linux/drivers/pci/controller/pci-tegra.c (revision 51c2db3ef0cb89a49ab7f47a40efd5d5b074ce8d)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCIe host controller driver for Tegra SoCs
4  *
5  * Copyright (c) 2010, CompuLab, Ltd.
6  * Author: Mike Rapoport <mike@compulab.co.il>
7  *
8  * Based on NVIDIA PCIe driver
9  * Copyright (c) 2008-2009, NVIDIA Corporation.
10  *
11  * Bits taken from arch/arm/mach-dove/pcie.c
12  *
13  * Author: Thierry Reding <treding@nvidia.com>
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/cleanup.h>
18 #include <linux/debugfs.h>
19 #include <linux/delay.h>
20 #include <linux/export.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/interrupt.h>
23 #include <linux/iopoll.h>
24 #include <linux/irq.h>
25 #include <linux/irqchip/chained_irq.h>
26 #include <linux/irqchip/irq-msi-lib.h>
27 #include <linux/irqdomain.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/msi.h>
32 #include <linux/of_address.h>
33 #include <linux/of_pci.h>
34 #include <linux/of_platform.h>
35 #include <linux/pci.h>
36 #include <linux/phy/phy.h>
37 #include <linux/pinctrl/consumer.h>
38 #include <linux/platform_device.h>
39 #include <linux/reset.h>
40 #include <linux/sizes.h>
41 #include <linux/slab.h>
42 #include <linux/vmalloc.h>
43 #include <linux/regulator/consumer.h>
44 
45 #include <soc/tegra/cpuidle.h>
46 #include <soc/tegra/pmc.h>
47 
48 #include "../pci.h"
49 
50 #define INT_PCI_MSI_NR (8 * 32)
51 
52 /* register definitions */
53 
54 #define AFI_AXI_BAR0_SZ	0x00
55 #define AFI_AXI_BAR1_SZ	0x04
56 #define AFI_AXI_BAR2_SZ	0x08
57 #define AFI_AXI_BAR3_SZ	0x0c
58 #define AFI_AXI_BAR4_SZ	0x10
59 #define AFI_AXI_BAR5_SZ	0x14
60 
61 #define AFI_AXI_BAR0_START	0x18
62 #define AFI_AXI_BAR1_START	0x1c
63 #define AFI_AXI_BAR2_START	0x20
64 #define AFI_AXI_BAR3_START	0x24
65 #define AFI_AXI_BAR4_START	0x28
66 #define AFI_AXI_BAR5_START	0x2c
67 
68 #define AFI_FPCI_BAR0	0x30
69 #define AFI_FPCI_BAR1	0x34
70 #define AFI_FPCI_BAR2	0x38
71 #define AFI_FPCI_BAR3	0x3c
72 #define AFI_FPCI_BAR4	0x40
73 #define AFI_FPCI_BAR5	0x44
74 
75 #define AFI_CACHE_BAR0_SZ	0x48
76 #define AFI_CACHE_BAR0_ST	0x4c
77 #define AFI_CACHE_BAR1_SZ	0x50
78 #define AFI_CACHE_BAR1_ST	0x54
79 
80 #define AFI_MSI_BAR_SZ		0x60
81 #define AFI_MSI_FPCI_BAR_ST	0x64
82 #define AFI_MSI_AXI_BAR_ST	0x68
83 
84 #define AFI_MSI_VEC(x)		(0x6c + ((x) * 4))
85 #define AFI_MSI_EN_VEC(x)	(0x8c + ((x) * 4))
86 
87 #define AFI_CONFIGURATION		0xac
88 #define  AFI_CONFIGURATION_EN_FPCI		(1 << 0)
89 #define  AFI_CONFIGURATION_CLKEN_OVERRIDE	(1 << 31)
90 
91 #define AFI_FPCI_ERROR_MASKS	0xb0
92 
93 #define AFI_INTR_MASK		0xb4
94 #define  AFI_INTR_MASK_INT_MASK	(1 << 0)
95 #define  AFI_INTR_MASK_MSI_MASK	(1 << 8)
96 
97 #define AFI_INTR_CODE			0xb8
98 #define  AFI_INTR_CODE_MASK		0xf
99 #define  AFI_INTR_INI_SLAVE_ERROR	1
100 #define  AFI_INTR_INI_DECODE_ERROR	2
101 #define  AFI_INTR_TARGET_ABORT		3
102 #define  AFI_INTR_MASTER_ABORT		4
103 #define  AFI_INTR_INVALID_WRITE		5
104 #define  AFI_INTR_LEGACY		6
105 #define  AFI_INTR_FPCI_DECODE_ERROR	7
106 #define  AFI_INTR_AXI_DECODE_ERROR	8
107 #define  AFI_INTR_FPCI_TIMEOUT		9
108 #define  AFI_INTR_PE_PRSNT_SENSE	10
109 #define  AFI_INTR_PE_CLKREQ_SENSE	11
110 #define  AFI_INTR_CLKCLAMP_SENSE	12
111 #define  AFI_INTR_RDY4PD_SENSE		13
112 #define  AFI_INTR_P2P_ERROR		14
113 
114 #define AFI_INTR_SIGNATURE	0xbc
115 #define AFI_UPPER_FPCI_ADDRESS	0xc0
116 #define AFI_SM_INTR_ENABLE	0xc4
117 #define  AFI_SM_INTR_INTA_ASSERT	(1 << 0)
118 #define  AFI_SM_INTR_INTB_ASSERT	(1 << 1)
119 #define  AFI_SM_INTR_INTC_ASSERT	(1 << 2)
120 #define  AFI_SM_INTR_INTD_ASSERT	(1 << 3)
121 #define  AFI_SM_INTR_INTA_DEASSERT	(1 << 4)
122 #define  AFI_SM_INTR_INTB_DEASSERT	(1 << 5)
123 #define  AFI_SM_INTR_INTC_DEASSERT	(1 << 6)
124 #define  AFI_SM_INTR_INTD_DEASSERT	(1 << 7)
125 
126 #define AFI_AFI_INTR_ENABLE		0xc8
127 #define  AFI_INTR_EN_INI_SLVERR		(1 << 0)
128 #define  AFI_INTR_EN_INI_DECERR		(1 << 1)
129 #define  AFI_INTR_EN_TGT_SLVERR		(1 << 2)
130 #define  AFI_INTR_EN_TGT_DECERR		(1 << 3)
131 #define  AFI_INTR_EN_TGT_WRERR		(1 << 4)
132 #define  AFI_INTR_EN_DFPCI_DECERR	(1 << 5)
133 #define  AFI_INTR_EN_AXI_DECERR		(1 << 6)
134 #define  AFI_INTR_EN_FPCI_TIMEOUT	(1 << 7)
135 #define  AFI_INTR_EN_PRSNT_SENSE	(1 << 8)
136 
137 #define AFI_PCIE_PME		0xf0
138 
139 #define AFI_PCIE_CONFIG					0x0f8
140 #define  AFI_PCIE_CONFIG_PCIE_DISABLE(x)		(1 << ((x) + 1))
141 #define  AFI_PCIE_CONFIG_PCIE_DISABLE_ALL		0xe
142 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK	(0xf << 20)
143 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE	(0x0 << 20)
144 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420	(0x0 << 20)
145 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1	(0x0 << 20)
146 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401	(0x0 << 20)
147 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL	(0x1 << 20)
148 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222	(0x1 << 20)
149 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1	(0x1 << 20)
150 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211	(0x1 << 20)
151 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411	(0x2 << 20)
152 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111	(0x2 << 20)
153 #define  AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(x)		(1 << ((x) + 29))
154 #define  AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO_ALL		(0x7 << 29)
155 
156 #define AFI_FUSE			0x104
157 #define  AFI_FUSE_PCIE_T0_GEN2_DIS	(1 << 2)
158 
159 #define AFI_PEX0_CTRL			0x110
160 #define AFI_PEX1_CTRL			0x118
161 #define  AFI_PEX_CTRL_RST		(1 << 0)
162 #define  AFI_PEX_CTRL_CLKREQ_EN		(1 << 1)
163 #define  AFI_PEX_CTRL_REFCLK_EN		(1 << 3)
164 #define  AFI_PEX_CTRL_OVERRIDE_EN	(1 << 4)
165 
166 #define AFI_PLLE_CONTROL		0x160
167 #define  AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
168 #define  AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
169 
170 #define AFI_PEXBIAS_CTRL_0		0x168
171 
172 #define RP_ECTL_2_R1	0x00000e84
173 #define  RP_ECTL_2_R1_RX_CTLE_1C_MASK		0xffff
174 
175 #define RP_ECTL_4_R1	0x00000e8c
176 #define  RP_ECTL_4_R1_RX_CDR_CTRL_1C_MASK	(0xffff << 16)
177 #define  RP_ECTL_4_R1_RX_CDR_CTRL_1C_SHIFT	16
178 
179 #define RP_ECTL_5_R1	0x00000e90
180 #define  RP_ECTL_5_R1_RX_EQ_CTRL_L_1C_MASK	0xffffffff
181 
182 #define RP_ECTL_6_R1	0x00000e94
183 #define  RP_ECTL_6_R1_RX_EQ_CTRL_H_1C_MASK	0xffffffff
184 
185 #define RP_ECTL_2_R2	0x00000ea4
186 #define  RP_ECTL_2_R2_RX_CTLE_1C_MASK	0xffff
187 
188 #define RP_ECTL_4_R2	0x00000eac
189 #define  RP_ECTL_4_R2_RX_CDR_CTRL_1C_MASK	(0xffff << 16)
190 #define  RP_ECTL_4_R2_RX_CDR_CTRL_1C_SHIFT	16
191 
192 #define RP_ECTL_5_R2	0x00000eb0
193 #define  RP_ECTL_5_R2_RX_EQ_CTRL_L_1C_MASK	0xffffffff
194 
195 #define RP_ECTL_6_R2	0x00000eb4
196 #define  RP_ECTL_6_R2_RX_EQ_CTRL_H_1C_MASK	0xffffffff
197 
198 #define RP_VEND_XP	0x00000f00
199 #define  RP_VEND_XP_DL_UP			(1 << 30)
200 #define  RP_VEND_XP_OPPORTUNISTIC_ACK		(1 << 27)
201 #define  RP_VEND_XP_OPPORTUNISTIC_UPDATEFC	(1 << 28)
202 #define  RP_VEND_XP_UPDATE_FC_THRESHOLD_MASK	(0xff << 18)
203 
204 #define RP_VEND_CTL0	0x00000f44
205 #define  RP_VEND_CTL0_DSK_RST_PULSE_WIDTH_MASK	(0xf << 12)
206 #define  RP_VEND_CTL0_DSK_RST_PULSE_WIDTH	(0x9 << 12)
207 
208 #define RP_VEND_CTL1	0x00000f48
209 #define  RP_VEND_CTL1_ERPT	(1 << 13)
210 
211 #define RP_VEND_XP_BIST	0x00000f4c
212 #define  RP_VEND_XP_BIST_GOTO_L1_L2_AFTER_DLLP_DONE	(1 << 28)
213 
214 #define RP_VEND_CTL2 0x00000fa8
215 #define  RP_VEND_CTL2_PCA_ENABLE (1 << 7)
216 
217 #define RP_PRIV_MISC	0x00000fe0
218 #define  RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT		(0xe << 0)
219 #define  RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT		(0xf << 0)
220 #define  RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD_MASK	(0x7f << 16)
221 #define  RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD		(0xf << 16)
222 #define  RP_PRIV_MISC_CTLR_CLK_CLAMP_ENABLE		(1 << 23)
223 #define  RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD_MASK	(0x7f << 24)
224 #define  RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD		(0xf << 24)
225 #define  RP_PRIV_MISC_TMS_CLK_CLAMP_ENABLE		(1 << 31)
226 
227 #define RP_LINK_CONTROL_STATUS			0x00000090
228 #define  RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE	0x20000000
229 #define  RP_LINK_CONTROL_STATUS_LINKSTAT_MASK	0x3fff0000
230 
231 #define RP_LINK_CONTROL_STATUS_2		0x000000b0
232 
233 #define PADS_CTL_SEL		0x0000009c
234 
235 #define PADS_CTL		0x000000a0
236 #define  PADS_CTL_IDDQ_1L	(1 << 0)
237 #define  PADS_CTL_TX_DATA_EN_1L	(1 << 6)
238 #define  PADS_CTL_RX_DATA_EN_1L	(1 << 10)
239 
240 #define PADS_PLL_CTL_TEGRA20			0x000000b8
241 #define PADS_PLL_CTL_TEGRA30			0x000000b4
242 #define  PADS_PLL_CTL_RST_B4SM			(1 << 1)
243 #define  PADS_PLL_CTL_LOCKDET			(1 << 8)
244 #define  PADS_PLL_CTL_REFCLK_MASK		(0x3 << 16)
245 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CML	(0 << 16)
246 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CMOS	(1 << 16)
247 #define  PADS_PLL_CTL_REFCLK_EXTERNAL		(2 << 16)
248 #define  PADS_PLL_CTL_TXCLKREF_MASK		(0x1 << 20)
249 #define  PADS_PLL_CTL_TXCLKREF_DIV10		(0 << 20)
250 #define  PADS_PLL_CTL_TXCLKREF_DIV5		(1 << 20)
251 #define  PADS_PLL_CTL_TXCLKREF_BUF_EN		(1 << 22)
252 
253 #define PADS_REFCLK_CFG0			0x000000c8
254 #define PADS_REFCLK_CFG1			0x000000cc
255 #define PADS_REFCLK_BIAS			0x000000d0
256 
257 /*
258  * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
259  * entries, one entry per PCIe port. These field definitions and desired
260  * values aren't in the TRM, but do come from NVIDIA.
261  */
262 #define PADS_REFCLK_CFG_TERM_SHIFT		2  /* 6:2 */
263 #define PADS_REFCLK_CFG_E_TERM_SHIFT		7
264 #define PADS_REFCLK_CFG_PREDI_SHIFT		8  /* 11:8 */
265 #define PADS_REFCLK_CFG_DRVI_SHIFT		12 /* 15:12 */
266 
267 #define PME_ACK_TIMEOUT 10000
268 #define LINK_RETRAIN_TIMEOUT 100000 /* in usec */
269 
270 struct tegra_msi {
271 	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
272 	struct irq_domain *domain;
273 	struct mutex map_lock;
274 	raw_spinlock_t mask_lock;
275 	void *virt;
276 	dma_addr_t phys;
277 	int irq;
278 };
279 
280 /* used to differentiate between Tegra SoC generations */
281 struct tegra_pcie_port_soc {
282 	struct {
283 		u8 turnoff_bit;
284 		u8 ack_bit;
285 	} pme;
286 };
287 
288 struct tegra_pcie_soc {
289 	unsigned int num_ports;
290 	const struct tegra_pcie_port_soc *ports;
291 	unsigned int msi_base_shift;
292 	unsigned long afi_pex2_ctrl;
293 	u32 pads_pll_ctl;
294 	u32 tx_ref_sel;
295 	u32 pads_refclk_cfg0;
296 	u32 pads_refclk_cfg1;
297 	u32 update_fc_threshold;
298 	bool has_pex_clkreq_en;
299 	bool has_pex_bias_ctrl;
300 	bool has_intr_prsnt_sense;
301 	bool has_cml_clk;
302 	bool has_gen2;
303 	bool force_pca_enable;
304 	bool program_uphy;
305 	bool update_clamp_threshold;
306 	bool program_deskew_time;
307 	bool update_fc_timer;
308 	bool has_cache_bars;
309 	struct {
310 		struct {
311 			u32 rp_ectl_2_r1;
312 			u32 rp_ectl_4_r1;
313 			u32 rp_ectl_5_r1;
314 			u32 rp_ectl_6_r1;
315 			u32 rp_ectl_2_r2;
316 			u32 rp_ectl_4_r2;
317 			u32 rp_ectl_5_r2;
318 			u32 rp_ectl_6_r2;
319 		} regs;
320 		bool enable;
321 	} ectl;
322 };
323 
324 struct tegra_pcie {
325 	struct device *dev;
326 
327 	void __iomem *pads;
328 	void __iomem *afi;
329 	void __iomem *cfg;
330 	int irq;
331 
332 	struct resource cs;
333 
334 	struct clk *pex_clk;
335 	struct clk *afi_clk;
336 	struct clk *pll_e;
337 	struct clk *cml_clk;
338 
339 	struct reset_control *pex_rst;
340 	struct reset_control *afi_rst;
341 	struct reset_control *pcie_xrst;
342 
343 	struct tegra_pmc *pmc;
344 
345 	bool legacy_phy;
346 	struct phy *phy;
347 
348 	struct tegra_msi msi;
349 
350 	struct list_head ports;
351 	u32 xbar_config;
352 
353 	struct regulator_bulk_data *supplies;
354 	unsigned int num_supplies;
355 
356 	const struct tegra_pcie_soc *soc;
357 	struct dentry *debugfs;
358 };
359 
360 static inline struct tegra_pcie *msi_to_pcie(struct tegra_msi *msi)
361 {
362 	return container_of(msi, struct tegra_pcie, msi);
363 }
364 
365 struct tegra_pcie_port {
366 	struct tegra_pcie *pcie;
367 	struct device_node *np;
368 	struct list_head list;
369 	struct resource regs;
370 	void __iomem *base;
371 	unsigned int index;
372 	unsigned int lanes;
373 
374 	struct phy **phys;
375 
376 	struct gpio_desc *reset_gpio;
377 };
378 
379 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
380 			      unsigned long offset)
381 {
382 	writel(value, pcie->afi + offset);
383 }
384 
385 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
386 {
387 	return readl(pcie->afi + offset);
388 }
389 
390 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
391 			       unsigned long offset)
392 {
393 	writel(value, pcie->pads + offset);
394 }
395 
396 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
397 {
398 	return readl(pcie->pads + offset);
399 }
400 
401 /*
402  * The configuration space mapping on Tegra is somewhat similar to the ECAM
403  * defined by PCIe. However it deviates a bit in how the 4 bits for extended
404  * register accesses are mapped:
405  *
406  *    [27:24] extended register number
407  *    [23:16] bus number
408  *    [15:11] device number
409  *    [10: 8] function number
410  *    [ 7: 0] register number
411  *
412  * Mapping the whole extended configuration space would require 256 MiB of
413  * virtual address space, only a small part of which will actually be used.
414  *
415  * To work around this, a 4 KiB region is used to generate the required
416  * configuration transaction with relevant B:D:F and register offset values.
417  * This is achieved by dynamically programming base address and size of
418  * AFI_AXI_BAR used for end point config space mapping to make sure that the
419  * address (access to which generates correct config transaction) falls in
420  * this 4 KiB region.
421  */
422 static unsigned int tegra_pcie_conf_offset(u8 bus, unsigned int devfn,
423 					   unsigned int where)
424 {
425 	return ((where & 0xf00) << 16) | (bus << 16) | (PCI_SLOT(devfn) << 11) |
426 	       (PCI_FUNC(devfn) << 8) | (where & 0xff);
427 }
428 
429 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
430 					unsigned int devfn,
431 					int where)
432 {
433 	struct tegra_pcie *pcie = bus->sysdata;
434 	void __iomem *addr = NULL;
435 
436 	if (bus->number == 0) {
437 		unsigned int slot = PCI_SLOT(devfn);
438 		struct tegra_pcie_port *port;
439 
440 		list_for_each_entry(port, &pcie->ports, list) {
441 			if (port->index + 1 == slot) {
442 				addr = port->base + (where & ~3);
443 				break;
444 			}
445 		}
446 	} else {
447 		unsigned int offset;
448 		u32 base;
449 
450 		offset = tegra_pcie_conf_offset(bus->number, devfn, where);
451 
452 		/* move 4 KiB window to offset within the FPCI region */
453 		base = 0xfe100000 + ((offset & ~(SZ_4K - 1)) >> 8);
454 		afi_writel(pcie, base, AFI_FPCI_BAR0);
455 
456 		/* move to correct offset within the 4 KiB page */
457 		addr = pcie->cfg + (offset & (SZ_4K - 1));
458 	}
459 
460 	return addr;
461 }
462 
463 static int tegra_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
464 				  int where, int size, u32 *value)
465 {
466 	if (bus->number == 0)
467 		return pci_generic_config_read32(bus, devfn, where, size,
468 						 value);
469 
470 	return pci_generic_config_read(bus, devfn, where, size, value);
471 }
472 
473 static int tegra_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
474 				   int where, int size, u32 value)
475 {
476 	if (bus->number == 0)
477 		return pci_generic_config_write32(bus, devfn, where, size,
478 						  value);
479 
480 	return pci_generic_config_write(bus, devfn, where, size, value);
481 }
482 
483 static struct pci_ops tegra_pcie_ops = {
484 	.map_bus = tegra_pcie_map_bus,
485 	.read = tegra_pcie_config_read,
486 	.write = tegra_pcie_config_write,
487 };
488 
489 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
490 {
491 	const struct tegra_pcie_soc *soc = port->pcie->soc;
492 	unsigned long ret = 0;
493 
494 	switch (port->index) {
495 	case 0:
496 		ret = AFI_PEX0_CTRL;
497 		break;
498 
499 	case 1:
500 		ret = AFI_PEX1_CTRL;
501 		break;
502 
503 	case 2:
504 		ret = soc->afi_pex2_ctrl;
505 		break;
506 	}
507 
508 	return ret;
509 }
510 
511 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
512 {
513 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
514 	unsigned long value;
515 
516 	/* pulse reset signal */
517 	if (port->reset_gpio) {
518 		gpiod_set_value(port->reset_gpio, 1);
519 	} else {
520 		value = afi_readl(port->pcie, ctrl);
521 		value &= ~AFI_PEX_CTRL_RST;
522 		afi_writel(port->pcie, value, ctrl);
523 	}
524 
525 	usleep_range(1000, 2000);
526 
527 	if (port->reset_gpio) {
528 		gpiod_set_value(port->reset_gpio, 0);
529 	} else {
530 		value = afi_readl(port->pcie, ctrl);
531 		value |= AFI_PEX_CTRL_RST;
532 		afi_writel(port->pcie, value, ctrl);
533 	}
534 }
535 
536 static void tegra_pcie_enable_rp_features(struct tegra_pcie_port *port)
537 {
538 	const struct tegra_pcie_soc *soc = port->pcie->soc;
539 	u32 value;
540 
541 	/* Enable AER capability */
542 	value = readl(port->base + RP_VEND_CTL1);
543 	value |= RP_VEND_CTL1_ERPT;
544 	writel(value, port->base + RP_VEND_CTL1);
545 
546 	/* Optimal settings to enhance bandwidth */
547 	value = readl(port->base + RP_VEND_XP);
548 	value |= RP_VEND_XP_OPPORTUNISTIC_ACK;
549 	value |= RP_VEND_XP_OPPORTUNISTIC_UPDATEFC;
550 	writel(value, port->base + RP_VEND_XP);
551 
552 	/*
553 	 * LTSSM will wait for DLLP to finish before entering L1 or L2,
554 	 * to avoid truncation of PM messages which results in receiver errors
555 	 */
556 	value = readl(port->base + RP_VEND_XP_BIST);
557 	value |= RP_VEND_XP_BIST_GOTO_L1_L2_AFTER_DLLP_DONE;
558 	writel(value, port->base + RP_VEND_XP_BIST);
559 
560 	value = readl(port->base + RP_PRIV_MISC);
561 	value |= RP_PRIV_MISC_CTLR_CLK_CLAMP_ENABLE;
562 	value |= RP_PRIV_MISC_TMS_CLK_CLAMP_ENABLE;
563 
564 	if (soc->update_clamp_threshold) {
565 		value &= ~(RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD_MASK |
566 				RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD_MASK);
567 		value |= RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD |
568 			RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD;
569 	}
570 
571 	writel(value, port->base + RP_PRIV_MISC);
572 }
573 
574 static void tegra_pcie_program_ectl_settings(struct tegra_pcie_port *port)
575 {
576 	const struct tegra_pcie_soc *soc = port->pcie->soc;
577 	u32 value;
578 
579 	value = readl(port->base + RP_ECTL_2_R1);
580 	value &= ~RP_ECTL_2_R1_RX_CTLE_1C_MASK;
581 	value |= soc->ectl.regs.rp_ectl_2_r1;
582 	writel(value, port->base + RP_ECTL_2_R1);
583 
584 	value = readl(port->base + RP_ECTL_4_R1);
585 	value &= ~RP_ECTL_4_R1_RX_CDR_CTRL_1C_MASK;
586 	value |= soc->ectl.regs.rp_ectl_4_r1 <<
587 				RP_ECTL_4_R1_RX_CDR_CTRL_1C_SHIFT;
588 	writel(value, port->base + RP_ECTL_4_R1);
589 
590 	value = readl(port->base + RP_ECTL_5_R1);
591 	value &= ~RP_ECTL_5_R1_RX_EQ_CTRL_L_1C_MASK;
592 	value |= soc->ectl.regs.rp_ectl_5_r1;
593 	writel(value, port->base + RP_ECTL_5_R1);
594 
595 	value = readl(port->base + RP_ECTL_6_R1);
596 	value &= ~RP_ECTL_6_R1_RX_EQ_CTRL_H_1C_MASK;
597 	value |= soc->ectl.regs.rp_ectl_6_r1;
598 	writel(value, port->base + RP_ECTL_6_R1);
599 
600 	value = readl(port->base + RP_ECTL_2_R2);
601 	value &= ~RP_ECTL_2_R2_RX_CTLE_1C_MASK;
602 	value |= soc->ectl.regs.rp_ectl_2_r2;
603 	writel(value, port->base + RP_ECTL_2_R2);
604 
605 	value = readl(port->base + RP_ECTL_4_R2);
606 	value &= ~RP_ECTL_4_R2_RX_CDR_CTRL_1C_MASK;
607 	value |= soc->ectl.regs.rp_ectl_4_r2 <<
608 				RP_ECTL_4_R2_RX_CDR_CTRL_1C_SHIFT;
609 	writel(value, port->base + RP_ECTL_4_R2);
610 
611 	value = readl(port->base + RP_ECTL_5_R2);
612 	value &= ~RP_ECTL_5_R2_RX_EQ_CTRL_L_1C_MASK;
613 	value |= soc->ectl.regs.rp_ectl_5_r2;
614 	writel(value, port->base + RP_ECTL_5_R2);
615 
616 	value = readl(port->base + RP_ECTL_6_R2);
617 	value &= ~RP_ECTL_6_R2_RX_EQ_CTRL_H_1C_MASK;
618 	value |= soc->ectl.regs.rp_ectl_6_r2;
619 	writel(value, port->base + RP_ECTL_6_R2);
620 }
621 
622 static void tegra_pcie_apply_sw_fixup(struct tegra_pcie_port *port)
623 {
624 	const struct tegra_pcie_soc *soc = port->pcie->soc;
625 	u32 value;
626 
627 	/*
628 	 * Sometimes link speed change from Gen2 to Gen1 fails due to
629 	 * instability in deskew logic on lane-0. Increase the deskew
630 	 * retry time to resolve this issue.
631 	 */
632 	if (soc->program_deskew_time) {
633 		value = readl(port->base + RP_VEND_CTL0);
634 		value &= ~RP_VEND_CTL0_DSK_RST_PULSE_WIDTH_MASK;
635 		value |= RP_VEND_CTL0_DSK_RST_PULSE_WIDTH;
636 		writel(value, port->base + RP_VEND_CTL0);
637 	}
638 
639 	if (soc->update_fc_timer) {
640 		value = readl(port->base + RP_VEND_XP);
641 		value &= ~RP_VEND_XP_UPDATE_FC_THRESHOLD_MASK;
642 		value |= soc->update_fc_threshold;
643 		writel(value, port->base + RP_VEND_XP);
644 	}
645 
646 	/*
647 	 * PCIe link doesn't come up with few legacy PCIe endpoints if
648 	 * root port advertises both Gen-1 and Gen-2 speeds in Tegra.
649 	 * Hence, the strategy followed here is to initially advertise
650 	 * only Gen-1 and after link is up, retrain link to Gen-2 speed
651 	 */
652 	value = readl(port->base + RP_LINK_CONTROL_STATUS_2);
653 	value &= ~PCI_EXP_LNKSTA_CLS;
654 	value |= PCI_EXP_LNKSTA_CLS_2_5GB;
655 	writel(value, port->base + RP_LINK_CONTROL_STATUS_2);
656 }
657 
658 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
659 {
660 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
661 	const struct tegra_pcie_soc *soc = port->pcie->soc;
662 	unsigned long value;
663 
664 	/* enable reference clock */
665 	value = afi_readl(port->pcie, ctrl);
666 	value |= AFI_PEX_CTRL_REFCLK_EN;
667 
668 	if (soc->has_pex_clkreq_en)
669 		value |= AFI_PEX_CTRL_CLKREQ_EN;
670 
671 	value |= AFI_PEX_CTRL_OVERRIDE_EN;
672 
673 	afi_writel(port->pcie, value, ctrl);
674 
675 	tegra_pcie_port_reset(port);
676 
677 	if (soc->force_pca_enable) {
678 		value = readl(port->base + RP_VEND_CTL2);
679 		value |= RP_VEND_CTL2_PCA_ENABLE;
680 		writel(value, port->base + RP_VEND_CTL2);
681 	}
682 
683 	tegra_pcie_enable_rp_features(port);
684 
685 	if (soc->ectl.enable)
686 		tegra_pcie_program_ectl_settings(port);
687 
688 	tegra_pcie_apply_sw_fixup(port);
689 }
690 
691 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
692 {
693 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
694 	const struct tegra_pcie_soc *soc = port->pcie->soc;
695 	unsigned long value;
696 
697 	/* assert port reset */
698 	value = afi_readl(port->pcie, ctrl);
699 	value &= ~AFI_PEX_CTRL_RST;
700 	afi_writel(port->pcie, value, ctrl);
701 
702 	/* disable reference clock */
703 	value = afi_readl(port->pcie, ctrl);
704 
705 	if (soc->has_pex_clkreq_en)
706 		value &= ~AFI_PEX_CTRL_CLKREQ_EN;
707 
708 	value &= ~AFI_PEX_CTRL_REFCLK_EN;
709 	afi_writel(port->pcie, value, ctrl);
710 
711 	/* disable PCIe port and set CLKREQ# as GPIO to allow PLLE power down */
712 	value = afi_readl(port->pcie, AFI_PCIE_CONFIG);
713 	value |= AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
714 	value |= AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(port->index);
715 	afi_writel(port->pcie, value, AFI_PCIE_CONFIG);
716 }
717 
718 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
719 {
720 	struct tegra_pcie *pcie = port->pcie;
721 	struct device *dev = pcie->dev;
722 
723 	devm_iounmap(dev, port->base);
724 	devm_release_mem_region(dev, port->regs.start,
725 				resource_size(&port->regs));
726 	list_del(&port->list);
727 	devm_kfree(dev, port);
728 }
729 
730 /* Tegra PCIE root complex wrongly reports device class */
731 static void tegra_pcie_fixup_class(struct pci_dev *dev)
732 {
733 	dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
734 }
735 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
736 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
737 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
738 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
739 
740 /* Tegra20 and Tegra30 PCIE requires relaxed ordering */
741 static void tegra_pcie_relax_enable(struct pci_dev *dev)
742 {
743 	pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
744 }
745 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_relax_enable);
746 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_relax_enable);
747 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_relax_enable);
748 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_relax_enable);
749 
750 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
751 {
752 	struct tegra_pcie *pcie = pdev->bus->sysdata;
753 	int irq;
754 
755 	tegra_cpuidle_pcie_irqs_in_use();
756 
757 	irq = of_irq_parse_and_map_pci(pdev, slot, pin);
758 	if (!irq)
759 		irq = pcie->irq;
760 
761 	return irq;
762 }
763 
764 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
765 {
766 	static const char * const err_msg[] = {
767 		"Unknown",
768 		"AXI slave error",
769 		"AXI decode error",
770 		"Target abort",
771 		"Master abort",
772 		"Invalid write",
773 		"Legacy interrupt",
774 		"Response decoding error",
775 		"AXI response decoding error",
776 		"Transaction timeout",
777 		"Slot present pin change",
778 		"Slot clock request change",
779 		"TMS clock ramp change",
780 		"TMS ready for power down",
781 		"Peer2Peer error",
782 	};
783 	struct tegra_pcie *pcie = arg;
784 	struct device *dev = pcie->dev;
785 	u32 code, signature;
786 
787 	code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
788 	signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
789 	afi_writel(pcie, 0, AFI_INTR_CODE);
790 
791 	if (code == AFI_INTR_LEGACY)
792 		return IRQ_NONE;
793 
794 	if (code >= ARRAY_SIZE(err_msg))
795 		code = 0;
796 
797 	/*
798 	 * do not pollute kernel log with master abort reports since they
799 	 * happen a lot during enumeration
800 	 */
801 	if (code == AFI_INTR_MASTER_ABORT || code == AFI_INTR_PE_PRSNT_SENSE)
802 		dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
803 	else
804 		dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
805 
806 	if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
807 	    code == AFI_INTR_FPCI_DECODE_ERROR) {
808 		u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
809 		u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
810 
811 		if (code == AFI_INTR_MASTER_ABORT)
812 			dev_dbg(dev, "  FPCI address: %10llx\n", address);
813 		else
814 			dev_err(dev, "  FPCI address: %10llx\n", address);
815 	}
816 
817 	return IRQ_HANDLED;
818 }
819 
820 /*
821  * FPCI map is as follows:
822  * - 0xfdfc000000: I/O space
823  * - 0xfdfe000000: type 0 configuration space
824  * - 0xfdff000000: type 1 configuration space
825  * - 0xfe00000000: type 0 extended configuration space
826  * - 0xfe10000000: type 1 extended configuration space
827  */
828 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
829 {
830 	u32 size;
831 	struct resource_entry *entry;
832 	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
833 
834 	/* Bar 0: type 1 extended configuration space */
835 	size = resource_size(&pcie->cs);
836 	afi_writel(pcie, pcie->cs.start, AFI_AXI_BAR0_START);
837 	afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
838 
839 	resource_list_for_each_entry(entry, &bridge->windows) {
840 		u32 fpci_bar, axi_address;
841 		struct resource *res = entry->res;
842 
843 		size = resource_size(res);
844 
845 		switch (resource_type(res)) {
846 		case IORESOURCE_IO:
847 			/* Bar 1: downstream IO bar */
848 			fpci_bar = 0xfdfc0000;
849 			axi_address = pci_pio_to_address(res->start);
850 			afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
851 			afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
852 			afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
853 			break;
854 		case IORESOURCE_MEM:
855 			fpci_bar = (((res->start >> 12) & 0x0fffffff) << 4) | 0x1;
856 			axi_address = res->start;
857 
858 			if (res->flags & IORESOURCE_PREFETCH) {
859 				/* Bar 2: prefetchable memory BAR */
860 				afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
861 				afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
862 				afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
863 
864 			} else {
865 				/* Bar 3: non prefetchable memory BAR */
866 				afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
867 				afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
868 				afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
869 			}
870 			break;
871 		}
872 	}
873 
874 	/* NULL out the remaining BARs as they are not used */
875 	afi_writel(pcie, 0, AFI_AXI_BAR4_START);
876 	afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
877 	afi_writel(pcie, 0, AFI_FPCI_BAR4);
878 
879 	afi_writel(pcie, 0, AFI_AXI_BAR5_START);
880 	afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
881 	afi_writel(pcie, 0, AFI_FPCI_BAR5);
882 
883 	if (pcie->soc->has_cache_bars) {
884 		/* map all upstream transactions as uncached */
885 		afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
886 		afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
887 		afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
888 		afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
889 	}
890 
891 	/* MSI translations are setup only when needed */
892 	afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
893 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
894 	afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
895 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
896 }
897 
898 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
899 {
900 	const struct tegra_pcie_soc *soc = pcie->soc;
901 	u32 value;
902 
903 	timeout = jiffies + msecs_to_jiffies(timeout);
904 
905 	while (time_before(jiffies, timeout)) {
906 		value = pads_readl(pcie, soc->pads_pll_ctl);
907 		if (value & PADS_PLL_CTL_LOCKDET)
908 			return 0;
909 	}
910 
911 	return -ETIMEDOUT;
912 }
913 
914 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
915 {
916 	struct device *dev = pcie->dev;
917 	const struct tegra_pcie_soc *soc = pcie->soc;
918 	u32 value;
919 	int err;
920 
921 	/* initialize internal PHY, enable up to 16 PCIE lanes */
922 	pads_writel(pcie, 0x0, PADS_CTL_SEL);
923 
924 	/* override IDDQ to 1 on all 4 lanes */
925 	value = pads_readl(pcie, PADS_CTL);
926 	value |= PADS_CTL_IDDQ_1L;
927 	pads_writel(pcie, value, PADS_CTL);
928 
929 	/*
930 	 * Set up PHY PLL inputs select PLLE output as refclock,
931 	 * set TX ref sel to div10 (not div5).
932 	 */
933 	value = pads_readl(pcie, soc->pads_pll_ctl);
934 	value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
935 	value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
936 	pads_writel(pcie, value, soc->pads_pll_ctl);
937 
938 	/* reset PLL */
939 	value = pads_readl(pcie, soc->pads_pll_ctl);
940 	value &= ~PADS_PLL_CTL_RST_B4SM;
941 	pads_writel(pcie, value, soc->pads_pll_ctl);
942 
943 	usleep_range(20, 100);
944 
945 	/* take PLL out of reset  */
946 	value = pads_readl(pcie, soc->pads_pll_ctl);
947 	value |= PADS_PLL_CTL_RST_B4SM;
948 	pads_writel(pcie, value, soc->pads_pll_ctl);
949 
950 	/* wait for the PLL to lock */
951 	err = tegra_pcie_pll_wait(pcie, 500);
952 	if (err < 0) {
953 		dev_err(dev, "PLL failed to lock: %d\n", err);
954 		return err;
955 	}
956 
957 	/* turn off IDDQ override */
958 	value = pads_readl(pcie, PADS_CTL);
959 	value &= ~PADS_CTL_IDDQ_1L;
960 	pads_writel(pcie, value, PADS_CTL);
961 
962 	/* enable TX/RX data */
963 	value = pads_readl(pcie, PADS_CTL);
964 	value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
965 	pads_writel(pcie, value, PADS_CTL);
966 
967 	return 0;
968 }
969 
970 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
971 {
972 	const struct tegra_pcie_soc *soc = pcie->soc;
973 	u32 value;
974 
975 	/* disable TX/RX data */
976 	value = pads_readl(pcie, PADS_CTL);
977 	value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
978 	pads_writel(pcie, value, PADS_CTL);
979 
980 	/* override IDDQ */
981 	value = pads_readl(pcie, PADS_CTL);
982 	value |= PADS_CTL_IDDQ_1L;
983 	pads_writel(pcie, value, PADS_CTL);
984 
985 	/* reset PLL */
986 	value = pads_readl(pcie, soc->pads_pll_ctl);
987 	value &= ~PADS_PLL_CTL_RST_B4SM;
988 	pads_writel(pcie, value, soc->pads_pll_ctl);
989 
990 	usleep_range(20, 100);
991 
992 	return 0;
993 }
994 
995 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
996 {
997 	struct device *dev = port->pcie->dev;
998 	unsigned int i;
999 	int err;
1000 
1001 	for (i = 0; i < port->lanes; i++) {
1002 		err = phy_power_on(port->phys[i]);
1003 		if (err < 0) {
1004 			dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
1005 			return err;
1006 		}
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
1013 {
1014 	struct device *dev = port->pcie->dev;
1015 	unsigned int i;
1016 	int err;
1017 
1018 	for (i = 0; i < port->lanes; i++) {
1019 		err = phy_power_off(port->phys[i]);
1020 		if (err < 0) {
1021 			dev_err(dev, "failed to power off PHY#%u: %d\n", i,
1022 				err);
1023 			return err;
1024 		}
1025 	}
1026 
1027 	return 0;
1028 }
1029 
1030 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
1031 {
1032 	struct device *dev = pcie->dev;
1033 	struct tegra_pcie_port *port;
1034 	int err;
1035 
1036 	if (pcie->legacy_phy) {
1037 		if (pcie->phy)
1038 			err = phy_power_on(pcie->phy);
1039 		else
1040 			err = tegra_pcie_phy_enable(pcie);
1041 
1042 		if (err < 0)
1043 			dev_err(dev, "failed to power on PHY: %d\n", err);
1044 
1045 		return err;
1046 	}
1047 
1048 	list_for_each_entry(port, &pcie->ports, list) {
1049 		err = tegra_pcie_port_phy_power_on(port);
1050 		if (err < 0) {
1051 			dev_err(dev,
1052 				"failed to power on PCIe port %u PHY: %d\n",
1053 				port->index, err);
1054 			return err;
1055 		}
1056 	}
1057 
1058 	return 0;
1059 }
1060 
1061 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
1062 {
1063 	struct device *dev = pcie->dev;
1064 	struct tegra_pcie_port *port;
1065 	int err;
1066 
1067 	if (pcie->legacy_phy) {
1068 		if (pcie->phy)
1069 			err = phy_power_off(pcie->phy);
1070 		else
1071 			err = tegra_pcie_phy_disable(pcie);
1072 
1073 		if (err < 0)
1074 			dev_err(dev, "failed to power off PHY: %d\n", err);
1075 
1076 		return err;
1077 	}
1078 
1079 	list_for_each_entry(port, &pcie->ports, list) {
1080 		err = tegra_pcie_port_phy_power_off(port);
1081 		if (err < 0) {
1082 			dev_err(dev,
1083 				"failed to power off PCIe port %u PHY: %d\n",
1084 				port->index, err);
1085 			return err;
1086 		}
1087 	}
1088 
1089 	return 0;
1090 }
1091 
1092 static void tegra_pcie_enable_controller(struct tegra_pcie *pcie)
1093 {
1094 	const struct tegra_pcie_soc *soc = pcie->soc;
1095 	struct tegra_pcie_port *port;
1096 	unsigned long value;
1097 
1098 	/* enable PLL power down */
1099 	if (pcie->phy) {
1100 		value = afi_readl(pcie, AFI_PLLE_CONTROL);
1101 		value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
1102 		value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1103 		afi_writel(pcie, value, AFI_PLLE_CONTROL);
1104 	}
1105 
1106 	/* power down PCIe slot clock bias pad */
1107 	if (soc->has_pex_bias_ctrl)
1108 		afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1109 
1110 	/* configure mode and disable all ports */
1111 	value = afi_readl(pcie, AFI_PCIE_CONFIG);
1112 	value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1113 	value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1114 	value |= AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO_ALL;
1115 
1116 	list_for_each_entry(port, &pcie->ports, list) {
1117 		value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1118 		value &= ~AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(port->index);
1119 	}
1120 
1121 	afi_writel(pcie, value, AFI_PCIE_CONFIG);
1122 
1123 	if (soc->has_gen2) {
1124 		value = afi_readl(pcie, AFI_FUSE);
1125 		value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1126 		afi_writel(pcie, value, AFI_FUSE);
1127 	} else {
1128 		value = afi_readl(pcie, AFI_FUSE);
1129 		value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1130 		afi_writel(pcie, value, AFI_FUSE);
1131 	}
1132 
1133 	/* Disable AFI dynamic clock gating and enable PCIe */
1134 	value = afi_readl(pcie, AFI_CONFIGURATION);
1135 	value |= AFI_CONFIGURATION_EN_FPCI;
1136 	value |= AFI_CONFIGURATION_CLKEN_OVERRIDE;
1137 	afi_writel(pcie, value, AFI_CONFIGURATION);
1138 
1139 	value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1140 		AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1141 		AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
1142 
1143 	if (soc->has_intr_prsnt_sense)
1144 		value |= AFI_INTR_EN_PRSNT_SENSE;
1145 
1146 	afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1147 	afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1148 
1149 	/* don't enable MSI for now, only when needed */
1150 	afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1151 
1152 	/* disable all exceptions */
1153 	afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1154 }
1155 
1156 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1157 {
1158 	struct device *dev = pcie->dev;
1159 	const struct tegra_pcie_soc *soc = pcie->soc;
1160 	int err;
1161 
1162 	reset_control_assert(pcie->afi_rst);
1163 
1164 	clk_disable_unprepare(pcie->pll_e);
1165 	if (soc->has_cml_clk)
1166 		clk_disable_unprepare(pcie->cml_clk);
1167 	clk_disable_unprepare(pcie->afi_clk);
1168 
1169 	if (!dev->pm_domain)
1170 		tegra_pmc_powergate_power_off(pcie->pmc, TEGRA_POWERGATE_PCIE);
1171 
1172 	err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1173 	if (err < 0)
1174 		dev_warn(dev, "failed to disable regulators: %d\n", err);
1175 }
1176 
1177 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1178 {
1179 	struct device *dev = pcie->dev;
1180 	const struct tegra_pcie_soc *soc = pcie->soc;
1181 	int err;
1182 
1183 	reset_control_assert(pcie->pcie_xrst);
1184 	reset_control_assert(pcie->afi_rst);
1185 	reset_control_assert(pcie->pex_rst);
1186 
1187 	if (!dev->pm_domain)
1188 		tegra_pmc_powergate_power_off(pcie->pmc, TEGRA_POWERGATE_PCIE);
1189 
1190 	/* enable regulators */
1191 	err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1192 	if (err < 0)
1193 		dev_err(dev, "failed to enable regulators: %d\n", err);
1194 
1195 	if (!dev->pm_domain) {
1196 		err = tegra_pmc_powergate_power_on(pcie->pmc,
1197 						   TEGRA_POWERGATE_PCIE);
1198 		if (err) {
1199 			dev_err(dev, "failed to power ungate: %d\n", err);
1200 			goto regulator_disable;
1201 		}
1202 		err = tegra_pmc_powergate_remove_clamping(pcie->pmc,
1203 							  TEGRA_POWERGATE_PCIE);
1204 		if (err) {
1205 			dev_err(dev, "failed to remove clamp: %d\n", err);
1206 			goto powergate;
1207 		}
1208 	}
1209 
1210 	err = clk_prepare_enable(pcie->afi_clk);
1211 	if (err < 0) {
1212 		dev_err(dev, "failed to enable AFI clock: %d\n", err);
1213 		goto powergate;
1214 	}
1215 
1216 	if (soc->has_cml_clk) {
1217 		err = clk_prepare_enable(pcie->cml_clk);
1218 		if (err < 0) {
1219 			dev_err(dev, "failed to enable CML clock: %d\n", err);
1220 			goto disable_afi_clk;
1221 		}
1222 	}
1223 
1224 	err = clk_prepare_enable(pcie->pll_e);
1225 	if (err < 0) {
1226 		dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1227 		goto disable_cml_clk;
1228 	}
1229 
1230 	reset_control_deassert(pcie->afi_rst);
1231 
1232 	return 0;
1233 
1234 disable_cml_clk:
1235 	if (soc->has_cml_clk)
1236 		clk_disable_unprepare(pcie->cml_clk);
1237 disable_afi_clk:
1238 	clk_disable_unprepare(pcie->afi_clk);
1239 powergate:
1240 	if (!dev->pm_domain)
1241 		tegra_pmc_powergate_power_off(pcie->pmc, TEGRA_POWERGATE_PCIE);
1242 regulator_disable:
1243 	regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1244 
1245 	return err;
1246 }
1247 
1248 static void tegra_pcie_apply_pad_settings(struct tegra_pcie *pcie)
1249 {
1250 	const struct tegra_pcie_soc *soc = pcie->soc;
1251 
1252 	/* Configure the reference clock driver */
1253 	pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
1254 
1255 	if (soc->num_ports > 2)
1256 		pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
1257 }
1258 
1259 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1260 {
1261 	struct device *dev = pcie->dev;
1262 	const struct tegra_pcie_soc *soc = pcie->soc;
1263 
1264 	pcie->pex_clk = devm_clk_get(dev, "pex");
1265 	if (IS_ERR(pcie->pex_clk))
1266 		return PTR_ERR(pcie->pex_clk);
1267 
1268 	pcie->afi_clk = devm_clk_get(dev, "afi");
1269 	if (IS_ERR(pcie->afi_clk))
1270 		return PTR_ERR(pcie->afi_clk);
1271 
1272 	pcie->pll_e = devm_clk_get(dev, "pll_e");
1273 	if (IS_ERR(pcie->pll_e))
1274 		return PTR_ERR(pcie->pll_e);
1275 
1276 	if (soc->has_cml_clk) {
1277 		pcie->cml_clk = devm_clk_get(dev, "cml");
1278 		if (IS_ERR(pcie->cml_clk))
1279 			return PTR_ERR(pcie->cml_clk);
1280 	}
1281 
1282 	return 0;
1283 }
1284 
1285 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1286 {
1287 	struct device *dev = pcie->dev;
1288 
1289 	pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1290 	if (IS_ERR(pcie->pex_rst))
1291 		return PTR_ERR(pcie->pex_rst);
1292 
1293 	pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1294 	if (IS_ERR(pcie->afi_rst))
1295 		return PTR_ERR(pcie->afi_rst);
1296 
1297 	pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1298 	if (IS_ERR(pcie->pcie_xrst))
1299 		return PTR_ERR(pcie->pcie_xrst);
1300 
1301 	return 0;
1302 }
1303 
1304 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1305 {
1306 	struct device *dev = pcie->dev;
1307 	int err;
1308 
1309 	pcie->phy = devm_phy_optional_get(dev, "pcie");
1310 	if (IS_ERR(pcie->phy)) {
1311 		err = PTR_ERR(pcie->phy);
1312 		dev_err(dev, "failed to get PHY: %d\n", err);
1313 		return err;
1314 	}
1315 
1316 	err = phy_init(pcie->phy);
1317 	if (err < 0) {
1318 		dev_err(dev, "failed to initialize PHY: %d\n", err);
1319 		return err;
1320 	}
1321 
1322 	pcie->legacy_phy = true;
1323 
1324 	return 0;
1325 }
1326 
1327 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1328 						  struct device_node *np,
1329 						  const char *consumer,
1330 						  unsigned int index)
1331 {
1332 	struct phy *phy;
1333 	char *name;
1334 
1335 	name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1336 	if (!name)
1337 		return ERR_PTR(-ENOMEM);
1338 
1339 	phy = devm_of_phy_optional_get(dev, np, name);
1340 	kfree(name);
1341 
1342 	return phy;
1343 }
1344 
1345 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1346 {
1347 	struct device *dev = port->pcie->dev;
1348 	struct phy *phy;
1349 	unsigned int i;
1350 	int err;
1351 
1352 	port->phys = devm_kcalloc(dev, port->lanes, sizeof(phy), GFP_KERNEL);
1353 	if (!port->phys)
1354 		return -ENOMEM;
1355 
1356 	for (i = 0; i < port->lanes; i++) {
1357 		phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1358 		if (IS_ERR(phy)) {
1359 			dev_err(dev, "failed to get PHY#%u: %pe\n", i, phy);
1360 			return PTR_ERR(phy);
1361 		}
1362 
1363 		err = phy_init(phy);
1364 		if (err < 0) {
1365 			dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1366 				err);
1367 			return err;
1368 		}
1369 
1370 		port->phys[i] = phy;
1371 	}
1372 
1373 	return 0;
1374 }
1375 
1376 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1377 {
1378 	const struct tegra_pcie_soc *soc = pcie->soc;
1379 	struct device_node *np = pcie->dev->of_node;
1380 	struct tegra_pcie_port *port;
1381 	int err;
1382 
1383 	if (!soc->has_gen2 || of_property_present(np, "phys"))
1384 		return tegra_pcie_phys_get_legacy(pcie);
1385 
1386 	list_for_each_entry(port, &pcie->ports, list) {
1387 		err = tegra_pcie_port_get_phys(port);
1388 		if (err < 0)
1389 			return err;
1390 	}
1391 
1392 	return 0;
1393 }
1394 
1395 static void tegra_pcie_phys_put(struct tegra_pcie *pcie)
1396 {
1397 	struct tegra_pcie_port *port;
1398 	struct device *dev = pcie->dev;
1399 	int err, i;
1400 
1401 	if (pcie->legacy_phy) {
1402 		err = phy_exit(pcie->phy);
1403 		if (err < 0)
1404 			dev_err(dev, "failed to teardown PHY: %d\n", err);
1405 		return;
1406 	}
1407 
1408 	list_for_each_entry(port, &pcie->ports, list) {
1409 		for (i = 0; i < port->lanes; i++) {
1410 			err = phy_exit(port->phys[i]);
1411 			if (err < 0)
1412 				dev_err(dev, "failed to teardown PHY#%u: %d\n",
1413 					i, err);
1414 		}
1415 	}
1416 }
1417 
1418 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1419 {
1420 	struct device *dev = pcie->dev;
1421 	struct platform_device *pdev = to_platform_device(dev);
1422 	struct resource *res;
1423 	const struct tegra_pcie_soc *soc = pcie->soc;
1424 	int err;
1425 
1426 	err = tegra_pcie_clocks_get(pcie);
1427 	if (err) {
1428 		dev_err(dev, "failed to get clocks: %d\n", err);
1429 		return err;
1430 	}
1431 
1432 	err = tegra_pcie_resets_get(pcie);
1433 	if (err) {
1434 		dev_err(dev, "failed to get resets: %d\n", err);
1435 		return err;
1436 	}
1437 
1438 	pcie->pmc = devm_tegra_pmc_get(dev);
1439 	if (IS_ERR(pcie->pmc))
1440 		return dev_err_probe(dev, PTR_ERR(pcie->pmc),
1441 				     "failed to get PMC\n");
1442 
1443 	if (soc->program_uphy) {
1444 		err = tegra_pcie_phys_get(pcie);
1445 		if (err < 0) {
1446 			dev_err(dev, "failed to get PHYs: %d\n", err);
1447 			return err;
1448 		}
1449 	}
1450 
1451 	pcie->pads = devm_platform_ioremap_resource_byname(pdev, "pads");
1452 	if (IS_ERR(pcie->pads)) {
1453 		err = PTR_ERR(pcie->pads);
1454 		goto phys_put;
1455 	}
1456 
1457 	pcie->afi = devm_platform_ioremap_resource_byname(pdev, "afi");
1458 	if (IS_ERR(pcie->afi)) {
1459 		err = PTR_ERR(pcie->afi);
1460 		goto phys_put;
1461 	}
1462 
1463 	/* request configuration space, but remap later, on demand */
1464 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1465 	if (!res) {
1466 		err = -EADDRNOTAVAIL;
1467 		goto phys_put;
1468 	}
1469 
1470 	pcie->cs = *res;
1471 
1472 	/* constrain configuration space to 4 KiB */
1473 	resource_set_size(&pcie->cs, SZ_4K);
1474 
1475 	pcie->cfg = devm_ioremap_resource(dev, &pcie->cs);
1476 	if (IS_ERR(pcie->cfg)) {
1477 		err = PTR_ERR(pcie->cfg);
1478 		goto phys_put;
1479 	}
1480 
1481 	/* request interrupt */
1482 	err = platform_get_irq_byname(pdev, "intr");
1483 	if (err < 0)
1484 		goto phys_put;
1485 
1486 	pcie->irq = err;
1487 
1488 	err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1489 	if (err) {
1490 		dev_err(dev, "failed to register IRQ: %d\n", err);
1491 		goto phys_put;
1492 	}
1493 
1494 	return 0;
1495 
1496 phys_put:
1497 	if (soc->program_uphy)
1498 		tegra_pcie_phys_put(pcie);
1499 
1500 	return err;
1501 }
1502 
1503 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1504 {
1505 	const struct tegra_pcie_soc *soc = pcie->soc;
1506 
1507 	if (pcie->irq > 0)
1508 		free_irq(pcie->irq, pcie);
1509 
1510 	if (soc->program_uphy)
1511 		tegra_pcie_phys_put(pcie);
1512 
1513 	return 0;
1514 }
1515 
1516 static void tegra_pcie_pme_turnoff(struct tegra_pcie_port *port)
1517 {
1518 	struct tegra_pcie *pcie = port->pcie;
1519 	const struct tegra_pcie_soc *soc = pcie->soc;
1520 	int err;
1521 	u32 val;
1522 	u8 ack_bit;
1523 
1524 	val = afi_readl(pcie, AFI_PCIE_PME);
1525 	val |= (0x1 << soc->ports[port->index].pme.turnoff_bit);
1526 	afi_writel(pcie, val, AFI_PCIE_PME);
1527 
1528 	ack_bit = soc->ports[port->index].pme.ack_bit;
1529 	err = readl_poll_timeout(pcie->afi + AFI_PCIE_PME, val,
1530 				 val & (0x1 << ack_bit), 1, PME_ACK_TIMEOUT);
1531 	if (err)
1532 		dev_err(pcie->dev, "PME Ack is not received on port: %d\n",
1533 			port->index);
1534 
1535 	usleep_range(10000, 11000);
1536 
1537 	val = afi_readl(pcie, AFI_PCIE_PME);
1538 	val &= ~(0x1 << soc->ports[port->index].pme.turnoff_bit);
1539 	afi_writel(pcie, val, AFI_PCIE_PME);
1540 }
1541 
1542 static void tegra_pcie_msi_irq(struct irq_desc *desc)
1543 {
1544 	struct tegra_pcie *pcie = irq_desc_get_handler_data(desc);
1545 	struct irq_chip *chip = irq_desc_get_chip(desc);
1546 	struct tegra_msi *msi = &pcie->msi;
1547 	struct device *dev = pcie->dev;
1548 	unsigned int i;
1549 
1550 	chained_irq_enter(chip, desc);
1551 
1552 	for (i = 0; i < 8; i++) {
1553 		unsigned long reg = afi_readl(pcie, AFI_MSI_VEC(i));
1554 
1555 		while (reg) {
1556 			unsigned int offset = find_first_bit(&reg, 32);
1557 			unsigned int index = i * 32 + offset;
1558 			int ret;
1559 
1560 			ret = generic_handle_domain_irq(msi->domain, index);
1561 			if (ret) {
1562 				/*
1563 				 * that's weird who triggered this?
1564 				 * just clear it
1565 				 */
1566 				dev_info(dev, "unexpected MSI\n");
1567 				afi_writel(pcie, BIT(index % 32), AFI_MSI_VEC(index));
1568 			}
1569 
1570 			/* see if there's any more pending in this vector */
1571 			reg = afi_readl(pcie, AFI_MSI_VEC(i));
1572 		}
1573 	}
1574 
1575 	chained_irq_exit(chip, desc);
1576 }
1577 
1578 static void tegra_msi_irq_ack(struct irq_data *d)
1579 {
1580 	struct tegra_msi *msi = irq_data_get_irq_chip_data(d);
1581 	struct tegra_pcie *pcie = msi_to_pcie(msi);
1582 	unsigned int index = d->hwirq / 32;
1583 
1584 	/* clear the interrupt */
1585 	afi_writel(pcie, BIT(d->hwirq % 32), AFI_MSI_VEC(index));
1586 }
1587 
1588 static void tegra_msi_irq_mask(struct irq_data *d)
1589 {
1590 	struct tegra_msi *msi = irq_data_get_irq_chip_data(d);
1591 	struct tegra_pcie *pcie = msi_to_pcie(msi);
1592 	unsigned int index = d->hwirq / 32;
1593 	u32 value;
1594 
1595 	scoped_guard(raw_spinlock_irqsave, &msi->mask_lock) {
1596 		value = afi_readl(pcie, AFI_MSI_EN_VEC(index));
1597 		value &= ~BIT(d->hwirq % 32);
1598 		afi_writel(pcie, value, AFI_MSI_EN_VEC(index));
1599 	}
1600 }
1601 
1602 static void tegra_msi_irq_unmask(struct irq_data *d)
1603 {
1604 	struct tegra_msi *msi = irq_data_get_irq_chip_data(d);
1605 	struct tegra_pcie *pcie = msi_to_pcie(msi);
1606 	unsigned int index = d->hwirq / 32;
1607 	u32 value;
1608 
1609 	scoped_guard(raw_spinlock_irqsave, &msi->mask_lock) {
1610 		value = afi_readl(pcie, AFI_MSI_EN_VEC(index));
1611 		value |= BIT(d->hwirq % 32);
1612 		afi_writel(pcie, value, AFI_MSI_EN_VEC(index));
1613 	}
1614 }
1615 
1616 static void tegra_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1617 {
1618 	struct tegra_msi *msi = irq_data_get_irq_chip_data(data);
1619 
1620 	msg->address_lo = lower_32_bits(msi->phys);
1621 	msg->address_hi = upper_32_bits(msi->phys);
1622 	msg->data = data->hwirq;
1623 }
1624 
1625 static struct irq_chip tegra_msi_bottom_chip = {
1626 	.name			= "Tegra MSI",
1627 	.irq_ack		= tegra_msi_irq_ack,
1628 	.irq_mask		= tegra_msi_irq_mask,
1629 	.irq_unmask		= tegra_msi_irq_unmask,
1630 	.irq_compose_msi_msg	= tegra_compose_msi_msg,
1631 };
1632 
1633 static int tegra_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
1634 				  unsigned int nr_irqs, void *args)
1635 {
1636 	struct tegra_msi *msi = domain->host_data;
1637 	unsigned int i;
1638 	int hwirq;
1639 
1640 	mutex_lock(&msi->map_lock);
1641 
1642 	hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
1643 
1644 	mutex_unlock(&msi->map_lock);
1645 
1646 	if (hwirq < 0)
1647 		return -ENOSPC;
1648 
1649 	for (i = 0; i < nr_irqs; i++)
1650 		irq_domain_set_info(domain, virq + i, hwirq + i,
1651 				    &tegra_msi_bottom_chip, domain->host_data,
1652 				    handle_edge_irq, NULL, NULL);
1653 
1654 	tegra_cpuidle_pcie_irqs_in_use();
1655 
1656 	return 0;
1657 }
1658 
1659 static void tegra_msi_domain_free(struct irq_domain *domain, unsigned int virq,
1660 				  unsigned int nr_irqs)
1661 {
1662 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1663 	struct tegra_msi *msi = domain->host_data;
1664 
1665 	mutex_lock(&msi->map_lock);
1666 
1667 	bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
1668 
1669 	mutex_unlock(&msi->map_lock);
1670 }
1671 
1672 static const struct irq_domain_ops tegra_msi_domain_ops = {
1673 	.alloc = tegra_msi_domain_alloc,
1674 	.free = tegra_msi_domain_free,
1675 };
1676 
1677 static const struct msi_parent_ops tegra_msi_parent_ops = {
1678 	.supported_flags	= (MSI_GENERIC_FLAGS_MASK	|
1679 				   MSI_FLAG_PCI_MSIX),
1680 	.required_flags		= (MSI_FLAG_USE_DEF_DOM_OPS	|
1681 				   MSI_FLAG_USE_DEF_CHIP_OPS	|
1682 				   MSI_FLAG_PCI_MSI_MASK_PARENT	|
1683 				   MSI_FLAG_NO_AFFINITY),
1684 	.chip_flags		= MSI_CHIP_FLAG_SET_ACK,
1685 	.bus_select_token	= DOMAIN_BUS_PCI_MSI,
1686 	.init_dev_msi_info	= msi_lib_init_dev_msi_info,
1687 };
1688 
1689 static int tegra_allocate_domains(struct tegra_msi *msi)
1690 {
1691 	struct tegra_pcie *pcie = msi_to_pcie(msi);
1692 	struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
1693 	struct irq_domain_info info = {
1694 		.fwnode		= fwnode,
1695 		.ops		= &tegra_msi_domain_ops,
1696 		.size		= INT_PCI_MSI_NR,
1697 		.host_data	= msi,
1698 	};
1699 
1700 	msi->domain = msi_create_parent_irq_domain(&info, &tegra_msi_parent_ops);
1701 	if (!msi->domain) {
1702 		dev_err(pcie->dev, "failed to create MSI domain\n");
1703 		return -ENOMEM;
1704 	}
1705 	return 0;
1706 }
1707 
1708 static void tegra_free_domains(struct tegra_msi *msi)
1709 {
1710 	irq_domain_remove(msi->domain);
1711 }
1712 
1713 static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
1714 {
1715 	struct platform_device *pdev = to_platform_device(pcie->dev);
1716 	struct tegra_msi *msi = &pcie->msi;
1717 	struct device *dev = pcie->dev;
1718 	int err;
1719 
1720 	mutex_init(&msi->map_lock);
1721 	raw_spin_lock_init(&msi->mask_lock);
1722 
1723 	if (IS_ENABLED(CONFIG_PCI_MSI)) {
1724 		err = tegra_allocate_domains(msi);
1725 		if (err)
1726 			return err;
1727 	}
1728 
1729 	err = platform_get_irq_byname(pdev, "msi");
1730 	if (err < 0)
1731 		goto free_irq_domain;
1732 
1733 	msi->irq = err;
1734 
1735 	irq_set_chained_handler_and_data(msi->irq, tegra_pcie_msi_irq, pcie);
1736 
1737 	/* Though the PCIe controller can address >32-bit address space, to
1738 	 * facilitate endpoints that support only 32-bit MSI target address,
1739 	 * the mask is set to 32-bit to make sure that MSI target address is
1740 	 * always a 32-bit address
1741 	 */
1742 	err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
1743 	if (err < 0) {
1744 		dev_err(dev, "failed to set DMA coherent mask: %d\n", err);
1745 		goto free_irq;
1746 	}
1747 
1748 	msi->virt = dma_alloc_attrs(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL,
1749 				    DMA_ATTR_NO_KERNEL_MAPPING);
1750 	if (!msi->virt) {
1751 		dev_err(dev, "failed to allocate DMA memory for MSI\n");
1752 		err = -ENOMEM;
1753 		goto free_irq;
1754 	}
1755 
1756 	return 0;
1757 
1758 free_irq:
1759 	irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
1760 free_irq_domain:
1761 	if (IS_ENABLED(CONFIG_PCI_MSI))
1762 		tegra_free_domains(msi);
1763 
1764 	return err;
1765 }
1766 
1767 static void tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1768 {
1769 	const struct tegra_pcie_soc *soc = pcie->soc;
1770 	struct tegra_msi *msi = &pcie->msi;
1771 	u32 reg, msi_state[INT_PCI_MSI_NR / 32];
1772 	int i;
1773 
1774 	afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1775 	afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1776 	/* this register is in 4K increments */
1777 	afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1778 
1779 	/* Restore the MSI allocation state */
1780 	bitmap_to_arr32(msi_state, msi->used, INT_PCI_MSI_NR);
1781 	for (i = 0; i < ARRAY_SIZE(msi_state); i++)
1782 		afi_writel(pcie, msi_state[i], AFI_MSI_EN_VEC(i));
1783 
1784 	/* and unmask the MSI interrupt */
1785 	reg = afi_readl(pcie, AFI_INTR_MASK);
1786 	reg |= AFI_INTR_MASK_MSI_MASK;
1787 	afi_writel(pcie, reg, AFI_INTR_MASK);
1788 }
1789 
1790 static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
1791 {
1792 	struct tegra_msi *msi = &pcie->msi;
1793 	unsigned int i, irq;
1794 
1795 	dma_free_attrs(pcie->dev, PAGE_SIZE, msi->virt, msi->phys,
1796 		       DMA_ATTR_NO_KERNEL_MAPPING);
1797 
1798 	for (i = 0; i < INT_PCI_MSI_NR; i++) {
1799 		irq = irq_find_mapping(msi->domain, i);
1800 		if (irq > 0)
1801 			irq_domain_free_irqs(irq, 1);
1802 	}
1803 
1804 	irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
1805 
1806 	if (IS_ENABLED(CONFIG_PCI_MSI))
1807 		tegra_free_domains(msi);
1808 }
1809 
1810 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1811 {
1812 	u32 value;
1813 
1814 	/* mask the MSI interrupt */
1815 	value = afi_readl(pcie, AFI_INTR_MASK);
1816 	value &= ~AFI_INTR_MASK_MSI_MASK;
1817 	afi_writel(pcie, value, AFI_INTR_MASK);
1818 
1819 	return 0;
1820 }
1821 
1822 static void tegra_pcie_disable_interrupts(struct tegra_pcie *pcie)
1823 {
1824 	u32 value;
1825 
1826 	value = afi_readl(pcie, AFI_INTR_MASK);
1827 	value &= ~AFI_INTR_MASK_INT_MASK;
1828 	afi_writel(pcie, value, AFI_INTR_MASK);
1829 }
1830 
1831 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1832 				      u32 *xbar)
1833 {
1834 	struct device *dev = pcie->dev;
1835 	struct device_node *np = dev->of_node;
1836 
1837 	if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1838 		switch (lanes) {
1839 		case 0x010004:
1840 			dev_info(dev, "4x1, 1x1 configuration\n");
1841 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401;
1842 			return 0;
1843 
1844 		case 0x010102:
1845 			dev_info(dev, "2x1, 1X1, 1x1 configuration\n");
1846 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1847 			return 0;
1848 
1849 		case 0x010101:
1850 			dev_info(dev, "1x1, 1x1, 1x1 configuration\n");
1851 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111;
1852 			return 0;
1853 
1854 		default:
1855 			dev_info(dev, "wrong configuration updated in DT, "
1856 				 "switching to default 2x1, 1x1, 1x1 "
1857 				 "configuration\n");
1858 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1859 			return 0;
1860 		}
1861 	} else if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1862 		   of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1863 		switch (lanes) {
1864 		case 0x0000104:
1865 			dev_info(dev, "4x1, 1x1 configuration\n");
1866 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1867 			return 0;
1868 
1869 		case 0x0000102:
1870 			dev_info(dev, "2x1, 1x1 configuration\n");
1871 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1872 			return 0;
1873 		}
1874 	} else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1875 		switch (lanes) {
1876 		case 0x00000204:
1877 			dev_info(dev, "4x1, 2x1 configuration\n");
1878 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1879 			return 0;
1880 
1881 		case 0x00020202:
1882 			dev_info(dev, "2x3 configuration\n");
1883 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1884 			return 0;
1885 
1886 		case 0x00010104:
1887 			dev_info(dev, "4x1, 1x2 configuration\n");
1888 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1889 			return 0;
1890 		}
1891 	} else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1892 		switch (lanes) {
1893 		case 0x00000004:
1894 			dev_info(dev, "single-mode configuration\n");
1895 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1896 			return 0;
1897 
1898 		case 0x00000202:
1899 			dev_info(dev, "dual-mode configuration\n");
1900 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1901 			return 0;
1902 		}
1903 	}
1904 
1905 	return -EINVAL;
1906 }
1907 
1908 /*
1909  * Check whether a given set of supplies is available in a device tree node.
1910  * This is used to check whether the new or the legacy device tree bindings
1911  * should be used.
1912  */
1913 static bool of_regulator_bulk_available(struct device_node *np,
1914 					struct regulator_bulk_data *supplies,
1915 					unsigned int num_supplies)
1916 {
1917 	char property[32];
1918 	unsigned int i;
1919 
1920 	for (i = 0; i < num_supplies; i++) {
1921 		snprintf(property, 32, "%s-supply", supplies[i].supply);
1922 
1923 		if (!of_property_present(np, property))
1924 			return false;
1925 	}
1926 
1927 	return true;
1928 }
1929 
1930 /*
1931  * Old versions of the device tree binding for this device used a set of power
1932  * supplies that didn't match the hardware inputs. This happened to work for a
1933  * number of cases but is not future proof. However to preserve backwards-
1934  * compatibility with old device trees, this function will try to use the old
1935  * set of supplies.
1936  */
1937 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1938 {
1939 	struct device *dev = pcie->dev;
1940 	struct device_node *np = dev->of_node;
1941 
1942 	if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1943 		pcie->num_supplies = 3;
1944 	else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1945 		pcie->num_supplies = 2;
1946 
1947 	if (pcie->num_supplies == 0) {
1948 		dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1949 		return -ENODEV;
1950 	}
1951 
1952 	pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1953 				      sizeof(*pcie->supplies),
1954 				      GFP_KERNEL);
1955 	if (!pcie->supplies)
1956 		return -ENOMEM;
1957 
1958 	pcie->supplies[0].supply = "pex-clk";
1959 	pcie->supplies[1].supply = "vdd";
1960 
1961 	if (pcie->num_supplies > 2)
1962 		pcie->supplies[2].supply = "avdd";
1963 
1964 	return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1965 }
1966 
1967 /*
1968  * Obtains the list of regulators required for a particular generation of the
1969  * IP block.
1970  *
1971  * This would've been nice to do simply by providing static tables for use
1972  * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1973  * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1974  * and either seems to be optional depending on which ports are being used.
1975  */
1976 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1977 {
1978 	struct device *dev = pcie->dev;
1979 	struct device_node *np = dev->of_node;
1980 	unsigned int i = 0;
1981 
1982 	if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1983 		pcie->num_supplies = 4;
1984 
1985 		pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1986 					      sizeof(*pcie->supplies),
1987 					      GFP_KERNEL);
1988 		if (!pcie->supplies)
1989 			return -ENOMEM;
1990 
1991 		pcie->supplies[i++].supply = "dvdd-pex";
1992 		pcie->supplies[i++].supply = "hvdd-pex-pll";
1993 		pcie->supplies[i++].supply = "hvdd-pex";
1994 		pcie->supplies[i++].supply = "vddio-pexctl-aud";
1995 	} else if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1996 		pcie->num_supplies = 3;
1997 
1998 		pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1999 					      sizeof(*pcie->supplies),
2000 					      GFP_KERNEL);
2001 		if (!pcie->supplies)
2002 			return -ENOMEM;
2003 
2004 		pcie->supplies[i++].supply = "hvddio-pex";
2005 		pcie->supplies[i++].supply = "dvddio-pex";
2006 		pcie->supplies[i++].supply = "vddio-pex-ctl";
2007 	} else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
2008 		pcie->num_supplies = 4;
2009 
2010 		pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2011 					      sizeof(*pcie->supplies),
2012 					      GFP_KERNEL);
2013 		if (!pcie->supplies)
2014 			return -ENOMEM;
2015 
2016 		pcie->supplies[i++].supply = "avddio-pex";
2017 		pcie->supplies[i++].supply = "dvddio-pex";
2018 		pcie->supplies[i++].supply = "hvdd-pex";
2019 		pcie->supplies[i++].supply = "vddio-pex-ctl";
2020 	} else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
2021 		bool need_pexa = false, need_pexb = false;
2022 
2023 		/* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
2024 		if (lane_mask & 0x0f)
2025 			need_pexa = true;
2026 
2027 		/* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
2028 		if (lane_mask & 0x30)
2029 			need_pexb = true;
2030 
2031 		pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
2032 					 (need_pexb ? 2 : 0);
2033 
2034 		pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2035 					      sizeof(*pcie->supplies),
2036 					      GFP_KERNEL);
2037 		if (!pcie->supplies)
2038 			return -ENOMEM;
2039 
2040 		pcie->supplies[i++].supply = "avdd-pex-pll";
2041 		pcie->supplies[i++].supply = "hvdd-pex";
2042 		pcie->supplies[i++].supply = "vddio-pex-ctl";
2043 		pcie->supplies[i++].supply = "avdd-plle";
2044 
2045 		if (need_pexa) {
2046 			pcie->supplies[i++].supply = "avdd-pexa";
2047 			pcie->supplies[i++].supply = "vdd-pexa";
2048 		}
2049 
2050 		if (need_pexb) {
2051 			pcie->supplies[i++].supply = "avdd-pexb";
2052 			pcie->supplies[i++].supply = "vdd-pexb";
2053 		}
2054 	} else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
2055 		pcie->num_supplies = 5;
2056 
2057 		pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2058 					      sizeof(*pcie->supplies),
2059 					      GFP_KERNEL);
2060 		if (!pcie->supplies)
2061 			return -ENOMEM;
2062 
2063 		pcie->supplies[0].supply = "avdd-pex";
2064 		pcie->supplies[1].supply = "vdd-pex";
2065 		pcie->supplies[2].supply = "avdd-pex-pll";
2066 		pcie->supplies[3].supply = "avdd-plle";
2067 		pcie->supplies[4].supply = "vddio-pex-clk";
2068 	}
2069 
2070 	if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
2071 					pcie->num_supplies))
2072 		return devm_regulator_bulk_get(dev, pcie->num_supplies,
2073 					       pcie->supplies);
2074 
2075 	/*
2076 	 * If not all regulators are available for this new scheme, assume
2077 	 * that the device tree complies with an older version of the device
2078 	 * tree binding.
2079 	 */
2080 	dev_info(dev, "using legacy DT binding for power supplies\n");
2081 
2082 	devm_kfree(dev, pcie->supplies);
2083 	pcie->num_supplies = 0;
2084 
2085 	return tegra_pcie_get_legacy_regulators(pcie);
2086 }
2087 
2088 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
2089 {
2090 	struct device *dev = pcie->dev;
2091 	struct device_node *np = dev->of_node;
2092 	const struct tegra_pcie_soc *soc = pcie->soc;
2093 	u32 lanes = 0, mask = 0;
2094 	unsigned int lane = 0;
2095 	int err;
2096 
2097 	/* parse root ports */
2098 	for_each_child_of_node_scoped(np, port) {
2099 		struct tegra_pcie_port *rp;
2100 		unsigned int index;
2101 		u32 value;
2102 		char *label;
2103 
2104 		err = of_pci_get_devfn(port);
2105 		if (err < 0)
2106 			return dev_err_probe(dev, err, "failed to parse address\n");
2107 
2108 		index = PCI_SLOT(err);
2109 
2110 		if (index < 1 || index > soc->num_ports)
2111 			return dev_err_probe(dev, -EINVAL,
2112 					     "invalid port number: %d\n", index);
2113 
2114 		index--;
2115 
2116 		err = of_property_read_u32(port, "nvidia,num-lanes", &value);
2117 		if (err < 0)
2118 			return dev_err_probe(dev, err,
2119 					     "failed to parse # of lanes\n");
2120 
2121 		if (value > 16)
2122 			return dev_err_probe(dev, -EINVAL,
2123 					     "invalid # of lanes: %u\n", value);
2124 
2125 		lanes |= value << (index << 3);
2126 
2127 		if (!of_device_is_available(port)) {
2128 			lane += value;
2129 			continue;
2130 		}
2131 
2132 		mask |= ((1 << value) - 1) << lane;
2133 		lane += value;
2134 
2135 		rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
2136 		if (!rp)
2137 			return -ENOMEM;
2138 
2139 		err = of_address_to_resource(port, 0, &rp->regs);
2140 		if (err < 0)
2141 			return dev_err_probe(dev, err, "failed to parse address\n");
2142 
2143 		INIT_LIST_HEAD(&rp->list);
2144 		rp->index = index;
2145 		rp->lanes = value;
2146 		rp->pcie = pcie;
2147 		rp->np = port;
2148 
2149 		rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
2150 		if (IS_ERR(rp->base))
2151 			return PTR_ERR(rp->base);
2152 
2153 		label = devm_kasprintf(dev, GFP_KERNEL, "pex-reset-%u", index);
2154 		if (!label)
2155 			return -ENOMEM;
2156 
2157 		/*
2158 		 * Returns -ENOENT if reset-gpios property is not populated
2159 		 * and in this case fall back to using AFI per port register
2160 		 * to toggle PERST# SFIO line.
2161 		 */
2162 		rp->reset_gpio = devm_fwnode_gpiod_get(dev,
2163 						       of_fwnode_handle(port),
2164 						       "reset",
2165 						       GPIOD_OUT_LOW,
2166 						       label);
2167 		if (IS_ERR(rp->reset_gpio)) {
2168 			if (PTR_ERR(rp->reset_gpio) == -ENOENT)
2169 				rp->reset_gpio = NULL;
2170 			else
2171 				return dev_err_probe(dev, PTR_ERR(rp->reset_gpio),
2172 						     "failed to get reset GPIO\n");
2173 		}
2174 
2175 		list_add_tail(&rp->list, &pcie->ports);
2176 	}
2177 
2178 	err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
2179 	if (err < 0)
2180 		return dev_err_probe(dev, err,
2181 				     "invalid lane configuration\n");
2182 
2183 	err = tegra_pcie_get_regulators(pcie, mask);
2184 	if (err < 0)
2185 		return err;
2186 
2187 	return 0;
2188 }
2189 
2190 /*
2191  * FIXME: If there are no PCIe cards attached, then calling this function
2192  * can result in the increase of the bootup time as there are big timeout
2193  * loops.
2194  */
2195 #define TEGRA_PCIE_LINKUP_TIMEOUT	200	/* up to 1.2 seconds */
2196 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2197 {
2198 	struct device *dev = port->pcie->dev;
2199 	unsigned int retries = 3;
2200 	unsigned long value;
2201 
2202 	/* override presence detection */
2203 	value = readl(port->base + RP_PRIV_MISC);
2204 	value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2205 	value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2206 	writel(value, port->base + RP_PRIV_MISC);
2207 
2208 	do {
2209 		unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2210 
2211 		do {
2212 			value = readl(port->base + RP_VEND_XP);
2213 
2214 			if (value & RP_VEND_XP_DL_UP)
2215 				break;
2216 
2217 			usleep_range(1000, 2000);
2218 		} while (--timeout);
2219 
2220 		if (!timeout) {
2221 			dev_dbg(dev, "link %u down, retrying\n", port->index);
2222 			goto retry;
2223 		}
2224 
2225 		timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2226 
2227 		do {
2228 			value = readl(port->base + RP_LINK_CONTROL_STATUS);
2229 
2230 			if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2231 				return true;
2232 
2233 			usleep_range(1000, 2000);
2234 		} while (--timeout);
2235 
2236 retry:
2237 		tegra_pcie_port_reset(port);
2238 	} while (--retries);
2239 
2240 	return false;
2241 }
2242 
2243 static void tegra_pcie_change_link_speed(struct tegra_pcie *pcie)
2244 {
2245 	struct device *dev = pcie->dev;
2246 	struct tegra_pcie_port *port;
2247 	ktime_t deadline;
2248 	u32 value;
2249 
2250 	list_for_each_entry(port, &pcie->ports, list) {
2251 		/*
2252 		 * "Supported Link Speeds Vector" in "Link Capabilities 2"
2253 		 * is not supported by Tegra. tegra_pcie_change_link_speed()
2254 		 * is called only for Tegra chips which support Gen2.
2255 		 * So there no harm if supported link speed is not verified.
2256 		 */
2257 		value = readl(port->base + RP_LINK_CONTROL_STATUS_2);
2258 		value &= ~PCI_EXP_LNKSTA_CLS;
2259 		value |= PCI_EXP_LNKSTA_CLS_5_0GB;
2260 		writel(value, port->base + RP_LINK_CONTROL_STATUS_2);
2261 
2262 		/*
2263 		 * Poll until link comes back from recovery to avoid race
2264 		 * condition.
2265 		 */
2266 		deadline = ktime_add_us(ktime_get(), LINK_RETRAIN_TIMEOUT);
2267 
2268 		while (ktime_before(ktime_get(), deadline)) {
2269 			value = readl(port->base + RP_LINK_CONTROL_STATUS);
2270 			if ((value & PCI_EXP_LNKSTA_LT) == 0)
2271 				break;
2272 
2273 			usleep_range(2000, 3000);
2274 		}
2275 
2276 		if (value & PCI_EXP_LNKSTA_LT)
2277 			dev_warn(dev, "PCIe port %u link is in recovery\n",
2278 				 port->index);
2279 
2280 		/* Retrain the link */
2281 		value = readl(port->base + RP_LINK_CONTROL_STATUS);
2282 		value |= PCI_EXP_LNKCTL_RL;
2283 		writel(value, port->base + RP_LINK_CONTROL_STATUS);
2284 
2285 		deadline = ktime_add_us(ktime_get(), LINK_RETRAIN_TIMEOUT);
2286 
2287 		while (ktime_before(ktime_get(), deadline)) {
2288 			value = readl(port->base + RP_LINK_CONTROL_STATUS);
2289 			if ((value & PCI_EXP_LNKSTA_LT) == 0)
2290 				break;
2291 
2292 			usleep_range(2000, 3000);
2293 		}
2294 
2295 		if (value & PCI_EXP_LNKSTA_LT)
2296 			dev_err(dev, "failed to retrain link of port %u\n",
2297 				port->index);
2298 	}
2299 }
2300 
2301 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2302 {
2303 	struct device *dev = pcie->dev;
2304 	struct tegra_pcie_port *port, *tmp;
2305 
2306 	list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2307 		dev_info(dev, "probing port %u, using %u lanes\n",
2308 			 port->index, port->lanes);
2309 
2310 		tegra_pcie_port_enable(port);
2311 	}
2312 
2313 	/* Start LTSSM from Tegra side */
2314 	reset_control_deassert(pcie->pcie_xrst);
2315 
2316 	list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2317 		if (tegra_pcie_port_check_link(port))
2318 			continue;
2319 
2320 		dev_info(dev, "link %u down, ignoring\n", port->index);
2321 
2322 		tegra_pcie_port_disable(port);
2323 		tegra_pcie_port_free(port);
2324 	}
2325 
2326 	if (pcie->soc->has_gen2)
2327 		tegra_pcie_change_link_speed(pcie);
2328 }
2329 
2330 static void tegra_pcie_disable_ports(struct tegra_pcie *pcie)
2331 {
2332 	struct tegra_pcie_port *port, *tmp;
2333 
2334 	reset_control_assert(pcie->pcie_xrst);
2335 
2336 	list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2337 		tegra_pcie_port_disable(port);
2338 }
2339 
2340 static const struct tegra_pcie_port_soc tegra20_pcie_ports[] = {
2341 	{ .pme.turnoff_bit = 0, .pme.ack_bit =  5 },
2342 	{ .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2343 };
2344 
2345 static const struct tegra_pcie_soc tegra20_pcie = {
2346 	.num_ports = 2,
2347 	.ports = tegra20_pcie_ports,
2348 	.msi_base_shift = 0,
2349 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2350 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2351 	.pads_refclk_cfg0 = 0xfa5cfa5c,
2352 	.has_pex_clkreq_en = false,
2353 	.has_pex_bias_ctrl = false,
2354 	.has_intr_prsnt_sense = false,
2355 	.has_cml_clk = false,
2356 	.has_gen2 = false,
2357 	.force_pca_enable = false,
2358 	.program_uphy = true,
2359 	.update_clamp_threshold = false,
2360 	.program_deskew_time = false,
2361 	.update_fc_timer = false,
2362 	.has_cache_bars = true,
2363 	.ectl.enable = false,
2364 };
2365 
2366 static const struct tegra_pcie_port_soc tegra30_pcie_ports[] = {
2367 	{ .pme.turnoff_bit =  0, .pme.ack_bit =  5 },
2368 	{ .pme.turnoff_bit =  8, .pme.ack_bit = 10 },
2369 	{ .pme.turnoff_bit = 16, .pme.ack_bit = 18 },
2370 };
2371 
2372 static const struct tegra_pcie_soc tegra30_pcie = {
2373 	.num_ports = 3,
2374 	.ports = tegra30_pcie_ports,
2375 	.msi_base_shift = 8,
2376 	.afi_pex2_ctrl = 0x128,
2377 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2378 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2379 	.pads_refclk_cfg0 = 0xfa5cfa5c,
2380 	.pads_refclk_cfg1 = 0xfa5cfa5c,
2381 	.has_pex_clkreq_en = true,
2382 	.has_pex_bias_ctrl = true,
2383 	.has_intr_prsnt_sense = true,
2384 	.has_cml_clk = true,
2385 	.has_gen2 = false,
2386 	.force_pca_enable = false,
2387 	.program_uphy = true,
2388 	.update_clamp_threshold = false,
2389 	.program_deskew_time = false,
2390 	.update_fc_timer = false,
2391 	.has_cache_bars = false,
2392 	.ectl.enable = false,
2393 };
2394 
2395 static const struct tegra_pcie_soc tegra124_pcie = {
2396 	.num_ports = 2,
2397 	.ports = tegra20_pcie_ports,
2398 	.msi_base_shift = 8,
2399 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2400 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2401 	.pads_refclk_cfg0 = 0x44ac44ac,
2402 	.has_pex_clkreq_en = true,
2403 	.has_pex_bias_ctrl = true,
2404 	.has_intr_prsnt_sense = true,
2405 	.has_cml_clk = true,
2406 	.has_gen2 = true,
2407 	.force_pca_enable = false,
2408 	.program_uphy = true,
2409 	.update_clamp_threshold = true,
2410 	.program_deskew_time = false,
2411 	.update_fc_timer = false,
2412 	.has_cache_bars = false,
2413 	.ectl.enable = false,
2414 };
2415 
2416 static const struct tegra_pcie_soc tegra210_pcie = {
2417 	.num_ports = 2,
2418 	.ports = tegra20_pcie_ports,
2419 	.msi_base_shift = 8,
2420 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2421 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2422 	.pads_refclk_cfg0 = 0x90b890b8,
2423 	/* FC threshold is bit[25:18] */
2424 	.update_fc_threshold = 0x01800000,
2425 	.has_pex_clkreq_en = true,
2426 	.has_pex_bias_ctrl = true,
2427 	.has_intr_prsnt_sense = true,
2428 	.has_cml_clk = true,
2429 	.has_gen2 = true,
2430 	.force_pca_enable = true,
2431 	.program_uphy = true,
2432 	.update_clamp_threshold = true,
2433 	.program_deskew_time = true,
2434 	.update_fc_timer = true,
2435 	.has_cache_bars = false,
2436 	.ectl = {
2437 		.regs = {
2438 			.rp_ectl_2_r1 = 0x0000000f,
2439 			.rp_ectl_4_r1 = 0x00000067,
2440 			.rp_ectl_5_r1 = 0x55010000,
2441 			.rp_ectl_6_r1 = 0x00000001,
2442 			.rp_ectl_2_r2 = 0x0000008f,
2443 			.rp_ectl_4_r2 = 0x000000c7,
2444 			.rp_ectl_5_r2 = 0x55010000,
2445 			.rp_ectl_6_r2 = 0x00000001,
2446 		},
2447 		.enable = true,
2448 	},
2449 };
2450 
2451 static const struct tegra_pcie_port_soc tegra186_pcie_ports[] = {
2452 	{ .pme.turnoff_bit =  0, .pme.ack_bit =  5 },
2453 	{ .pme.turnoff_bit =  8, .pme.ack_bit = 10 },
2454 	{ .pme.turnoff_bit = 12, .pme.ack_bit = 14 },
2455 };
2456 
2457 static const struct tegra_pcie_soc tegra186_pcie = {
2458 	.num_ports = 3,
2459 	.ports = tegra186_pcie_ports,
2460 	.msi_base_shift = 8,
2461 	.afi_pex2_ctrl = 0x19c,
2462 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2463 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2464 	.pads_refclk_cfg0 = 0x80b880b8,
2465 	.pads_refclk_cfg1 = 0x000480b8,
2466 	.has_pex_clkreq_en = true,
2467 	.has_pex_bias_ctrl = true,
2468 	.has_intr_prsnt_sense = true,
2469 	.has_cml_clk = false,
2470 	.has_gen2 = true,
2471 	.force_pca_enable = false,
2472 	.program_uphy = false,
2473 	.update_clamp_threshold = false,
2474 	.program_deskew_time = false,
2475 	.update_fc_timer = false,
2476 	.has_cache_bars = false,
2477 	.ectl.enable = false,
2478 };
2479 
2480 static const struct of_device_id tegra_pcie_of_match[] = {
2481 	{ .compatible = "nvidia,tegra186-pcie", .data = &tegra186_pcie },
2482 	{ .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2483 	{ .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2484 	{ .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2485 	{ .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2486 	{ },
2487 };
2488 MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
2489 
2490 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2491 {
2492 	struct tegra_pcie *pcie = s->private;
2493 
2494 	if (list_empty(&pcie->ports))
2495 		return NULL;
2496 
2497 	seq_puts(s, "Index  Status\n");
2498 
2499 	return seq_list_start(&pcie->ports, *pos);
2500 }
2501 
2502 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2503 {
2504 	struct tegra_pcie *pcie = s->private;
2505 
2506 	return seq_list_next(v, &pcie->ports, pos);
2507 }
2508 
2509 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2510 {
2511 }
2512 
2513 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2514 {
2515 	bool up = false, active = false;
2516 	struct tegra_pcie_port *port;
2517 	unsigned int value;
2518 
2519 	port = list_entry(v, struct tegra_pcie_port, list);
2520 
2521 	value = readl(port->base + RP_VEND_XP);
2522 
2523 	if (value & RP_VEND_XP_DL_UP)
2524 		up = true;
2525 
2526 	value = readl(port->base + RP_LINK_CONTROL_STATUS);
2527 
2528 	if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2529 		active = true;
2530 
2531 	seq_printf(s, "%2u     ", port->index);
2532 
2533 	if (up)
2534 		seq_puts(s, "up");
2535 
2536 	if (active) {
2537 		if (up)
2538 			seq_puts(s, ", ");
2539 
2540 		seq_puts(s, "active");
2541 	}
2542 
2543 	seq_puts(s, "\n");
2544 	return 0;
2545 }
2546 
2547 static const struct seq_operations tegra_pcie_ports_sops = {
2548 	.start = tegra_pcie_ports_seq_start,
2549 	.next = tegra_pcie_ports_seq_next,
2550 	.stop = tegra_pcie_ports_seq_stop,
2551 	.show = tegra_pcie_ports_seq_show,
2552 };
2553 
2554 DEFINE_SEQ_ATTRIBUTE(tegra_pcie_ports);
2555 
2556 static void tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2557 {
2558 	pcie->debugfs = debugfs_create_dir("pcie", NULL);
2559 
2560 	debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs, pcie,
2561 			    &tegra_pcie_ports_fops);
2562 }
2563 
2564 static int tegra_pcie_probe(struct platform_device *pdev)
2565 {
2566 	struct device *dev = &pdev->dev;
2567 	struct pci_host_bridge *host;
2568 	struct tegra_pcie *pcie;
2569 	int err;
2570 
2571 	host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2572 	if (!host)
2573 		return -ENOMEM;
2574 
2575 	pcie = pci_host_bridge_priv(host);
2576 	host->sysdata = pcie;
2577 	platform_set_drvdata(pdev, pcie);
2578 
2579 	pcie->soc = of_device_get_match_data(dev);
2580 	INIT_LIST_HEAD(&pcie->ports);
2581 	pcie->dev = dev;
2582 
2583 	err = tegra_pcie_parse_dt(pcie);
2584 	if (err < 0)
2585 		return err;
2586 
2587 	err = tegra_pcie_get_resources(pcie);
2588 	if (err < 0) {
2589 		dev_err(dev, "failed to request resources: %d\n", err);
2590 		return err;
2591 	}
2592 
2593 	err = tegra_pcie_msi_setup(pcie);
2594 	if (err < 0) {
2595 		dev_err(dev, "failed to enable MSI support: %d\n", err);
2596 		goto put_resources;
2597 	}
2598 
2599 	pm_runtime_enable(pcie->dev);
2600 	err = pm_runtime_get_sync(pcie->dev);
2601 	if (err < 0) {
2602 		dev_err(dev, "fail to enable pcie controller: %d\n", err);
2603 		goto pm_runtime_put;
2604 	}
2605 
2606 	host->ops = &tegra_pcie_ops;
2607 	host->map_irq = tegra_pcie_map_irq;
2608 
2609 	err = pci_host_probe(host);
2610 	if (err < 0) {
2611 		dev_err(dev, "failed to register host: %d\n", err);
2612 		goto pm_runtime_put;
2613 	}
2614 
2615 	if (IS_ENABLED(CONFIG_DEBUG_FS))
2616 		tegra_pcie_debugfs_init(pcie);
2617 
2618 	return 0;
2619 
2620 pm_runtime_put:
2621 	pm_runtime_put_sync(pcie->dev);
2622 	pm_runtime_disable(pcie->dev);
2623 	tegra_pcie_msi_teardown(pcie);
2624 put_resources:
2625 	tegra_pcie_put_resources(pcie);
2626 	return err;
2627 }
2628 
2629 static int tegra_pcie_pm_suspend(struct device *dev)
2630 {
2631 	struct tegra_pcie *pcie = dev_get_drvdata(dev);
2632 	struct tegra_pcie_port *port;
2633 	int err;
2634 
2635 	list_for_each_entry(port, &pcie->ports, list)
2636 		tegra_pcie_pme_turnoff(port);
2637 
2638 	tegra_pcie_disable_ports(pcie);
2639 
2640 	/*
2641 	 * AFI_INTR is unmasked in tegra_pcie_enable_controller(), mask it to
2642 	 * avoid unwanted interrupts raised by AFI after pex_rst is asserted.
2643 	 */
2644 	tegra_pcie_disable_interrupts(pcie);
2645 
2646 	if (pcie->soc->program_uphy) {
2647 		err = tegra_pcie_phy_power_off(pcie);
2648 		if (err < 0)
2649 			dev_err(dev, "failed to power off PHY(s): %d\n", err);
2650 	}
2651 
2652 	reset_control_assert(pcie->pex_rst);
2653 	clk_disable_unprepare(pcie->pex_clk);
2654 
2655 	if (IS_ENABLED(CONFIG_PCI_MSI))
2656 		tegra_pcie_disable_msi(pcie);
2657 
2658 	pinctrl_pm_select_idle_state(dev);
2659 	tegra_pcie_power_off(pcie);
2660 
2661 	return 0;
2662 }
2663 
2664 static int tegra_pcie_pm_resume(struct device *dev)
2665 {
2666 	struct tegra_pcie *pcie = dev_get_drvdata(dev);
2667 	int err;
2668 
2669 	err = tegra_pcie_power_on(pcie);
2670 	if (err) {
2671 		dev_err(dev, "tegra pcie power on fail: %d\n", err);
2672 		return err;
2673 	}
2674 
2675 	err = pinctrl_pm_select_default_state(dev);
2676 	if (err < 0) {
2677 		dev_err(dev, "failed to disable PCIe IO DPD: %d\n", err);
2678 		goto poweroff;
2679 	}
2680 
2681 	tegra_pcie_enable_controller(pcie);
2682 	tegra_pcie_setup_translations(pcie);
2683 
2684 	if (IS_ENABLED(CONFIG_PCI_MSI))
2685 		tegra_pcie_enable_msi(pcie);
2686 
2687 	err = clk_prepare_enable(pcie->pex_clk);
2688 	if (err) {
2689 		dev_err(dev, "failed to enable PEX clock: %d\n", err);
2690 		goto pex_dpd_enable;
2691 	}
2692 
2693 	reset_control_deassert(pcie->pex_rst);
2694 
2695 	if (pcie->soc->program_uphy) {
2696 		err = tegra_pcie_phy_power_on(pcie);
2697 		if (err < 0) {
2698 			dev_err(dev, "failed to power on PHY(s): %d\n", err);
2699 			goto disable_pex_clk;
2700 		}
2701 	}
2702 
2703 	tegra_pcie_apply_pad_settings(pcie);
2704 	tegra_pcie_enable_ports(pcie);
2705 
2706 	return 0;
2707 
2708 disable_pex_clk:
2709 	reset_control_assert(pcie->pex_rst);
2710 	clk_disable_unprepare(pcie->pex_clk);
2711 pex_dpd_enable:
2712 	pinctrl_pm_select_idle_state(dev);
2713 poweroff:
2714 	tegra_pcie_power_off(pcie);
2715 
2716 	return err;
2717 }
2718 
2719 static const struct dev_pm_ops tegra_pcie_pm_ops = {
2720 	RUNTIME_PM_OPS(tegra_pcie_pm_suspend, tegra_pcie_pm_resume, NULL)
2721 	NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_pcie_pm_suspend, tegra_pcie_pm_resume)
2722 };
2723 
2724 static struct platform_driver tegra_pcie_driver = {
2725 	.driver = {
2726 		.name = "tegra-pcie",
2727 		.of_match_table = tegra_pcie_of_match,
2728 		.suppress_bind_attrs = true,
2729 		.pm = &tegra_pcie_pm_ops,
2730 	},
2731 	.probe = tegra_pcie_probe,
2732 };
2733 builtin_platform_driver(tegra_pcie_driver);
2734 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
2735 MODULE_DESCRIPTION("NVIDIA PCI host controller driver");
2736 MODULE_LICENSE("GPL");
2737