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 | |
| 278 | static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where) |
| 279 | { |
| 280 | return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) | |
| 281 | (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) | |
| 282 | (where & 0xfc); |
| 283 | } |
| 284 | |
| 285 | static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf, |
| 286 | int where, unsigned long *address) |
| 287 | { |
| 288 | unsigned int bus = PCI_BUS(bdf); |
| 289 | |
| 290 | if (bus == 0) { |
| 291 | unsigned int dev = PCI_DEV(bdf); |
| 292 | struct tegra_pcie_port *port; |
| 293 | |
| 294 | list_for_each_entry(port, &pcie->ports, list) { |
| 295 | if (port->index + 1 == dev) { |
| 296 | *address = port->regs.start + (where & ~3); |
| 297 | return 0; |
| 298 | } |
| 299 | } |
Stephen Warren | bbe71b2 | 2016-04-20 15:46:50 -0600 | [diff] [blame] | 300 | return -EFAULT; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 301 | } else { |
Stephen Warren | bbe71b2 | 2016-04-20 15:46:50 -0600 | [diff] [blame] | 302 | #ifdef CONFIG_TEGRA20 |
| 303 | unsigned int dev = PCI_DEV(bdf); |
| 304 | if (dev != 0) |
| 305 | return -EFAULT; |
| 306 | #endif |
| 307 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 308 | *address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where); |
| 309 | return 0; |
| 310 | } |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 313 | 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] | 314 | uint offset, ulong *valuep, |
| 315 | enum pci_size_t size) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 316 | { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 317 | struct tegra_pcie *pcie = dev_get_priv(bus); |
| 318 | unsigned long address, value; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 319 | int err; |
| 320 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 321 | err = tegra_pcie_conf_address(pcie, bdf, offset, &address); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 322 | if (err < 0) { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 323 | value = 0xffffffff; |
| 324 | goto done; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 327 | value = readl(address); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 328 | |
Stephen Warren | bbe71b2 | 2016-04-20 15:46:50 -0600 | [diff] [blame] | 329 | #ifdef CONFIG_TEGRA20 |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 330 | /* fixup root port class */ |
| 331 | if (PCI_BUS(bdf) == 0) { |
Stephen Warren | bbe71b2 | 2016-04-20 15:46:50 -0600 | [diff] [blame] | 332 | if ((offset & ~3) == PCI_CLASS_REVISION) { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 333 | value &= ~0x00ff0000; |
| 334 | value |= PCI_CLASS_BRIDGE_PCI << 16; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
Stephen Warren | bbe71b2 | 2016-04-20 15:46:50 -0600 | [diff] [blame] | 337 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 338 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 339 | done: |
| 340 | *valuep = pci_conv_32_to_size(value, offset, size); |
| 341 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 345 | static int pci_tegra_write_config(struct udevice *bus, pci_dev_t bdf, |
| 346 | uint offset, ulong value, |
| 347 | enum pci_size_t size) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 348 | { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 349 | struct tegra_pcie *pcie = dev_get_priv(bus); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 350 | unsigned long address; |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 351 | ulong old; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 352 | int err; |
| 353 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 354 | err = tegra_pcie_conf_address(pcie, bdf, offset, &address); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 355 | if (err < 0) |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 356 | return 0; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 357 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 358 | old = readl(address); |
| 359 | value = pci_conv_size_to_32(old, value, offset, size); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 360 | writel(value, address); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 365 | 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] | 366 | { |
| 367 | const u32 *addr; |
| 368 | int len; |
| 369 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 370 | addr = ofnode_get_property(node, "assigned-addresses", &len); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 371 | if (!addr) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 372 | pr_err("property \"assigned-addresses\" not found"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 373 | return -FDT_ERR_NOTFOUND; |
| 374 | } |
| 375 | |
| 376 | port->regs.start = fdt32_to_cpu(addr[2]); |
| 377 | port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 382 | static int tegra_pcie_get_xbar_config(ofnode node, u32 lanes, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 383 | enum tegra_pci_id id, unsigned long *xbar) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 384 | { |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 385 | switch (id) { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 386 | case TEGRA20_PCIE: |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 387 | switch (lanes) { |
| 388 | case 0x00000004: |
| 389 | debug("single-mode configuration\n"); |
| 390 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE; |
| 391 | return 0; |
| 392 | |
| 393 | case 0x00000202: |
| 394 | debug("dual-mode configuration\n"); |
| 395 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL; |
| 396 | return 0; |
| 397 | } |
| 398 | break; |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 399 | case TEGRA30_PCIE: |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 400 | switch (lanes) { |
| 401 | case 0x00000204: |
| 402 | debug("4x1, 2x1 configuration\n"); |
| 403 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420; |
| 404 | return 0; |
| 405 | |
| 406 | case 0x00020202: |
| 407 | debug("2x3 configuration\n"); |
| 408 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222; |
| 409 | return 0; |
| 410 | |
| 411 | case 0x00010104: |
| 412 | debug("4x1, 1x2 configuration\n"); |
| 413 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411; |
| 414 | return 0; |
| 415 | } |
| 416 | break; |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 417 | case TEGRA124_PCIE: |
| 418 | case TEGRA210_PCIE: |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 419 | switch (lanes) { |
| 420 | case 0x0000104: |
| 421 | debug("4x1, 1x1 configuration\n"); |
| 422 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1; |
| 423 | return 0; |
| 424 | |
| 425 | case 0x0000102: |
| 426 | debug("2x1, 1x1 configuration\n"); |
| 427 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1; |
| 428 | return 0; |
| 429 | } |
| 430 | break; |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 431 | case TEGRA186_PCIE: |
| 432 | switch (lanes) { |
| 433 | case 0x0010004: |
| 434 | debug("x4 x1 configuration\n"); |
| 435 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_401; |
| 436 | return 0; |
| 437 | |
| 438 | case 0x0010102: |
| 439 | debug("x2 x1 x1 configuration\n"); |
| 440 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_211; |
| 441 | return 0; |
| 442 | |
| 443 | case 0x0010101: |
| 444 | debug("x1 x1 x1 configuration\n"); |
| 445 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_T186_111; |
| 446 | return 0; |
| 447 | } |
| 448 | break; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 449 | default: |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | return -FDT_ERR_NOTFOUND; |
| 454 | } |
| 455 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 456 | 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] | 457 | { |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 458 | struct fdt_pci_addr addr; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 459 | int err; |
| 460 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 461 | err = ofnode_read_u32_default(node, "nvidia,num-lanes", -1); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 462 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 463 | pr_err("failed to parse \"nvidia,num-lanes\" property"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 464 | return err; |
| 465 | } |
| 466 | |
| 467 | *lanes = err; |
| 468 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 469 | err = ofnode_read_pci_addr(node, 0, "reg", &addr); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 470 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 471 | pr_err("failed to parse \"reg\" property"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 472 | return err; |
| 473 | } |
| 474 | |
Sjoerd Simons | e3c6417 | 2015-01-20 18:06:53 +0100 | [diff] [blame] | 475 | *index = PCI_DEV(addr.phys_hi) - 1; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 480 | int __weak tegra_pcie_board_init(void) |
| 481 | { |
| 482 | return 0; |
| 483 | } |
| 484 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 485 | 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] | 486 | struct tegra_pcie *pcie) |
| 487 | { |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 488 | ofnode subnode; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 489 | u32 lanes = 0; |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 490 | int err; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 491 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 492 | err = dev_read_resource(dev, 0, &pcie->pads); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 493 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 494 | pr_err("resource \"pads\" not found"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 495 | return err; |
| 496 | } |
| 497 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 498 | err = dev_read_resource(dev, 1, &pcie->afi); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 499 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 500 | pr_err("resource \"afi\" not found"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 501 | return err; |
| 502 | } |
| 503 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 504 | err = dev_read_resource(dev, 2, &pcie->cs); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 505 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 506 | pr_err("resource \"cs\" not found"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 507 | return err; |
| 508 | } |
| 509 | |
Simon Glass | e49f495 | 2016-01-17 14:51:55 -0700 | [diff] [blame] | 510 | err = tegra_pcie_board_init(); |
| 511 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 512 | pr_err("tegra_pcie_board_init() failed: err=%d", err); |
Simon Glass | e49f495 | 2016-01-17 14:51:55 -0700 | [diff] [blame] | 513 | return err; |
| 514 | } |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 515 | |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 516 | #ifndef CONFIG_TEGRA186 |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 517 | pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE); |
| 518 | if (pcie->phy) { |
| 519 | err = tegra_xusb_phy_prepare(pcie->phy); |
| 520 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 521 | pr_err("failed to prepare PHY: %d", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 522 | return err; |
| 523 | } |
| 524 | } |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 525 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 526 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 527 | dev_for_each_subnode(subnode, dev) { |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 528 | unsigned int index = 0, num_lanes = 0; |
| 529 | struct tegra_pcie_port *port; |
| 530 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 531 | err = tegra_pcie_parse_port_info(subnode, &index, &num_lanes); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 532 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 533 | pr_err("failed to obtain root port info"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | |
| 537 | lanes |= num_lanes << (index << 3); |
| 538 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 539 | if (!ofnode_is_available(subnode)) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 540 | continue; |
| 541 | |
| 542 | port = malloc(sizeof(*port)); |
| 543 | if (!port) |
| 544 | continue; |
| 545 | |
| 546 | memset(port, 0, sizeof(*port)); |
| 547 | port->num_lanes = num_lanes; |
| 548 | port->index = index; |
| 549 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 550 | err = tegra_pcie_port_parse_dt(subnode, port); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 551 | if (err < 0) { |
| 552 | free(port); |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | list_add_tail(&port->list, &pcie->ports); |
| 557 | port->pcie = pcie; |
| 558 | } |
| 559 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 560 | err = tegra_pcie_get_xbar_config(dev_ofnode(dev), lanes, id, |
| 561 | &pcie->xbar); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 562 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 563 | pr_err("invalid lane configuration"); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 564 | return err; |
| 565 | } |
| 566 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 570 | #ifdef CONFIG_TEGRA186 |
| 571 | static int tegra_pcie_power_on(struct tegra_pcie *pcie) |
| 572 | { |
| 573 | int ret; |
| 574 | |
| 575 | ret = power_domain_on(&pcie->pwrdom); |
| 576 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 577 | pr_err("power_domain_on() failed: %d\n", ret); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | ret = clk_enable(&pcie->clk_afi); |
| 582 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 583 | pr_err("clk_enable(afi) failed: %d\n", ret); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | ret = clk_enable(&pcie->clk_pex); |
| 588 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 589 | pr_err("clk_enable(pex) failed: %d\n", ret); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | ret = reset_deassert(&pcie->reset_afi); |
| 594 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 595 | pr_err("reset_deassert(afi) failed: %d\n", ret); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | ret = reset_deassert(&pcie->reset_pex); |
| 600 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 601 | pr_err("reset_deassert(pex) failed: %d\n", ret); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | #else |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 608 | static int tegra_pcie_power_on(struct tegra_pcie *pcie) |
| 609 | { |
| 610 | const struct tegra_pcie_soc *soc = pcie->soc; |
| 611 | unsigned long value; |
| 612 | int err; |
| 613 | |
| 614 | /* reset PCIEXCLK logic, AFI controller and PCIe controller */ |
| 615 | reset_set_enable(PERIPH_ID_PCIEXCLK, 1); |
| 616 | reset_set_enable(PERIPH_ID_AFI, 1); |
| 617 | reset_set_enable(PERIPH_ID_PCIE, 1); |
| 618 | |
| 619 | err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE); |
| 620 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 621 | pr_err("failed to power off PCIe partition: %d", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 622 | return err; |
| 623 | } |
| 624 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 625 | err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE, |
| 626 | PERIPH_ID_PCIE); |
| 627 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 628 | pr_err("failed to power up PCIe partition: %d", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 629 | return err; |
| 630 | } |
| 631 | |
| 632 | /* take AFI controller out of reset */ |
| 633 | reset_set_enable(PERIPH_ID_AFI, 0); |
| 634 | |
| 635 | /* enable AFI clock */ |
| 636 | clock_enable(PERIPH_ID_AFI); |
| 637 | |
| 638 | if (soc->has_cml_clk) { |
| 639 | /* enable CML clock */ |
| 640 | value = readl(NV_PA_CLK_RST_BASE + 0x48c); |
| 641 | value |= (1 << 0); |
| 642 | value &= ~(1 << 1); |
| 643 | writel(value, NV_PA_CLK_RST_BASE + 0x48c); |
| 644 | } |
| 645 | |
| 646 | err = tegra_plle_enable(); |
| 647 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 648 | pr_err("failed to enable PLLE: %d\n", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 649 | return err; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout) |
| 656 | { |
| 657 | const struct tegra_pcie_soc *soc = pcie->soc; |
| 658 | unsigned long start = get_timer(0); |
| 659 | u32 value; |
| 660 | |
| 661 | while (get_timer(start) < timeout) { |
| 662 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 663 | if (value & PADS_PLL_CTL_LOCKDET) |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | return -ETIMEDOUT; |
| 668 | } |
| 669 | |
| 670 | static int tegra_pcie_phy_enable(struct tegra_pcie *pcie) |
| 671 | { |
| 672 | const struct tegra_pcie_soc *soc = pcie->soc; |
| 673 | u32 value; |
| 674 | int err; |
| 675 | |
| 676 | /* initialize internal PHY, enable up to 16 PCIe lanes */ |
| 677 | pads_writel(pcie, 0, PADS_CTL_SEL); |
| 678 | |
| 679 | /* override IDDQ to 1 on all 4 lanes */ |
| 680 | value = pads_readl(pcie, PADS_CTL); |
| 681 | value |= PADS_CTL_IDDQ_1L; |
| 682 | pads_writel(pcie, value, PADS_CTL); |
| 683 | |
| 684 | /* |
| 685 | * Set up PHY PLL inputs select PLLE output as refclock, set TX |
| 686 | * ref sel to div10 (not div5). |
| 687 | */ |
| 688 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 689 | value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK); |
| 690 | value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel; |
| 691 | pads_writel(pcie, value, soc->pads_pll_ctl); |
| 692 | |
| 693 | /* reset PLL */ |
| 694 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 695 | value &= ~PADS_PLL_CTL_RST_B4SM; |
| 696 | pads_writel(pcie, value, soc->pads_pll_ctl); |
| 697 | |
| 698 | udelay(20); |
| 699 | |
| 700 | /* take PLL out of reset */ |
| 701 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 702 | value |= PADS_PLL_CTL_RST_B4SM; |
| 703 | pads_writel(pcie, value, soc->pads_pll_ctl); |
| 704 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 705 | /* wait for the PLL to lock */ |
| 706 | err = tegra_pcie_pll_wait(pcie, 500); |
| 707 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 708 | pr_err("PLL failed to lock: %d", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 709 | return err; |
| 710 | } |
| 711 | |
| 712 | /* turn off IDDQ override */ |
| 713 | value = pads_readl(pcie, PADS_CTL); |
| 714 | value &= ~PADS_CTL_IDDQ_1L; |
| 715 | pads_writel(pcie, value, PADS_CTL); |
| 716 | |
| 717 | /* enable TX/RX data */ |
| 718 | value = pads_readl(pcie, PADS_CTL); |
| 719 | value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L; |
| 720 | pads_writel(pcie, value, PADS_CTL); |
| 721 | |
| 722 | return 0; |
| 723 | } |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 724 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 725 | |
| 726 | static int tegra_pcie_enable_controller(struct tegra_pcie *pcie) |
| 727 | { |
| 728 | const struct tegra_pcie_soc *soc = pcie->soc; |
| 729 | struct tegra_pcie_port *port; |
| 730 | u32 value; |
| 731 | int err; |
| 732 | |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 733 | #ifdef CONFIG_TEGRA186 |
| 734 | { |
| 735 | #else |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 736 | if (pcie->phy) { |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 737 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 738 | value = afi_readl(pcie, AFI_PLLE_CONTROL); |
| 739 | value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL; |
| 740 | value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN; |
| 741 | afi_writel(pcie, value, AFI_PLLE_CONTROL); |
| 742 | } |
| 743 | |
| 744 | if (soc->has_pex_bias_ctrl) |
| 745 | afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0); |
| 746 | |
| 747 | value = afi_readl(pcie, AFI_PCIE_CONFIG); |
| 748 | value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK; |
| 749 | value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar; |
| 750 | |
| 751 | list_for_each_entry(port, &pcie->ports, list) |
| 752 | value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index); |
| 753 | |
| 754 | afi_writel(pcie, value, AFI_PCIE_CONFIG); |
| 755 | |
| 756 | value = afi_readl(pcie, AFI_FUSE); |
| 757 | |
| 758 | if (soc->has_gen2) |
| 759 | value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS; |
| 760 | else |
| 761 | value |= AFI_FUSE_PCIE_T0_GEN2_DIS; |
| 762 | |
| 763 | afi_writel(pcie, value, AFI_FUSE); |
| 764 | |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 765 | #ifndef CONFIG_TEGRA186 |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 766 | if (pcie->phy) |
| 767 | err = tegra_xusb_phy_enable(pcie->phy); |
| 768 | else |
| 769 | err = tegra_pcie_phy_enable(pcie); |
| 770 | |
| 771 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 772 | pr_err("failed to power on PHY: %d\n", err); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 773 | return err; |
| 774 | } |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 775 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 776 | |
| 777 | /* take the PCIEXCLK logic out of reset */ |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 778 | #ifdef CONFIG_TEGRA186 |
| 779 | err = reset_deassert(&pcie->reset_pcie_x); |
| 780 | if (err) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 781 | pr_err("reset_deassert(pcie_x) failed: %d\n", err); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 782 | return err; |
| 783 | } |
| 784 | #else |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 785 | reset_set_enable(PERIPH_ID_PCIEXCLK, 0); |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 786 | #endif |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 787 | |
| 788 | /* finally enable PCIe */ |
| 789 | value = afi_readl(pcie, AFI_CONFIGURATION); |
| 790 | value |= AFI_CONFIGURATION_EN_FPCI; |
| 791 | afi_writel(pcie, value, AFI_CONFIGURATION); |
| 792 | |
| 793 | /* disable all interrupts */ |
| 794 | afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE); |
| 795 | afi_writel(pcie, 0, AFI_SM_INTR_ENABLE); |
| 796 | afi_writel(pcie, 0, AFI_INTR_MASK); |
| 797 | afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 802 | static int tegra_pcie_setup_translations(struct udevice *bus) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 803 | { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 804 | struct tegra_pcie *pcie = dev_get_priv(bus); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 805 | unsigned long fpci, axi, size; |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 806 | struct pci_region *io, *mem, *pref; |
| 807 | int count; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 808 | |
| 809 | /* BAR 0: type 1 extended configuration space */ |
| 810 | fpci = 0xfe100000; |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 811 | size = resource_size(&pcie->cs); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 812 | axi = pcie->cs.start; |
| 813 | |
| 814 | afi_writel(pcie, axi, AFI_AXI_BAR0_START); |
| 815 | afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ); |
| 816 | afi_writel(pcie, fpci, AFI_FPCI_BAR0); |
| 817 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 818 | count = pci_get_regions(bus, &io, &mem, &pref); |
| 819 | if (count != 3) |
| 820 | return -EINVAL; |
| 821 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 822 | /* BAR 1: downstream I/O */ |
| 823 | fpci = 0xfdfc0000; |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 824 | size = io->size; |
| 825 | axi = io->phys_start; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 826 | |
| 827 | afi_writel(pcie, axi, AFI_AXI_BAR1_START); |
| 828 | afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ); |
| 829 | afi_writel(pcie, fpci, AFI_FPCI_BAR1); |
| 830 | |
| 831 | /* BAR 2: prefetchable memory */ |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 832 | fpci = (((pref->phys_start >> 12) & 0x0fffffff) << 4) | 0x1; |
| 833 | size = pref->size; |
| 834 | axi = pref->phys_start; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 835 | |
| 836 | afi_writel(pcie, axi, AFI_AXI_BAR2_START); |
| 837 | afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ); |
| 838 | afi_writel(pcie, fpci, AFI_FPCI_BAR2); |
| 839 | |
| 840 | /* BAR 3: non-prefetchable memory */ |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 841 | fpci = (((mem->phys_start >> 12) & 0x0fffffff) << 4) | 0x1; |
| 842 | size = mem->size; |
| 843 | axi = mem->phys_start; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 844 | |
| 845 | afi_writel(pcie, axi, AFI_AXI_BAR3_START); |
| 846 | afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ); |
| 847 | afi_writel(pcie, fpci, AFI_FPCI_BAR3); |
| 848 | |
| 849 | /* NULL out the remaining BARs as they are not used */ |
| 850 | afi_writel(pcie, 0, AFI_AXI_BAR4_START); |
| 851 | afi_writel(pcie, 0, AFI_AXI_BAR4_SZ); |
| 852 | afi_writel(pcie, 0, AFI_FPCI_BAR4); |
| 853 | |
| 854 | afi_writel(pcie, 0, AFI_AXI_BAR5_START); |
| 855 | afi_writel(pcie, 0, AFI_AXI_BAR5_SZ); |
| 856 | afi_writel(pcie, 0, AFI_FPCI_BAR5); |
| 857 | |
| 858 | /* map all upstream transactions as uncached */ |
| 859 | afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST); |
| 860 | afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ); |
| 861 | afi_writel(pcie, 0, AFI_CACHE_BAR1_ST); |
| 862 | afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ); |
| 863 | |
| 864 | /* MSI translations are setup only when needed */ |
| 865 | afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST); |
| 866 | afi_writel(pcie, 0, AFI_MSI_BAR_SZ); |
| 867 | afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST); |
| 868 | afi_writel(pcie, 0, AFI_MSI_BAR_SZ); |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 869 | |
| 870 | return 0; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port) |
| 874 | { |
| 875 | unsigned long ret = 0; |
| 876 | |
| 877 | switch (port->index) { |
| 878 | case 0: |
| 879 | ret = AFI_PEX0_CTRL; |
| 880 | break; |
| 881 | |
| 882 | case 1: |
| 883 | ret = AFI_PEX1_CTRL; |
| 884 | break; |
| 885 | |
| 886 | case 2: |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 887 | ret = port->pcie->soc->afi_pex2_ctrl; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 888 | break; |
| 889 | } |
| 890 | |
| 891 | return ret; |
| 892 | } |
| 893 | |
Marcel Ziswiler | 53f4863 | 2018-05-08 17:34:09 +0200 | [diff] [blame] | 894 | void tegra_pcie_port_reset(struct tegra_pcie_port *port) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 895 | { |
| 896 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
| 897 | unsigned long value; |
| 898 | |
| 899 | /* pulse reset signel */ |
| 900 | value = afi_readl(port->pcie, ctrl); |
| 901 | value &= ~AFI_PEX_CTRL_RST; |
| 902 | afi_writel(port->pcie, value, ctrl); |
| 903 | |
| 904 | udelay(2000); |
| 905 | |
| 906 | value = afi_readl(port->pcie, ctrl); |
| 907 | value |= AFI_PEX_CTRL_RST; |
| 908 | afi_writel(port->pcie, value, ctrl); |
| 909 | } |
| 910 | |
Marcel Ziswiler | 53f4863 | 2018-05-08 17:34:09 +0200 | [diff] [blame] | 911 | int tegra_pcie_port_index_of_port(struct tegra_pcie_port *port) |
| 912 | { |
| 913 | return port->index; |
| 914 | } |
| 915 | |
| 916 | void __weak tegra_pcie_board_port_reset(struct tegra_pcie_port *port) |
| 917 | { |
| 918 | tegra_pcie_port_reset(port); |
| 919 | } |
| 920 | |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 921 | static void tegra_pcie_port_enable(struct tegra_pcie_port *port) |
| 922 | { |
Stephen Warren | 29f49fc | 2016-06-24 08:36:04 -0600 | [diff] [blame] | 923 | struct tegra_pcie *pcie = port->pcie; |
| 924 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 925 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
| 926 | unsigned long value; |
| 927 | |
| 928 | /* enable reference clock */ |
Stephen Warren | 29f49fc | 2016-06-24 08:36:04 -0600 | [diff] [blame] | 929 | value = afi_readl(pcie, ctrl); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 930 | value |= AFI_PEX_CTRL_REFCLK_EN; |
| 931 | |
Stephen Warren | 29f49fc | 2016-06-24 08:36:04 -0600 | [diff] [blame] | 932 | if (pcie->soc->has_pex_clkreq_en) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 933 | value |= AFI_PEX_CTRL_CLKREQ_EN; |
| 934 | |
| 935 | value |= AFI_PEX_CTRL_OVERRIDE_EN; |
| 936 | |
Stephen Warren | 29f49fc | 2016-06-24 08:36:04 -0600 | [diff] [blame] | 937 | afi_writel(pcie, value, ctrl); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 938 | |
Marcel Ziswiler | 53f4863 | 2018-05-08 17:34:09 +0200 | [diff] [blame] | 939 | tegra_pcie_board_port_reset(port); |
Stephen Warren | e3553be | 2015-10-05 17:00:42 -0600 | [diff] [blame] | 940 | |
| 941 | if (soc->force_pca_enable) { |
| 942 | value = rp_readl(port, RP_VEND_CTL2); |
| 943 | value |= RP_VEND_CTL2_PCA_ENABLE; |
| 944 | rp_writel(port, value, RP_VEND_CTL2); |
| 945 | } |
Stephen Warren | 29f49fc | 2016-06-24 08:36:04 -0600 | [diff] [blame] | 946 | |
| 947 | /* configure the reference clock driver */ |
| 948 | pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0); |
| 949 | if (soc->num_ports > 2) |
| 950 | pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port) |
| 954 | { |
| 955 | unsigned int retries = 3; |
| 956 | unsigned long value; |
| 957 | |
| 958 | value = rp_readl(port, RP_PRIV_MISC); |
| 959 | value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT; |
| 960 | value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT; |
| 961 | rp_writel(port, value, RP_PRIV_MISC); |
| 962 | |
| 963 | do { |
| 964 | unsigned int timeout = 200; |
| 965 | |
| 966 | do { |
| 967 | value = rp_readl(port, RP_VEND_XP); |
| 968 | if (value & RP_VEND_XP_DL_UP) |
| 969 | break; |
| 970 | |
| 971 | udelay(2000); |
| 972 | } while (--timeout); |
| 973 | |
| 974 | if (!timeout) { |
| 975 | debug("link %u down, retrying\n", port->index); |
| 976 | goto retry; |
| 977 | } |
| 978 | |
| 979 | timeout = 200; |
| 980 | |
| 981 | do { |
| 982 | value = rp_readl(port, RP_LINK_CONTROL_STATUS); |
| 983 | if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE) |
| 984 | return true; |
| 985 | |
| 986 | udelay(2000); |
| 987 | } while (--timeout); |
| 988 | |
| 989 | retry: |
Marcel Ziswiler | 53f4863 | 2018-05-08 17:34:09 +0200 | [diff] [blame] | 990 | tegra_pcie_board_port_reset(port); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 991 | } while (--retries); |
| 992 | |
| 993 | return false; |
| 994 | } |
| 995 | |
| 996 | static void tegra_pcie_port_disable(struct tegra_pcie_port *port) |
| 997 | { |
| 998 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
| 999 | unsigned long value; |
| 1000 | |
| 1001 | /* assert port reset */ |
| 1002 | value = afi_readl(port->pcie, ctrl); |
| 1003 | value &= ~AFI_PEX_CTRL_RST; |
| 1004 | afi_writel(port->pcie, value, ctrl); |
| 1005 | |
| 1006 | /* disable reference clock */ |
| 1007 | value = afi_readl(port->pcie, ctrl); |
| 1008 | value &= ~AFI_PEX_CTRL_REFCLK_EN; |
| 1009 | afi_writel(port->pcie, value, ctrl); |
| 1010 | } |
| 1011 | |
| 1012 | static void tegra_pcie_port_free(struct tegra_pcie_port *port) |
| 1013 | { |
| 1014 | list_del(&port->list); |
| 1015 | free(port); |
| 1016 | } |
| 1017 | |
| 1018 | static int tegra_pcie_enable(struct tegra_pcie *pcie) |
| 1019 | { |
| 1020 | struct tegra_pcie_port *port, *tmp; |
| 1021 | |
| 1022 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) { |
| 1023 | debug("probing port %u, using %u lanes\n", port->index, |
| 1024 | port->num_lanes); |
| 1025 | |
| 1026 | tegra_pcie_port_enable(port); |
| 1027 | |
| 1028 | if (tegra_pcie_port_check_link(port)) |
| 1029 | continue; |
| 1030 | |
| 1031 | debug("link %u down, ignoring\n", port->index); |
| 1032 | |
| 1033 | tegra_pcie_port_disable(port); |
| 1034 | tegra_pcie_port_free(port); |
| 1035 | } |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1040 | static const struct tegra_pcie_soc pci_tegra_soc[] = { |
| 1041 | [TEGRA20_PCIE] = { |
| 1042 | .num_ports = 2, |
| 1043 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA20, |
| 1044 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10, |
Stephen Warren | d132c47 | 2016-06-21 12:47:51 -0600 | [diff] [blame] | 1045 | .pads_refclk_cfg0 = 0xfa5cfa5c, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1046 | .has_pex_clkreq_en = false, |
| 1047 | .has_pex_bias_ctrl = false, |
| 1048 | .has_cml_clk = false, |
| 1049 | .has_gen2 = false, |
| 1050 | }, |
| 1051 | [TEGRA30_PCIE] = { |
| 1052 | .num_ports = 3, |
| 1053 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 1054 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 1055 | .afi_pex2_ctrl = AFI_PEX2_CTRL, |
Stephen Warren | d132c47 | 2016-06-21 12:47:51 -0600 | [diff] [blame] | 1056 | .pads_refclk_cfg0 = 0xfa5cfa5c, |
| 1057 | .pads_refclk_cfg1 = 0xfa5cfa5c, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1058 | .has_pex_clkreq_en = true, |
| 1059 | .has_pex_bias_ctrl = true, |
| 1060 | .has_cml_clk = true, |
| 1061 | .has_gen2 = false, |
| 1062 | }, |
| 1063 | [TEGRA124_PCIE] = { |
| 1064 | .num_ports = 2, |
| 1065 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 1066 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
Stephen Warren | d132c47 | 2016-06-21 12:47:51 -0600 | [diff] [blame] | 1067 | .pads_refclk_cfg0 = 0x44ac44ac, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1068 | .has_pex_clkreq_en = true, |
| 1069 | .has_pex_bias_ctrl = true, |
| 1070 | .has_cml_clk = true, |
| 1071 | .has_gen2 = true, |
| 1072 | }, |
| 1073 | [TEGRA210_PCIE] = { |
| 1074 | .num_ports = 2, |
| 1075 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 1076 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
Stephen Warren | d132c47 | 2016-06-21 12:47:51 -0600 | [diff] [blame] | 1077 | .pads_refclk_cfg0 = 0x90b890b8, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1078 | .has_pex_clkreq_en = true, |
| 1079 | .has_pex_bias_ctrl = true, |
| 1080 | .has_cml_clk = true, |
| 1081 | .has_gen2 = true, |
| 1082 | .force_pca_enable = true, |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 1083 | }, |
| 1084 | [TEGRA186_PCIE] = { |
| 1085 | .num_ports = 3, |
| 1086 | .afi_pex2_ctrl = AFI_PEX2_CTRL_T186, |
| 1087 | .pads_refclk_cfg0 = 0x80b880b8, |
| 1088 | .pads_refclk_cfg1 = 0x000480b8, |
| 1089 | .has_pex_clkreq_en = true, |
| 1090 | .has_pex_bias_ctrl = true, |
| 1091 | .has_gen2 = true, |
| 1092 | }, |
Stephen Warren | 5e44dce | 2015-10-05 17:00:44 -0600 | [diff] [blame] | 1093 | }; |
| 1094 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1095 | static int pci_tegra_ofdata_to_platdata(struct udevice *dev) |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1096 | { |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1097 | struct tegra_pcie *pcie = dev_get_priv(dev); |
| 1098 | enum tegra_pci_id id; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1099 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1100 | id = dev_get_driver_data(dev); |
| 1101 | pcie->soc = &pci_tegra_soc[id]; |
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 | INIT_LIST_HEAD(&pcie->ports); |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1104 | |
Simon Glass | e6a30e0 | 2017-07-25 08:30:09 -0600 | [diff] [blame] | 1105 | if (tegra_pcie_parse_dt(dev, id, pcie)) |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1106 | return -EINVAL; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1107 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1110 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1111 | static int pci_tegra_probe(struct udevice *dev) |
| 1112 | { |
| 1113 | struct tegra_pcie *pcie = dev_get_priv(dev); |
| 1114 | int err; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1115 | |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 1116 | #ifdef CONFIG_TEGRA186 |
| 1117 | err = clk_get_by_name(dev, "afi", &pcie->clk_afi); |
| 1118 | if (err) { |
| 1119 | debug("clk_get_by_name(afi) failed: %d\n", err); |
| 1120 | return err; |
| 1121 | } |
| 1122 | |
| 1123 | err = clk_get_by_name(dev, "pex", &pcie->clk_pex); |
| 1124 | if (err) { |
| 1125 | debug("clk_get_by_name(pex) failed: %d\n", err); |
| 1126 | return err; |
| 1127 | } |
| 1128 | |
| 1129 | err = reset_get_by_name(dev, "afi", &pcie->reset_afi); |
| 1130 | if (err) { |
| 1131 | debug("reset_get_by_name(afi) failed: %d\n", err); |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
| 1135 | err = reset_get_by_name(dev, "pex", &pcie->reset_pex); |
| 1136 | if (err) { |
| 1137 | debug("reset_get_by_name(pex) failed: %d\n", err); |
| 1138 | return err; |
| 1139 | } |
| 1140 | |
| 1141 | err = reset_get_by_name(dev, "pcie_x", &pcie->reset_pcie_x); |
| 1142 | if (err) { |
| 1143 | debug("reset_get_by_name(pcie_x) failed: %d\n", err); |
| 1144 | return err; |
| 1145 | } |
| 1146 | |
| 1147 | err = power_domain_get(dev, &pcie->pwrdom); |
| 1148 | if (err) { |
| 1149 | debug("power_domain_get() failed: %d\n", err); |
| 1150 | return err; |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1154 | err = tegra_pcie_power_on(pcie); |
| 1155 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1156 | pr_err("failed to power on"); |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1157 | return err; |
| 1158 | } |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1159 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1160 | err = tegra_pcie_enable_controller(pcie); |
| 1161 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1162 | pr_err("failed to enable controller"); |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1163 | return err; |
| 1164 | } |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1165 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1166 | err = tegra_pcie_setup_translations(dev); |
| 1167 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1168 | pr_err("failed to decode ranges"); |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1169 | return err; |
| 1170 | } |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1171 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1172 | err = tegra_pcie_enable(pcie); |
| 1173 | if (err < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1174 | pr_err("failed to enable PCIe"); |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1175 | return err; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1181 | static const struct dm_pci_ops pci_tegra_ops = { |
| 1182 | .read_config = pci_tegra_read_config, |
| 1183 | .write_config = pci_tegra_write_config, |
| 1184 | }; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1185 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1186 | static const struct udevice_id pci_tegra_ids[] = { |
| 1187 | { .compatible = "nvidia,tegra20-pcie", .data = TEGRA20_PCIE }, |
| 1188 | { .compatible = "nvidia,tegra30-pcie", .data = TEGRA30_PCIE }, |
| 1189 | { .compatible = "nvidia,tegra124-pcie", .data = TEGRA124_PCIE }, |
| 1190 | { .compatible = "nvidia,tegra210-pcie", .data = TEGRA210_PCIE }, |
Stephen Warren | 86f6a94 | 2016-08-05 16:10:34 -0600 | [diff] [blame] | 1191 | { .compatible = "nvidia,tegra186-pcie", .data = TEGRA186_PCIE }, |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1192 | { } |
| 1193 | }; |
Thierry Reding | 412c758 | 2014-12-09 22:25:12 -0700 | [diff] [blame] | 1194 | |
Simon Glass | 46fcfc1 | 2015-11-19 20:27:02 -0700 | [diff] [blame] | 1195 | U_BOOT_DRIVER(pci_tegra) = { |
| 1196 | .name = "pci_tegra", |
| 1197 | .id = UCLASS_PCI, |
| 1198 | .of_match = pci_tegra_ids, |
| 1199 | .ops = &pci_tegra_ops, |
| 1200 | .ofdata_to_platdata = pci_tegra_ofdata_to_platdata, |
| 1201 | .probe = pci_tegra_probe, |
| 1202 | .priv_auto_alloc_size = sizeof(struct tegra_pcie), |
| 1203 | }; |