Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * PCIe host controller driver for the following SoCs
0004  * Tegra194
0005  * Tegra234
0006  *
0007  * Copyright (C) 2019-2022 NVIDIA Corporation.
0008  *
0009  * Author: Vidya Sagar <vidyas@nvidia.com>
0010  */
0011 
0012 #include <linux/clk.h>
0013 #include <linux/debugfs.h>
0014 #include <linux/delay.h>
0015 #include <linux/gpio.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/iopoll.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/of.h>
0022 #include <linux/of_device.h>
0023 #include <linux/of_gpio.h>
0024 #include <linux/of_irq.h>
0025 #include <linux/of_pci.h>
0026 #include <linux/pci.h>
0027 #include <linux/phy/phy.h>
0028 #include <linux/pinctrl/consumer.h>
0029 #include <linux/platform_device.h>
0030 #include <linux/pm_runtime.h>
0031 #include <linux/random.h>
0032 #include <linux/reset.h>
0033 #include <linux/resource.h>
0034 #include <linux/types.h>
0035 #include "pcie-designware.h"
0036 #include <soc/tegra/bpmp.h>
0037 #include <soc/tegra/bpmp-abi.h>
0038 #include "../../pci.h"
0039 
0040 #define TEGRA194_DWC_IP_VER         0x490A
0041 #define TEGRA234_DWC_IP_VER         0x562A
0042 
0043 #define APPL_PINMUX             0x0
0044 #define APPL_PINMUX_PEX_RST         BIT(0)
0045 #define APPL_PINMUX_CLKREQ_OVERRIDE_EN      BIT(2)
0046 #define APPL_PINMUX_CLKREQ_OVERRIDE     BIT(3)
0047 #define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN   BIT(4)
0048 #define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE  BIT(5)
0049 
0050 #define APPL_CTRL               0x4
0051 #define APPL_CTRL_SYS_PRE_DET_STATE     BIT(6)
0052 #define APPL_CTRL_LTSSM_EN          BIT(7)
0053 #define APPL_CTRL_HW_HOT_RST_EN         BIT(20)
0054 #define APPL_CTRL_HW_HOT_RST_MODE_MASK      GENMASK(1, 0)
0055 #define APPL_CTRL_HW_HOT_RST_MODE_SHIFT     22
0056 #define APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST  0x1
0057 #define APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST_LTSSM_EN 0x2
0058 
0059 #define APPL_INTR_EN_L0_0           0x8
0060 #define APPL_INTR_EN_L0_0_LINK_STATE_INT_EN BIT(0)
0061 #define APPL_INTR_EN_L0_0_MSI_RCV_INT_EN    BIT(4)
0062 #define APPL_INTR_EN_L0_0_INT_INT_EN        BIT(8)
0063 #define APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN BIT(15)
0064 #define APPL_INTR_EN_L0_0_CDM_REG_CHK_INT_EN    BIT(19)
0065 #define APPL_INTR_EN_L0_0_SYS_INTR_EN       BIT(30)
0066 #define APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN   BIT(31)
0067 
0068 #define APPL_INTR_STATUS_L0         0xC
0069 #define APPL_INTR_STATUS_L0_LINK_STATE_INT  BIT(0)
0070 #define APPL_INTR_STATUS_L0_INT_INT     BIT(8)
0071 #define APPL_INTR_STATUS_L0_PCI_CMD_EN_INT  BIT(15)
0072 #define APPL_INTR_STATUS_L0_PEX_RST_INT     BIT(16)
0073 #define APPL_INTR_STATUS_L0_CDM_REG_CHK_INT BIT(18)
0074 
0075 #define APPL_INTR_EN_L1_0_0             0x1C
0076 #define APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN BIT(1)
0077 #define APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN     BIT(3)
0078 #define APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN   BIT(30)
0079 
0080 #define APPL_INTR_STATUS_L1_0_0             0x20
0081 #define APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED  BIT(1)
0082 #define APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED  BIT(3)
0083 #define APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE      BIT(30)
0084 
0085 #define APPL_INTR_STATUS_L1_1           0x2C
0086 #define APPL_INTR_STATUS_L1_2           0x30
0087 #define APPL_INTR_STATUS_L1_3           0x34
0088 #define APPL_INTR_STATUS_L1_6           0x3C
0089 #define APPL_INTR_STATUS_L1_7           0x40
0090 #define APPL_INTR_STATUS_L1_15_CFG_BME_CHGED    BIT(1)
0091 
0092 #define APPL_INTR_EN_L1_8_0         0x44
0093 #define APPL_INTR_EN_L1_8_BW_MGT_INT_EN     BIT(2)
0094 #define APPL_INTR_EN_L1_8_AUTO_BW_INT_EN    BIT(3)
0095 #define APPL_INTR_EN_L1_8_INTX_EN       BIT(11)
0096 #define APPL_INTR_EN_L1_8_AER_INT_EN        BIT(15)
0097 
0098 #define APPL_INTR_STATUS_L1_8_0         0x4C
0099 #define APPL_INTR_STATUS_L1_8_0_EDMA_INT_MASK   GENMASK(11, 6)
0100 #define APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS  BIT(2)
0101 #define APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS BIT(3)
0102 
0103 #define APPL_INTR_STATUS_L1_9           0x54
0104 #define APPL_INTR_STATUS_L1_10          0x58
0105 #define APPL_INTR_STATUS_L1_11          0x64
0106 #define APPL_INTR_STATUS_L1_13          0x74
0107 #define APPL_INTR_STATUS_L1_14          0x78
0108 #define APPL_INTR_STATUS_L1_15          0x7C
0109 #define APPL_INTR_STATUS_L1_17          0x88
0110 
0111 #define APPL_INTR_EN_L1_18              0x90
0112 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMPLT        BIT(2)
0113 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR      BIT(1)
0114 #define APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR    BIT(0)
0115 
0116 #define APPL_INTR_STATUS_L1_18              0x94
0117 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT    BIT(2)
0118 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR  BIT(1)
0119 #define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR    BIT(0)
0120 
0121 #define APPL_MSI_CTRL_1             0xAC
0122 
0123 #define APPL_MSI_CTRL_2             0xB0
0124 
0125 #define APPL_LEGACY_INTX            0xB8
0126 
0127 #define APPL_LTR_MSG_1              0xC4
0128 #define LTR_MSG_REQ             BIT(15)
0129 #define LTR_MST_NO_SNOOP_SHIFT          16
0130 
0131 #define APPL_LTR_MSG_2              0xC8
0132 #define APPL_LTR_MSG_2_LTR_MSG_REQ_STATE    BIT(3)
0133 
0134 #define APPL_LINK_STATUS            0xCC
0135 #define APPL_LINK_STATUS_RDLH_LINK_UP       BIT(0)
0136 
0137 #define APPL_DEBUG              0xD0
0138 #define APPL_DEBUG_PM_LINKST_IN_L2_LAT      BIT(21)
0139 #define APPL_DEBUG_PM_LINKST_IN_L0      0x11
0140 #define APPL_DEBUG_LTSSM_STATE_MASK     GENMASK(8, 3)
0141 #define APPL_DEBUG_LTSSM_STATE_SHIFT        3
0142 #define LTSSM_STATE_PRE_DETECT          5
0143 
0144 #define APPL_RADM_STATUS            0xE4
0145 #define APPL_PM_XMT_TURNOFF_STATE       BIT(0)
0146 
0147 #define APPL_DM_TYPE                0x100
0148 #define APPL_DM_TYPE_MASK           GENMASK(3, 0)
0149 #define APPL_DM_TYPE_RP             0x4
0150 #define APPL_DM_TYPE_EP             0x0
0151 
0152 #define APPL_CFG_BASE_ADDR          0x104
0153 #define APPL_CFG_BASE_ADDR_MASK         GENMASK(31, 12)
0154 
0155 #define APPL_CFG_IATU_DMA_BASE_ADDR     0x108
0156 #define APPL_CFG_IATU_DMA_BASE_ADDR_MASK    GENMASK(31, 18)
0157 
0158 #define APPL_CFG_MISC               0x110
0159 #define APPL_CFG_MISC_SLV_EP_MODE       BIT(14)
0160 #define APPL_CFG_MISC_ARCACHE_MASK      GENMASK(13, 10)
0161 #define APPL_CFG_MISC_ARCACHE_SHIFT     10
0162 #define APPL_CFG_MISC_ARCACHE_VAL       3
0163 
0164 #define APPL_CFG_SLCG_OVERRIDE          0x114
0165 #define APPL_CFG_SLCG_OVERRIDE_SLCG_EN_MASTER   BIT(0)
0166 
0167 #define APPL_CAR_RESET_OVRD             0x12C
0168 #define APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N BIT(0)
0169 
0170 #define IO_BASE_IO_DECODE               BIT(0)
0171 #define IO_BASE_IO_DECODE_BIT8              BIT(8)
0172 
0173 #define CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE      BIT(0)
0174 #define CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE    BIT(16)
0175 
0176 #define CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF 0x718
0177 #define CFG_TIMER_CTRL_ACK_NAK_SHIFT    (19)
0178 
0179 #define N_FTS_VAL                   52
0180 #define FTS_VAL                     52
0181 
0182 #define GEN3_EQ_CONTROL_OFF         0x8a8
0183 #define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT  8
0184 #define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK   GENMASK(23, 8)
0185 #define GEN3_EQ_CONTROL_OFF_FB_MODE_MASK    GENMASK(3, 0)
0186 
0187 #define PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT  0x8D0
0188 #define AMBA_ERROR_RESPONSE_CRS_SHIFT       3
0189 #define AMBA_ERROR_RESPONSE_CRS_MASK        GENMASK(1, 0)
0190 #define AMBA_ERROR_RESPONSE_CRS_OKAY        0
0191 #define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFFFFFF   1
0192 #define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001   2
0193 
0194 #define MSIX_ADDR_MATCH_LOW_OFF         0x940
0195 #define MSIX_ADDR_MATCH_LOW_OFF_EN      BIT(0)
0196 #define MSIX_ADDR_MATCH_LOW_OFF_MASK        GENMASK(31, 2)
0197 
0198 #define MSIX_ADDR_MATCH_HIGH_OFF        0x944
0199 #define MSIX_ADDR_MATCH_HIGH_OFF_MASK       GENMASK(31, 0)
0200 
0201 #define PORT_LOGIC_MSIX_DOORBELL            0x948
0202 
0203 #define CAP_SPCIE_CAP_OFF           0x154
0204 #define CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK   GENMASK(3, 0)
0205 #define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK   GENMASK(11, 8)
0206 #define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT  8
0207 
0208 #define PME_ACK_TIMEOUT 10000
0209 
0210 #define LTSSM_TIMEOUT 50000 /* 50ms */
0211 
0212 #define GEN3_GEN4_EQ_PRESET_INIT    5
0213 
0214 #define GEN1_CORE_CLK_FREQ  62500000
0215 #define GEN2_CORE_CLK_FREQ  125000000
0216 #define GEN3_CORE_CLK_FREQ  250000000
0217 #define GEN4_CORE_CLK_FREQ  500000000
0218 
0219 #define LTR_MSG_TIMEOUT     (100 * 1000)
0220 
0221 #define PERST_DEBOUNCE_TIME (5 * 1000)
0222 
0223 #define EP_STATE_DISABLED   0
0224 #define EP_STATE_ENABLED    1
0225 
0226 static const unsigned int pcie_gen_freq[] = {
0227     GEN1_CORE_CLK_FREQ,
0228     GEN2_CORE_CLK_FREQ,
0229     GEN3_CORE_CLK_FREQ,
0230     GEN4_CORE_CLK_FREQ
0231 };
0232 
0233 struct tegra_pcie_dw_of_data {
0234     u32 version;
0235     enum dw_pcie_device_mode mode;
0236     bool has_msix_doorbell_access_fix;
0237     bool has_sbr_reset_fix;
0238     bool has_l1ss_exit_fix;
0239     bool has_ltr_req_fix;
0240     u32 cdm_chk_int_en_bit;
0241     u32 gen4_preset_vec;
0242     u8 n_fts[2];
0243 };
0244 
0245 struct tegra_pcie_dw {
0246     struct device *dev;
0247     struct resource *appl_res;
0248     struct resource *dbi_res;
0249     struct resource *atu_dma_res;
0250     void __iomem *appl_base;
0251     struct clk *core_clk;
0252     struct reset_control *core_apb_rst;
0253     struct reset_control *core_rst;
0254     struct dw_pcie pci;
0255     struct tegra_bpmp *bpmp;
0256 
0257     struct tegra_pcie_dw_of_data *of_data;
0258 
0259     bool supports_clkreq;
0260     bool enable_cdm_check;
0261     bool enable_srns;
0262     bool link_state;
0263     bool update_fc_fixup;
0264     bool enable_ext_refclk;
0265     u8 init_link_width;
0266     u32 msi_ctrl_int;
0267     u32 num_lanes;
0268     u32 cid;
0269     u32 cfg_link_cap_l1sub;
0270     u32 ras_des_cap;
0271     u32 pcie_cap_base;
0272     u32 aspm_cmrt;
0273     u32 aspm_pwr_on_t;
0274     u32 aspm_l0s_enter_lat;
0275 
0276     struct regulator *pex_ctl_supply;
0277     struct regulator *slot_ctl_3v3;
0278     struct regulator *slot_ctl_12v;
0279 
0280     unsigned int phy_count;
0281     struct phy **phys;
0282 
0283     struct dentry *debugfs;
0284 
0285     /* Endpoint mode specific */
0286     struct gpio_desc *pex_rst_gpiod;
0287     struct gpio_desc *pex_refclk_sel_gpiod;
0288     unsigned int pex_rst_irq;
0289     int ep_state;
0290 };
0291 
0292 static inline struct tegra_pcie_dw *to_tegra_pcie(struct dw_pcie *pci)
0293 {
0294     return container_of(pci, struct tegra_pcie_dw, pci);
0295 }
0296 
0297 static inline void appl_writel(struct tegra_pcie_dw *pcie, const u32 value,
0298                    const u32 reg)
0299 {
0300     writel_relaxed(value, pcie->appl_base + reg);
0301 }
0302 
0303 static inline u32 appl_readl(struct tegra_pcie_dw *pcie, const u32 reg)
0304 {
0305     return readl_relaxed(pcie->appl_base + reg);
0306 }
0307 
0308 struct tegra_pcie_soc {
0309     enum dw_pcie_device_mode mode;
0310 };
0311 
0312 static void apply_bad_link_workaround(struct dw_pcie_rp *pp)
0313 {
0314     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0315     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0316     u32 current_link_width;
0317     u16 val;
0318 
0319     /*
0320      * NOTE:- Since this scenario is uncommon and link as such is not
0321      * stable anyway, not waiting to confirm if link is really
0322      * transitioning to Gen-2 speed
0323      */
0324     val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA);
0325     if (val & PCI_EXP_LNKSTA_LBMS) {
0326         current_link_width = (val & PCI_EXP_LNKSTA_NLW) >>
0327                      PCI_EXP_LNKSTA_NLW_SHIFT;
0328         if (pcie->init_link_width > current_link_width) {
0329             dev_warn(pci->dev, "PCIe link is bad, width reduced\n");
0330             val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
0331                         PCI_EXP_LNKCTL2);
0332             val &= ~PCI_EXP_LNKCTL2_TLS;
0333             val |= PCI_EXP_LNKCTL2_TLS_2_5GT;
0334             dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
0335                        PCI_EXP_LNKCTL2, val);
0336 
0337             val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
0338                         PCI_EXP_LNKCTL);
0339             val |= PCI_EXP_LNKCTL_RL;
0340             dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
0341                        PCI_EXP_LNKCTL, val);
0342         }
0343     }
0344 }
0345 
0346 static irqreturn_t tegra_pcie_rp_irq_handler(int irq, void *arg)
0347 {
0348     struct tegra_pcie_dw *pcie = arg;
0349     struct dw_pcie *pci = &pcie->pci;
0350     struct dw_pcie_rp *pp = &pci->pp;
0351     u32 val, status_l0, status_l1;
0352     u16 val_w;
0353 
0354     status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0);
0355     if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) {
0356         status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0);
0357         appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0);
0358         if (!pcie->of_data->has_sbr_reset_fix &&
0359             status_l1 & APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED) {
0360             /* SBR & Surprise Link Down WAR */
0361             val = appl_readl(pcie, APPL_CAR_RESET_OVRD);
0362             val &= ~APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N;
0363             appl_writel(pcie, val, APPL_CAR_RESET_OVRD);
0364             udelay(1);
0365             val = appl_readl(pcie, APPL_CAR_RESET_OVRD);
0366             val |= APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N;
0367             appl_writel(pcie, val, APPL_CAR_RESET_OVRD);
0368 
0369             val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
0370             val |= PORT_LOGIC_SPEED_CHANGE;
0371             dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
0372         }
0373     }
0374 
0375     if (status_l0 & APPL_INTR_STATUS_L0_INT_INT) {
0376         status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_8_0);
0377         if (status_l1 & APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS) {
0378             appl_writel(pcie,
0379                     APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS,
0380                     APPL_INTR_STATUS_L1_8_0);
0381             apply_bad_link_workaround(pp);
0382         }
0383         if (status_l1 & APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS) {
0384             val_w = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
0385                           PCI_EXP_LNKSTA);
0386             val_w |= PCI_EXP_LNKSTA_LBMS;
0387             dw_pcie_writew_dbi(pci, pcie->pcie_cap_base +
0388                        PCI_EXP_LNKSTA, val_w);
0389 
0390             appl_writel(pcie,
0391                     APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS,
0392                     APPL_INTR_STATUS_L1_8_0);
0393 
0394             val_w = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
0395                           PCI_EXP_LNKSTA);
0396             dev_dbg(pci->dev, "Link Speed : Gen-%u\n", val_w &
0397                 PCI_EXP_LNKSTA_CLS);
0398         }
0399     }
0400 
0401     if (status_l0 & APPL_INTR_STATUS_L0_CDM_REG_CHK_INT) {
0402         status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_18);
0403         val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS);
0404         if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT) {
0405             dev_info(pci->dev, "CDM check complete\n");
0406             val |= PCIE_PL_CHK_REG_CHK_REG_COMPLETE;
0407         }
0408         if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR) {
0409             dev_err(pci->dev, "CDM comparison mismatch\n");
0410             val |= PCIE_PL_CHK_REG_CHK_REG_COMPARISON_ERROR;
0411         }
0412         if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR) {
0413             dev_err(pci->dev, "CDM Logic error\n");
0414             val |= PCIE_PL_CHK_REG_CHK_REG_LOGIC_ERROR;
0415         }
0416         dw_pcie_writel_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS, val);
0417         val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_ERR_ADDR);
0418         dev_err(pci->dev, "CDM Error Address Offset = 0x%08X\n", val);
0419     }
0420 
0421     return IRQ_HANDLED;
0422 }
0423 
0424 static void pex_ep_event_hot_rst_done(struct tegra_pcie_dw *pcie)
0425 {
0426     u32 val;
0427 
0428     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
0429     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
0430     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
0431     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
0432     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
0433     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
0434     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
0435     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
0436     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
0437     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
0438     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
0439     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
0440     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
0441     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
0442     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
0443     appl_writel(pcie, 0xFFFFFFFF, APPL_MSI_CTRL_2);
0444 
0445     val = appl_readl(pcie, APPL_CTRL);
0446     val |= APPL_CTRL_LTSSM_EN;
0447     appl_writel(pcie, val, APPL_CTRL);
0448 }
0449 
0450 static irqreturn_t tegra_pcie_ep_irq_thread(int irq, void *arg)
0451 {
0452     struct tegra_pcie_dw *pcie = arg;
0453     struct dw_pcie *pci = &pcie->pci;
0454     u32 val, speed;
0455 
0456     speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) &
0457         PCI_EXP_LNKSTA_CLS;
0458     clk_set_rate(pcie->core_clk, pcie_gen_freq[speed - 1]);
0459 
0460     if (pcie->of_data->has_ltr_req_fix)
0461         return IRQ_HANDLED;
0462 
0463     /* If EP doesn't advertise L1SS, just return */
0464     val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub);
0465     if (!(val & (PCI_L1SS_CAP_ASPM_L1_1 | PCI_L1SS_CAP_ASPM_L1_2)))
0466         return IRQ_HANDLED;
0467 
0468     /* Check if BME is set to '1' */
0469     val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
0470     if (val & PCI_COMMAND_MASTER) {
0471         ktime_t timeout;
0472 
0473         /* 110us for both snoop and no-snoop */
0474         val = 110 | (2 << PCI_LTR_SCALE_SHIFT) | LTR_MSG_REQ;
0475         val |= (val << LTR_MST_NO_SNOOP_SHIFT);
0476         appl_writel(pcie, val, APPL_LTR_MSG_1);
0477 
0478         /* Send LTR upstream */
0479         val = appl_readl(pcie, APPL_LTR_MSG_2);
0480         val |= APPL_LTR_MSG_2_LTR_MSG_REQ_STATE;
0481         appl_writel(pcie, val, APPL_LTR_MSG_2);
0482 
0483         timeout = ktime_add_us(ktime_get(), LTR_MSG_TIMEOUT);
0484         for (;;) {
0485             val = appl_readl(pcie, APPL_LTR_MSG_2);
0486             if (!(val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE))
0487                 break;
0488             if (ktime_after(ktime_get(), timeout))
0489                 break;
0490             usleep_range(1000, 1100);
0491         }
0492         if (val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE)
0493             dev_err(pcie->dev, "Failed to send LTR message\n");
0494     }
0495 
0496     return IRQ_HANDLED;
0497 }
0498 
0499 static irqreturn_t tegra_pcie_ep_hard_irq(int irq, void *arg)
0500 {
0501     struct tegra_pcie_dw *pcie = arg;
0502     struct dw_pcie_ep *ep = &pcie->pci.ep;
0503     int spurious = 1;
0504     u32 status_l0, status_l1, link_status;
0505 
0506     status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0);
0507     if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) {
0508         status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0);
0509         appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0);
0510 
0511         if (status_l1 & APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE)
0512             pex_ep_event_hot_rst_done(pcie);
0513 
0514         if (status_l1 & APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED) {
0515             link_status = appl_readl(pcie, APPL_LINK_STATUS);
0516             if (link_status & APPL_LINK_STATUS_RDLH_LINK_UP) {
0517                 dev_dbg(pcie->dev, "Link is up with Host\n");
0518                 dw_pcie_ep_linkup(ep);
0519             }
0520         }
0521 
0522         spurious = 0;
0523     }
0524 
0525     if (status_l0 & APPL_INTR_STATUS_L0_PCI_CMD_EN_INT) {
0526         status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_15);
0527         appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_15);
0528 
0529         if (status_l1 & APPL_INTR_STATUS_L1_15_CFG_BME_CHGED)
0530             return IRQ_WAKE_THREAD;
0531 
0532         spurious = 0;
0533     }
0534 
0535     if (spurious) {
0536         dev_warn(pcie->dev, "Random interrupt (STATUS = 0x%08X)\n",
0537              status_l0);
0538         appl_writel(pcie, status_l0, APPL_INTR_STATUS_L0);
0539     }
0540 
0541     return IRQ_HANDLED;
0542 }
0543 
0544 static int tegra_pcie_dw_rd_own_conf(struct pci_bus *bus, u32 devfn, int where,
0545                      int size, u32 *val)
0546 {
0547     struct dw_pcie_rp *pp = bus->sysdata;
0548     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0549     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0550 
0551     /*
0552      * This is an endpoint mode specific register happen to appear even
0553      * when controller is operating in root port mode and system hangs
0554      * when it is accessed with link being in ASPM-L1 state.
0555      * So skip accessing it altogether
0556      */
0557     if (!pcie->of_data->has_msix_doorbell_access_fix &&
0558         !PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL) {
0559         *val = 0x00000000;
0560         return PCIBIOS_SUCCESSFUL;
0561     }
0562 
0563     return pci_generic_config_read(bus, devfn, where, size, val);
0564 }
0565 
0566 static int tegra_pcie_dw_wr_own_conf(struct pci_bus *bus, u32 devfn, int where,
0567                      int size, u32 val)
0568 {
0569     struct dw_pcie_rp *pp = bus->sysdata;
0570     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0571     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0572 
0573     /*
0574      * This is an endpoint mode specific register happen to appear even
0575      * when controller is operating in root port mode and system hangs
0576      * when it is accessed with link being in ASPM-L1 state.
0577      * So skip accessing it altogether
0578      */
0579     if (!pcie->of_data->has_msix_doorbell_access_fix &&
0580         !PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL)
0581         return PCIBIOS_SUCCESSFUL;
0582 
0583     return pci_generic_config_write(bus, devfn, where, size, val);
0584 }
0585 
0586 static struct pci_ops tegra_pci_ops = {
0587     .map_bus = dw_pcie_own_conf_map_bus,
0588     .read = tegra_pcie_dw_rd_own_conf,
0589     .write = tegra_pcie_dw_wr_own_conf,
0590 };
0591 
0592 #if defined(CONFIG_PCIEASPM)
0593 static void disable_aspm_l11(struct tegra_pcie_dw *pcie)
0594 {
0595     u32 val;
0596 
0597     val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub);
0598     val &= ~PCI_L1SS_CAP_ASPM_L1_1;
0599     dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val);
0600 }
0601 
0602 static void disable_aspm_l12(struct tegra_pcie_dw *pcie)
0603 {
0604     u32 val;
0605 
0606     val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub);
0607     val &= ~PCI_L1SS_CAP_ASPM_L1_2;
0608     dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val);
0609 }
0610 
0611 static inline u32 event_counter_prog(struct tegra_pcie_dw *pcie, u32 event)
0612 {
0613     u32 val;
0614 
0615     val = dw_pcie_readl_dbi(&pcie->pci, pcie->ras_des_cap +
0616                 PCIE_RAS_DES_EVENT_COUNTER_CONTROL);
0617     val &= ~(EVENT_COUNTER_EVENT_SEL_MASK << EVENT_COUNTER_EVENT_SEL_SHIFT);
0618     val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
0619     val |= event << EVENT_COUNTER_EVENT_SEL_SHIFT;
0620     val |= EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
0621     dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
0622                PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
0623     val = dw_pcie_readl_dbi(&pcie->pci, pcie->ras_des_cap +
0624                 PCIE_RAS_DES_EVENT_COUNTER_DATA);
0625 
0626     return val;
0627 }
0628 
0629 static int aspm_state_cnt(struct seq_file *s, void *data)
0630 {
0631     struct tegra_pcie_dw *pcie = (struct tegra_pcie_dw *)
0632                      dev_get_drvdata(s->private);
0633     u32 val;
0634 
0635     seq_printf(s, "Tx L0s entry count : %u\n",
0636            event_counter_prog(pcie, EVENT_COUNTER_EVENT_Tx_L0S));
0637 
0638     seq_printf(s, "Rx L0s entry count : %u\n",
0639            event_counter_prog(pcie, EVENT_COUNTER_EVENT_Rx_L0S));
0640 
0641     seq_printf(s, "Link L1 entry count : %u\n",
0642            event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1));
0643 
0644     seq_printf(s, "Link L1.1 entry count : %u\n",
0645            event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_1));
0646 
0647     seq_printf(s, "Link L1.2 entry count : %u\n",
0648            event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_2));
0649 
0650     /* Clear all counters */
0651     dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
0652                PCIE_RAS_DES_EVENT_COUNTER_CONTROL,
0653                EVENT_COUNTER_ALL_CLEAR);
0654 
0655     /* Re-enable counting */
0656     val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
0657     val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
0658     dw_pcie_writel_dbi(&pcie->pci, pcie->ras_des_cap +
0659                PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
0660 
0661     return 0;
0662 }
0663 
0664 static void init_host_aspm(struct tegra_pcie_dw *pcie)
0665 {
0666     struct dw_pcie *pci = &pcie->pci;
0667     u32 val;
0668 
0669     val = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_L1SS);
0670     pcie->cfg_link_cap_l1sub = val + PCI_L1SS_CAP;
0671 
0672     pcie->ras_des_cap = dw_pcie_find_ext_capability(&pcie->pci,
0673                             PCI_EXT_CAP_ID_VNDR);
0674 
0675     /* Enable ASPM counters */
0676     val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT;
0677     val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT;
0678     dw_pcie_writel_dbi(pci, pcie->ras_des_cap +
0679                PCIE_RAS_DES_EVENT_COUNTER_CONTROL, val);
0680 
0681     /* Program T_cmrt and T_pwr_on values */
0682     val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub);
0683     val &= ~(PCI_L1SS_CAP_CM_RESTORE_TIME | PCI_L1SS_CAP_P_PWR_ON_VALUE);
0684     val |= (pcie->aspm_cmrt << 8);
0685     val |= (pcie->aspm_pwr_on_t << 19);
0686     dw_pcie_writel_dbi(pci, pcie->cfg_link_cap_l1sub, val);
0687 
0688     /* Program L0s and L1 entrance latencies */
0689     val = dw_pcie_readl_dbi(pci, PCIE_PORT_AFR);
0690     val &= ~PORT_AFR_L0S_ENTRANCE_LAT_MASK;
0691     val |= (pcie->aspm_l0s_enter_lat << PORT_AFR_L0S_ENTRANCE_LAT_SHIFT);
0692     val |= PORT_AFR_ENTER_ASPM;
0693     dw_pcie_writel_dbi(pci, PCIE_PORT_AFR, val);
0694 }
0695 
0696 static void init_debugfs(struct tegra_pcie_dw *pcie)
0697 {
0698     debugfs_create_devm_seqfile(pcie->dev, "aspm_state_cnt", pcie->debugfs,
0699                     aspm_state_cnt);
0700 }
0701 #else
0702 static inline void disable_aspm_l12(struct tegra_pcie_dw *pcie) { return; }
0703 static inline void disable_aspm_l11(struct tegra_pcie_dw *pcie) { return; }
0704 static inline void init_host_aspm(struct tegra_pcie_dw *pcie) { return; }
0705 static inline void init_debugfs(struct tegra_pcie_dw *pcie) { return; }
0706 #endif
0707 
0708 static void tegra_pcie_enable_system_interrupts(struct dw_pcie_rp *pp)
0709 {
0710     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0711     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0712     u32 val;
0713     u16 val_w;
0714 
0715     val = appl_readl(pcie, APPL_INTR_EN_L0_0);
0716     val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN;
0717     appl_writel(pcie, val, APPL_INTR_EN_L0_0);
0718 
0719     if (!pcie->of_data->has_sbr_reset_fix) {
0720         val = appl_readl(pcie, APPL_INTR_EN_L1_0_0);
0721         val |= APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN;
0722         appl_writel(pcie, val, APPL_INTR_EN_L1_0_0);
0723     }
0724 
0725     if (pcie->enable_cdm_check) {
0726         val = appl_readl(pcie, APPL_INTR_EN_L0_0);
0727         val |= pcie->of_data->cdm_chk_int_en_bit;
0728         appl_writel(pcie, val, APPL_INTR_EN_L0_0);
0729 
0730         val = appl_readl(pcie, APPL_INTR_EN_L1_18);
0731         val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR;
0732         val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR;
0733         appl_writel(pcie, val, APPL_INTR_EN_L1_18);
0734     }
0735 
0736     val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base +
0737                   PCI_EXP_LNKSTA);
0738     pcie->init_link_width = (val_w & PCI_EXP_LNKSTA_NLW) >>
0739                 PCI_EXP_LNKSTA_NLW_SHIFT;
0740 
0741     val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base +
0742                   PCI_EXP_LNKCTL);
0743     val_w |= PCI_EXP_LNKCTL_LBMIE;
0744     dw_pcie_writew_dbi(&pcie->pci, pcie->pcie_cap_base + PCI_EXP_LNKCTL,
0745                val_w);
0746 }
0747 
0748 static void tegra_pcie_enable_legacy_interrupts(struct dw_pcie_rp *pp)
0749 {
0750     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0751     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0752     u32 val;
0753 
0754     /* Enable legacy interrupt generation */
0755     val = appl_readl(pcie, APPL_INTR_EN_L0_0);
0756     val |= APPL_INTR_EN_L0_0_SYS_INTR_EN;
0757     val |= APPL_INTR_EN_L0_0_INT_INT_EN;
0758     appl_writel(pcie, val, APPL_INTR_EN_L0_0);
0759 
0760     val = appl_readl(pcie, APPL_INTR_EN_L1_8_0);
0761     val |= APPL_INTR_EN_L1_8_INTX_EN;
0762     val |= APPL_INTR_EN_L1_8_AUTO_BW_INT_EN;
0763     val |= APPL_INTR_EN_L1_8_BW_MGT_INT_EN;
0764     if (IS_ENABLED(CONFIG_PCIEAER))
0765         val |= APPL_INTR_EN_L1_8_AER_INT_EN;
0766     appl_writel(pcie, val, APPL_INTR_EN_L1_8_0);
0767 }
0768 
0769 static void tegra_pcie_enable_msi_interrupts(struct dw_pcie_rp *pp)
0770 {
0771     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0772     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0773     u32 val;
0774 
0775     /* Enable MSI interrupt generation */
0776     val = appl_readl(pcie, APPL_INTR_EN_L0_0);
0777     val |= APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN;
0778     val |= APPL_INTR_EN_L0_0_MSI_RCV_INT_EN;
0779     appl_writel(pcie, val, APPL_INTR_EN_L0_0);
0780 }
0781 
0782 static void tegra_pcie_enable_interrupts(struct dw_pcie_rp *pp)
0783 {
0784     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0785     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0786 
0787     /* Clear interrupt statuses before enabling interrupts */
0788     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
0789     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
0790     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
0791     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
0792     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
0793     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
0794     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
0795     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
0796     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
0797     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
0798     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
0799     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
0800     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
0801     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
0802     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
0803 
0804     tegra_pcie_enable_system_interrupts(pp);
0805     tegra_pcie_enable_legacy_interrupts(pp);
0806     if (IS_ENABLED(CONFIG_PCI_MSI))
0807         tegra_pcie_enable_msi_interrupts(pp);
0808 }
0809 
0810 static void config_gen3_gen4_eq_presets(struct tegra_pcie_dw *pcie)
0811 {
0812     struct dw_pcie *pci = &pcie->pci;
0813     u32 val, offset, i;
0814 
0815     /* Program init preset */
0816     for (i = 0; i < pcie->num_lanes; i++) {
0817         val = dw_pcie_readw_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2));
0818         val &= ~CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK;
0819         val |= GEN3_GEN4_EQ_PRESET_INIT;
0820         val &= ~CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK;
0821         val |= (GEN3_GEN4_EQ_PRESET_INIT <<
0822                CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT);
0823         dw_pcie_writew_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2), val);
0824 
0825         offset = dw_pcie_find_ext_capability(pci,
0826                              PCI_EXT_CAP_ID_PL_16GT) +
0827                 PCI_PL_16GT_LE_CTRL;
0828         val = dw_pcie_readb_dbi(pci, offset + i);
0829         val &= ~PCI_PL_16GT_LE_CTRL_DSP_TX_PRESET_MASK;
0830         val |= GEN3_GEN4_EQ_PRESET_INIT;
0831         val &= ~PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK;
0832         val |= (GEN3_GEN4_EQ_PRESET_INIT <<
0833             PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT);
0834         dw_pcie_writeb_dbi(pci, offset + i, val);
0835     }
0836 
0837     val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
0838     val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
0839     dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
0840 
0841     val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF);
0842     val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK;
0843     val |= (0x3ff << GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT);
0844     val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK;
0845     dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val);
0846 
0847     val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
0848     val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
0849     val |= (0x1 << GEN3_RELATED_OFF_RATE_SHADOW_SEL_SHIFT);
0850     dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
0851 
0852     val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF);
0853     val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK;
0854     val |= (pcie->of_data->gen4_preset_vec <<
0855         GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT);
0856     val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK;
0857     dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val);
0858 
0859     val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
0860     val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK;
0861     dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
0862 }
0863 
0864 static int tegra_pcie_dw_host_init(struct dw_pcie_rp *pp)
0865 {
0866     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0867     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0868     u32 val;
0869     u16 val_16;
0870 
0871     pp->bridge->ops = &tegra_pci_ops;
0872 
0873     if (!pcie->pcie_cap_base)
0874         pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci,
0875                                   PCI_CAP_ID_EXP);
0876 
0877     val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL);
0878     val_16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
0879     val_16 |= PCI_EXP_DEVCTL_PAYLOAD_256B;
0880     dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL, val_16);
0881 
0882     val = dw_pcie_readl_dbi(pci, PCI_IO_BASE);
0883     val &= ~(IO_BASE_IO_DECODE | IO_BASE_IO_DECODE_BIT8);
0884     dw_pcie_writel_dbi(pci, PCI_IO_BASE, val);
0885 
0886     val = dw_pcie_readl_dbi(pci, PCI_PREF_MEMORY_BASE);
0887     val |= CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE;
0888     val |= CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE;
0889     dw_pcie_writel_dbi(pci, PCI_PREF_MEMORY_BASE, val);
0890 
0891     dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
0892 
0893     /* Enable as 0xFFFF0001 response for CRS */
0894     val = dw_pcie_readl_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT);
0895     val &= ~(AMBA_ERROR_RESPONSE_CRS_MASK << AMBA_ERROR_RESPONSE_CRS_SHIFT);
0896     val |= (AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001 <<
0897         AMBA_ERROR_RESPONSE_CRS_SHIFT);
0898     dw_pcie_writel_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT, val);
0899 
0900     /* Configure Max lane width from DT */
0901     val = dw_pcie_readl_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP);
0902     val &= ~PCI_EXP_LNKCAP_MLW;
0903     val |= (pcie->num_lanes << PCI_EXP_LNKSTA_NLW_SHIFT);
0904     dw_pcie_writel_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP, val);
0905 
0906     /* Clear Slot Clock Configuration bit if SRNS configuration */
0907     if (pcie->enable_srns) {
0908         val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
0909                        PCI_EXP_LNKSTA);
0910         val_16 &= ~PCI_EXP_LNKSTA_SLC;
0911         dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA,
0912                    val_16);
0913     }
0914 
0915     config_gen3_gen4_eq_presets(pcie);
0916 
0917     init_host_aspm(pcie);
0918 
0919     /* Disable ASPM-L1SS advertisement if there is no CLKREQ routing */
0920     if (!pcie->supports_clkreq) {
0921         disable_aspm_l11(pcie);
0922         disable_aspm_l12(pcie);
0923     }
0924 
0925     if (!pcie->of_data->has_l1ss_exit_fix) {
0926         val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
0927         val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL;
0928         dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
0929     }
0930 
0931     if (pcie->update_fc_fixup) {
0932         val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF);
0933         val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT;
0934         dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val);
0935     }
0936 
0937     clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ);
0938 
0939     return 0;
0940 }
0941 
0942 static int tegra_pcie_dw_start_link(struct dw_pcie *pci)
0943 {
0944     u32 val, offset, speed, tmp;
0945     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
0946     struct dw_pcie_rp *pp = &pci->pp;
0947     bool retry = true;
0948 
0949     if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
0950         enable_irq(pcie->pex_rst_irq);
0951         return 0;
0952     }
0953 
0954 retry_link:
0955     /* Assert RST */
0956     val = appl_readl(pcie, APPL_PINMUX);
0957     val &= ~APPL_PINMUX_PEX_RST;
0958     appl_writel(pcie, val, APPL_PINMUX);
0959 
0960     usleep_range(100, 200);
0961 
0962     /* Enable LTSSM */
0963     val = appl_readl(pcie, APPL_CTRL);
0964     val |= APPL_CTRL_LTSSM_EN;
0965     appl_writel(pcie, val, APPL_CTRL);
0966 
0967     /* De-assert RST */
0968     val = appl_readl(pcie, APPL_PINMUX);
0969     val |= APPL_PINMUX_PEX_RST;
0970     appl_writel(pcie, val, APPL_PINMUX);
0971 
0972     msleep(100);
0973 
0974     if (dw_pcie_wait_for_link(pci)) {
0975         if (!retry)
0976             return 0;
0977         /*
0978          * There are some endpoints which can't get the link up if
0979          * root port has Data Link Feature (DLF) enabled.
0980          * Refer Spec rev 4.0 ver 1.0 sec 3.4.2 & 7.7.4 for more info
0981          * on Scaled Flow Control and DLF.
0982          * So, need to confirm that is indeed the case here and attempt
0983          * link up once again with DLF disabled.
0984          */
0985         val = appl_readl(pcie, APPL_DEBUG);
0986         val &= APPL_DEBUG_LTSSM_STATE_MASK;
0987         val >>= APPL_DEBUG_LTSSM_STATE_SHIFT;
0988         tmp = appl_readl(pcie, APPL_LINK_STATUS);
0989         tmp &= APPL_LINK_STATUS_RDLH_LINK_UP;
0990         if (!(val == 0x11 && !tmp)) {
0991             /* Link is down for all good reasons */
0992             return 0;
0993         }
0994 
0995         dev_info(pci->dev, "Link is down in DLL");
0996         dev_info(pci->dev, "Trying again with DLFE disabled\n");
0997         /* Disable LTSSM */
0998         val = appl_readl(pcie, APPL_CTRL);
0999         val &= ~APPL_CTRL_LTSSM_EN;
1000         appl_writel(pcie, val, APPL_CTRL);
1001 
1002         reset_control_assert(pcie->core_rst);
1003         reset_control_deassert(pcie->core_rst);
1004 
1005         offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_DLF);
1006         val = dw_pcie_readl_dbi(pci, offset + PCI_DLF_CAP);
1007         val &= ~PCI_DLF_EXCHANGE_ENABLE;
1008         dw_pcie_writel_dbi(pci, offset + PCI_DLF_CAP, val);
1009 
1010         tegra_pcie_dw_host_init(pp);
1011         dw_pcie_setup_rc(pp);
1012 
1013         retry = false;
1014         goto retry_link;
1015     }
1016 
1017     speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) &
1018         PCI_EXP_LNKSTA_CLS;
1019     clk_set_rate(pcie->core_clk, pcie_gen_freq[speed - 1]);
1020 
1021     tegra_pcie_enable_interrupts(pp);
1022 
1023     return 0;
1024 }
1025 
1026 static int tegra_pcie_dw_link_up(struct dw_pcie *pci)
1027 {
1028     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1029     u32 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA);
1030 
1031     return !!(val & PCI_EXP_LNKSTA_DLLLA);
1032 }
1033 
1034 static void tegra_pcie_dw_stop_link(struct dw_pcie *pci)
1035 {
1036     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1037 
1038     disable_irq(pcie->pex_rst_irq);
1039 }
1040 
1041 static const struct dw_pcie_ops tegra_dw_pcie_ops = {
1042     .link_up = tegra_pcie_dw_link_up,
1043     .start_link = tegra_pcie_dw_start_link,
1044     .stop_link = tegra_pcie_dw_stop_link,
1045 };
1046 
1047 static const struct dw_pcie_host_ops tegra_pcie_dw_host_ops = {
1048     .host_init = tegra_pcie_dw_host_init,
1049 };
1050 
1051 static void tegra_pcie_disable_phy(struct tegra_pcie_dw *pcie)
1052 {
1053     unsigned int phy_count = pcie->phy_count;
1054 
1055     while (phy_count--) {
1056         phy_power_off(pcie->phys[phy_count]);
1057         phy_exit(pcie->phys[phy_count]);
1058     }
1059 }
1060 
1061 static int tegra_pcie_enable_phy(struct tegra_pcie_dw *pcie)
1062 {
1063     unsigned int i;
1064     int ret;
1065 
1066     for (i = 0; i < pcie->phy_count; i++) {
1067         ret = phy_init(pcie->phys[i]);
1068         if (ret < 0)
1069             goto phy_power_off;
1070 
1071         ret = phy_power_on(pcie->phys[i]);
1072         if (ret < 0)
1073             goto phy_exit;
1074     }
1075 
1076     return 0;
1077 
1078 phy_power_off:
1079     while (i--) {
1080         phy_power_off(pcie->phys[i]);
1081 phy_exit:
1082         phy_exit(pcie->phys[i]);
1083     }
1084 
1085     return ret;
1086 }
1087 
1088 static int tegra_pcie_dw_parse_dt(struct tegra_pcie_dw *pcie)
1089 {
1090     struct platform_device *pdev = to_platform_device(pcie->dev);
1091     struct device_node *np = pcie->dev->of_node;
1092     int ret;
1093 
1094     pcie->dbi_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
1095     if (!pcie->dbi_res) {
1096         dev_err(pcie->dev, "Failed to find \"dbi\" region\n");
1097         return -ENODEV;
1098     }
1099 
1100     ret = of_property_read_u32(np, "nvidia,aspm-cmrt-us", &pcie->aspm_cmrt);
1101     if (ret < 0) {
1102         dev_info(pcie->dev, "Failed to read ASPM T_cmrt: %d\n", ret);
1103         return ret;
1104     }
1105 
1106     ret = of_property_read_u32(np, "nvidia,aspm-pwr-on-t-us",
1107                    &pcie->aspm_pwr_on_t);
1108     if (ret < 0)
1109         dev_info(pcie->dev, "Failed to read ASPM Power On time: %d\n",
1110              ret);
1111 
1112     ret = of_property_read_u32(np, "nvidia,aspm-l0s-entrance-latency-us",
1113                    &pcie->aspm_l0s_enter_lat);
1114     if (ret < 0)
1115         dev_info(pcie->dev,
1116              "Failed to read ASPM L0s Entrance latency: %d\n", ret);
1117 
1118     ret = of_property_read_u32(np, "num-lanes", &pcie->num_lanes);
1119     if (ret < 0) {
1120         dev_err(pcie->dev, "Failed to read num-lanes: %d\n", ret);
1121         return ret;
1122     }
1123 
1124     ret = of_property_read_u32_index(np, "nvidia,bpmp", 1, &pcie->cid);
1125     if (ret) {
1126         dev_err(pcie->dev, "Failed to read Controller-ID: %d\n", ret);
1127         return ret;
1128     }
1129 
1130     ret = of_property_count_strings(np, "phy-names");
1131     if (ret < 0) {
1132         dev_err(pcie->dev, "Failed to find PHY entries: %d\n",
1133             ret);
1134         return ret;
1135     }
1136     pcie->phy_count = ret;
1137 
1138     if (of_property_read_bool(np, "nvidia,update-fc-fixup"))
1139         pcie->update_fc_fixup = true;
1140 
1141     /* RP using an external REFCLK is supported only in Tegra234 */
1142     if (pcie->of_data->version == TEGRA194_DWC_IP_VER) {
1143         if (pcie->of_data->mode == DW_PCIE_EP_TYPE)
1144             pcie->enable_ext_refclk = true;
1145     } else {
1146         pcie->enable_ext_refclk =
1147             of_property_read_bool(pcie->dev->of_node,
1148                           "nvidia,enable-ext-refclk");
1149     }
1150 
1151     pcie->supports_clkreq =
1152         of_property_read_bool(pcie->dev->of_node, "supports-clkreq");
1153 
1154     pcie->enable_cdm_check =
1155         of_property_read_bool(np, "snps,enable-cdm-check");
1156 
1157     if (pcie->of_data->version == TEGRA234_DWC_IP_VER)
1158         pcie->enable_srns =
1159             of_property_read_bool(np, "nvidia,enable-srns");
1160 
1161     if (pcie->of_data->mode == DW_PCIE_RC_TYPE)
1162         return 0;
1163 
1164     /* Endpoint mode specific DT entries */
1165     pcie->pex_rst_gpiod = devm_gpiod_get(pcie->dev, "reset", GPIOD_IN);
1166     if (IS_ERR(pcie->pex_rst_gpiod)) {
1167         int err = PTR_ERR(pcie->pex_rst_gpiod);
1168         const char *level = KERN_ERR;
1169 
1170         if (err == -EPROBE_DEFER)
1171             level = KERN_DEBUG;
1172 
1173         dev_printk(level, pcie->dev,
1174                dev_fmt("Failed to get PERST GPIO: %d\n"),
1175                err);
1176         return err;
1177     }
1178 
1179     pcie->pex_refclk_sel_gpiod = devm_gpiod_get(pcie->dev,
1180                             "nvidia,refclk-select",
1181                             GPIOD_OUT_HIGH);
1182     if (IS_ERR(pcie->pex_refclk_sel_gpiod)) {
1183         int err = PTR_ERR(pcie->pex_refclk_sel_gpiod);
1184         const char *level = KERN_ERR;
1185 
1186         if (err == -EPROBE_DEFER)
1187             level = KERN_DEBUG;
1188 
1189         dev_printk(level, pcie->dev,
1190                dev_fmt("Failed to get REFCLK select GPIOs: %d\n"),
1191                err);
1192         pcie->pex_refclk_sel_gpiod = NULL;
1193     }
1194 
1195     return 0;
1196 }
1197 
1198 static int tegra_pcie_bpmp_set_ctrl_state(struct tegra_pcie_dw *pcie,
1199                       bool enable)
1200 {
1201     struct mrq_uphy_response resp;
1202     struct tegra_bpmp_message msg;
1203     struct mrq_uphy_request req;
1204 
1205     /*
1206      * Controller-5 doesn't need to have its state set by BPMP-FW in
1207      * Tegra194
1208      */
1209     if (pcie->of_data->version == TEGRA194_DWC_IP_VER && pcie->cid == 5)
1210         return 0;
1211 
1212     memset(&req, 0, sizeof(req));
1213     memset(&resp, 0, sizeof(resp));
1214 
1215     req.cmd = CMD_UPHY_PCIE_CONTROLLER_STATE;
1216     req.controller_state.pcie_controller = pcie->cid;
1217     req.controller_state.enable = enable;
1218 
1219     memset(&msg, 0, sizeof(msg));
1220     msg.mrq = MRQ_UPHY;
1221     msg.tx.data = &req;
1222     msg.tx.size = sizeof(req);
1223     msg.rx.data = &resp;
1224     msg.rx.size = sizeof(resp);
1225 
1226     return tegra_bpmp_transfer(pcie->bpmp, &msg);
1227 }
1228 
1229 static int tegra_pcie_bpmp_set_pll_state(struct tegra_pcie_dw *pcie,
1230                      bool enable)
1231 {
1232     struct mrq_uphy_response resp;
1233     struct tegra_bpmp_message msg;
1234     struct mrq_uphy_request req;
1235 
1236     memset(&req, 0, sizeof(req));
1237     memset(&resp, 0, sizeof(resp));
1238 
1239     if (enable) {
1240         req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_INIT;
1241         req.ep_ctrlr_pll_init.ep_controller = pcie->cid;
1242     } else {
1243         req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_OFF;
1244         req.ep_ctrlr_pll_off.ep_controller = pcie->cid;
1245     }
1246 
1247     memset(&msg, 0, sizeof(msg));
1248     msg.mrq = MRQ_UPHY;
1249     msg.tx.data = &req;
1250     msg.tx.size = sizeof(req);
1251     msg.rx.data = &resp;
1252     msg.rx.size = sizeof(resp);
1253 
1254     return tegra_bpmp_transfer(pcie->bpmp, &msg);
1255 }
1256 
1257 static void tegra_pcie_downstream_dev_to_D0(struct tegra_pcie_dw *pcie)
1258 {
1259     struct dw_pcie_rp *pp = &pcie->pci.pp;
1260     struct pci_bus *child, *root_bus = NULL;
1261     struct pci_dev *pdev;
1262 
1263     /*
1264      * link doesn't go into L2 state with some of the endpoints with Tegra
1265      * if they are not in D0 state. So, need to make sure that immediate
1266      * downstream devices are in D0 state before sending PME_TurnOff to put
1267      * link into L2 state.
1268      * This is as per PCI Express Base r4.0 v1.0 September 27-2017,
1269      * 5.2 Link State Power Management (Page #428).
1270      */
1271 
1272     list_for_each_entry(child, &pp->bridge->bus->children, node) {
1273         /* Bring downstream devices to D0 if they are not already in */
1274         if (child->parent == pp->bridge->bus) {
1275             root_bus = child;
1276             break;
1277         }
1278     }
1279 
1280     if (!root_bus) {
1281         dev_err(pcie->dev, "Failed to find downstream devices\n");
1282         return;
1283     }
1284 
1285     list_for_each_entry(pdev, &root_bus->devices, bus_list) {
1286         if (PCI_SLOT(pdev->devfn) == 0) {
1287             if (pci_set_power_state(pdev, PCI_D0))
1288                 dev_err(pcie->dev,
1289                     "Failed to transition %s to D0 state\n",
1290                     dev_name(&pdev->dev));
1291         }
1292     }
1293 }
1294 
1295 static int tegra_pcie_get_slot_regulators(struct tegra_pcie_dw *pcie)
1296 {
1297     pcie->slot_ctl_3v3 = devm_regulator_get_optional(pcie->dev, "vpcie3v3");
1298     if (IS_ERR(pcie->slot_ctl_3v3)) {
1299         if (PTR_ERR(pcie->slot_ctl_3v3) != -ENODEV)
1300             return PTR_ERR(pcie->slot_ctl_3v3);
1301 
1302         pcie->slot_ctl_3v3 = NULL;
1303     }
1304 
1305     pcie->slot_ctl_12v = devm_regulator_get_optional(pcie->dev, "vpcie12v");
1306     if (IS_ERR(pcie->slot_ctl_12v)) {
1307         if (PTR_ERR(pcie->slot_ctl_12v) != -ENODEV)
1308             return PTR_ERR(pcie->slot_ctl_12v);
1309 
1310         pcie->slot_ctl_12v = NULL;
1311     }
1312 
1313     return 0;
1314 }
1315 
1316 static int tegra_pcie_enable_slot_regulators(struct tegra_pcie_dw *pcie)
1317 {
1318     int ret;
1319 
1320     if (pcie->slot_ctl_3v3) {
1321         ret = regulator_enable(pcie->slot_ctl_3v3);
1322         if (ret < 0) {
1323             dev_err(pcie->dev,
1324                 "Failed to enable 3.3V slot supply: %d\n", ret);
1325             return ret;
1326         }
1327     }
1328 
1329     if (pcie->slot_ctl_12v) {
1330         ret = regulator_enable(pcie->slot_ctl_12v);
1331         if (ret < 0) {
1332             dev_err(pcie->dev,
1333                 "Failed to enable 12V slot supply: %d\n", ret);
1334             goto fail_12v_enable;
1335         }
1336     }
1337 
1338     /*
1339      * According to PCI Express Card Electromechanical Specification
1340      * Revision 1.1, Table-2.4, T_PVPERL (Power stable to PERST# inactive)
1341      * should be a minimum of 100ms.
1342      */
1343     if (pcie->slot_ctl_3v3 || pcie->slot_ctl_12v)
1344         msleep(100);
1345 
1346     return 0;
1347 
1348 fail_12v_enable:
1349     if (pcie->slot_ctl_3v3)
1350         regulator_disable(pcie->slot_ctl_3v3);
1351     return ret;
1352 }
1353 
1354 static void tegra_pcie_disable_slot_regulators(struct tegra_pcie_dw *pcie)
1355 {
1356     if (pcie->slot_ctl_12v)
1357         regulator_disable(pcie->slot_ctl_12v);
1358     if (pcie->slot_ctl_3v3)
1359         regulator_disable(pcie->slot_ctl_3v3);
1360 }
1361 
1362 static int tegra_pcie_config_controller(struct tegra_pcie_dw *pcie,
1363                     bool en_hw_hot_rst)
1364 {
1365     int ret;
1366     u32 val;
1367 
1368     ret = tegra_pcie_bpmp_set_ctrl_state(pcie, true);
1369     if (ret) {
1370         dev_err(pcie->dev,
1371             "Failed to enable controller %u: %d\n", pcie->cid, ret);
1372         return ret;
1373     }
1374 
1375     if (pcie->enable_ext_refclk) {
1376         ret = tegra_pcie_bpmp_set_pll_state(pcie, true);
1377         if (ret) {
1378             dev_err(pcie->dev, "Failed to init UPHY: %d\n", ret);
1379             goto fail_pll_init;
1380         }
1381     }
1382 
1383     ret = tegra_pcie_enable_slot_regulators(pcie);
1384     if (ret < 0)
1385         goto fail_slot_reg_en;
1386 
1387     ret = regulator_enable(pcie->pex_ctl_supply);
1388     if (ret < 0) {
1389         dev_err(pcie->dev, "Failed to enable regulator: %d\n", ret);
1390         goto fail_reg_en;
1391     }
1392 
1393     ret = clk_prepare_enable(pcie->core_clk);
1394     if (ret) {
1395         dev_err(pcie->dev, "Failed to enable core clock: %d\n", ret);
1396         goto fail_core_clk;
1397     }
1398 
1399     ret = reset_control_deassert(pcie->core_apb_rst);
1400     if (ret) {
1401         dev_err(pcie->dev, "Failed to deassert core APB reset: %d\n",
1402             ret);
1403         goto fail_core_apb_rst;
1404     }
1405 
1406     if (en_hw_hot_rst || pcie->of_data->has_sbr_reset_fix) {
1407         /* Enable HW_HOT_RST mode */
1408         val = appl_readl(pcie, APPL_CTRL);
1409         val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
1410              APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
1411         val |= (APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST_LTSSM_EN <<
1412             APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
1413         val |= APPL_CTRL_HW_HOT_RST_EN;
1414         appl_writel(pcie, val, APPL_CTRL);
1415     }
1416 
1417     ret = tegra_pcie_enable_phy(pcie);
1418     if (ret) {
1419         dev_err(pcie->dev, "Failed to enable PHY: %d\n", ret);
1420         goto fail_phy;
1421     }
1422 
1423     /* Update CFG base address */
1424     appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK,
1425             APPL_CFG_BASE_ADDR);
1426 
1427     /* Configure this core for RP mode operation */
1428     appl_writel(pcie, APPL_DM_TYPE_RP, APPL_DM_TYPE);
1429 
1430     appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE);
1431 
1432     val = appl_readl(pcie, APPL_CTRL);
1433     appl_writel(pcie, val | APPL_CTRL_SYS_PRE_DET_STATE, APPL_CTRL);
1434 
1435     val = appl_readl(pcie, APPL_CFG_MISC);
1436     val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT);
1437     appl_writel(pcie, val, APPL_CFG_MISC);
1438 
1439     if (pcie->enable_srns || pcie->enable_ext_refclk) {
1440         /*
1441          * When Tegra PCIe RP is using external clock, it cannot supply
1442          * same clock to its downstream hierarchy. Hence, gate PCIe RP
1443          * REFCLK out pads when RP & EP are using separate clocks or RP
1444          * is using an external REFCLK.
1445          */
1446         val = appl_readl(pcie, APPL_PINMUX);
1447         val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1448         val &= ~APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1449         appl_writel(pcie, val, APPL_PINMUX);
1450     }
1451 
1452     if (!pcie->supports_clkreq) {
1453         val = appl_readl(pcie, APPL_PINMUX);
1454         val |= APPL_PINMUX_CLKREQ_OVERRIDE_EN;
1455         val &= ~APPL_PINMUX_CLKREQ_OVERRIDE;
1456         appl_writel(pcie, val, APPL_PINMUX);
1457     }
1458 
1459     /* Update iATU_DMA base address */
1460     appl_writel(pcie,
1461             pcie->atu_dma_res->start & APPL_CFG_IATU_DMA_BASE_ADDR_MASK,
1462             APPL_CFG_IATU_DMA_BASE_ADDR);
1463 
1464     reset_control_deassert(pcie->core_rst);
1465 
1466     return ret;
1467 
1468 fail_phy:
1469     reset_control_assert(pcie->core_apb_rst);
1470 fail_core_apb_rst:
1471     clk_disable_unprepare(pcie->core_clk);
1472 fail_core_clk:
1473     regulator_disable(pcie->pex_ctl_supply);
1474 fail_reg_en:
1475     tegra_pcie_disable_slot_regulators(pcie);
1476 fail_slot_reg_en:
1477     if (pcie->enable_ext_refclk)
1478         tegra_pcie_bpmp_set_pll_state(pcie, false);
1479 fail_pll_init:
1480     tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1481 
1482     return ret;
1483 }
1484 
1485 static void tegra_pcie_unconfig_controller(struct tegra_pcie_dw *pcie)
1486 {
1487     int ret;
1488 
1489     ret = reset_control_assert(pcie->core_rst);
1490     if (ret)
1491         dev_err(pcie->dev, "Failed to assert \"core\" reset: %d\n", ret);
1492 
1493     tegra_pcie_disable_phy(pcie);
1494 
1495     ret = reset_control_assert(pcie->core_apb_rst);
1496     if (ret)
1497         dev_err(pcie->dev, "Failed to assert APB reset: %d\n", ret);
1498 
1499     clk_disable_unprepare(pcie->core_clk);
1500 
1501     ret = regulator_disable(pcie->pex_ctl_supply);
1502     if (ret)
1503         dev_err(pcie->dev, "Failed to disable regulator: %d\n", ret);
1504 
1505     tegra_pcie_disable_slot_regulators(pcie);
1506 
1507     if (pcie->enable_ext_refclk) {
1508         ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1509         if (ret)
1510             dev_err(pcie->dev, "Failed to deinit UPHY: %d\n", ret);
1511     }
1512 
1513     ret = tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1514     if (ret)
1515         dev_err(pcie->dev, "Failed to disable controller %d: %d\n",
1516             pcie->cid, ret);
1517 }
1518 
1519 static int tegra_pcie_init_controller(struct tegra_pcie_dw *pcie)
1520 {
1521     struct dw_pcie *pci = &pcie->pci;
1522     struct dw_pcie_rp *pp = &pci->pp;
1523     int ret;
1524 
1525     ret = tegra_pcie_config_controller(pcie, false);
1526     if (ret < 0)
1527         return ret;
1528 
1529     pp->ops = &tegra_pcie_dw_host_ops;
1530 
1531     ret = dw_pcie_host_init(pp);
1532     if (ret < 0) {
1533         dev_err(pcie->dev, "Failed to add PCIe port: %d\n", ret);
1534         goto fail_host_init;
1535     }
1536 
1537     return 0;
1538 
1539 fail_host_init:
1540     tegra_pcie_unconfig_controller(pcie);
1541     return ret;
1542 }
1543 
1544 static int tegra_pcie_try_link_l2(struct tegra_pcie_dw *pcie)
1545 {
1546     u32 val;
1547 
1548     if (!tegra_pcie_dw_link_up(&pcie->pci))
1549         return 0;
1550 
1551     val = appl_readl(pcie, APPL_RADM_STATUS);
1552     val |= APPL_PM_XMT_TURNOFF_STATE;
1553     appl_writel(pcie, val, APPL_RADM_STATUS);
1554 
1555     return readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG, val,
1556                  val & APPL_DEBUG_PM_LINKST_IN_L2_LAT,
1557                  1, PME_ACK_TIMEOUT);
1558 }
1559 
1560 static void tegra_pcie_dw_pme_turnoff(struct tegra_pcie_dw *pcie)
1561 {
1562     u32 data;
1563     int err;
1564 
1565     if (!tegra_pcie_dw_link_up(&pcie->pci)) {
1566         dev_dbg(pcie->dev, "PCIe link is not up...!\n");
1567         return;
1568     }
1569 
1570     /*
1571      * PCIe controller exits from L2 only if reset is applied, so
1572      * controller doesn't handle interrupts. But in cases where
1573      * L2 entry fails, PERST# is asserted which can trigger surprise
1574      * link down AER. However this function call happens in
1575      * suspend_noirq(), so AER interrupt will not be processed.
1576      * Disable all interrupts to avoid such a scenario.
1577      */
1578     appl_writel(pcie, 0x0, APPL_INTR_EN_L0_0);
1579 
1580     if (tegra_pcie_try_link_l2(pcie)) {
1581         dev_info(pcie->dev, "Link didn't transition to L2 state\n");
1582         /*
1583          * TX lane clock freq will reset to Gen1 only if link is in L2
1584          * or detect state.
1585          * So apply pex_rst to end point to force RP to go into detect
1586          * state
1587          */
1588         data = appl_readl(pcie, APPL_PINMUX);
1589         data &= ~APPL_PINMUX_PEX_RST;
1590         appl_writel(pcie, data, APPL_PINMUX);
1591 
1592         /*
1593          * Some cards do not go to detect state even after de-asserting
1594          * PERST#. So, de-assert LTSSM to bring link to detect state.
1595          */
1596         data = readl(pcie->appl_base + APPL_CTRL);
1597         data &= ~APPL_CTRL_LTSSM_EN;
1598         writel(data, pcie->appl_base + APPL_CTRL);
1599 
1600         err = readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG,
1601                         data,
1602                         ((data &
1603                         APPL_DEBUG_LTSSM_STATE_MASK) >>
1604                         APPL_DEBUG_LTSSM_STATE_SHIFT) ==
1605                         LTSSM_STATE_PRE_DETECT,
1606                         1, LTSSM_TIMEOUT);
1607         if (err)
1608             dev_info(pcie->dev, "Link didn't go to detect state\n");
1609     }
1610     /*
1611      * DBI registers may not be accessible after this as PLL-E would be
1612      * down depending on how CLKREQ is pulled by end point
1613      */
1614     data = appl_readl(pcie, APPL_PINMUX);
1615     data |= (APPL_PINMUX_CLKREQ_OVERRIDE_EN | APPL_PINMUX_CLKREQ_OVERRIDE);
1616     /* Cut REFCLK to slot */
1617     data |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1618     data &= ~APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1619     appl_writel(pcie, data, APPL_PINMUX);
1620 }
1621 
1622 static void tegra_pcie_deinit_controller(struct tegra_pcie_dw *pcie)
1623 {
1624     tegra_pcie_downstream_dev_to_D0(pcie);
1625     dw_pcie_host_deinit(&pcie->pci.pp);
1626     tegra_pcie_dw_pme_turnoff(pcie);
1627     tegra_pcie_unconfig_controller(pcie);
1628 }
1629 
1630 static int tegra_pcie_config_rp(struct tegra_pcie_dw *pcie)
1631 {
1632     struct device *dev = pcie->dev;
1633     char *name;
1634     int ret;
1635 
1636     pm_runtime_enable(dev);
1637 
1638     ret = pm_runtime_get_sync(dev);
1639     if (ret < 0) {
1640         dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n",
1641             ret);
1642         goto fail_pm_get_sync;
1643     }
1644 
1645     ret = pinctrl_pm_select_default_state(dev);
1646     if (ret < 0) {
1647         dev_err(dev, "Failed to configure sideband pins: %d\n", ret);
1648         goto fail_pm_get_sync;
1649     }
1650 
1651     ret = tegra_pcie_init_controller(pcie);
1652     if (ret < 0) {
1653         dev_err(dev, "Failed to initialize controller: %d\n", ret);
1654         goto fail_pm_get_sync;
1655     }
1656 
1657     pcie->link_state = tegra_pcie_dw_link_up(&pcie->pci);
1658     if (!pcie->link_state) {
1659         ret = -ENOMEDIUM;
1660         goto fail_host_init;
1661     }
1662 
1663     name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
1664     if (!name) {
1665         ret = -ENOMEM;
1666         goto fail_host_init;
1667     }
1668 
1669     pcie->debugfs = debugfs_create_dir(name, NULL);
1670     init_debugfs(pcie);
1671 
1672     return ret;
1673 
1674 fail_host_init:
1675     tegra_pcie_deinit_controller(pcie);
1676 fail_pm_get_sync:
1677     pm_runtime_put_sync(dev);
1678     pm_runtime_disable(dev);
1679     return ret;
1680 }
1681 
1682 static void pex_ep_event_pex_rst_assert(struct tegra_pcie_dw *pcie)
1683 {
1684     u32 val;
1685     int ret;
1686 
1687     if (pcie->ep_state == EP_STATE_DISABLED)
1688         return;
1689 
1690     /* Disable LTSSM */
1691     val = appl_readl(pcie, APPL_CTRL);
1692     val &= ~APPL_CTRL_LTSSM_EN;
1693     appl_writel(pcie, val, APPL_CTRL);
1694 
1695     ret = readl_poll_timeout(pcie->appl_base + APPL_DEBUG, val,
1696                  ((val & APPL_DEBUG_LTSSM_STATE_MASK) >>
1697                  APPL_DEBUG_LTSSM_STATE_SHIFT) ==
1698                  LTSSM_STATE_PRE_DETECT,
1699                  1, LTSSM_TIMEOUT);
1700     if (ret)
1701         dev_err(pcie->dev, "Failed to go Detect state: %d\n", ret);
1702 
1703     reset_control_assert(pcie->core_rst);
1704 
1705     tegra_pcie_disable_phy(pcie);
1706 
1707     reset_control_assert(pcie->core_apb_rst);
1708 
1709     clk_disable_unprepare(pcie->core_clk);
1710 
1711     pm_runtime_put_sync(pcie->dev);
1712 
1713     if (pcie->enable_ext_refclk) {
1714         ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1715         if (ret)
1716             dev_err(pcie->dev, "Failed to turn off UPHY: %d\n",
1717                 ret);
1718     }
1719 
1720     ret = tegra_pcie_bpmp_set_pll_state(pcie, false);
1721     if (ret)
1722         dev_err(pcie->dev, "Failed to turn off UPHY: %d\n", ret);
1723 
1724     pcie->ep_state = EP_STATE_DISABLED;
1725     dev_dbg(pcie->dev, "Uninitialization of endpoint is completed\n");
1726 }
1727 
1728 static void pex_ep_event_pex_rst_deassert(struct tegra_pcie_dw *pcie)
1729 {
1730     struct dw_pcie *pci = &pcie->pci;
1731     struct dw_pcie_ep *ep = &pci->ep;
1732     struct device *dev = pcie->dev;
1733     u32 val;
1734     int ret;
1735     u16 val_16;
1736 
1737     if (pcie->ep_state == EP_STATE_ENABLED)
1738         return;
1739 
1740     ret = pm_runtime_resume_and_get(dev);
1741     if (ret < 0) {
1742         dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n",
1743             ret);
1744         return;
1745     }
1746 
1747     ret = tegra_pcie_bpmp_set_ctrl_state(pcie, true);
1748     if (ret) {
1749         dev_err(pcie->dev, "Failed to enable controller %u: %d\n",
1750             pcie->cid, ret);
1751         goto fail_set_ctrl_state;
1752     }
1753 
1754     if (pcie->enable_ext_refclk) {
1755         ret = tegra_pcie_bpmp_set_pll_state(pcie, true);
1756         if (ret) {
1757             dev_err(dev, "Failed to init UPHY for PCIe EP: %d\n",
1758                 ret);
1759             goto fail_pll_init;
1760         }
1761     }
1762 
1763     ret = clk_prepare_enable(pcie->core_clk);
1764     if (ret) {
1765         dev_err(dev, "Failed to enable core clock: %d\n", ret);
1766         goto fail_core_clk_enable;
1767     }
1768 
1769     ret = reset_control_deassert(pcie->core_apb_rst);
1770     if (ret) {
1771         dev_err(dev, "Failed to deassert core APB reset: %d\n", ret);
1772         goto fail_core_apb_rst;
1773     }
1774 
1775     ret = tegra_pcie_enable_phy(pcie);
1776     if (ret) {
1777         dev_err(dev, "Failed to enable PHY: %d\n", ret);
1778         goto fail_phy;
1779     }
1780 
1781     /* Clear any stale interrupt statuses */
1782     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0);
1783     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0);
1784     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1);
1785     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2);
1786     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3);
1787     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6);
1788     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7);
1789     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0);
1790     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9);
1791     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10);
1792     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11);
1793     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13);
1794     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14);
1795     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15);
1796     appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17);
1797 
1798     /* configure this core for EP mode operation */
1799     val = appl_readl(pcie, APPL_DM_TYPE);
1800     val &= ~APPL_DM_TYPE_MASK;
1801     val |= APPL_DM_TYPE_EP;
1802     appl_writel(pcie, val, APPL_DM_TYPE);
1803 
1804     appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE);
1805 
1806     val = appl_readl(pcie, APPL_CTRL);
1807     val |= APPL_CTRL_SYS_PRE_DET_STATE;
1808     val |= APPL_CTRL_HW_HOT_RST_EN;
1809     appl_writel(pcie, val, APPL_CTRL);
1810 
1811     val = appl_readl(pcie, APPL_CFG_MISC);
1812     val |= APPL_CFG_MISC_SLV_EP_MODE;
1813     val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT);
1814     appl_writel(pcie, val, APPL_CFG_MISC);
1815 
1816     val = appl_readl(pcie, APPL_PINMUX);
1817     val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN;
1818     val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE;
1819     appl_writel(pcie, val, APPL_PINMUX);
1820 
1821     appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK,
1822             APPL_CFG_BASE_ADDR);
1823 
1824     appl_writel(pcie, pcie->atu_dma_res->start &
1825             APPL_CFG_IATU_DMA_BASE_ADDR_MASK,
1826             APPL_CFG_IATU_DMA_BASE_ADDR);
1827 
1828     val = appl_readl(pcie, APPL_INTR_EN_L0_0);
1829     val |= APPL_INTR_EN_L0_0_SYS_INTR_EN;
1830     val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN;
1831     val |= APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN;
1832     appl_writel(pcie, val, APPL_INTR_EN_L0_0);
1833 
1834     val = appl_readl(pcie, APPL_INTR_EN_L1_0_0);
1835     val |= APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN;
1836     val |= APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN;
1837     appl_writel(pcie, val, APPL_INTR_EN_L1_0_0);
1838 
1839     reset_control_deassert(pcie->core_rst);
1840 
1841     if (pcie->update_fc_fixup) {
1842         val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF);
1843         val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT;
1844         dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val);
1845     }
1846 
1847     config_gen3_gen4_eq_presets(pcie);
1848 
1849     init_host_aspm(pcie);
1850 
1851     /* Disable ASPM-L1SS advertisement if there is no CLKREQ routing */
1852     if (!pcie->supports_clkreq) {
1853         disable_aspm_l11(pcie);
1854         disable_aspm_l12(pcie);
1855     }
1856 
1857     if (!pcie->of_data->has_l1ss_exit_fix) {
1858         val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
1859         val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL;
1860         dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
1861     }
1862 
1863     pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci,
1864                               PCI_CAP_ID_EXP);
1865 
1866     val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL);
1867     val_16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
1868     val_16 |= PCI_EXP_DEVCTL_PAYLOAD_256B;
1869     dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_DEVCTL, val_16);
1870 
1871     /* Clear Slot Clock Configuration bit if SRNS configuration */
1872     if (pcie->enable_srns) {
1873         val_16 = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base +
1874                        PCI_EXP_LNKSTA);
1875         val_16 &= ~PCI_EXP_LNKSTA_SLC;
1876         dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA,
1877                    val_16);
1878     }
1879 
1880     clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ);
1881 
1882     val = (ep->msi_mem_phys & MSIX_ADDR_MATCH_LOW_OFF_MASK);
1883     val |= MSIX_ADDR_MATCH_LOW_OFF_EN;
1884     dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_LOW_OFF, val);
1885     val = (upper_32_bits(ep->msi_mem_phys) & MSIX_ADDR_MATCH_HIGH_OFF_MASK);
1886     dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_HIGH_OFF, val);
1887 
1888     ret = dw_pcie_ep_init_complete(ep);
1889     if (ret) {
1890         dev_err(dev, "Failed to complete initialization: %d\n", ret);
1891         goto fail_init_complete;
1892     }
1893 
1894     dw_pcie_ep_init_notify(ep);
1895 
1896     /* Program the private control to allow sending LTR upstream */
1897     if (pcie->of_data->has_ltr_req_fix) {
1898         val = appl_readl(pcie, APPL_LTR_MSG_2);
1899         val |= APPL_LTR_MSG_2_LTR_MSG_REQ_STATE;
1900         appl_writel(pcie, val, APPL_LTR_MSG_2);
1901     }
1902 
1903     /* Enable LTSSM */
1904     val = appl_readl(pcie, APPL_CTRL);
1905     val |= APPL_CTRL_LTSSM_EN;
1906     appl_writel(pcie, val, APPL_CTRL);
1907 
1908     pcie->ep_state = EP_STATE_ENABLED;
1909     dev_dbg(dev, "Initialization of endpoint is completed\n");
1910 
1911     return;
1912 
1913 fail_init_complete:
1914     reset_control_assert(pcie->core_rst);
1915     tegra_pcie_disable_phy(pcie);
1916 fail_phy:
1917     reset_control_assert(pcie->core_apb_rst);
1918 fail_core_apb_rst:
1919     clk_disable_unprepare(pcie->core_clk);
1920 fail_core_clk_enable:
1921     tegra_pcie_bpmp_set_pll_state(pcie, false);
1922 fail_pll_init:
1923     tegra_pcie_bpmp_set_ctrl_state(pcie, false);
1924 fail_set_ctrl_state:
1925     pm_runtime_put_sync(dev);
1926 }
1927 
1928 static irqreturn_t tegra_pcie_ep_pex_rst_irq(int irq, void *arg)
1929 {
1930     struct tegra_pcie_dw *pcie = arg;
1931 
1932     if (gpiod_get_value(pcie->pex_rst_gpiod))
1933         pex_ep_event_pex_rst_assert(pcie);
1934     else
1935         pex_ep_event_pex_rst_deassert(pcie);
1936 
1937     return IRQ_HANDLED;
1938 }
1939 
1940 static int tegra_pcie_ep_raise_legacy_irq(struct tegra_pcie_dw *pcie, u16 irq)
1941 {
1942     /* Tegra194 supports only INTA */
1943     if (irq > 1)
1944         return -EINVAL;
1945 
1946     appl_writel(pcie, 1, APPL_LEGACY_INTX);
1947     usleep_range(1000, 2000);
1948     appl_writel(pcie, 0, APPL_LEGACY_INTX);
1949     return 0;
1950 }
1951 
1952 static int tegra_pcie_ep_raise_msi_irq(struct tegra_pcie_dw *pcie, u16 irq)
1953 {
1954     if (unlikely(irq > 31))
1955         return -EINVAL;
1956 
1957     appl_writel(pcie, BIT(irq), APPL_MSI_CTRL_1);
1958 
1959     return 0;
1960 }
1961 
1962 static int tegra_pcie_ep_raise_msix_irq(struct tegra_pcie_dw *pcie, u16 irq)
1963 {
1964     struct dw_pcie_ep *ep = &pcie->pci.ep;
1965 
1966     writel(irq, ep->msi_mem);
1967 
1968     return 0;
1969 }
1970 
1971 static int tegra_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
1972                    enum pci_epc_irq_type type,
1973                    u16 interrupt_num)
1974 {
1975     struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1976     struct tegra_pcie_dw *pcie = to_tegra_pcie(pci);
1977 
1978     switch (type) {
1979     case PCI_EPC_IRQ_LEGACY:
1980         return tegra_pcie_ep_raise_legacy_irq(pcie, interrupt_num);
1981 
1982     case PCI_EPC_IRQ_MSI:
1983         return tegra_pcie_ep_raise_msi_irq(pcie, interrupt_num);
1984 
1985     case PCI_EPC_IRQ_MSIX:
1986         return tegra_pcie_ep_raise_msix_irq(pcie, interrupt_num);
1987 
1988     default:
1989         dev_err(pci->dev, "Unknown IRQ type\n");
1990         return -EPERM;
1991     }
1992 
1993     return 0;
1994 }
1995 
1996 static const struct pci_epc_features tegra_pcie_epc_features = {
1997     .linkup_notifier = true,
1998     .core_init_notifier = true,
1999     .msi_capable = false,
2000     .msix_capable = false,
2001     .reserved_bar = 1 << BAR_2 | 1 << BAR_3 | 1 << BAR_4 | 1 << BAR_5,
2002     .bar_fixed_64bit = 1 << BAR_0,
2003     .bar_fixed_size[0] = SZ_1M,
2004 };
2005 
2006 static const struct pci_epc_features*
2007 tegra_pcie_ep_get_features(struct dw_pcie_ep *ep)
2008 {
2009     return &tegra_pcie_epc_features;
2010 }
2011 
2012 static const struct dw_pcie_ep_ops pcie_ep_ops = {
2013     .raise_irq = tegra_pcie_ep_raise_irq,
2014     .get_features = tegra_pcie_ep_get_features,
2015 };
2016 
2017 static int tegra_pcie_config_ep(struct tegra_pcie_dw *pcie,
2018                 struct platform_device *pdev)
2019 {
2020     struct dw_pcie *pci = &pcie->pci;
2021     struct device *dev = pcie->dev;
2022     struct dw_pcie_ep *ep;
2023     char *name;
2024     int ret;
2025 
2026     ep = &pci->ep;
2027     ep->ops = &pcie_ep_ops;
2028 
2029     ep->page_size = SZ_64K;
2030 
2031     ret = gpiod_set_debounce(pcie->pex_rst_gpiod, PERST_DEBOUNCE_TIME);
2032     if (ret < 0) {
2033         dev_err(dev, "Failed to set PERST GPIO debounce time: %d\n",
2034             ret);
2035         return ret;
2036     }
2037 
2038     ret = gpiod_to_irq(pcie->pex_rst_gpiod);
2039     if (ret < 0) {
2040         dev_err(dev, "Failed to get IRQ for PERST GPIO: %d\n", ret);
2041         return ret;
2042     }
2043     pcie->pex_rst_irq = (unsigned int)ret;
2044 
2045     name = devm_kasprintf(dev, GFP_KERNEL, "tegra_pcie_%u_pex_rst_irq",
2046                   pcie->cid);
2047     if (!name) {
2048         dev_err(dev, "Failed to create PERST IRQ string\n");
2049         return -ENOMEM;
2050     }
2051 
2052     irq_set_status_flags(pcie->pex_rst_irq, IRQ_NOAUTOEN);
2053 
2054     pcie->ep_state = EP_STATE_DISABLED;
2055 
2056     ret = devm_request_threaded_irq(dev, pcie->pex_rst_irq, NULL,
2057                     tegra_pcie_ep_pex_rst_irq,
2058                     IRQF_TRIGGER_RISING |
2059                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2060                     name, (void *)pcie);
2061     if (ret < 0) {
2062         dev_err(dev, "Failed to request IRQ for PERST: %d\n", ret);
2063         return ret;
2064     }
2065 
2066     pm_runtime_enable(dev);
2067 
2068     ret = dw_pcie_ep_init(ep);
2069     if (ret) {
2070         dev_err(dev, "Failed to initialize DWC Endpoint subsystem: %d\n",
2071             ret);
2072         pm_runtime_disable(dev);
2073         return ret;
2074     }
2075 
2076     return 0;
2077 }
2078 
2079 static int tegra_pcie_dw_probe(struct platform_device *pdev)
2080 {
2081     const struct tegra_pcie_dw_of_data *data;
2082     struct device *dev = &pdev->dev;
2083     struct resource *atu_dma_res;
2084     struct tegra_pcie_dw *pcie;
2085     struct dw_pcie_rp *pp;
2086     struct dw_pcie *pci;
2087     struct phy **phys;
2088     char *name;
2089     int ret;
2090     u32 i;
2091 
2092     data = of_device_get_match_data(dev);
2093 
2094     pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
2095     if (!pcie)
2096         return -ENOMEM;
2097 
2098     pci = &pcie->pci;
2099     pci->dev = &pdev->dev;
2100     pci->ops = &tegra_dw_pcie_ops;
2101     pcie->dev = &pdev->dev;
2102     pcie->of_data = (struct tegra_pcie_dw_of_data *)data;
2103     pci->n_fts[0] = pcie->of_data->n_fts[0];
2104     pci->n_fts[1] = pcie->of_data->n_fts[1];
2105     pp = &pci->pp;
2106     pp->num_vectors = MAX_MSI_IRQS;
2107 
2108     ret = tegra_pcie_dw_parse_dt(pcie);
2109     if (ret < 0) {
2110         const char *level = KERN_ERR;
2111 
2112         if (ret == -EPROBE_DEFER)
2113             level = KERN_DEBUG;
2114 
2115         dev_printk(level, dev,
2116                dev_fmt("Failed to parse device tree: %d\n"),
2117                ret);
2118         return ret;
2119     }
2120 
2121     ret = tegra_pcie_get_slot_regulators(pcie);
2122     if (ret < 0) {
2123         const char *level = KERN_ERR;
2124 
2125         if (ret == -EPROBE_DEFER)
2126             level = KERN_DEBUG;
2127 
2128         dev_printk(level, dev,
2129                dev_fmt("Failed to get slot regulators: %d\n"),
2130                ret);
2131         return ret;
2132     }
2133 
2134     if (pcie->pex_refclk_sel_gpiod)
2135         gpiod_set_value(pcie->pex_refclk_sel_gpiod, 1);
2136 
2137     pcie->pex_ctl_supply = devm_regulator_get(dev, "vddio-pex-ctl");
2138     if (IS_ERR(pcie->pex_ctl_supply)) {
2139         ret = PTR_ERR(pcie->pex_ctl_supply);
2140         if (ret != -EPROBE_DEFER)
2141             dev_err(dev, "Failed to get regulator: %ld\n",
2142                 PTR_ERR(pcie->pex_ctl_supply));
2143         return ret;
2144     }
2145 
2146     pcie->core_clk = devm_clk_get(dev, "core");
2147     if (IS_ERR(pcie->core_clk)) {
2148         dev_err(dev, "Failed to get core clock: %ld\n",
2149             PTR_ERR(pcie->core_clk));
2150         return PTR_ERR(pcie->core_clk);
2151     }
2152 
2153     pcie->appl_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2154                               "appl");
2155     if (!pcie->appl_res) {
2156         dev_err(dev, "Failed to find \"appl\" region\n");
2157         return -ENODEV;
2158     }
2159 
2160     pcie->appl_base = devm_ioremap_resource(dev, pcie->appl_res);
2161     if (IS_ERR(pcie->appl_base))
2162         return PTR_ERR(pcie->appl_base);
2163 
2164     pcie->core_apb_rst = devm_reset_control_get(dev, "apb");
2165     if (IS_ERR(pcie->core_apb_rst)) {
2166         dev_err(dev, "Failed to get APB reset: %ld\n",
2167             PTR_ERR(pcie->core_apb_rst));
2168         return PTR_ERR(pcie->core_apb_rst);
2169     }
2170 
2171     phys = devm_kcalloc(dev, pcie->phy_count, sizeof(*phys), GFP_KERNEL);
2172     if (!phys)
2173         return -ENOMEM;
2174 
2175     for (i = 0; i < pcie->phy_count; i++) {
2176         name = kasprintf(GFP_KERNEL, "p2u-%u", i);
2177         if (!name) {
2178             dev_err(dev, "Failed to create P2U string\n");
2179             return -ENOMEM;
2180         }
2181         phys[i] = devm_phy_get(dev, name);
2182         kfree(name);
2183         if (IS_ERR(phys[i])) {
2184             ret = PTR_ERR(phys[i]);
2185             if (ret != -EPROBE_DEFER)
2186                 dev_err(dev, "Failed to get PHY: %d\n", ret);
2187             return ret;
2188         }
2189     }
2190 
2191     pcie->phys = phys;
2192 
2193     atu_dma_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2194                            "atu_dma");
2195     if (!atu_dma_res) {
2196         dev_err(dev, "Failed to find \"atu_dma\" region\n");
2197         return -ENODEV;
2198     }
2199     pcie->atu_dma_res = atu_dma_res;
2200 
2201     pci->atu_size = resource_size(atu_dma_res);
2202     pci->atu_base = devm_ioremap_resource(dev, atu_dma_res);
2203     if (IS_ERR(pci->atu_base))
2204         return PTR_ERR(pci->atu_base);
2205 
2206     pcie->core_rst = devm_reset_control_get(dev, "core");
2207     if (IS_ERR(pcie->core_rst)) {
2208         dev_err(dev, "Failed to get core reset: %ld\n",
2209             PTR_ERR(pcie->core_rst));
2210         return PTR_ERR(pcie->core_rst);
2211     }
2212 
2213     pp->irq = platform_get_irq_byname(pdev, "intr");
2214     if (pp->irq < 0)
2215         return pp->irq;
2216 
2217     pcie->bpmp = tegra_bpmp_get(dev);
2218     if (IS_ERR(pcie->bpmp))
2219         return PTR_ERR(pcie->bpmp);
2220 
2221     platform_set_drvdata(pdev, pcie);
2222 
2223     switch (pcie->of_data->mode) {
2224     case DW_PCIE_RC_TYPE:
2225         ret = devm_request_irq(dev, pp->irq, tegra_pcie_rp_irq_handler,
2226                        IRQF_SHARED, "tegra-pcie-intr", pcie);
2227         if (ret) {
2228             dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq,
2229                 ret);
2230             goto fail;
2231         }
2232 
2233         ret = tegra_pcie_config_rp(pcie);
2234         if (ret && ret != -ENOMEDIUM)
2235             goto fail;
2236         else
2237             return 0;
2238         break;
2239 
2240     case DW_PCIE_EP_TYPE:
2241         ret = devm_request_threaded_irq(dev, pp->irq,
2242                         tegra_pcie_ep_hard_irq,
2243                         tegra_pcie_ep_irq_thread,
2244                         IRQF_SHARED | IRQF_ONESHOT,
2245                         "tegra-pcie-ep-intr", pcie);
2246         if (ret) {
2247             dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq,
2248                 ret);
2249             goto fail;
2250         }
2251 
2252         ret = tegra_pcie_config_ep(pcie, pdev);
2253         if (ret < 0)
2254             goto fail;
2255         break;
2256 
2257     default:
2258         dev_err(dev, "Invalid PCIe device type %d\n",
2259             pcie->of_data->mode);
2260     }
2261 
2262 fail:
2263     tegra_bpmp_put(pcie->bpmp);
2264     return ret;
2265 }
2266 
2267 static int tegra_pcie_dw_remove(struct platform_device *pdev)
2268 {
2269     struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev);
2270 
2271     if (pcie->of_data->mode == DW_PCIE_RC_TYPE) {
2272         if (!pcie->link_state)
2273             return 0;
2274 
2275         debugfs_remove_recursive(pcie->debugfs);
2276         tegra_pcie_deinit_controller(pcie);
2277         pm_runtime_put_sync(pcie->dev);
2278     } else {
2279         disable_irq(pcie->pex_rst_irq);
2280         pex_ep_event_pex_rst_assert(pcie);
2281     }
2282 
2283     pm_runtime_disable(pcie->dev);
2284     tegra_bpmp_put(pcie->bpmp);
2285     if (pcie->pex_refclk_sel_gpiod)
2286         gpiod_set_value(pcie->pex_refclk_sel_gpiod, 0);
2287 
2288     return 0;
2289 }
2290 
2291 static int tegra_pcie_dw_suspend_late(struct device *dev)
2292 {
2293     struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2294     u32 val;
2295 
2296     if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
2297         dev_err(dev, "Failed to Suspend as Tegra PCIe is in EP mode\n");
2298         return -EPERM;
2299     }
2300 
2301     if (!pcie->link_state)
2302         return 0;
2303 
2304     /* Enable HW_HOT_RST mode */
2305     if (!pcie->of_data->has_sbr_reset_fix) {
2306         val = appl_readl(pcie, APPL_CTRL);
2307         val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
2308              APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
2309         val |= APPL_CTRL_HW_HOT_RST_EN;
2310         appl_writel(pcie, val, APPL_CTRL);
2311     }
2312 
2313     return 0;
2314 }
2315 
2316 static int tegra_pcie_dw_suspend_noirq(struct device *dev)
2317 {
2318     struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2319 
2320     if (!pcie->link_state)
2321         return 0;
2322 
2323     tegra_pcie_downstream_dev_to_D0(pcie);
2324     tegra_pcie_dw_pme_turnoff(pcie);
2325     tegra_pcie_unconfig_controller(pcie);
2326 
2327     return 0;
2328 }
2329 
2330 static int tegra_pcie_dw_resume_noirq(struct device *dev)
2331 {
2332     struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2333     int ret;
2334 
2335     if (!pcie->link_state)
2336         return 0;
2337 
2338     ret = tegra_pcie_config_controller(pcie, true);
2339     if (ret < 0)
2340         return ret;
2341 
2342     ret = tegra_pcie_dw_host_init(&pcie->pci.pp);
2343     if (ret < 0) {
2344         dev_err(dev, "Failed to init host: %d\n", ret);
2345         goto fail_host_init;
2346     }
2347 
2348     dw_pcie_setup_rc(&pcie->pci.pp);
2349 
2350     ret = tegra_pcie_dw_start_link(&pcie->pci);
2351     if (ret < 0)
2352         goto fail_host_init;
2353 
2354     return 0;
2355 
2356 fail_host_init:
2357     tegra_pcie_unconfig_controller(pcie);
2358     return ret;
2359 }
2360 
2361 static int tegra_pcie_dw_resume_early(struct device *dev)
2362 {
2363     struct tegra_pcie_dw *pcie = dev_get_drvdata(dev);
2364     u32 val;
2365 
2366     if (pcie->of_data->mode == DW_PCIE_EP_TYPE) {
2367         dev_err(dev, "Suspend is not supported in EP mode");
2368         return -ENOTSUPP;
2369     }
2370 
2371     if (!pcie->link_state)
2372         return 0;
2373 
2374     /* Disable HW_HOT_RST mode */
2375     if (!pcie->of_data->has_sbr_reset_fix) {
2376         val = appl_readl(pcie, APPL_CTRL);
2377         val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK <<
2378              APPL_CTRL_HW_HOT_RST_MODE_SHIFT);
2379         val |= APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST <<
2380                APPL_CTRL_HW_HOT_RST_MODE_SHIFT;
2381         val &= ~APPL_CTRL_HW_HOT_RST_EN;
2382         appl_writel(pcie, val, APPL_CTRL);
2383     }
2384 
2385     return 0;
2386 }
2387 
2388 static void tegra_pcie_dw_shutdown(struct platform_device *pdev)
2389 {
2390     struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev);
2391 
2392     if (pcie->of_data->mode == DW_PCIE_RC_TYPE) {
2393         if (!pcie->link_state)
2394             return;
2395 
2396         debugfs_remove_recursive(pcie->debugfs);
2397         tegra_pcie_downstream_dev_to_D0(pcie);
2398 
2399         disable_irq(pcie->pci.pp.irq);
2400         if (IS_ENABLED(CONFIG_PCI_MSI))
2401             disable_irq(pcie->pci.pp.msi_irq[0]);
2402 
2403         tegra_pcie_dw_pme_turnoff(pcie);
2404         tegra_pcie_unconfig_controller(pcie);
2405         pm_runtime_put_sync(pcie->dev);
2406     } else {
2407         disable_irq(pcie->pex_rst_irq);
2408         pex_ep_event_pex_rst_assert(pcie);
2409     }
2410 }
2411 
2412 static const struct tegra_pcie_dw_of_data tegra194_pcie_dw_rc_of_data = {
2413     .version = TEGRA194_DWC_IP_VER,
2414     .mode = DW_PCIE_RC_TYPE,
2415     .cdm_chk_int_en_bit = BIT(19),
2416     /* Gen4 - 5, 6, 8 and 9 presets enabled */
2417     .gen4_preset_vec = 0x360,
2418     .n_fts = { 52, 52 },
2419 };
2420 
2421 static const struct tegra_pcie_dw_of_data tegra194_pcie_dw_ep_of_data = {
2422     .version = TEGRA194_DWC_IP_VER,
2423     .mode = DW_PCIE_EP_TYPE,
2424     .cdm_chk_int_en_bit = BIT(19),
2425     /* Gen4 - 5, 6, 8 and 9 presets enabled */
2426     .gen4_preset_vec = 0x360,
2427     .n_fts = { 52, 52 },
2428 };
2429 
2430 static const struct tegra_pcie_dw_of_data tegra234_pcie_dw_rc_of_data = {
2431     .version = TEGRA234_DWC_IP_VER,
2432     .mode = DW_PCIE_RC_TYPE,
2433     .has_msix_doorbell_access_fix = true,
2434     .has_sbr_reset_fix = true,
2435     .has_l1ss_exit_fix = true,
2436     .cdm_chk_int_en_bit = BIT(18),
2437     /* Gen4 - 6, 8 and 9 presets enabled */
2438     .gen4_preset_vec = 0x340,
2439     .n_fts = { 52, 80 },
2440 };
2441 
2442 static const struct tegra_pcie_dw_of_data tegra234_pcie_dw_ep_of_data = {
2443     .version = TEGRA234_DWC_IP_VER,
2444     .mode = DW_PCIE_EP_TYPE,
2445     .has_l1ss_exit_fix = true,
2446     .has_ltr_req_fix = true,
2447     .cdm_chk_int_en_bit = BIT(18),
2448     /* Gen4 - 6, 8 and 9 presets enabled */
2449     .gen4_preset_vec = 0x340,
2450     .n_fts = { 52, 80 },
2451 };
2452 
2453 static const struct of_device_id tegra_pcie_dw_of_match[] = {
2454     {
2455         .compatible = "nvidia,tegra194-pcie",
2456         .data = &tegra194_pcie_dw_rc_of_data,
2457     },
2458     {
2459         .compatible = "nvidia,tegra194-pcie-ep",
2460         .data = &tegra194_pcie_dw_ep_of_data,
2461     },
2462     {
2463         .compatible = "nvidia,tegra234-pcie",
2464         .data = &tegra234_pcie_dw_rc_of_data,
2465     },
2466     {
2467         .compatible = "nvidia,tegra234-pcie-ep",
2468         .data = &tegra234_pcie_dw_ep_of_data,
2469     },
2470     {}
2471 };
2472 
2473 static const struct dev_pm_ops tegra_pcie_dw_pm_ops = {
2474     .suspend_late = tegra_pcie_dw_suspend_late,
2475     .suspend_noirq = tegra_pcie_dw_suspend_noirq,
2476     .resume_noirq = tegra_pcie_dw_resume_noirq,
2477     .resume_early = tegra_pcie_dw_resume_early,
2478 };
2479 
2480 static struct platform_driver tegra_pcie_dw_driver = {
2481     .probe = tegra_pcie_dw_probe,
2482     .remove = tegra_pcie_dw_remove,
2483     .shutdown = tegra_pcie_dw_shutdown,
2484     .driver = {
2485         .name   = "tegra194-pcie",
2486         .pm = &tegra_pcie_dw_pm_ops,
2487         .of_match_table = tegra_pcie_dw_of_match,
2488     },
2489 };
2490 module_platform_driver(tegra_pcie_dw_driver);
2491 
2492 MODULE_DEVICE_TABLE(of, tegra_pcie_dw_of_match);
2493 
2494 MODULE_AUTHOR("Vidya Sagar <vidyas@nvidia.com>");
2495 MODULE_DESCRIPTION("NVIDIA PCIe host controller driver");
2496 MODULE_LICENSE("GPL v2");