Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1 | /* |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2 | * PCIe host controller driver for Tegra SoCs |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2010, CompuLab, Ltd. |
| 5 | * Author: Mike Rapoport <mike@compulab.co.il> |
| 6 | * |
| 7 | * Based on NVIDIA PCIe driver |
| 8 | * Copyright (c) 2008-2009, NVIDIA Corporation. |
| 9 | * |
| 10 | * Bits taken from arch/arm/mach-dove/pcie.c |
| 11 | * |
Paul Gortmaker | ad18327 | 2016-07-02 19:13:31 -0400 | [diff] [blame] | 12 | * Author: Thierry Reding <treding@nvidia.com> |
| 13 | * |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 22 | * more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License along |
| 25 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 26 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/clk.h> |
Thierry Reding | 2cb989f | 2014-07-22 12:30:46 -0600 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/export.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/irq.h> |
| 35 | #include <linux/irqdomain.h> |
| 36 | #include <linux/kernel.h> |
Paul Gortmaker | ad18327 | 2016-07-02 19:13:31 -0400 | [diff] [blame] | 37 | #include <linux/init.h> |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 38 | #include <linux/msi.h> |
| 39 | #include <linux/of_address.h> |
| 40 | #include <linux/of_pci.h> |
| 41 | #include <linux/of_platform.h> |
| 42 | #include <linux/pci.h> |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 43 | #include <linux/phy/phy.h> |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 44 | #include <linux/platform_device.h> |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 45 | #include <linux/reset.h> |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 46 | #include <linux/sizes.h> |
| 47 | #include <linux/slab.h> |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 48 | #include <linux/vmalloc.h> |
| 49 | #include <linux/regulator/consumer.h> |
| 50 | |
Thierry Reding | 306a7f9 | 2014-07-17 13:17:24 +0200 | [diff] [blame] | 51 | #include <soc/tegra/cpuidle.h> |
Thierry Reding | 7232398 | 2014-07-11 13:19:06 +0200 | [diff] [blame] | 52 | #include <soc/tegra/pmc.h> |
Thierry Reding | 306a7f9 | 2014-07-17 13:17:24 +0200 | [diff] [blame] | 53 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 54 | #define INT_PCI_MSI_NR (8 * 32) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 55 | |
| 56 | /* register definitions */ |
| 57 | |
| 58 | #define AFI_AXI_BAR0_SZ 0x00 |
| 59 | #define AFI_AXI_BAR1_SZ 0x04 |
| 60 | #define AFI_AXI_BAR2_SZ 0x08 |
| 61 | #define AFI_AXI_BAR3_SZ 0x0c |
| 62 | #define AFI_AXI_BAR4_SZ 0x10 |
| 63 | #define AFI_AXI_BAR5_SZ 0x14 |
| 64 | |
| 65 | #define AFI_AXI_BAR0_START 0x18 |
| 66 | #define AFI_AXI_BAR1_START 0x1c |
| 67 | #define AFI_AXI_BAR2_START 0x20 |
| 68 | #define AFI_AXI_BAR3_START 0x24 |
| 69 | #define AFI_AXI_BAR4_START 0x28 |
| 70 | #define AFI_AXI_BAR5_START 0x2c |
| 71 | |
| 72 | #define AFI_FPCI_BAR0 0x30 |
| 73 | #define AFI_FPCI_BAR1 0x34 |
| 74 | #define AFI_FPCI_BAR2 0x38 |
| 75 | #define AFI_FPCI_BAR3 0x3c |
| 76 | #define AFI_FPCI_BAR4 0x40 |
| 77 | #define AFI_FPCI_BAR5 0x44 |
| 78 | |
| 79 | #define AFI_CACHE_BAR0_SZ 0x48 |
| 80 | #define AFI_CACHE_BAR0_ST 0x4c |
| 81 | #define AFI_CACHE_BAR1_SZ 0x50 |
| 82 | #define AFI_CACHE_BAR1_ST 0x54 |
| 83 | |
| 84 | #define AFI_MSI_BAR_SZ 0x60 |
| 85 | #define AFI_MSI_FPCI_BAR_ST 0x64 |
| 86 | #define AFI_MSI_AXI_BAR_ST 0x68 |
| 87 | |
| 88 | #define AFI_MSI_VEC0 0x6c |
| 89 | #define AFI_MSI_VEC1 0x70 |
| 90 | #define AFI_MSI_VEC2 0x74 |
| 91 | #define AFI_MSI_VEC3 0x78 |
| 92 | #define AFI_MSI_VEC4 0x7c |
| 93 | #define AFI_MSI_VEC5 0x80 |
| 94 | #define AFI_MSI_VEC6 0x84 |
| 95 | #define AFI_MSI_VEC7 0x88 |
| 96 | |
| 97 | #define AFI_MSI_EN_VEC0 0x8c |
| 98 | #define AFI_MSI_EN_VEC1 0x90 |
| 99 | #define AFI_MSI_EN_VEC2 0x94 |
| 100 | #define AFI_MSI_EN_VEC3 0x98 |
| 101 | #define AFI_MSI_EN_VEC4 0x9c |
| 102 | #define AFI_MSI_EN_VEC5 0xa0 |
| 103 | #define AFI_MSI_EN_VEC6 0xa4 |
| 104 | #define AFI_MSI_EN_VEC7 0xa8 |
| 105 | |
| 106 | #define AFI_CONFIGURATION 0xac |
| 107 | #define AFI_CONFIGURATION_EN_FPCI (1 << 0) |
| 108 | |
| 109 | #define AFI_FPCI_ERROR_MASKS 0xb0 |
| 110 | |
| 111 | #define AFI_INTR_MASK 0xb4 |
| 112 | #define AFI_INTR_MASK_INT_MASK (1 << 0) |
| 113 | #define AFI_INTR_MASK_MSI_MASK (1 << 8) |
| 114 | |
| 115 | #define AFI_INTR_CODE 0xb8 |
| 116 | #define AFI_INTR_CODE_MASK 0xf |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 117 | #define AFI_INTR_INI_SLAVE_ERROR 1 |
| 118 | #define AFI_INTR_INI_DECODE_ERROR 2 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 119 | #define AFI_INTR_TARGET_ABORT 3 |
| 120 | #define AFI_INTR_MASTER_ABORT 4 |
| 121 | #define AFI_INTR_INVALID_WRITE 5 |
| 122 | #define AFI_INTR_LEGACY 6 |
| 123 | #define AFI_INTR_FPCI_DECODE_ERROR 7 |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 124 | #define AFI_INTR_AXI_DECODE_ERROR 8 |
| 125 | #define AFI_INTR_FPCI_TIMEOUT 9 |
| 126 | #define AFI_INTR_PE_PRSNT_SENSE 10 |
| 127 | #define AFI_INTR_PE_CLKREQ_SENSE 11 |
| 128 | #define AFI_INTR_CLKCLAMP_SENSE 12 |
| 129 | #define AFI_INTR_RDY4PD_SENSE 13 |
| 130 | #define AFI_INTR_P2P_ERROR 14 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 131 | |
| 132 | #define AFI_INTR_SIGNATURE 0xbc |
| 133 | #define AFI_UPPER_FPCI_ADDRESS 0xc0 |
| 134 | #define AFI_SM_INTR_ENABLE 0xc4 |
| 135 | #define AFI_SM_INTR_INTA_ASSERT (1 << 0) |
| 136 | #define AFI_SM_INTR_INTB_ASSERT (1 << 1) |
| 137 | #define AFI_SM_INTR_INTC_ASSERT (1 << 2) |
| 138 | #define AFI_SM_INTR_INTD_ASSERT (1 << 3) |
| 139 | #define AFI_SM_INTR_INTA_DEASSERT (1 << 4) |
| 140 | #define AFI_SM_INTR_INTB_DEASSERT (1 << 5) |
| 141 | #define AFI_SM_INTR_INTC_DEASSERT (1 << 6) |
| 142 | #define AFI_SM_INTR_INTD_DEASSERT (1 << 7) |
| 143 | |
| 144 | #define AFI_AFI_INTR_ENABLE 0xc8 |
| 145 | #define AFI_INTR_EN_INI_SLVERR (1 << 0) |
| 146 | #define AFI_INTR_EN_INI_DECERR (1 << 1) |
| 147 | #define AFI_INTR_EN_TGT_SLVERR (1 << 2) |
| 148 | #define AFI_INTR_EN_TGT_DECERR (1 << 3) |
| 149 | #define AFI_INTR_EN_TGT_WRERR (1 << 4) |
| 150 | #define AFI_INTR_EN_DFPCI_DECERR (1 << 5) |
| 151 | #define AFI_INTR_EN_AXI_DECERR (1 << 6) |
| 152 | #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 153 | #define AFI_INTR_EN_PRSNT_SENSE (1 << 8) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 154 | |
| 155 | #define AFI_PCIE_CONFIG 0x0f8 |
| 156 | #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1)) |
| 157 | #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe |
| 158 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20) |
| 159 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 160 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20) |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 161 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 162 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 163 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20) |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 164 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 165 | #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 166 | |
| 167 | #define AFI_FUSE 0x104 |
| 168 | #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2) |
| 169 | |
| 170 | #define AFI_PEX0_CTRL 0x110 |
| 171 | #define AFI_PEX1_CTRL 0x118 |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 172 | #define AFI_PEX2_CTRL 0x128 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 173 | #define AFI_PEX_CTRL_RST (1 << 0) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 174 | #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 175 | #define AFI_PEX_CTRL_REFCLK_EN (1 << 3) |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 176 | #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4) |
| 177 | |
| 178 | #define AFI_PLLE_CONTROL 0x160 |
| 179 | #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9) |
| 180 | #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 181 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 182 | #define AFI_PEXBIAS_CTRL_0 0x168 |
| 183 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 184 | #define RP_VEND_XP 0x00000f00 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 185 | #define RP_VEND_XP_DL_UP (1 << 30) |
| 186 | |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 187 | #define RP_VEND_CTL2 0x00000fa8 |
| 188 | #define RP_VEND_CTL2_PCA_ENABLE (1 << 7) |
| 189 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 190 | #define RP_PRIV_MISC 0x00000fe0 |
| 191 | #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0) |
| 192 | #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0) |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 193 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 194 | #define RP_LINK_CONTROL_STATUS 0x00000090 |
| 195 | #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000 |
| 196 | #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000 |
| 197 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 198 | #define PADS_CTL_SEL 0x0000009c |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 199 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 200 | #define PADS_CTL 0x000000a0 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 201 | #define PADS_CTL_IDDQ_1L (1 << 0) |
| 202 | #define PADS_CTL_TX_DATA_EN_1L (1 << 6) |
| 203 | #define PADS_CTL_RX_DATA_EN_1L (1 << 10) |
| 204 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 205 | #define PADS_PLL_CTL_TEGRA20 0x000000b8 |
| 206 | #define PADS_PLL_CTL_TEGRA30 0x000000b4 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 207 | #define PADS_PLL_CTL_RST_B4SM (1 << 1) |
| 208 | #define PADS_PLL_CTL_LOCKDET (1 << 8) |
| 209 | #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16) |
| 210 | #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16) |
| 211 | #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16) |
| 212 | #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16) |
| 213 | #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20) |
| 214 | #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20) |
| 215 | #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20) |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 216 | #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22) |
| 217 | |
Thierry Reding | acb341e | 2016-07-25 16:02:05 -0500 | [diff] [blame] | 218 | #define PADS_REFCLK_CFG0 0x000000c8 |
| 219 | #define PADS_REFCLK_CFG1 0x000000cc |
| 220 | #define PADS_REFCLK_BIAS 0x000000d0 |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 221 | |
Stephen Warren | b02b07a | 2013-08-09 16:49:25 +0200 | [diff] [blame] | 222 | /* |
| 223 | * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit |
| 224 | * entries, one entry per PCIe port. These field definitions and desired |
| 225 | * values aren't in the TRM, but do come from NVIDIA. |
| 226 | */ |
| 227 | #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */ |
| 228 | #define PADS_REFCLK_CFG_E_TERM_SHIFT 7 |
| 229 | #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */ |
| 230 | #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */ |
| 231 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 232 | struct tegra_msi { |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 233 | struct msi_controller chip; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 234 | DECLARE_BITMAP(used, INT_PCI_MSI_NR); |
| 235 | struct irq_domain *domain; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 236 | struct mutex lock; |
Thierry Reding | c016555 | 2017-05-04 22:10:31 +0200 | [diff] [blame] | 237 | u64 phys; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 238 | int irq; |
| 239 | }; |
| 240 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 241 | /* used to differentiate between Tegra SoC generations */ |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 242 | struct tegra_pcie_soc { |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 243 | unsigned int num_ports; |
| 244 | unsigned int msi_base_shift; |
| 245 | u32 pads_pll_ctl; |
| 246 | u32 tx_ref_sel; |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 247 | u32 pads_refclk_cfg0; |
| 248 | u32 pads_refclk_cfg1; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 249 | bool has_pex_clkreq_en; |
| 250 | bool has_pex_bias_ctrl; |
| 251 | bool has_intr_prsnt_sense; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 252 | bool has_cml_clk; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 253 | bool has_gen2; |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 254 | bool force_pca_enable; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 255 | }; |
| 256 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 257 | static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 258 | { |
| 259 | return container_of(chip, struct tegra_msi, chip); |
| 260 | } |
| 261 | |
| 262 | struct tegra_pcie { |
| 263 | struct device *dev; |
| 264 | |
| 265 | void __iomem *pads; |
| 266 | void __iomem *afi; |
| 267 | int irq; |
| 268 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 269 | struct list_head buses; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 270 | struct resource *cs; |
| 271 | |
| 272 | struct resource io; |
Thierry Reding | 5106787 | 2014-11-27 09:54:09 +0100 | [diff] [blame] | 273 | struct resource pio; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 274 | struct resource mem; |
| 275 | struct resource prefetch; |
| 276 | struct resource busn; |
| 277 | |
Thierry Reding | 56e75e2 | 2016-02-09 15:52:32 +0100 | [diff] [blame] | 278 | struct { |
| 279 | resource_size_t mem; |
| 280 | resource_size_t io; |
| 281 | } offset; |
| 282 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 283 | struct clk *pex_clk; |
| 284 | struct clk *afi_clk; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 285 | struct clk *pll_e; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 286 | struct clk *cml_clk; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 287 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 288 | struct reset_control *pex_rst; |
| 289 | struct reset_control *afi_rst; |
| 290 | struct reset_control *pcie_xrst; |
| 291 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 292 | bool legacy_phy; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 293 | struct phy *phy; |
| 294 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 295 | struct tegra_msi msi; |
| 296 | |
| 297 | struct list_head ports; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 298 | u32 xbar_config; |
| 299 | |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 300 | struct regulator_bulk_data *supplies; |
| 301 | unsigned int num_supplies; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 302 | |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 303 | const struct tegra_pcie_soc *soc; |
Thierry Reding | 2cb989f | 2014-07-22 12:30:46 -0600 | [diff] [blame] | 304 | struct dentry *debugfs; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | struct tegra_pcie_port { |
| 308 | struct tegra_pcie *pcie; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 309 | struct device_node *np; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 310 | struct list_head list; |
| 311 | struct resource regs; |
| 312 | void __iomem *base; |
| 313 | unsigned int index; |
| 314 | unsigned int lanes; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 315 | |
| 316 | struct phy **phys; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | struct tegra_pcie_bus { |
| 320 | struct vm_struct *area; |
| 321 | struct list_head list; |
| 322 | unsigned int nr; |
| 323 | }; |
| 324 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 325 | static inline void afi_writel(struct tegra_pcie *pcie, u32 value, |
| 326 | unsigned long offset) |
| 327 | { |
| 328 | writel(value, pcie->afi + offset); |
| 329 | } |
| 330 | |
| 331 | static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset) |
| 332 | { |
| 333 | return readl(pcie->afi + offset); |
| 334 | } |
| 335 | |
| 336 | static inline void pads_writel(struct tegra_pcie *pcie, u32 value, |
| 337 | unsigned long offset) |
| 338 | { |
| 339 | writel(value, pcie->pads + offset); |
| 340 | } |
| 341 | |
| 342 | static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset) |
| 343 | { |
| 344 | return readl(pcie->pads + offset); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * The configuration space mapping on Tegra is somewhat similar to the ECAM |
| 349 | * defined by PCIe. However it deviates a bit in how the 4 bits for extended |
| 350 | * register accesses are mapped: |
| 351 | * |
| 352 | * [27:24] extended register number |
| 353 | * [23:16] bus number |
| 354 | * [15:11] device number |
| 355 | * [10: 8] function number |
| 356 | * [ 7: 0] register number |
| 357 | * |
| 358 | * Mapping the whole extended configuration space would require 256 MiB of |
| 359 | * virtual address space, only a small part of which will actually be used. |
| 360 | * To work around this, a 1 MiB of virtual addresses are allocated per bus |
| 361 | * when the bus is first accessed. When the physical range is mapped, the |
| 362 | * the bus number bits are hidden so that the extended register number bits |
| 363 | * appear as bits [19:16]. Therefore the virtual mapping looks like this: |
| 364 | * |
| 365 | * [19:16] extended register number |
| 366 | * [15:11] device number |
| 367 | * [10: 8] function number |
| 368 | * [ 7: 0] register number |
| 369 | * |
| 370 | * This is achieved by stitching together 16 chunks of 64 KiB of physical |
| 371 | * address space via the MMU. |
| 372 | */ |
| 373 | static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where) |
| 374 | { |
| 375 | return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) | |
| 376 | (PCI_FUNC(devfn) << 8) | (where & 0xfc); |
| 377 | } |
| 378 | |
| 379 | static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie, |
| 380 | unsigned int busnr) |
| 381 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 382 | struct device *dev = pcie->dev; |
Lorenzo Pieralisi | 3e02dc4 | 2017-04-19 17:49:06 +0100 | [diff] [blame] | 383 | pgprot_t prot = pgprot_noncached(PAGE_KERNEL); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 384 | phys_addr_t cs = pcie->cs->start; |
| 385 | struct tegra_pcie_bus *bus; |
| 386 | unsigned int i; |
| 387 | int err; |
| 388 | |
| 389 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
| 390 | if (!bus) |
| 391 | return ERR_PTR(-ENOMEM); |
| 392 | |
| 393 | INIT_LIST_HEAD(&bus->list); |
| 394 | bus->nr = busnr; |
| 395 | |
| 396 | /* allocate 1 MiB of virtual addresses */ |
| 397 | bus->area = get_vm_area(SZ_1M, VM_IOREMAP); |
| 398 | if (!bus->area) { |
| 399 | err = -ENOMEM; |
| 400 | goto free; |
| 401 | } |
| 402 | |
| 403 | /* map each of the 16 chunks of 64 KiB each */ |
| 404 | for (i = 0; i < 16; i++) { |
| 405 | unsigned long virt = (unsigned long)bus->area->addr + |
| 406 | i * SZ_64K; |
Peter Daifuku | 8d41794 | 2014-08-26 17:11:36 +0200 | [diff] [blame] | 407 | phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 408 | |
| 409 | err = ioremap_page_range(virt, virt + SZ_64K, phys, prot); |
| 410 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 411 | dev_err(dev, "ioremap_page_range() failed: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 412 | goto unmap; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return bus; |
| 417 | |
| 418 | unmap: |
| 419 | vunmap(bus->area->addr); |
| 420 | free: |
| 421 | kfree(bus); |
| 422 | return ERR_PTR(err); |
| 423 | } |
| 424 | |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 425 | static int tegra_pcie_add_bus(struct pci_bus *bus) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 426 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 427 | struct pci_host_bridge *host = pci_find_host_bridge(bus); |
| 428 | struct tegra_pcie *pcie = pci_host_bridge_priv(host); |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 429 | struct tegra_pcie_bus *b; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 430 | |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 431 | b = tegra_pcie_bus_alloc(pcie, bus->number); |
| 432 | if (IS_ERR(b)) |
| 433 | return PTR_ERR(b); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 434 | |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 435 | list_add_tail(&b->list, &pcie->buses); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 436 | |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 437 | return 0; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 440 | static void tegra_pcie_remove_bus(struct pci_bus *child) |
| 441 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 442 | struct pci_host_bridge *host = pci_find_host_bridge(child); |
| 443 | struct tegra_pcie *pcie = pci_host_bridge_priv(host); |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 444 | struct tegra_pcie_bus *bus, *tmp; |
| 445 | |
| 446 | list_for_each_entry_safe(bus, tmp, &pcie->buses, list) { |
| 447 | if (bus->nr == child->number) { |
| 448 | vunmap(bus->area->addr); |
| 449 | list_del(&bus->list); |
| 450 | kfree(bus); |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus, |
| 457 | unsigned int devfn, |
| 458 | int where) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 459 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 460 | struct pci_host_bridge *host = pci_find_host_bridge(bus); |
| 461 | struct tegra_pcie *pcie = pci_host_bridge_priv(host); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 462 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 463 | void __iomem *addr = NULL; |
| 464 | |
| 465 | if (bus->number == 0) { |
| 466 | unsigned int slot = PCI_SLOT(devfn); |
| 467 | struct tegra_pcie_port *port; |
| 468 | |
| 469 | list_for_each_entry(port, &pcie->ports, list) { |
| 470 | if (port->index + 1 == slot) { |
| 471 | addr = port->base + (where & ~3); |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } else { |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 476 | struct tegra_pcie_bus *b; |
| 477 | |
| 478 | list_for_each_entry(b, &pcie->buses, list) |
| 479 | if (b->nr == bus->number) |
| 480 | addr = (void __iomem *)b->area->addr; |
| 481 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 482 | if (!addr) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 483 | dev_err(dev, "failed to map cfg. space for bus %u\n", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 484 | bus->number); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | addr += tegra_pcie_conf_offset(devfn, where); |
| 489 | } |
| 490 | |
| 491 | return addr; |
| 492 | } |
| 493 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 494 | static struct pci_ops tegra_pcie_ops = { |
Thierry Reding | b4d18d7 | 2016-02-09 15:30:48 +0100 | [diff] [blame] | 495 | .add_bus = tegra_pcie_add_bus, |
| 496 | .remove_bus = tegra_pcie_remove_bus, |
| 497 | .map_bus = tegra_pcie_map_bus, |
Rob Herring | 0e7ac8d | 2015-01-09 20:34:48 -0600 | [diff] [blame] | 498 | .read = pci_generic_config_read32, |
| 499 | .write = pci_generic_config_write32, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port) |
| 503 | { |
| 504 | unsigned long ret = 0; |
| 505 | |
| 506 | switch (port->index) { |
| 507 | case 0: |
| 508 | ret = AFI_PEX0_CTRL; |
| 509 | break; |
| 510 | |
| 511 | case 1: |
| 512 | ret = AFI_PEX1_CTRL; |
| 513 | break; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 514 | |
| 515 | case 2: |
| 516 | ret = AFI_PEX2_CTRL; |
| 517 | break; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static void tegra_pcie_port_reset(struct tegra_pcie_port *port) |
| 524 | { |
| 525 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
| 526 | unsigned long value; |
| 527 | |
| 528 | /* pulse reset signal */ |
| 529 | value = afi_readl(port->pcie, ctrl); |
| 530 | value &= ~AFI_PEX_CTRL_RST; |
| 531 | afi_writel(port->pcie, value, ctrl); |
| 532 | |
| 533 | usleep_range(1000, 2000); |
| 534 | |
| 535 | value = afi_readl(port->pcie, ctrl); |
| 536 | value |= AFI_PEX_CTRL_RST; |
| 537 | afi_writel(port->pcie, value, ctrl); |
| 538 | } |
| 539 | |
| 540 | static void tegra_pcie_port_enable(struct tegra_pcie_port *port) |
| 541 | { |
| 542 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 543 | const struct tegra_pcie_soc *soc = port->pcie->soc; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 544 | unsigned long value; |
| 545 | |
| 546 | /* enable reference clock */ |
| 547 | value = afi_readl(port->pcie, ctrl); |
| 548 | value |= AFI_PEX_CTRL_REFCLK_EN; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 549 | |
| 550 | if (soc->has_pex_clkreq_en) |
| 551 | value |= AFI_PEX_CTRL_CLKREQ_EN; |
| 552 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 553 | value |= AFI_PEX_CTRL_OVERRIDE_EN; |
| 554 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 555 | afi_writel(port->pcie, value, ctrl); |
| 556 | |
| 557 | tegra_pcie_port_reset(port); |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 558 | |
| 559 | if (soc->force_pca_enable) { |
| 560 | value = readl(port->base + RP_VEND_CTL2); |
| 561 | value |= RP_VEND_CTL2_PCA_ENABLE; |
| 562 | writel(value, port->base + RP_VEND_CTL2); |
| 563 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static void tegra_pcie_port_disable(struct tegra_pcie_port *port) |
| 567 | { |
| 568 | unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port); |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 569 | const struct tegra_pcie_soc *soc = port->pcie->soc; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 570 | unsigned long value; |
| 571 | |
| 572 | /* assert port reset */ |
| 573 | value = afi_readl(port->pcie, ctrl); |
| 574 | value &= ~AFI_PEX_CTRL_RST; |
| 575 | afi_writel(port->pcie, value, ctrl); |
| 576 | |
| 577 | /* disable reference clock */ |
| 578 | value = afi_readl(port->pcie, ctrl); |
Thierry Reding | 0d20d62 | 2014-08-26 17:11:35 +0200 | [diff] [blame] | 579 | |
| 580 | if (soc->has_pex_clkreq_en) |
| 581 | value &= ~AFI_PEX_CTRL_CLKREQ_EN; |
| 582 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 583 | value &= ~AFI_PEX_CTRL_REFCLK_EN; |
| 584 | afi_writel(port->pcie, value, ctrl); |
| 585 | } |
| 586 | |
| 587 | static void tegra_pcie_port_free(struct tegra_pcie_port *port) |
| 588 | { |
| 589 | struct tegra_pcie *pcie = port->pcie; |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 590 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 591 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 592 | devm_iounmap(dev, port->base); |
| 593 | devm_release_mem_region(dev, port->regs.start, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 594 | resource_size(&port->regs)); |
| 595 | list_del(&port->list); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 596 | devm_kfree(dev, port); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 599 | /* Tegra PCIE root complex wrongly reports device class */ |
| 600 | static void tegra_pcie_fixup_class(struct pci_dev *dev) |
| 601 | { |
| 602 | dev->class = PCI_CLASS_BRIDGE_PCI << 8; |
| 603 | } |
| 604 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class); |
| 605 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 606 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class); |
| 607 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 608 | |
| 609 | /* Tegra PCIE requires relaxed ordering */ |
| 610 | static void tegra_pcie_relax_enable(struct pci_dev *dev) |
| 611 | { |
| 612 | pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN); |
| 613 | } |
| 614 | DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable); |
| 615 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 616 | static int tegra_pcie_request_resources(struct tegra_pcie *pcie) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 617 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 618 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
| 619 | struct list_head *windows = &host->windows; |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 620 | struct device *dev = pcie->dev; |
Thierry Reding | 41534e5 | 2014-08-01 14:15:11 +0200 | [diff] [blame] | 621 | int err; |
| 622 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 623 | pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io); |
| 624 | pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem); |
| 625 | pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem); |
| 626 | pci_add_resource(windows, &pcie->busn); |
Thierry Reding | 56e75e2 | 2016-02-09 15:52:32 +0100 | [diff] [blame] | 627 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 628 | err = devm_request_pci_bus_resources(dev, windows); |
Thierry Reding | 56e75e2 | 2016-02-09 15:52:32 +0100 | [diff] [blame] | 629 | if (err < 0) |
| 630 | return err; |
| 631 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 632 | pci_remap_iospace(&pcie->pio, pcie->io.start); |
Lorenzo Pieralisi | 13f392e | 2016-08-15 17:50:46 +0100 | [diff] [blame] | 633 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 634 | return 0; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin) |
| 638 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 639 | struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus); |
| 640 | struct tegra_pcie *pcie = pci_host_bridge_priv(host); |
Lucas Stach | f5d3352 | 2014-04-16 10:24:32 -0600 | [diff] [blame] | 641 | int irq; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 642 | |
Stephen Warren | b4f1737 | 2013-05-06 14:19:19 -0600 | [diff] [blame] | 643 | tegra_cpuidle_pcie_irqs_in_use(); |
| 644 | |
Lucas Stach | f5d3352 | 2014-04-16 10:24:32 -0600 | [diff] [blame] | 645 | irq = of_irq_parse_and_map_pci(pdev, slot, pin); |
| 646 | if (!irq) |
| 647 | irq = pcie->irq; |
| 648 | |
| 649 | return irq; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 652 | static irqreturn_t tegra_pcie_isr(int irq, void *arg) |
| 653 | { |
| 654 | const char *err_msg[] = { |
| 655 | "Unknown", |
| 656 | "AXI slave error", |
| 657 | "AXI decode error", |
| 658 | "Target abort", |
| 659 | "Master abort", |
| 660 | "Invalid write", |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 661 | "Legacy interrupt", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 662 | "Response decoding error", |
| 663 | "AXI response decoding error", |
| 664 | "Transaction timeout", |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 665 | "Slot present pin change", |
| 666 | "Slot clock request change", |
| 667 | "TMS clock ramp change", |
| 668 | "TMS ready for power down", |
| 669 | "Peer2Peer error", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 670 | }; |
| 671 | struct tegra_pcie *pcie = arg; |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 672 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 673 | u32 code, signature; |
| 674 | |
| 675 | code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK; |
| 676 | signature = afi_readl(pcie, AFI_INTR_SIGNATURE); |
| 677 | afi_writel(pcie, 0, AFI_INTR_CODE); |
| 678 | |
| 679 | if (code == AFI_INTR_LEGACY) |
| 680 | return IRQ_NONE; |
| 681 | |
| 682 | if (code >= ARRAY_SIZE(err_msg)) |
| 683 | code = 0; |
| 684 | |
| 685 | /* |
| 686 | * do not pollute kernel log with master abort reports since they |
| 687 | * happen a lot during enumeration |
| 688 | */ |
| 689 | if (code == AFI_INTR_MASTER_ABORT) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 690 | dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 691 | else |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 692 | dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 693 | |
| 694 | if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT || |
| 695 | code == AFI_INTR_FPCI_DECODE_ERROR) { |
| 696 | u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff; |
| 697 | u64 address = (u64)fpci << 32 | (signature & 0xfffffffc); |
| 698 | |
| 699 | if (code == AFI_INTR_MASTER_ABORT) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 700 | dev_dbg(dev, " FPCI address: %10llx\n", address); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 701 | else |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 702 | dev_err(dev, " FPCI address: %10llx\n", address); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return IRQ_HANDLED; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * FPCI map is as follows: |
| 710 | * - 0xfdfc000000: I/O space |
| 711 | * - 0xfdfe000000: type 0 configuration space |
| 712 | * - 0xfdff000000: type 1 configuration space |
| 713 | * - 0xfe00000000: type 0 extended configuration space |
| 714 | * - 0xfe10000000: type 1 extended configuration space |
| 715 | */ |
| 716 | static void tegra_pcie_setup_translations(struct tegra_pcie *pcie) |
| 717 | { |
| 718 | u32 fpci_bar, size, axi_address; |
| 719 | |
| 720 | /* Bar 0: type 1 extended configuration space */ |
| 721 | fpci_bar = 0xfe100000; |
| 722 | size = resource_size(pcie->cs); |
| 723 | axi_address = pcie->cs->start; |
| 724 | afi_writel(pcie, axi_address, AFI_AXI_BAR0_START); |
| 725 | afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ); |
| 726 | afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0); |
| 727 | |
| 728 | /* Bar 1: downstream IO bar */ |
| 729 | fpci_bar = 0xfdfc0000; |
| 730 | size = resource_size(&pcie->io); |
Thierry Reding | 5106787 | 2014-11-27 09:54:09 +0100 | [diff] [blame] | 731 | axi_address = pcie->io.start; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 732 | afi_writel(pcie, axi_address, AFI_AXI_BAR1_START); |
| 733 | afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ); |
| 734 | afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1); |
| 735 | |
| 736 | /* Bar 2: prefetchable memory BAR */ |
| 737 | fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1; |
| 738 | size = resource_size(&pcie->prefetch); |
| 739 | axi_address = pcie->prefetch.start; |
| 740 | afi_writel(pcie, axi_address, AFI_AXI_BAR2_START); |
| 741 | afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ); |
| 742 | afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2); |
| 743 | |
| 744 | /* Bar 3: non prefetchable memory BAR */ |
| 745 | fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1; |
| 746 | size = resource_size(&pcie->mem); |
| 747 | axi_address = pcie->mem.start; |
| 748 | afi_writel(pcie, axi_address, AFI_AXI_BAR3_START); |
| 749 | afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ); |
| 750 | afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3); |
| 751 | |
| 752 | /* NULL out the remaining BARs as they are not used */ |
| 753 | afi_writel(pcie, 0, AFI_AXI_BAR4_START); |
| 754 | afi_writel(pcie, 0, AFI_AXI_BAR4_SZ); |
| 755 | afi_writel(pcie, 0, AFI_FPCI_BAR4); |
| 756 | |
| 757 | afi_writel(pcie, 0, AFI_AXI_BAR5_START); |
| 758 | afi_writel(pcie, 0, AFI_AXI_BAR5_SZ); |
| 759 | afi_writel(pcie, 0, AFI_FPCI_BAR5); |
| 760 | |
| 761 | /* map all upstream transactions as uncached */ |
Thierry Reding | e32faa3 | 2016-02-09 15:52:33 +0100 | [diff] [blame] | 762 | afi_writel(pcie, 0, AFI_CACHE_BAR0_ST); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 763 | afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ); |
| 764 | afi_writel(pcie, 0, AFI_CACHE_BAR1_ST); |
| 765 | afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ); |
| 766 | |
| 767 | /* MSI translations are setup only when needed */ |
| 768 | afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST); |
| 769 | afi_writel(pcie, 0, AFI_MSI_BAR_SZ); |
| 770 | afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST); |
| 771 | afi_writel(pcie, 0, AFI_MSI_BAR_SZ); |
| 772 | } |
| 773 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 774 | static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 775 | { |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 776 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 777 | u32 value; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 778 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 779 | timeout = jiffies + msecs_to_jiffies(timeout); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 780 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 781 | while (time_before(jiffies, timeout)) { |
| 782 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 783 | if (value & PADS_PLL_CTL_LOCKDET) |
| 784 | return 0; |
| 785 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 786 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 787 | return -ETIMEDOUT; |
| 788 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 789 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 790 | static int tegra_pcie_phy_enable(struct tegra_pcie *pcie) |
| 791 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 792 | struct device *dev = pcie->dev; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 793 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 794 | u32 value; |
| 795 | int err; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 796 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 797 | /* initialize internal PHY, enable up to 16 PCIE lanes */ |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 798 | pads_writel(pcie, 0x0, PADS_CTL_SEL); |
| 799 | |
| 800 | /* override IDDQ to 1 on all 4 lanes */ |
| 801 | value = pads_readl(pcie, PADS_CTL); |
| 802 | value |= PADS_CTL_IDDQ_1L; |
| 803 | pads_writel(pcie, value, PADS_CTL); |
| 804 | |
| 805 | /* |
| 806 | * Set up PHY PLL inputs select PLLE output as refclock, |
| 807 | * set TX ref sel to div10 (not div5). |
| 808 | */ |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 809 | value = pads_readl(pcie, soc->pads_pll_ctl); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 810 | value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 811 | value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel; |
| 812 | pads_writel(pcie, value, soc->pads_pll_ctl); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 813 | |
Eric Yuen | ec73276 | 2014-08-26 17:11:37 +0200 | [diff] [blame] | 814 | /* reset PLL */ |
| 815 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 816 | value &= ~PADS_PLL_CTL_RST_B4SM; |
| 817 | pads_writel(pcie, value, soc->pads_pll_ctl); |
| 818 | |
| 819 | usleep_range(20, 100); |
| 820 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 821 | /* take PLL out of reset */ |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 822 | value = pads_readl(pcie, soc->pads_pll_ctl); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 823 | value |= PADS_PLL_CTL_RST_B4SM; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 824 | pads_writel(pcie, value, soc->pads_pll_ctl); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 825 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 826 | /* wait for the PLL to lock */ |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 827 | err = tegra_pcie_pll_wait(pcie, 500); |
| 828 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 829 | dev_err(dev, "PLL failed to lock: %d\n", err); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 830 | return err; |
| 831 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 832 | |
| 833 | /* turn off IDDQ override */ |
| 834 | value = pads_readl(pcie, PADS_CTL); |
| 835 | value &= ~PADS_CTL_IDDQ_1L; |
| 836 | pads_writel(pcie, value, PADS_CTL); |
| 837 | |
| 838 | /* enable TX/RX data */ |
| 839 | value = pads_readl(pcie, PADS_CTL); |
| 840 | value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L; |
| 841 | pads_writel(pcie, value, PADS_CTL); |
| 842 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 846 | static int tegra_pcie_phy_disable(struct tegra_pcie *pcie) |
| 847 | { |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 848 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 849 | u32 value; |
| 850 | |
| 851 | /* disable TX/RX data */ |
| 852 | value = pads_readl(pcie, PADS_CTL); |
| 853 | value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L); |
| 854 | pads_writel(pcie, value, PADS_CTL); |
| 855 | |
| 856 | /* override IDDQ */ |
| 857 | value = pads_readl(pcie, PADS_CTL); |
| 858 | value |= PADS_CTL_IDDQ_1L; |
Bjorn Helgaas | 8dd99bc | 2016-10-05 16:04:13 -0500 | [diff] [blame] | 859 | pads_writel(pcie, value, PADS_CTL); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 860 | |
| 861 | /* reset PLL */ |
| 862 | value = pads_readl(pcie, soc->pads_pll_ctl); |
| 863 | value &= ~PADS_PLL_CTL_RST_B4SM; |
| 864 | pads_writel(pcie, value, soc->pads_pll_ctl); |
| 865 | |
| 866 | usleep_range(20, 100); |
| 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port) |
| 872 | { |
| 873 | struct device *dev = port->pcie->dev; |
| 874 | unsigned int i; |
| 875 | int err; |
| 876 | |
| 877 | for (i = 0; i < port->lanes; i++) { |
| 878 | err = phy_power_on(port->phys[i]); |
| 879 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 880 | dev_err(dev, "failed to power on PHY#%u: %d\n", i, err); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 881 | return err; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port) |
| 889 | { |
| 890 | struct device *dev = port->pcie->dev; |
| 891 | unsigned int i; |
| 892 | int err; |
| 893 | |
| 894 | for (i = 0; i < port->lanes; i++) { |
| 895 | err = phy_power_off(port->phys[i]); |
| 896 | if (err < 0) { |
| 897 | dev_err(dev, "failed to power off PHY#%u: %d\n", i, |
| 898 | err); |
| 899 | return err; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie) |
| 907 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 908 | struct device *dev = pcie->dev; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 909 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 910 | struct tegra_pcie_port *port; |
| 911 | int err; |
| 912 | |
| 913 | if (pcie->legacy_phy) { |
| 914 | if (pcie->phy) |
| 915 | err = phy_power_on(pcie->phy); |
| 916 | else |
| 917 | err = tegra_pcie_phy_enable(pcie); |
| 918 | |
| 919 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 920 | dev_err(dev, "failed to power on PHY: %d\n", err); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 921 | |
| 922 | return err; |
| 923 | } |
| 924 | |
| 925 | list_for_each_entry(port, &pcie->ports, list) { |
| 926 | err = tegra_pcie_port_phy_power_on(port); |
| 927 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 928 | dev_err(dev, |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 929 | "failed to power on PCIe port %u PHY: %d\n", |
| 930 | port->index, err); |
| 931 | return err; |
| 932 | } |
| 933 | } |
| 934 | |
Stephen Warren | cf5d3180 | 2016-07-25 16:02:21 -0500 | [diff] [blame] | 935 | /* Configure the reference clock driver */ |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 936 | pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0); |
Stephen Warren | cf5d3180 | 2016-07-25 16:02:21 -0500 | [diff] [blame] | 937 | |
| 938 | if (soc->num_ports > 2) |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 939 | pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1); |
Stephen Warren | cf5d3180 | 2016-07-25 16:02:21 -0500 | [diff] [blame] | 940 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie) |
| 945 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 946 | struct device *dev = pcie->dev; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 947 | struct tegra_pcie_port *port; |
| 948 | int err; |
| 949 | |
| 950 | if (pcie->legacy_phy) { |
| 951 | if (pcie->phy) |
| 952 | err = phy_power_off(pcie->phy); |
| 953 | else |
| 954 | err = tegra_pcie_phy_disable(pcie); |
| 955 | |
| 956 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 957 | dev_err(dev, "failed to power off PHY: %d\n", err); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 958 | |
| 959 | return err; |
| 960 | } |
| 961 | |
| 962 | list_for_each_entry(port, &pcie->ports, list) { |
| 963 | err = tegra_pcie_port_phy_power_off(port); |
| 964 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 965 | dev_err(dev, |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 966 | "failed to power off PCIe port %u PHY: %d\n", |
| 967 | port->index, err); |
| 968 | return err; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 975 | static int tegra_pcie_enable_controller(struct tegra_pcie *pcie) |
| 976 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 977 | struct device *dev = pcie->dev; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 978 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 979 | struct tegra_pcie_port *port; |
| 980 | unsigned long value; |
| 981 | int err; |
| 982 | |
| 983 | /* enable PLL power down */ |
| 984 | if (pcie->phy) { |
| 985 | value = afi_readl(pcie, AFI_PLLE_CONTROL); |
| 986 | value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL; |
| 987 | value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN; |
| 988 | afi_writel(pcie, value, AFI_PLLE_CONTROL); |
| 989 | } |
| 990 | |
| 991 | /* power down PCIe slot clock bias pad */ |
| 992 | if (soc->has_pex_bias_ctrl) |
| 993 | afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0); |
| 994 | |
| 995 | /* configure mode and disable all ports */ |
| 996 | value = afi_readl(pcie, AFI_PCIE_CONFIG); |
| 997 | value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK; |
| 998 | value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config; |
| 999 | |
| 1000 | list_for_each_entry(port, &pcie->ports, list) |
| 1001 | value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index); |
| 1002 | |
| 1003 | afi_writel(pcie, value, AFI_PCIE_CONFIG); |
| 1004 | |
| 1005 | if (soc->has_gen2) { |
| 1006 | value = afi_readl(pcie, AFI_FUSE); |
| 1007 | value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS; |
| 1008 | afi_writel(pcie, value, AFI_FUSE); |
| 1009 | } else { |
| 1010 | value = afi_readl(pcie, AFI_FUSE); |
| 1011 | value |= AFI_FUSE_PCIE_T0_GEN2_DIS; |
| 1012 | afi_writel(pcie, value, AFI_FUSE); |
| 1013 | } |
| 1014 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1015 | err = tegra_pcie_phy_power_on(pcie); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1016 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1017 | dev_err(dev, "failed to power on PHY(s): %d\n", err); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1018 | return err; |
| 1019 | } |
| 1020 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1021 | /* take the PCIe interface module out of reset */ |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1022 | reset_control_deassert(pcie->pcie_xrst); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1023 | |
| 1024 | /* finally enable PCIe */ |
| 1025 | value = afi_readl(pcie, AFI_CONFIGURATION); |
| 1026 | value |= AFI_CONFIGURATION_EN_FPCI; |
| 1027 | afi_writel(pcie, value, AFI_CONFIGURATION); |
| 1028 | |
| 1029 | value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR | |
| 1030 | AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR | |
| 1031 | AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1032 | |
| 1033 | if (soc->has_intr_prsnt_sense) |
| 1034 | value |= AFI_INTR_EN_PRSNT_SENSE; |
| 1035 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1036 | afi_writel(pcie, value, AFI_AFI_INTR_ENABLE); |
| 1037 | afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE); |
| 1038 | |
| 1039 | /* don't enable MSI for now, only when needed */ |
| 1040 | afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK); |
| 1041 | |
| 1042 | /* disable all exceptions */ |
| 1043 | afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS); |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static void tegra_pcie_power_off(struct tegra_pcie *pcie) |
| 1049 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1050 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1051 | int err; |
| 1052 | |
| 1053 | /* TODO: disable and unprepare clocks? */ |
| 1054 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1055 | err = tegra_pcie_phy_power_off(pcie); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1056 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1057 | dev_err(dev, "failed to power off PHY(s): %d\n", err); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1058 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1059 | reset_control_assert(pcie->pcie_xrst); |
| 1060 | reset_control_assert(pcie->afi_rst); |
| 1061 | reset_control_assert(pcie->pex_rst); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1062 | |
| 1063 | tegra_powergate_power_off(TEGRA_POWERGATE_PCIE); |
| 1064 | |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1065 | err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1066 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1067 | dev_warn(dev, "failed to disable regulators: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static int tegra_pcie_power_on(struct tegra_pcie *pcie) |
| 1071 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1072 | struct device *dev = pcie->dev; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 1073 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1074 | int err; |
| 1075 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1076 | reset_control_assert(pcie->pcie_xrst); |
| 1077 | reset_control_assert(pcie->afi_rst); |
| 1078 | reset_control_assert(pcie->pex_rst); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1079 | |
| 1080 | tegra_powergate_power_off(TEGRA_POWERGATE_PCIE); |
| 1081 | |
| 1082 | /* enable regulators */ |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1083 | err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies); |
| 1084 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1085 | dev_err(dev, "failed to enable regulators: %d\n", err); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1086 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1087 | err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE, |
Stephen Warren | 80b2879 | 2013-11-06 15:45:46 -0700 | [diff] [blame] | 1088 | pcie->pex_clk, |
| 1089 | pcie->pex_rst); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1090 | if (err) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1091 | dev_err(dev, "powerup sequence failed: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1092 | return err; |
| 1093 | } |
| 1094 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1095 | reset_control_deassert(pcie->afi_rst); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1096 | |
| 1097 | err = clk_prepare_enable(pcie->afi_clk); |
| 1098 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1099 | dev_err(dev, "failed to enable AFI clock: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1100 | return err; |
| 1101 | } |
| 1102 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1103 | if (soc->has_cml_clk) { |
| 1104 | err = clk_prepare_enable(pcie->cml_clk); |
| 1105 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1106 | dev_err(dev, "failed to enable CML clock: %d\n", err); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1107 | return err; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1111 | err = clk_prepare_enable(pcie->pll_e); |
| 1112 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1113 | dev_err(dev, "failed to enable PLLE clock: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1114 | return err; |
| 1115 | } |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int tegra_pcie_clocks_get(struct tegra_pcie *pcie) |
| 1121 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1122 | struct device *dev = pcie->dev; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 1123 | const struct tegra_pcie_soc *soc = pcie->soc; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1124 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1125 | pcie->pex_clk = devm_clk_get(dev, "pex"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1126 | if (IS_ERR(pcie->pex_clk)) |
| 1127 | return PTR_ERR(pcie->pex_clk); |
| 1128 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1129 | pcie->afi_clk = devm_clk_get(dev, "afi"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1130 | if (IS_ERR(pcie->afi_clk)) |
| 1131 | return PTR_ERR(pcie->afi_clk); |
| 1132 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1133 | pcie->pll_e = devm_clk_get(dev, "pll_e"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1134 | if (IS_ERR(pcie->pll_e)) |
| 1135 | return PTR_ERR(pcie->pll_e); |
| 1136 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1137 | if (soc->has_cml_clk) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1138 | pcie->cml_clk = devm_clk_get(dev, "cml"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1139 | if (IS_ERR(pcie->cml_clk)) |
| 1140 | return PTR_ERR(pcie->cml_clk); |
| 1141 | } |
| 1142 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1146 | static int tegra_pcie_resets_get(struct tegra_pcie *pcie) |
| 1147 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1148 | struct device *dev = pcie->dev; |
| 1149 | |
Philipp Zabel | 4b9cc2c | 2017-07-19 17:25:57 +0200 | [diff] [blame] | 1150 | pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex"); |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1151 | if (IS_ERR(pcie->pex_rst)) |
| 1152 | return PTR_ERR(pcie->pex_rst); |
| 1153 | |
Philipp Zabel | 4b9cc2c | 2017-07-19 17:25:57 +0200 | [diff] [blame] | 1154 | pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi"); |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1155 | if (IS_ERR(pcie->afi_rst)) |
| 1156 | return PTR_ERR(pcie->afi_rst); |
| 1157 | |
Philipp Zabel | 4b9cc2c | 2017-07-19 17:25:57 +0200 | [diff] [blame] | 1158 | pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x"); |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1159 | if (IS_ERR(pcie->pcie_xrst)) |
| 1160 | return PTR_ERR(pcie->pcie_xrst); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1165 | static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie) |
| 1166 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1167 | struct device *dev = pcie->dev; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1168 | int err; |
| 1169 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1170 | pcie->phy = devm_phy_optional_get(dev, "pcie"); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1171 | if (IS_ERR(pcie->phy)) { |
| 1172 | err = PTR_ERR(pcie->phy); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1173 | dev_err(dev, "failed to get PHY: %d\n", err); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1174 | return err; |
| 1175 | } |
| 1176 | |
| 1177 | err = phy_init(pcie->phy); |
| 1178 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1179 | dev_err(dev, "failed to initialize PHY: %d\n", err); |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1180 | return err; |
| 1181 | } |
| 1182 | |
| 1183 | pcie->legacy_phy = true; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static struct phy *devm_of_phy_optional_get_index(struct device *dev, |
| 1189 | struct device_node *np, |
| 1190 | const char *consumer, |
| 1191 | unsigned int index) |
| 1192 | { |
| 1193 | struct phy *phy; |
| 1194 | char *name; |
| 1195 | |
| 1196 | name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index); |
| 1197 | if (!name) |
| 1198 | return ERR_PTR(-ENOMEM); |
| 1199 | |
| 1200 | phy = devm_of_phy_get(dev, np, name); |
| 1201 | kfree(name); |
| 1202 | |
| 1203 | if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV) |
| 1204 | phy = NULL; |
| 1205 | |
| 1206 | return phy; |
| 1207 | } |
| 1208 | |
| 1209 | static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port) |
| 1210 | { |
| 1211 | struct device *dev = port->pcie->dev; |
| 1212 | struct phy *phy; |
| 1213 | unsigned int i; |
| 1214 | int err; |
| 1215 | |
| 1216 | port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL); |
| 1217 | if (!port->phys) |
| 1218 | return -ENOMEM; |
| 1219 | |
| 1220 | for (i = 0; i < port->lanes; i++) { |
| 1221 | phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i); |
| 1222 | if (IS_ERR(phy)) { |
| 1223 | dev_err(dev, "failed to get PHY#%u: %ld\n", i, |
| 1224 | PTR_ERR(phy)); |
| 1225 | return PTR_ERR(phy); |
| 1226 | } |
| 1227 | |
| 1228 | err = phy_init(phy); |
| 1229 | if (err < 0) { |
| 1230 | dev_err(dev, "failed to initialize PHY#%u: %d\n", i, |
| 1231 | err); |
| 1232 | return err; |
| 1233 | } |
| 1234 | |
| 1235 | port->phys[i] = phy; |
| 1236 | } |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int tegra_pcie_phys_get(struct tegra_pcie *pcie) |
| 1242 | { |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 1243 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1244 | struct device_node *np = pcie->dev->of_node; |
| 1245 | struct tegra_pcie_port *port; |
| 1246 | int err; |
| 1247 | |
| 1248 | if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL) |
| 1249 | return tegra_pcie_phys_get_legacy(pcie); |
| 1250 | |
| 1251 | list_for_each_entry(port, &pcie->ports, list) { |
| 1252 | err = tegra_pcie_port_get_phys(port); |
| 1253 | if (err < 0) |
| 1254 | return err; |
| 1255 | } |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1260 | static int tegra_pcie_get_resources(struct tegra_pcie *pcie) |
| 1261 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1262 | struct device *dev = pcie->dev; |
| 1263 | struct platform_device *pdev = to_platform_device(dev); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1264 | struct resource *pads, *afi, *res; |
| 1265 | int err; |
| 1266 | |
| 1267 | err = tegra_pcie_clocks_get(pcie); |
| 1268 | if (err) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1269 | dev_err(dev, "failed to get clocks: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1270 | return err; |
| 1271 | } |
| 1272 | |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1273 | err = tegra_pcie_resets_get(pcie); |
| 1274 | if (err) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1275 | dev_err(dev, "failed to get resets: %d\n", err); |
Stephen Warren | 3127a6b | 2013-11-06 15:56:58 -0700 | [diff] [blame] | 1276 | return err; |
| 1277 | } |
| 1278 | |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1279 | err = tegra_pcie_phys_get(pcie); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1280 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1281 | dev_err(dev, "failed to get PHYs: %d\n", err); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1282 | return err; |
| 1283 | } |
| 1284 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1285 | err = tegra_pcie_power_on(pcie); |
| 1286 | if (err) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1287 | dev_err(dev, "failed to power up: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1288 | return err; |
| 1289 | } |
| 1290 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1291 | pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads"); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1292 | pcie->pads = devm_ioremap_resource(dev, pads); |
Julia Lawall | dc05ee3 | 2013-08-26 11:11:09 +0200 | [diff] [blame] | 1293 | if (IS_ERR(pcie->pads)) { |
| 1294 | err = PTR_ERR(pcie->pads); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1295 | goto poweroff; |
| 1296 | } |
| 1297 | |
| 1298 | afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi"); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1299 | pcie->afi = devm_ioremap_resource(dev, afi); |
Julia Lawall | dc05ee3 | 2013-08-26 11:11:09 +0200 | [diff] [blame] | 1300 | if (IS_ERR(pcie->afi)) { |
| 1301 | err = PTR_ERR(pcie->afi); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1302 | goto poweroff; |
| 1303 | } |
| 1304 | |
Julia Lawall | dc05ee3 | 2013-08-26 11:11:09 +0200 | [diff] [blame] | 1305 | /* request configuration space, but remap later, on demand */ |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1306 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs"); |
| 1307 | if (!res) { |
| 1308 | err = -EADDRNOTAVAIL; |
| 1309 | goto poweroff; |
| 1310 | } |
| 1311 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1312 | pcie->cs = devm_request_mem_region(dev, res->start, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1313 | resource_size(res), res->name); |
| 1314 | if (!pcie->cs) { |
| 1315 | err = -EADDRNOTAVAIL; |
| 1316 | goto poweroff; |
| 1317 | } |
| 1318 | |
| 1319 | /* request interrupt */ |
| 1320 | err = platform_get_irq_byname(pdev, "intr"); |
| 1321 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1322 | dev_err(dev, "failed to get IRQ: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1323 | goto poweroff; |
| 1324 | } |
| 1325 | |
| 1326 | pcie->irq = err; |
| 1327 | |
| 1328 | err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie); |
| 1329 | if (err) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1330 | dev_err(dev, "failed to register IRQ: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1331 | goto poweroff; |
| 1332 | } |
| 1333 | |
| 1334 | return 0; |
| 1335 | |
| 1336 | poweroff: |
| 1337 | tegra_pcie_power_off(pcie); |
| 1338 | return err; |
| 1339 | } |
| 1340 | |
| 1341 | static int tegra_pcie_put_resources(struct tegra_pcie *pcie) |
| 1342 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1343 | struct device *dev = pcie->dev; |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1344 | int err; |
| 1345 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1346 | if (pcie->irq > 0) |
| 1347 | free_irq(pcie->irq, pcie); |
| 1348 | |
| 1349 | tegra_pcie_power_off(pcie); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1350 | |
| 1351 | err = phy_exit(pcie->phy); |
| 1352 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1353 | dev_err(dev, "failed to teardown PHY: %d\n", err); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1354 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | static int tegra_msi_alloc(struct tegra_msi *chip) |
| 1359 | { |
| 1360 | int msi; |
| 1361 | |
| 1362 | mutex_lock(&chip->lock); |
| 1363 | |
| 1364 | msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR); |
| 1365 | if (msi < INT_PCI_MSI_NR) |
| 1366 | set_bit(msi, chip->used); |
| 1367 | else |
| 1368 | msi = -ENOSPC; |
| 1369 | |
| 1370 | mutex_unlock(&chip->lock); |
| 1371 | |
| 1372 | return msi; |
| 1373 | } |
| 1374 | |
| 1375 | static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq) |
| 1376 | { |
| 1377 | struct device *dev = chip->chip.dev; |
| 1378 | |
| 1379 | mutex_lock(&chip->lock); |
| 1380 | |
| 1381 | if (!test_bit(irq, chip->used)) |
| 1382 | dev_err(dev, "trying to free unused MSI#%lu\n", irq); |
| 1383 | else |
| 1384 | clear_bit(irq, chip->used); |
| 1385 | |
| 1386 | mutex_unlock(&chip->lock); |
| 1387 | } |
| 1388 | |
| 1389 | static irqreturn_t tegra_pcie_msi_irq(int irq, void *data) |
| 1390 | { |
| 1391 | struct tegra_pcie *pcie = data; |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1392 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1393 | struct tegra_msi *msi = &pcie->msi; |
| 1394 | unsigned int i, processed = 0; |
| 1395 | |
| 1396 | for (i = 0; i < 8; i++) { |
| 1397 | unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4); |
| 1398 | |
| 1399 | while (reg) { |
| 1400 | unsigned int offset = find_first_bit(®, 32); |
| 1401 | unsigned int index = i * 32 + offset; |
| 1402 | unsigned int irq; |
| 1403 | |
| 1404 | /* clear the interrupt */ |
| 1405 | afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4); |
| 1406 | |
| 1407 | irq = irq_find_mapping(msi->domain, index); |
| 1408 | if (irq) { |
| 1409 | if (test_bit(index, msi->used)) |
| 1410 | generic_handle_irq(irq); |
| 1411 | else |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1412 | dev_info(dev, "unhandled MSI\n"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1413 | } else { |
| 1414 | /* |
| 1415 | * that's weird who triggered this? |
| 1416 | * just clear it |
| 1417 | */ |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1418 | dev_info(dev, "unexpected MSI\n"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* see if there's any more pending in this vector */ |
| 1422 | reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4); |
| 1423 | |
| 1424 | processed++; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | return processed > 0 ? IRQ_HANDLED : IRQ_NONE; |
| 1429 | } |
| 1430 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 1431 | static int tegra_msi_setup_irq(struct msi_controller *chip, |
| 1432 | struct pci_dev *pdev, struct msi_desc *desc) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1433 | { |
| 1434 | struct tegra_msi *msi = to_tegra_msi(chip); |
| 1435 | struct msi_msg msg; |
| 1436 | unsigned int irq; |
| 1437 | int hwirq; |
| 1438 | |
| 1439 | hwirq = tegra_msi_alloc(msi); |
| 1440 | if (hwirq < 0) |
| 1441 | return hwirq; |
| 1442 | |
| 1443 | irq = irq_create_mapping(msi->domain, hwirq); |
Jisheng Zhang | 019fa46 | 2014-07-29 09:33:30 +0800 | [diff] [blame] | 1444 | if (!irq) { |
| 1445 | tegra_msi_free(msi, hwirq); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1446 | return -EINVAL; |
Jisheng Zhang | 019fa46 | 2014-07-29 09:33:30 +0800 | [diff] [blame] | 1447 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1448 | |
| 1449 | irq_set_msi_desc(irq, desc); |
| 1450 | |
Thierry Reding | c016555 | 2017-05-04 22:10:31 +0200 | [diff] [blame] | 1451 | msg.address_lo = lower_32_bits(msi->phys); |
| 1452 | msg.address_hi = upper_32_bits(msi->phys); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1453 | msg.data = hwirq; |
| 1454 | |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 1455 | pci_write_msi_msg(irq, &msg); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 1460 | static void tegra_msi_teardown_irq(struct msi_controller *chip, |
| 1461 | unsigned int irq) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1462 | { |
| 1463 | struct tegra_msi *msi = to_tegra_msi(chip); |
| 1464 | struct irq_data *d = irq_get_irq_data(irq); |
Jisheng Zhang | 019fa46 | 2014-07-29 09:33:30 +0800 | [diff] [blame] | 1465 | irq_hw_number_t hwirq = irqd_to_hwirq(d); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1466 | |
Jisheng Zhang | 019fa46 | 2014-07-29 09:33:30 +0800 | [diff] [blame] | 1467 | irq_dispose_mapping(irq); |
| 1468 | tegra_msi_free(msi, hwirq); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static struct irq_chip tegra_msi_irq_chip = { |
| 1472 | .name = "Tegra PCIe MSI", |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 1473 | .irq_enable = pci_msi_unmask_irq, |
| 1474 | .irq_disable = pci_msi_mask_irq, |
| 1475 | .irq_mask = pci_msi_mask_irq, |
| 1476 | .irq_unmask = pci_msi_unmask_irq, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1477 | }; |
| 1478 | |
| 1479 | static int tegra_msi_map(struct irq_domain *domain, unsigned int irq, |
| 1480 | irq_hw_number_t hwirq) |
| 1481 | { |
| 1482 | irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq); |
| 1483 | irq_set_chip_data(irq, domain->host_data); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1484 | |
Stephen Warren | b4f1737 | 2013-05-06 14:19:19 -0600 | [diff] [blame] | 1485 | tegra_cpuidle_pcie_irqs_in_use(); |
| 1486 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | static const struct irq_domain_ops msi_domain_ops = { |
| 1491 | .map = tegra_msi_map, |
| 1492 | }; |
| 1493 | |
| 1494 | static int tegra_pcie_enable_msi(struct tegra_pcie *pcie) |
| 1495 | { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 1496 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
| 1497 | struct platform_device *pdev = to_platform_device(pcie->dev); |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 1498 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1499 | struct tegra_msi *msi = &pcie->msi; |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 1500 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1501 | int err; |
| 1502 | u32 reg; |
| 1503 | |
| 1504 | mutex_init(&msi->lock); |
| 1505 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1506 | msi->chip.dev = dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1507 | msi->chip.setup_irq = tegra_msi_setup_irq; |
| 1508 | msi->chip.teardown_irq = tegra_msi_teardown_irq; |
| 1509 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1510 | msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1511 | &msi_domain_ops, &msi->chip); |
| 1512 | if (!msi->domain) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1513 | dev_err(dev, "failed to create IRQ domain\n"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1514 | return -ENOMEM; |
| 1515 | } |
| 1516 | |
| 1517 | err = platform_get_irq_byname(pdev, "msi"); |
| 1518 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1519 | dev_err(dev, "failed to get IRQ: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1520 | goto err; |
| 1521 | } |
| 1522 | |
| 1523 | msi->irq = err; |
| 1524 | |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 1525 | err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD, |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1526 | tegra_msi_irq_chip.name, pcie); |
| 1527 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1528 | dev_err(dev, "failed to request IRQ: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1529 | goto err; |
| 1530 | } |
| 1531 | |
Thierry Reding | d7bd554 | 2017-05-04 22:10:32 +0200 | [diff] [blame] | 1532 | /* |
| 1533 | * The PCI host bridge on Tegra contains some logic that intercepts |
| 1534 | * MSI writes, which means that the MSI target address doesn't have |
| 1535 | * to point to actual physical memory. Rather than allocating one 4 |
| 1536 | * KiB page of system memory that's never used, we can simply pick |
| 1537 | * an arbitrary address within an area reserved for system memory |
| 1538 | * in the FPCI address map. |
| 1539 | * |
| 1540 | * However, in order to avoid confusion, we pick an address that |
| 1541 | * doesn't map to physical memory. The FPCI address map reserves a |
| 1542 | * 1012 GiB region for system memory and memory-mapped I/O. Since |
| 1543 | * none of the Tegra SoCs that contain this PCI host bridge can |
| 1544 | * address more than 16 GiB of system memory, the last 4 KiB of |
| 1545 | * these 1012 GiB is a good candidate. |
| 1546 | */ |
| 1547 | msi->phys = 0xfcfffff000; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1548 | |
Thierry Reding | c016555 | 2017-05-04 22:10:31 +0200 | [diff] [blame] | 1549 | afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST); |
| 1550 | afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1551 | /* this register is in 4K increments */ |
| 1552 | afi_writel(pcie, 1, AFI_MSI_BAR_SZ); |
| 1553 | |
| 1554 | /* enable all MSI vectors */ |
| 1555 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0); |
| 1556 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1); |
| 1557 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2); |
| 1558 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3); |
| 1559 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4); |
| 1560 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5); |
| 1561 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6); |
| 1562 | afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7); |
| 1563 | |
| 1564 | /* and unmask the MSI interrupt */ |
| 1565 | reg = afi_readl(pcie, AFI_INTR_MASK); |
| 1566 | reg |= AFI_INTR_MASK_MSI_MASK; |
| 1567 | afi_writel(pcie, reg, AFI_INTR_MASK); |
| 1568 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 1569 | host->msi = &msi->chip; |
| 1570 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1571 | return 0; |
| 1572 | |
| 1573 | err: |
| 1574 | irq_domain_remove(msi->domain); |
| 1575 | return err; |
| 1576 | } |
| 1577 | |
| 1578 | static int tegra_pcie_disable_msi(struct tegra_pcie *pcie) |
| 1579 | { |
| 1580 | struct tegra_msi *msi = &pcie->msi; |
| 1581 | unsigned int i, irq; |
| 1582 | u32 value; |
| 1583 | |
| 1584 | /* mask the MSI interrupt */ |
| 1585 | value = afi_readl(pcie, AFI_INTR_MASK); |
| 1586 | value &= ~AFI_INTR_MASK_MSI_MASK; |
| 1587 | afi_writel(pcie, value, AFI_INTR_MASK); |
| 1588 | |
| 1589 | /* disable all MSI vectors */ |
| 1590 | afi_writel(pcie, 0, AFI_MSI_EN_VEC0); |
| 1591 | afi_writel(pcie, 0, AFI_MSI_EN_VEC1); |
| 1592 | afi_writel(pcie, 0, AFI_MSI_EN_VEC2); |
| 1593 | afi_writel(pcie, 0, AFI_MSI_EN_VEC3); |
| 1594 | afi_writel(pcie, 0, AFI_MSI_EN_VEC4); |
| 1595 | afi_writel(pcie, 0, AFI_MSI_EN_VEC5); |
| 1596 | afi_writel(pcie, 0, AFI_MSI_EN_VEC6); |
| 1597 | afi_writel(pcie, 0, AFI_MSI_EN_VEC7); |
| 1598 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1599 | if (msi->irq > 0) |
| 1600 | free_irq(msi->irq, pcie); |
| 1601 | |
| 1602 | for (i = 0; i < INT_PCI_MSI_NR; i++) { |
| 1603 | irq = irq_find_mapping(msi->domain, i); |
| 1604 | if (irq > 0) |
| 1605 | irq_dispose_mapping(irq); |
| 1606 | } |
| 1607 | |
| 1608 | irq_domain_remove(msi->domain); |
| 1609 | |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes, |
| 1614 | u32 *xbar) |
| 1615 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1616 | struct device *dev = pcie->dev; |
| 1617 | struct device_node *np = dev->of_node; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1618 | |
Thierry Reding | c7a091c | 2016-11-25 11:57:15 +0100 | [diff] [blame] | 1619 | if (of_device_is_compatible(np, "nvidia,tegra124-pcie") || |
| 1620 | of_device_is_compatible(np, "nvidia,tegra210-pcie")) { |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1621 | switch (lanes) { |
| 1622 | case 0x0000104: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1623 | dev_info(dev, "4x1, 1x1 configuration\n"); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1624 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1; |
| 1625 | return 0; |
| 1626 | |
| 1627 | case 0x0000102: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1628 | dev_info(dev, "2x1, 1x1 configuration\n"); |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1629 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1; |
| 1630 | return 0; |
| 1631 | } |
| 1632 | } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) { |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1633 | switch (lanes) { |
| 1634 | case 0x00000204: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1635 | dev_info(dev, "4x1, 2x1 configuration\n"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1636 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420; |
| 1637 | return 0; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1638 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1639 | case 0x00020202: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1640 | dev_info(dev, "2x3 configuration\n"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1641 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222; |
| 1642 | return 0; |
| 1643 | |
| 1644 | case 0x00010104: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1645 | dev_info(dev, "4x1, 1x2 configuration\n"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1646 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411; |
| 1647 | return 0; |
| 1648 | } |
| 1649 | } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) { |
| 1650 | switch (lanes) { |
| 1651 | case 0x00000004: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1652 | dev_info(dev, "single-mode configuration\n"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1653 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE; |
| 1654 | return 0; |
| 1655 | |
| 1656 | case 0x00000202: |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1657 | dev_info(dev, "dual-mode configuration\n"); |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1658 | *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL; |
| 1659 | return 0; |
| 1660 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | return -EINVAL; |
| 1664 | } |
| 1665 | |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1666 | /* |
| 1667 | * Check whether a given set of supplies is available in a device tree node. |
| 1668 | * This is used to check whether the new or the legacy device tree bindings |
| 1669 | * should be used. |
| 1670 | */ |
| 1671 | static bool of_regulator_bulk_available(struct device_node *np, |
| 1672 | struct regulator_bulk_data *supplies, |
| 1673 | unsigned int num_supplies) |
| 1674 | { |
| 1675 | char property[32]; |
| 1676 | unsigned int i; |
| 1677 | |
| 1678 | for (i = 0; i < num_supplies; i++) { |
| 1679 | snprintf(property, 32, "%s-supply", supplies[i].supply); |
| 1680 | |
| 1681 | if (of_find_property(np, property, NULL) == NULL) |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Old versions of the device tree binding for this device used a set of power |
| 1690 | * supplies that didn't match the hardware inputs. This happened to work for a |
| 1691 | * number of cases but is not future proof. However to preserve backwards- |
| 1692 | * compatibility with old device trees, this function will try to use the old |
| 1693 | * set of supplies. |
| 1694 | */ |
| 1695 | static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie) |
| 1696 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1697 | struct device *dev = pcie->dev; |
| 1698 | struct device_node *np = dev->of_node; |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1699 | |
| 1700 | if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) |
| 1701 | pcie->num_supplies = 3; |
| 1702 | else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) |
| 1703 | pcie->num_supplies = 2; |
| 1704 | |
| 1705 | if (pcie->num_supplies == 0) { |
Rob Herring | b63773a | 2017-07-18 16:43:21 -0500 | [diff] [blame] | 1706 | dev_err(dev, "device %pOF not supported in legacy mode\n", np); |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1707 | return -ENODEV; |
| 1708 | } |
| 1709 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1710 | pcie->supplies = devm_kcalloc(dev, pcie->num_supplies, |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1711 | sizeof(*pcie->supplies), |
| 1712 | GFP_KERNEL); |
| 1713 | if (!pcie->supplies) |
| 1714 | return -ENOMEM; |
| 1715 | |
| 1716 | pcie->supplies[0].supply = "pex-clk"; |
| 1717 | pcie->supplies[1].supply = "vdd"; |
| 1718 | |
| 1719 | if (pcie->num_supplies > 2) |
| 1720 | pcie->supplies[2].supply = "avdd"; |
| 1721 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1722 | return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies); |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * Obtains the list of regulators required for a particular generation of the |
| 1727 | * IP block. |
| 1728 | * |
| 1729 | * This would've been nice to do simply by providing static tables for use |
| 1730 | * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky |
| 1731 | * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB) |
| 1732 | * and either seems to be optional depending on which ports are being used. |
| 1733 | */ |
| 1734 | static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask) |
| 1735 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1736 | struct device *dev = pcie->dev; |
| 1737 | struct device_node *np = dev->of_node; |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1738 | unsigned int i = 0; |
| 1739 | |
Thierry Reding | c7a091c | 2016-11-25 11:57:15 +0100 | [diff] [blame] | 1740 | if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) { |
| 1741 | pcie->num_supplies = 6; |
| 1742 | |
| 1743 | pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies, |
| 1744 | sizeof(*pcie->supplies), |
| 1745 | GFP_KERNEL); |
| 1746 | if (!pcie->supplies) |
| 1747 | return -ENOMEM; |
| 1748 | |
| 1749 | pcie->supplies[i++].supply = "avdd-pll-uerefe"; |
| 1750 | pcie->supplies[i++].supply = "hvddio-pex"; |
| 1751 | pcie->supplies[i++].supply = "dvddio-pex"; |
| 1752 | pcie->supplies[i++].supply = "dvdd-pex-pll"; |
| 1753 | pcie->supplies[i++].supply = "hvdd-pex-pll-e"; |
| 1754 | pcie->supplies[i++].supply = "vddio-pex-ctl"; |
| 1755 | } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) { |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1756 | pcie->num_supplies = 7; |
| 1757 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1758 | pcie->supplies = devm_kcalloc(dev, pcie->num_supplies, |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 1759 | sizeof(*pcie->supplies), |
| 1760 | GFP_KERNEL); |
| 1761 | if (!pcie->supplies) |
| 1762 | return -ENOMEM; |
| 1763 | |
| 1764 | pcie->supplies[i++].supply = "avddio-pex"; |
| 1765 | pcie->supplies[i++].supply = "dvddio-pex"; |
| 1766 | pcie->supplies[i++].supply = "avdd-pex-pll"; |
| 1767 | pcie->supplies[i++].supply = "hvdd-pex"; |
| 1768 | pcie->supplies[i++].supply = "hvdd-pex-pll-e"; |
| 1769 | pcie->supplies[i++].supply = "vddio-pex-ctl"; |
| 1770 | pcie->supplies[i++].supply = "avdd-pll-erefe"; |
| 1771 | } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) { |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1772 | bool need_pexa = false, need_pexb = false; |
| 1773 | |
| 1774 | /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */ |
| 1775 | if (lane_mask & 0x0f) |
| 1776 | need_pexa = true; |
| 1777 | |
| 1778 | /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */ |
| 1779 | if (lane_mask & 0x30) |
| 1780 | need_pexb = true; |
| 1781 | |
| 1782 | pcie->num_supplies = 4 + (need_pexa ? 2 : 0) + |
| 1783 | (need_pexb ? 2 : 0); |
| 1784 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1785 | pcie->supplies = devm_kcalloc(dev, pcie->num_supplies, |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1786 | sizeof(*pcie->supplies), |
| 1787 | GFP_KERNEL); |
| 1788 | if (!pcie->supplies) |
| 1789 | return -ENOMEM; |
| 1790 | |
| 1791 | pcie->supplies[i++].supply = "avdd-pex-pll"; |
| 1792 | pcie->supplies[i++].supply = "hvdd-pex"; |
| 1793 | pcie->supplies[i++].supply = "vddio-pex-ctl"; |
| 1794 | pcie->supplies[i++].supply = "avdd-plle"; |
| 1795 | |
| 1796 | if (need_pexa) { |
| 1797 | pcie->supplies[i++].supply = "avdd-pexa"; |
| 1798 | pcie->supplies[i++].supply = "vdd-pexa"; |
| 1799 | } |
| 1800 | |
| 1801 | if (need_pexb) { |
| 1802 | pcie->supplies[i++].supply = "avdd-pexb"; |
| 1803 | pcie->supplies[i++].supply = "vdd-pexb"; |
| 1804 | } |
| 1805 | } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) { |
| 1806 | pcie->num_supplies = 5; |
| 1807 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1808 | pcie->supplies = devm_kcalloc(dev, pcie->num_supplies, |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1809 | sizeof(*pcie->supplies), |
| 1810 | GFP_KERNEL); |
| 1811 | if (!pcie->supplies) |
| 1812 | return -ENOMEM; |
| 1813 | |
| 1814 | pcie->supplies[0].supply = "avdd-pex"; |
| 1815 | pcie->supplies[1].supply = "vdd-pex"; |
| 1816 | pcie->supplies[2].supply = "avdd-pex-pll"; |
| 1817 | pcie->supplies[3].supply = "avdd-plle"; |
| 1818 | pcie->supplies[4].supply = "vddio-pex-clk"; |
| 1819 | } |
| 1820 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1821 | if (of_regulator_bulk_available(dev->of_node, pcie->supplies, |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1822 | pcie->num_supplies)) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1823 | return devm_regulator_bulk_get(dev, pcie->num_supplies, |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1824 | pcie->supplies); |
| 1825 | |
| 1826 | /* |
| 1827 | * If not all regulators are available for this new scheme, assume |
| 1828 | * that the device tree complies with an older version of the device |
| 1829 | * tree binding. |
| 1830 | */ |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1831 | dev_info(dev, "using legacy DT binding for power supplies\n"); |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1832 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1833 | devm_kfree(dev, pcie->supplies); |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1834 | pcie->num_supplies = 0; |
| 1835 | |
| 1836 | return tegra_pcie_get_legacy_regulators(pcie); |
| 1837 | } |
| 1838 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1839 | static int tegra_pcie_parse_dt(struct tegra_pcie *pcie) |
| 1840 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1841 | struct device *dev = pcie->dev; |
| 1842 | struct device_node *np = dev->of_node, *port; |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 1843 | const struct tegra_pcie_soc *soc = pcie->soc; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1844 | struct of_pci_range_parser parser; |
| 1845 | struct of_pci_range range; |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1846 | u32 lanes = 0, mask = 0; |
| 1847 | unsigned int lane = 0; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1848 | struct resource res; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1849 | int err; |
| 1850 | |
| 1851 | if (of_pci_range_parser_init(&parser, np)) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1852 | dev_err(dev, "missing \"ranges\" property\n"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1853 | return -EINVAL; |
| 1854 | } |
| 1855 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1856 | for_each_of_pci_range(&parser, &range) { |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 1857 | err = of_pci_range_to_resource(&range, np, &res); |
| 1858 | if (err < 0) |
| 1859 | return err; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1860 | |
| 1861 | switch (res.flags & IORESOURCE_TYPE_BITS) { |
| 1862 | case IORESOURCE_IO: |
Thierry Reding | 56e75e2 | 2016-02-09 15:52:32 +0100 | [diff] [blame] | 1863 | /* Track the bus -> CPU I/O mapping offset. */ |
| 1864 | pcie->offset.io = res.start - range.pci_addr; |
| 1865 | |
Thierry Reding | 5106787 | 2014-11-27 09:54:09 +0100 | [diff] [blame] | 1866 | memcpy(&pcie->pio, &res, sizeof(res)); |
| 1867 | pcie->pio.name = np->full_name; |
| 1868 | |
| 1869 | /* |
| 1870 | * The Tegra PCIe host bridge uses this to program the |
| 1871 | * mapping of the I/O space to the physical address, |
| 1872 | * so we override the .start and .end fields here that |
| 1873 | * of_pci_range_to_resource() converted to I/O space. |
| 1874 | * We also set the IORESOURCE_MEM type to clarify that |
| 1875 | * the resource is in the physical memory space. |
| 1876 | */ |
| 1877 | pcie->io.start = range.cpu_addr; |
| 1878 | pcie->io.end = range.cpu_addr + range.size - 1; |
| 1879 | pcie->io.flags = IORESOURCE_MEM; |
| 1880 | pcie->io.name = "I/O"; |
| 1881 | |
| 1882 | memcpy(&res, &pcie->io, sizeof(res)); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1883 | break; |
| 1884 | |
| 1885 | case IORESOURCE_MEM: |
Thierry Reding | 56e75e2 | 2016-02-09 15:52:32 +0100 | [diff] [blame] | 1886 | /* |
| 1887 | * Track the bus -> CPU memory mapping offset. This |
| 1888 | * assumes that the prefetchable and non-prefetchable |
| 1889 | * regions will be the last of type IORESOURCE_MEM in |
| 1890 | * the ranges property. |
| 1891 | * */ |
| 1892 | pcie->offset.mem = res.start - range.pci_addr; |
| 1893 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1894 | if (res.flags & IORESOURCE_PREFETCH) { |
| 1895 | memcpy(&pcie->prefetch, &res, sizeof(res)); |
Thierry Reding | 41534e5 | 2014-08-01 14:15:11 +0200 | [diff] [blame] | 1896 | pcie->prefetch.name = "prefetchable"; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1897 | } else { |
| 1898 | memcpy(&pcie->mem, &res, sizeof(res)); |
Thierry Reding | 41534e5 | 2014-08-01 14:15:11 +0200 | [diff] [blame] | 1899 | pcie->mem.name = "non-prefetchable"; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1900 | } |
| 1901 | break; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | err = of_pci_parse_bus_range(np, &pcie->busn); |
| 1906 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1907 | dev_err(dev, "failed to parse ranges property: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1908 | pcie->busn.name = np->name; |
| 1909 | pcie->busn.start = 0; |
| 1910 | pcie->busn.end = 0xff; |
| 1911 | pcie->busn.flags = IORESOURCE_BUS; |
| 1912 | } |
| 1913 | |
| 1914 | /* parse root ports */ |
| 1915 | for_each_child_of_node(np, port) { |
| 1916 | struct tegra_pcie_port *rp; |
| 1917 | unsigned int index; |
| 1918 | u32 value; |
| 1919 | |
| 1920 | err = of_pci_get_devfn(port); |
| 1921 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1922 | dev_err(dev, "failed to parse address: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1923 | return err; |
| 1924 | } |
| 1925 | |
| 1926 | index = PCI_SLOT(err); |
| 1927 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 1928 | if (index < 1 || index > soc->num_ports) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1929 | dev_err(dev, "invalid port number: %d\n", index); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1930 | return -EINVAL; |
| 1931 | } |
| 1932 | |
| 1933 | index--; |
| 1934 | |
| 1935 | err = of_property_read_u32(port, "nvidia,num-lanes", &value); |
| 1936 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1937 | dev_err(dev, "failed to parse # of lanes: %d\n", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1938 | err); |
| 1939 | return err; |
| 1940 | } |
| 1941 | |
| 1942 | if (value > 16) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1943 | dev_err(dev, "invalid # of lanes: %u\n", value); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1944 | return -EINVAL; |
| 1945 | } |
| 1946 | |
| 1947 | lanes |= value << (index << 3); |
| 1948 | |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1949 | if (!of_device_is_available(port)) { |
| 1950 | lane += value; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1951 | continue; |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | mask |= ((1 << value) - 1) << lane; |
| 1955 | lane += value; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1956 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1957 | rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1958 | if (!rp) |
| 1959 | return -ENOMEM; |
| 1960 | |
| 1961 | err = of_address_to_resource(port, 0, &rp->regs); |
| 1962 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1963 | dev_err(dev, "failed to parse address: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1964 | return err; |
| 1965 | } |
| 1966 | |
| 1967 | INIT_LIST_HEAD(&rp->list); |
| 1968 | rp->index = index; |
| 1969 | rp->lanes = value; |
| 1970 | rp->pcie = pcie; |
Thierry Reding | 6fe7c18 | 2015-11-11 18:25:59 +0100 | [diff] [blame] | 1971 | rp->np = port; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1972 | |
Lorenzo Pieralisi | 3e02dc4 | 2017-04-19 17:49:06 +0100 | [diff] [blame] | 1973 | rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs); |
Julia Lawall | dc05ee3 | 2013-08-26 11:11:09 +0200 | [diff] [blame] | 1974 | if (IS_ERR(rp->base)) |
| 1975 | return PTR_ERR(rp->base); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1976 | |
| 1977 | list_add_tail(&rp->list, &pcie->ports); |
| 1978 | } |
| 1979 | |
| 1980 | err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config); |
| 1981 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 1982 | dev_err(dev, "invalid lane configuration\n"); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1983 | return err; |
| 1984 | } |
| 1985 | |
Thierry Reding | 077fb15 | 2014-05-28 16:49:13 +0200 | [diff] [blame] | 1986 | err = tegra_pcie_get_regulators(pcie, mask); |
| 1987 | if (err < 0) |
| 1988 | return err; |
| 1989 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * FIXME: If there are no PCIe cards attached, then calling this function |
| 1995 | * can result in the increase of the bootup time as there are big timeout |
| 1996 | * loops. |
| 1997 | */ |
| 1998 | #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */ |
| 1999 | static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port) |
| 2000 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2001 | struct device *dev = port->pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2002 | unsigned int retries = 3; |
| 2003 | unsigned long value; |
| 2004 | |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2005 | /* override presence detection */ |
| 2006 | value = readl(port->base + RP_PRIV_MISC); |
| 2007 | value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT; |
| 2008 | value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT; |
| 2009 | writel(value, port->base + RP_PRIV_MISC); |
| 2010 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2011 | do { |
| 2012 | unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT; |
| 2013 | |
| 2014 | do { |
| 2015 | value = readl(port->base + RP_VEND_XP); |
| 2016 | |
| 2017 | if (value & RP_VEND_XP_DL_UP) |
| 2018 | break; |
| 2019 | |
| 2020 | usleep_range(1000, 2000); |
| 2021 | } while (--timeout); |
| 2022 | |
| 2023 | if (!timeout) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2024 | dev_err(dev, "link %u down, retrying\n", port->index); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2025 | goto retry; |
| 2026 | } |
| 2027 | |
| 2028 | timeout = TEGRA_PCIE_LINKUP_TIMEOUT; |
| 2029 | |
| 2030 | do { |
| 2031 | value = readl(port->base + RP_LINK_CONTROL_STATUS); |
| 2032 | |
| 2033 | if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE) |
| 2034 | return true; |
| 2035 | |
| 2036 | usleep_range(1000, 2000); |
| 2037 | } while (--timeout); |
| 2038 | |
| 2039 | retry: |
| 2040 | tegra_pcie_port_reset(port); |
| 2041 | } while (--retries); |
| 2042 | |
| 2043 | return false; |
| 2044 | } |
| 2045 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2046 | static void tegra_pcie_enable_ports(struct tegra_pcie *pcie) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2047 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2048 | struct device *dev = pcie->dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2049 | struct tegra_pcie_port *port, *tmp; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2050 | |
| 2051 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2052 | dev_info(dev, "probing port %u, using %u lanes\n", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2053 | port->index, port->lanes); |
| 2054 | |
| 2055 | tegra_pcie_port_enable(port); |
| 2056 | |
| 2057 | if (tegra_pcie_port_check_link(port)) |
| 2058 | continue; |
| 2059 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2060 | dev_info(dev, "link %u down, ignoring\n", port->index); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2061 | |
| 2062 | tegra_pcie_port_disable(port); |
| 2063 | tegra_pcie_port_free(port); |
| 2064 | } |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 2067 | static const struct tegra_pcie_soc tegra20_pcie = { |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2068 | .num_ports = 2, |
| 2069 | .msi_base_shift = 0, |
| 2070 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA20, |
| 2071 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10, |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 2072 | .pads_refclk_cfg0 = 0xfa5cfa5c, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2073 | .has_pex_clkreq_en = false, |
| 2074 | .has_pex_bias_ctrl = false, |
| 2075 | .has_intr_prsnt_sense = false, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2076 | .has_cml_clk = false, |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2077 | .has_gen2 = false, |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 2078 | .force_pca_enable = false, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2079 | }; |
| 2080 | |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 2081 | static const struct tegra_pcie_soc tegra30_pcie = { |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2082 | .num_ports = 3, |
| 2083 | .msi_base_shift = 8, |
| 2084 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 2085 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 2086 | .pads_refclk_cfg0 = 0xfa5cfa5c, |
| 2087 | .pads_refclk_cfg1 = 0xfa5cfa5c, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2088 | .has_pex_clkreq_en = true, |
| 2089 | .has_pex_bias_ctrl = true, |
| 2090 | .has_intr_prsnt_sense = true, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2091 | .has_cml_clk = true, |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2092 | .has_gen2 = false, |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 2093 | .force_pca_enable = false, |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2094 | }; |
| 2095 | |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 2096 | static const struct tegra_pcie_soc tegra124_pcie = { |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2097 | .num_ports = 2, |
| 2098 | .msi_base_shift = 8, |
| 2099 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 2100 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
Stephen Warren | f814430 | 2016-07-25 16:02:27 -0500 | [diff] [blame] | 2101 | .pads_refclk_cfg0 = 0x44ac44ac, |
Thierry Reding | 7f1f054 | 2014-08-26 17:11:38 +0200 | [diff] [blame] | 2102 | .has_pex_clkreq_en = true, |
| 2103 | .has_pex_bias_ctrl = true, |
| 2104 | .has_intr_prsnt_sense = true, |
| 2105 | .has_cml_clk = true, |
| 2106 | .has_gen2 = true, |
Thierry Reding | 76245ca | 2016-11-25 11:57:14 +0100 | [diff] [blame] | 2107 | .force_pca_enable = false, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2108 | }; |
| 2109 | |
Thierry Reding | c7a091c | 2016-11-25 11:57:15 +0100 | [diff] [blame] | 2110 | static const struct tegra_pcie_soc tegra210_pcie = { |
| 2111 | .num_ports = 2, |
| 2112 | .msi_base_shift = 8, |
| 2113 | .pads_pll_ctl = PADS_PLL_CTL_TEGRA30, |
| 2114 | .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN, |
| 2115 | .pads_refclk_cfg0 = 0x90b890b8, |
| 2116 | .has_pex_clkreq_en = true, |
| 2117 | .has_pex_bias_ctrl = true, |
| 2118 | .has_intr_prsnt_sense = true, |
| 2119 | .has_cml_clk = true, |
| 2120 | .has_gen2 = true, |
| 2121 | .force_pca_enable = true, |
| 2122 | }; |
| 2123 | |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2124 | static const struct of_device_id tegra_pcie_of_match[] = { |
Thierry Reding | c7a091c | 2016-11-25 11:57:15 +0100 | [diff] [blame] | 2125 | { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie }, |
Thierry Reding | a7fbae2 | 2016-08-15 17:31:31 +0200 | [diff] [blame] | 2126 | { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie }, |
| 2127 | { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie }, |
| 2128 | { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie }, |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2129 | { }, |
| 2130 | }; |
Jay Agarwal | 94716cd | 2013-08-09 16:49:24 +0200 | [diff] [blame] | 2131 | |
Thierry Reding | 2cb989f | 2014-07-22 12:30:46 -0600 | [diff] [blame] | 2132 | static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos) |
| 2133 | { |
| 2134 | struct tegra_pcie *pcie = s->private; |
| 2135 | |
| 2136 | if (list_empty(&pcie->ports)) |
| 2137 | return NULL; |
| 2138 | |
| 2139 | seq_printf(s, "Index Status\n"); |
| 2140 | |
| 2141 | return seq_list_start(&pcie->ports, *pos); |
| 2142 | } |
| 2143 | |
| 2144 | static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2145 | { |
| 2146 | struct tegra_pcie *pcie = s->private; |
| 2147 | |
| 2148 | return seq_list_next(v, &pcie->ports, pos); |
| 2149 | } |
| 2150 | |
| 2151 | static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v) |
| 2152 | { |
| 2153 | } |
| 2154 | |
| 2155 | static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v) |
| 2156 | { |
| 2157 | bool up = false, active = false; |
| 2158 | struct tegra_pcie_port *port; |
| 2159 | unsigned int value; |
| 2160 | |
| 2161 | port = list_entry(v, struct tegra_pcie_port, list); |
| 2162 | |
| 2163 | value = readl(port->base + RP_VEND_XP); |
| 2164 | |
| 2165 | if (value & RP_VEND_XP_DL_UP) |
| 2166 | up = true; |
| 2167 | |
| 2168 | value = readl(port->base + RP_LINK_CONTROL_STATUS); |
| 2169 | |
| 2170 | if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE) |
| 2171 | active = true; |
| 2172 | |
| 2173 | seq_printf(s, "%2u ", port->index); |
| 2174 | |
| 2175 | if (up) |
| 2176 | seq_printf(s, "up"); |
| 2177 | |
| 2178 | if (active) { |
| 2179 | if (up) |
| 2180 | seq_printf(s, ", "); |
| 2181 | |
| 2182 | seq_printf(s, "active"); |
| 2183 | } |
| 2184 | |
| 2185 | seq_printf(s, "\n"); |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | static const struct seq_operations tegra_pcie_ports_seq_ops = { |
| 2190 | .start = tegra_pcie_ports_seq_start, |
| 2191 | .next = tegra_pcie_ports_seq_next, |
| 2192 | .stop = tegra_pcie_ports_seq_stop, |
| 2193 | .show = tegra_pcie_ports_seq_show, |
| 2194 | }; |
| 2195 | |
| 2196 | static int tegra_pcie_ports_open(struct inode *inode, struct file *file) |
| 2197 | { |
| 2198 | struct tegra_pcie *pcie = inode->i_private; |
| 2199 | struct seq_file *s; |
| 2200 | int err; |
| 2201 | |
| 2202 | err = seq_open(file, &tegra_pcie_ports_seq_ops); |
| 2203 | if (err) |
| 2204 | return err; |
| 2205 | |
| 2206 | s = file->private_data; |
| 2207 | s->private = pcie; |
| 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | static const struct file_operations tegra_pcie_ports_ops = { |
| 2213 | .owner = THIS_MODULE, |
| 2214 | .open = tegra_pcie_ports_open, |
| 2215 | .read = seq_read, |
| 2216 | .llseek = seq_lseek, |
| 2217 | .release = seq_release, |
| 2218 | }; |
| 2219 | |
| 2220 | static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie) |
| 2221 | { |
| 2222 | struct dentry *file; |
| 2223 | |
| 2224 | pcie->debugfs = debugfs_create_dir("pcie", NULL); |
| 2225 | if (!pcie->debugfs) |
| 2226 | return -ENOMEM; |
| 2227 | |
| 2228 | file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs, |
| 2229 | pcie, &tegra_pcie_ports_ops); |
| 2230 | if (!file) |
| 2231 | goto remove; |
| 2232 | |
| 2233 | return 0; |
| 2234 | |
| 2235 | remove: |
| 2236 | debugfs_remove_recursive(pcie->debugfs); |
| 2237 | pcie->debugfs = NULL; |
| 2238 | return -ENOMEM; |
| 2239 | } |
| 2240 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2241 | static int tegra_pcie_probe(struct platform_device *pdev) |
| 2242 | { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2243 | struct device *dev = &pdev->dev; |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2244 | struct pci_host_bridge *host; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2245 | struct tegra_pcie *pcie; |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2246 | struct pci_bus *child; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2247 | int err; |
| 2248 | |
Lorenzo Pieralisi | 792abc6 | 2017-06-28 15:13:54 -0500 | [diff] [blame] | 2249 | host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2250 | if (!host) |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2251 | return -ENOMEM; |
| 2252 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2253 | pcie = pci_host_bridge_priv(host); |
| 2254 | |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2255 | pcie->soc = of_device_get_match_data(dev); |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 2256 | INIT_LIST_HEAD(&pcie->buses); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2257 | INIT_LIST_HEAD(&pcie->ports); |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2258 | pcie->dev = dev; |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2259 | |
| 2260 | err = tegra_pcie_parse_dt(pcie); |
| 2261 | if (err < 0) |
| 2262 | return err; |
| 2263 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2264 | err = tegra_pcie_get_resources(pcie); |
| 2265 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2266 | dev_err(dev, "failed to request resources: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2267 | return err; |
| 2268 | } |
| 2269 | |
| 2270 | err = tegra_pcie_enable_controller(pcie); |
| 2271 | if (err) |
| 2272 | goto put_resources; |
| 2273 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2274 | err = tegra_pcie_request_resources(pcie); |
| 2275 | if (err) |
| 2276 | goto put_resources; |
| 2277 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2278 | /* setup the AFI address translations */ |
| 2279 | tegra_pcie_setup_translations(pcie); |
| 2280 | |
| 2281 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 2282 | err = tegra_pcie_enable_msi(pcie); |
| 2283 | if (err < 0) { |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2284 | dev_err(dev, "failed to enable MSI support: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2285 | goto put_resources; |
| 2286 | } |
| 2287 | } |
| 2288 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2289 | tegra_pcie_enable_ports(pcie); |
| 2290 | |
| 2291 | pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS); |
| 2292 | host->busnr = pcie->busn.start; |
| 2293 | host->dev.parent = &pdev->dev; |
| 2294 | host->ops = &tegra_pcie_ops; |
Lorenzo Pieralisi | dd5fcce | 2017-06-28 15:14:05 -0500 | [diff] [blame] | 2295 | host->map_irq = tegra_pcie_map_irq; |
| 2296 | host->swizzle_irq = pci_common_swizzle; |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2297 | |
Lorenzo Pieralisi | cea9bc0 | 2017-06-28 15:13:55 -0500 | [diff] [blame] | 2298 | err = pci_scan_root_bus_bridge(host); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2299 | if (err < 0) { |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2300 | dev_err(dev, "failed to register host: %d\n", err); |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2301 | goto disable_msi; |
| 2302 | } |
| 2303 | |
Arnd Bergmann | 76f2541 | 2016-11-25 11:57:12 +0100 | [diff] [blame] | 2304 | pci_bus_size_bridges(host->bus); |
| 2305 | pci_bus_assign_resources(host->bus); |
| 2306 | |
| 2307 | list_for_each_entry(child, &host->bus->children, node) |
| 2308 | pcie_bus_configure_settings(child); |
| 2309 | |
| 2310 | pci_bus_add_devices(host->bus); |
| 2311 | |
Thierry Reding | 2cb989f | 2014-07-22 12:30:46 -0600 | [diff] [blame] | 2312 | if (IS_ENABLED(CONFIG_DEBUG_FS)) { |
| 2313 | err = tegra_pcie_debugfs_init(pcie); |
| 2314 | if (err < 0) |
Bjorn Helgaas | a581fa9 | 2016-10-06 13:43:04 -0500 | [diff] [blame] | 2315 | dev_err(dev, "failed to setup debugfs: %d\n", err); |
Thierry Reding | 2cb989f | 2014-07-22 12:30:46 -0600 | [diff] [blame] | 2316 | } |
| 2317 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2318 | return 0; |
| 2319 | |
| 2320 | disable_msi: |
| 2321 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 2322 | tegra_pcie_disable_msi(pcie); |
| 2323 | put_resources: |
| 2324 | tegra_pcie_put_resources(pcie); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2328 | static struct platform_driver tegra_pcie_driver = { |
| 2329 | .driver = { |
| 2330 | .name = "tegra-pcie", |
Thierry Reding | d1523b5 | 2013-08-09 16:49:19 +0200 | [diff] [blame] | 2331 | .of_match_table = tegra_pcie_of_match, |
| 2332 | .suppress_bind_attrs = true, |
| 2333 | }, |
| 2334 | .probe = tegra_pcie_probe, |
| 2335 | }; |
Paul Gortmaker | ad18327 | 2016-07-02 19:13:31 -0400 | [diff] [blame] | 2336 | builtin_platform_driver(tegra_pcie_driver); |