1 // SPDX-License-Identifier: GPL-2.0+
3 * PCIe host controller driver for Tegra SoCs
5 * Copyright (c) 2010, CompuLab, Ltd.
6 * Author: Mike Rapoport <mike@compulab.co.il>
8 * Based on NVIDIA PCIe driver
9 * Copyright (c) 2008-2009, NVIDIA Corporation.
11 * Bits taken from arch/arm/mach-dove/pcie.c
13 * Author: Thierry Reding <treding@nvidia.com>
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 /* Tegra20 and Tegra30 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_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_relax_enable);
554 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_relax_enable);
555 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_relax_enable);
556 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_relax_enable);
558 static int tegra_pcie_request_resources(struct tegra_pcie *pcie)
560 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
561 struct list_head *windows = &host->windows;
562 struct device *dev = pcie->dev;
565 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
566 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
567 pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem);
568 pci_add_resource(windows, &pcie->busn);
570 err = devm_request_pci_bus_resources(dev, windows);
572 pci_free_resource_list(windows);
576 pci_remap_iospace(&pcie->pio, pcie->io.start);
581 static void tegra_pcie_free_resources(struct tegra_pcie *pcie)
583 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
584 struct list_head *windows = &host->windows;
586 pci_unmap_iospace(&pcie->pio);
587 pci_free_resource_list(windows);
590 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
592 struct tegra_pcie *pcie = pdev->bus->sysdata;
595 tegra_cpuidle_pcie_irqs_in_use();
597 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
604 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
606 const char *err_msg[] = {
614 "Response decoding error",
615 "AXI response decoding error",
616 "Transaction timeout",
617 "Slot present pin change",
618 "Slot clock request change",
619 "TMS clock ramp change",
620 "TMS ready for power down",
623 struct tegra_pcie *pcie = arg;
624 struct device *dev = pcie->dev;
627 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
628 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
629 afi_writel(pcie, 0, AFI_INTR_CODE);
631 if (code == AFI_INTR_LEGACY)
634 if (code >= ARRAY_SIZE(err_msg))
638 * do not pollute kernel log with master abort reports since they
639 * happen a lot during enumeration
641 if (code == AFI_INTR_MASTER_ABORT)
642 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
644 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
646 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
647 code == AFI_INTR_FPCI_DECODE_ERROR) {
648 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
649 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
651 if (code == AFI_INTR_MASTER_ABORT)
652 dev_dbg(dev, " FPCI address: %10llx\n", address);
654 dev_err(dev, " FPCI address: %10llx\n", address);
661 * FPCI map is as follows:
662 * - 0xfdfc000000: I/O space
663 * - 0xfdfe000000: type 0 configuration space
664 * - 0xfdff000000: type 1 configuration space
665 * - 0xfe00000000: type 0 extended configuration space
666 * - 0xfe10000000: type 1 extended configuration space
668 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
670 u32 fpci_bar, size, axi_address;
672 /* Bar 0: type 1 extended configuration space */
673 size = resource_size(&pcie->cs);
674 afi_writel(pcie, pcie->cs.start, AFI_AXI_BAR0_START);
675 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
677 /* Bar 1: downstream IO bar */
678 fpci_bar = 0xfdfc0000;
679 size = resource_size(&pcie->io);
680 axi_address = pcie->io.start;
681 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
682 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
683 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
685 /* Bar 2: prefetchable memory BAR */
686 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
687 size = resource_size(&pcie->prefetch);
688 axi_address = pcie->prefetch.start;
689 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
690 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
691 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
693 /* Bar 3: non prefetchable memory BAR */
694 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
695 size = resource_size(&pcie->mem);
696 axi_address = pcie->mem.start;
697 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
698 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
699 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
701 /* NULL out the remaining BARs as they are not used */
702 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
703 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
704 afi_writel(pcie, 0, AFI_FPCI_BAR4);
706 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
707 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
708 afi_writel(pcie, 0, AFI_FPCI_BAR5);
710 /* map all upstream transactions as uncached */
711 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
712 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
713 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
714 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
716 /* MSI translations are setup only when needed */
717 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
718 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
719 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
720 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
723 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
725 const struct tegra_pcie_soc *soc = pcie->soc;
728 timeout = jiffies + msecs_to_jiffies(timeout);
730 while (time_before(jiffies, timeout)) {
731 value = pads_readl(pcie, soc->pads_pll_ctl);
732 if (value & PADS_PLL_CTL_LOCKDET)
739 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
741 struct device *dev = pcie->dev;
742 const struct tegra_pcie_soc *soc = pcie->soc;
746 /* initialize internal PHY, enable up to 16 PCIE lanes */
747 pads_writel(pcie, 0x0, PADS_CTL_SEL);
749 /* override IDDQ to 1 on all 4 lanes */
750 value = pads_readl(pcie, PADS_CTL);
751 value |= PADS_CTL_IDDQ_1L;
752 pads_writel(pcie, value, PADS_CTL);
755 * Set up PHY PLL inputs select PLLE output as refclock,
756 * set TX ref sel to div10 (not div5).
758 value = pads_readl(pcie, soc->pads_pll_ctl);
759 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
760 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
761 pads_writel(pcie, value, soc->pads_pll_ctl);
764 value = pads_readl(pcie, soc->pads_pll_ctl);
765 value &= ~PADS_PLL_CTL_RST_B4SM;
766 pads_writel(pcie, value, soc->pads_pll_ctl);
768 usleep_range(20, 100);
770 /* take PLL out of reset */
771 value = pads_readl(pcie, soc->pads_pll_ctl);
772 value |= PADS_PLL_CTL_RST_B4SM;
773 pads_writel(pcie, value, soc->pads_pll_ctl);
775 /* wait for the PLL to lock */
776 err = tegra_pcie_pll_wait(pcie, 500);
778 dev_err(dev, "PLL failed to lock: %d\n", err);
782 /* turn off IDDQ override */
783 value = pads_readl(pcie, PADS_CTL);
784 value &= ~PADS_CTL_IDDQ_1L;
785 pads_writel(pcie, value, PADS_CTL);
787 /* enable TX/RX data */
788 value = pads_readl(pcie, PADS_CTL);
789 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
790 pads_writel(pcie, value, PADS_CTL);
795 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
797 const struct tegra_pcie_soc *soc = pcie->soc;
800 /* disable TX/RX data */
801 value = pads_readl(pcie, PADS_CTL);
802 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
803 pads_writel(pcie, value, PADS_CTL);
806 value = pads_readl(pcie, PADS_CTL);
807 value |= PADS_CTL_IDDQ_1L;
808 pads_writel(pcie, value, PADS_CTL);
811 value = pads_readl(pcie, soc->pads_pll_ctl);
812 value &= ~PADS_PLL_CTL_RST_B4SM;
813 pads_writel(pcie, value, soc->pads_pll_ctl);
815 usleep_range(20, 100);
820 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
822 struct device *dev = port->pcie->dev;
826 for (i = 0; i < port->lanes; i++) {
827 err = phy_power_on(port->phys[i]);
829 dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
837 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
839 struct device *dev = port->pcie->dev;
843 for (i = 0; i < port->lanes; i++) {
844 err = phy_power_off(port->phys[i]);
846 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
855 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
857 struct device *dev = pcie->dev;
858 const struct tegra_pcie_soc *soc = pcie->soc;
859 struct tegra_pcie_port *port;
862 if (pcie->legacy_phy) {
864 err = phy_power_on(pcie->phy);
866 err = tegra_pcie_phy_enable(pcie);
869 dev_err(dev, "failed to power on PHY: %d\n", err);
874 list_for_each_entry(port, &pcie->ports, list) {
875 err = tegra_pcie_port_phy_power_on(port);
878 "failed to power on PCIe port %u PHY: %d\n",
884 /* Configure the reference clock driver */
885 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
887 if (soc->num_ports > 2)
888 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
893 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
895 struct device *dev = pcie->dev;
896 struct tegra_pcie_port *port;
899 if (pcie->legacy_phy) {
901 err = phy_power_off(pcie->phy);
903 err = tegra_pcie_phy_disable(pcie);
906 dev_err(dev, "failed to power off PHY: %d\n", err);
911 list_for_each_entry(port, &pcie->ports, list) {
912 err = tegra_pcie_port_phy_power_off(port);
915 "failed to power off PCIe port %u PHY: %d\n",
924 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
926 struct device *dev = pcie->dev;
927 const struct tegra_pcie_soc *soc = pcie->soc;
928 struct tegra_pcie_port *port;
932 /* enable PLL power down */
934 value = afi_readl(pcie, AFI_PLLE_CONTROL);
935 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
936 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
937 afi_writel(pcie, value, AFI_PLLE_CONTROL);
940 /* power down PCIe slot clock bias pad */
941 if (soc->has_pex_bias_ctrl)
942 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
944 /* configure mode and disable all ports */
945 value = afi_readl(pcie, AFI_PCIE_CONFIG);
946 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
947 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
949 list_for_each_entry(port, &pcie->ports, list)
950 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
952 afi_writel(pcie, value, AFI_PCIE_CONFIG);
955 value = afi_readl(pcie, AFI_FUSE);
956 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
957 afi_writel(pcie, value, AFI_FUSE);
959 value = afi_readl(pcie, AFI_FUSE);
960 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
961 afi_writel(pcie, value, AFI_FUSE);
964 if (soc->program_uphy) {
965 err = tegra_pcie_phy_power_on(pcie);
967 dev_err(dev, "failed to power on PHY(s): %d\n", err);
972 /* take the PCIe interface module out of reset */
973 reset_control_deassert(pcie->pcie_xrst);
975 /* finally enable PCIe */
976 value = afi_readl(pcie, AFI_CONFIGURATION);
977 value |= AFI_CONFIGURATION_EN_FPCI;
978 afi_writel(pcie, value, AFI_CONFIGURATION);
980 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
981 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
982 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
984 if (soc->has_intr_prsnt_sense)
985 value |= AFI_INTR_EN_PRSNT_SENSE;
987 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
988 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
990 /* don't enable MSI for now, only when needed */
991 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
993 /* disable all exceptions */
994 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
999 static void tegra_pcie_disable_controller(struct tegra_pcie *pcie)
1003 reset_control_assert(pcie->pcie_xrst);
1005 if (pcie->soc->program_uphy) {
1006 err = tegra_pcie_phy_power_off(pcie);
1008 dev_err(pcie->dev, "failed to power off PHY(s): %d\n",
1013 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1015 struct device *dev = pcie->dev;
1016 const struct tegra_pcie_soc *soc = pcie->soc;
1019 reset_control_assert(pcie->afi_rst);
1020 reset_control_assert(pcie->pex_rst);
1022 clk_disable_unprepare(pcie->pll_e);
1023 if (soc->has_cml_clk)
1024 clk_disable_unprepare(pcie->cml_clk);
1025 clk_disable_unprepare(pcie->afi_clk);
1026 clk_disable_unprepare(pcie->pex_clk);
1028 if (!dev->pm_domain)
1029 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1031 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1033 dev_warn(dev, "failed to disable regulators: %d\n", err);
1036 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1038 struct device *dev = pcie->dev;
1039 const struct tegra_pcie_soc *soc = pcie->soc;
1042 reset_control_assert(pcie->pcie_xrst);
1043 reset_control_assert(pcie->afi_rst);
1044 reset_control_assert(pcie->pex_rst);
1046 if (!dev->pm_domain)
1047 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1049 /* enable regulators */
1050 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1052 dev_err(dev, "failed to enable regulators: %d\n", err);
1054 if (dev->pm_domain) {
1055 err = clk_prepare_enable(pcie->pex_clk);
1057 dev_err(dev, "failed to enable PEX clock: %d\n", err);
1060 reset_control_deassert(pcie->pex_rst);
1062 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
1066 dev_err(dev, "powerup sequence failed: %d\n", err);
1071 reset_control_deassert(pcie->afi_rst);
1073 err = clk_prepare_enable(pcie->afi_clk);
1075 dev_err(dev, "failed to enable AFI clock: %d\n", err);
1079 if (soc->has_cml_clk) {
1080 err = clk_prepare_enable(pcie->cml_clk);
1082 dev_err(dev, "failed to enable CML clock: %d\n", err);
1087 err = clk_prepare_enable(pcie->pll_e);
1089 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1096 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1098 struct device *dev = pcie->dev;
1099 const struct tegra_pcie_soc *soc = pcie->soc;
1101 pcie->pex_clk = devm_clk_get(dev, "pex");
1102 if (IS_ERR(pcie->pex_clk))
1103 return PTR_ERR(pcie->pex_clk);
1105 pcie->afi_clk = devm_clk_get(dev, "afi");
1106 if (IS_ERR(pcie->afi_clk))
1107 return PTR_ERR(pcie->afi_clk);
1109 pcie->pll_e = devm_clk_get(dev, "pll_e");
1110 if (IS_ERR(pcie->pll_e))
1111 return PTR_ERR(pcie->pll_e);
1113 if (soc->has_cml_clk) {
1114 pcie->cml_clk = devm_clk_get(dev, "cml");
1115 if (IS_ERR(pcie->cml_clk))
1116 return PTR_ERR(pcie->cml_clk);
1122 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1124 struct device *dev = pcie->dev;
1126 pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1127 if (IS_ERR(pcie->pex_rst))
1128 return PTR_ERR(pcie->pex_rst);
1130 pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1131 if (IS_ERR(pcie->afi_rst))
1132 return PTR_ERR(pcie->afi_rst);
1134 pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1135 if (IS_ERR(pcie->pcie_xrst))
1136 return PTR_ERR(pcie->pcie_xrst);
1141 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1143 struct device *dev = pcie->dev;
1146 pcie->phy = devm_phy_optional_get(dev, "pcie");
1147 if (IS_ERR(pcie->phy)) {
1148 err = PTR_ERR(pcie->phy);
1149 dev_err(dev, "failed to get PHY: %d\n", err);
1153 err = phy_init(pcie->phy);
1155 dev_err(dev, "failed to initialize PHY: %d\n", err);
1159 pcie->legacy_phy = true;
1164 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1165 struct device_node *np,
1166 const char *consumer,
1172 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1174 return ERR_PTR(-ENOMEM);
1176 phy = devm_of_phy_get(dev, np, name);
1179 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1185 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1187 struct device *dev = port->pcie->dev;
1192 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1196 for (i = 0; i < port->lanes; i++) {
1197 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1199 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1201 return PTR_ERR(phy);
1204 err = phy_init(phy);
1206 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1211 port->phys[i] = phy;
1217 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1219 const struct tegra_pcie_soc *soc = pcie->soc;
1220 struct device_node *np = pcie->dev->of_node;
1221 struct tegra_pcie_port *port;
1224 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1225 return tegra_pcie_phys_get_legacy(pcie);
1227 list_for_each_entry(port, &pcie->ports, list) {
1228 err = tegra_pcie_port_get_phys(port);
1236 static void tegra_pcie_phys_put(struct tegra_pcie *pcie)
1238 struct tegra_pcie_port *port;
1239 struct device *dev = pcie->dev;
1242 if (pcie->legacy_phy) {
1243 err = phy_exit(pcie->phy);
1245 dev_err(dev, "failed to teardown PHY: %d\n", err);
1249 list_for_each_entry(port, &pcie->ports, list) {
1250 for (i = 0; i < port->lanes; i++) {
1251 err = phy_exit(port->phys[i]);
1253 dev_err(dev, "failed to teardown PHY#%u: %d\n",
1260 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1262 struct device *dev = pcie->dev;
1263 struct platform_device *pdev = to_platform_device(dev);
1264 struct resource *pads, *afi, *res;
1265 const struct tegra_pcie_soc *soc = pcie->soc;
1268 err = tegra_pcie_clocks_get(pcie);
1270 dev_err(dev, "failed to get clocks: %d\n", err);
1274 err = tegra_pcie_resets_get(pcie);
1276 dev_err(dev, "failed to get resets: %d\n", err);
1280 if (soc->program_uphy) {
1281 err = tegra_pcie_phys_get(pcie);
1283 dev_err(dev, "failed to get PHYs: %d\n", err);
1288 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1289 pcie->pads = devm_ioremap_resource(dev, pads);
1290 if (IS_ERR(pcie->pads)) {
1291 err = PTR_ERR(pcie->pads);
1295 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1296 pcie->afi = devm_ioremap_resource(dev, afi);
1297 if (IS_ERR(pcie->afi)) {
1298 err = PTR_ERR(pcie->afi);
1302 /* request configuration space, but remap later, on demand */
1303 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1305 err = -EADDRNOTAVAIL;
1311 /* constrain configuration space to 4 KiB */
1312 pcie->cs.end = pcie->cs.start + SZ_4K - 1;
1314 pcie->cfg = devm_ioremap_resource(dev, &pcie->cs);
1315 if (IS_ERR(pcie->cfg)) {
1316 err = PTR_ERR(pcie->cfg);
1320 /* request interrupt */
1321 err = platform_get_irq_byname(pdev, "intr");
1323 dev_err(dev, "failed to get IRQ: %d\n", err);
1329 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1331 dev_err(dev, "failed to register IRQ: %d\n", err);
1338 if (soc->program_uphy)
1339 tegra_pcie_phys_put(pcie);
1343 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1345 const struct tegra_pcie_soc *soc = pcie->soc;
1348 free_irq(pcie->irq, pcie);
1350 if (soc->program_uphy)
1351 tegra_pcie_phys_put(pcie);
1356 static void tegra_pcie_pme_turnoff(struct tegra_pcie_port *port)
1358 struct tegra_pcie *pcie = port->pcie;
1359 const struct tegra_pcie_soc *soc = pcie->soc;
1364 val = afi_readl(pcie, AFI_PCIE_PME);
1365 val |= (0x1 << soc->ports[port->index].pme.turnoff_bit);
1366 afi_writel(pcie, val, AFI_PCIE_PME);
1368 ack_bit = soc->ports[port->index].pme.ack_bit;
1369 err = readl_poll_timeout(pcie->afi + AFI_PCIE_PME, val,
1370 val & (0x1 << ack_bit), 1, PME_ACK_TIMEOUT);
1372 dev_err(pcie->dev, "PME Ack is not received on port: %d\n",
1375 usleep_range(10000, 11000);
1377 val = afi_readl(pcie, AFI_PCIE_PME);
1378 val &= ~(0x1 << soc->ports[port->index].pme.turnoff_bit);
1379 afi_writel(pcie, val, AFI_PCIE_PME);
1382 static int tegra_msi_alloc(struct tegra_msi *chip)
1386 mutex_lock(&chip->lock);
1388 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1389 if (msi < INT_PCI_MSI_NR)
1390 set_bit(msi, chip->used);
1394 mutex_unlock(&chip->lock);
1399 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1401 struct device *dev = chip->chip.dev;
1403 mutex_lock(&chip->lock);
1405 if (!test_bit(irq, chip->used))
1406 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1408 clear_bit(irq, chip->used);
1410 mutex_unlock(&chip->lock);
1413 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1415 struct tegra_pcie *pcie = data;
1416 struct device *dev = pcie->dev;
1417 struct tegra_msi *msi = &pcie->msi;
1418 unsigned int i, processed = 0;
1420 for (i = 0; i < 8; i++) {
1421 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1424 unsigned int offset = find_first_bit(®, 32);
1425 unsigned int index = i * 32 + offset;
1428 /* clear the interrupt */
1429 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1431 irq = irq_find_mapping(msi->domain, index);
1433 if (test_bit(index, msi->used))
1434 generic_handle_irq(irq);
1436 dev_info(dev, "unhandled MSI\n");
1439 * that's weird who triggered this?
1442 dev_info(dev, "unexpected MSI\n");
1445 /* see if there's any more pending in this vector */
1446 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1452 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1455 static int tegra_msi_setup_irq(struct msi_controller *chip,
1456 struct pci_dev *pdev, struct msi_desc *desc)
1458 struct tegra_msi *msi = to_tegra_msi(chip);
1463 hwirq = tegra_msi_alloc(msi);
1467 irq = irq_create_mapping(msi->domain, hwirq);
1469 tegra_msi_free(msi, hwirq);
1473 irq_set_msi_desc(irq, desc);
1475 msg.address_lo = lower_32_bits(msi->phys);
1476 msg.address_hi = upper_32_bits(msi->phys);
1479 pci_write_msi_msg(irq, &msg);
1484 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1487 struct tegra_msi *msi = to_tegra_msi(chip);
1488 struct irq_data *d = irq_get_irq_data(irq);
1489 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1491 irq_dispose_mapping(irq);
1492 tegra_msi_free(msi, hwirq);
1495 static struct irq_chip tegra_msi_irq_chip = {
1496 .name = "Tegra PCIe MSI",
1497 .irq_enable = pci_msi_unmask_irq,
1498 .irq_disable = pci_msi_mask_irq,
1499 .irq_mask = pci_msi_mask_irq,
1500 .irq_unmask = pci_msi_unmask_irq,
1503 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1504 irq_hw_number_t hwirq)
1506 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1507 irq_set_chip_data(irq, domain->host_data);
1509 tegra_cpuidle_pcie_irqs_in_use();
1514 static const struct irq_domain_ops msi_domain_ops = {
1515 .map = tegra_msi_map,
1518 static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
1520 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1521 struct platform_device *pdev = to_platform_device(pcie->dev);
1522 struct tegra_msi *msi = &pcie->msi;
1523 struct device *dev = pcie->dev;
1526 mutex_init(&msi->lock);
1528 msi->chip.dev = dev;
1529 msi->chip.setup_irq = tegra_msi_setup_irq;
1530 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1532 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
1533 &msi_domain_ops, &msi->chip);
1535 dev_err(dev, "failed to create IRQ domain\n");
1539 err = platform_get_irq_byname(pdev, "msi");
1541 dev_err(dev, "failed to get IRQ: %d\n", err);
1547 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1548 tegra_msi_irq_chip.name, pcie);
1550 dev_err(dev, "failed to request IRQ: %d\n", err);
1554 /* setup AFI/FPCI range */
1555 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1556 msi->phys = virt_to_phys((void *)msi->pages);
1557 host->msi = &msi->chip;
1562 irq_domain_remove(msi->domain);
1566 static void tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1568 const struct tegra_pcie_soc *soc = pcie->soc;
1569 struct tegra_msi *msi = &pcie->msi;
1572 afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1573 afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1574 /* this register is in 4K increments */
1575 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1577 /* enable all MSI vectors */
1578 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1579 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1580 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1581 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1582 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1583 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1584 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1585 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1587 /* and unmask the MSI interrupt */
1588 reg = afi_readl(pcie, AFI_INTR_MASK);
1589 reg |= AFI_INTR_MASK_MSI_MASK;
1590 afi_writel(pcie, reg, AFI_INTR_MASK);
1593 static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
1595 struct tegra_msi *msi = &pcie->msi;
1596 unsigned int i, irq;
1598 free_pages(msi->pages, 0);
1601 free_irq(msi->irq, pcie);
1603 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1604 irq = irq_find_mapping(msi->domain, i);
1606 irq_dispose_mapping(irq);
1609 irq_domain_remove(msi->domain);
1612 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1616 /* mask the MSI interrupt */
1617 value = afi_readl(pcie, AFI_INTR_MASK);
1618 value &= ~AFI_INTR_MASK_MSI_MASK;
1619 afi_writel(pcie, value, AFI_INTR_MASK);
1621 /* disable all MSI vectors */
1622 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1623 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1624 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1625 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1626 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1627 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1628 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1629 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1634 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1637 struct device *dev = pcie->dev;
1638 struct device_node *np = dev->of_node;
1640 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1643 dev_info(dev, "4x1, 1x1 configuration\n");
1644 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401;
1648 dev_info(dev, "2x1, 1X1, 1x1 configuration\n");
1649 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1653 dev_info(dev, "1x1, 1x1, 1x1 configuration\n");
1654 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111;
1658 dev_info(dev, "wrong configuration updated in DT, "
1659 "switching to default 2x1, 1x1, 1x1 "
1661 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211;
1664 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1665 of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1668 dev_info(dev, "4x1, 1x1 configuration\n");
1669 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1673 dev_info(dev, "2x1, 1x1 configuration\n");
1674 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1677 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1680 dev_info(dev, "4x1, 2x1 configuration\n");
1681 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1685 dev_info(dev, "2x3 configuration\n");
1686 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1690 dev_info(dev, "4x1, 1x2 configuration\n");
1691 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1694 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1697 dev_info(dev, "single-mode configuration\n");
1698 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1702 dev_info(dev, "dual-mode configuration\n");
1703 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1712 * Check whether a given set of supplies is available in a device tree node.
1713 * This is used to check whether the new or the legacy device tree bindings
1716 static bool of_regulator_bulk_available(struct device_node *np,
1717 struct regulator_bulk_data *supplies,
1718 unsigned int num_supplies)
1723 for (i = 0; i < num_supplies; i++) {
1724 snprintf(property, 32, "%s-supply", supplies[i].supply);
1726 if (of_find_property(np, property, NULL) == NULL)
1734 * Old versions of the device tree binding for this device used a set of power
1735 * supplies that didn't match the hardware inputs. This happened to work for a
1736 * number of cases but is not future proof. However to preserve backwards-
1737 * compatibility with old device trees, this function will try to use the old
1740 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1742 struct device *dev = pcie->dev;
1743 struct device_node *np = dev->of_node;
1745 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1746 pcie->num_supplies = 3;
1747 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1748 pcie->num_supplies = 2;
1750 if (pcie->num_supplies == 0) {
1751 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1755 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1756 sizeof(*pcie->supplies),
1758 if (!pcie->supplies)
1761 pcie->supplies[0].supply = "pex-clk";
1762 pcie->supplies[1].supply = "vdd";
1764 if (pcie->num_supplies > 2)
1765 pcie->supplies[2].supply = "avdd";
1767 return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1771 * Obtains the list of regulators required for a particular generation of the
1774 * This would've been nice to do simply by providing static tables for use
1775 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1776 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1777 * and either seems to be optional depending on which ports are being used.
1779 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1781 struct device *dev = pcie->dev;
1782 struct device_node *np = dev->of_node;
1785 if (of_device_is_compatible(np, "nvidia,tegra186-pcie")) {
1786 pcie->num_supplies = 4;
1788 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1789 sizeof(*pcie->supplies),
1791 if (!pcie->supplies)
1794 pcie->supplies[i++].supply = "dvdd-pex";
1795 pcie->supplies[i++].supply = "hvdd-pex-pll";
1796 pcie->supplies[i++].supply = "hvdd-pex";
1797 pcie->supplies[i++].supply = "vddio-pexctl-aud";
1798 } else if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1799 pcie->num_supplies = 6;
1801 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1802 sizeof(*pcie->supplies),
1804 if (!pcie->supplies)
1807 pcie->supplies[i++].supply = "avdd-pll-uerefe";
1808 pcie->supplies[i++].supply = "hvddio-pex";
1809 pcie->supplies[i++].supply = "dvddio-pex";
1810 pcie->supplies[i++].supply = "dvdd-pex-pll";
1811 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1812 pcie->supplies[i++].supply = "vddio-pex-ctl";
1813 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1814 pcie->num_supplies = 7;
1816 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1817 sizeof(*pcie->supplies),
1819 if (!pcie->supplies)
1822 pcie->supplies[i++].supply = "avddio-pex";
1823 pcie->supplies[i++].supply = "dvddio-pex";
1824 pcie->supplies[i++].supply = "avdd-pex-pll";
1825 pcie->supplies[i++].supply = "hvdd-pex";
1826 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1827 pcie->supplies[i++].supply = "vddio-pex-ctl";
1828 pcie->supplies[i++].supply = "avdd-pll-erefe";
1829 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1830 bool need_pexa = false, need_pexb = false;
1832 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1833 if (lane_mask & 0x0f)
1836 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1837 if (lane_mask & 0x30)
1840 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1841 (need_pexb ? 2 : 0);
1843 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1844 sizeof(*pcie->supplies),
1846 if (!pcie->supplies)
1849 pcie->supplies[i++].supply = "avdd-pex-pll";
1850 pcie->supplies[i++].supply = "hvdd-pex";
1851 pcie->supplies[i++].supply = "vddio-pex-ctl";
1852 pcie->supplies[i++].supply = "avdd-plle";
1855 pcie->supplies[i++].supply = "avdd-pexa";
1856 pcie->supplies[i++].supply = "vdd-pexa";
1860 pcie->supplies[i++].supply = "avdd-pexb";
1861 pcie->supplies[i++].supply = "vdd-pexb";
1863 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1864 pcie->num_supplies = 5;
1866 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1867 sizeof(*pcie->supplies),
1869 if (!pcie->supplies)
1872 pcie->supplies[0].supply = "avdd-pex";
1873 pcie->supplies[1].supply = "vdd-pex";
1874 pcie->supplies[2].supply = "avdd-pex-pll";
1875 pcie->supplies[3].supply = "avdd-plle";
1876 pcie->supplies[4].supply = "vddio-pex-clk";
1879 if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
1880 pcie->num_supplies))
1881 return devm_regulator_bulk_get(dev, pcie->num_supplies,
1885 * If not all regulators are available for this new scheme, assume
1886 * that the device tree complies with an older version of the device
1889 dev_info(dev, "using legacy DT binding for power supplies\n");
1891 devm_kfree(dev, pcie->supplies);
1892 pcie->num_supplies = 0;
1894 return tegra_pcie_get_legacy_regulators(pcie);
1897 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1899 struct device *dev = pcie->dev;
1900 struct device_node *np = dev->of_node, *port;
1901 const struct tegra_pcie_soc *soc = pcie->soc;
1902 struct of_pci_range_parser parser;
1903 struct of_pci_range range;
1904 u32 lanes = 0, mask = 0;
1905 unsigned int lane = 0;
1906 struct resource res;
1909 if (of_pci_range_parser_init(&parser, np)) {
1910 dev_err(dev, "missing \"ranges\" property\n");
1914 for_each_of_pci_range(&parser, &range) {
1915 err = of_pci_range_to_resource(&range, np, &res);
1919 switch (res.flags & IORESOURCE_TYPE_BITS) {
1921 /* Track the bus -> CPU I/O mapping offset. */
1922 pcie->offset.io = res.start - range.pci_addr;
1924 memcpy(&pcie->pio, &res, sizeof(res));
1925 pcie->pio.name = np->full_name;
1928 * The Tegra PCIe host bridge uses this to program the
1929 * mapping of the I/O space to the physical address,
1930 * so we override the .start and .end fields here that
1931 * of_pci_range_to_resource() converted to I/O space.
1932 * We also set the IORESOURCE_MEM type to clarify that
1933 * the resource is in the physical memory space.
1935 pcie->io.start = range.cpu_addr;
1936 pcie->io.end = range.cpu_addr + range.size - 1;
1937 pcie->io.flags = IORESOURCE_MEM;
1938 pcie->io.name = "I/O";
1940 memcpy(&res, &pcie->io, sizeof(res));
1943 case IORESOURCE_MEM:
1945 * Track the bus -> CPU memory mapping offset. This
1946 * assumes that the prefetchable and non-prefetchable
1947 * regions will be the last of type IORESOURCE_MEM in
1948 * the ranges property.
1950 pcie->offset.mem = res.start - range.pci_addr;
1952 if (res.flags & IORESOURCE_PREFETCH) {
1953 memcpy(&pcie->prefetch, &res, sizeof(res));
1954 pcie->prefetch.name = "prefetchable";
1956 memcpy(&pcie->mem, &res, sizeof(res));
1957 pcie->mem.name = "non-prefetchable";
1963 err = of_pci_parse_bus_range(np, &pcie->busn);
1965 dev_err(dev, "failed to parse ranges property: %d\n", err);
1966 pcie->busn.name = np->name;
1967 pcie->busn.start = 0;
1968 pcie->busn.end = 0xff;
1969 pcie->busn.flags = IORESOURCE_BUS;
1972 /* parse root ports */
1973 for_each_child_of_node(np, port) {
1974 struct tegra_pcie_port *rp;
1978 err = of_pci_get_devfn(port);
1980 dev_err(dev, "failed to parse address: %d\n", err);
1984 index = PCI_SLOT(err);
1986 if (index < 1 || index > soc->num_ports) {
1987 dev_err(dev, "invalid port number: %d\n", index);
1994 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1996 dev_err(dev, "failed to parse # of lanes: %d\n",
2002 dev_err(dev, "invalid # of lanes: %u\n", value);
2007 lanes |= value << (index << 3);
2009 if (!of_device_is_available(port)) {
2014 mask |= ((1 << value) - 1) << lane;
2017 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
2023 err = of_address_to_resource(port, 0, &rp->regs);
2025 dev_err(dev, "failed to parse address: %d\n", err);
2029 INIT_LIST_HEAD(&rp->list);
2035 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
2036 if (IS_ERR(rp->base))
2037 return PTR_ERR(rp->base);
2039 list_add_tail(&rp->list, &pcie->ports);
2042 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
2044 dev_err(dev, "invalid lane configuration\n");
2048 err = tegra_pcie_get_regulators(pcie, mask);
2060 * FIXME: If there are no PCIe cards attached, then calling this function
2061 * can result in the increase of the bootup time as there are big timeout
2064 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
2065 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2067 struct device *dev = port->pcie->dev;
2068 unsigned int retries = 3;
2069 unsigned long value;
2071 /* override presence detection */
2072 value = readl(port->base + RP_PRIV_MISC);
2073 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2074 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2075 writel(value, port->base + RP_PRIV_MISC);
2078 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2081 value = readl(port->base + RP_VEND_XP);
2083 if (value & RP_VEND_XP_DL_UP)
2086 usleep_range(1000, 2000);
2087 } while (--timeout);
2090 dev_err(dev, "link %u down, retrying\n", port->index);
2094 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2097 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2099 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2102 usleep_range(1000, 2000);
2103 } while (--timeout);
2106 tegra_pcie_port_reset(port);
2107 } while (--retries);
2112 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2114 struct device *dev = pcie->dev;
2115 struct tegra_pcie_port *port, *tmp;
2117 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2118 dev_info(dev, "probing port %u, using %u lanes\n",
2119 port->index, port->lanes);
2121 tegra_pcie_port_enable(port);
2123 if (tegra_pcie_port_check_link(port))
2126 dev_info(dev, "link %u down, ignoring\n", port->index);
2128 tegra_pcie_port_disable(port);
2129 tegra_pcie_port_free(port);
2133 static void tegra_pcie_disable_ports(struct tegra_pcie *pcie)
2135 struct tegra_pcie_port *port, *tmp;
2137 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2138 tegra_pcie_port_disable(port);
2141 static const struct tegra_pcie_port_soc tegra20_pcie_ports[] = {
2142 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2143 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2146 static const struct tegra_pcie_soc tegra20_pcie = {
2148 .ports = tegra20_pcie_ports,
2149 .msi_base_shift = 0,
2150 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2151 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2152 .pads_refclk_cfg0 = 0xfa5cfa5c,
2153 .has_pex_clkreq_en = false,
2154 .has_pex_bias_ctrl = false,
2155 .has_intr_prsnt_sense = false,
2156 .has_cml_clk = false,
2158 .force_pca_enable = false,
2159 .program_uphy = true,
2162 static const struct tegra_pcie_port_soc tegra30_pcie_ports[] = {
2163 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2164 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2165 { .pme.turnoff_bit = 16, .pme.ack_bit = 18 },
2168 static const struct tegra_pcie_soc tegra30_pcie = {
2170 .ports = tegra30_pcie_ports,
2171 .msi_base_shift = 8,
2172 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2173 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2174 .pads_refclk_cfg0 = 0xfa5cfa5c,
2175 .pads_refclk_cfg1 = 0xfa5cfa5c,
2176 .has_pex_clkreq_en = true,
2177 .has_pex_bias_ctrl = true,
2178 .has_intr_prsnt_sense = true,
2179 .has_cml_clk = true,
2181 .force_pca_enable = false,
2182 .program_uphy = true,
2185 static const struct tegra_pcie_soc tegra124_pcie = {
2187 .ports = tegra20_pcie_ports,
2188 .msi_base_shift = 8,
2189 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2190 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2191 .pads_refclk_cfg0 = 0x44ac44ac,
2192 .has_pex_clkreq_en = true,
2193 .has_pex_bias_ctrl = true,
2194 .has_intr_prsnt_sense = true,
2195 .has_cml_clk = true,
2197 .force_pca_enable = false,
2198 .program_uphy = true,
2201 static const struct tegra_pcie_soc tegra210_pcie = {
2203 .ports = tegra20_pcie_ports,
2204 .msi_base_shift = 8,
2205 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2206 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2207 .pads_refclk_cfg0 = 0x90b890b8,
2208 .has_pex_clkreq_en = true,
2209 .has_pex_bias_ctrl = true,
2210 .has_intr_prsnt_sense = true,
2211 .has_cml_clk = true,
2213 .force_pca_enable = true,
2214 .program_uphy = true,
2217 static const struct tegra_pcie_port_soc tegra186_pcie_ports[] = {
2218 { .pme.turnoff_bit = 0, .pme.ack_bit = 5 },
2219 { .pme.turnoff_bit = 8, .pme.ack_bit = 10 },
2220 { .pme.turnoff_bit = 12, .pme.ack_bit = 14 },
2223 static const struct tegra_pcie_soc tegra186_pcie = {
2225 .ports = tegra186_pcie_ports,
2226 .msi_base_shift = 8,
2227 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2228 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2229 .pads_refclk_cfg0 = 0x80b880b8,
2230 .pads_refclk_cfg1 = 0x000480b8,
2231 .has_pex_clkreq_en = true,
2232 .has_pex_bias_ctrl = true,
2233 .has_intr_prsnt_sense = true,
2234 .has_cml_clk = false,
2236 .force_pca_enable = false,
2237 .program_uphy = false,
2240 static const struct of_device_id tegra_pcie_of_match[] = {
2241 { .compatible = "nvidia,tegra186-pcie", .data = &tegra186_pcie },
2242 { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2243 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2244 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2245 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2248 MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
2250 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2252 struct tegra_pcie *pcie = s->private;
2254 if (list_empty(&pcie->ports))
2257 seq_printf(s, "Index Status\n");
2259 return seq_list_start(&pcie->ports, *pos);
2262 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2264 struct tegra_pcie *pcie = s->private;
2266 return seq_list_next(v, &pcie->ports, pos);
2269 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2273 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2275 bool up = false, active = false;
2276 struct tegra_pcie_port *port;
2279 port = list_entry(v, struct tegra_pcie_port, list);
2281 value = readl(port->base + RP_VEND_XP);
2283 if (value & RP_VEND_XP_DL_UP)
2286 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2288 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2291 seq_printf(s, "%2u ", port->index);
2294 seq_printf(s, "up");
2298 seq_printf(s, ", ");
2300 seq_printf(s, "active");
2303 seq_printf(s, "\n");
2307 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2308 .start = tegra_pcie_ports_seq_start,
2309 .next = tegra_pcie_ports_seq_next,
2310 .stop = tegra_pcie_ports_seq_stop,
2311 .show = tegra_pcie_ports_seq_show,
2314 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2316 struct tegra_pcie *pcie = inode->i_private;
2320 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2324 s = file->private_data;
2330 static const struct file_operations tegra_pcie_ports_ops = {
2331 .owner = THIS_MODULE,
2332 .open = tegra_pcie_ports_open,
2334 .llseek = seq_lseek,
2335 .release = seq_release,
2338 static void tegra_pcie_debugfs_exit(struct tegra_pcie *pcie)
2340 debugfs_remove_recursive(pcie->debugfs);
2341 pcie->debugfs = NULL;
2344 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2346 struct dentry *file;
2348 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2352 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2353 pcie, &tegra_pcie_ports_ops);
2360 tegra_pcie_debugfs_exit(pcie);
2364 static int tegra_pcie_probe(struct platform_device *pdev)
2366 struct device *dev = &pdev->dev;
2367 struct pci_host_bridge *host;
2368 struct tegra_pcie *pcie;
2369 struct pci_bus *child;
2372 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2376 pcie = pci_host_bridge_priv(host);
2377 host->sysdata = pcie;
2378 platform_set_drvdata(pdev, pcie);
2380 pcie->soc = of_device_get_match_data(dev);
2381 INIT_LIST_HEAD(&pcie->ports);
2384 err = tegra_pcie_parse_dt(pcie);
2388 err = tegra_pcie_get_resources(pcie);
2390 dev_err(dev, "failed to request resources: %d\n", err);
2394 err = tegra_pcie_msi_setup(pcie);
2396 dev_err(dev, "failed to enable MSI support: %d\n", err);
2400 pm_runtime_enable(pcie->dev);
2401 err = pm_runtime_get_sync(pcie->dev);
2403 dev_err(dev, "fail to enable pcie controller: %d\n", err);
2404 goto pm_runtime_put;
2407 err = tegra_pcie_request_resources(pcie);
2409 goto pm_runtime_put;
2411 host->busnr = pcie->busn.start;
2412 host->dev.parent = &pdev->dev;
2413 host->ops = &tegra_pcie_ops;
2414 host->map_irq = tegra_pcie_map_irq;
2415 host->swizzle_irq = pci_common_swizzle;
2417 err = pci_scan_root_bus_bridge(host);
2419 dev_err(dev, "failed to register host: %d\n", err);
2420 goto free_resources;
2423 pci_bus_size_bridges(host->bus);
2424 pci_bus_assign_resources(host->bus);
2426 list_for_each_entry(child, &host->bus->children, node)
2427 pcie_bus_configure_settings(child);
2429 pci_bus_add_devices(host->bus);
2431 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2432 err = tegra_pcie_debugfs_init(pcie);
2434 dev_err(dev, "failed to setup debugfs: %d\n", err);
2440 tegra_pcie_free_resources(pcie);
2442 pm_runtime_put_sync(pcie->dev);
2443 pm_runtime_disable(pcie->dev);
2444 tegra_pcie_msi_teardown(pcie);
2446 tegra_pcie_put_resources(pcie);
2450 static int tegra_pcie_remove(struct platform_device *pdev)
2452 struct tegra_pcie *pcie = platform_get_drvdata(pdev);
2453 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
2454 struct tegra_pcie_port *port, *tmp;
2456 if (IS_ENABLED(CONFIG_DEBUG_FS))
2457 tegra_pcie_debugfs_exit(pcie);
2459 pci_stop_root_bus(host->bus);
2460 pci_remove_root_bus(host->bus);
2461 tegra_pcie_free_resources(pcie);
2462 pm_runtime_put_sync(pcie->dev);
2463 pm_runtime_disable(pcie->dev);
2465 if (IS_ENABLED(CONFIG_PCI_MSI))
2466 tegra_pcie_msi_teardown(pcie);
2468 tegra_pcie_put_resources(pcie);
2470 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
2471 tegra_pcie_port_free(port);
2476 static int __maybe_unused tegra_pcie_pm_suspend(struct device *dev)
2478 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2479 struct tegra_pcie_port *port;
2481 list_for_each_entry(port, &pcie->ports, list)
2482 tegra_pcie_pme_turnoff(port);
2484 tegra_pcie_disable_ports(pcie);
2486 if (IS_ENABLED(CONFIG_PCI_MSI))
2487 tegra_pcie_disable_msi(pcie);
2489 tegra_pcie_disable_controller(pcie);
2490 tegra_pcie_power_off(pcie);
2495 static int __maybe_unused tegra_pcie_pm_resume(struct device *dev)
2497 struct tegra_pcie *pcie = dev_get_drvdata(dev);
2500 err = tegra_pcie_power_on(pcie);
2502 dev_err(dev, "tegra pcie power on fail: %d\n", err);
2505 err = tegra_pcie_enable_controller(pcie);
2507 dev_err(dev, "tegra pcie controller enable fail: %d\n", err);
2510 tegra_pcie_setup_translations(pcie);
2512 if (IS_ENABLED(CONFIG_PCI_MSI))
2513 tegra_pcie_enable_msi(pcie);
2515 tegra_pcie_enable_ports(pcie);
2520 tegra_pcie_power_off(pcie);
2525 static const struct dev_pm_ops tegra_pcie_pm_ops = {
2526 SET_RUNTIME_PM_OPS(tegra_pcie_pm_suspend, tegra_pcie_pm_resume, NULL)
2527 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_pcie_pm_suspend,
2528 tegra_pcie_pm_resume)
2531 static struct platform_driver tegra_pcie_driver = {
2533 .name = "tegra-pcie",
2534 .of_match_table = tegra_pcie_of_match,
2535 .suppress_bind_attrs = true,
2536 .pm = &tegra_pcie_pm_ops,
2538 .probe = tegra_pcie_probe,
2539 .remove = tegra_pcie_remove,
2541 module_platform_driver(tegra_pcie_driver);
2542 MODULE_LICENSE("GPL");