xref: /rk3399_rockchip-uboot/drivers/pci/pci_tegra.c (revision 90aa625c9a9e1fb7a2f001fd8e50099bacaf92b8)
1f315828bSThierry Reding /*
2f315828bSThierry Reding  * Copyright (c) 2010, CompuLab, Ltd.
3f315828bSThierry Reding  * Author: Mike Rapoport <mike@compulab.co.il>
4f315828bSThierry Reding  *
5f315828bSThierry Reding  * Based on NVIDIA PCIe driver
6f315828bSThierry Reding  * Copyright (c) 2008-2009, NVIDIA Corporation.
7f315828bSThierry Reding  *
8f315828bSThierry Reding  * Copyright (c) 2013-2014, NVIDIA Corporation.
9f315828bSThierry Reding  *
10f315828bSThierry Reding  * SPDX-License-Identifier:	GPL-2.0
11f315828bSThierry Reding  */
12f315828bSThierry Reding 
13f315828bSThierry Reding #define pr_fmt(fmt) "tegra-pcie: " fmt
14f315828bSThierry Reding 
15f315828bSThierry Reding #include <common.h>
16bbc5b36bSStephen Warren #include <clk.h>
17e81ca884SSimon Glass #include <dm.h>
18f315828bSThierry Reding #include <errno.h>
19f315828bSThierry Reding #include <malloc.h>
20f315828bSThierry Reding #include <pci.h>
21bbc5b36bSStephen Warren #include <power-domain.h>
22bbc5b36bSStephen Warren #include <reset.h>
23f315828bSThierry Reding 
24f315828bSThierry Reding #include <asm/io.h>
25f315828bSThierry Reding #include <asm/gpio.h>
26f315828bSThierry Reding 
2768f00811SSimon Glass #include <linux/ioport.h>
28bbc5b36bSStephen Warren #include <linux/list.h>
29bbc5b36bSStephen Warren 
30bbc5b36bSStephen Warren #ifndef CONFIG_TEGRA186
31f315828bSThierry Reding #include <asm/arch/clock.h>
32f315828bSThierry Reding #include <asm/arch/powergate.h>
33f315828bSThierry Reding #include <asm/arch-tegra/xusb-padctl.h>
34f315828bSThierry Reding #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
35bbc5b36bSStephen Warren #endif
36bbc5b36bSStephen Warren 
37bbc5b36bSStephen Warren /*
38bbc5b36bSStephen Warren  * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
39bbc5b36bSStephen Warren  * should not be present. These are needed because newer Tegra SoCs support
40bbc5b36bSStephen Warren  * only the standard clock/reset APIs, whereas older Tegra SoCs support only
41bbc5b36bSStephen Warren  * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
42bbc5b36bSStephen Warren  * fixed to implement the standard APIs, and all drivers converted to solely
43bbc5b36bSStephen Warren  * use the new standard APIs, with no ifdefs.
44bbc5b36bSStephen Warren  */
45f315828bSThierry Reding 
46f315828bSThierry Reding DECLARE_GLOBAL_DATA_PTR;
47f315828bSThierry Reding 
48f315828bSThierry Reding #define AFI_AXI_BAR0_SZ	0x00
49f315828bSThierry Reding #define AFI_AXI_BAR1_SZ	0x04
50f315828bSThierry Reding #define AFI_AXI_BAR2_SZ	0x08
51f315828bSThierry Reding #define AFI_AXI_BAR3_SZ	0x0c
52f315828bSThierry Reding #define AFI_AXI_BAR4_SZ	0x10
53f315828bSThierry Reding #define AFI_AXI_BAR5_SZ	0x14
54f315828bSThierry Reding 
55f315828bSThierry Reding #define AFI_AXI_BAR0_START	0x18
56f315828bSThierry Reding #define AFI_AXI_BAR1_START	0x1c
57f315828bSThierry Reding #define AFI_AXI_BAR2_START	0x20
58f315828bSThierry Reding #define AFI_AXI_BAR3_START	0x24
59f315828bSThierry Reding #define AFI_AXI_BAR4_START	0x28
60f315828bSThierry Reding #define AFI_AXI_BAR5_START	0x2c
61f315828bSThierry Reding 
62f315828bSThierry Reding #define AFI_FPCI_BAR0	0x30
63f315828bSThierry Reding #define AFI_FPCI_BAR1	0x34
64f315828bSThierry Reding #define AFI_FPCI_BAR2	0x38
65f315828bSThierry Reding #define AFI_FPCI_BAR3	0x3c
66f315828bSThierry Reding #define AFI_FPCI_BAR4	0x40
67f315828bSThierry Reding #define AFI_FPCI_BAR5	0x44
68f315828bSThierry Reding 
69f315828bSThierry Reding #define AFI_CACHE_BAR0_SZ	0x48
70f315828bSThierry Reding #define AFI_CACHE_BAR0_ST	0x4c
71f315828bSThierry Reding #define AFI_CACHE_BAR1_SZ	0x50
72f315828bSThierry Reding #define AFI_CACHE_BAR1_ST	0x54
73f315828bSThierry Reding 
74f315828bSThierry Reding #define AFI_MSI_BAR_SZ		0x60
75f315828bSThierry Reding #define AFI_MSI_FPCI_BAR_ST	0x64
76f315828bSThierry Reding #define AFI_MSI_AXI_BAR_ST	0x68
77f315828bSThierry Reding 
78f315828bSThierry Reding #define AFI_CONFIGURATION		0xac
79f315828bSThierry Reding #define  AFI_CONFIGURATION_EN_FPCI	(1 << 0)
80f315828bSThierry Reding 
81f315828bSThierry Reding #define AFI_FPCI_ERROR_MASKS	0xb0
82f315828bSThierry Reding 
83f315828bSThierry Reding #define AFI_INTR_MASK		0xb4
84f315828bSThierry Reding #define  AFI_INTR_MASK_INT_MASK	(1 << 0)
85f315828bSThierry Reding #define  AFI_INTR_MASK_MSI_MASK	(1 << 8)
86f315828bSThierry Reding 
87f315828bSThierry Reding #define AFI_SM_INTR_ENABLE	0xc4
88f315828bSThierry Reding #define  AFI_SM_INTR_INTA_ASSERT	(1 << 0)
89f315828bSThierry Reding #define  AFI_SM_INTR_INTB_ASSERT	(1 << 1)
90f315828bSThierry Reding #define  AFI_SM_INTR_INTC_ASSERT	(1 << 2)
91f315828bSThierry Reding #define  AFI_SM_INTR_INTD_ASSERT	(1 << 3)
92f315828bSThierry Reding #define  AFI_SM_INTR_INTA_DEASSERT	(1 << 4)
93f315828bSThierry Reding #define  AFI_SM_INTR_INTB_DEASSERT	(1 << 5)
94f315828bSThierry Reding #define  AFI_SM_INTR_INTC_DEASSERT	(1 << 6)
95f315828bSThierry Reding #define  AFI_SM_INTR_INTD_DEASSERT	(1 << 7)
96f315828bSThierry Reding 
97f315828bSThierry Reding #define AFI_AFI_INTR_ENABLE		0xc8
98f315828bSThierry Reding #define  AFI_INTR_EN_INI_SLVERR		(1 << 0)
99f315828bSThierry Reding #define  AFI_INTR_EN_INI_DECERR		(1 << 1)
100f315828bSThierry Reding #define  AFI_INTR_EN_TGT_SLVERR		(1 << 2)
101f315828bSThierry Reding #define  AFI_INTR_EN_TGT_DECERR		(1 << 3)
102f315828bSThierry Reding #define  AFI_INTR_EN_TGT_WRERR		(1 << 4)
103f315828bSThierry Reding #define  AFI_INTR_EN_DFPCI_DECERR	(1 << 5)
104f315828bSThierry Reding #define  AFI_INTR_EN_AXI_DECERR		(1 << 6)
105f315828bSThierry Reding #define  AFI_INTR_EN_FPCI_TIMEOUT	(1 << 7)
106f315828bSThierry Reding #define  AFI_INTR_EN_PRSNT_SENSE	(1 << 8)
107f315828bSThierry Reding 
108f315828bSThierry Reding #define AFI_PCIE_CONFIG					0x0f8
109f315828bSThierry Reding #define  AFI_PCIE_CONFIG_PCIE_DISABLE(x)		(1 << ((x) + 1))
110f315828bSThierry Reding #define  AFI_PCIE_CONFIG_PCIE_DISABLE_ALL		0xe
111f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK	(0xf << 20)
112f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE	(0x0 << 20)
113f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420	(0x0 << 20)
114f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1	(0x0 << 20)
115f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL	(0x1 << 20)
116f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222	(0x1 << 20)
117f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1	(0x1 << 20)
118f315828bSThierry Reding #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411	(0x2 << 20)
119bbc5b36bSStephen Warren #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_401	(0x0 << 20)
120bbc5b36bSStephen Warren #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_211	(0x1 << 20)
121bbc5b36bSStephen Warren #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_111	(0x2 << 20)
122f315828bSThierry Reding 
123f315828bSThierry Reding #define AFI_FUSE			0x104
124f315828bSThierry Reding #define  AFI_FUSE_PCIE_T0_GEN2_DIS	(1 << 2)
125f315828bSThierry Reding 
126f315828bSThierry Reding #define AFI_PEX0_CTRL			0x110
127f315828bSThierry Reding #define AFI_PEX1_CTRL			0x118
128f315828bSThierry Reding #define AFI_PEX2_CTRL			0x128
129bbc5b36bSStephen Warren #define AFI_PEX2_CTRL_T186		0x19c
130f315828bSThierry Reding #define  AFI_PEX_CTRL_RST		(1 << 0)
131f315828bSThierry Reding #define  AFI_PEX_CTRL_CLKREQ_EN		(1 << 1)
132f315828bSThierry Reding #define  AFI_PEX_CTRL_REFCLK_EN		(1 << 3)
133f315828bSThierry Reding #define  AFI_PEX_CTRL_OVERRIDE_EN	(1 << 4)
134f315828bSThierry Reding 
135f315828bSThierry Reding #define AFI_PLLE_CONTROL		0x160
136f315828bSThierry Reding #define  AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
137f315828bSThierry Reding #define  AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
138f315828bSThierry Reding 
139f315828bSThierry Reding #define AFI_PEXBIAS_CTRL_0		0x168
140f315828bSThierry Reding 
141f315828bSThierry Reding #define PADS_CTL_SEL		0x0000009C
142f315828bSThierry Reding 
143f315828bSThierry Reding #define PADS_CTL		0x000000A0
144f315828bSThierry Reding #define  PADS_CTL_IDDQ_1L	(1 <<  0)
145f315828bSThierry Reding #define  PADS_CTL_TX_DATA_EN_1L	(1 <<  6)
146f315828bSThierry Reding #define  PADS_CTL_RX_DATA_EN_1L	(1 << 10)
147f315828bSThierry Reding 
148f315828bSThierry Reding #define PADS_PLL_CTL_TEGRA20			0x000000B8
149f315828bSThierry Reding #define PADS_PLL_CTL_TEGRA30			0x000000B4
150f315828bSThierry Reding #define  PADS_PLL_CTL_RST_B4SM			(0x1 <<  1)
151f315828bSThierry Reding #define  PADS_PLL_CTL_LOCKDET			(0x1 <<  8)
152f315828bSThierry Reding #define  PADS_PLL_CTL_REFCLK_MASK		(0x3 << 16)
153f315828bSThierry Reding #define  PADS_PLL_CTL_REFCLK_INTERNAL_CML	(0x0 << 16)
154f315828bSThierry Reding #define  PADS_PLL_CTL_REFCLK_INTERNAL_CMOS	(0x1 << 16)
155f315828bSThierry Reding #define  PADS_PLL_CTL_REFCLK_EXTERNAL		(0x2 << 16)
156f315828bSThierry Reding #define  PADS_PLL_CTL_TXCLKREF_MASK		(0x1 << 20)
157f315828bSThierry Reding #define  PADS_PLL_CTL_TXCLKREF_DIV10		(0x0 << 20)
158f315828bSThierry Reding #define  PADS_PLL_CTL_TXCLKREF_DIV5		(0x1 << 20)
159f315828bSThierry Reding #define  PADS_PLL_CTL_TXCLKREF_BUF_EN		(0x1 << 22)
160f315828bSThierry Reding 
161f315828bSThierry Reding #define PADS_REFCLK_CFG0			0x000000C8
162f315828bSThierry Reding #define PADS_REFCLK_CFG1			0x000000CC
163f315828bSThierry Reding 
164f315828bSThierry Reding /*
165f315828bSThierry Reding  * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
166f315828bSThierry Reding  * entries, one entry per PCIe port. These field definitions and desired
167f315828bSThierry Reding  * values aren't in the TRM, but do come from NVIDIA.
168f315828bSThierry Reding  */
169f315828bSThierry Reding #define PADS_REFCLK_CFG_TERM_SHIFT		2  /* 6:2 */
170f315828bSThierry Reding #define PADS_REFCLK_CFG_E_TERM_SHIFT		7
171f315828bSThierry Reding #define PADS_REFCLK_CFG_PREDI_SHIFT		8  /* 11:8 */
172f315828bSThierry Reding #define PADS_REFCLK_CFG_DRVI_SHIFT		12 /* 15:12 */
173f315828bSThierry Reding 
174f315828bSThierry Reding #define RP_VEND_XP	0x00000F00
175f315828bSThierry Reding #define  RP_VEND_XP_DL_UP	(1 << 30)
176f315828bSThierry Reding 
177514e1913SStephen Warren #define RP_VEND_CTL2				0x00000FA8
178514e1913SStephen Warren #define  RP_VEND_CTL2_PCA_ENABLE		(1 << 7)
179514e1913SStephen Warren 
180f315828bSThierry Reding #define RP_PRIV_MISC	0x00000FE0
181f315828bSThierry Reding #define  RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
182f315828bSThierry Reding #define  RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
183f315828bSThierry Reding 
184f315828bSThierry Reding #define RP_LINK_CONTROL_STATUS			0x00000090
185f315828bSThierry Reding #define  RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE	0x20000000
186f315828bSThierry Reding #define  RP_LINK_CONTROL_STATUS_LINKSTAT_MASK	0x3fff0000
187f315828bSThierry Reding 
188e81ca884SSimon Glass enum tegra_pci_id {
189e81ca884SSimon Glass 	TEGRA20_PCIE,
190e81ca884SSimon Glass 	TEGRA30_PCIE,
191e81ca884SSimon Glass 	TEGRA124_PCIE,
192e81ca884SSimon Glass 	TEGRA210_PCIE,
193bbc5b36bSStephen Warren 	TEGRA186_PCIE,
194e81ca884SSimon Glass };
195f315828bSThierry Reding 
196f315828bSThierry Reding struct tegra_pcie_port {
197f315828bSThierry Reding 	struct tegra_pcie *pcie;
198f315828bSThierry Reding 
199f315828bSThierry Reding 	struct fdt_resource regs;
200f315828bSThierry Reding 	unsigned int num_lanes;
201f315828bSThierry Reding 	unsigned int index;
202f315828bSThierry Reding 
203f315828bSThierry Reding 	struct list_head list;
204f315828bSThierry Reding };
205f315828bSThierry Reding 
206f315828bSThierry Reding struct tegra_pcie_soc {
207f315828bSThierry Reding 	unsigned int num_ports;
208f315828bSThierry Reding 	unsigned long pads_pll_ctl;
209f315828bSThierry Reding 	unsigned long tx_ref_sel;
210bbc5b36bSStephen Warren 	unsigned long afi_pex2_ctrl;
2113cfc6be4SStephen Warren 	u32 pads_refclk_cfg0;
2123cfc6be4SStephen Warren 	u32 pads_refclk_cfg1;
213f315828bSThierry Reding 	bool has_pex_clkreq_en;
214f315828bSThierry Reding 	bool has_pex_bias_ctrl;
215f315828bSThierry Reding 	bool has_cml_clk;
216f315828bSThierry Reding 	bool has_gen2;
217514e1913SStephen Warren 	bool force_pca_enable;
218f315828bSThierry Reding };
219f315828bSThierry Reding 
220f315828bSThierry Reding struct tegra_pcie {
221f315828bSThierry Reding 	struct pci_controller hose;
222f315828bSThierry Reding 
22368f00811SSimon Glass 	struct resource pads;
22468f00811SSimon Glass 	struct resource afi;
22568f00811SSimon Glass 	struct resource cs;
226f315828bSThierry Reding 
227f315828bSThierry Reding 	struct list_head ports;
228f315828bSThierry Reding 	unsigned long xbar;
229f315828bSThierry Reding 
230f315828bSThierry Reding 	const struct tegra_pcie_soc *soc;
231bbc5b36bSStephen Warren 
232bbc5b36bSStephen Warren #ifdef CONFIG_TEGRA186
233bbc5b36bSStephen Warren 	struct clk clk_afi;
234bbc5b36bSStephen Warren 	struct clk clk_pex;
235bbc5b36bSStephen Warren 	struct reset_ctl reset_afi;
236bbc5b36bSStephen Warren 	struct reset_ctl reset_pex;
237bbc5b36bSStephen Warren 	struct reset_ctl reset_pcie_x;
238bbc5b36bSStephen Warren 	struct power_domain pwrdom;
239bbc5b36bSStephen Warren #else
240f315828bSThierry Reding 	struct tegra_xusb_phy *phy;
241bbc5b36bSStephen Warren #endif
242f315828bSThierry Reding };
243f315828bSThierry Reding 
afi_writel(struct tegra_pcie * pcie,unsigned long value,unsigned long offset)244f315828bSThierry Reding static void afi_writel(struct tegra_pcie *pcie, unsigned long value,
245f315828bSThierry Reding 		       unsigned long offset)
246f315828bSThierry Reding {
247f315828bSThierry Reding 	writel(value, pcie->afi.start + offset);
248f315828bSThierry Reding }
249f315828bSThierry Reding 
afi_readl(struct tegra_pcie * pcie,unsigned long offset)250f315828bSThierry Reding static unsigned long afi_readl(struct tegra_pcie *pcie, unsigned long offset)
251f315828bSThierry Reding {
252f315828bSThierry Reding 	return readl(pcie->afi.start + offset);
253f315828bSThierry Reding }
254f315828bSThierry Reding 
pads_writel(struct tegra_pcie * pcie,unsigned long value,unsigned long offset)255f315828bSThierry Reding static void pads_writel(struct tegra_pcie *pcie, unsigned long value,
256f315828bSThierry Reding 			unsigned long offset)
257f315828bSThierry Reding {
258f315828bSThierry Reding 	writel(value, pcie->pads.start + offset);
259f315828bSThierry Reding }
260f315828bSThierry Reding 
261bbc5b36bSStephen Warren #ifndef CONFIG_TEGRA186
pads_readl(struct tegra_pcie * pcie,unsigned long offset)262f315828bSThierry Reding static unsigned long pads_readl(struct tegra_pcie *pcie, unsigned long offset)
263f315828bSThierry Reding {
264f315828bSThierry Reding 	return readl(pcie->pads.start + offset);
265f315828bSThierry Reding }
266bbc5b36bSStephen Warren #endif
267f315828bSThierry Reding 
rp_readl(struct tegra_pcie_port * port,unsigned long offset)268f315828bSThierry Reding static unsigned long rp_readl(struct tegra_pcie_port *port,
269f315828bSThierry Reding 			      unsigned long offset)
270f315828bSThierry Reding {
271f315828bSThierry Reding 	return readl(port->regs.start + offset);
272f315828bSThierry Reding }
273f315828bSThierry Reding 
rp_writel(struct tegra_pcie_port * port,unsigned long value,unsigned long offset)274f315828bSThierry Reding static void rp_writel(struct tegra_pcie_port *port, unsigned long value,
275f315828bSThierry Reding 		      unsigned long offset)
276f315828bSThierry Reding {
277f315828bSThierry Reding 	writel(value, port->regs.start + offset);
278f315828bSThierry Reding }
279f315828bSThierry Reding 
tegra_pcie_conf_offset(pci_dev_t bdf,int where)280f315828bSThierry Reding static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where)
281f315828bSThierry Reding {
282f315828bSThierry Reding 	return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) |
283f315828bSThierry Reding 	       (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) |
284f315828bSThierry Reding 	       (where & 0xfc);
285f315828bSThierry Reding }
286f315828bSThierry Reding 
tegra_pcie_conf_address(struct tegra_pcie * pcie,pci_dev_t bdf,int where,unsigned long * address)287f315828bSThierry Reding static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf,
288f315828bSThierry Reding 				   int where, unsigned long *address)
289f315828bSThierry Reding {
290f315828bSThierry Reding 	unsigned int bus = PCI_BUS(bdf);
291f315828bSThierry Reding 
292f315828bSThierry Reding 	if (bus == 0) {
293f315828bSThierry Reding 		unsigned int dev = PCI_DEV(bdf);
294f315828bSThierry Reding 		struct tegra_pcie_port *port;
295f315828bSThierry Reding 
296f315828bSThierry Reding 		list_for_each_entry(port, &pcie->ports, list) {
297f315828bSThierry Reding 			if (port->index + 1 == dev) {
298f315828bSThierry Reding 				*address = port->regs.start + (where & ~3);
299f315828bSThierry Reding 				return 0;
300f315828bSThierry Reding 			}
301f315828bSThierry Reding 		}
302f5c6db84SStephen Warren 		return -EFAULT;
303f315828bSThierry Reding 	} else {
304f5c6db84SStephen Warren #ifdef CONFIG_TEGRA20
305f5c6db84SStephen Warren 		unsigned int dev = PCI_DEV(bdf);
306f5c6db84SStephen Warren 		if (dev != 0)
307f5c6db84SStephen Warren 			return -EFAULT;
308f5c6db84SStephen Warren #endif
309f5c6db84SStephen Warren 
310f315828bSThierry Reding 		*address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where);
311f315828bSThierry Reding 		return 0;
312f315828bSThierry Reding 	}
313f315828bSThierry Reding }
314f315828bSThierry Reding 
pci_tegra_read_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)315e81ca884SSimon Glass static int pci_tegra_read_config(struct udevice *bus, pci_dev_t bdf,
316e81ca884SSimon Glass 				 uint offset, ulong *valuep,
317e81ca884SSimon Glass 				 enum pci_size_t size)
318f315828bSThierry Reding {
319e81ca884SSimon Glass 	struct tegra_pcie *pcie = dev_get_priv(bus);
320e81ca884SSimon Glass 	unsigned long address, value;
321f315828bSThierry Reding 	int err;
322f315828bSThierry Reding 
323e81ca884SSimon Glass 	err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
324f315828bSThierry Reding 	if (err < 0) {
325e81ca884SSimon Glass 		value = 0xffffffff;
326e81ca884SSimon Glass 		goto done;
327f315828bSThierry Reding 	}
328f315828bSThierry Reding 
329e81ca884SSimon Glass 	value = readl(address);
330f315828bSThierry Reding 
331f5c6db84SStephen Warren #ifdef CONFIG_TEGRA20
332f315828bSThierry Reding 	/* fixup root port class */
333f315828bSThierry Reding 	if (PCI_BUS(bdf) == 0) {
334f5c6db84SStephen Warren 		if ((offset & ~3) == PCI_CLASS_REVISION) {
335e81ca884SSimon Glass 			value &= ~0x00ff0000;
336e81ca884SSimon Glass 			value |= PCI_CLASS_BRIDGE_PCI << 16;
337f315828bSThierry Reding 		}
338f315828bSThierry Reding 	}
339f5c6db84SStephen Warren #endif
340f315828bSThierry Reding 
341e81ca884SSimon Glass done:
342e81ca884SSimon Glass 	*valuep = pci_conv_32_to_size(value, offset, size);
343e81ca884SSimon Glass 
344f315828bSThierry Reding 	return 0;
345f315828bSThierry Reding }
346f315828bSThierry Reding 
pci_tegra_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)347e81ca884SSimon Glass static int pci_tegra_write_config(struct udevice *bus, pci_dev_t bdf,
348e81ca884SSimon Glass 				  uint offset, ulong value,
349e81ca884SSimon Glass 				  enum pci_size_t size)
350f315828bSThierry Reding {
351e81ca884SSimon Glass 	struct tegra_pcie *pcie = dev_get_priv(bus);
352f315828bSThierry Reding 	unsigned long address;
353e81ca884SSimon Glass 	ulong old;
354f315828bSThierry Reding 	int err;
355f315828bSThierry Reding 
356e81ca884SSimon Glass 	err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
357f315828bSThierry Reding 	if (err < 0)
358e81ca884SSimon Glass 		return 0;
359f315828bSThierry Reding 
360e81ca884SSimon Glass 	old = readl(address);
361e81ca884SSimon Glass 	value = pci_conv_size_to_32(old, value, offset, size);
362f315828bSThierry Reding 	writel(value, address);
363f315828bSThierry Reding 
364f315828bSThierry Reding 	return 0;
365f315828bSThierry Reding }
366f315828bSThierry Reding 
tegra_pcie_port_parse_dt(ofnode node,struct tegra_pcie_port * port)36768f00811SSimon Glass static int tegra_pcie_port_parse_dt(ofnode node, struct tegra_pcie_port *port)
368f315828bSThierry Reding {
369f315828bSThierry Reding 	const u32 *addr;
370f315828bSThierry Reding 	int len;
371f315828bSThierry Reding 
37268f00811SSimon Glass 	addr = ofnode_get_property(node, "assigned-addresses", &len);
373f315828bSThierry Reding 	if (!addr) {
374*90aa625cSMasahiro Yamada 		pr_err("property \"assigned-addresses\" not found");
375f315828bSThierry Reding 		return -FDT_ERR_NOTFOUND;
376f315828bSThierry Reding 	}
377f315828bSThierry Reding 
378f315828bSThierry Reding 	port->regs.start = fdt32_to_cpu(addr[2]);
379f315828bSThierry Reding 	port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]);
380f315828bSThierry Reding 
381f315828bSThierry Reding 	return 0;
382f315828bSThierry Reding }
383f315828bSThierry Reding 
tegra_pcie_get_xbar_config(ofnode node,u32 lanes,enum tegra_pci_id id,unsigned long * xbar)38468f00811SSimon Glass static int tegra_pcie_get_xbar_config(ofnode node, u32 lanes,
385e81ca884SSimon Glass 				      enum tegra_pci_id id, unsigned long *xbar)
386f315828bSThierry Reding {
387f315828bSThierry Reding 	switch (id) {
388e81ca884SSimon Glass 	case TEGRA20_PCIE:
389f315828bSThierry Reding 		switch (lanes) {
390f315828bSThierry Reding 		case 0x00000004:
391f315828bSThierry Reding 			debug("single-mode configuration\n");
392f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
393f315828bSThierry Reding 			return 0;
394f315828bSThierry Reding 
395f315828bSThierry Reding 		case 0x00000202:
396f315828bSThierry Reding 			debug("dual-mode configuration\n");
397f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
398f315828bSThierry Reding 			return 0;
399f315828bSThierry Reding 		}
400f315828bSThierry Reding 		break;
401e81ca884SSimon Glass 	case TEGRA30_PCIE:
402f315828bSThierry Reding 		switch (lanes) {
403f315828bSThierry Reding 		case 0x00000204:
404f315828bSThierry Reding 			debug("4x1, 2x1 configuration\n");
405f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
406f315828bSThierry Reding 			return 0;
407f315828bSThierry Reding 
408f315828bSThierry Reding 		case 0x00020202:
409f315828bSThierry Reding 			debug("2x3 configuration\n");
410f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
411f315828bSThierry Reding 			return 0;
412f315828bSThierry Reding 
413f315828bSThierry Reding 		case 0x00010104:
414f315828bSThierry Reding 			debug("4x1, 1x2 configuration\n");
415f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
416f315828bSThierry Reding 			return 0;
417f315828bSThierry Reding 		}
418f315828bSThierry Reding 		break;
419e81ca884SSimon Glass 	case TEGRA124_PCIE:
420e81ca884SSimon Glass 	case TEGRA210_PCIE:
421f315828bSThierry Reding 		switch (lanes) {
422f315828bSThierry Reding 		case 0x0000104:
423f315828bSThierry Reding 			debug("4x1, 1x1 configuration\n");
424f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
425f315828bSThierry Reding 			return 0;
426f315828bSThierry Reding 
427f315828bSThierry Reding 		case 0x0000102:
428f315828bSThierry Reding 			debug("2x1, 1x1 configuration\n");
429f315828bSThierry Reding 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
430f315828bSThierry Reding 			return 0;
431f315828bSThierry Reding 		}
432f315828bSThierry Reding 		break;
433bbc5b36bSStephen Warren 	case TEGRA186_PCIE:
434bbc5b36bSStephen Warren 		switch (lanes) {
435bbc5b36bSStephen Warren 		case 0x0010004:
436bbc5b36bSStephen Warren 			debug("x4 x1 configuration\n");
437bbc5b36bSStephen Warren 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_401;
438bbc5b36bSStephen Warren 			return 0;
439bbc5b36bSStephen Warren 
440bbc5b36bSStephen Warren 		case 0x0010102:
441bbc5b36bSStephen Warren 			debug("x2 x1 x1 configuration\n");
442bbc5b36bSStephen Warren 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_211;
443bbc5b36bSStephen Warren 			return 0;
444bbc5b36bSStephen Warren 
445bbc5b36bSStephen Warren 		case 0x0010101:
446bbc5b36bSStephen Warren 			debug("x1 x1 x1 configuration\n");
447bbc5b36bSStephen Warren 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_111;
448bbc5b36bSStephen Warren 			return 0;
449bbc5b36bSStephen Warren 		}
450bbc5b36bSStephen Warren 		break;
451f315828bSThierry Reding 	default:
452f315828bSThierry Reding 		break;
453f315828bSThierry Reding 	}
454f315828bSThierry Reding 
455f315828bSThierry Reding 	return -FDT_ERR_NOTFOUND;
456f315828bSThierry Reding }
457f315828bSThierry Reding 
tegra_pcie_parse_port_info(ofnode node,uint * index,uint * lanes)45868f00811SSimon Glass static int tegra_pcie_parse_port_info(ofnode node, uint *index, uint *lanes)
459f315828bSThierry Reding {
460a62e84d7SBin Meng 	struct fdt_pci_addr addr;
461f315828bSThierry Reding 	int err;
462f315828bSThierry Reding 
46368f00811SSimon Glass 	err = ofnode_read_u32_default(node, "nvidia,num-lanes", -1);
464f315828bSThierry Reding 	if (err < 0) {
465*90aa625cSMasahiro Yamada 		pr_err("failed to parse \"nvidia,num-lanes\" property");
466f315828bSThierry Reding 		return err;
467f315828bSThierry Reding 	}
468f315828bSThierry Reding 
469f315828bSThierry Reding 	*lanes = err;
470f315828bSThierry Reding 
47168f00811SSimon Glass 	err = ofnode_read_pci_addr(node, 0, "reg", &addr);
472f315828bSThierry Reding 	if (err < 0) {
473*90aa625cSMasahiro Yamada 		pr_err("failed to parse \"reg\" property");
474f315828bSThierry Reding 		return err;
475f315828bSThierry Reding 	}
476f315828bSThierry Reding 
477053b86e6SSjoerd Simons 	*index = PCI_DEV(addr.phys_hi) - 1;
478f315828bSThierry Reding 
479f315828bSThierry Reding 	return 0;
480f315828bSThierry Reding }
481f315828bSThierry Reding 
tegra_pcie_board_init(void)482e81ca884SSimon Glass int __weak tegra_pcie_board_init(void)
483e81ca884SSimon Glass {
484e81ca884SSimon Glass 	return 0;
485e81ca884SSimon Glass }
486e81ca884SSimon Glass 
tegra_pcie_parse_dt(struct udevice * dev,enum tegra_pci_id id,struct tegra_pcie * pcie)48768f00811SSimon Glass static int tegra_pcie_parse_dt(struct udevice *dev, enum tegra_pci_id id,
488f315828bSThierry Reding 			       struct tegra_pcie *pcie)
489f315828bSThierry Reding {
49068f00811SSimon Glass 	ofnode subnode;
491f315828bSThierry Reding 	u32 lanes = 0;
49268f00811SSimon Glass 	int err;
493f315828bSThierry Reding 
49468f00811SSimon Glass 	err = dev_read_resource(dev, 0, &pcie->pads);
495f315828bSThierry Reding 	if (err < 0) {
496*90aa625cSMasahiro Yamada 		pr_err("resource \"pads\" not found");
497f315828bSThierry Reding 		return err;
498f315828bSThierry Reding 	}
499f315828bSThierry Reding 
50068f00811SSimon Glass 	err = dev_read_resource(dev, 1, &pcie->afi);
501f315828bSThierry Reding 	if (err < 0) {
502*90aa625cSMasahiro Yamada 		pr_err("resource \"afi\" not found");
503f315828bSThierry Reding 		return err;
504f315828bSThierry Reding 	}
505f315828bSThierry Reding 
50668f00811SSimon Glass 	err = dev_read_resource(dev, 2, &pcie->cs);
507f315828bSThierry Reding 	if (err < 0) {
508*90aa625cSMasahiro Yamada 		pr_err("resource \"cs\" not found");
509f315828bSThierry Reding 		return err;
510f315828bSThierry Reding 	}
511f315828bSThierry Reding 
512dfa71e9fSSimon Glass 	err = tegra_pcie_board_init();
513dfa71e9fSSimon Glass 	if (err < 0) {
514*90aa625cSMasahiro Yamada 		pr_err("tegra_pcie_board_init() failed: err=%d", err);
515dfa71e9fSSimon Glass 		return err;
516dfa71e9fSSimon Glass 	}
517e81ca884SSimon Glass 
518bbc5b36bSStephen Warren #ifndef CONFIG_TEGRA186
519f315828bSThierry Reding 	pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
520f315828bSThierry Reding 	if (pcie->phy) {
521f315828bSThierry Reding 		err = tegra_xusb_phy_prepare(pcie->phy);
522f315828bSThierry Reding 		if (err < 0) {
523*90aa625cSMasahiro Yamada 			pr_err("failed to prepare PHY: %d", err);
524f315828bSThierry Reding 			return err;
525f315828bSThierry Reding 		}
526f315828bSThierry Reding 	}
527bbc5b36bSStephen Warren #endif
528f315828bSThierry Reding 
52968f00811SSimon Glass 	dev_for_each_subnode(subnode, dev) {
530f315828bSThierry Reding 		unsigned int index = 0, num_lanes = 0;
531f315828bSThierry Reding 		struct tegra_pcie_port *port;
532f315828bSThierry Reding 
53368f00811SSimon Glass 		err = tegra_pcie_parse_port_info(subnode, &index, &num_lanes);
534f315828bSThierry Reding 		if (err < 0) {
535*90aa625cSMasahiro Yamada 			pr_err("failed to obtain root port info");
536f315828bSThierry Reding 			continue;
537f315828bSThierry Reding 		}
538f315828bSThierry Reding 
539f315828bSThierry Reding 		lanes |= num_lanes << (index << 3);
540f315828bSThierry Reding 
54168f00811SSimon Glass 		if (!ofnode_is_available(subnode))
542f315828bSThierry Reding 			continue;
543f315828bSThierry Reding 
544f315828bSThierry Reding 		port = malloc(sizeof(*port));
545f315828bSThierry Reding 		if (!port)
546f315828bSThierry Reding 			continue;
547f315828bSThierry Reding 
548f315828bSThierry Reding 		memset(port, 0, sizeof(*port));
549f315828bSThierry Reding 		port->num_lanes = num_lanes;
550f315828bSThierry Reding 		port->index = index;
551f315828bSThierry Reding 
55268f00811SSimon Glass 		err = tegra_pcie_port_parse_dt(subnode, port);
553f315828bSThierry Reding 		if (err < 0) {
554f315828bSThierry Reding 			free(port);
555f315828bSThierry Reding 			continue;
556f315828bSThierry Reding 		}
557f315828bSThierry Reding 
558f315828bSThierry Reding 		list_add_tail(&port->list, &pcie->ports);
559f315828bSThierry Reding 		port->pcie = pcie;
560f315828bSThierry Reding 	}
561f315828bSThierry Reding 
56268f00811SSimon Glass 	err = tegra_pcie_get_xbar_config(dev_ofnode(dev), lanes, id,
56368f00811SSimon Glass 					 &pcie->xbar);
564f315828bSThierry Reding 	if (err < 0) {
565*90aa625cSMasahiro Yamada 		pr_err("invalid lane configuration");
566f315828bSThierry Reding 		return err;
567f315828bSThierry Reding 	}
568f315828bSThierry Reding 
569f315828bSThierry Reding 	return 0;
570f315828bSThierry Reding }
571f315828bSThierry Reding 
572bbc5b36bSStephen Warren #ifdef CONFIG_TEGRA186
tegra_pcie_power_on(struct tegra_pcie * pcie)573bbc5b36bSStephen Warren static int tegra_pcie_power_on(struct tegra_pcie *pcie)
574bbc5b36bSStephen Warren {
575bbc5b36bSStephen Warren 	int ret;
576bbc5b36bSStephen Warren 
577bbc5b36bSStephen Warren 	ret = power_domain_on(&pcie->pwrdom);
578bbc5b36bSStephen Warren 	if (ret) {
579*90aa625cSMasahiro Yamada 		pr_err("power_domain_on() failed: %d\n", ret);
580bbc5b36bSStephen Warren 		return ret;
581bbc5b36bSStephen Warren 	}
582bbc5b36bSStephen Warren 
583bbc5b36bSStephen Warren 	ret = clk_enable(&pcie->clk_afi);
584bbc5b36bSStephen Warren 	if (ret) {
585*90aa625cSMasahiro Yamada 		pr_err("clk_enable(afi) failed: %d\n", ret);
586bbc5b36bSStephen Warren 		return ret;
587bbc5b36bSStephen Warren 	}
588bbc5b36bSStephen Warren 
589bbc5b36bSStephen Warren 	ret = clk_enable(&pcie->clk_pex);
590bbc5b36bSStephen Warren 	if (ret) {
591*90aa625cSMasahiro Yamada 		pr_err("clk_enable(pex) failed: %d\n", ret);
592bbc5b36bSStephen Warren 		return ret;
593bbc5b36bSStephen Warren 	}
594bbc5b36bSStephen Warren 
595bbc5b36bSStephen Warren 	ret = reset_deassert(&pcie->reset_afi);
596bbc5b36bSStephen Warren 	if (ret) {
597*90aa625cSMasahiro Yamada 		pr_err("reset_deassert(afi) failed: %d\n", ret);
598bbc5b36bSStephen Warren 		return ret;
599bbc5b36bSStephen Warren 	}
600bbc5b36bSStephen Warren 
601bbc5b36bSStephen Warren 	ret = reset_deassert(&pcie->reset_pex);
602bbc5b36bSStephen Warren 	if (ret) {
603*90aa625cSMasahiro Yamada 		pr_err("reset_deassert(pex) failed: %d\n", ret);
604bbc5b36bSStephen Warren 		return ret;
605bbc5b36bSStephen Warren 	}
606bbc5b36bSStephen Warren 
607bbc5b36bSStephen Warren 	return 0;
608bbc5b36bSStephen Warren }
609bbc5b36bSStephen Warren #else
tegra_pcie_power_on(struct tegra_pcie * pcie)610f315828bSThierry Reding static int tegra_pcie_power_on(struct tegra_pcie *pcie)
611f315828bSThierry Reding {
612f315828bSThierry Reding 	const struct tegra_pcie_soc *soc = pcie->soc;
613f315828bSThierry Reding 	unsigned long value;
614f315828bSThierry Reding 	int err;
615f315828bSThierry Reding 
616f315828bSThierry Reding 	/* reset PCIEXCLK logic, AFI controller and PCIe controller */
617f315828bSThierry Reding 	reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
618f315828bSThierry Reding 	reset_set_enable(PERIPH_ID_AFI, 1);
619f315828bSThierry Reding 	reset_set_enable(PERIPH_ID_PCIE, 1);
620f315828bSThierry Reding 
621f315828bSThierry Reding 	err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
622f315828bSThierry Reding 	if (err < 0) {
623*90aa625cSMasahiro Yamada 		pr_err("failed to power off PCIe partition: %d", err);
624f315828bSThierry Reding 		return err;
625f315828bSThierry Reding 	}
626f315828bSThierry Reding 
627f315828bSThierry Reding 	err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
628f315828bSThierry Reding 						PERIPH_ID_PCIE);
629f315828bSThierry Reding 	if (err < 0) {
630*90aa625cSMasahiro Yamada 		pr_err("failed to power up PCIe partition: %d", err);
631f315828bSThierry Reding 		return err;
632f315828bSThierry Reding 	}
633f315828bSThierry Reding 
634f315828bSThierry Reding 	/* take AFI controller out of reset */
635f315828bSThierry Reding 	reset_set_enable(PERIPH_ID_AFI, 0);
636f315828bSThierry Reding 
637f315828bSThierry Reding 	/* enable AFI clock */
638f315828bSThierry Reding 	clock_enable(PERIPH_ID_AFI);
639f315828bSThierry Reding 
640f315828bSThierry Reding 	if (soc->has_cml_clk) {
641f315828bSThierry Reding 		/* enable CML clock */
642f315828bSThierry Reding 		value = readl(NV_PA_CLK_RST_BASE + 0x48c);
643f315828bSThierry Reding 		value |= (1 << 0);
644f315828bSThierry Reding 		value &= ~(1 << 1);
645f315828bSThierry Reding 		writel(value, NV_PA_CLK_RST_BASE + 0x48c);
646f315828bSThierry Reding 	}
647f315828bSThierry Reding 
648f315828bSThierry Reding 	err = tegra_plle_enable();
649f315828bSThierry Reding 	if (err < 0) {
650*90aa625cSMasahiro Yamada 		pr_err("failed to enable PLLE: %d\n", err);
651f315828bSThierry Reding 		return err;
652f315828bSThierry Reding 	}
653f315828bSThierry Reding 
654f315828bSThierry Reding 	return 0;
655f315828bSThierry Reding }
656f315828bSThierry Reding 
tegra_pcie_pll_wait(struct tegra_pcie * pcie,unsigned long timeout)657f315828bSThierry Reding static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
658f315828bSThierry Reding {
659f315828bSThierry Reding 	const struct tegra_pcie_soc *soc = pcie->soc;
660f315828bSThierry Reding 	unsigned long start = get_timer(0);
661f315828bSThierry Reding 	u32 value;
662f315828bSThierry Reding 
663f315828bSThierry Reding 	while (get_timer(start) < timeout) {
664f315828bSThierry Reding 		value = pads_readl(pcie, soc->pads_pll_ctl);
665f315828bSThierry Reding 		if (value & PADS_PLL_CTL_LOCKDET)
666f315828bSThierry Reding 			return 0;
667f315828bSThierry Reding 	}
668f315828bSThierry Reding 
669f315828bSThierry Reding 	return -ETIMEDOUT;
670f315828bSThierry Reding }
671f315828bSThierry Reding 
tegra_pcie_phy_enable(struct tegra_pcie * pcie)672f315828bSThierry Reding static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
673f315828bSThierry Reding {
674f315828bSThierry Reding 	const struct tegra_pcie_soc *soc = pcie->soc;
675f315828bSThierry Reding 	u32 value;
676f315828bSThierry Reding 	int err;
677f315828bSThierry Reding 
678f315828bSThierry Reding 	/* initialize internal PHY, enable up to 16 PCIe lanes */
679f315828bSThierry Reding 	pads_writel(pcie, 0, PADS_CTL_SEL);
680f315828bSThierry Reding 
681f315828bSThierry Reding 	/* override IDDQ to 1 on all 4 lanes */
682f315828bSThierry Reding 	value = pads_readl(pcie, PADS_CTL);
683f315828bSThierry Reding 	value |= PADS_CTL_IDDQ_1L;
684f315828bSThierry Reding 	pads_writel(pcie, value, PADS_CTL);
685f315828bSThierry Reding 
686f315828bSThierry Reding 	/*
687f315828bSThierry Reding 	 * Set up PHY PLL inputs select PLLE output as refclock, set TX
688f315828bSThierry Reding 	 * ref sel to div10 (not div5).
689f315828bSThierry Reding 	 */
690f315828bSThierry Reding 	value = pads_readl(pcie, soc->pads_pll_ctl);
691f315828bSThierry Reding 	value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
692f315828bSThierry Reding 	value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
693f315828bSThierry Reding 	pads_writel(pcie, value, soc->pads_pll_ctl);
694f315828bSThierry Reding 
695f315828bSThierry Reding 	/* reset PLL */
696f315828bSThierry Reding 	value = pads_readl(pcie, soc->pads_pll_ctl);
697f315828bSThierry Reding 	value &= ~PADS_PLL_CTL_RST_B4SM;
698f315828bSThierry Reding 	pads_writel(pcie, value, soc->pads_pll_ctl);
699f315828bSThierry Reding 
700f315828bSThierry Reding 	udelay(20);
701f315828bSThierry Reding 
702f315828bSThierry Reding 	/* take PLL out of reset */
703f315828bSThierry Reding 	value = pads_readl(pcie, soc->pads_pll_ctl);
704f315828bSThierry Reding 	value |= PADS_PLL_CTL_RST_B4SM;
705f315828bSThierry Reding 	pads_writel(pcie, value, soc->pads_pll_ctl);
706f315828bSThierry Reding 
707f315828bSThierry Reding 	/* wait for the PLL to lock */
708f315828bSThierry Reding 	err = tegra_pcie_pll_wait(pcie, 500);
709f315828bSThierry Reding 	if (err < 0) {
710*90aa625cSMasahiro Yamada 		pr_err("PLL failed to lock: %d", err);
711f315828bSThierry Reding 		return err;
712f315828bSThierry Reding 	}
713f315828bSThierry Reding 
714f315828bSThierry Reding 	/* turn off IDDQ override */
715f315828bSThierry Reding 	value = pads_readl(pcie, PADS_CTL);
716f315828bSThierry Reding 	value &= ~PADS_CTL_IDDQ_1L;
717f315828bSThierry Reding 	pads_writel(pcie, value, PADS_CTL);
718f315828bSThierry Reding 
719f315828bSThierry Reding 	/* enable TX/RX data */
720f315828bSThierry Reding 	value = pads_readl(pcie, PADS_CTL);
721f315828bSThierry Reding 	value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
722f315828bSThierry Reding 	pads_writel(pcie, value, PADS_CTL);
723f315828bSThierry Reding 
724f315828bSThierry Reding 	return 0;
725f315828bSThierry Reding }
726bbc5b36bSStephen Warren #endif
727f315828bSThierry Reding 
tegra_pcie_enable_controller(struct tegra_pcie * pcie)728f315828bSThierry Reding static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
729f315828bSThierry Reding {
730f315828bSThierry Reding 	const struct tegra_pcie_soc *soc = pcie->soc;
731f315828bSThierry Reding 	struct tegra_pcie_port *port;
732f315828bSThierry Reding 	u32 value;
733f315828bSThierry Reding 	int err;
734f315828bSThierry Reding 
735bbc5b36bSStephen Warren #ifdef CONFIG_TEGRA186
736bbc5b36bSStephen Warren 	{
737bbc5b36bSStephen Warren #else
738f315828bSThierry Reding 	if (pcie->phy) {
739bbc5b36bSStephen Warren #endif
740f315828bSThierry Reding 		value = afi_readl(pcie, AFI_PLLE_CONTROL);
741f315828bSThierry Reding 		value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
742f315828bSThierry Reding 		value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
743f315828bSThierry Reding 		afi_writel(pcie, value, AFI_PLLE_CONTROL);
744f315828bSThierry Reding 	}
745f315828bSThierry Reding 
746f315828bSThierry Reding 	if (soc->has_pex_bias_ctrl)
747f315828bSThierry Reding 		afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
748f315828bSThierry Reding 
749f315828bSThierry Reding 	value = afi_readl(pcie, AFI_PCIE_CONFIG);
750f315828bSThierry Reding 	value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
751f315828bSThierry Reding 	value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
752f315828bSThierry Reding 
753f315828bSThierry Reding 	list_for_each_entry(port, &pcie->ports, list)
754f315828bSThierry Reding 		value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
755f315828bSThierry Reding 
756f315828bSThierry Reding 	afi_writel(pcie, value, AFI_PCIE_CONFIG);
757f315828bSThierry Reding 
758f315828bSThierry Reding 	value = afi_readl(pcie, AFI_FUSE);
759f315828bSThierry Reding 
760f315828bSThierry Reding 	if (soc->has_gen2)
761f315828bSThierry Reding 		value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
762f315828bSThierry Reding 	else
763f315828bSThierry Reding 		value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
764f315828bSThierry Reding 
765f315828bSThierry Reding 	afi_writel(pcie, value, AFI_FUSE);
766f315828bSThierry Reding 
767bbc5b36bSStephen Warren #ifndef CONFIG_TEGRA186
768f315828bSThierry Reding 	if (pcie->phy)
769f315828bSThierry Reding 		err = tegra_xusb_phy_enable(pcie->phy);
770f315828bSThierry Reding 	else
771f315828bSThierry Reding 		err = tegra_pcie_phy_enable(pcie);
772f315828bSThierry Reding 
773f315828bSThierry Reding 	if (err < 0) {
774*90aa625cSMasahiro Yamada 		pr_err("failed to power on PHY: %d\n", err);
775f315828bSThierry Reding 		return err;
776f315828bSThierry Reding 	}
777bbc5b36bSStephen Warren #endif
778f315828bSThierry Reding 
779f315828bSThierry Reding 	/* take the PCIEXCLK logic out of reset */
780bbc5b36bSStephen Warren #ifdef CONFIG_TEGRA186
781bbc5b36bSStephen Warren 	err = reset_deassert(&pcie->reset_pcie_x);
782bbc5b36bSStephen Warren 	if (err) {
783*90aa625cSMasahiro Yamada 		pr_err("reset_deassert(pcie_x) failed: %d\n", err);
784bbc5b36bSStephen Warren 		return err;
785bbc5b36bSStephen Warren 	}
786bbc5b36bSStephen Warren #else
787f315828bSThierry Reding 	reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
788bbc5b36bSStephen Warren #endif
789f315828bSThierry Reding 
790f315828bSThierry Reding 	/* finally enable PCIe */
791f315828bSThierry Reding 	value = afi_readl(pcie, AFI_CONFIGURATION);
792f315828bSThierry Reding 	value |= AFI_CONFIGURATION_EN_FPCI;
793f315828bSThierry Reding 	afi_writel(pcie, value, AFI_CONFIGURATION);
794f315828bSThierry Reding 
795f315828bSThierry Reding 	/* disable all interrupts */
796f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
797f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
798f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_INTR_MASK);
799f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
800f315828bSThierry Reding 
801f315828bSThierry Reding 	return 0;
802f315828bSThierry Reding }
803f315828bSThierry Reding 
804e81ca884SSimon Glass static int tegra_pcie_setup_translations(struct udevice *bus)
805f315828bSThierry Reding {
806e81ca884SSimon Glass 	struct tegra_pcie *pcie = dev_get_priv(bus);
807f315828bSThierry Reding 	unsigned long fpci, axi, size;
808e81ca884SSimon Glass 	struct pci_region *io, *mem, *pref;
809e81ca884SSimon Glass 	int count;
810f315828bSThierry Reding 
811f315828bSThierry Reding 	/* BAR 0: type 1 extended configuration space */
812f315828bSThierry Reding 	fpci = 0xfe100000;
81368f00811SSimon Glass 	size = resource_size(&pcie->cs);
814f315828bSThierry Reding 	axi = pcie->cs.start;
815f315828bSThierry Reding 
816f315828bSThierry Reding 	afi_writel(pcie, axi, AFI_AXI_BAR0_START);
817f315828bSThierry Reding 	afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
818f315828bSThierry Reding 	afi_writel(pcie, fpci, AFI_FPCI_BAR0);
819f315828bSThierry Reding 
820e81ca884SSimon Glass 	count = pci_get_regions(bus, &io, &mem, &pref);
821e81ca884SSimon Glass 	if (count != 3)
822e81ca884SSimon Glass 		return -EINVAL;
823e81ca884SSimon Glass 
824f315828bSThierry Reding 	/* BAR 1: downstream I/O */
825f315828bSThierry Reding 	fpci = 0xfdfc0000;
826e81ca884SSimon Glass 	size = io->size;
827e81ca884SSimon Glass 	axi = io->phys_start;
828f315828bSThierry Reding 
829f315828bSThierry Reding 	afi_writel(pcie, axi, AFI_AXI_BAR1_START);
830f315828bSThierry Reding 	afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
831f315828bSThierry Reding 	afi_writel(pcie, fpci, AFI_FPCI_BAR1);
832f315828bSThierry Reding 
833f315828bSThierry Reding 	/* BAR 2: prefetchable memory */
834e81ca884SSimon Glass 	fpci = (((pref->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
835e81ca884SSimon Glass 	size = pref->size;
836e81ca884SSimon Glass 	axi = pref->phys_start;
837f315828bSThierry Reding 
838f315828bSThierry Reding 	afi_writel(pcie, axi, AFI_AXI_BAR2_START);
839f315828bSThierry Reding 	afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
840f315828bSThierry Reding 	afi_writel(pcie, fpci, AFI_FPCI_BAR2);
841f315828bSThierry Reding 
842f315828bSThierry Reding 	/* BAR 3: non-prefetchable memory */
843e81ca884SSimon Glass 	fpci = (((mem->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
844e81ca884SSimon Glass 	size = mem->size;
845e81ca884SSimon Glass 	axi = mem->phys_start;
846f315828bSThierry Reding 
847f315828bSThierry Reding 	afi_writel(pcie, axi, AFI_AXI_BAR3_START);
848f315828bSThierry Reding 	afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
849f315828bSThierry Reding 	afi_writel(pcie, fpci, AFI_FPCI_BAR3);
850f315828bSThierry Reding 
851f315828bSThierry Reding 	/* NULL out the remaining BARs as they are not used */
852f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_AXI_BAR4_START);
853f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
854f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_FPCI_BAR4);
855f315828bSThierry Reding 
856f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_AXI_BAR5_START);
857f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
858f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_FPCI_BAR5);
859f315828bSThierry Reding 
860f315828bSThierry Reding 	/* map all upstream transactions as uncached */
861f315828bSThierry Reding 	afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
862f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
863f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
864f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
865f315828bSThierry Reding 
866f315828bSThierry Reding 	/* MSI translations are setup only when needed */
867f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
868f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
869f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
870f315828bSThierry Reding 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
871e81ca884SSimon Glass 
872e81ca884SSimon Glass 	return 0;
873f315828bSThierry Reding }
874f315828bSThierry Reding 
875f315828bSThierry Reding static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
876f315828bSThierry Reding {
877f315828bSThierry Reding 	unsigned long ret = 0;
878f315828bSThierry Reding 
879f315828bSThierry Reding 	switch (port->index) {
880f315828bSThierry Reding 	case 0:
881f315828bSThierry Reding 		ret = AFI_PEX0_CTRL;
882f315828bSThierry Reding 		break;
883f315828bSThierry Reding 
884f315828bSThierry Reding 	case 1:
885f315828bSThierry Reding 		ret = AFI_PEX1_CTRL;
886f315828bSThierry Reding 		break;
887f315828bSThierry Reding 
888f315828bSThierry Reding 	case 2:
889bbc5b36bSStephen Warren 		ret = port->pcie->soc->afi_pex2_ctrl;
890f315828bSThierry Reding 		break;
891f315828bSThierry Reding 	}
892f315828bSThierry Reding 
893f315828bSThierry Reding 	return ret;
894f315828bSThierry Reding }
895f315828bSThierry Reding 
896f315828bSThierry Reding static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
897f315828bSThierry Reding {
898f315828bSThierry Reding 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
899f315828bSThierry Reding 	unsigned long value;
900f315828bSThierry Reding 
901f315828bSThierry Reding 	/* pulse reset signel */
902f315828bSThierry Reding 	value = afi_readl(port->pcie, ctrl);
903f315828bSThierry Reding 	value &= ~AFI_PEX_CTRL_RST;
904f315828bSThierry Reding 	afi_writel(port->pcie, value, ctrl);
905f315828bSThierry Reding 
906f315828bSThierry Reding 	udelay(2000);
907f315828bSThierry Reding 
908f315828bSThierry Reding 	value = afi_readl(port->pcie, ctrl);
909f315828bSThierry Reding 	value |= AFI_PEX_CTRL_RST;
910f315828bSThierry Reding 	afi_writel(port->pcie, value, ctrl);
911f315828bSThierry Reding }
912f315828bSThierry Reding 
913f315828bSThierry Reding static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
914f315828bSThierry Reding {
915f39a6a32SStephen Warren 	struct tegra_pcie *pcie = port->pcie;
916f39a6a32SStephen Warren 	const struct tegra_pcie_soc *soc = pcie->soc;
917f315828bSThierry Reding 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
918f315828bSThierry Reding 	unsigned long value;
919f315828bSThierry Reding 
920f315828bSThierry Reding 	/* enable reference clock */
921f39a6a32SStephen Warren 	value = afi_readl(pcie, ctrl);
922f315828bSThierry Reding 	value |= AFI_PEX_CTRL_REFCLK_EN;
923f315828bSThierry Reding 
924f39a6a32SStephen Warren 	if (pcie->soc->has_pex_clkreq_en)
925f315828bSThierry Reding 		value |= AFI_PEX_CTRL_CLKREQ_EN;
926f315828bSThierry Reding 
927f315828bSThierry Reding 	value |= AFI_PEX_CTRL_OVERRIDE_EN;
928f315828bSThierry Reding 
929f39a6a32SStephen Warren 	afi_writel(pcie, value, ctrl);
930f315828bSThierry Reding 
931f315828bSThierry Reding 	tegra_pcie_port_reset(port);
932514e1913SStephen Warren 
933514e1913SStephen Warren 	if (soc->force_pca_enable) {
934514e1913SStephen Warren 		value = rp_readl(port, RP_VEND_CTL2);
935514e1913SStephen Warren 		value |= RP_VEND_CTL2_PCA_ENABLE;
936514e1913SStephen Warren 		rp_writel(port, value, RP_VEND_CTL2);
937514e1913SStephen Warren 	}
938f39a6a32SStephen Warren 
939f39a6a32SStephen Warren 	/* configure the reference clock driver */
940f39a6a32SStephen Warren 	pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
941f39a6a32SStephen Warren 	if (soc->num_ports > 2)
942f39a6a32SStephen Warren 		pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
943f315828bSThierry Reding }
944f315828bSThierry Reding 
945f315828bSThierry Reding static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
946f315828bSThierry Reding {
947f315828bSThierry Reding 	unsigned int retries = 3;
948f315828bSThierry Reding 	unsigned long value;
949f315828bSThierry Reding 
950f315828bSThierry Reding 	value = rp_readl(port, RP_PRIV_MISC);
951f315828bSThierry Reding 	value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
952f315828bSThierry Reding 	value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
953f315828bSThierry Reding 	rp_writel(port, value, RP_PRIV_MISC);
954f315828bSThierry Reding 
955f315828bSThierry Reding 	do {
956f315828bSThierry Reding 		unsigned int timeout = 200;
957f315828bSThierry Reding 
958f315828bSThierry Reding 		do {
959f315828bSThierry Reding 			value = rp_readl(port, RP_VEND_XP);
960f315828bSThierry Reding 			if (value & RP_VEND_XP_DL_UP)
961f315828bSThierry Reding 				break;
962f315828bSThierry Reding 
963f315828bSThierry Reding 			udelay(2000);
964f315828bSThierry Reding 		} while (--timeout);
965f315828bSThierry Reding 
966f315828bSThierry Reding 		if (!timeout) {
967f315828bSThierry Reding 			debug("link %u down, retrying\n", port->index);
968f315828bSThierry Reding 			goto retry;
969f315828bSThierry Reding 		}
970f315828bSThierry Reding 
971f315828bSThierry Reding 		timeout = 200;
972f315828bSThierry Reding 
973f315828bSThierry Reding 		do {
974f315828bSThierry Reding 			value = rp_readl(port, RP_LINK_CONTROL_STATUS);
975f315828bSThierry Reding 			if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
976f315828bSThierry Reding 				return true;
977f315828bSThierry Reding 
978f315828bSThierry Reding 			udelay(2000);
979f315828bSThierry Reding 		} while (--timeout);
980f315828bSThierry Reding 
981f315828bSThierry Reding retry:
982f315828bSThierry Reding 		tegra_pcie_port_reset(port);
983f315828bSThierry Reding 	} while (--retries);
984f315828bSThierry Reding 
985f315828bSThierry Reding 	return false;
986f315828bSThierry Reding }
987f315828bSThierry Reding 
988f315828bSThierry Reding static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
989f315828bSThierry Reding {
990f315828bSThierry Reding 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
991f315828bSThierry Reding 	unsigned long value;
992f315828bSThierry Reding 
993f315828bSThierry Reding 	/* assert port reset */
994f315828bSThierry Reding 	value = afi_readl(port->pcie, ctrl);
995f315828bSThierry Reding 	value &= ~AFI_PEX_CTRL_RST;
996f315828bSThierry Reding 	afi_writel(port->pcie, value, ctrl);
997f315828bSThierry Reding 
998f315828bSThierry Reding 	/* disable reference clock */
999f315828bSThierry Reding 	value = afi_readl(port->pcie, ctrl);
1000f315828bSThierry Reding 	value &= ~AFI_PEX_CTRL_REFCLK_EN;
1001f315828bSThierry Reding 	afi_writel(port->pcie, value, ctrl);
1002f315828bSThierry Reding }
1003f315828bSThierry Reding 
1004f315828bSThierry Reding static void tegra_pcie_port_free(struct tegra_pcie_port *port)
1005f315828bSThierry Reding {
1006f315828bSThierry Reding 	list_del(&port->list);
1007f315828bSThierry Reding 	free(port);
1008f315828bSThierry Reding }
1009f315828bSThierry Reding 
1010f315828bSThierry Reding static int tegra_pcie_enable(struct tegra_pcie *pcie)
1011f315828bSThierry Reding {
1012f315828bSThierry Reding 	struct tegra_pcie_port *port, *tmp;
1013f315828bSThierry Reding 
1014f315828bSThierry Reding 	list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1015f315828bSThierry Reding 		debug("probing port %u, using %u lanes\n", port->index,
1016f315828bSThierry Reding 		      port->num_lanes);
1017f315828bSThierry Reding 
1018f315828bSThierry Reding 		tegra_pcie_port_enable(port);
1019f315828bSThierry Reding 
1020f315828bSThierry Reding 		if (tegra_pcie_port_check_link(port))
1021f315828bSThierry Reding 			continue;
1022f315828bSThierry Reding 
1023f315828bSThierry Reding 		debug("link %u down, ignoring\n", port->index);
1024f315828bSThierry Reding 
1025f315828bSThierry Reding 		tegra_pcie_port_disable(port);
1026f315828bSThierry Reding 		tegra_pcie_port_free(port);
1027f315828bSThierry Reding 	}
1028f315828bSThierry Reding 
1029f315828bSThierry Reding 	return 0;
1030f315828bSThierry Reding }
1031f315828bSThierry Reding 
1032e81ca884SSimon Glass static const struct tegra_pcie_soc pci_tegra_soc[] = {
1033e81ca884SSimon Glass 	[TEGRA20_PCIE] = {
1034f315828bSThierry Reding 		.num_ports = 2,
1035f315828bSThierry Reding 		.pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1036f315828bSThierry Reding 		.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
10373cfc6be4SStephen Warren 		.pads_refclk_cfg0 = 0xfa5cfa5c,
1038f315828bSThierry Reding 		.has_pex_clkreq_en = false,
1039f315828bSThierry Reding 		.has_pex_bias_ctrl = false,
1040f315828bSThierry Reding 		.has_cml_clk = false,
1041f315828bSThierry Reding 		.has_gen2 = false,
1042e81ca884SSimon Glass 	},
1043e81ca884SSimon Glass 	[TEGRA30_PCIE] = {
1044f315828bSThierry Reding 		.num_ports = 3,
1045f315828bSThierry Reding 		.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1046f315828bSThierry Reding 		.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1047bbc5b36bSStephen Warren 		.afi_pex2_ctrl = AFI_PEX2_CTRL,
10483cfc6be4SStephen Warren 		.pads_refclk_cfg0 = 0xfa5cfa5c,
10493cfc6be4SStephen Warren 		.pads_refclk_cfg1 = 0xfa5cfa5c,
1050f315828bSThierry Reding 		.has_pex_clkreq_en = true,
1051f315828bSThierry Reding 		.has_pex_bias_ctrl = true,
1052f315828bSThierry Reding 		.has_cml_clk = true,
1053f315828bSThierry Reding 		.has_gen2 = false,
1054e81ca884SSimon Glass 	},
1055e81ca884SSimon Glass 	[TEGRA124_PCIE] = {
1056f315828bSThierry Reding 		.num_ports = 2,
1057f315828bSThierry Reding 		.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1058f315828bSThierry Reding 		.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
10593cfc6be4SStephen Warren 		.pads_refclk_cfg0 = 0x44ac44ac,
1060f315828bSThierry Reding 		.has_pex_clkreq_en = true,
1061f315828bSThierry Reding 		.has_pex_bias_ctrl = true,
1062f315828bSThierry Reding 		.has_cml_clk = true,
1063f315828bSThierry Reding 		.has_gen2 = true,
1064e81ca884SSimon Glass 	},
1065e81ca884SSimon Glass 	[TEGRA210_PCIE] = {
1066d9eda6c4SStephen Warren 		.num_ports = 2,
1067d9eda6c4SStephen Warren 		.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1068d9eda6c4SStephen Warren 		.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
10693cfc6be4SStephen Warren 		.pads_refclk_cfg0 = 0x90b890b8,
1070d9eda6c4SStephen Warren 		.has_pex_clkreq_en = true,
1071d9eda6c4SStephen Warren 		.has_pex_bias_ctrl = true,
1072d9eda6c4SStephen Warren 		.has_cml_clk = true,
1073d9eda6c4SStephen Warren 		.has_gen2 = true,
1074d9eda6c4SStephen Warren 		.force_pca_enable = true,
1075bbc5b36bSStephen Warren 	},
1076bbc5b36bSStephen Warren 	[TEGRA186_PCIE] = {
1077bbc5b36bSStephen Warren 		.num_ports = 3,
1078bbc5b36bSStephen Warren 		.afi_pex2_ctrl = AFI_PEX2_CTRL_T186,
1079bbc5b36bSStephen Warren 		.pads_refclk_cfg0 = 0x80b880b8,
1080bbc5b36bSStephen Warren 		.pads_refclk_cfg1 = 0x000480b8,
1081bbc5b36bSStephen Warren 		.has_pex_clkreq_en = true,
1082bbc5b36bSStephen Warren 		.has_pex_bias_ctrl = true,
1083bbc5b36bSStephen Warren 		.has_gen2 = true,
1084bbc5b36bSStephen Warren 	},
1085d9eda6c4SStephen Warren };
1086d9eda6c4SStephen Warren 
1087e81ca884SSimon Glass static int pci_tegra_ofdata_to_platdata(struct udevice *dev)
1088f315828bSThierry Reding {
1089e81ca884SSimon Glass 	struct tegra_pcie *pcie = dev_get_priv(dev);
1090e81ca884SSimon Glass 	enum tegra_pci_id id;
1091bec05246SStephen Warren 
1092e81ca884SSimon Glass 	id = dev_get_driver_data(dev);
1093e81ca884SSimon Glass 	pcie->soc = &pci_tegra_soc[id];
1094f315828bSThierry Reding 
1095f315828bSThierry Reding 	INIT_LIST_HEAD(&pcie->ports);
1096f315828bSThierry Reding 
109768f00811SSimon Glass 	if (tegra_pcie_parse_dt(dev, id, pcie))
1098e81ca884SSimon Glass 		return -EINVAL;
1099e81ca884SSimon Glass 
1100e81ca884SSimon Glass 	return 0;
1101f315828bSThierry Reding }
1102f315828bSThierry Reding 
1103e81ca884SSimon Glass static int pci_tegra_probe(struct udevice *dev)
1104e81ca884SSimon Glass {
1105e81ca884SSimon Glass 	struct tegra_pcie *pcie = dev_get_priv(dev);
1106e81ca884SSimon Glass 	int err;
1107e81ca884SSimon Glass 
1108bbc5b36bSStephen Warren #ifdef CONFIG_TEGRA186
1109bbc5b36bSStephen Warren 	err = clk_get_by_name(dev, "afi", &pcie->clk_afi);
1110bbc5b36bSStephen Warren 	if (err) {
1111bbc5b36bSStephen Warren 		debug("clk_get_by_name(afi) failed: %d\n", err);
1112bbc5b36bSStephen Warren 		return err;
1113bbc5b36bSStephen Warren 	}
1114bbc5b36bSStephen Warren 
1115bbc5b36bSStephen Warren 	err = clk_get_by_name(dev, "pex", &pcie->clk_pex);
1116bbc5b36bSStephen Warren 	if (err) {
1117bbc5b36bSStephen Warren 		debug("clk_get_by_name(pex) failed: %d\n", err);
1118bbc5b36bSStephen Warren 		return err;
1119bbc5b36bSStephen Warren 	}
1120bbc5b36bSStephen Warren 
1121bbc5b36bSStephen Warren 	err = reset_get_by_name(dev, "afi", &pcie->reset_afi);
1122bbc5b36bSStephen Warren 	if (err) {
1123bbc5b36bSStephen Warren 		debug("reset_get_by_name(afi) failed: %d\n", err);
1124bbc5b36bSStephen Warren 		return err;
1125bbc5b36bSStephen Warren 	}
1126bbc5b36bSStephen Warren 
1127bbc5b36bSStephen Warren 	err = reset_get_by_name(dev, "pex", &pcie->reset_pex);
1128bbc5b36bSStephen Warren 	if (err) {
1129bbc5b36bSStephen Warren 		debug("reset_get_by_name(pex) failed: %d\n", err);
1130bbc5b36bSStephen Warren 		return err;
1131bbc5b36bSStephen Warren 	}
1132bbc5b36bSStephen Warren 
1133bbc5b36bSStephen Warren 	err = reset_get_by_name(dev, "pcie_x", &pcie->reset_pcie_x);
1134bbc5b36bSStephen Warren 	if (err) {
1135bbc5b36bSStephen Warren 		debug("reset_get_by_name(pcie_x) failed: %d\n", err);
1136bbc5b36bSStephen Warren 		return err;
1137bbc5b36bSStephen Warren 	}
1138bbc5b36bSStephen Warren 
1139bbc5b36bSStephen Warren 	err = power_domain_get(dev, &pcie->pwrdom);
1140bbc5b36bSStephen Warren 	if (err) {
1141bbc5b36bSStephen Warren 		debug("power_domain_get() failed: %d\n", err);
1142bbc5b36bSStephen Warren 		return err;
1143bbc5b36bSStephen Warren 	}
1144bbc5b36bSStephen Warren #endif
1145bbc5b36bSStephen Warren 
1146f315828bSThierry Reding 	err = tegra_pcie_power_on(pcie);
1147f315828bSThierry Reding 	if (err < 0) {
1148*90aa625cSMasahiro Yamada 		pr_err("failed to power on");
1149e81ca884SSimon Glass 		return err;
1150f315828bSThierry Reding 	}
1151f315828bSThierry Reding 
1152f315828bSThierry Reding 	err = tegra_pcie_enable_controller(pcie);
1153f315828bSThierry Reding 	if (err < 0) {
1154*90aa625cSMasahiro Yamada 		pr_err("failed to enable controller");
1155e81ca884SSimon Glass 		return err;
1156f315828bSThierry Reding 	}
1157f315828bSThierry Reding 
1158e81ca884SSimon Glass 	err = tegra_pcie_setup_translations(dev);
1159e81ca884SSimon Glass 	if (err < 0) {
1160*90aa625cSMasahiro Yamada 		pr_err("failed to decode ranges");
1161e81ca884SSimon Glass 		return err;
1162e81ca884SSimon Glass 	}
1163f315828bSThierry Reding 
1164f315828bSThierry Reding 	err = tegra_pcie_enable(pcie);
1165f315828bSThierry Reding 	if (err < 0) {
1166*90aa625cSMasahiro Yamada 		pr_err("failed to enable PCIe");
1167e81ca884SSimon Glass 		return err;
1168f315828bSThierry Reding 	}
1169f315828bSThierry Reding 
1170f315828bSThierry Reding 	return 0;
1171f315828bSThierry Reding }
1172f315828bSThierry Reding 
1173e81ca884SSimon Glass static const struct dm_pci_ops pci_tegra_ops = {
1174e81ca884SSimon Glass 	.read_config	= pci_tegra_read_config,
1175e81ca884SSimon Glass 	.write_config	= pci_tegra_write_config,
1176e81ca884SSimon Glass };
1177f315828bSThierry Reding 
1178e81ca884SSimon Glass static const struct udevice_id pci_tegra_ids[] = {
1179e81ca884SSimon Glass 	{ .compatible = "nvidia,tegra20-pcie", .data = TEGRA20_PCIE },
1180e81ca884SSimon Glass 	{ .compatible = "nvidia,tegra30-pcie", .data = TEGRA30_PCIE },
1181e81ca884SSimon Glass 	{ .compatible = "nvidia,tegra124-pcie", .data = TEGRA124_PCIE },
1182e81ca884SSimon Glass 	{ .compatible = "nvidia,tegra210-pcie", .data = TEGRA210_PCIE },
1183bbc5b36bSStephen Warren 	{ .compatible = "nvidia,tegra186-pcie", .data = TEGRA186_PCIE },
1184e81ca884SSimon Glass 	{ }
1185e81ca884SSimon Glass };
1186a02e2635SStephen Warren 
1187e81ca884SSimon Glass U_BOOT_DRIVER(pci_tegra) = {
1188e81ca884SSimon Glass 	.name	= "pci_tegra",
1189e81ca884SSimon Glass 	.id	= UCLASS_PCI,
1190e81ca884SSimon Glass 	.of_match = pci_tegra_ids,
1191e81ca884SSimon Glass 	.ops	= &pci_tegra_ops,
1192e81ca884SSimon Glass 	.ofdata_to_platdata = pci_tegra_ofdata_to_platdata,
1193e81ca884SSimon Glass 	.probe	= pci_tegra_probe,
1194e81ca884SSimon Glass 	.priv_auto_alloc_size = sizeof(struct tegra_pcie),
1195e81ca884SSimon Glass };
1196