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