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