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/interrupt.h>
21 #include <linux/iopoll.h>
22 #include <linux/irq.h>
23 #include <linux/irqdomain.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/msi.h>
28 #include <linux/of_address.h>
29 #include <linux/of_pci.h>
30 #include <linux/of_platform.h>
31 #include <linux/pci.h>
32 #include <linux/phy/phy.h>
33 #include <linux/platform_device.h>
34 #include <linux/reset.h>
35 #include <linux/sizes.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/regulator/consumer.h>
40 #include <soc/tegra/cpuidle.h>
41 #include <soc/tegra/pmc.h>
45 #define INT_PCI_MSI_NR (8 * 32)
47 /* register definitions */
49 #define AFI_AXI_BAR0_SZ 0x00
50 #define AFI_AXI_BAR1_SZ 0x04
51 #define AFI_AXI_BAR2_SZ 0x08
52 #define AFI_AXI_BAR3_SZ 0x0c
53 #define AFI_AXI_BAR4_SZ 0x10
54 #define AFI_AXI_BAR5_SZ 0x14
56 #define AFI_AXI_BAR0_START 0x18
57 #define AFI_AXI_BAR1_START 0x1c
58 #define AFI_AXI_BAR2_START 0x20
59 #define AFI_AXI_BAR3_START 0x24
60 #define AFI_AXI_BAR4_START 0x28
61 #define AFI_AXI_BAR5_START 0x2c
63 #define AFI_FPCI_BAR0 0x30
64 #define AFI_FPCI_BAR1 0x34
65 #define AFI_FPCI_BAR2 0x38
66 #define AFI_FPCI_BAR3 0x3c
67 #define AFI_FPCI_BAR4 0x40
68 #define AFI_FPCI_BAR5 0x44
70 #define AFI_CACHE_BAR0_SZ 0x48
71 #define AFI_CACHE_BAR0_ST 0x4c
72 #define AFI_CACHE_BAR1_SZ 0x50
73 #define AFI_CACHE_BAR1_ST 0x54
75 #define AFI_MSI_BAR_SZ 0x60
76 #define AFI_MSI_FPCI_BAR_ST 0x64
77 #define AFI_MSI_AXI_BAR_ST 0x68
79 #define AFI_MSI_VEC0 0x6c
80 #define AFI_MSI_VEC1 0x70
81 #define AFI_MSI_VEC2 0x74
82 #define AFI_MSI_VEC3 0x78
83 #define AFI_MSI_VEC4 0x7c
84 #define AFI_MSI_VEC5 0x80
85 #define AFI_MSI_VEC6 0x84
86 #define AFI_MSI_VEC7 0x88
88 #define AFI_MSI_EN_VEC0 0x8c
89 #define AFI_MSI_EN_VEC1 0x90
90 #define AFI_MSI_EN_VEC2 0x94
91 #define AFI_MSI_EN_VEC3 0x98
92 #define AFI_MSI_EN_VEC4 0x9c
93 #define AFI_MSI_EN_VEC5 0xa0
94 #define AFI_MSI_EN_VEC6 0xa4
95 #define AFI_MSI_EN_VEC7 0xa8
97 #define AFI_CONFIGURATION 0xac
98 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
100 #define AFI_FPCI_ERROR_MASKS 0xb0
102 #define AFI_INTR_MASK 0xb4
103 #define AFI_INTR_MASK_INT_MASK (1 << 0)
104 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
106 #define AFI_INTR_CODE 0xb8
107 #define AFI_INTR_CODE_MASK 0xf
108 #define AFI_INTR_INI_SLAVE_ERROR 1
109 #define AFI_INTR_INI_DECODE_ERROR 2
110 #define AFI_INTR_TARGET_ABORT 3
111 #define AFI_INTR_MASTER_ABORT 4
112 #define AFI_INTR_INVALID_WRITE 5
113 #define AFI_INTR_LEGACY 6
114 #define AFI_INTR_FPCI_DECODE_ERROR 7
115 #define AFI_INTR_AXI_DECODE_ERROR 8
116 #define AFI_INTR_FPCI_TIMEOUT 9
117 #define AFI_INTR_PE_PRSNT_SENSE 10
118 #define AFI_INTR_PE_CLKREQ_SENSE 11
119 #define AFI_INTR_CLKCLAMP_SENSE 12
120 #define AFI_INTR_RDY4PD_SENSE 13
121 #define AFI_INTR_P2P_ERROR 14
123 #define AFI_INTR_SIGNATURE 0xbc
124 #define AFI_UPPER_FPCI_ADDRESS 0xc0
125 #define AFI_SM_INTR_ENABLE 0xc4
126 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
127 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
128 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
129 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
130 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
131 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
132 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
133 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
135 #define AFI_AFI_INTR_ENABLE 0xc8
136 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
137 #define AFI_INTR_EN_INI_DECERR (1 << 1)
138 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
139 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
140 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
141 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
142 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
143 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
144 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
146 #define AFI_PCIE_PME 0xf0
148 #define AFI_PCIE_CONFIG 0x0f8
149 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
150 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
151 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
152 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
153 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
154 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
155 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401 (0x0 << 20)
156 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
157 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
158 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
159 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211 (0x1 << 20)
160 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
161 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111 (0x2 << 20)
163 #define AFI_FUSE 0x104
164 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
166 #define AFI_PEX0_CTRL 0x110
167 #define AFI_PEX1_CTRL 0x118
168 #define AFI_PEX2_CTRL 0x128
169 #define AFI_PEX_CTRL_RST (1 << 0)
170 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
171 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
172 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
174 #define AFI_PLLE_CONTROL 0x160
175 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
176 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
178 #define AFI_PEXBIAS_CTRL_0 0x168
180 #define RP_VEND_XP 0x00000f00
181 #define RP_VEND_XP_DL_UP (1 << 30)
183 #define RP_VEND_CTL2 0x00000fa8
184 #define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
186 #define RP_PRIV_MISC 0x00000fe0
187 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
188 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
190 #define RP_LINK_CONTROL_STATUS 0x00000090
191 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
192 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
194 #define PADS_CTL_SEL 0x0000009c
196 #define PADS_CTL 0x000000a0
197 #define PADS_CTL_IDDQ_1L (1 << 0)
198 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
199 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
201 #define PADS_PLL_CTL_TEGRA20 0x000000b8
202 #define PADS_PLL_CTL_TEGRA30 0x000000b4
203 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
204 #define PADS_PLL_CTL_LOCKDET (1 << 8)
205 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
206 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
207 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
208 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
209 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
210 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
211 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
212 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
214 #define PADS_REFCLK_CFG0 0x000000c8
215 #define PADS_REFCLK_CFG1 0x000000cc
216 #define PADS_REFCLK_BIAS 0x000000d0
219 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
220 * entries, one entry per PCIe port. These field definitions and desired
221 * values aren't in the TRM, but do come from NVIDIA.
223 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
224 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
225 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
226 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
228 #define PME_ACK_TIMEOUT 10000
231 struct msi_controller chip;
232 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
233 struct irq_domain *domain;
240 /* used to differentiate between Tegra SoC generations */
241 struct tegra_pcie_port_soc {
248 struct tegra_pcie_soc {
249 unsigned int num_ports;
250 const struct tegra_pcie_port_soc *ports;
251 unsigned int msi_base_shift;
254 u32 pads_refclk_cfg0;
255 u32 pads_refclk_cfg1;
256 bool has_pex_clkreq_en;
257 bool has_pex_bias_ctrl;
258 bool has_intr_prsnt_sense;
261 bool force_pca_enable;
265 static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
267 return container_of(chip, struct tegra_msi, chip);
282 struct resource prefetch;
283 struct resource busn;
295 struct reset_control *pex_rst;
296 struct reset_control *afi_rst;
297 struct reset_control *pcie_xrst;
302 struct tegra_msi msi;
304 struct list_head ports;
307 struct regulator_bulk_data *supplies;
308 unsigned int num_supplies;
310 const struct tegra_pcie_soc *soc;
311 struct dentry *debugfs;
314 struct tegra_pcie_port {
315 struct tegra_pcie *pcie;
316 struct device_node *np;
317 struct list_head list;
318 struct resource regs;
326 struct tegra_pcie_bus {
327 struct list_head list;
331 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
332 unsigned long offset)
334 writel(value, pcie->afi + offset);
337 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
339 return readl(pcie->afi + offset);
342 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
343 unsigned long offset)
345 writel(value, pcie->pads + offset);
348 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
350 return readl(pcie->pads + offset);
354 * The configuration space mapping on Tegra is somewhat similar to the ECAM
355 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
356 * register accesses are mapped:
358 * [27:24] extended register number
360 * [15:11] device number
361 * [10: 8] function number
362 * [ 7: 0] register number
364 * Mapping the whole extended configuration space would require 256 MiB of
365 * virtual address space, only a small part of which will actually be used.
367 * To work around this, a 4 KiB region is used to generate the required
368 * configuration transaction with relevant B:D:F and register offset values.
369 * This is achieved by dynamically programming base address and size of
370 * AFI_AXI_BAR used for end point config space mapping to make sure that the
371 * address (access to which generates correct config transaction) falls in
374 static unsigned int tegra_pcie_conf_offset(u8 bus, unsigned int devfn,
377 return ((where & 0xf00) << 16) | (bus << 16) | (PCI_SLOT(devfn) << 11) |
378 (PCI_FUNC(devfn) << 8) | (where & 0xff);
381 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
385 struct tegra_pcie *pcie = bus->sysdata;
386 void __iomem *addr = NULL;
388 if (bus->number == 0) {
389 unsigned int slot = PCI_SLOT(devfn);
390 struct tegra_pcie_port *port;
392 list_for_each_entry(port, &pcie->ports, list) {
393 if (port->index + 1 == slot) {
394 addr = port->base + (where & ~3);
402 offset = tegra_pcie_conf_offset(bus->number, devfn, where);
404 /* move 4 KiB window to offset within the FPCI region */
405 base = 0xfe100000 + ((offset & ~(SZ_4K - 1)) >> 8);
406 afi_writel(pcie, base, AFI_FPCI_BAR0);
408 /* move to correct offset within the 4 KiB page */
409 addr = pcie->cfg + (offset & (SZ_4K - 1));
415 static int tegra_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
416 int where, int size, u32 *value)
418 if (bus->number == 0)
419 return pci_generic_config_read32(bus, devfn, where, size,
422 return pci_generic_config_read(bus, devfn, where, size, value);
425 static int tegra_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
426 int where, int size, u32 value)
428 if (bus->number == 0)
429 return pci_generic_config_write32(bus, devfn, where, size,
432 return pci_generic_config_write(bus, devfn, where, size, value);
435 static struct pci_ops tegra_pcie_ops = {
436 .map_bus = tegra_pcie_map_bus,
437 .read = tegra_pcie_config_read,
438 .write = tegra_pcie_config_write,
441 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
443 unsigned long ret = 0;
445 switch (port->index) {
462 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
464 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
467 /* pulse reset signal */
468 value = afi_readl(port->pcie, ctrl);
469 value &= ~AFI_PEX_CTRL_RST;
470 afi_writel(port->pcie, value, ctrl);
472 usleep_range(1000, 2000);
474 value = afi_readl(port->pcie, ctrl);
475 value |= AFI_PEX_CTRL_RST;
476 afi_writel(port->pcie, value, ctrl);
479 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
481 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
482 const struct tegra_pcie_soc *soc = port->pcie->soc;
485 /* enable reference clock */
486 value = afi_readl(port->pcie, ctrl);
487 value |= AFI_PEX_CTRL_REFCLK_EN;
489 if (soc->has_pex_clkreq_en)
490 value |= AFI_PEX_CTRL_CLKREQ_EN;
492 value |= AFI_PEX_CTRL_OVERRIDE_EN;
494 afi_writel(port->pcie, value, ctrl);
496 tegra_pcie_port_reset(port);
498 if (soc->force_pca_enable) {
499 value = readl(port->base + RP_VEND_CTL2);
500 value |= RP_VEND_CTL2_PCA_ENABLE;
501 writel(value, port->base + RP_VEND_CTL2);
505 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
507 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
508 const struct tegra_pcie_soc *soc = port->pcie->soc;
511 /* assert port reset */
512 value = afi_readl(port->pcie, ctrl);
513 value &= ~AFI_PEX_CTRL_RST;
514 afi_writel(port->pcie, value, ctrl);
516 /* disable reference clock */
517 value = afi_readl(port->pcie, ctrl);
519 if (soc->has_pex_clkreq_en)
520 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
522 value &= ~AFI_PEX_CTRL_REFCLK_EN;
523 afi_writel(port->pcie, value, ctrl);
526 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
528 struct tegra_pcie *pcie = port->pcie;
529 struct device *dev = pcie->dev;
531 devm_iounmap(dev, port->base);
532 devm_release_mem_region(dev, port->regs.start,
533 resource_size(&port->regs));
534 list_del(&port->list);
535 devm_kfree(dev, port);
538 /* Tegra PCIE root complex wrongly reports device class */
539 static void tegra_pcie_fixup_class(struct pci_dev *dev)
541 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
543 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
544 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
545 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
546 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
548 /* Tegra PCIE requires relaxed ordering */
549 static void tegra_pcie_relax_enable(struct pci_dev *dev)
551 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
553 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
555 static int tegra_pcie_request_resources(struct tegra_pcie *pcie)
557 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
558 struct list_head *windows = &host->windows;
559 struct device *dev = pcie->dev;
562 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
563 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
564 pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem);
565 pci_add_resource(windows, &pcie->busn);
567 err = devm_request_pci_bus_resources(dev, windows);
569 pci_free_resource_list(windows);
573 pci_remap_iospace(&pcie->pio, pcie->io.start);
578 static void tegra_pcie_free_resources(struct tegra_pcie *pcie)
580 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
581 struct list_head *windows = &host->windows;
583 pci_unmap_iospace(&pcie->pio);
584 pci_free_resource_list(windows);
587 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
589 struct tegra_pcie *pcie = pdev->bus->sysdata;
592 tegra_cpuidle_pcie_irqs_in_use();
594 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
601 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
603 const char *err_msg[] = {
611 "Response decoding error",
612 "AXI response decoding error",
613 "Transaction timeout",
614 "Slot present pin change",
615 "Slot clock request change",
616 "TMS clock ramp change",
617 "TMS ready for power down",
620 struct tegra_pcie *pcie = arg;
621 struct device *dev = pcie->dev;
624 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
625 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
626 afi_writel(pcie, 0, AFI_INTR_CODE);
628 if (code == AFI_INTR_LEGACY)
631 if (code >= ARRAY_SIZE(err_msg))
635 * do not pollute kernel log with master abort reports since they
636 * happen a lot during enumeration
638 if (code == AFI_INTR_MASTER_ABORT)
639 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
641 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
643 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
644 code == AFI_INTR_FPCI_DECODE_ERROR) {
645 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
646 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
648 if (code == AFI_INTR_MASTER_ABORT)
649 dev_dbg(dev, " FPCI address: %10llx\n", address);
651 dev_err(dev, " FPCI address: %10llx\n", address);
658 * FPCI map is as follows:
659 * - 0xfdfc000000: I/O space
660 * - 0xfdfe000000: type 0 configuration space
661 * - 0xfdff000000: type 1 configuration space
662 * - 0xfe00000000: type 0 extended configuration space
663 * - 0xfe10000000: type 1 extended configuration space
665 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
667 u32 fpci_bar, size, axi_address;
669 /* Bar 0: type 1 extended configuration space */
670 size = resource_size(&pcie->cs);
671 afi_writel(pcie, pcie->cs.start, AFI_AXI_BAR0_START);
672 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
674 /* Bar 1: downstream IO bar */
675 fpci_bar = 0xfdfc0000;
676 size = resource_size(&pcie->io);
677 axi_address = pcie->io.start;
678 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
679 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
680 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
682 /* Bar 2: prefetchable memory BAR */
683 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
684 size = resource_size(&pcie->prefetch);
685 axi_address = pcie->prefetch.start;
686 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
687 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
688 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
690 /* Bar 3: non prefetchable memory BAR */
691 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
692 size = resource_size(&pcie->mem);
693 axi_address = pcie->mem.start;
694 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
695 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
696 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
698 /* NULL out the remaining BARs as they are not used */
699 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
700 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
701 afi_writel(pcie, 0, AFI_FPCI_BAR4);
703 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
704 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
705 afi_writel(pcie, 0, AFI_FPCI_BAR5);
707 /* map all upstream transactions as uncached */
708 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
709 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
710 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
711 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
713 /* MSI translations are setup only when needed */
714 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
715 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
716 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
717 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
720 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
722 const struct tegra_pcie_soc *soc = pcie->soc;
725 timeout = jiffies + msecs_to_jiffies(timeout);
727 while (time_before(jiffies, timeout)) {
728 value = pads_readl(pcie, soc->pads_pll_ctl);
729 if (value & PADS_PLL_CTL_LOCKDET)
736 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
738 struct device *dev = pcie->dev;
739 const struct tegra_pcie_soc *soc = pcie->soc;
743 /* initialize internal PHY, enable up to 16 PCIE lanes */
744 pads_writel(pcie, 0x0, PADS_CTL_SEL);
746 /* override IDDQ to 1 on all 4 lanes */
747 value = pads_readl(pcie, PADS_CTL);
748 value |= PADS_CTL_IDDQ_1L;
749 pads_writel(pcie, value, PADS_CTL);
752 * Set up PHY PLL inputs select PLLE output as refclock,
753 * set TX ref sel to div10 (not div5).
755 value = pads_readl(pcie, soc->pads_pll_ctl);
756 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
757 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
758 pads_writel(pcie, value, soc->pads_pll_ctl);
761 value = pads_readl(pcie, soc->pads_pll_ctl);
762 value &= ~PADS_PLL_CTL_RST_B4SM;
763 pads_writel(pcie, value, soc->pads_pll_ctl);
765 usleep_range(20, 100);
767 /* take PLL out of reset */
768 value = pads_readl(pcie, soc->pads_pll_ctl);
769 value |= PADS_PLL_CTL_RST_B4SM;
770 pads_writel(pcie, value, soc->pads_pll_ctl);
772 /* wait for the PLL to lock */
773 err = tegra_pcie_pll_wait(pcie, 500);
775 dev_err(dev, "PLL failed to lock: %d\n", err);
779 /* turn off IDDQ override */
780 value = pads_readl(pcie, PADS_CTL);
781 value &= ~PADS_CTL_IDDQ_1L;
782 pads_writel(pcie, value, PADS_CTL);
784 /* enable TX/RX data */
785 value = pads_readl(pcie, PADS_CTL);
786 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
787 pads_writel(pcie, value, PADS_CTL);
792 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
794 const struct tegra_pcie_soc *soc = pcie->soc;
797 /* disable TX/RX data */
798 value = pads_readl(pcie, PADS_CTL);
799 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
800 pads_writel(pcie, value, PADS_CTL);
803 value = pads_readl(pcie, PADS_CTL);
804 value |= PADS_CTL_IDDQ_1L;
805 pads_writel(pcie, value, PADS_CTL);
808 value = pads_readl(pcie, soc->pads_pll_ctl);
809 value &= ~PADS_PLL_CTL_RST_B4SM;
810 pads_writel(pcie, value, soc->pads_pll_ctl);
812 usleep_range(20, 100);
817 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
819 struct device *dev = port->pcie->dev;
823 for (i = 0; i < port->lanes; i++) {
824 err = phy_power_on(port->phys[i]);
826 dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
834 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
836 struct device *dev = port->pcie->dev;
840 for (i = 0; i < port->lanes; i++) {
841 err = phy_power_off(port->phys[i]);
843 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
852 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
854 struct device *dev = pcie->dev;
855 const struct tegra_pcie_soc *soc = pcie->soc;
856 struct tegra_pcie_port *port;
859 if (pcie->legacy_phy) {
861 err = phy_power_on(pcie->phy);
863 err = tegra_pcie_phy_enable(pcie);
866 dev_err(dev, "failed to power on PHY: %d\n", err);
871 list_for_each_entry(port, &pcie->ports, list) {
872 err = tegra_pcie_port_phy_power_on(port);
875 "failed to power on PCIe port %u PHY: %d\n",
881 /* Configure the reference clock driver */
882 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
884 if (soc->num_ports > 2)
885 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
890 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
892 struct device *dev = pcie->dev;
893 struct tegra_pcie_port *port;
896 if (pcie->legacy_phy) {
898 err = phy_power_off(pcie->phy);
900 err = tegra_pcie_phy_disable(pcie);
903 dev_err(dev, "failed to power off PHY: %d\n", err);
908 list_for_each_entry(port, &pcie->ports, list) {
909 err = tegra_pcie_port_phy_power_off(port);
912 "failed to power off PCIe port %u PHY: %d\n",
921 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
923 struct device *dev = pcie->dev;
924 const struct tegra_pcie_soc *soc = pcie->soc;
925 struct tegra_pcie_port *port;
929 /* enable PLL power down */
931 value = afi_readl(pcie, AFI_PLLE_CONTROL);
932 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
933 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
934 afi_writel(pcie, value, AFI_PLLE_CONTROL);
937 /* power down PCIe slot clock bias pad */
938 if (soc->has_pex_bias_ctrl)
939 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
941 /* configure mode and disable all ports */
942 value = afi_readl(pcie, AFI_PCIE_CONFIG);
943 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
944 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
946 list_for_each_entry(port, &pcie->ports, list)
947 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
949 afi_writel(pcie, value, AFI_PCIE_CONFIG);
952 value = afi_readl(pcie, AFI_FUSE);
953 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
954 afi_writel(pcie, value, AFI_FUSE);
956 value = afi_readl(pcie, AFI_FUSE);
957 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
958 afi_writel(pcie, value, AFI_FUSE);
961 if (soc->program_uphy) {
962 err = tegra_pcie_phy_power_on(pcie);
964 dev_err(dev, "failed to power on PHY(s): %d\n", err);
969 /* take the PCIe interface module out of reset */
970 reset_control_deassert(pcie->pcie_xrst);
972 /* finally enable PCIe */
973 value = afi_readl(pcie, AFI_CONFIGURATION);
974 value |= AFI_CONFIGURATION_EN_FPCI;
975 afi_writel(pcie, value, AFI_CONFIGURATION);
977 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
978 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
979 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
981 if (soc->has_intr_prsnt_sense)
982 value |= AFI_INTR_EN_PRSNT_SENSE;
984 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
985 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
987 /* don't enable MSI for now, only when needed */
988 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
990 /* disable all exceptions */
991 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
996 static void tegra_pcie_disable_controller(struct tegra_pcie *pcie)
1000 reset_control_assert(pcie->pcie_xrst);
1002 if (pcie->soc->program_uphy) {
1003 err = tegra_pcie_phy_power_off(pcie);
1005 dev_err(pcie->dev, "failed to power off PHY(s): %d\n",
1010 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1012 struct device *dev = pcie->dev;
1013 const struct tegra_pcie_soc *soc = pcie->soc;
1016 reset_control_assert(pcie->afi_rst);
1017 reset_control_assert(pcie->pex_rst);
1019 clk_disable_unprepare(pcie->pll_e);
1020 if (soc->has_cml_clk)
1021 clk_disable_unprepare(pcie->cml_clk);
1022 clk_disable_unprepare(pcie->afi_clk);
1023 clk_disable_unprepare(pcie->pex_clk);
1025 if (!dev->pm_domain)
1026 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1028 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1030 dev_warn(dev, "failed to disable regulators: %d\n", err);
1033 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1035 struct device *dev = pcie->dev;
1036 const struct tegra_pcie_soc *soc = pcie->soc;
1039 reset_control_assert(pcie->pcie_xrst);
1040 reset_control_assert(pcie->afi_rst);
1041 reset_control_assert(pcie->pex_rst);
1043 if (!dev->pm_domain)
1044 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1046 /* enable regulators */
1047 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1049 dev_err(dev, "failed to enable regulators: %d\n", err);
1051 if (dev->pm_domain) {
1052 err = clk_prepare_enable(pcie->pex_clk);
1054 dev_err(dev, "failed to enable PEX clock: %d\n", err);
1057 reset_control_deassert(pcie->pex_rst);
1059 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
1063 dev_err(dev, "powerup sequence failed: %d\n", err);
1068 reset_control_deassert(pcie->afi_rst);
1070 err = clk_prepare_enable(pcie->afi_clk);
1072 dev_err(dev, "failed to enable AFI clock: %d\n", err);
1076 if (soc->has_cml_clk) {
1077 err = clk_prepare_enable(pcie->cml_clk);
1079 dev_err(dev, "failed to enable CML clock: %d\n", err);
1084 err = clk_prepare_enable(pcie->pll_e);
1086 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1093 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1095 struct device *dev = pcie->dev;
1096 const struct tegra_pcie_soc *soc = pcie->soc;
1098 pcie->pex_clk = devm_clk_get(dev, "pex");
1099 if (IS_ERR(pcie->pex_clk))
1100 return PTR_ERR(pcie->pex_clk);
1102 pcie->afi_clk = devm_clk_get(dev, "afi");
1103 if (IS_ERR(pcie->afi_clk))
1104 return PTR_ERR(pcie->afi_clk);
1106 pcie->pll_e = devm_clk_get(dev, "pll_e");
1107 if (IS_ERR(pcie->pll_e))
1108 return PTR_ERR(pcie->pll_e);
1110 if (soc->has_cml_clk) {
1111 pcie->cml_clk = devm_clk_get(dev, "cml");
1112 if (IS_ERR(pcie->cml_clk))
1113 return PTR_ERR(pcie->cml_clk);
1119 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1121 struct device *dev = pcie->dev;
1123 pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1124 if (IS_ERR(pcie->pex_rst))
1125 return PTR_ERR(pcie->pex_rst);
1127 pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1128 if (IS_ERR(pcie->afi_rst))
1129 return PTR_ERR(pcie->afi_rst);
1131 pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1132 if (IS_ERR(pcie->pcie_xrst))
1133 return PTR_ERR(pcie->pcie_xrst);
1138 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1140 struct device *dev = pcie->dev;
1143 pcie->phy = devm_phy_optional_get(dev, "pcie");
1144 if (IS_ERR(pcie->phy)) {
1145 err = PTR_ERR(pcie->phy);
1146 dev_err(dev, "failed to get PHY: %d\n", err);
1150 err = phy_init(pcie->phy);
1152 dev_err(dev, "failed to initialize PHY: %d\n", err);
1156 pcie->legacy_phy = true;
1161 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1162 struct device_node *np,
1163 const char *consumer,
1169 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1171 return ERR_PTR(-ENOMEM);
1173 phy = devm_of_phy_get(dev, np, name);
1176 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1182 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1184 struct device *dev = port->pcie->dev;
1189 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1193 for (i = 0; i < port->lanes; i++) {
1194 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1196 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1198 return PTR_ERR(phy);
1201 err = phy_init(phy);
1203 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1208 port->phys[i] = phy;
1214 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1216 const struct tegra_pcie_soc *soc = pcie->soc;
1217 struct device_node *np = pcie->dev->of_node;
1218 struct tegra_pcie_port *port;
1221 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1222 return tegra_pcie_phys_get_legacy(pcie);
1224 list_for_each_entry(port, &pcie->ports, list) {
1225 err = tegra_pcie_port_get_phys(port);
1233 static void tegra_pcie_phys_put(struct tegra_pcie *pcie)
1235 struct tegra_pcie_port *port;
1236 struct device *dev = pcie->dev;
1239 if (pcie->legacy_phy) {
1240 err = phy_exit(pcie->phy);
1242 dev_err(dev, "failed to teardown PHY: %d\n", err);
1246 list_for_each_entry(port, &pcie->ports, list) {
1247 for (i = 0; i < port->lanes; i++) {
1248 err = phy_exit(port->phys[i]);
1250 dev_err(dev, "failed to teardown PHY#%u: %d\n",
1257 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1259 struct device *dev = pcie->dev;
1260 struct platform_device *pdev = to_platform_device(dev);
1261 struct resource *pads, *afi, *res;
1262 const struct tegra_pcie_soc *soc = pcie->soc;
1265 err = tegra_pcie_clocks_get(pcie);
1267 dev_err(dev, "failed to get clocks: %d\n", err);
1271 err = tegra_pcie_resets_get(pcie);
1273 dev_err(dev, "failed to get resets: %d\n", err);
1277 if (soc->program_uphy) {
1278 err = tegra_pcie_phys_get(pcie);
1280 dev_err(dev, "failed to get PHYs: %d\n", err);
1285 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1286 pcie->pads = devm_ioremap_resource(dev, pads);
1287 if (IS_ERR(pcie->pads)) {
1288 err = PTR_ERR(pcie->pads);
1292 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1293 pcie->afi = devm_ioremap_resource(dev, afi);
1294 if (IS_ERR(pcie->afi)) {
1295 err = PTR_ERR(pcie->afi);
1299 /* request configuration space, but remap later, on demand */
1300 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1302 err = -EADDRNOTAVAIL;
1308 /* constrain configuration space to 4 KiB */
1309 pcie->cs.end = pcie->cs.start + SZ_4K - 1;
1311 pcie->cfg = devm_ioremap_resource(dev, &pcie->cs);
1312 if (IS_ERR(pcie->cfg)) {
1313 err = PTR_ERR(pcie->cfg);
1317 /* request interrupt */
1318 err = platform_get_irq_byname(pdev, "intr");
1320 dev_err(dev, "failed to get IRQ: %d\n", err);
1326 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1328 dev_err(dev, "failed to register IRQ: %d\n", err);
1335 if (soc->program_uphy)
1336 tegra_pcie_phys_put(pcie);
1340 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1342 const struct tegra_pcie_soc *soc = pcie->soc;
1345 free_irq(pcie->irq, pcie);
1347 if (soc->program_uphy)
1348 tegra_pcie_phys_put(pcie);
1353 static void tegra_pcie_pme_turnoff(struct tegra_pcie_port *port)
1355 struct tegra_pcie *pcie = port->pcie;
1356 const struct tegra_pcie_soc *soc = pcie->soc;
1361 val = afi_readl(pcie, AFI_PCIE_PME);
1362 val |= (0x1 << soc->ports[port->index].pme.turnoff_bit);
1363 afi_writel(pcie, val, AFI_PCIE_PME);
1365 ack_bit = soc->ports[port->index].pme.ack_bit;
1366 err = readl_poll_timeout(pcie->afi + AFI_PCIE_PME, val,
1367 val & (0x1 << ack_bit), 1, PME_ACK_TIMEOUT);
1369 dev_err(pcie->dev, "PME Ack is not received on port: %d\n",
1372 usleep_range(10000, 11000);
1374 val = afi_readl(pcie, AFI_PCIE_PME);
1375 val &= ~(0x1 << soc->ports[port->index].pme.turnoff_bit);
1376 afi_writel(pcie, val, AFI_PCIE_PME);
1379 static int tegra_msi_alloc(struct tegra_msi *chip)
1383 mutex_lock(&chip->lock);
1385 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1386 if (msi < INT_PCI_MSI_NR)
1387 set_bit(msi, chip->used);
1391 mutex_unlock(&chip->lock);
1396 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1398 struct device *dev = chip->chip.dev;
1400 mutex_lock(&chip->lock);
1402 if (!test_bit(irq, chip->used))
1403 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1405 clear_bit(irq, chip->used);
1407 mutex_unlock(&chip->lock);
1410 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1412 struct tegra_pcie *pcie = data;
1413 struct device *dev = pcie->dev;
1414 struct tegra_msi *msi = &pcie->msi;
1415 unsigned int i, processed = 0;
1417 for (i = 0; i < 8; i++) {
1418 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1421 unsigned int offset = find_first_bit(®, 32);
1422 unsigned int index = i * 32 + offset;
1425 /* clear the interrupt */
1426 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1428 irq = irq_find_mapping(msi->domain, index);
1430 if (test_bit(index, msi->used))
1431 generic_handle_irq(irq);
1433 dev_info(dev, "unhandled MSI\n");
1436 * that's weird who triggered this?
1439 dev_info(dev, "unexpected MSI\n");
1442 /* see if there's any more pending in this vector */
1443 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1449 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1452 static int tegra_msi_setup_irq(struct msi_controller *chip,
1453 struct pci_dev *pdev, struct msi_desc *desc)
1455 struct tegra_msi *msi = to_tegra_msi(chip);
1460 hwirq = tegra_msi_alloc(msi);
1464 irq = irq_create_mapping(msi->domain, hwirq);
1466 tegra_msi_free(msi, hwirq);
1470 irq_set_msi_desc(irq, desc);
1472 msg.address_lo = lower_32_bits(msi->phys);
1473 msg.address_hi = upper_32_bits(msi->phys);
1476 pci_write_msi_msg(irq, &msg);
1481 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1484 struct tegra_msi *msi = to_tegra_msi(chip);
1485 struct irq_data *d = irq_get_irq_data(irq);
1486 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1488 irq_dispose_mapping(irq);
1489 tegra_msi_free(msi, hwirq);
1492 static struct irq_chip tegra_msi_irq_chip = {
1493 .name = "Tegra PCIe MSI",
1494 .irq_enable = pci_msi_unmask_irq,
1495 .irq_disable = pci_msi_mask_irq,
1496 .irq_mask = pci_msi_mask_irq,
1497 .irq_unmask = pci_msi_unmask_irq,
1500 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1501 irq_hw_number_t hwirq)
1503 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1504 irq_set_chip_data(irq, domain->host_data);
1506 tegra_cpuidle_pcie_irqs_in_use();
1511 static const struct irq_domain_ops msi_domain_ops = {
1512 .map = tegra_msi_map,
1515 static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
1517 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1518 struct platform_device *pdev = to_platform_device(pcie->dev);
1519 struct tegra_msi *msi = &pcie->msi;
1520 struct device *dev = pcie->dev;
1523 mutex_init(&msi->lock);
1525 msi->chip.dev = dev;
1526 msi->chip.setup_irq = tegra_msi_setup_irq;
1527 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1529 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
1530 &msi_domain_ops, &msi->chip);
1532 dev_err(dev, "failed to create IRQ domain\n");
1536 err = platform_get_irq_byname(pdev, "msi");
1538 dev_err(dev, "failed to get IRQ: %d\n", err);
1539 goto free_irq_domain;
1544 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1545 tegra_msi_irq_chip.name, pcie);
1547 dev_err(dev, "failed to request IRQ: %d\n", err);
1548 goto free_irq_domain;
1551 /* Though the PCIe controller can address >32-bit address space, to
1552 * facilitate endpoints that support only 32-bit MSI target address,
1553 * the mask is set to 32-bit to make sure that MSI target address is
1554 * always a 32-bit address
1556 err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
1558 dev_err(dev, "failed to set DMA coherent mask: %d\n", err);
1562 msi->virt = dma_alloc_attrs(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL,
1563 DMA_ATTR_NO_KERNEL_MAPPING);
1565 dev_err(dev, "failed to allocate DMA memory for MSI\n");
1570 host->msi = &msi->chip;
1575 free_irq(msi->irq, pcie);
1577 irq_domain_remove(msi->domain);
1581 static void tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1583 const struct tegra_pcie_soc *soc = pcie->soc;
1584 struct tegra_msi *msi = &pcie->msi;
1587 afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1588 afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1589 /* this register is in 4K increments */
1590 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1592 /* enable all MSI vectors */
1593 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1594 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1595 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1596 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1597 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1598 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1599 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1600 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1602 /* and unmask the MSI interrupt */
1603 reg = afi_readl(pcie, AFI_INTR_MASK);
1604 reg |= AFI_INTR_MASK_MSI_MASK;
1605 afi_writel(pcie, reg, AFI_INTR_MASK);
1608 static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
1610 struct tegra_msi *msi = &pcie->msi;
1611 unsigned int i, irq;
1613 dma_free_attrs(pcie->dev, PAGE_SIZE, msi->virt, msi->phys,
1614 DMA_ATTR_NO_KERNEL_MAPPING);
1617 free_irq(msi->irq, pcie);
1619 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1620 irq = irq_find_mapping(msi->domain, i);
1622 irq_dispose_mapping(irq);
1625 irq_domain_remove(msi->domain);
1628 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1632 /* mask the MSI interrupt */
1633 value = afi_readl(pcie, AFI_INTR_MASK);
1634 value &= ~AFI_INTR_MASK_MSI_MASK;
1635 afi_writel(pcie, value, AFI_INTR_MASK);
1637 /* disable all MSI vectors */
1638 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1639 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1640 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1641 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1642 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1643 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1644 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1645 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1650 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1653 struct device *dev = pcie->dev;
1654 struct device_node *np = dev->of_node;
1656 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1659 dev_info(dev, "4x1, 1x1 configuration\n");
1660 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401;
1664 dev_info(dev, "2x1, 1X1, 1x1 configuration\n");
1665 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1669 dev_info(dev, "1x1, 1x1, 1x1 configuration\n");
1670 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111;
1674 dev_info(dev, "wrong configuration updated in DT, "
1675 "switching to default 2x1, 1x1, 1x1 "
1677 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1680 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1681 of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1684 dev_info(dev, "4x1, 1x1 configuration\n");
1685 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1689 dev_info(dev, "2x1, 1x1 configuration\n");
1690 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1693 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1696 dev_info(dev, "4x1, 2x1 configuration\n");
1697 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1701 dev_info(dev, "2x3 configuration\n");
1702 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1706 dev_info(dev, "4x1, 1x2 configuration\n");
1707 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1710 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1713 dev_info(dev, "single-mode configuration\n");
1714 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1718 dev_info(dev, "dual-mode configuration\n");
1719 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1728 * Check whether a given set of supplies is available in a device tree node.
1729 * This is used to check whether the new or the legacy device tree bindings
1732 static bool of_regulator_bulk_available(struct device_node *np,
1733 struct regulator_bulk_data *supplies,
1734 unsigned int num_supplies)
1739 for (i = 0; i < num_supplies; i++) {
1740 snprintf(property, 32, "%s-supply", supplies[i].supply);
1742 if (of_find_property(np, property, NULL) == NULL)
1750 * Old versions of the device tree binding for this device used a set of power
1751 * supplies that didn't match the hardware inputs. This happened to work for a
1752 * number of cases but is not future proof. However to preserve backwards-
1753 * compatibility with old device trees, this function will try to use the old
1756 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1758 struct device *dev = pcie->dev;
1759 struct device_node *np = dev->of_node;
1761 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1762 pcie->num_supplies = 3;
1763 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1764 pcie->num_supplies = 2;
1766 if (pcie->num_supplies == 0) {
1767 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1771 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1772 sizeof(*pcie->supplies),
1774 if (!pcie->supplies)
1777 pcie->supplies[0].supply = "pex-clk";
1778 pcie->supplies[1].supply = "vdd";
1780 if (pcie->num_supplies > 2)
1781 pcie->supplies[2].supply = "avdd";
1783 return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1787 * Obtains the list of regulators required for a particular generation of the
1790 * This would've been nice to do simply by providing static tables for use
1791 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1792 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1793 * and either seems to be optional depending on which ports are being used.
1795 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1797 struct device *dev = pcie->dev;
1798 struct device_node *np = dev->of_node;
1801 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1802 pcie->num_supplies = 4;
1804 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1805 sizeof(*pcie->supplies),
1807 if (!pcie->supplies)
1810 pcie->supplies[i++].supply = "dvdd-pex";
1811 pcie->supplies[i++].supply = "hvdd-pex-pll";
1812 pcie->supplies[i++].supply = "hvdd-pex";
1813 pcie->supplies[i++].supply = "vddio-pexctl-aud";
1814 } else if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1815 pcie->num_supplies = 6;
1817 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1818 sizeof(*pcie->supplies),
1820 if (!pcie->supplies)
1823 pcie->supplies[i++].supply = "avdd-pll-uerefe";
1824 pcie->supplies[i++].supply = "hvddio-pex";
1825 pcie->supplies[i++].supply = "dvddio-pex";
1826 pcie->supplies[i++].supply = "dvdd-pex-pll";
1827 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1828 pcie->supplies[i++].supply = "vddio-pex-ctl";
1829 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1830 pcie->num_supplies = 7;
1832 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1833 sizeof(*pcie->supplies),
1835 if (!pcie->supplies)
1838 pcie->supplies[i++].supply = "avddio-pex";
1839 pcie->supplies[i++].supply = "dvddio-pex";
1840 pcie->supplies[i++].supply = "avdd-pex-pll";
1841 pcie->supplies[i++].supply = "hvdd-pex";
1842 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1843 pcie->supplies[i++].supply = "vddio-pex-ctl";
1844 pcie->supplies[i++].supply = "avdd-pll-erefe";
1845 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1846 bool need_pexa = false, need_pexb = false;
1848 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1849 if (lane_mask & 0x0f)
1852 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1853 if (lane_mask & 0x30)
1856 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1857 (need_pexb ? 2 : 0);
1859 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1860 sizeof(*pcie->supplies),
1862 if (!pcie->supplies)
1865 pcie->supplies[i++].supply = "avdd-pex-pll";
1866 pcie->supplies[i++].supply = "hvdd-pex";
1867 pcie->supplies[i++].supply = "vddio-pex-ctl";
1868 pcie->supplies[i++].supply = "avdd-plle";
1871 pcie->supplies[i++].supply = "avdd-pexa";
1872 pcie->supplies[i++].supply = "vdd-pexa";
1876 pcie->supplies[i++].supply = "avdd-pexb";
1877 pcie->supplies[i++].supply = "vdd-pexb";
1879 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1880 pcie->num_supplies = 5;
1882 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1883 sizeof(*pcie->supplies),
1885 if (!pcie->supplies)
1888 pcie->supplies[0].supply = "avdd-pex";
1889 pcie->supplies[1].supply = "vdd-pex";
1890 pcie->supplies[2].supply = "avdd-pex-pll";
1891 pcie->supplies[3].supply = "avdd-plle";
1892 pcie->supplies[4].supply = "vddio-pex-clk";
1895 if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
1896 pcie->num_supplies))
1897 return devm_regulator_bulk_get(dev, pcie->num_supplies,
1901 * If not all regulators are available for this new scheme, assume
1902 * that the device tree complies with an older version of the device
1905 dev_info(dev, "using legacy DT binding for power supplies\n");
1907 devm_kfree(dev, pcie->supplies);
1908 pcie->num_supplies = 0;
1910 return tegra_pcie_get_legacy_regulators(pcie);
1913 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1915 struct device *dev = pcie->dev;
1916 struct device_node *np = dev->of_node, *port;
1917 const struct tegra_pcie_soc *soc = pcie->soc;
1918 struct of_pci_range_parser parser;
1919 struct of_pci_range range;
1920 u32 lanes = 0, mask = 0;
1921 unsigned int lane = 0;
1922 struct resource res;
1925 if (of_pci_range_parser_init(&parser, np)) {
1926 dev_err(dev, "missing \"ranges\" property\n");
1930 for_each_of_pci_range(&parser, &range) {
1931 err = of_pci_range_to_resource(&range, np, &res);
1935 switch (res.flags & IORESOURCE_TYPE_BITS) {
1937 /* Track the bus -> CPU I/O mapping offset. */
1938 pcie->offset.io = res.start - range.pci_addr;
1940 memcpy(&pcie->pio, &res, sizeof(res));
1941 pcie->pio.name = np->full_name;
1944 * The Tegra PCIe host bridge uses this to program the
1945 * mapping of the I/O space to the physical address,
1946 * so we override the .start and .end fields here that
1947 * of_pci_range_to_resource() converted to I/O space.
1948 * We also set the IORESOURCE_MEM type to clarify that
1949 * the resource is in the physical memory space.
1951 pcie->io.start = range.cpu_addr;
1952 pcie->io.end = range.cpu_addr + range.size - 1;
1953 pcie->io.flags = IORESOURCE_MEM;
1954 pcie->io.name = "I/O";
1956 memcpy(&res, &pcie->io, sizeof(res));
1959 case IORESOURCE_MEM:
1961 * Track the bus -> CPU memory mapping offset. This
1962 * assumes that the prefetchable and non-prefetchable
1963 * regions will be the last of type IORESOURCE_MEM in
1964 * the ranges property.
1966 pcie->offset.mem = res.start - range.pci_addr;
1968 if (res.flags & IORESOURCE_PREFETCH) {
1969 memcpy(&pcie->prefetch, &res, sizeof(res));
1970 pcie->prefetch.name = "prefetchable";
1972 memcpy(&pcie->mem, &res, sizeof(res));
1973 pcie->mem.name = "non-prefetchable";
1979 err = of_pci_parse_bus_range(np, &pcie->busn);
1981 dev_err(dev, "failed to parse ranges property: %d\n", err);
1982 pcie->busn.name = np->name;
1983 pcie->busn.start = 0;
1984 pcie->busn.end = 0xff;
1985 pcie->busn.flags = IORESOURCE_BUS;
1988 /* parse root ports */
1989 for_each_child_of_node(np, port) {
1990 struct tegra_pcie_port *rp;
1994 err = of_pci_get_devfn(port);
1996 dev_err(dev, "failed to parse address: %d\n", err);
2000 index = PCI_SLOT(err);
2002 if (index < 1 || index > soc->num_ports) {
2003 dev_err(dev, "invalid port number: %d\n", index);
2009 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
2011 dev_err(dev, "failed to parse # of lanes: %d\n",
2017 dev_err(dev, "invalid # of lanes: %u\n", value);
2021 lanes |= value << (index << 3);
2023 if (!of_device_is_available(port)) {
2028 mask |= ((1 << value) - 1) << lane;
2031 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
2035 err = of_address_to_resource(port, 0, &rp->regs);
2037 dev_err(dev, "failed to parse address: %d\n", err);
2041 INIT_LIST_HEAD(&rp->list);
2047 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
2048 if (IS_ERR(rp->base))
2049 return PTR_ERR(rp->base);
2051 list_add_tail(&rp->list, &pcie->ports);
2054 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
2056 dev_err(dev, "invalid lane configuration\n");
2060 err = tegra_pcie_get_regulators(pcie, mask);
2068 * FIXME: If there are no PCIe cards attached, then calling this function
2069 * can result in the increase of the bootup time as there are big timeout
2072 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
2073 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2075 struct device *dev = port->pcie->dev;
2076 unsigned int retries = 3;
2077 unsigned long value;
2079 /* override presence detection */
2080 value = readl(port->base + RP_PRIV_MISC);
2081 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2082 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2083 writel(value, port->base + RP_PRIV_MISC);
2086 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2089 value = readl(port->base + RP_VEND_XP);
2091 if (value & RP_VEND_XP_DL_UP)
2094 usleep_range(1000, 2000);
2095 } while (--timeout);
2098 dev_err(dev, "link %u down, retrying\n", port->index);
2102 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2105 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2107 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2110 usleep_range(1000, 2000);
2111 } while (--timeout);
2114 tegra_pcie_port_reset(port);
2115 } while (--retries);
2120 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2122 struct device *dev = pcie->dev;
2123 struct tegra_pcie_port *port, *tmp;
2125 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2126 dev_info(dev, "probing port %u, using %u lanes\n",
2127 port->index, port->lanes);
2129 tegra_pcie_port_enable(port);
2131 if (tegra_pcie_port_check_link(port))
2134 dev_info(dev, "link %u down, ignoring\n", port->index);
2136 tegra_pcie_port_disable(port);
2137 tegra_pcie_port_free(port);
2141 static void tegra_pcie_disable_ports(struct tegra_pcie *pcie)
2143 struct tegra_pcie_port *port, *tmp;
2145 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2146 tegra_pcie_port_disable(port);
2149 static const struct tegra_pcie_port_soc tegra20_pcie_ports[] = {
2150 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2151 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2154 static const struct tegra_pcie_soc tegra20_pcie = {
2156 .ports = tegra20_pcie_ports,
2157 .msi_base_shift = 0,
2158 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2159 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2160 .pads_refclk_cfg0 = 0xfa5cfa5c,
2161 .has_pex_clkreq_en = false,
2162 .has_pex_bias_ctrl = false,
2163 .has_intr_prsnt_sense = false,
2164 .has_cml_clk = false,
2166 .force_pca_enable = false,
2167 .program_uphy = true,
2170 static const struct tegra_pcie_port_soc tegra30_pcie_ports[] = {
2171 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2172 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2173 { .pme.turnoff_bit = 16, .pme.ack_bit = 18 },
2176 static const struct tegra_pcie_soc tegra30_pcie = {
2178 .ports = tegra30_pcie_ports,
2179 .msi_base_shift = 8,
2180 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2181 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2182 .pads_refclk_cfg0 = 0xfa5cfa5c,
2183 .pads_refclk_cfg1 = 0xfa5cfa5c,
2184 .has_pex_clkreq_en = true,
2185 .has_pex_bias_ctrl = true,
2186 .has_intr_prsnt_sense = true,
2187 .has_cml_clk = true,
2189 .force_pca_enable = false,
2190 .program_uphy = true,
2193 static const struct tegra_pcie_soc tegra124_pcie = {
2195 .ports = tegra20_pcie_ports,
2196 .msi_base_shift = 8,
2197 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2198 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2199 .pads_refclk_cfg0 = 0x44ac44ac,
2200 .has_pex_clkreq_en = true,
2201 .has_pex_bias_ctrl = true,
2202 .has_intr_prsnt_sense = true,
2203 .has_cml_clk = true,
2205 .force_pca_enable = false,
2206 .program_uphy = true,
2209 static const struct tegra_pcie_soc tegra210_pcie = {
2211 .ports = tegra20_pcie_ports,
2212 .msi_base_shift = 8,
2213 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2214 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2215 .pads_refclk_cfg0 = 0x90b890b8,
2216 .has_pex_clkreq_en = true,
2217 .has_pex_bias_ctrl = true,
2218 .has_intr_prsnt_sense = true,
2219 .has_cml_clk = true,
2221 .force_pca_enable = true,
2222 .program_uphy = true,
2225 static const struct tegra_pcie_port_soc tegra186_pcie_ports[] = {
2226 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2227 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2228 { .pme.turnoff_bit = 12, .pme.ack_bit = 14 },
2231 static const struct tegra_pcie_soc tegra186_pcie = {
2233 .ports = tegra186_pcie_ports,
2234 .msi_base_shift = 8,
2235 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2236 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2237 .pads_refclk_cfg0 = 0x80b880b8,
2238 .pads_refclk_cfg1 = 0x000480b8,
2239 .has_pex_clkreq_en = true,
2240 .has_pex_bias_ctrl = true,
2241 .has_intr_prsnt_sense = true,
2242 .has_cml_clk = false,
2244 .force_pca_enable = false,
2245 .program_uphy = false,
2248 static const struct of_device_id tegra_pcie_of_match[] = {
2249 { .compatible = "nvidia,tegra186-pcie", .data = &tegra186_pcie },
2250 { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2251 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2252 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2253 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2257 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2259 struct tegra_pcie *pcie = s->private;
2261 if (list_empty(&pcie->ports))
2264 seq_printf(s, "Index Status\n");
2266 return seq_list_start(&pcie->ports, *pos);
2269 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2271 struct tegra_pcie *pcie = s->private;
2273 return seq_list_next(v, &pcie->ports, pos);
2276 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2280 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2282 bool up = false, active = false;
2283 struct tegra_pcie_port *port;
2286 port = list_entry(v, struct tegra_pcie_port, list);
2288 value = readl(port->base + RP_VEND_XP);
2290 if (value & RP_VEND_XP_DL_UP)
2293 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2295 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2298 seq_printf(s, "%2u ", port->index);
2301 seq_printf(s, "up");
2305 seq_printf(s, ", ");
2307 seq_printf(s, "active");
2310 seq_printf(s, "\n");
2314 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2315 .start = tegra_pcie_ports_seq_start,
2316 .next = tegra_pcie_ports_seq_next,
2317 .stop = tegra_pcie_ports_seq_stop,
2318 .show = tegra_pcie_ports_seq_show,
2321 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2323 struct tegra_pcie *pcie = inode->i_private;
2327 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2331 s = file->private_data;
2337 static const struct file_operations tegra_pcie_ports_ops = {
2338 .owner = THIS_MODULE,
2339 .open = tegra_pcie_ports_open,
2341 .llseek = seq_lseek,
2342 .release = seq_release,
2345 static void tegra_pcie_debugfs_exit(struct tegra_pcie *pcie)
2347 debugfs_remove_recursive(pcie->debugfs);
2348 pcie->debugfs = NULL;
2351 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2353 struct dentry *file;
2355 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2359 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2360 pcie, &tegra_pcie_ports_ops);
2367 tegra_pcie_debugfs_exit(pcie);
2371 static int tegra_pcie_probe(struct platform_device *pdev)
2373 struct device *dev = &pdev->dev;
2374 struct pci_host_bridge *host;
2375 struct tegra_pcie *pcie;
2376 struct pci_bus *child;
2379 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2383 pcie = pci_host_bridge_priv(host);
2384 host->sysdata = pcie;
2385 platform_set_drvdata(pdev, pcie);
2387 pcie->soc = of_device_get_match_data(dev);
2388 INIT_LIST_HEAD(&pcie->ports);
2391 err = tegra_pcie_parse_dt(pcie);
2395 err = tegra_pcie_get_resources(pcie);
2397 dev_err(dev, "failed to request resources: %d\n", err);
2401 err = tegra_pcie_msi_setup(pcie);
2403 dev_err(dev, "failed to enable MSI support: %d\n", err);
2407 pm_runtime_enable(pcie->dev);
2408 err = pm_runtime_get_sync(pcie->dev);
2410 dev_err(dev, "fail to enable pcie controller: %d\n", err);
2414 err = tegra_pcie_request_resources(pcie);
2416 goto pm_runtime_put;
2418 host->busnr = pcie->busn.start;
2419 host->dev.parent = &pdev->dev;
2420 host->ops = &tegra_pcie_ops;
2421 host->map_irq = tegra_pcie_map_irq;
2422 host->swizzle_irq = pci_common_swizzle;
2424 err = pci_scan_root_bus_bridge(host);
2426 dev_err(dev, "failed to register host: %d\n", err);
2427 goto free_resources;
2430 pci_bus_size_bridges(host->bus);
2431 pci_bus_assign_resources(host->bus);
2433 list_for_each_entry(child, &host->bus->children, node)
2434 pcie_bus_configure_settings(child);
2436 pci_bus_add_devices(host->bus);
2438 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2439 err = tegra_pcie_debugfs_init(pcie);
2441 dev_err(dev, "failed to setup debugfs: %d\n", err);
2447 tegra_pcie_free_resources(pcie);
2449 pm_runtime_put_sync(pcie->dev);
2450 pm_runtime_disable(pcie->dev);
2452 tegra_pcie_msi_teardown(pcie);
2454 tegra_pcie_put_resources(pcie);
2458 static int tegra_pcie_remove(struct platform_device *pdev)
2460 struct tegra_pcie *pcie = platform_get_drvdata(pdev);
2461 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
2462 struct tegra_pcie_port *port, *tmp;
2464 if (IS_ENABLED(CONFIG_DEBUG_FS))
2465 tegra_pcie_debugfs_exit(pcie);
2467 pci_stop_root_bus(host->bus);
2468 pci_remove_root_bus(host->bus);
2469 tegra_pcie_free_resources(pcie);
2470 pm_runtime_put_sync(pcie->dev);
2471 pm_runtime_disable(pcie->dev);
2473 if (IS_ENABLED(CONFIG_PCI_MSI))
2474 tegra_pcie_msi_teardown(pcie);
2476 tegra_pcie_put_resources(pcie);
2478 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2479 tegra_pcie_port_free(port);
2484 static int __maybe_unused tegra_pcie_pm_suspend(struct device *dev)
2486 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2487 struct tegra_pcie_port *port;
2489 list_for_each_entry(port, &pcie->ports, list)
2490 tegra_pcie_pme_turnoff(port);
2492 tegra_pcie_disable_ports(pcie);
2494 if (IS_ENABLED(CONFIG_PCI_MSI))
2495 tegra_pcie_disable_msi(pcie);
2497 tegra_pcie_disable_controller(pcie);
2498 tegra_pcie_power_off(pcie);
2503 static int __maybe_unused tegra_pcie_pm_resume(struct device *dev)
2505 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2508 err = tegra_pcie_power_on(pcie);
2510 dev_err(dev, "tegra pcie power on fail: %d\n", err);
2513 err = tegra_pcie_enable_controller(pcie);
2515 dev_err(dev, "tegra pcie controller enable fail: %d\n", err);
2518 tegra_pcie_setup_translations(pcie);
2520 if (IS_ENABLED(CONFIG_PCI_MSI))
2521 tegra_pcie_enable_msi(pcie);
2523 tegra_pcie_enable_ports(pcie);
2528 tegra_pcie_power_off(pcie);
2533 static const struct dev_pm_ops tegra_pcie_pm_ops = {
2534 SET_RUNTIME_PM_OPS(tegra_pcie_pm_suspend, tegra_pcie_pm_resume, NULL)
2535 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_pcie_pm_suspend,
2536 tegra_pcie_pm_resume)
2539 static struct platform_driver tegra_pcie_driver = {
2541 .name = "tegra-pcie",
2542 .of_match_table = tegra_pcie_of_match,
2543 .suppress_bind_attrs = true,
2544 .pm = &tegra_pcie_pm_ops,
2546 .probe = tegra_pcie_probe,
2547 .remove = tegra_pcie_remove,
2549 module_platform_driver(tegra_pcie_driver);
2550 MODULE_LICENSE("GPL");