xref: /linux/drivers/pci/controller/pci-tegra.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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: %ld\n", i,
1360 				PTR_ERR(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