blob: 3b4b276abe4452a99e6083dc48a26cdef3599678 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Thierry Reding412c7582014-12-09 22:25:12 -07002/*
3 * Copyright (c) 2010, CompuLab, Ltd.
4 * Author: Mike Rapoport <mike@compulab.co.il>
5 *
6 * Based on NVIDIA PCIe driver
7 * Copyright (c) 2008-2009, NVIDIA Corporation.
8 *
9 * Copyright (c) 2013-2014, NVIDIA Corporation.
Thierry Reding412c7582014-12-09 22:25:12 -070010 */
11
Thierry Reding412c7582014-12-09 22:25:12 -070012#define pr_fmt(fmt) "tegra-pcie: " fmt
13
14#include <common.h>
Stephen Warren86f6a942016-08-05 16:10:34 -060015#include <clk.h>
Simon Glass46fcfc12015-11-19 20:27:02 -070016#include <dm.h>
Thierry Reding412c7582014-12-09 22:25:12 -070017#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Thierry Reding412c7582014-12-09 22:25:12 -070019#include <malloc.h>
20#include <pci.h>
Marcel Ziswiler53f48632018-05-08 17:34:09 +020021#include <pci_tegra.h>
Stephen Warren86f6a942016-08-05 16:10:34 -060022#include <power-domain.h>
23#include <reset.h>
Thierry Reding412c7582014-12-09 22:25:12 -070024
25#include <asm/io.h>
26#include <asm/gpio.h>
27
Simon Glasse6a30e02017-07-25 08:30:09 -060028#include <linux/ioport.h>
Stephen Warren86f6a942016-08-05 16:10:34 -060029#include <linux/list.h>
30
31#ifndef CONFIG_TEGRA186
Thierry Reding412c7582014-12-09 22:25:12 -070032#include <asm/arch/clock.h>
33#include <asm/arch/powergate.h>
34#include <asm/arch-tegra/xusb-padctl.h>
Thierry Reding412c7582014-12-09 22:25:12 -070035#include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
Stephen Warren86f6a942016-08-05 16:10:34 -060036#endif
37
38/*
39 * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
40 * should not be present. These are needed because newer Tegra SoCs support
41 * only the standard clock/reset APIs, whereas older Tegra SoCs support only
42 * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
43 * fixed to implement the standard APIs, and all drivers converted to solely
44 * use the new standard APIs, with no ifdefs.
45 */
Thierry Reding412c7582014-12-09 22:25:12 -070046
Thierry Reding412c7582014-12-09 22:25:12 -070047#define AFI_AXI_BAR0_SZ 0x00
48#define AFI_AXI_BAR1_SZ 0x04
49#define AFI_AXI_BAR2_SZ 0x08
50#define AFI_AXI_BAR3_SZ 0x0c
51#define AFI_AXI_BAR4_SZ 0x10
52#define AFI_AXI_BAR5_SZ 0x14
53
54#define AFI_AXI_BAR0_START 0x18
55#define AFI_AXI_BAR1_START 0x1c
56#define AFI_AXI_BAR2_START 0x20
57#define AFI_AXI_BAR3_START 0x24
58#define AFI_AXI_BAR4_START 0x28
59#define AFI_AXI_BAR5_START 0x2c
60
61#define AFI_FPCI_BAR0 0x30
62#define AFI_FPCI_BAR1 0x34
63#define AFI_FPCI_BAR2 0x38
64#define AFI_FPCI_BAR3 0x3c
65#define AFI_FPCI_BAR4 0x40
66#define AFI_FPCI_BAR5 0x44
67
68#define AFI_CACHE_BAR0_SZ 0x48
69#define AFI_CACHE_BAR0_ST 0x4c
70#define AFI_CACHE_BAR1_SZ 0x50
71#define AFI_CACHE_BAR1_ST 0x54
72
73#define AFI_MSI_BAR_SZ 0x60
74#define AFI_MSI_FPCI_BAR_ST 0x64
75#define AFI_MSI_AXI_BAR_ST 0x68
76
77#define AFI_CONFIGURATION 0xac
78#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
79
80#define AFI_FPCI_ERROR_MASKS 0xb0
81
82#define AFI_INTR_MASK 0xb4
83#define AFI_INTR_MASK_INT_MASK (1 << 0)
84#define AFI_INTR_MASK_MSI_MASK (1 << 8)
85
86#define AFI_SM_INTR_ENABLE 0xc4
87#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
88#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
89#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
90#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
91#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
92#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
93#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
94#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
95
96#define AFI_AFI_INTR_ENABLE 0xc8
97#define AFI_INTR_EN_INI_SLVERR (1 << 0)
98#define AFI_INTR_EN_INI_DECERR (1 << 1)
99#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
100#define AFI_INTR_EN_TGT_DECERR (1 << 3)
101#define AFI_INTR_EN_TGT_WRERR (1 << 4)
102#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
103#define AFI_INTR_EN_AXI_DECERR (1 << 6)
104#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
105#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
106
107#define AFI_PCIE_CONFIG 0x0f8
108#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
109#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
110#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
111#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
112#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
113#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
114#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
115#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
116#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
117#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Stephen Warren86f6a942016-08-05 16:10:34 -0600118#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_401 (0x0 << 20)
119#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_211 (0x1 << 20)
120#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_111 (0x2 << 20)
Thierry Reding412c7582014-12-09 22:25:12 -0700121
122#define AFI_FUSE 0x104
123#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
124
125#define AFI_PEX0_CTRL 0x110
126#define AFI_PEX1_CTRL 0x118
127#define AFI_PEX2_CTRL 0x128
Stephen Warren86f6a942016-08-05 16:10:34 -0600128#define AFI_PEX2_CTRL_T186 0x19c
Thierry Reding412c7582014-12-09 22:25:12 -0700129#define AFI_PEX_CTRL_RST (1 << 0)
130#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
131#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
132#define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
133
134#define AFI_PLLE_CONTROL 0x160
135#define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
136#define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
137
138#define AFI_PEXBIAS_CTRL_0 0x168
139
140#define PADS_CTL_SEL 0x0000009C
141
142#define PADS_CTL 0x000000A0
143#define PADS_CTL_IDDQ_1L (1 << 0)
144#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
145#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
146
147#define PADS_PLL_CTL_TEGRA20 0x000000B8
148#define PADS_PLL_CTL_TEGRA30 0x000000B4
149#define PADS_PLL_CTL_RST_B4SM (0x1 << 1)
150#define PADS_PLL_CTL_LOCKDET (0x1 << 8)
151#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
152#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0x0 << 16)
153#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (0x1 << 16)
154#define PADS_PLL_CTL_REFCLK_EXTERNAL (0x2 << 16)
155#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
156#define PADS_PLL_CTL_TXCLKREF_DIV10 (0x0 << 20)
157#define PADS_PLL_CTL_TXCLKREF_DIV5 (0x1 << 20)
158#define PADS_PLL_CTL_TXCLKREF_BUF_EN (0x1 << 22)
159
160#define PADS_REFCLK_CFG0 0x000000C8
161#define PADS_REFCLK_CFG1 0x000000CC
162
163/*
164 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
165 * entries, one entry per PCIe port. These field definitions and desired
166 * values aren't in the TRM, but do come from NVIDIA.
167 */
168#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
169#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
170#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
171#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
172
Thierry Reding412c7582014-12-09 22:25:12 -0700173#define RP_VEND_XP 0x00000F00
174#define RP_VEND_XP_DL_UP (1 << 30)
175
Stephen Warrene3553be2015-10-05 17:00:42 -0600176#define RP_VEND_CTL2 0x00000FA8
177#define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
178
Thierry Reding412c7582014-12-09 22:25:12 -0700179#define RP_PRIV_MISC 0x00000FE0
180#define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
181#define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
182
183#define RP_LINK_CONTROL_STATUS 0x00000090
184#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
185#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
186
Simon Glass46fcfc12015-11-19 20:27:02 -0700187enum tegra_pci_id {
188 TEGRA20_PCIE,
189 TEGRA30_PCIE,
190 TEGRA124_PCIE,
191 TEGRA210_PCIE,
Stephen Warren86f6a942016-08-05 16:10:34 -0600192 TEGRA186_PCIE,
Simon Glass46fcfc12015-11-19 20:27:02 -0700193};
Thierry Reding412c7582014-12-09 22:25:12 -0700194
195struct tegra_pcie_port {
196 struct tegra_pcie *pcie;
197
198 struct fdt_resource regs;
199 unsigned int num_lanes;
200 unsigned int index;
201
202 struct list_head list;
203};
204
205struct tegra_pcie_soc {
206 unsigned int num_ports;
207 unsigned long pads_pll_ctl;
208 unsigned long tx_ref_sel;
Stephen Warren86f6a942016-08-05 16:10:34 -0600209 unsigned long afi_pex2_ctrl;
Stephen Warrend132c472016-06-21 12:47:51 -0600210 u32 pads_refclk_cfg0;
211 u32 pads_refclk_cfg1;
Thierry Reding412c7582014-12-09 22:25:12 -0700212 bool has_pex_clkreq_en;
213 bool has_pex_bias_ctrl;
214 bool has_cml_clk;
215 bool has_gen2;
Stephen Warrene3553be2015-10-05 17:00:42 -0600216 bool force_pca_enable;
Thierry Reding412c7582014-12-09 22:25:12 -0700217};
218
219struct tegra_pcie {
Simon Glasse6a30e02017-07-25 08:30:09 -0600220 struct resource pads;
221 struct resource afi;
222 struct resource cs;
Thierry Reding412c7582014-12-09 22:25:12 -0700223
Thierry Reding412c7582014-12-09 22:25:12 -0700224 struct list_head ports;
225 unsigned long xbar;
226
227 const struct tegra_pcie_soc *soc;
Stephen Warren86f6a942016-08-05 16:10:34 -0600228
229#ifdef CONFIG_TEGRA186
230 struct clk clk_afi;
231 struct clk clk_pex;
232 struct reset_ctl reset_afi;
233 struct reset_ctl reset_pex;
234 struct reset_ctl reset_pcie_x;
235 struct power_domain pwrdom;
236#else
Thierry Reding412c7582014-12-09 22:25:12 -0700237 struct tegra_xusb_phy *phy;
Stephen Warren86f6a942016-08-05 16:10:34 -0600238#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700239};
240
Thierry Reding412c7582014-12-09 22:25:12 -0700241static void afi_writel(struct tegra_pcie *pcie, unsigned long value,
242 unsigned long offset)
243{
244 writel(value, pcie->afi.start + offset);
245}
246
247static unsigned long afi_readl(struct tegra_pcie *pcie, unsigned long offset)
248{
249 return readl(pcie->afi.start + offset);
250}
251
252static void pads_writel(struct tegra_pcie *pcie, unsigned long value,
253 unsigned long offset)
254{
255 writel(value, pcie->pads.start + offset);
256}
257
Stephen Warren86f6a942016-08-05 16:10:34 -0600258#ifndef CONFIG_TEGRA186
Thierry Reding412c7582014-12-09 22:25:12 -0700259static unsigned long pads_readl(struct tegra_pcie *pcie, unsigned long offset)
260{
261 return readl(pcie->pads.start + offset);
262}
Stephen Warren86f6a942016-08-05 16:10:34 -0600263#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700264
265static unsigned long rp_readl(struct tegra_pcie_port *port,
266 unsigned long offset)
267{
268 return readl(port->regs.start + offset);
269}
270
271static void rp_writel(struct tegra_pcie_port *port, unsigned long value,
272 unsigned long offset)
273{
274 writel(value, port->regs.start + offset);
275}
276
277static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where)
278{
279 return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) |
280 (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) |
281 (where & 0xfc);
282}
283
284static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf,
285 int where, unsigned long *address)
286{
287 unsigned int bus = PCI_BUS(bdf);
288
289 if (bus == 0) {
290 unsigned int dev = PCI_DEV(bdf);
291 struct tegra_pcie_port *port;
292
293 list_for_each_entry(port, &pcie->ports, list) {
294 if (port->index + 1 == dev) {
295 *address = port->regs.start + (where & ~3);
296 return 0;
297 }
298 }
Stephen Warrenbbe71b22016-04-20 15:46:50 -0600299 return -EFAULT;
Thierry Reding412c7582014-12-09 22:25:12 -0700300 } else {
Stephen Warrenbbe71b22016-04-20 15:46:50 -0600301#ifdef CONFIG_TEGRA20
302 unsigned int dev = PCI_DEV(bdf);
303 if (dev != 0)
304 return -EFAULT;
305#endif
306
Thierry Reding412c7582014-12-09 22:25:12 -0700307 *address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where);
308 return 0;
309 }
Thierry Reding412c7582014-12-09 22:25:12 -0700310}
311
Simon Glass2a311e82020-01-27 08:49:37 -0700312static int pci_tegra_read_config(const struct udevice *bus, pci_dev_t bdf,
Simon Glass46fcfc12015-11-19 20:27:02 -0700313 uint offset, ulong *valuep,
314 enum pci_size_t size)
Thierry Reding412c7582014-12-09 22:25:12 -0700315{
Simon Glass46fcfc12015-11-19 20:27:02 -0700316 struct tegra_pcie *pcie = dev_get_priv(bus);
317 unsigned long address, value;
Thierry Reding412c7582014-12-09 22:25:12 -0700318 int err;
319
Simon Glass46fcfc12015-11-19 20:27:02 -0700320 err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
Thierry Reding412c7582014-12-09 22:25:12 -0700321 if (err < 0) {
Simon Glass46fcfc12015-11-19 20:27:02 -0700322 value = 0xffffffff;
323 goto done;
Thierry Reding412c7582014-12-09 22:25:12 -0700324 }
325
Simon Glass46fcfc12015-11-19 20:27:02 -0700326 value = readl(address);
Thierry Reding412c7582014-12-09 22:25:12 -0700327
Stephen Warrenbbe71b22016-04-20 15:46:50 -0600328#ifdef CONFIG_TEGRA20
Thierry Reding412c7582014-12-09 22:25:12 -0700329 /* fixup root port class */
330 if (PCI_BUS(bdf) == 0) {
Stephen Warrenbbe71b22016-04-20 15:46:50 -0600331 if ((offset & ~3) == PCI_CLASS_REVISION) {
Simon Glass46fcfc12015-11-19 20:27:02 -0700332 value &= ~0x00ff0000;
333 value |= PCI_CLASS_BRIDGE_PCI << 16;
Thierry Reding412c7582014-12-09 22:25:12 -0700334 }
335 }
Stephen Warrenbbe71b22016-04-20 15:46:50 -0600336#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700337
Simon Glass46fcfc12015-11-19 20:27:02 -0700338done:
339 *valuep = pci_conv_32_to_size(value, offset, size);
340
Thierry Reding412c7582014-12-09 22:25:12 -0700341 return 0;
342}
343
Simon Glass46fcfc12015-11-19 20:27:02 -0700344static int pci_tegra_write_config(struct udevice *bus, pci_dev_t bdf,
345 uint offset, ulong value,
346 enum pci_size_t size)
Thierry Reding412c7582014-12-09 22:25:12 -0700347{
Simon Glass46fcfc12015-11-19 20:27:02 -0700348 struct tegra_pcie *pcie = dev_get_priv(bus);
Thierry Reding412c7582014-12-09 22:25:12 -0700349 unsigned long address;
Simon Glass46fcfc12015-11-19 20:27:02 -0700350 ulong old;
Thierry Reding412c7582014-12-09 22:25:12 -0700351 int err;
352
Simon Glass46fcfc12015-11-19 20:27:02 -0700353 err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
Thierry Reding412c7582014-12-09 22:25:12 -0700354 if (err < 0)
Simon Glass46fcfc12015-11-19 20:27:02 -0700355 return 0;
Thierry Reding412c7582014-12-09 22:25:12 -0700356
Simon Glass46fcfc12015-11-19 20:27:02 -0700357 old = readl(address);
358 value = pci_conv_size_to_32(old, value, offset, size);
Thierry Reding412c7582014-12-09 22:25:12 -0700359 writel(value, address);
360
361 return 0;
362}
363
Simon Glasse6a30e02017-07-25 08:30:09 -0600364static int tegra_pcie_port_parse_dt(ofnode node, struct tegra_pcie_port *port)
Thierry Reding412c7582014-12-09 22:25:12 -0700365{
366 const u32 *addr;
367 int len;
368
Simon Glasse6a30e02017-07-25 08:30:09 -0600369 addr = ofnode_get_property(node, "assigned-addresses", &len);
Thierry Reding412c7582014-12-09 22:25:12 -0700370 if (!addr) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900371 pr_err("property \"assigned-addresses\" not found");
Thierry Reding412c7582014-12-09 22:25:12 -0700372 return -FDT_ERR_NOTFOUND;
373 }
374
375 port->regs.start = fdt32_to_cpu(addr[2]);
376 port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]);
377
378 return 0;
379}
380
Simon Glasse6a30e02017-07-25 08:30:09 -0600381static int tegra_pcie_get_xbar_config(ofnode node, u32 lanes,
Simon Glass46fcfc12015-11-19 20:27:02 -0700382 enum tegra_pci_id id, unsigned long *xbar)
Thierry Reding412c7582014-12-09 22:25:12 -0700383{
Thierry Reding412c7582014-12-09 22:25:12 -0700384 switch (id) {
Simon Glass46fcfc12015-11-19 20:27:02 -0700385 case TEGRA20_PCIE:
Thierry Reding412c7582014-12-09 22:25:12 -0700386 switch (lanes) {
387 case 0x00000004:
388 debug("single-mode configuration\n");
389 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
390 return 0;
391
392 case 0x00000202:
393 debug("dual-mode configuration\n");
394 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
395 return 0;
396 }
397 break;
Simon Glass46fcfc12015-11-19 20:27:02 -0700398 case TEGRA30_PCIE:
Thierry Reding412c7582014-12-09 22:25:12 -0700399 switch (lanes) {
400 case 0x00000204:
401 debug("4x1, 2x1 configuration\n");
402 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
403 return 0;
404
405 case 0x00020202:
406 debug("2x3 configuration\n");
407 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
408 return 0;
409
410 case 0x00010104:
411 debug("4x1, 1x2 configuration\n");
412 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
413 return 0;
414 }
415 break;
Simon Glass46fcfc12015-11-19 20:27:02 -0700416 case TEGRA124_PCIE:
417 case TEGRA210_PCIE:
Thierry Reding412c7582014-12-09 22:25:12 -0700418 switch (lanes) {
419 case 0x0000104:
420 debug("4x1, 1x1 configuration\n");
421 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
422 return 0;
423
424 case 0x0000102:
425 debug("2x1, 1x1 configuration\n");
426 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
427 return 0;
428 }
429 break;
Stephen Warren86f6a942016-08-05 16:10:34 -0600430 case TEGRA186_PCIE:
431 switch (lanes) {
432 case 0x0010004:
433 debug("x4 x1 configuration\n");
434 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_401;
435 return 0;
436
437 case 0x0010102:
438 debug("x2 x1 x1 configuration\n");
439 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_211;
440 return 0;
441
442 case 0x0010101:
443 debug("x1 x1 x1 configuration\n");
444 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_111;
445 return 0;
446 }
447 break;
Thierry Reding412c7582014-12-09 22:25:12 -0700448 default:
449 break;
450 }
451
452 return -FDT_ERR_NOTFOUND;
453}
454
Simon Glasse6a30e02017-07-25 08:30:09 -0600455static int tegra_pcie_parse_port_info(ofnode node, uint *index, uint *lanes)
Thierry Reding412c7582014-12-09 22:25:12 -0700456{
Bin Meng779847e2014-12-31 16:05:11 +0800457 struct fdt_pci_addr addr;
Thierry Reding412c7582014-12-09 22:25:12 -0700458 int err;
459
Simon Glasse6a30e02017-07-25 08:30:09 -0600460 err = ofnode_read_u32_default(node, "nvidia,num-lanes", -1);
Thierry Reding412c7582014-12-09 22:25:12 -0700461 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900462 pr_err("failed to parse \"nvidia,num-lanes\" property");
Thierry Reding412c7582014-12-09 22:25:12 -0700463 return err;
464 }
465
466 *lanes = err;
467
Simon Glasse6a30e02017-07-25 08:30:09 -0600468 err = ofnode_read_pci_addr(node, 0, "reg", &addr);
Thierry Reding412c7582014-12-09 22:25:12 -0700469 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900470 pr_err("failed to parse \"reg\" property");
Thierry Reding412c7582014-12-09 22:25:12 -0700471 return err;
472 }
473
Sjoerd Simonse3c64172015-01-20 18:06:53 +0100474 *index = PCI_DEV(addr.phys_hi) - 1;
Thierry Reding412c7582014-12-09 22:25:12 -0700475
476 return 0;
477}
478
Simon Glass46fcfc12015-11-19 20:27:02 -0700479int __weak tegra_pcie_board_init(void)
480{
481 return 0;
482}
483
Simon Glasse6a30e02017-07-25 08:30:09 -0600484static int tegra_pcie_parse_dt(struct udevice *dev, enum tegra_pci_id id,
Thierry Reding412c7582014-12-09 22:25:12 -0700485 struct tegra_pcie *pcie)
486{
Simon Glasse6a30e02017-07-25 08:30:09 -0600487 ofnode subnode;
Thierry Reding412c7582014-12-09 22:25:12 -0700488 u32 lanes = 0;
Simon Glasse6a30e02017-07-25 08:30:09 -0600489 int err;
Thierry Reding412c7582014-12-09 22:25:12 -0700490
Simon Glasse6a30e02017-07-25 08:30:09 -0600491 err = dev_read_resource(dev, 0, &pcie->pads);
Thierry Reding412c7582014-12-09 22:25:12 -0700492 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900493 pr_err("resource \"pads\" not found");
Thierry Reding412c7582014-12-09 22:25:12 -0700494 return err;
495 }
496
Simon Glasse6a30e02017-07-25 08:30:09 -0600497 err = dev_read_resource(dev, 1, &pcie->afi);
Thierry Reding412c7582014-12-09 22:25:12 -0700498 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900499 pr_err("resource \"afi\" not found");
Thierry Reding412c7582014-12-09 22:25:12 -0700500 return err;
501 }
502
Simon Glasse6a30e02017-07-25 08:30:09 -0600503 err = dev_read_resource(dev, 2, &pcie->cs);
Thierry Reding412c7582014-12-09 22:25:12 -0700504 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900505 pr_err("resource \"cs\" not found");
Thierry Reding412c7582014-12-09 22:25:12 -0700506 return err;
507 }
508
Simon Glasse49f4952016-01-17 14:51:55 -0700509 err = tegra_pcie_board_init();
510 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900511 pr_err("tegra_pcie_board_init() failed: err=%d", err);
Simon Glasse49f4952016-01-17 14:51:55 -0700512 return err;
513 }
Simon Glass46fcfc12015-11-19 20:27:02 -0700514
Stephen Warren86f6a942016-08-05 16:10:34 -0600515#ifndef CONFIG_TEGRA186
Thierry Reding412c7582014-12-09 22:25:12 -0700516 pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
517 if (pcie->phy) {
518 err = tegra_xusb_phy_prepare(pcie->phy);
519 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900520 pr_err("failed to prepare PHY: %d", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700521 return err;
522 }
523 }
Stephen Warren86f6a942016-08-05 16:10:34 -0600524#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700525
Simon Glasse6a30e02017-07-25 08:30:09 -0600526 dev_for_each_subnode(subnode, dev) {
Thierry Reding412c7582014-12-09 22:25:12 -0700527 unsigned int index = 0, num_lanes = 0;
528 struct tegra_pcie_port *port;
529
Simon Glasse6a30e02017-07-25 08:30:09 -0600530 err = tegra_pcie_parse_port_info(subnode, &index, &num_lanes);
Thierry Reding412c7582014-12-09 22:25:12 -0700531 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900532 pr_err("failed to obtain root port info");
Thierry Reding412c7582014-12-09 22:25:12 -0700533 continue;
534 }
535
536 lanes |= num_lanes << (index << 3);
537
Simon Glasse6a30e02017-07-25 08:30:09 -0600538 if (!ofnode_is_available(subnode))
Thierry Reding412c7582014-12-09 22:25:12 -0700539 continue;
540
541 port = malloc(sizeof(*port));
542 if (!port)
543 continue;
544
545 memset(port, 0, sizeof(*port));
546 port->num_lanes = num_lanes;
547 port->index = index;
548
Simon Glasse6a30e02017-07-25 08:30:09 -0600549 err = tegra_pcie_port_parse_dt(subnode, port);
Thierry Reding412c7582014-12-09 22:25:12 -0700550 if (err < 0) {
551 free(port);
552 continue;
553 }
554
555 list_add_tail(&port->list, &pcie->ports);
556 port->pcie = pcie;
557 }
558
Simon Glasse6a30e02017-07-25 08:30:09 -0600559 err = tegra_pcie_get_xbar_config(dev_ofnode(dev), lanes, id,
560 &pcie->xbar);
Thierry Reding412c7582014-12-09 22:25:12 -0700561 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900562 pr_err("invalid lane configuration");
Thierry Reding412c7582014-12-09 22:25:12 -0700563 return err;
564 }
565
Thierry Reding412c7582014-12-09 22:25:12 -0700566 return 0;
567}
568
Stephen Warren86f6a942016-08-05 16:10:34 -0600569#ifdef CONFIG_TEGRA186
570static int tegra_pcie_power_on(struct tegra_pcie *pcie)
571{
572 int ret;
573
574 ret = power_domain_on(&pcie->pwrdom);
575 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900576 pr_err("power_domain_on() failed: %d\n", ret);
Stephen Warren86f6a942016-08-05 16:10:34 -0600577 return ret;
578 }
579
580 ret = clk_enable(&pcie->clk_afi);
581 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900582 pr_err("clk_enable(afi) failed: %d\n", ret);
Stephen Warren86f6a942016-08-05 16:10:34 -0600583 return ret;
584 }
585
586 ret = clk_enable(&pcie->clk_pex);
587 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900588 pr_err("clk_enable(pex) failed: %d\n", ret);
Stephen Warren86f6a942016-08-05 16:10:34 -0600589 return ret;
590 }
591
592 ret = reset_deassert(&pcie->reset_afi);
593 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900594 pr_err("reset_deassert(afi) failed: %d\n", ret);
Stephen Warren86f6a942016-08-05 16:10:34 -0600595 return ret;
596 }
597
598 ret = reset_deassert(&pcie->reset_pex);
599 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900600 pr_err("reset_deassert(pex) failed: %d\n", ret);
Stephen Warren86f6a942016-08-05 16:10:34 -0600601 return ret;
602 }
603
604 return 0;
605}
606#else
Thierry Reding412c7582014-12-09 22:25:12 -0700607static int tegra_pcie_power_on(struct tegra_pcie *pcie)
608{
609 const struct tegra_pcie_soc *soc = pcie->soc;
610 unsigned long value;
611 int err;
612
613 /* reset PCIEXCLK logic, AFI controller and PCIe controller */
614 reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
615 reset_set_enable(PERIPH_ID_AFI, 1);
616 reset_set_enable(PERIPH_ID_PCIE, 1);
617
618 err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
619 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900620 pr_err("failed to power off PCIe partition: %d", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700621 return err;
622 }
623
Thierry Reding412c7582014-12-09 22:25:12 -0700624 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
625 PERIPH_ID_PCIE);
626 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900627 pr_err("failed to power up PCIe partition: %d", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700628 return err;
629 }
630
631 /* take AFI controller out of reset */
632 reset_set_enable(PERIPH_ID_AFI, 0);
633
634 /* enable AFI clock */
635 clock_enable(PERIPH_ID_AFI);
636
637 if (soc->has_cml_clk) {
638 /* enable CML clock */
639 value = readl(NV_PA_CLK_RST_BASE + 0x48c);
640 value |= (1 << 0);
641 value &= ~(1 << 1);
642 writel(value, NV_PA_CLK_RST_BASE + 0x48c);
643 }
644
645 err = tegra_plle_enable();
646 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900647 pr_err("failed to enable PLLE: %d\n", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700648 return err;
649 }
650
651 return 0;
652}
653
654static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
655{
656 const struct tegra_pcie_soc *soc = pcie->soc;
657 unsigned long start = get_timer(0);
658 u32 value;
659
660 while (get_timer(start) < timeout) {
661 value = pads_readl(pcie, soc->pads_pll_ctl);
662 if (value & PADS_PLL_CTL_LOCKDET)
663 return 0;
664 }
665
666 return -ETIMEDOUT;
667}
668
669static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
670{
671 const struct tegra_pcie_soc *soc = pcie->soc;
672 u32 value;
673 int err;
674
675 /* initialize internal PHY, enable up to 16 PCIe lanes */
676 pads_writel(pcie, 0, PADS_CTL_SEL);
677
678 /* override IDDQ to 1 on all 4 lanes */
679 value = pads_readl(pcie, PADS_CTL);
680 value |= PADS_CTL_IDDQ_1L;
681 pads_writel(pcie, value, PADS_CTL);
682
683 /*
684 * Set up PHY PLL inputs select PLLE output as refclock, set TX
685 * ref sel to div10 (not div5).
686 */
687 value = pads_readl(pcie, soc->pads_pll_ctl);
688 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
689 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
690 pads_writel(pcie, value, soc->pads_pll_ctl);
691
692 /* reset PLL */
693 value = pads_readl(pcie, soc->pads_pll_ctl);
694 value &= ~PADS_PLL_CTL_RST_B4SM;
695 pads_writel(pcie, value, soc->pads_pll_ctl);
696
697 udelay(20);
698
699 /* take PLL out of reset */
700 value = pads_readl(pcie, soc->pads_pll_ctl);
701 value |= PADS_PLL_CTL_RST_B4SM;
702 pads_writel(pcie, value, soc->pads_pll_ctl);
703
Thierry Reding412c7582014-12-09 22:25:12 -0700704 /* wait for the PLL to lock */
705 err = tegra_pcie_pll_wait(pcie, 500);
706 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900707 pr_err("PLL failed to lock: %d", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700708 return err;
709 }
710
711 /* turn off IDDQ override */
712 value = pads_readl(pcie, PADS_CTL);
713 value &= ~PADS_CTL_IDDQ_1L;
714 pads_writel(pcie, value, PADS_CTL);
715
716 /* enable TX/RX data */
717 value = pads_readl(pcie, PADS_CTL);
718 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
719 pads_writel(pcie, value, PADS_CTL);
720
721 return 0;
722}
Stephen Warren86f6a942016-08-05 16:10:34 -0600723#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700724
725static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
726{
727 const struct tegra_pcie_soc *soc = pcie->soc;
728 struct tegra_pcie_port *port;
729 u32 value;
730 int err;
731
Stephen Warren86f6a942016-08-05 16:10:34 -0600732#ifdef CONFIG_TEGRA186
733 {
734#else
Thierry Reding412c7582014-12-09 22:25:12 -0700735 if (pcie->phy) {
Stephen Warren86f6a942016-08-05 16:10:34 -0600736#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700737 value = afi_readl(pcie, AFI_PLLE_CONTROL);
738 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
739 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
740 afi_writel(pcie, value, AFI_PLLE_CONTROL);
741 }
742
743 if (soc->has_pex_bias_ctrl)
744 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
745
746 value = afi_readl(pcie, AFI_PCIE_CONFIG);
747 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
748 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
749
750 list_for_each_entry(port, &pcie->ports, list)
751 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
752
753 afi_writel(pcie, value, AFI_PCIE_CONFIG);
754
755 value = afi_readl(pcie, AFI_FUSE);
756
757 if (soc->has_gen2)
758 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
759 else
760 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
761
762 afi_writel(pcie, value, AFI_FUSE);
763
Stephen Warren86f6a942016-08-05 16:10:34 -0600764#ifndef CONFIG_TEGRA186
Thierry Reding412c7582014-12-09 22:25:12 -0700765 if (pcie->phy)
766 err = tegra_xusb_phy_enable(pcie->phy);
767 else
768 err = tegra_pcie_phy_enable(pcie);
769
770 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900771 pr_err("failed to power on PHY: %d\n", err);
Thierry Reding412c7582014-12-09 22:25:12 -0700772 return err;
773 }
Stephen Warren86f6a942016-08-05 16:10:34 -0600774#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700775
776 /* take the PCIEXCLK logic out of reset */
Stephen Warren86f6a942016-08-05 16:10:34 -0600777#ifdef CONFIG_TEGRA186
778 err = reset_deassert(&pcie->reset_pcie_x);
779 if (err) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900780 pr_err("reset_deassert(pcie_x) failed: %d\n", err);
Stephen Warren86f6a942016-08-05 16:10:34 -0600781 return err;
782 }
783#else
Thierry Reding412c7582014-12-09 22:25:12 -0700784 reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
Stephen Warren86f6a942016-08-05 16:10:34 -0600785#endif
Thierry Reding412c7582014-12-09 22:25:12 -0700786
787 /* finally enable PCIe */
788 value = afi_readl(pcie, AFI_CONFIGURATION);
789 value |= AFI_CONFIGURATION_EN_FPCI;
790 afi_writel(pcie, value, AFI_CONFIGURATION);
791
792 /* disable all interrupts */
793 afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
794 afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
795 afi_writel(pcie, 0, AFI_INTR_MASK);
796 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
797
798 return 0;
799}
800
Simon Glass46fcfc12015-11-19 20:27:02 -0700801static int tegra_pcie_setup_translations(struct udevice *bus)
Thierry Reding412c7582014-12-09 22:25:12 -0700802{
Simon Glass46fcfc12015-11-19 20:27:02 -0700803 struct tegra_pcie *pcie = dev_get_priv(bus);
Thierry Reding412c7582014-12-09 22:25:12 -0700804 unsigned long fpci, axi, size;
Simon Glass46fcfc12015-11-19 20:27:02 -0700805 struct pci_region *io, *mem, *pref;
806 int count;
Thierry Reding412c7582014-12-09 22:25:12 -0700807
808 /* BAR 0: type 1 extended configuration space */
809 fpci = 0xfe100000;
Simon Glasse6a30e02017-07-25 08:30:09 -0600810 size = resource_size(&pcie->cs);
Thierry Reding412c7582014-12-09 22:25:12 -0700811 axi = pcie->cs.start;
812
813 afi_writel(pcie, axi, AFI_AXI_BAR0_START);
814 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
815 afi_writel(pcie, fpci, AFI_FPCI_BAR0);
816
Simon Glass46fcfc12015-11-19 20:27:02 -0700817 count = pci_get_regions(bus, &io, &mem, &pref);
818 if (count != 3)
819 return -EINVAL;
820
Thierry Reding412c7582014-12-09 22:25:12 -0700821 /* BAR 1: downstream I/O */
822 fpci = 0xfdfc0000;
Simon Glass46fcfc12015-11-19 20:27:02 -0700823 size = io->size;
824 axi = io->phys_start;
Thierry Reding412c7582014-12-09 22:25:12 -0700825
826 afi_writel(pcie, axi, AFI_AXI_BAR1_START);
827 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
828 afi_writel(pcie, fpci, AFI_FPCI_BAR1);
829
830 /* BAR 2: prefetchable memory */
Simon Glass46fcfc12015-11-19 20:27:02 -0700831 fpci = (((pref->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
832 size = pref->size;
833 axi = pref->phys_start;
Thierry Reding412c7582014-12-09 22:25:12 -0700834
835 afi_writel(pcie, axi, AFI_AXI_BAR2_START);
836 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
837 afi_writel(pcie, fpci, AFI_FPCI_BAR2);
838
839 /* BAR 3: non-prefetchable memory */
Simon Glass46fcfc12015-11-19 20:27:02 -0700840 fpci = (((mem->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
841 size = mem->size;
842 axi = mem->phys_start;
Thierry Reding412c7582014-12-09 22:25:12 -0700843
844 afi_writel(pcie, axi, AFI_AXI_BAR3_START);
845 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
846 afi_writel(pcie, fpci, AFI_FPCI_BAR3);
847
848 /* NULL out the remaining BARs as they are not used */
849 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
850 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
851 afi_writel(pcie, 0, AFI_FPCI_BAR4);
852
853 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
854 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
855 afi_writel(pcie, 0, AFI_FPCI_BAR5);
856
857 /* map all upstream transactions as uncached */
858 afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
859 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
860 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
861 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
862
863 /* MSI translations are setup only when needed */
864 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
865 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
866 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
867 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
Simon Glass46fcfc12015-11-19 20:27:02 -0700868
869 return 0;
Thierry Reding412c7582014-12-09 22:25:12 -0700870}
871
872static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
873{
874 unsigned long ret = 0;
875
876 switch (port->index) {
877 case 0:
878 ret = AFI_PEX0_CTRL;
879 break;
880
881 case 1:
882 ret = AFI_PEX1_CTRL;
883 break;
884
885 case 2:
Stephen Warren86f6a942016-08-05 16:10:34 -0600886 ret = port->pcie->soc->afi_pex2_ctrl;
Thierry Reding412c7582014-12-09 22:25:12 -0700887 break;
888 }
889
890 return ret;
891}
892
Marcel Ziswiler53f48632018-05-08 17:34:09 +0200893void tegra_pcie_port_reset(struct tegra_pcie_port *port)
Thierry Reding412c7582014-12-09 22:25:12 -0700894{
895 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
896 unsigned long value;
897
898 /* pulse reset signel */
899 value = afi_readl(port->pcie, ctrl);
900 value &= ~AFI_PEX_CTRL_RST;
901 afi_writel(port->pcie, value, ctrl);
902
903 udelay(2000);
904
905 value = afi_readl(port->pcie, ctrl);
906 value |= AFI_PEX_CTRL_RST;
907 afi_writel(port->pcie, value, ctrl);
908}
909
Marcel Ziswiler53f48632018-05-08 17:34:09 +0200910int tegra_pcie_port_index_of_port(struct tegra_pcie_port *port)
911{
912 return port->index;
913}
914
915void __weak tegra_pcie_board_port_reset(struct tegra_pcie_port *port)
916{
917 tegra_pcie_port_reset(port);
918}
919
Thierry Reding412c7582014-12-09 22:25:12 -0700920static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
921{
Stephen Warren29f49fc2016-06-24 08:36:04 -0600922 struct tegra_pcie *pcie = port->pcie;
923 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding412c7582014-12-09 22:25:12 -0700924 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
925 unsigned long value;
926
927 /* enable reference clock */
Stephen Warren29f49fc2016-06-24 08:36:04 -0600928 value = afi_readl(pcie, ctrl);
Thierry Reding412c7582014-12-09 22:25:12 -0700929 value |= AFI_PEX_CTRL_REFCLK_EN;
930
Stephen Warren29f49fc2016-06-24 08:36:04 -0600931 if (pcie->soc->has_pex_clkreq_en)
Thierry Reding412c7582014-12-09 22:25:12 -0700932 value |= AFI_PEX_CTRL_CLKREQ_EN;
933
934 value |= AFI_PEX_CTRL_OVERRIDE_EN;
935
Stephen Warren29f49fc2016-06-24 08:36:04 -0600936 afi_writel(pcie, value, ctrl);
Thierry Reding412c7582014-12-09 22:25:12 -0700937
Marcel Ziswiler53f48632018-05-08 17:34:09 +0200938 tegra_pcie_board_port_reset(port);
Stephen Warrene3553be2015-10-05 17:00:42 -0600939
940 if (soc->force_pca_enable) {
941 value = rp_readl(port, RP_VEND_CTL2);
942 value |= RP_VEND_CTL2_PCA_ENABLE;
943 rp_writel(port, value, RP_VEND_CTL2);
944 }
Stephen Warren29f49fc2016-06-24 08:36:04 -0600945
946 /* configure the reference clock driver */
947 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
948 if (soc->num_ports > 2)
949 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
Thierry Reding412c7582014-12-09 22:25:12 -0700950}
951
952static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
953{
954 unsigned int retries = 3;
955 unsigned long value;
956
957 value = rp_readl(port, RP_PRIV_MISC);
958 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
959 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
960 rp_writel(port, value, RP_PRIV_MISC);
961
962 do {
963 unsigned int timeout = 200;
964
965 do {
966 value = rp_readl(port, RP_VEND_XP);
967 if (value & RP_VEND_XP_DL_UP)
968 break;
969
970 udelay(2000);
971 } while (--timeout);
972
973 if (!timeout) {
974 debug("link %u down, retrying\n", port->index);
975 goto retry;
976 }
977
978 timeout = 200;
979
980 do {
981 value = rp_readl(port, RP_LINK_CONTROL_STATUS);
982 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
983 return true;
984
985 udelay(2000);
986 } while (--timeout);
987
988retry:
Marcel Ziswiler53f48632018-05-08 17:34:09 +0200989 tegra_pcie_board_port_reset(port);
Thierry Reding412c7582014-12-09 22:25:12 -0700990 } while (--retries);
991
992 return false;
993}
994
995static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
996{
997 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
998 unsigned long value;
999
1000 /* assert port reset */
1001 value = afi_readl(port->pcie, ctrl);
1002 value &= ~AFI_PEX_CTRL_RST;
1003 afi_writel(port->pcie, value, ctrl);
1004
1005 /* disable reference clock */
1006 value = afi_readl(port->pcie, ctrl);
1007 value &= ~AFI_PEX_CTRL_REFCLK_EN;
1008 afi_writel(port->pcie, value, ctrl);
1009}
1010
1011static void tegra_pcie_port_free(struct tegra_pcie_port *port)
1012{
1013 list_del(&port->list);
1014 free(port);
1015}
1016
1017static int tegra_pcie_enable(struct tegra_pcie *pcie)
1018{
1019 struct tegra_pcie_port *port, *tmp;
1020
1021 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1022 debug("probing port %u, using %u lanes\n", port->index,
1023 port->num_lanes);
1024
1025 tegra_pcie_port_enable(port);
1026
1027 if (tegra_pcie_port_check_link(port))
1028 continue;
1029
1030 debug("link %u down, ignoring\n", port->index);
1031
1032 tegra_pcie_port_disable(port);
1033 tegra_pcie_port_free(port);
1034 }
1035
1036 return 0;
1037}
1038
Simon Glass46fcfc12015-11-19 20:27:02 -07001039static const struct tegra_pcie_soc pci_tegra_soc[] = {
1040 [TEGRA20_PCIE] = {
1041 .num_ports = 2,
1042 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1043 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
Stephen Warrend132c472016-06-21 12:47:51 -06001044 .pads_refclk_cfg0 = 0xfa5cfa5c,
Simon Glass46fcfc12015-11-19 20:27:02 -07001045 .has_pex_clkreq_en = false,
1046 .has_pex_bias_ctrl = false,
1047 .has_cml_clk = false,
1048 .has_gen2 = false,
1049 },
1050 [TEGRA30_PCIE] = {
1051 .num_ports = 3,
1052 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1053 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warren86f6a942016-08-05 16:10:34 -06001054 .afi_pex2_ctrl = AFI_PEX2_CTRL,
Stephen Warrend132c472016-06-21 12:47:51 -06001055 .pads_refclk_cfg0 = 0xfa5cfa5c,
1056 .pads_refclk_cfg1 = 0xfa5cfa5c,
Simon Glass46fcfc12015-11-19 20:27:02 -07001057 .has_pex_clkreq_en = true,
1058 .has_pex_bias_ctrl = true,
1059 .has_cml_clk = true,
1060 .has_gen2 = false,
1061 },
1062 [TEGRA124_PCIE] = {
1063 .num_ports = 2,
1064 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1065 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrend132c472016-06-21 12:47:51 -06001066 .pads_refclk_cfg0 = 0x44ac44ac,
Simon Glass46fcfc12015-11-19 20:27:02 -07001067 .has_pex_clkreq_en = true,
1068 .has_pex_bias_ctrl = true,
1069 .has_cml_clk = true,
1070 .has_gen2 = true,
1071 },
1072 [TEGRA210_PCIE] = {
1073 .num_ports = 2,
1074 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1075 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrend132c472016-06-21 12:47:51 -06001076 .pads_refclk_cfg0 = 0x90b890b8,
Simon Glass46fcfc12015-11-19 20:27:02 -07001077 .has_pex_clkreq_en = true,
1078 .has_pex_bias_ctrl = true,
1079 .has_cml_clk = true,
1080 .has_gen2 = true,
1081 .force_pca_enable = true,
Stephen Warren86f6a942016-08-05 16:10:34 -06001082 },
1083 [TEGRA186_PCIE] = {
1084 .num_ports = 3,
1085 .afi_pex2_ctrl = AFI_PEX2_CTRL_T186,
1086 .pads_refclk_cfg0 = 0x80b880b8,
1087 .pads_refclk_cfg1 = 0x000480b8,
1088 .has_pex_clkreq_en = true,
1089 .has_pex_bias_ctrl = true,
1090 .has_gen2 = true,
1091 },
Stephen Warren5e44dce2015-10-05 17:00:44 -06001092};
1093
Simon Glass46fcfc12015-11-19 20:27:02 -07001094static int pci_tegra_ofdata_to_platdata(struct udevice *dev)
Thierry Reding412c7582014-12-09 22:25:12 -07001095{
Simon Glass46fcfc12015-11-19 20:27:02 -07001096 struct tegra_pcie *pcie = dev_get_priv(dev);
1097 enum tegra_pci_id id;
Thierry Reding412c7582014-12-09 22:25:12 -07001098
Simon Glass46fcfc12015-11-19 20:27:02 -07001099 id = dev_get_driver_data(dev);
1100 pcie->soc = &pci_tegra_soc[id];
Thierry Reding412c7582014-12-09 22:25:12 -07001101
Simon Glass46fcfc12015-11-19 20:27:02 -07001102 INIT_LIST_HEAD(&pcie->ports);
Thierry Reding412c7582014-12-09 22:25:12 -07001103
Simon Glasse6a30e02017-07-25 08:30:09 -06001104 if (tegra_pcie_parse_dt(dev, id, pcie))
Simon Glass46fcfc12015-11-19 20:27:02 -07001105 return -EINVAL;
Thierry Reding412c7582014-12-09 22:25:12 -07001106
Simon Glass46fcfc12015-11-19 20:27:02 -07001107 return 0;
1108}
Thierry Reding412c7582014-12-09 22:25:12 -07001109
Simon Glass46fcfc12015-11-19 20:27:02 -07001110static int pci_tegra_probe(struct udevice *dev)
1111{
1112 struct tegra_pcie *pcie = dev_get_priv(dev);
1113 int err;
Thierry Reding412c7582014-12-09 22:25:12 -07001114
Stephen Warren86f6a942016-08-05 16:10:34 -06001115#ifdef CONFIG_TEGRA186
1116 err = clk_get_by_name(dev, "afi", &pcie->clk_afi);
1117 if (err) {
1118 debug("clk_get_by_name(afi) failed: %d\n", err);
1119 return err;
1120 }
1121
1122 err = clk_get_by_name(dev, "pex", &pcie->clk_pex);
1123 if (err) {
1124 debug("clk_get_by_name(pex) failed: %d\n", err);
1125 return err;
1126 }
1127
1128 err = reset_get_by_name(dev, "afi", &pcie->reset_afi);
1129 if (err) {
1130 debug("reset_get_by_name(afi) failed: %d\n", err);
1131 return err;
1132 }
1133
1134 err = reset_get_by_name(dev, "pex", &pcie->reset_pex);
1135 if (err) {
1136 debug("reset_get_by_name(pex) failed: %d\n", err);
1137 return err;
1138 }
1139
1140 err = reset_get_by_name(dev, "pcie_x", &pcie->reset_pcie_x);
1141 if (err) {
1142 debug("reset_get_by_name(pcie_x) failed: %d\n", err);
1143 return err;
1144 }
1145
1146 err = power_domain_get(dev, &pcie->pwrdom);
1147 if (err) {
1148 debug("power_domain_get() failed: %d\n", err);
1149 return err;
1150 }
1151#endif
1152
Simon Glass46fcfc12015-11-19 20:27:02 -07001153 err = tegra_pcie_power_on(pcie);
1154 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001155 pr_err("failed to power on");
Simon Glass46fcfc12015-11-19 20:27:02 -07001156 return err;
1157 }
Thierry Reding412c7582014-12-09 22:25:12 -07001158
Simon Glass46fcfc12015-11-19 20:27:02 -07001159 err = tegra_pcie_enable_controller(pcie);
1160 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001161 pr_err("failed to enable controller");
Simon Glass46fcfc12015-11-19 20:27:02 -07001162 return err;
1163 }
Thierry Reding412c7582014-12-09 22:25:12 -07001164
Simon Glass46fcfc12015-11-19 20:27:02 -07001165 err = tegra_pcie_setup_translations(dev);
1166 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001167 pr_err("failed to decode ranges");
Simon Glass46fcfc12015-11-19 20:27:02 -07001168 return err;
1169 }
Thierry Reding412c7582014-12-09 22:25:12 -07001170
Simon Glass46fcfc12015-11-19 20:27:02 -07001171 err = tegra_pcie_enable(pcie);
1172 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001173 pr_err("failed to enable PCIe");
Simon Glass46fcfc12015-11-19 20:27:02 -07001174 return err;
Thierry Reding412c7582014-12-09 22:25:12 -07001175 }
1176
1177 return 0;
1178}
1179
Simon Glass46fcfc12015-11-19 20:27:02 -07001180static const struct dm_pci_ops pci_tegra_ops = {
1181 .read_config = pci_tegra_read_config,
1182 .write_config = pci_tegra_write_config,
1183};
Thierry Reding412c7582014-12-09 22:25:12 -07001184
Simon Glass46fcfc12015-11-19 20:27:02 -07001185static const struct udevice_id pci_tegra_ids[] = {
1186 { .compatible = "nvidia,tegra20-pcie", .data = TEGRA20_PCIE },
1187 { .compatible = "nvidia,tegra30-pcie", .data = TEGRA30_PCIE },
1188 { .compatible = "nvidia,tegra124-pcie", .data = TEGRA124_PCIE },
1189 { .compatible = "nvidia,tegra210-pcie", .data = TEGRA210_PCIE },
Stephen Warren86f6a942016-08-05 16:10:34 -06001190 { .compatible = "nvidia,tegra186-pcie", .data = TEGRA186_PCIE },
Simon Glass46fcfc12015-11-19 20:27:02 -07001191 { }
1192};
Thierry Reding412c7582014-12-09 22:25:12 -07001193
Simon Glass46fcfc12015-11-19 20:27:02 -07001194U_BOOT_DRIVER(pci_tegra) = {
1195 .name = "pci_tegra",
1196 .id = UCLASS_PCI,
1197 .of_match = pci_tegra_ids,
1198 .ops = &pci_tegra_ops,
1199 .ofdata_to_platdata = pci_tegra_ofdata_to_platdata,
1200 .probe = pci_tegra_probe,
1201 .priv_auto_alloc_size = sizeof(struct tegra_pcie),
1202};