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