1 // SPDX-License-Identifier: GPL-2.0+
3 * PCIe host controller driver for Tegra SoCs
5 * Copyright (c) 2010, CompuLab, Ltd.
8 * Based on NVIDIA PCIe driver
9 * Copyright (c) 2008-2009, NVIDIA Corporation.
11 * Bits taken from arch/arm/mach-dove/pcie.c
16 #include <linux/clk.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/export.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/interrupt.h>
22 #include <linux/iopoll.h>
23 #include <linux/irq.h>
24 #include <linux/irqdomain.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/msi.h>
29 #include <linux/of_address.h>
30 #include <linux/of_pci.h>
31 #include <linux/of_platform.h>
32 #include <linux/pci.h>
33 #include <linux/phy/phy.h>
34 #include <linux/pinctrl/consumer.h>
35 #include <linux/platform_device.h>
36 #include <linux/reset.h>
37 #include <linux/sizes.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/regulator/consumer.h>
42 #include <soc/tegra/cpuidle.h>
43 #include <soc/tegra/pmc.h>
47 #define INT_PCI_MSI_NR (8 * 32)
49 /* register definitions */
51 #define AFI_AXI_BAR0_SZ 0x00
52 #define AFI_AXI_BAR1_SZ 0x04
53 #define AFI_AXI_BAR2_SZ 0x08
54 #define AFI_AXI_BAR3_SZ 0x0c
55 #define AFI_AXI_BAR4_SZ 0x10
56 #define AFI_AXI_BAR5_SZ 0x14
58 #define AFI_AXI_BAR0_START 0x18
59 #define AFI_AXI_BAR1_START 0x1c
60 #define AFI_AXI_BAR2_START 0x20
61 #define AFI_AXI_BAR3_START 0x24
62 #define AFI_AXI_BAR4_START 0x28
63 #define AFI_AXI_BAR5_START 0x2c
65 #define AFI_FPCI_BAR0 0x30
66 #define AFI_FPCI_BAR1 0x34
67 #define AFI_FPCI_BAR2 0x38
68 #define AFI_FPCI_BAR3 0x3c
69 #define AFI_FPCI_BAR4 0x40
70 #define AFI_FPCI_BAR5 0x44
72 #define AFI_CACHE_BAR0_SZ 0x48
73 #define AFI_CACHE_BAR0_ST 0x4c
74 #define AFI_CACHE_BAR1_SZ 0x50
75 #define AFI_CACHE_BAR1_ST 0x54
77 #define AFI_MSI_BAR_SZ 0x60
78 #define AFI_MSI_FPCI_BAR_ST 0x64
79 #define AFI_MSI_AXI_BAR_ST 0x68
81 #define AFI_MSI_VEC0 0x6c
82 #define AFI_MSI_VEC1 0x70
83 #define AFI_MSI_VEC2 0x74
84 #define AFI_MSI_VEC3 0x78
85 #define AFI_MSI_VEC4 0x7c
86 #define AFI_MSI_VEC5 0x80
87 #define AFI_MSI_VEC6 0x84
88 #define AFI_MSI_VEC7 0x88
90 #define AFI_MSI_EN_VEC0 0x8c
91 #define AFI_MSI_EN_VEC1 0x90
92 #define AFI_MSI_EN_VEC2 0x94
93 #define AFI_MSI_EN_VEC3 0x98
94 #define AFI_MSI_EN_VEC4 0x9c
95 #define AFI_MSI_EN_VEC5 0xa0
96 #define AFI_MSI_EN_VEC6 0xa4
97 #define AFI_MSI_EN_VEC7 0xa8
99 #define AFI_CONFIGURATION 0xac
100 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
101 #define AFI_CONFIGURATION_CLKEN_OVERRIDE (1 << 31)
103 #define AFI_FPCI_ERROR_MASKS 0xb0
105 #define AFI_INTR_MASK 0xb4
106 #define AFI_INTR_MASK_INT_MASK (1 << 0)
107 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
109 #define AFI_INTR_CODE 0xb8
110 #define AFI_INTR_CODE_MASK 0xf
111 #define AFI_INTR_INI_SLAVE_ERROR 1
112 #define AFI_INTR_INI_DECODE_ERROR 2
113 #define AFI_INTR_TARGET_ABORT 3
114 #define AFI_INTR_MASTER_ABORT 4
115 #define AFI_INTR_INVALID_WRITE 5
116 #define AFI_INTR_LEGACY 6
117 #define AFI_INTR_FPCI_DECODE_ERROR 7
118 #define AFI_INTR_AXI_DECODE_ERROR 8
119 #define AFI_INTR_FPCI_TIMEOUT 9
120 #define AFI_INTR_PE_PRSNT_SENSE 10
121 #define AFI_INTR_PE_CLKREQ_SENSE 11
122 #define AFI_INTR_CLKCLAMP_SENSE 12
123 #define AFI_INTR_RDY4PD_SENSE 13
124 #define AFI_INTR_P2P_ERROR 14
126 #define AFI_INTR_SIGNATURE 0xbc
127 #define AFI_UPPER_FPCI_ADDRESS 0xc0
128 #define AFI_SM_INTR_ENABLE 0xc4
129 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
130 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
131 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
132 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
133 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
134 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
135 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
136 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
138 #define AFI_AFI_INTR_ENABLE 0xc8
139 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
140 #define AFI_INTR_EN_INI_DECERR (1 << 1)
141 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
142 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
143 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
144 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
145 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
146 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
147 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
149 #define AFI_PCIE_PME 0xf0
151 #define AFI_PCIE_CONFIG 0x0f8
152 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
153 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
154 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
155 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
156 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
157 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
158 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401 (0x0 << 20)
159 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
160 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
161 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
162 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211 (0x1 << 20)
163 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
164 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111 (0x2 << 20)
165 #define AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(x) (1 << ((x) + 29))
166 #define AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO_ALL (0x7 << 29)
168 #define AFI_FUSE 0x104
169 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
171 #define AFI_PEX0_CTRL 0x110
172 #define AFI_PEX1_CTRL 0x118
173 #define AFI_PEX_CTRL_RST (1 << 0)
174 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
175 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
176 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
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)
182 #define AFI_PEXBIAS_CTRL_0 0x168
184 #define RP_ECTL_2_R1 0x00000e84
185 #define RP_ECTL_2_R1_RX_CTLE_1C_MASK 0xffff
187 #define RP_ECTL_4_R1 0x00000e8c
188 #define RP_ECTL_4_R1_RX_CDR_CTRL_1C_MASK (0xffff << 16)
189 #define RP_ECTL_4_R1_RX_CDR_CTRL_1C_SHIFT 16
191 #define RP_ECTL_5_R1 0x00000e90
192 #define RP_ECTL_5_R1_RX_EQ_CTRL_L_1C_MASK 0xffffffff
194 #define RP_ECTL_6_R1 0x00000e94
195 #define RP_ECTL_6_R1_RX_EQ_CTRL_H_1C_MASK 0xffffffff
197 #define RP_ECTL_2_R2 0x00000ea4
198 #define RP_ECTL_2_R2_RX_CTLE_1C_MASK 0xffff
200 #define RP_ECTL_4_R2 0x00000eac
201 #define RP_ECTL_4_R2_RX_CDR_CTRL_1C_MASK (0xffff << 16)
202 #define RP_ECTL_4_R2_RX_CDR_CTRL_1C_SHIFT 16
204 #define RP_ECTL_5_R2 0x00000eb0
205 #define RP_ECTL_5_R2_RX_EQ_CTRL_L_1C_MASK 0xffffffff
207 #define RP_ECTL_6_R2 0x00000eb4
208 #define RP_ECTL_6_R2_RX_EQ_CTRL_H_1C_MASK 0xffffffff
210 #define RP_VEND_XP 0x00000f00
211 #define RP_VEND_XP_DL_UP (1 << 30)
212 #define RP_VEND_XP_OPPORTUNISTIC_ACK (1 << 27)
213 #define RP_VEND_XP_OPPORTUNISTIC_UPDATEFC (1 << 28)
214 #define RP_VEND_XP_UPDATE_FC_THRESHOLD_MASK (0xff << 18)
216 #define RP_VEND_CTL0 0x00000f44
217 #define RP_VEND_CTL0_DSK_RST_PULSE_WIDTH_MASK (0xf << 12)
218 #define RP_VEND_CTL0_DSK_RST_PULSE_WIDTH (0x9 << 12)
220 #define RP_VEND_CTL1 0x00000f48
221 #define RP_VEND_CTL1_ERPT (1 << 13)
223 #define RP_VEND_XP_BIST 0x00000f4c
224 #define RP_VEND_XP_BIST_GOTO_L1_L2_AFTER_DLLP_DONE (1 << 28)
226 #define RP_VEND_CTL2 0x00000fa8
227 #define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
229 #define RP_PRIV_MISC 0x00000fe0
230 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
231 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
232 #define RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD_MASK (0x7f << 16)
233 #define RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD (0xf << 16)
234 #define RP_PRIV_MISC_CTLR_CLK_CLAMP_ENABLE (1 << 23)
235 #define RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD_MASK (0x7f << 24)
236 #define RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD (0xf << 24)
237 #define RP_PRIV_MISC_TMS_CLK_CLAMP_ENABLE (1 << 31)
239 #define RP_LINK_CONTROL_STATUS 0x00000090
240 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
241 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
243 #define RP_LINK_CONTROL_STATUS_2 0x000000b0
245 #define PADS_CTL_SEL 0x0000009c
247 #define PADS_CTL 0x000000a0
248 #define PADS_CTL_IDDQ_1L (1 << 0)
249 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
250 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
252 #define PADS_PLL_CTL_TEGRA20 0x000000b8
253 #define PADS_PLL_CTL_TEGRA30 0x000000b4
254 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
255 #define PADS_PLL_CTL_LOCKDET (1 << 8)
256 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
257 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
258 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
259 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
260 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
261 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
262 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
263 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
265 #define PADS_REFCLK_CFG0 0x000000c8
266 #define PADS_REFCLK_CFG1 0x000000cc
267 #define PADS_REFCLK_BIAS 0x000000d0
270 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
271 * entries, one entry per PCIe port. These field definitions and desired
272 * values aren't in the TRM, but do come from NVIDIA.
274 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
275 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
276 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
277 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
279 #define PME_ACK_TIMEOUT 10000
280 #define LINK_RETRAIN_TIMEOUT 100000 /* in usec */
283 struct msi_controller chip;
284 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
285 struct irq_domain *domain;
292 /* used to differentiate between Tegra SoC generations */
293 struct tegra_pcie_port_soc {
300 struct tegra_pcie_soc {
301 unsigned int num_ports;
302 const struct tegra_pcie_port_soc *ports;
303 unsigned int msi_base_shift;
304 unsigned long afi_pex2_ctrl;
307 u32 pads_refclk_cfg0;
308 u32 pads_refclk_cfg1;
309 u32 update_fc_threshold;
310 bool has_pex_clkreq_en;
311 bool has_pex_bias_ctrl;
312 bool has_intr_prsnt_sense;
315 bool force_pca_enable;
317 bool update_clamp_threshold;
318 bool program_deskew_time;
319 bool update_fc_timer;
336 static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
338 return container_of(chip, struct tegra_msi, chip);
356 struct reset_control *pex_rst;
357 struct reset_control *afi_rst;
358 struct reset_control *pcie_xrst;
363 struct tegra_msi msi;
365 struct list_head ports;
368 struct regulator_bulk_data *supplies;
369 unsigned int num_supplies;
371 const struct tegra_pcie_soc *soc;
372 struct dentry *debugfs;
375 struct tegra_pcie_port {
376 struct tegra_pcie *pcie;
377 struct device_node *np;
378 struct list_head list;
379 struct resource regs;
386 struct gpio_desc *reset_gpio;
389 struct tegra_pcie_bus {
390 struct list_head list;
394 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
395 unsigned long offset)
397 writel(value, pcie->afi + offset);
400 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
402 return readl(pcie->afi + offset);
405 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
406 unsigned long offset)
408 writel(value, pcie->pads + offset);
411 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
413 return readl(pcie->pads + offset);
417 * The configuration space mapping on Tegra is somewhat similar to the ECAM
418 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
419 * register accesses are mapped:
421 * [27:24] extended register number
423 * [15:11] device number
424 * [10: 8] function number
425 * [ 7: 0] register number
427 * Mapping the whole extended configuration space would require 256 MiB of
428 * virtual address space, only a small part of which will actually be used.
430 * To work around this, a 4 KiB region is used to generate the required
431 * configuration transaction with relevant B:D:F and register offset values.
432 * This is achieved by dynamically programming base address and size of
433 * AFI_AXI_BAR used for end point config space mapping to make sure that the
434 * address (access to which generates correct config transaction) falls in
437 static unsigned int tegra_pcie_conf_offset(u8 bus, unsigned int devfn,
440 return ((where & 0xf00) << 16) | (bus << 16) | (PCI_SLOT(devfn) << 11) |
441 (PCI_FUNC(devfn) << 8) | (where & 0xff);
444 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
448 struct tegra_pcie *pcie = bus->sysdata;
449 void __iomem *addr = NULL;
451 if (bus->number == 0) {
452 unsigned int slot = PCI_SLOT(devfn);
453 struct tegra_pcie_port *port;
455 list_for_each_entry(port, &pcie->ports, list) {
456 if (port->index + 1 == slot) {
457 addr = port->base + (where & ~3);
465 offset = tegra_pcie_conf_offset(bus->number, devfn, where);
467 /* move 4 KiB window to offset within the FPCI region */
468 base = 0xfe100000 + ((offset & ~(SZ_4K - 1)) >> 8);
469 afi_writel(pcie, base, AFI_FPCI_BAR0);
471 /* move to correct offset within the 4 KiB page */
472 addr = pcie->cfg + (offset & (SZ_4K - 1));
478 static int tegra_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
479 int where, int size, u32 *value)
481 if (bus->number == 0)
482 return pci_generic_config_read32(bus, devfn, where, size,
485 return pci_generic_config_read(bus, devfn, where, size, value);
488 static int tegra_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
489 int where, int size, u32 value)
491 if (bus->number == 0)
492 return pci_generic_config_write32(bus, devfn, where, size,
495 return pci_generic_config_write(bus, devfn, where, size, value);
498 static struct pci_ops tegra_pcie_ops = {
499 .map_bus = tegra_pcie_map_bus,
500 .read = tegra_pcie_config_read,
501 .write = tegra_pcie_config_write,
504 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
506 const struct tegra_pcie_soc *soc = port->pcie->soc;
507 unsigned long ret = 0;
509 switch (port->index) {
519 ret = soc->afi_pex2_ctrl;
526 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
528 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
531 /* pulse reset signal */
532 if (port->reset_gpio) {
533 gpiod_set_value(port->reset_gpio, 1);
535 value = afi_readl(port->pcie, ctrl);
536 value &= ~AFI_PEX_CTRL_RST;
537 afi_writel(port->pcie, value, ctrl);
540 usleep_range(1000, 2000);
542 if (port->reset_gpio) {
543 gpiod_set_value(port->reset_gpio, 0);
545 value = afi_readl(port->pcie, ctrl);
546 value |= AFI_PEX_CTRL_RST;
547 afi_writel(port->pcie, value, ctrl);
551 static void tegra_pcie_enable_rp_features(struct tegra_pcie_port *port)
553 const struct tegra_pcie_soc *soc = port->pcie->soc;
556 /* Enable AER capability */
557 value = readl(port->base + RP_VEND_CTL1);
558 value |= RP_VEND_CTL1_ERPT;
559 writel(value, port->base + RP_VEND_CTL1);
561 /* Optimal settings to enhance bandwidth */
562 value = readl(port->base + RP_VEND_XP);
563 value |= RP_VEND_XP_OPPORTUNISTIC_ACK;
564 value |= RP_VEND_XP_OPPORTUNISTIC_UPDATEFC;
565 writel(value, port->base + RP_VEND_XP);
568 * LTSSM will wait for DLLP to finish before entering L1 or L2,
569 * to avoid truncation of PM messages which results in receiver errors
571 value = readl(port->base + RP_VEND_XP_BIST);
572 value |= RP_VEND_XP_BIST_GOTO_L1_L2_AFTER_DLLP_DONE;
573 writel(value, port->base + RP_VEND_XP_BIST);
575 value = readl(port->base + RP_PRIV_MISC);
576 value |= RP_PRIV_MISC_CTLR_CLK_CLAMP_ENABLE;
577 value |= RP_PRIV_MISC_TMS_CLK_CLAMP_ENABLE;
579 if (soc->update_clamp_threshold) {
580 value &= ~(RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD_MASK |
581 RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD_MASK);
582 value |= RP_PRIV_MISC_CTLR_CLK_CLAMP_THRESHOLD |
583 RP_PRIV_MISC_TMS_CLK_CLAMP_THRESHOLD;
586 writel(value, port->base + RP_PRIV_MISC);
589 static void tegra_pcie_program_ectl_settings(struct tegra_pcie_port *port)
591 const struct tegra_pcie_soc *soc = port->pcie->soc;
594 value = readl(port->base + RP_ECTL_2_R1);
595 value &= ~RP_ECTL_2_R1_RX_CTLE_1C_MASK;
596 value |= soc->ectl.regs.rp_ectl_2_r1;
597 writel(value, port->base + RP_ECTL_2_R1);
599 value = readl(port->base + RP_ECTL_4_R1);
600 value &= ~RP_ECTL_4_R1_RX_CDR_CTRL_1C_MASK;
601 value |= soc->ectl.regs.rp_ectl_4_r1 <<
602 RP_ECTL_4_R1_RX_CDR_CTRL_1C_SHIFT;
603 writel(value, port->base + RP_ECTL_4_R1);
605 value = readl(port->base + RP_ECTL_5_R1);
606 value &= ~RP_ECTL_5_R1_RX_EQ_CTRL_L_1C_MASK;
607 value |= soc->ectl.regs.rp_ectl_5_r1;
608 writel(value, port->base + RP_ECTL_5_R1);
610 value = readl(port->base + RP_ECTL_6_R1);
611 value &= ~RP_ECTL_6_R1_RX_EQ_CTRL_H_1C_MASK;
612 value |= soc->ectl.regs.rp_ectl_6_r1;
613 writel(value, port->base + RP_ECTL_6_R1);
615 value = readl(port->base + RP_ECTL_2_R2);
616 value &= ~RP_ECTL_2_R2_RX_CTLE_1C_MASK;
617 value |= soc->ectl.regs.rp_ectl_2_r2;
618 writel(value, port->base + RP_ECTL_2_R2);
620 value = readl(port->base + RP_ECTL_4_R2);
621 value &= ~RP_ECTL_4_R2_RX_CDR_CTRL_1C_MASK;
622 value |= soc->ectl.regs.rp_ectl_4_r2 <<
623 RP_ECTL_4_R2_RX_CDR_CTRL_1C_SHIFT;
624 writel(value, port->base + RP_ECTL_4_R2);
626 value = readl(port->base + RP_ECTL_5_R2);
627 value &= ~RP_ECTL_5_R2_RX_EQ_CTRL_L_1C_MASK;
628 value |= soc->ectl.regs.rp_ectl_5_r2;
629 writel(value, port->base + RP_ECTL_5_R2);
631 value = readl(port->base + RP_ECTL_6_R2);
632 value &= ~RP_ECTL_6_R2_RX_EQ_CTRL_H_1C_MASK;
633 value |= soc->ectl.regs.rp_ectl_6_r2;
634 writel(value, port->base + RP_ECTL_6_R2);
637 static void tegra_pcie_apply_sw_fixup(struct tegra_pcie_port *port)
639 const struct tegra_pcie_soc *soc = port->pcie->soc;
643 * Sometimes link speed change from Gen2 to Gen1 fails due to
644 * instability in deskew logic on lane-0. Increase the deskew
645 * retry time to resolve this issue.
647 if (soc->program_deskew_time) {
648 value = readl(port->base + RP_VEND_CTL0);
649 value &= ~RP_VEND_CTL0_DSK_RST_PULSE_WIDTH_MASK;
650 value |= RP_VEND_CTL0_DSK_RST_PULSE_WIDTH;
651 writel(value, port->base + RP_VEND_CTL0);
654 if (soc->update_fc_timer) {
655 value = readl(port->base + RP_VEND_XP);
656 value &= ~RP_VEND_XP_UPDATE_FC_THRESHOLD_MASK;
657 value |= soc->update_fc_threshold;
658 writel(value, port->base + RP_VEND_XP);
662 * PCIe link doesn't come up with few legacy PCIe endpoints if
663 * root port advertises both Gen-1 and Gen-2 speeds in Tegra.
664 * Hence, the strategy followed here is to initially advertise
665 * only Gen-1 and after link is up, retrain link to Gen-2 speed
667 value = readl(port->base + RP_LINK_CONTROL_STATUS_2);
668 value &= ~PCI_EXP_LNKSTA_CLS;
669 value |= PCI_EXP_LNKSTA_CLS_2_5GB;
670 writel(value, port->base + RP_LINK_CONTROL_STATUS_2);
673 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
675 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
676 const struct tegra_pcie_soc *soc = port->pcie->soc;
679 /* enable reference clock */
680 value = afi_readl(port->pcie, ctrl);
681 value |= AFI_PEX_CTRL_REFCLK_EN;
683 if (soc->has_pex_clkreq_en)
684 value |= AFI_PEX_CTRL_CLKREQ_EN;
686 value |= AFI_PEX_CTRL_OVERRIDE_EN;
688 afi_writel(port->pcie, value, ctrl);
690 tegra_pcie_port_reset(port);
692 if (soc->force_pca_enable) {
693 value = readl(port->base + RP_VEND_CTL2);
694 value |= RP_VEND_CTL2_PCA_ENABLE;
695 writel(value, port->base + RP_VEND_CTL2);
698 tegra_pcie_enable_rp_features(port);
700 if (soc->ectl.enable)
701 tegra_pcie_program_ectl_settings(port);
703 tegra_pcie_apply_sw_fixup(port);
706 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
708 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
709 const struct tegra_pcie_soc *soc = port->pcie->soc;
712 /* assert port reset */
713 value = afi_readl(port->pcie, ctrl);
714 value &= ~AFI_PEX_CTRL_RST;
715 afi_writel(port->pcie, value, ctrl);
717 /* disable reference clock */
718 value = afi_readl(port->pcie, ctrl);
720 if (soc->has_pex_clkreq_en)
721 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
723 value &= ~AFI_PEX_CTRL_REFCLK_EN;
724 afi_writel(port->pcie, value, ctrl);
726 /* disable PCIe port and set CLKREQ# as GPIO to allow PLLE power down */
727 value = afi_readl(port->pcie, AFI_PCIE_CONFIG);
728 value |= AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
729 value |= AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(port->index);
730 afi_writel(port->pcie, value, AFI_PCIE_CONFIG);
733 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
735 struct tegra_pcie *pcie = port->pcie;
736 struct device *dev = pcie->dev;
738 devm_iounmap(dev, port->base);
739 devm_release_mem_region(dev, port->regs.start,
740 resource_size(&port->regs));
741 list_del(&port->list);
742 devm_kfree(dev, port);
745 /* Tegra PCIE root complex wrongly reports device class */
746 static void tegra_pcie_fixup_class(struct pci_dev *dev)
748 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
750 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
751 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
752 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
753 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
755 /* Tegra20 and Tegra30 PCIE requires relaxed ordering */
756 static void tegra_pcie_relax_enable(struct pci_dev *dev)
758 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
760 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_relax_enable);
761 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_relax_enable);
762 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_relax_enable);
763 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_relax_enable);
765 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
767 struct tegra_pcie *pcie = pdev->bus->sysdata;
770 tegra_cpuidle_pcie_irqs_in_use();
772 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
779 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
781 const char *err_msg[] = {
789 "Response decoding error",
790 "AXI response decoding error",
791 "Transaction timeout",
792 "Slot present pin change",
793 "Slot clock request change",
794 "TMS clock ramp change",
795 "TMS ready for power down",
798 struct tegra_pcie *pcie = arg;
799 struct device *dev = pcie->dev;
802 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
803 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
804 afi_writel(pcie, 0, AFI_INTR_CODE);
806 if (code == AFI_INTR_LEGACY)
809 if (code >= ARRAY_SIZE(err_msg))
813 * do not pollute kernel log with master abort reports since they
814 * happen a lot during enumeration
816 if (code == AFI_INTR_MASTER_ABORT || code == AFI_INTR_PE_PRSNT_SENSE)
817 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
819 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
821 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
822 code == AFI_INTR_FPCI_DECODE_ERROR) {
823 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
824 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
826 if (code == AFI_INTR_MASTER_ABORT)
827 dev_dbg(dev, " FPCI address: %10llx\n", address);
829 dev_err(dev, " FPCI address: %10llx\n", address);
836 * FPCI map is as follows:
837 * - 0xfdfc000000: I/O space
838 * - 0xfdfe000000: type 0 configuration space
839 * - 0xfdff000000: type 1 configuration space
840 * - 0xfe00000000: type 0 extended configuration space
841 * - 0xfe10000000: type 1 extended configuration space
843 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
846 struct resource_entry *entry;
847 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
849 /* Bar 0: type 1 extended configuration space */
850 size = resource_size(&pcie->cs);
851 afi_writel(pcie, pcie->cs.start, AFI_AXI_BAR0_START);
852 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
854 resource_list_for_each_entry(entry, &bridge->windows) {
855 u32 fpci_bar, axi_address;
856 struct resource *res = entry->res;
858 size = resource_size(res);
860 switch (resource_type(res)) {
862 /* Bar 1: downstream IO bar */
863 fpci_bar = 0xfdfc0000;
864 axi_address = pci_pio_to_address(res->start);
865 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
866 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
867 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
870 fpci_bar = (((res->start >> 12) & 0x0fffffff) << 4) | 0x1;
871 axi_address = res->start;
873 if (res->flags & IORESOURCE_PREFETCH) {
874 /* Bar 2: prefetchable memory BAR */
875 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
876 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
877 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
880 /* Bar 3: non prefetchable memory BAR */
881 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
882 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
883 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
889 /* NULL out the remaining BARs as they are not used */
890 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
891 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
892 afi_writel(pcie, 0, AFI_FPCI_BAR4);
894 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
895 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
896 afi_writel(pcie, 0, AFI_FPCI_BAR5);
898 if (pcie->soc->has_cache_bars) {
899 /* map all upstream transactions as uncached */
900 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
901 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
902 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
903 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
906 /* MSI translations are setup only when needed */
907 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
908 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
909 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
910 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
913 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
915 const struct tegra_pcie_soc *soc = pcie->soc;
918 timeout = jiffies + msecs_to_jiffies(timeout);
920 while (time_before(jiffies, timeout)) {
921 value = pads_readl(pcie, soc->pads_pll_ctl);
922 if (value & PADS_PLL_CTL_LOCKDET)
929 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
931 struct device *dev = pcie->dev;
932 const struct tegra_pcie_soc *soc = pcie->soc;
936 /* initialize internal PHY, enable up to 16 PCIE lanes */
937 pads_writel(pcie, 0x0, PADS_CTL_SEL);
939 /* override IDDQ to 1 on all 4 lanes */
940 value = pads_readl(pcie, PADS_CTL);
941 value |= PADS_CTL_IDDQ_1L;
942 pads_writel(pcie, value, PADS_CTL);
945 * Set up PHY PLL inputs select PLLE output as refclock,
946 * set TX ref sel to div10 (not div5).
948 value = pads_readl(pcie, soc->pads_pll_ctl);
949 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
950 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
951 pads_writel(pcie, value, soc->pads_pll_ctl);
954 value = pads_readl(pcie, soc->pads_pll_ctl);
955 value &= ~PADS_PLL_CTL_RST_B4SM;
956 pads_writel(pcie, value, soc->pads_pll_ctl);
958 usleep_range(20, 100);
960 /* take PLL out of reset */
961 value = pads_readl(pcie, soc->pads_pll_ctl);
962 value |= PADS_PLL_CTL_RST_B4SM;
963 pads_writel(pcie, value, soc->pads_pll_ctl);
965 /* wait for the PLL to lock */
966 err = tegra_pcie_pll_wait(pcie, 500);
968 dev_err(dev, "PLL failed to lock: %d\n", err);
972 /* turn off IDDQ override */
973 value = pads_readl(pcie, PADS_CTL);
974 value &= ~PADS_CTL_IDDQ_1L;
975 pads_writel(pcie, value, PADS_CTL);
977 /* enable TX/RX data */
978 value = pads_readl(pcie, PADS_CTL);
979 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
980 pads_writel(pcie, value, PADS_CTL);
985 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
987 const struct tegra_pcie_soc *soc = pcie->soc;
990 /* disable TX/RX data */
991 value = pads_readl(pcie, PADS_CTL);
992 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
993 pads_writel(pcie, value, PADS_CTL);
996 value = pads_readl(pcie, PADS_CTL);
997 value |= PADS_CTL_IDDQ_1L;
998 pads_writel(pcie, value, PADS_CTL);
1001 value = pads_readl(pcie, soc->pads_pll_ctl);
1002 value &= ~PADS_PLL_CTL_RST_B4SM;
1003 pads_writel(pcie, value, soc->pads_pll_ctl);
1005 usleep_range(20, 100);
1010 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
1012 struct device *dev = port->pcie->dev;
1016 for (i = 0; i < port->lanes; i++) {
1017 err = phy_power_on(port->phys[i]);
1019 dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
1027 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
1029 struct device *dev = port->pcie->dev;
1033 for (i = 0; i < port->lanes; i++) {
1034 err = phy_power_off(port->phys[i]);
1036 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
1045 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
1047 struct device *dev = pcie->dev;
1048 struct tegra_pcie_port *port;
1051 if (pcie->legacy_phy) {
1053 err = phy_power_on(pcie->phy);
1055 err = tegra_pcie_phy_enable(pcie);
1058 dev_err(dev, "failed to power on PHY: %d\n", err);
1063 list_for_each_entry(port, &pcie->ports, list) {
1064 err = tegra_pcie_port_phy_power_on(port);
1067 "failed to power on PCIe port %u PHY: %d\n",
1076 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
1078 struct device *dev = pcie->dev;
1079 struct tegra_pcie_port *port;
1082 if (pcie->legacy_phy) {
1084 err = phy_power_off(pcie->phy);
1086 err = tegra_pcie_phy_disable(pcie);
1089 dev_err(dev, "failed to power off PHY: %d\n", err);
1094 list_for_each_entry(port, &pcie->ports, list) {
1095 err = tegra_pcie_port_phy_power_off(port);
1098 "failed to power off PCIe port %u PHY: %d\n",
1107 static void tegra_pcie_enable_controller(struct tegra_pcie *pcie)
1109 const struct tegra_pcie_soc *soc = pcie->soc;
1110 struct tegra_pcie_port *port;
1111 unsigned long value;
1113 /* enable PLL power down */
1115 value = afi_readl(pcie, AFI_PLLE_CONTROL);
1116 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
1117 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1118 afi_writel(pcie, value, AFI_PLLE_CONTROL);
1121 /* power down PCIe slot clock bias pad */
1122 if (soc->has_pex_bias_ctrl)
1123 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1125 /* configure mode and disable all ports */
1126 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1127 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1128 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1129 value |= AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO_ALL;
1131 list_for_each_entry(port, &pcie->ports, list) {
1132 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1133 value &= ~AFI_PCIE_CONFIG_PCIE_CLKREQ_GPIO(port->index);
1136 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1138 if (soc->has_gen2) {
1139 value = afi_readl(pcie, AFI_FUSE);
1140 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1141 afi_writel(pcie, value, AFI_FUSE);
1143 value = afi_readl(pcie, AFI_FUSE);
1144 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1145 afi_writel(pcie, value, AFI_FUSE);
1148 /* Disable AFI dynamic clock gating and enable PCIe */
1149 value = afi_readl(pcie, AFI_CONFIGURATION);
1150 value |= AFI_CONFIGURATION_EN_FPCI;
1151 value |= AFI_CONFIGURATION_CLKEN_OVERRIDE;
1152 afi_writel(pcie, value, AFI_CONFIGURATION);
1154 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1155 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1156 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
1158 if (soc->has_intr_prsnt_sense)
1159 value |= AFI_INTR_EN_PRSNT_SENSE;
1161 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1162 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1164 /* don't enable MSI for now, only when needed */
1165 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1167 /* disable all exceptions */
1168 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1171 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1173 struct device *dev = pcie->dev;
1174 const struct tegra_pcie_soc *soc = pcie->soc;
1177 reset_control_assert(pcie->afi_rst);
1179 clk_disable_unprepare(pcie->pll_e);
1180 if (soc->has_cml_clk)
1181 clk_disable_unprepare(pcie->cml_clk);
1182 clk_disable_unprepare(pcie->afi_clk);
1184 if (!dev->pm_domain)
1185 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1187 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1189 dev_warn(dev, "failed to disable regulators: %d\n", err);
1192 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1194 struct device *dev = pcie->dev;
1195 const struct tegra_pcie_soc *soc = pcie->soc;
1198 reset_control_assert(pcie->pcie_xrst);
1199 reset_control_assert(pcie->afi_rst);
1200 reset_control_assert(pcie->pex_rst);
1202 if (!dev->pm_domain)
1203 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1205 /* enable regulators */
1206 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1208 dev_err(dev, "failed to enable regulators: %d\n", err);
1210 if (!dev->pm_domain) {
1211 err = tegra_powergate_power_on(TEGRA_POWERGATE_PCIE);
1213 dev_err(dev, "failed to power ungate: %d\n", err);
1214 goto regulator_disable;
1216 err = tegra_powergate_remove_clamping(TEGRA_POWERGATE_PCIE);
1218 dev_err(dev, "failed to remove clamp: %d\n", err);
1223 err = clk_prepare_enable(pcie->afi_clk);
1225 dev_err(dev, "failed to enable AFI clock: %d\n", err);
1229 if (soc->has_cml_clk) {
1230 err = clk_prepare_enable(pcie->cml_clk);
1232 dev_err(dev, "failed to enable CML clock: %d\n", err);
1233 goto disable_afi_clk;
1237 err = clk_prepare_enable(pcie->pll_e);
1239 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1240 goto disable_cml_clk;
1243 reset_control_deassert(pcie->afi_rst);
1248 if (soc->has_cml_clk)
1249 clk_disable_unprepare(pcie->cml_clk);
1251 clk_disable_unprepare(pcie->afi_clk);
1253 if (!dev->pm_domain)
1254 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1256 regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1261 static void tegra_pcie_apply_pad_settings(struct tegra_pcie *pcie)
1263 const struct tegra_pcie_soc *soc = pcie->soc;
1265 /* Configure the reference clock driver */
1266 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
1268 if (soc->num_ports > 2)
1269 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
1272 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1274 struct device *dev = pcie->dev;
1275 const struct tegra_pcie_soc *soc = pcie->soc;
1277 pcie->pex_clk = devm_clk_get(dev, "pex");
1278 if (IS_ERR(pcie->pex_clk))
1279 return PTR_ERR(pcie->pex_clk);
1281 pcie->afi_clk = devm_clk_get(dev, "afi");
1282 if (IS_ERR(pcie->afi_clk))
1283 return PTR_ERR(pcie->afi_clk);
1285 pcie->pll_e = devm_clk_get(dev, "pll_e");
1286 if (IS_ERR(pcie->pll_e))
1287 return PTR_ERR(pcie->pll_e);
1289 if (soc->has_cml_clk) {
1290 pcie->cml_clk = devm_clk_get(dev, "cml");
1291 if (IS_ERR(pcie->cml_clk))
1292 return PTR_ERR(pcie->cml_clk);
1298 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1300 struct device *dev = pcie->dev;
1302 pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1303 if (IS_ERR(pcie->pex_rst))
1304 return PTR_ERR(pcie->pex_rst);
1306 pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1307 if (IS_ERR(pcie->afi_rst))
1308 return PTR_ERR(pcie->afi_rst);
1310 pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1311 if (IS_ERR(pcie->pcie_xrst))
1312 return PTR_ERR(pcie->pcie_xrst);
1317 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1319 struct device *dev = pcie->dev;
1322 pcie->phy = devm_phy_optional_get(dev, "pcie");
1323 if (IS_ERR(pcie->phy)) {
1324 err = PTR_ERR(pcie->phy);
1325 dev_err(dev, "failed to get PHY: %d\n", err);
1329 err = phy_init(pcie->phy);
1331 dev_err(dev, "failed to initialize PHY: %d\n", err);
1335 pcie->legacy_phy = true;
1340 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1341 struct device_node *np,
1342 const char *consumer,
1348 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1350 return ERR_PTR(-ENOMEM);
1352 phy = devm_of_phy_get(dev, np, name);
1355 if (PTR_ERR(phy) == -ENODEV)
1361 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1363 struct device *dev = port->pcie->dev;
1368 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1372 for (i = 0; i < port->lanes; i++) {
1373 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1375 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1377 return PTR_ERR(phy);
1380 err = phy_init(phy);
1382 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1387 port->phys[i] = phy;
1393 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1395 const struct tegra_pcie_soc *soc = pcie->soc;
1396 struct device_node *np = pcie->dev->of_node;
1397 struct tegra_pcie_port *port;
1400 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1401 return tegra_pcie_phys_get_legacy(pcie);
1403 list_for_each_entry(port, &pcie->ports, list) {
1404 err = tegra_pcie_port_get_phys(port);
1412 static void tegra_pcie_phys_put(struct tegra_pcie *pcie)
1414 struct tegra_pcie_port *port;
1415 struct device *dev = pcie->dev;
1418 if (pcie->legacy_phy) {
1419 err = phy_exit(pcie->phy);
1421 dev_err(dev, "failed to teardown PHY: %d\n", err);
1425 list_for_each_entry(port, &pcie->ports, list) {
1426 for (i = 0; i < port->lanes; i++) {
1427 err = phy_exit(port->phys[i]);
1429 dev_err(dev, "failed to teardown PHY#%u: %d\n",
1436 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1438 struct device *dev = pcie->dev;
1439 struct platform_device *pdev = to_platform_device(dev);
1440 struct resource *res;
1441 const struct tegra_pcie_soc *soc = pcie->soc;
1444 err = tegra_pcie_clocks_get(pcie);
1446 dev_err(dev, "failed to get clocks: %d\n", err);
1450 err = tegra_pcie_resets_get(pcie);
1452 dev_err(dev, "failed to get resets: %d\n", err);
1456 if (soc->program_uphy) {
1457 err = tegra_pcie_phys_get(pcie);
1459 dev_err(dev, "failed to get PHYs: %d\n", err);
1464 pcie->pads = devm_platform_ioremap_resource_byname(pdev, "pads");
1465 if (IS_ERR(pcie->pads)) {
1466 err = PTR_ERR(pcie->pads);
1470 pcie->afi = devm_platform_ioremap_resource_byname(pdev, "afi");
1471 if (IS_ERR(pcie->afi)) {
1472 err = PTR_ERR(pcie->afi);
1476 /* request configuration space, but remap later, on demand */
1477 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1479 err = -EADDRNOTAVAIL;
1485 /* constrain configuration space to 4 KiB */
1486 pcie->cs.end = pcie->cs.start + SZ_4K - 1;
1488 pcie->cfg = devm_ioremap_resource(dev, &pcie->cs);
1489 if (IS_ERR(pcie->cfg)) {
1490 err = PTR_ERR(pcie->cfg);
1494 /* request interrupt */
1495 err = platform_get_irq_byname(pdev, "intr");
1497 dev_err(dev, "failed to get IRQ: %d\n", err);
1503 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1505 dev_err(dev, "failed to register IRQ: %d\n", err);
1512 if (soc->program_uphy)
1513 tegra_pcie_phys_put(pcie);
1517 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1519 const struct tegra_pcie_soc *soc = pcie->soc;
1522 free_irq(pcie->irq, pcie);
1524 if (soc->program_uphy)
1525 tegra_pcie_phys_put(pcie);
1530 static void tegra_pcie_pme_turnoff(struct tegra_pcie_port *port)
1532 struct tegra_pcie *pcie = port->pcie;
1533 const struct tegra_pcie_soc *soc = pcie->soc;
1538 val = afi_readl(pcie, AFI_PCIE_PME);
1539 val |= (0x1 << soc->ports[port->index].pme.turnoff_bit);
1540 afi_writel(pcie, val, AFI_PCIE_PME);
1542 ack_bit = soc->ports[port->index].pme.ack_bit;
1543 err = readl_poll_timeout(pcie->afi + AFI_PCIE_PME, val,
1544 val & (0x1 << ack_bit), 1, PME_ACK_TIMEOUT);
1546 dev_err(pcie->dev, "PME Ack is not received on port: %d\n",
1549 usleep_range(10000, 11000);
1551 val = afi_readl(pcie, AFI_PCIE_PME);
1552 val &= ~(0x1 << soc->ports[port->index].pme.turnoff_bit);
1553 afi_writel(pcie, val, AFI_PCIE_PME);
1556 static int tegra_msi_alloc(struct tegra_msi *chip)
1560 mutex_lock(&chip->lock);
1562 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1563 if (msi < INT_PCI_MSI_NR)
1564 set_bit(msi, chip->used);
1568 mutex_unlock(&chip->lock);
1573 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1575 struct device *dev = chip->chip.dev;
1577 mutex_lock(&chip->lock);
1579 if (!test_bit(irq, chip->used))
1580 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1582 clear_bit(irq, chip->used);
1584 mutex_unlock(&chip->lock);
1587 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1589 struct tegra_pcie *pcie = data;
1590 struct device *dev = pcie->dev;
1591 struct tegra_msi *msi = &pcie->msi;
1592 unsigned int i, processed = 0;
1594 for (i = 0; i < 8; i++) {
1595 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1598 unsigned int offset = find_first_bit(®, 32);
1599 unsigned int index = i * 32 + offset;
1602 /* clear the interrupt */
1603 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1605 irq = irq_find_mapping(msi->domain, index);
1607 if (test_bit(index, msi->used))
1608 generic_handle_irq(irq);
1610 dev_info(dev, "unhandled MSI\n");
1613 * that's weird who triggered this?
1616 dev_info(dev, "unexpected MSI\n");
1619 /* see if there's any more pending in this vector */
1620 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1626 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1629 static int tegra_msi_setup_irq(struct msi_controller *chip,
1630 struct pci_dev *pdev, struct msi_desc *desc)
1632 struct tegra_msi *msi = to_tegra_msi(chip);
1637 hwirq = tegra_msi_alloc(msi);
1641 irq = irq_create_mapping(msi->domain, hwirq);
1643 tegra_msi_free(msi, hwirq);
1647 irq_set_msi_desc(irq, desc);
1649 msg.address_lo = lower_32_bits(msi->phys);
1650 msg.address_hi = upper_32_bits(msi->phys);
1653 pci_write_msi_msg(irq, &msg);
1658 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1661 struct tegra_msi *msi = to_tegra_msi(chip);
1662 struct irq_data *d = irq_get_irq_data(irq);
1663 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1665 irq_dispose_mapping(irq);
1666 tegra_msi_free(msi, hwirq);
1669 static struct irq_chip tegra_msi_irq_chip = {
1670 .name = "Tegra PCIe MSI",
1671 .irq_enable = pci_msi_unmask_irq,
1672 .irq_disable = pci_msi_mask_irq,
1673 .irq_mask = pci_msi_mask_irq,
1674 .irq_unmask = pci_msi_unmask_irq,
1677 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1678 irq_hw_number_t hwirq)
1680 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1681 irq_set_chip_data(irq, domain->host_data);
1683 tegra_cpuidle_pcie_irqs_in_use();
1688 static const struct irq_domain_ops msi_domain_ops = {
1689 .map = tegra_msi_map,
1692 static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
1694 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1695 struct platform_device *pdev = to_platform_device(pcie->dev);
1696 struct tegra_msi *msi = &pcie->msi;
1697 struct device *dev = pcie->dev;
1700 mutex_init(&msi->lock);
1702 msi->chip.dev = dev;
1703 msi->chip.setup_irq = tegra_msi_setup_irq;
1704 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1706 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
1707 &msi_domain_ops, &msi->chip);
1709 dev_err(dev, "failed to create IRQ domain\n");
1713 err = platform_get_irq_byname(pdev, "msi");
1715 dev_err(dev, "failed to get IRQ: %d\n", err);
1716 goto free_irq_domain;
1721 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1722 tegra_msi_irq_chip.name, pcie);
1724 dev_err(dev, "failed to request IRQ: %d\n", err);
1725 goto free_irq_domain;
1728 /* Though the PCIe controller can address >32-bit address space, to
1729 * facilitate endpoints that support only 32-bit MSI target address,
1730 * the mask is set to 32-bit to make sure that MSI target address is
1731 * always a 32-bit address
1733 err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
1735 dev_err(dev, "failed to set DMA coherent mask: %d\n", err);
1739 msi->virt = dma_alloc_attrs(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL,
1740 DMA_ATTR_NO_KERNEL_MAPPING);
1742 dev_err(dev, "failed to allocate DMA memory for MSI\n");
1747 host->msi = &msi->chip;
1752 free_irq(msi->irq, pcie);
1754 irq_domain_remove(msi->domain);
1758 static void tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1760 const struct tegra_pcie_soc *soc = pcie->soc;
1761 struct tegra_msi *msi = &pcie->msi;
1764 afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1765 afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1766 /* this register is in 4K increments */
1767 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1769 /* enable all MSI vectors */
1770 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1771 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1772 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1773 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1774 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1775 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1776 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1777 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1779 /* and unmask the MSI interrupt */
1780 reg = afi_readl(pcie, AFI_INTR_MASK);
1781 reg |= AFI_INTR_MASK_MSI_MASK;
1782 afi_writel(pcie, reg, AFI_INTR_MASK);
1785 static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
1787 struct tegra_msi *msi = &pcie->msi;
1788 unsigned int i, irq;
1790 dma_free_attrs(pcie->dev, PAGE_SIZE, msi->virt, msi->phys,
1791 DMA_ATTR_NO_KERNEL_MAPPING);
1794 free_irq(msi->irq, pcie);
1796 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1797 irq = irq_find_mapping(msi->domain, i);
1799 irq_dispose_mapping(irq);
1802 irq_domain_remove(msi->domain);
1805 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1809 /* mask the MSI interrupt */
1810 value = afi_readl(pcie, AFI_INTR_MASK);
1811 value &= ~AFI_INTR_MASK_MSI_MASK;
1812 afi_writel(pcie, value, AFI_INTR_MASK);
1814 /* disable all MSI vectors */
1815 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1816 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1817 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1818 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1819 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1820 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1821 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1822 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1827 static void tegra_pcie_disable_interrupts(struct tegra_pcie *pcie)
1831 value = afi_readl(pcie, AFI_INTR_MASK);
1832 value &= ~AFI_INTR_MASK_INT_MASK;
1833 afi_writel(pcie, value, AFI_INTR_MASK);
1836 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1839 struct device *dev = pcie->dev;
1840 struct device_node *np = dev->of_node;
1842 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1845 dev_info(dev, "4x1, 1x1 configuration\n");
1846 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401;
1850 dev_info(dev, "2x1, 1X1, 1x1 configuration\n");
1851 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1855 dev_info(dev, "1x1, 1x1, 1x1 configuration\n");
1856 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111;
1860 dev_info(dev, "wrong configuration updated in DT, "
1861 "switching to default 2x1, 1x1, 1x1 "
1863 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1866 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1867 of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1870 dev_info(dev, "4x1, 1x1 configuration\n");
1871 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1875 dev_info(dev, "2x1, 1x1 configuration\n");
1876 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1879 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1882 dev_info(dev, "4x1, 2x1 configuration\n");
1883 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1887 dev_info(dev, "2x3 configuration\n");
1888 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1892 dev_info(dev, "4x1, 1x2 configuration\n");
1893 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1896 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1899 dev_info(dev, "single-mode configuration\n");
1900 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1904 dev_info(dev, "dual-mode configuration\n");
1905 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1914 * Check whether a given set of supplies is available in a device tree node.
1915 * This is used to check whether the new or the legacy device tree bindings
1918 static bool of_regulator_bulk_available(struct device_node *np,
1919 struct regulator_bulk_data *supplies,
1920 unsigned int num_supplies)
1925 for (i = 0; i < num_supplies; i++) {
1926 snprintf(property, 32, "%s-supply", supplies[i].supply);
1928 if (of_find_property(np, property, NULL) == NULL)
1936 * Old versions of the device tree binding for this device used a set of power
1937 * supplies that didn't match the hardware inputs. This happened to work for a
1938 * number of cases but is not future proof. However to preserve backwards-
1939 * compatibility with old device trees, this function will try to use the old
1942 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1944 struct device *dev = pcie->dev;
1945 struct device_node *np = dev->of_node;
1947 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1948 pcie->num_supplies = 3;
1949 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1950 pcie->num_supplies = 2;
1952 if (pcie->num_supplies == 0) {
1953 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1957 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1958 sizeof(*pcie->supplies),
1960 if (!pcie->supplies)
1963 pcie->supplies[0].supply = "pex-clk";
1964 pcie->supplies[1].supply = "vdd";
1966 if (pcie->num_supplies > 2)
1967 pcie->supplies[2].supply = "avdd";
1969 return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1973 * Obtains the list of regulators required for a particular generation of the
1976 * This would've been nice to do simply by providing static tables for use
1977 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1978 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1979 * and either seems to be optional depending on which ports are being used.
1981 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1983 struct device *dev = pcie->dev;
1984 struct device_node *np = dev->of_node;
1987 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1988 pcie->num_supplies = 4;
1990 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1991 sizeof(*pcie->supplies),
1993 if (!pcie->supplies)
1996 pcie->supplies[i++].supply = "dvdd-pex";
1997 pcie->supplies[i++].supply = "hvdd-pex-pll";
1998 pcie->supplies[i++].supply = "hvdd-pex";
1999 pcie->supplies[i++].supply = "vddio-pexctl-aud";
2000 } else if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
2001 pcie->num_supplies = 3;
2003 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
2004 sizeof(*pcie->supplies),
2006 if (!pcie->supplies)
2009 pcie->supplies[i++].supply = "hvddio-pex";
2010 pcie->supplies[i++].supply = "dvddio-pex";
2011 pcie->supplies[i++].supply = "vddio-pex-ctl";
2012 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
2013 pcie->num_supplies = 4;
2015 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2016 sizeof(*pcie->supplies),
2018 if (!pcie->supplies)
2021 pcie->supplies[i++].supply = "avddio-pex";
2022 pcie->supplies[i++].supply = "dvddio-pex";
2023 pcie->supplies[i++].supply = "hvdd-pex";
2024 pcie->supplies[i++].supply = "vddio-pex-ctl";
2025 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
2026 bool need_pexa = false, need_pexb = false;
2028 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
2029 if (lane_mask & 0x0f)
2032 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
2033 if (lane_mask & 0x30)
2036 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
2037 (need_pexb ? 2 : 0);
2039 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2040 sizeof(*pcie->supplies),
2042 if (!pcie->supplies)
2045 pcie->supplies[i++].supply = "avdd-pex-pll";
2046 pcie->supplies[i++].supply = "hvdd-pex";
2047 pcie->supplies[i++].supply = "vddio-pex-ctl";
2048 pcie->supplies[i++].supply = "avdd-plle";
2051 pcie->supplies[i++].supply = "avdd-pexa";
2052 pcie->supplies[i++].supply = "vdd-pexa";
2056 pcie->supplies[i++].supply = "avdd-pexb";
2057 pcie->supplies[i++].supply = "vdd-pexb";
2059 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
2060 pcie->num_supplies = 5;
2062 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
2063 sizeof(*pcie->supplies),
2065 if (!pcie->supplies)
2068 pcie->supplies[0].supply = "avdd-pex";
2069 pcie->supplies[1].supply = "vdd-pex";
2070 pcie->supplies[2].supply = "avdd-pex-pll";
2071 pcie->supplies[3].supply = "avdd-plle";
2072 pcie->supplies[4].supply = "vddio-pex-clk";
2075 if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
2076 pcie->num_supplies))
2077 return devm_regulator_bulk_get(dev, pcie->num_supplies,
2081 * If not all regulators are available for this new scheme, assume
2082 * that the device tree complies with an older version of the device
2085 dev_info(dev, "using legacy DT binding for power supplies\n");
2087 devm_kfree(dev, pcie->supplies);
2088 pcie->num_supplies = 0;
2090 return tegra_pcie_get_legacy_regulators(pcie);
2093 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
2095 struct device *dev = pcie->dev;
2096 struct device_node *np = dev->of_node, *port;
2097 const struct tegra_pcie_soc *soc = pcie->soc;
2098 u32 lanes = 0, mask = 0;
2099 unsigned int lane = 0;
2102 /* parse root ports */
2103 for_each_child_of_node(np, port) {
2104 struct tegra_pcie_port *rp;
2109 err = of_pci_get_devfn(port);
2111 dev_err(dev, "failed to parse address: %d\n", err);
2115 index = PCI_SLOT(err);
2117 if (index < 1 || index > soc->num_ports) {
2118 dev_err(dev, "invalid port number: %d\n", index);
2125 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
2127 dev_err(dev, "failed to parse # of lanes: %d\n",
2133 dev_err(dev, "invalid # of lanes: %u\n", value);
2138 lanes |= value << (index << 3);
2140 if (!of_device_is_available(port)) {
2145 mask |= ((1 << value) - 1) << lane;
2148 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
2154 err = of_address_to_resource(port, 0, &rp->regs);
2156 dev_err(dev, "failed to parse address: %d\n", err);
2160 INIT_LIST_HEAD(&rp->list);
2166 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
2167 if (IS_ERR(rp->base))
2168 return PTR_ERR(rp->base);
2170 label = devm_kasprintf(dev, GFP_KERNEL, "pex-reset-%u", index);
2172 dev_err(dev, "failed to create reset GPIO label\n");
2177 * Returns -ENOENT if reset-gpios property is not populated
2178 * and in this case fall back to using AFI per port register
2179 * to toggle PERST# SFIO line.
2181 rp->reset_gpio = devm_gpiod_get_from_of_node(dev, port,
2185 if (IS_ERR(rp->reset_gpio)) {
2186 if (PTR_ERR(rp->reset_gpio) == -ENOENT) {
2187 rp->reset_gpio = NULL;
2189 dev_err(dev, "failed to get reset GPIO: %ld\n",
2190 PTR_ERR(rp->reset_gpio));
2191 return PTR_ERR(rp->reset_gpio);
2195 list_add_tail(&rp->list, &pcie->ports);
2198 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
2200 dev_err(dev, "invalid lane configuration\n");
2204 err = tegra_pcie_get_regulators(pcie, mask);
2216 * FIXME: If there are no PCIe cards attached, then calling this function
2217 * can result in the increase of the bootup time as there are big timeout
2220 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
2221 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2223 struct device *dev = port->pcie->dev;
2224 unsigned int retries = 3;
2225 unsigned long value;
2227 /* override presence detection */
2228 value = readl(port->base + RP_PRIV_MISC);
2229 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2230 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2231 writel(value, port->base + RP_PRIV_MISC);
2234 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2237 value = readl(port->base + RP_VEND_XP);
2239 if (value & RP_VEND_XP_DL_UP)
2242 usleep_range(1000, 2000);
2243 } while (--timeout);
2246 dev_dbg(dev, "link %u down, retrying\n", port->index);
2250 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2253 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2255 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2258 usleep_range(1000, 2000);
2259 } while (--timeout);
2262 tegra_pcie_port_reset(port);
2263 } while (--retries);
2268 static void tegra_pcie_change_link_speed(struct tegra_pcie *pcie)
2270 struct device *dev = pcie->dev;
2271 struct tegra_pcie_port *port;
2275 list_for_each_entry(port, &pcie->ports, list) {
2277 * "Supported Link Speeds Vector" in "Link Capabilities 2"
2278 * is not supported by Tegra. tegra_pcie_change_link_speed()
2279 * is called only for Tegra chips which support Gen2.
2280 * So there no harm if supported link speed is not verified.
2282 value = readl(port->base + RP_LINK_CONTROL_STATUS_2);
2283 value &= ~PCI_EXP_LNKSTA_CLS;
2284 value |= PCI_EXP_LNKSTA_CLS_5_0GB;
2285 writel(value, port->base + RP_LINK_CONTROL_STATUS_2);
2288 * Poll until link comes back from recovery to avoid race
2291 deadline = ktime_add_us(ktime_get(), LINK_RETRAIN_TIMEOUT);
2293 while (ktime_before(ktime_get(), deadline)) {
2294 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2295 if ((value & PCI_EXP_LNKSTA_LT) == 0)
2298 usleep_range(2000, 3000);
2301 if (value & PCI_EXP_LNKSTA_LT)
2302 dev_warn(dev, "PCIe port %u link is in recovery\n",
2305 /* Retrain the link */
2306 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2307 value |= PCI_EXP_LNKCTL_RL;
2308 writel(value, port->base + RP_LINK_CONTROL_STATUS);
2310 deadline = ktime_add_us(ktime_get(), LINK_RETRAIN_TIMEOUT);
2312 while (ktime_before(ktime_get(), deadline)) {
2313 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2314 if ((value & PCI_EXP_LNKSTA_LT) == 0)
2317 usleep_range(2000, 3000);
2320 if (value & PCI_EXP_LNKSTA_LT)
2321 dev_err(dev, "failed to retrain link of port %u\n",
2326 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2328 struct device *dev = pcie->dev;
2329 struct tegra_pcie_port *port, *tmp;
2331 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2332 dev_info(dev, "probing port %u, using %u lanes\n",
2333 port->index, port->lanes);
2335 tegra_pcie_port_enable(port);
2338 /* Start LTSSM from Tegra side */
2339 reset_control_deassert(pcie->pcie_xrst);
2341 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2342 if (tegra_pcie_port_check_link(port))
2345 dev_info(dev, "link %u down, ignoring\n", port->index);
2347 tegra_pcie_port_disable(port);
2348 tegra_pcie_port_free(port);
2351 if (pcie->soc->has_gen2)
2352 tegra_pcie_change_link_speed(pcie);
2355 static void tegra_pcie_disable_ports(struct tegra_pcie *pcie)
2357 struct tegra_pcie_port *port, *tmp;
2359 reset_control_assert(pcie->pcie_xrst);
2361 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2362 tegra_pcie_port_disable(port);
2365 static const struct tegra_pcie_port_soc tegra20_pcie_ports[] = {
2366 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2367 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2370 static const struct tegra_pcie_soc tegra20_pcie = {
2372 .ports = tegra20_pcie_ports,
2373 .msi_base_shift = 0,
2374 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2375 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2376 .pads_refclk_cfg0 = 0xfa5cfa5c,
2377 .has_pex_clkreq_en = false,
2378 .has_pex_bias_ctrl = false,
2379 .has_intr_prsnt_sense = false,
2380 .has_cml_clk = false,
2382 .force_pca_enable = false,
2383 .program_uphy = true,
2384 .update_clamp_threshold = false,
2385 .program_deskew_time = false,
2386 .update_fc_timer = false,
2387 .has_cache_bars = true,
2388 .ectl.enable = false,
2391 static const struct tegra_pcie_port_soc tegra30_pcie_ports[] = {
2392 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2393 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2394 { .pme.turnoff_bit = 16, .pme.ack_bit = 18 },
2397 static const struct tegra_pcie_soc tegra30_pcie = {
2399 .ports = tegra30_pcie_ports,
2400 .msi_base_shift = 8,
2401 .afi_pex2_ctrl = 0x128,
2402 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2403 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2404 .pads_refclk_cfg0 = 0xfa5cfa5c,
2405 .pads_refclk_cfg1 = 0xfa5cfa5c,
2406 .has_pex_clkreq_en = true,
2407 .has_pex_bias_ctrl = true,
2408 .has_intr_prsnt_sense = true,
2409 .has_cml_clk = true,
2411 .force_pca_enable = false,
2412 .program_uphy = true,
2413 .update_clamp_threshold = false,
2414 .program_deskew_time = false,
2415 .update_fc_timer = false,
2416 .has_cache_bars = false,
2417 .ectl.enable = false,
2420 static const struct tegra_pcie_soc tegra124_pcie = {
2422 .ports = tegra20_pcie_ports,
2423 .msi_base_shift = 8,
2424 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2425 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2426 .pads_refclk_cfg0 = 0x44ac44ac,
2427 .has_pex_clkreq_en = true,
2428 .has_pex_bias_ctrl = true,
2429 .has_intr_prsnt_sense = true,
2430 .has_cml_clk = true,
2432 .force_pca_enable = false,
2433 .program_uphy = true,
2434 .update_clamp_threshold = true,
2435 .program_deskew_time = false,
2436 .update_fc_timer = false,
2437 .has_cache_bars = false,
2438 .ectl.enable = false,
2441 static const struct tegra_pcie_soc tegra210_pcie = {
2443 .ports = tegra20_pcie_ports,
2444 .msi_base_shift = 8,
2445 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2446 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2447 .pads_refclk_cfg0 = 0x90b890b8,
2448 /* FC threshold is bit[25:18] */
2449 .update_fc_threshold = 0x01800000,
2450 .has_pex_clkreq_en = true,
2451 .has_pex_bias_ctrl = true,
2452 .has_intr_prsnt_sense = true,
2453 .has_cml_clk = true,
2455 .force_pca_enable = true,
2456 .program_uphy = true,
2457 .update_clamp_threshold = true,
2458 .program_deskew_time = true,
2459 .update_fc_timer = true,
2460 .has_cache_bars = false,
2463 .rp_ectl_2_r1 = 0x0000000f,
2464 .rp_ectl_4_r1 = 0x00000067,
2465 .rp_ectl_5_r1 = 0x55010000,
2466 .rp_ectl_6_r1 = 0x00000001,
2467 .rp_ectl_2_r2 = 0x0000008f,
2468 .rp_ectl_4_r2 = 0x000000c7,
2469 .rp_ectl_5_r2 = 0x55010000,
2470 .rp_ectl_6_r2 = 0x00000001,
2476 static const struct tegra_pcie_port_soc tegra186_pcie_ports[] = {
2477 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2478 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2479 { .pme.turnoff_bit = 12, .pme.ack_bit = 14 },
2482 static const struct tegra_pcie_soc tegra186_pcie = {
2484 .ports = tegra186_pcie_ports,
2485 .msi_base_shift = 8,
2486 .afi_pex2_ctrl = 0x19c,
2487 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2488 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2489 .pads_refclk_cfg0 = 0x80b880b8,
2490 .pads_refclk_cfg1 = 0x000480b8,
2491 .has_pex_clkreq_en = true,
2492 .has_pex_bias_ctrl = true,
2493 .has_intr_prsnt_sense = true,
2494 .has_cml_clk = false,
2496 .force_pca_enable = false,
2497 .program_uphy = false,
2498 .update_clamp_threshold = false,
2499 .program_deskew_time = false,
2500 .update_fc_timer = false,
2501 .has_cache_bars = false,
2502 .ectl.enable = false,
2505 static const struct of_device_id tegra_pcie_of_match[] = {
2506 { .compatible = "nvidia,tegra186-pcie", .data = &tegra186_pcie },
2507 { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2508 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2509 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2510 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2514 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2516 struct tegra_pcie *pcie = s->private;
2518 if (list_empty(&pcie->ports))
2521 seq_printf(s, "Index Status\n");
2523 return seq_list_start(&pcie->ports, *pos);
2526 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2528 struct tegra_pcie *pcie = s->private;
2530 return seq_list_next(v, &pcie->ports, pos);
2533 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2537 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2539 bool up = false, active = false;
2540 struct tegra_pcie_port *port;
2543 port = list_entry(v, struct tegra_pcie_port, list);
2545 value = readl(port->base + RP_VEND_XP);
2547 if (value & RP_VEND_XP_DL_UP)
2550 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2552 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2555 seq_printf(s, "%2u ", port->index);
2558 seq_printf(s, "up");
2562 seq_printf(s, ", ");
2564 seq_printf(s, "active");
2567 seq_printf(s, "\n");
2571 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2572 .start = tegra_pcie_ports_seq_start,
2573 .next = tegra_pcie_ports_seq_next,
2574 .stop = tegra_pcie_ports_seq_stop,
2575 .show = tegra_pcie_ports_seq_show,
2578 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2580 struct tegra_pcie *pcie = inode->i_private;
2584 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2588 s = file->private_data;
2594 static const struct file_operations tegra_pcie_ports_ops = {
2595 .owner = THIS_MODULE,
2596 .open = tegra_pcie_ports_open,
2598 .llseek = seq_lseek,
2599 .release = seq_release,
2602 static void tegra_pcie_debugfs_exit(struct tegra_pcie *pcie)
2604 debugfs_remove_recursive(pcie->debugfs);
2605 pcie->debugfs = NULL;
2608 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2610 struct dentry *file;
2612 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2616 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2617 pcie, &tegra_pcie_ports_ops);
2624 tegra_pcie_debugfs_exit(pcie);
2628 static int tegra_pcie_probe(struct platform_device *pdev)
2630 struct device *dev = &pdev->dev;
2631 struct pci_host_bridge *host;
2632 struct tegra_pcie *pcie;
2635 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2639 pcie = pci_host_bridge_priv(host);
2640 host->sysdata = pcie;
2641 platform_set_drvdata(pdev, pcie);
2643 pcie->soc = of_device_get_match_data(dev);
2644 INIT_LIST_HEAD(&pcie->ports);
2647 err = tegra_pcie_parse_dt(pcie);
2651 err = tegra_pcie_get_resources(pcie);
2653 dev_err(dev, "failed to request resources: %d\n", err);
2657 err = tegra_pcie_msi_setup(pcie);
2659 dev_err(dev, "failed to enable MSI support: %d\n", err);
2663 pm_runtime_enable(pcie->dev);
2664 err = pm_runtime_get_sync(pcie->dev);
2666 dev_err(dev, "fail to enable pcie controller: %d\n", err);
2667 goto pm_runtime_put;
2670 host->ops = &tegra_pcie_ops;
2671 host->map_irq = tegra_pcie_map_irq;
2673 err = pci_host_probe(host);
2675 dev_err(dev, "failed to register host: %d\n", err);
2676 goto pm_runtime_put;
2679 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2680 err = tegra_pcie_debugfs_init(pcie);
2682 dev_err(dev, "failed to setup debugfs: %d\n", err);
2688 pm_runtime_put_sync(pcie->dev);
2689 pm_runtime_disable(pcie->dev);
2690 tegra_pcie_msi_teardown(pcie);
2692 tegra_pcie_put_resources(pcie);
2696 static int tegra_pcie_remove(struct platform_device *pdev)
2698 struct tegra_pcie *pcie = platform_get_drvdata(pdev);
2699 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
2700 struct tegra_pcie_port *port, *tmp;
2702 if (IS_ENABLED(CONFIG_DEBUG_FS))
2703 tegra_pcie_debugfs_exit(pcie);
2705 pci_stop_root_bus(host->bus);
2706 pci_remove_root_bus(host->bus);
2707 pm_runtime_put_sync(pcie->dev);
2708 pm_runtime_disable(pcie->dev);
2710 if (IS_ENABLED(CONFIG_PCI_MSI))
2711 tegra_pcie_msi_teardown(pcie);
2713 tegra_pcie_put_resources(pcie);
2715 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2716 tegra_pcie_port_free(port);
2721 static int __maybe_unused tegra_pcie_pm_suspend(struct device *dev)
2723 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2724 struct tegra_pcie_port *port;
2727 list_for_each_entry(port, &pcie->ports, list)
2728 tegra_pcie_pme_turnoff(port);
2730 tegra_pcie_disable_ports(pcie);
2733 * AFI_INTR is unmasked in tegra_pcie_enable_controller(), mask it to
2734 * avoid unwanted interrupts raised by AFI after pex_rst is asserted.
2736 tegra_pcie_disable_interrupts(pcie);
2738 if (pcie->soc->program_uphy) {
2739 err = tegra_pcie_phy_power_off(pcie);
2741 dev_err(dev, "failed to power off PHY(s): %d\n", err);
2744 reset_control_assert(pcie->pex_rst);
2745 clk_disable_unprepare(pcie->pex_clk);
2747 if (IS_ENABLED(CONFIG_PCI_MSI))
2748 tegra_pcie_disable_msi(pcie);
2750 pinctrl_pm_select_idle_state(dev);
2751 tegra_pcie_power_off(pcie);
2756 static int __maybe_unused tegra_pcie_pm_resume(struct device *dev)
2758 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2761 err = tegra_pcie_power_on(pcie);
2763 dev_err(dev, "tegra pcie power on fail: %d\n", err);
2767 err = pinctrl_pm_select_default_state(dev);
2769 dev_err(dev, "failed to disable PCIe IO DPD: %d\n", err);
2773 tegra_pcie_enable_controller(pcie);
2774 tegra_pcie_setup_translations(pcie);
2776 if (IS_ENABLED(CONFIG_PCI_MSI))
2777 tegra_pcie_enable_msi(pcie);
2779 err = clk_prepare_enable(pcie->pex_clk);
2781 dev_err(dev, "failed to enable PEX clock: %d\n", err);
2782 goto pex_dpd_enable;
2785 reset_control_deassert(pcie->pex_rst);
2787 if (pcie->soc->program_uphy) {
2788 err = tegra_pcie_phy_power_on(pcie);
2790 dev_err(dev, "failed to power on PHY(s): %d\n", err);
2791 goto disable_pex_clk;
2795 tegra_pcie_apply_pad_settings(pcie);
2796 tegra_pcie_enable_ports(pcie);
2801 reset_control_assert(pcie->pex_rst);
2802 clk_disable_unprepare(pcie->pex_clk);
2804 pinctrl_pm_select_idle_state(dev);
2806 tegra_pcie_power_off(pcie);
2811 static const struct dev_pm_ops tegra_pcie_pm_ops = {
2812 SET_RUNTIME_PM_OPS(tegra_pcie_pm_suspend, tegra_pcie_pm_resume, NULL)
2813 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_pcie_pm_suspend,
2814 tegra_pcie_pm_resume)
2817 static struct platform_driver tegra_pcie_driver = {
2819 .name = "tegra-pcie",
2820 .of_match_table = tegra_pcie_of_match,
2821 .suppress_bind_attrs = true,
2822 .pm = &tegra_pcie_pm_ops,
2824 .probe = tegra_pcie_probe,
2825 .remove = tegra_pcie_remove,
2827 module_platform_driver(tegra_pcie_driver);
2828 MODULE_LICENSE("GPL");