2 * PCIe host controller driver for Tegra SoCs
4 * Copyright (c) 2010, CompuLab, Ltd.
5 * Author: Mike Rapoport <mike@compulab.co.il>
7 * Based on NVIDIA PCIe driver
8 * Copyright (c) 2008-2009, NVIDIA Corporation.
10 * Bits taken from arch/arm/mach-dove/pcie.c
12 * Author: Thierry Reding <treding@nvidia.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <linux/clk.h>
30 #include <linux/debugfs.h>
31 #include <linux/delay.h>
32 #include <linux/export.h>
33 #include <linux/interrupt.h>
34 #include <linux/irq.h>
35 #include <linux/irqdomain.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/msi.h>
39 #include <linux/of_address.h>
40 #include <linux/of_pci.h>
41 #include <linux/of_platform.h>
42 #include <linux/pci.h>
43 #include <linux/phy/phy.h>
44 #include <linux/platform_device.h>
45 #include <linux/reset.h>
46 #include <linux/sizes.h>
47 #include <linux/slab.h>
48 #include <linux/vmalloc.h>
49 #include <linux/regulator/consumer.h>
51 #include <soc/tegra/cpuidle.h>
52 #include <soc/tegra/pmc.h>
54 #define INT_PCI_MSI_NR (8 * 32)
56 /* register definitions */
58 #define AFI_AXI_BAR0_SZ 0x00
59 #define AFI_AXI_BAR1_SZ 0x04
60 #define AFI_AXI_BAR2_SZ 0x08
61 #define AFI_AXI_BAR3_SZ 0x0c
62 #define AFI_AXI_BAR4_SZ 0x10
63 #define AFI_AXI_BAR5_SZ 0x14
65 #define AFI_AXI_BAR0_START 0x18
66 #define AFI_AXI_BAR1_START 0x1c
67 #define AFI_AXI_BAR2_START 0x20
68 #define AFI_AXI_BAR3_START 0x24
69 #define AFI_AXI_BAR4_START 0x28
70 #define AFI_AXI_BAR5_START 0x2c
72 #define AFI_FPCI_BAR0 0x30
73 #define AFI_FPCI_BAR1 0x34
74 #define AFI_FPCI_BAR2 0x38
75 #define AFI_FPCI_BAR3 0x3c
76 #define AFI_FPCI_BAR4 0x40
77 #define AFI_FPCI_BAR5 0x44
79 #define AFI_CACHE_BAR0_SZ 0x48
80 #define AFI_CACHE_BAR0_ST 0x4c
81 #define AFI_CACHE_BAR1_SZ 0x50
82 #define AFI_CACHE_BAR1_ST 0x54
84 #define AFI_MSI_BAR_SZ 0x60
85 #define AFI_MSI_FPCI_BAR_ST 0x64
86 #define AFI_MSI_AXI_BAR_ST 0x68
88 #define AFI_MSI_VEC0 0x6c
89 #define AFI_MSI_VEC1 0x70
90 #define AFI_MSI_VEC2 0x74
91 #define AFI_MSI_VEC3 0x78
92 #define AFI_MSI_VEC4 0x7c
93 #define AFI_MSI_VEC5 0x80
94 #define AFI_MSI_VEC6 0x84
95 #define AFI_MSI_VEC7 0x88
97 #define AFI_MSI_EN_VEC0 0x8c
98 #define AFI_MSI_EN_VEC1 0x90
99 #define AFI_MSI_EN_VEC2 0x94
100 #define AFI_MSI_EN_VEC3 0x98
101 #define AFI_MSI_EN_VEC4 0x9c
102 #define AFI_MSI_EN_VEC5 0xa0
103 #define AFI_MSI_EN_VEC6 0xa4
104 #define AFI_MSI_EN_VEC7 0xa8
106 #define AFI_CONFIGURATION 0xac
107 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
109 #define AFI_FPCI_ERROR_MASKS 0xb0
111 #define AFI_INTR_MASK 0xb4
112 #define AFI_INTR_MASK_INT_MASK (1 << 0)
113 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
115 #define AFI_INTR_CODE 0xb8
116 #define AFI_INTR_CODE_MASK 0xf
117 #define AFI_INTR_INI_SLAVE_ERROR 1
118 #define AFI_INTR_INI_DECODE_ERROR 2
119 #define AFI_INTR_TARGET_ABORT 3
120 #define AFI_INTR_MASTER_ABORT 4
121 #define AFI_INTR_INVALID_WRITE 5
122 #define AFI_INTR_LEGACY 6
123 #define AFI_INTR_FPCI_DECODE_ERROR 7
124 #define AFI_INTR_AXI_DECODE_ERROR 8
125 #define AFI_INTR_FPCI_TIMEOUT 9
126 #define AFI_INTR_PE_PRSNT_SENSE 10
127 #define AFI_INTR_PE_CLKREQ_SENSE 11
128 #define AFI_INTR_CLKCLAMP_SENSE 12
129 #define AFI_INTR_RDY4PD_SENSE 13
130 #define AFI_INTR_P2P_ERROR 14
132 #define AFI_INTR_SIGNATURE 0xbc
133 #define AFI_UPPER_FPCI_ADDRESS 0xc0
134 #define AFI_SM_INTR_ENABLE 0xc4
135 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
136 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
137 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
138 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
139 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
140 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
141 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
142 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
144 #define AFI_AFI_INTR_ENABLE 0xc8
145 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
146 #define AFI_INTR_EN_INI_DECERR (1 << 1)
147 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
148 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
149 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
150 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
151 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
152 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
153 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
155 #define AFI_PCIE_CONFIG 0x0f8
156 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
157 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
158 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
159 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
160 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
161 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
162 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
163 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
164 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
165 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
167 #define AFI_FUSE 0x104
168 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
170 #define AFI_PEX0_CTRL 0x110
171 #define AFI_PEX1_CTRL 0x118
172 #define AFI_PEX2_CTRL 0x128
173 #define AFI_PEX_CTRL_RST (1 << 0)
174 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
175 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
176 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
178 #define AFI_PLLE_CONTROL 0x160
179 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
180 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
182 #define AFI_PEXBIAS_CTRL_0 0x168
184 #define RP_VEND_XP 0x00000f00
185 #define RP_VEND_XP_DL_UP (1 << 30)
187 #define RP_VEND_CTL2 0x00000fa8
188 #define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
190 #define RP_PRIV_MISC 0x00000fe0
191 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
192 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
194 #define RP_LINK_CONTROL_STATUS 0x00000090
195 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
196 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
198 #define PADS_CTL_SEL 0x0000009c
200 #define PADS_CTL 0x000000a0
201 #define PADS_CTL_IDDQ_1L (1 << 0)
202 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
203 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
205 #define PADS_PLL_CTL_TEGRA20 0x000000b8
206 #define PADS_PLL_CTL_TEGRA30 0x000000b4
207 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
208 #define PADS_PLL_CTL_LOCKDET (1 << 8)
209 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
210 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
211 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
212 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
213 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
214 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
215 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
216 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
218 #define PADS_REFCLK_CFG0 0x000000c8
219 #define PADS_REFCLK_CFG1 0x000000cc
220 #define PADS_REFCLK_BIAS 0x000000d0
223 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
224 * entries, one entry per PCIe port. These field definitions and desired
225 * values aren't in the TRM, but do come from NVIDIA.
227 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
228 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
229 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
230 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
233 struct msi_controller chip;
234 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
235 struct irq_domain *domain;
242 /* used to differentiate between Tegra SoC generations */
243 struct tegra_pcie_soc {
244 unsigned int num_ports;
245 unsigned int msi_base_shift;
248 u32 pads_refclk_cfg0;
249 u32 pads_refclk_cfg1;
250 bool has_pex_clkreq_en;
251 bool has_pex_bias_ctrl;
252 bool has_intr_prsnt_sense;
255 bool force_pca_enable;
258 static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
260 return container_of(chip, struct tegra_msi, chip);
270 struct list_head buses;
276 struct resource prefetch;
277 struct resource busn;
289 struct reset_control *pex_rst;
290 struct reset_control *afi_rst;
291 struct reset_control *pcie_xrst;
296 struct tegra_msi msi;
298 struct list_head ports;
301 struct regulator_bulk_data *supplies;
302 unsigned int num_supplies;
304 const struct tegra_pcie_soc *soc;
305 struct dentry *debugfs;
308 struct tegra_pcie_port {
309 struct tegra_pcie *pcie;
310 struct device_node *np;
311 struct list_head list;
312 struct resource regs;
320 struct tegra_pcie_bus {
321 struct vm_struct *area;
322 struct list_head list;
326 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
327 unsigned long offset)
329 writel(value, pcie->afi + offset);
332 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
334 return readl(pcie->afi + offset);
337 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
338 unsigned long offset)
340 writel(value, pcie->pads + offset);
343 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
345 return readl(pcie->pads + offset);
349 * The configuration space mapping on Tegra is somewhat similar to the ECAM
350 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
351 * register accesses are mapped:
353 * [27:24] extended register number
355 * [15:11] device number
356 * [10: 8] function number
357 * [ 7: 0] register number
359 * Mapping the whole extended configuration space would require 256 MiB of
360 * virtual address space, only a small part of which will actually be used.
361 * To work around this, a 1 MiB of virtual addresses are allocated per bus
362 * when the bus is first accessed. When the physical range is mapped, the
363 * the bus number bits are hidden so that the extended register number bits
364 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
366 * [19:16] extended register number
367 * [15:11] device number
368 * [10: 8] function number
369 * [ 7: 0] register number
371 * This is achieved by stitching together 16 chunks of 64 KiB of physical
372 * address space via the MMU.
374 static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
376 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
377 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
380 static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
383 struct device *dev = pcie->dev;
384 pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
385 phys_addr_t cs = pcie->cs->start;
386 struct tegra_pcie_bus *bus;
390 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
392 return ERR_PTR(-ENOMEM);
394 INIT_LIST_HEAD(&bus->list);
397 /* allocate 1 MiB of virtual addresses */
398 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
404 /* map each of the 16 chunks of 64 KiB each */
405 for (i = 0; i < 16; i++) {
406 unsigned long virt = (unsigned long)bus->area->addr +
408 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
410 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
412 dev_err(dev, "ioremap_page_range() failed: %d\n", err);
420 vunmap(bus->area->addr);
426 static int tegra_pcie_add_bus(struct pci_bus *bus)
428 struct pci_host_bridge *host = pci_find_host_bridge(bus);
429 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
430 struct tegra_pcie_bus *b;
432 b = tegra_pcie_bus_alloc(pcie, bus->number);
436 list_add_tail(&b->list, &pcie->buses);
441 static void tegra_pcie_remove_bus(struct pci_bus *child)
443 struct pci_host_bridge *host = pci_find_host_bridge(child);
444 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
445 struct tegra_pcie_bus *bus, *tmp;
447 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
448 if (bus->nr == child->number) {
449 vunmap(bus->area->addr);
450 list_del(&bus->list);
457 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
461 struct pci_host_bridge *host = pci_find_host_bridge(bus);
462 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
463 struct device *dev = pcie->dev;
464 void __iomem *addr = NULL;
466 if (bus->number == 0) {
467 unsigned int slot = PCI_SLOT(devfn);
468 struct tegra_pcie_port *port;
470 list_for_each_entry(port, &pcie->ports, list) {
471 if (port->index + 1 == slot) {
472 addr = port->base + (where & ~3);
477 struct tegra_pcie_bus *b;
479 list_for_each_entry(b, &pcie->buses, list)
480 if (b->nr == bus->number)
481 addr = (void __iomem *)b->area->addr;
484 dev_err(dev, "failed to map cfg. space for bus %u\n",
489 addr += tegra_pcie_conf_offset(devfn, where);
495 static struct pci_ops tegra_pcie_ops = {
496 .add_bus = tegra_pcie_add_bus,
497 .remove_bus = tegra_pcie_remove_bus,
498 .map_bus = tegra_pcie_map_bus,
499 .read = pci_generic_config_read32,
500 .write = pci_generic_config_write32,
503 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
505 unsigned long ret = 0;
507 switch (port->index) {
524 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
526 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
529 /* pulse reset signal */
530 value = afi_readl(port->pcie, ctrl);
531 value &= ~AFI_PEX_CTRL_RST;
532 afi_writel(port->pcie, value, ctrl);
534 usleep_range(1000, 2000);
536 value = afi_readl(port->pcie, ctrl);
537 value |= AFI_PEX_CTRL_RST;
538 afi_writel(port->pcie, value, ctrl);
541 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
543 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
544 const struct tegra_pcie_soc *soc = port->pcie->soc;
547 /* enable reference clock */
548 value = afi_readl(port->pcie, ctrl);
549 value |= AFI_PEX_CTRL_REFCLK_EN;
551 if (soc->has_pex_clkreq_en)
552 value |= AFI_PEX_CTRL_CLKREQ_EN;
554 value |= AFI_PEX_CTRL_OVERRIDE_EN;
556 afi_writel(port->pcie, value, ctrl);
558 tegra_pcie_port_reset(port);
560 if (soc->force_pca_enable) {
561 value = readl(port->base + RP_VEND_CTL2);
562 value |= RP_VEND_CTL2_PCA_ENABLE;
563 writel(value, port->base + RP_VEND_CTL2);
567 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
569 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
570 const struct tegra_pcie_soc *soc = port->pcie->soc;
573 /* assert port reset */
574 value = afi_readl(port->pcie, ctrl);
575 value &= ~AFI_PEX_CTRL_RST;
576 afi_writel(port->pcie, value, ctrl);
578 /* disable reference clock */
579 value = afi_readl(port->pcie, ctrl);
581 if (soc->has_pex_clkreq_en)
582 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
584 value &= ~AFI_PEX_CTRL_REFCLK_EN;
585 afi_writel(port->pcie, value, ctrl);
588 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
590 struct tegra_pcie *pcie = port->pcie;
591 struct device *dev = pcie->dev;
593 devm_iounmap(dev, port->base);
594 devm_release_mem_region(dev, port->regs.start,
595 resource_size(&port->regs));
596 list_del(&port->list);
597 devm_kfree(dev, port);
600 /* Tegra PCIE root complex wrongly reports device class */
601 static void tegra_pcie_fixup_class(struct pci_dev *dev)
603 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
605 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
606 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
607 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
608 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
610 /* Tegra20 and Tegra30 PCIE requires relaxed ordering */
611 static void tegra_pcie_relax_enable(struct pci_dev *dev)
613 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
615 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_relax_enable);
616 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_relax_enable);
617 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_relax_enable);
618 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_relax_enable);
620 static int tegra_pcie_request_resources(struct tegra_pcie *pcie)
622 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
623 struct list_head *windows = &host->windows;
624 struct device *dev = pcie->dev;
627 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
628 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
629 pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem);
630 pci_add_resource(windows, &pcie->busn);
632 err = devm_request_pci_bus_resources(dev, windows);
636 pci_remap_iospace(&pcie->pio, pcie->io.start);
641 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
643 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
644 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
647 tegra_cpuidle_pcie_irqs_in_use();
649 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
656 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
658 const char *err_msg[] = {
666 "Response decoding error",
667 "AXI response decoding error",
668 "Transaction timeout",
669 "Slot present pin change",
670 "Slot clock request change",
671 "TMS clock ramp change",
672 "TMS ready for power down",
675 struct tegra_pcie *pcie = arg;
676 struct device *dev = pcie->dev;
679 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
680 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
681 afi_writel(pcie, 0, AFI_INTR_CODE);
683 if (code == AFI_INTR_LEGACY)
686 if (code >= ARRAY_SIZE(err_msg))
690 * do not pollute kernel log with master abort reports since they
691 * happen a lot during enumeration
693 if (code == AFI_INTR_MASTER_ABORT)
694 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
696 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
698 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
699 code == AFI_INTR_FPCI_DECODE_ERROR) {
700 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
701 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
703 if (code == AFI_INTR_MASTER_ABORT)
704 dev_dbg(dev, " FPCI address: %10llx\n", address);
706 dev_err(dev, " FPCI address: %10llx\n", address);
713 * FPCI map is as follows:
714 * - 0xfdfc000000: I/O space
715 * - 0xfdfe000000: type 0 configuration space
716 * - 0xfdff000000: type 1 configuration space
717 * - 0xfe00000000: type 0 extended configuration space
718 * - 0xfe10000000: type 1 extended configuration space
720 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
722 u32 fpci_bar, size, axi_address;
724 /* Bar 0: type 1 extended configuration space */
725 fpci_bar = 0xfe100000;
726 size = resource_size(pcie->cs);
727 axi_address = pcie->cs->start;
728 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
729 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
730 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
732 /* Bar 1: downstream IO bar */
733 fpci_bar = 0xfdfc0000;
734 size = resource_size(&pcie->io);
735 axi_address = pcie->io.start;
736 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
737 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
738 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
740 /* Bar 2: prefetchable memory BAR */
741 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
742 size = resource_size(&pcie->prefetch);
743 axi_address = pcie->prefetch.start;
744 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
745 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
746 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
748 /* Bar 3: non prefetchable memory BAR */
749 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
750 size = resource_size(&pcie->mem);
751 axi_address = pcie->mem.start;
752 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
753 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
754 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
756 /* NULL out the remaining BARs as they are not used */
757 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
758 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
759 afi_writel(pcie, 0, AFI_FPCI_BAR4);
761 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
762 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
763 afi_writel(pcie, 0, AFI_FPCI_BAR5);
765 /* map all upstream transactions as uncached */
766 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
767 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
768 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
769 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
771 /* MSI translations are setup only when needed */
772 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
773 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
774 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
775 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
778 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
780 const struct tegra_pcie_soc *soc = pcie->soc;
783 timeout = jiffies + msecs_to_jiffies(timeout);
785 while (time_before(jiffies, timeout)) {
786 value = pads_readl(pcie, soc->pads_pll_ctl);
787 if (value & PADS_PLL_CTL_LOCKDET)
794 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
796 struct device *dev = pcie->dev;
797 const struct tegra_pcie_soc *soc = pcie->soc;
801 /* initialize internal PHY, enable up to 16 PCIE lanes */
802 pads_writel(pcie, 0x0, PADS_CTL_SEL);
804 /* override IDDQ to 1 on all 4 lanes */
805 value = pads_readl(pcie, PADS_CTL);
806 value |= PADS_CTL_IDDQ_1L;
807 pads_writel(pcie, value, PADS_CTL);
810 * Set up PHY PLL inputs select PLLE output as refclock,
811 * set TX ref sel to div10 (not div5).
813 value = pads_readl(pcie, soc->pads_pll_ctl);
814 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
815 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
816 pads_writel(pcie, value, soc->pads_pll_ctl);
819 value = pads_readl(pcie, soc->pads_pll_ctl);
820 value &= ~PADS_PLL_CTL_RST_B4SM;
821 pads_writel(pcie, value, soc->pads_pll_ctl);
823 usleep_range(20, 100);
825 /* take PLL out of reset */
826 value = pads_readl(pcie, soc->pads_pll_ctl);
827 value |= PADS_PLL_CTL_RST_B4SM;
828 pads_writel(pcie, value, soc->pads_pll_ctl);
830 /* wait for the PLL to lock */
831 err = tegra_pcie_pll_wait(pcie, 500);
833 dev_err(dev, "PLL failed to lock: %d\n", err);
837 /* turn off IDDQ override */
838 value = pads_readl(pcie, PADS_CTL);
839 value &= ~PADS_CTL_IDDQ_1L;
840 pads_writel(pcie, value, PADS_CTL);
842 /* enable TX/RX data */
843 value = pads_readl(pcie, PADS_CTL);
844 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
845 pads_writel(pcie, value, PADS_CTL);
850 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
852 const struct tegra_pcie_soc *soc = pcie->soc;
855 /* disable TX/RX data */
856 value = pads_readl(pcie, PADS_CTL);
857 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
858 pads_writel(pcie, value, PADS_CTL);
861 value = pads_readl(pcie, PADS_CTL);
862 value |= PADS_CTL_IDDQ_1L;
863 pads_writel(pcie, value, PADS_CTL);
866 value = pads_readl(pcie, soc->pads_pll_ctl);
867 value &= ~PADS_PLL_CTL_RST_B4SM;
868 pads_writel(pcie, value, soc->pads_pll_ctl);
870 usleep_range(20, 100);
875 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
877 struct device *dev = port->pcie->dev;
881 for (i = 0; i < port->lanes; i++) {
882 err = phy_power_on(port->phys[i]);
884 dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
892 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
894 struct device *dev = port->pcie->dev;
898 for (i = 0; i < port->lanes; i++) {
899 err = phy_power_off(port->phys[i]);
901 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
910 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
912 struct device *dev = pcie->dev;
913 const struct tegra_pcie_soc *soc = pcie->soc;
914 struct tegra_pcie_port *port;
917 if (pcie->legacy_phy) {
919 err = phy_power_on(pcie->phy);
921 err = tegra_pcie_phy_enable(pcie);
924 dev_err(dev, "failed to power on PHY: %d\n", err);
929 list_for_each_entry(port, &pcie->ports, list) {
930 err = tegra_pcie_port_phy_power_on(port);
933 "failed to power on PCIe port %u PHY: %d\n",
939 /* Configure the reference clock driver */
940 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
942 if (soc->num_ports > 2)
943 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
948 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
950 struct device *dev = pcie->dev;
951 struct tegra_pcie_port *port;
954 if (pcie->legacy_phy) {
956 err = phy_power_off(pcie->phy);
958 err = tegra_pcie_phy_disable(pcie);
961 dev_err(dev, "failed to power off PHY: %d\n", err);
966 list_for_each_entry(port, &pcie->ports, list) {
967 err = tegra_pcie_port_phy_power_off(port);
970 "failed to power off PCIe port %u PHY: %d\n",
979 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
981 struct device *dev = pcie->dev;
982 const struct tegra_pcie_soc *soc = pcie->soc;
983 struct tegra_pcie_port *port;
987 /* enable PLL power down */
989 value = afi_readl(pcie, AFI_PLLE_CONTROL);
990 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
991 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
992 afi_writel(pcie, value, AFI_PLLE_CONTROL);
995 /* power down PCIe slot clock bias pad */
996 if (soc->has_pex_bias_ctrl)
997 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
999 /* configure mode and disable all ports */
1000 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1001 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1002 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1004 list_for_each_entry(port, &pcie->ports, list)
1005 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1007 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1009 if (soc->has_gen2) {
1010 value = afi_readl(pcie, AFI_FUSE);
1011 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1012 afi_writel(pcie, value, AFI_FUSE);
1014 value = afi_readl(pcie, AFI_FUSE);
1015 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1016 afi_writel(pcie, value, AFI_FUSE);
1019 err = tegra_pcie_phy_power_on(pcie);
1021 dev_err(dev, "failed to power on PHY(s): %d\n", err);
1025 /* take the PCIe interface module out of reset */
1026 reset_control_deassert(pcie->pcie_xrst);
1028 /* finally enable PCIe */
1029 value = afi_readl(pcie, AFI_CONFIGURATION);
1030 value |= AFI_CONFIGURATION_EN_FPCI;
1031 afi_writel(pcie, value, AFI_CONFIGURATION);
1033 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1034 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1035 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
1037 if (soc->has_intr_prsnt_sense)
1038 value |= AFI_INTR_EN_PRSNT_SENSE;
1040 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1041 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1043 /* don't enable MSI for now, only when needed */
1044 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1046 /* disable all exceptions */
1047 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1052 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1054 struct device *dev = pcie->dev;
1057 /* TODO: disable and unprepare clocks? */
1059 err = tegra_pcie_phy_power_off(pcie);
1061 dev_err(dev, "failed to power off PHY(s): %d\n", err);
1063 reset_control_assert(pcie->pcie_xrst);
1064 reset_control_assert(pcie->afi_rst);
1065 reset_control_assert(pcie->pex_rst);
1067 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1069 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1071 dev_warn(dev, "failed to disable regulators: %d\n", err);
1074 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1076 struct device *dev = pcie->dev;
1077 const struct tegra_pcie_soc *soc = pcie->soc;
1080 reset_control_assert(pcie->pcie_xrst);
1081 reset_control_assert(pcie->afi_rst);
1082 reset_control_assert(pcie->pex_rst);
1084 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1086 /* enable regulators */
1087 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1089 dev_err(dev, "failed to enable regulators: %d\n", err);
1091 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
1095 dev_err(dev, "powerup sequence failed: %d\n", err);
1099 reset_control_deassert(pcie->afi_rst);
1101 err = clk_prepare_enable(pcie->afi_clk);
1103 dev_err(dev, "failed to enable AFI clock: %d\n", err);
1107 if (soc->has_cml_clk) {
1108 err = clk_prepare_enable(pcie->cml_clk);
1110 dev_err(dev, "failed to enable CML clock: %d\n", err);
1115 err = clk_prepare_enable(pcie->pll_e);
1117 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1124 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1126 struct device *dev = pcie->dev;
1127 const struct tegra_pcie_soc *soc = pcie->soc;
1129 pcie->pex_clk = devm_clk_get(dev, "pex");
1130 if (IS_ERR(pcie->pex_clk))
1131 return PTR_ERR(pcie->pex_clk);
1133 pcie->afi_clk = devm_clk_get(dev, "afi");
1134 if (IS_ERR(pcie->afi_clk))
1135 return PTR_ERR(pcie->afi_clk);
1137 pcie->pll_e = devm_clk_get(dev, "pll_e");
1138 if (IS_ERR(pcie->pll_e))
1139 return PTR_ERR(pcie->pll_e);
1141 if (soc->has_cml_clk) {
1142 pcie->cml_clk = devm_clk_get(dev, "cml");
1143 if (IS_ERR(pcie->cml_clk))
1144 return PTR_ERR(pcie->cml_clk);
1150 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1152 struct device *dev = pcie->dev;
1154 pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1155 if (IS_ERR(pcie->pex_rst))
1156 return PTR_ERR(pcie->pex_rst);
1158 pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1159 if (IS_ERR(pcie->afi_rst))
1160 return PTR_ERR(pcie->afi_rst);
1162 pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1163 if (IS_ERR(pcie->pcie_xrst))
1164 return PTR_ERR(pcie->pcie_xrst);
1169 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1171 struct device *dev = pcie->dev;
1174 pcie->phy = devm_phy_optional_get(dev, "pcie");
1175 if (IS_ERR(pcie->phy)) {
1176 err = PTR_ERR(pcie->phy);
1177 dev_err(dev, "failed to get PHY: %d\n", err);
1181 err = phy_init(pcie->phy);
1183 dev_err(dev, "failed to initialize PHY: %d\n", err);
1187 pcie->legacy_phy = true;
1192 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1193 struct device_node *np,
1194 const char *consumer,
1200 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1202 return ERR_PTR(-ENOMEM);
1204 phy = devm_of_phy_get(dev, np, name);
1207 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1213 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1215 struct device *dev = port->pcie->dev;
1220 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1224 for (i = 0; i < port->lanes; i++) {
1225 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1227 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1229 return PTR_ERR(phy);
1232 err = phy_init(phy);
1234 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1239 port->phys[i] = phy;
1245 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1247 const struct tegra_pcie_soc *soc = pcie->soc;
1248 struct device_node *np = pcie->dev->of_node;
1249 struct tegra_pcie_port *port;
1252 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1253 return tegra_pcie_phys_get_legacy(pcie);
1255 list_for_each_entry(port, &pcie->ports, list) {
1256 err = tegra_pcie_port_get_phys(port);
1264 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1266 struct device *dev = pcie->dev;
1267 struct platform_device *pdev = to_platform_device(dev);
1268 struct resource *pads, *afi, *res;
1271 err = tegra_pcie_clocks_get(pcie);
1273 dev_err(dev, "failed to get clocks: %d\n", err);
1277 err = tegra_pcie_resets_get(pcie);
1279 dev_err(dev, "failed to get resets: %d\n", err);
1283 err = tegra_pcie_phys_get(pcie);
1285 dev_err(dev, "failed to get PHYs: %d\n", err);
1289 err = tegra_pcie_power_on(pcie);
1291 dev_err(dev, "failed to power up: %d\n", err);
1295 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1296 pcie->pads = devm_ioremap_resource(dev, pads);
1297 if (IS_ERR(pcie->pads)) {
1298 err = PTR_ERR(pcie->pads);
1302 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1303 pcie->afi = devm_ioremap_resource(dev, afi);
1304 if (IS_ERR(pcie->afi)) {
1305 err = PTR_ERR(pcie->afi);
1309 /* request configuration space, but remap later, on demand */
1310 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1312 err = -EADDRNOTAVAIL;
1316 pcie->cs = devm_request_mem_region(dev, res->start,
1317 resource_size(res), res->name);
1319 err = -EADDRNOTAVAIL;
1323 /* request interrupt */
1324 err = platform_get_irq_byname(pdev, "intr");
1326 dev_err(dev, "failed to get IRQ: %d\n", err);
1332 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1334 dev_err(dev, "failed to register IRQ: %d\n", err);
1341 tegra_pcie_power_off(pcie);
1345 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1347 struct device *dev = pcie->dev;
1351 free_irq(pcie->irq, pcie);
1353 tegra_pcie_power_off(pcie);
1355 err = phy_exit(pcie->phy);
1357 dev_err(dev, "failed to teardown PHY: %d\n", err);
1362 static int tegra_msi_alloc(struct tegra_msi *chip)
1366 mutex_lock(&chip->lock);
1368 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1369 if (msi < INT_PCI_MSI_NR)
1370 set_bit(msi, chip->used);
1374 mutex_unlock(&chip->lock);
1379 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1381 struct device *dev = chip->chip.dev;
1383 mutex_lock(&chip->lock);
1385 if (!test_bit(irq, chip->used))
1386 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1388 clear_bit(irq, chip->used);
1390 mutex_unlock(&chip->lock);
1393 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1395 struct tegra_pcie *pcie = data;
1396 struct device *dev = pcie->dev;
1397 struct tegra_msi *msi = &pcie->msi;
1398 unsigned int i, processed = 0;
1400 for (i = 0; i < 8; i++) {
1401 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1404 unsigned int offset = find_first_bit(®, 32);
1405 unsigned int index = i * 32 + offset;
1408 /* clear the interrupt */
1409 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1411 irq = irq_find_mapping(msi->domain, index);
1413 if (test_bit(index, msi->used))
1414 generic_handle_irq(irq);
1416 dev_info(dev, "unhandled MSI\n");
1419 * that's weird who triggered this?
1422 dev_info(dev, "unexpected MSI\n");
1425 /* see if there's any more pending in this vector */
1426 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1432 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1435 static int tegra_msi_setup_irq(struct msi_controller *chip,
1436 struct pci_dev *pdev, struct msi_desc *desc)
1438 struct tegra_msi *msi = to_tegra_msi(chip);
1443 hwirq = tegra_msi_alloc(msi);
1447 irq = irq_create_mapping(msi->domain, hwirq);
1449 tegra_msi_free(msi, hwirq);
1453 irq_set_msi_desc(irq, desc);
1455 msg.address_lo = lower_32_bits(msi->phys);
1456 msg.address_hi = upper_32_bits(msi->phys);
1459 pci_write_msi_msg(irq, &msg);
1464 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1467 struct tegra_msi *msi = to_tegra_msi(chip);
1468 struct irq_data *d = irq_get_irq_data(irq);
1469 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1471 irq_dispose_mapping(irq);
1472 tegra_msi_free(msi, hwirq);
1475 static struct irq_chip tegra_msi_irq_chip = {
1476 .name = "Tegra PCIe MSI",
1477 .irq_enable = pci_msi_unmask_irq,
1478 .irq_disable = pci_msi_mask_irq,
1479 .irq_mask = pci_msi_mask_irq,
1480 .irq_unmask = pci_msi_unmask_irq,
1483 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1484 irq_hw_number_t hwirq)
1486 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1487 irq_set_chip_data(irq, domain->host_data);
1489 tegra_cpuidle_pcie_irqs_in_use();
1494 static const struct irq_domain_ops msi_domain_ops = {
1495 .map = tegra_msi_map,
1498 static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1500 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1501 struct platform_device *pdev = to_platform_device(pcie->dev);
1502 const struct tegra_pcie_soc *soc = pcie->soc;
1503 struct tegra_msi *msi = &pcie->msi;
1504 struct device *dev = pcie->dev;
1508 mutex_init(&msi->lock);
1510 msi->chip.dev = dev;
1511 msi->chip.setup_irq = tegra_msi_setup_irq;
1512 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1514 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
1515 &msi_domain_ops, &msi->chip);
1517 dev_err(dev, "failed to create IRQ domain\n");
1521 err = platform_get_irq_byname(pdev, "msi");
1523 dev_err(dev, "failed to get IRQ: %d\n", err);
1529 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1530 tegra_msi_irq_chip.name, pcie);
1532 dev_err(dev, "failed to request IRQ: %d\n", err);
1536 /* setup AFI/FPCI range */
1537 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1538 msi->phys = virt_to_phys((void *)msi->pages);
1540 afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1541 afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1542 /* this register is in 4K increments */
1543 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1545 /* enable all MSI vectors */
1546 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1547 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1548 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1549 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1550 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1551 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1552 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1553 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1555 /* and unmask the MSI interrupt */
1556 reg = afi_readl(pcie, AFI_INTR_MASK);
1557 reg |= AFI_INTR_MASK_MSI_MASK;
1558 afi_writel(pcie, reg, AFI_INTR_MASK);
1560 host->msi = &msi->chip;
1565 irq_domain_remove(msi->domain);
1569 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1571 struct tegra_msi *msi = &pcie->msi;
1572 unsigned int i, irq;
1575 /* mask the MSI interrupt */
1576 value = afi_readl(pcie, AFI_INTR_MASK);
1577 value &= ~AFI_INTR_MASK_MSI_MASK;
1578 afi_writel(pcie, value, AFI_INTR_MASK);
1580 /* disable all MSI vectors */
1581 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1582 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1583 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1584 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1585 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1586 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1587 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1588 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1590 free_pages(msi->pages, 0);
1593 free_irq(msi->irq, pcie);
1595 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1596 irq = irq_find_mapping(msi->domain, i);
1598 irq_dispose_mapping(irq);
1601 irq_domain_remove(msi->domain);
1606 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1609 struct device *dev = pcie->dev;
1610 struct device_node *np = dev->of_node;
1612 if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1613 of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1616 dev_info(dev, "4x1, 1x1 configuration\n");
1617 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1621 dev_info(dev, "2x1, 1x1 configuration\n");
1622 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1625 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1628 dev_info(dev, "4x1, 2x1 configuration\n");
1629 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1633 dev_info(dev, "2x3 configuration\n");
1634 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1638 dev_info(dev, "4x1, 1x2 configuration\n");
1639 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1642 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1645 dev_info(dev, "single-mode configuration\n");
1646 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1650 dev_info(dev, "dual-mode configuration\n");
1651 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1660 * Check whether a given set of supplies is available in a device tree node.
1661 * This is used to check whether the new or the legacy device tree bindings
1664 static bool of_regulator_bulk_available(struct device_node *np,
1665 struct regulator_bulk_data *supplies,
1666 unsigned int num_supplies)
1671 for (i = 0; i < num_supplies; i++) {
1672 snprintf(property, 32, "%s-supply", supplies[i].supply);
1674 if (of_find_property(np, property, NULL) == NULL)
1682 * Old versions of the device tree binding for this device used a set of power
1683 * supplies that didn't match the hardware inputs. This happened to work for a
1684 * number of cases but is not future proof. However to preserve backwards-
1685 * compatibility with old device trees, this function will try to use the old
1688 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1690 struct device *dev = pcie->dev;
1691 struct device_node *np = dev->of_node;
1693 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1694 pcie->num_supplies = 3;
1695 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1696 pcie->num_supplies = 2;
1698 if (pcie->num_supplies == 0) {
1699 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1703 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1704 sizeof(*pcie->supplies),
1706 if (!pcie->supplies)
1709 pcie->supplies[0].supply = "pex-clk";
1710 pcie->supplies[1].supply = "vdd";
1712 if (pcie->num_supplies > 2)
1713 pcie->supplies[2].supply = "avdd";
1715 return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1719 * Obtains the list of regulators required for a particular generation of the
1722 * This would've been nice to do simply by providing static tables for use
1723 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1724 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1725 * and either seems to be optional depending on which ports are being used.
1727 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1729 struct device *dev = pcie->dev;
1730 struct device_node *np = dev->of_node;
1733 if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1734 pcie->num_supplies = 6;
1736 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1737 sizeof(*pcie->supplies),
1739 if (!pcie->supplies)
1742 pcie->supplies[i++].supply = "avdd-pll-uerefe";
1743 pcie->supplies[i++].supply = "hvddio-pex";
1744 pcie->supplies[i++].supply = "dvddio-pex";
1745 pcie->supplies[i++].supply = "dvdd-pex-pll";
1746 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1747 pcie->supplies[i++].supply = "vddio-pex-ctl";
1748 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1749 pcie->num_supplies = 7;
1751 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1752 sizeof(*pcie->supplies),
1754 if (!pcie->supplies)
1757 pcie->supplies[i++].supply = "avddio-pex";
1758 pcie->supplies[i++].supply = "dvddio-pex";
1759 pcie->supplies[i++].supply = "avdd-pex-pll";
1760 pcie->supplies[i++].supply = "hvdd-pex";
1761 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1762 pcie->supplies[i++].supply = "vddio-pex-ctl";
1763 pcie->supplies[i++].supply = "avdd-pll-erefe";
1764 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1765 bool need_pexa = false, need_pexb = false;
1767 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1768 if (lane_mask & 0x0f)
1771 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1772 if (lane_mask & 0x30)
1775 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1776 (need_pexb ? 2 : 0);
1778 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1779 sizeof(*pcie->supplies),
1781 if (!pcie->supplies)
1784 pcie->supplies[i++].supply = "avdd-pex-pll";
1785 pcie->supplies[i++].supply = "hvdd-pex";
1786 pcie->supplies[i++].supply = "vddio-pex-ctl";
1787 pcie->supplies[i++].supply = "avdd-plle";
1790 pcie->supplies[i++].supply = "avdd-pexa";
1791 pcie->supplies[i++].supply = "vdd-pexa";
1795 pcie->supplies[i++].supply = "avdd-pexb";
1796 pcie->supplies[i++].supply = "vdd-pexb";
1798 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1799 pcie->num_supplies = 5;
1801 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1802 sizeof(*pcie->supplies),
1804 if (!pcie->supplies)
1807 pcie->supplies[0].supply = "avdd-pex";
1808 pcie->supplies[1].supply = "vdd-pex";
1809 pcie->supplies[2].supply = "avdd-pex-pll";
1810 pcie->supplies[3].supply = "avdd-plle";
1811 pcie->supplies[4].supply = "vddio-pex-clk";
1814 if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
1815 pcie->num_supplies))
1816 return devm_regulator_bulk_get(dev, pcie->num_supplies,
1820 * If not all regulators are available for this new scheme, assume
1821 * that the device tree complies with an older version of the device
1824 dev_info(dev, "using legacy DT binding for power supplies\n");
1826 devm_kfree(dev, pcie->supplies);
1827 pcie->num_supplies = 0;
1829 return tegra_pcie_get_legacy_regulators(pcie);
1832 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1834 struct device *dev = pcie->dev;
1835 struct device_node *np = dev->of_node, *port;
1836 const struct tegra_pcie_soc *soc = pcie->soc;
1837 struct of_pci_range_parser parser;
1838 struct of_pci_range range;
1839 u32 lanes = 0, mask = 0;
1840 unsigned int lane = 0;
1841 struct resource res;
1844 if (of_pci_range_parser_init(&parser, np)) {
1845 dev_err(dev, "missing \"ranges\" property\n");
1849 for_each_of_pci_range(&parser, &range) {
1850 err = of_pci_range_to_resource(&range, np, &res);
1854 switch (res.flags & IORESOURCE_TYPE_BITS) {
1856 /* Track the bus -> CPU I/O mapping offset. */
1857 pcie->offset.io = res.start - range.pci_addr;
1859 memcpy(&pcie->pio, &res, sizeof(res));
1860 pcie->pio.name = np->full_name;
1863 * The Tegra PCIe host bridge uses this to program the
1864 * mapping of the I/O space to the physical address,
1865 * so we override the .start and .end fields here that
1866 * of_pci_range_to_resource() converted to I/O space.
1867 * We also set the IORESOURCE_MEM type to clarify that
1868 * the resource is in the physical memory space.
1870 pcie->io.start = range.cpu_addr;
1871 pcie->io.end = range.cpu_addr + range.size - 1;
1872 pcie->io.flags = IORESOURCE_MEM;
1873 pcie->io.name = "I/O";
1875 memcpy(&res, &pcie->io, sizeof(res));
1878 case IORESOURCE_MEM:
1880 * Track the bus -> CPU memory mapping offset. This
1881 * assumes that the prefetchable and non-prefetchable
1882 * regions will be the last of type IORESOURCE_MEM in
1883 * the ranges property.
1885 pcie->offset.mem = res.start - range.pci_addr;
1887 if (res.flags & IORESOURCE_PREFETCH) {
1888 memcpy(&pcie->prefetch, &res, sizeof(res));
1889 pcie->prefetch.name = "prefetchable";
1891 memcpy(&pcie->mem, &res, sizeof(res));
1892 pcie->mem.name = "non-prefetchable";
1898 err = of_pci_parse_bus_range(np, &pcie->busn);
1900 dev_err(dev, "failed to parse ranges property: %d\n", err);
1901 pcie->busn.name = np->name;
1902 pcie->busn.start = 0;
1903 pcie->busn.end = 0xff;
1904 pcie->busn.flags = IORESOURCE_BUS;
1907 /* parse root ports */
1908 for_each_child_of_node(np, port) {
1909 struct tegra_pcie_port *rp;
1913 err = of_pci_get_devfn(port);
1915 dev_err(dev, "failed to parse address: %d\n", err);
1919 index = PCI_SLOT(err);
1921 if (index < 1 || index > soc->num_ports) {
1922 dev_err(dev, "invalid port number: %d\n", index);
1929 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1931 dev_err(dev, "failed to parse # of lanes: %d\n",
1937 dev_err(dev, "invalid # of lanes: %u\n", value);
1942 lanes |= value << (index << 3);
1944 if (!of_device_is_available(port)) {
1949 mask |= ((1 << value) - 1) << lane;
1952 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
1958 err = of_address_to_resource(port, 0, &rp->regs);
1960 dev_err(dev, "failed to parse address: %d\n", err);
1964 INIT_LIST_HEAD(&rp->list);
1970 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
1971 if (IS_ERR(rp->base))
1972 return PTR_ERR(rp->base);
1974 list_add_tail(&rp->list, &pcie->ports);
1977 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1979 dev_err(dev, "invalid lane configuration\n");
1983 err = tegra_pcie_get_regulators(pcie, mask);
1995 * FIXME: If there are no PCIe cards attached, then calling this function
1996 * can result in the increase of the bootup time as there are big timeout
1999 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
2000 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2002 struct device *dev = port->pcie->dev;
2003 unsigned int retries = 3;
2004 unsigned long value;
2006 /* override presence detection */
2007 value = readl(port->base + RP_PRIV_MISC);
2008 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2009 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2010 writel(value, port->base + RP_PRIV_MISC);
2013 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2016 value = readl(port->base + RP_VEND_XP);
2018 if (value & RP_VEND_XP_DL_UP)
2021 usleep_range(1000, 2000);
2022 } while (--timeout);
2025 dev_err(dev, "link %u down, retrying\n", port->index);
2029 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2032 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2034 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2037 usleep_range(1000, 2000);
2038 } while (--timeout);
2041 tegra_pcie_port_reset(port);
2042 } while (--retries);
2047 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2049 struct device *dev = pcie->dev;
2050 struct tegra_pcie_port *port, *tmp;
2052 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2053 dev_info(dev, "probing port %u, using %u lanes\n",
2054 port->index, port->lanes);
2056 tegra_pcie_port_enable(port);
2058 if (tegra_pcie_port_check_link(port))
2061 dev_info(dev, "link %u down, ignoring\n", port->index);
2063 tegra_pcie_port_disable(port);
2064 tegra_pcie_port_free(port);
2068 static const struct tegra_pcie_soc tegra20_pcie = {
2070 .msi_base_shift = 0,
2071 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2072 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2073 .pads_refclk_cfg0 = 0xfa5cfa5c,
2074 .has_pex_clkreq_en = false,
2075 .has_pex_bias_ctrl = false,
2076 .has_intr_prsnt_sense = false,
2077 .has_cml_clk = false,
2079 .force_pca_enable = false,
2082 static const struct tegra_pcie_soc tegra30_pcie = {
2084 .msi_base_shift = 8,
2085 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2086 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2087 .pads_refclk_cfg0 = 0xfa5cfa5c,
2088 .pads_refclk_cfg1 = 0xfa5cfa5c,
2089 .has_pex_clkreq_en = true,
2090 .has_pex_bias_ctrl = true,
2091 .has_intr_prsnt_sense = true,
2092 .has_cml_clk = true,
2094 .force_pca_enable = false,
2097 static const struct tegra_pcie_soc tegra124_pcie = {
2099 .msi_base_shift = 8,
2100 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2101 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2102 .pads_refclk_cfg0 = 0x44ac44ac,
2103 .has_pex_clkreq_en = true,
2104 .has_pex_bias_ctrl = true,
2105 .has_intr_prsnt_sense = true,
2106 .has_cml_clk = true,
2108 .force_pca_enable = false,
2111 static const struct tegra_pcie_soc tegra210_pcie = {
2113 .msi_base_shift = 8,
2114 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2115 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2116 .pads_refclk_cfg0 = 0x90b890b8,
2117 .has_pex_clkreq_en = true,
2118 .has_pex_bias_ctrl = true,
2119 .has_intr_prsnt_sense = true,
2120 .has_cml_clk = true,
2122 .force_pca_enable = true,
2125 static const struct of_device_id tegra_pcie_of_match[] = {
2126 { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2127 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2128 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2129 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2133 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2135 struct tegra_pcie *pcie = s->private;
2137 if (list_empty(&pcie->ports))
2140 seq_printf(s, "Index Status\n");
2142 return seq_list_start(&pcie->ports, *pos);
2145 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2147 struct tegra_pcie *pcie = s->private;
2149 return seq_list_next(v, &pcie->ports, pos);
2152 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2156 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2158 bool up = false, active = false;
2159 struct tegra_pcie_port *port;
2162 port = list_entry(v, struct tegra_pcie_port, list);
2164 value = readl(port->base + RP_VEND_XP);
2166 if (value & RP_VEND_XP_DL_UP)
2169 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2171 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2174 seq_printf(s, "%2u ", port->index);
2177 seq_printf(s, "up");
2181 seq_printf(s, ", ");
2183 seq_printf(s, "active");
2186 seq_printf(s, "\n");
2190 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2191 .start = tegra_pcie_ports_seq_start,
2192 .next = tegra_pcie_ports_seq_next,
2193 .stop = tegra_pcie_ports_seq_stop,
2194 .show = tegra_pcie_ports_seq_show,
2197 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2199 struct tegra_pcie *pcie = inode->i_private;
2203 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2207 s = file->private_data;
2213 static const struct file_operations tegra_pcie_ports_ops = {
2214 .owner = THIS_MODULE,
2215 .open = tegra_pcie_ports_open,
2217 .llseek = seq_lseek,
2218 .release = seq_release,
2221 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2223 struct dentry *file;
2225 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2229 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2230 pcie, &tegra_pcie_ports_ops);
2237 debugfs_remove_recursive(pcie->debugfs);
2238 pcie->debugfs = NULL;
2242 static int tegra_pcie_probe(struct platform_device *pdev)
2244 struct device *dev = &pdev->dev;
2245 struct pci_host_bridge *host;
2246 struct tegra_pcie *pcie;
2247 struct pci_bus *child;
2250 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2254 pcie = pci_host_bridge_priv(host);
2256 pcie->soc = of_device_get_match_data(dev);
2257 INIT_LIST_HEAD(&pcie->buses);
2258 INIT_LIST_HEAD(&pcie->ports);
2261 err = tegra_pcie_parse_dt(pcie);
2265 err = tegra_pcie_get_resources(pcie);
2267 dev_err(dev, "failed to request resources: %d\n", err);
2271 err = tegra_pcie_enable_controller(pcie);
2275 err = tegra_pcie_request_resources(pcie);
2279 /* setup the AFI address translations */
2280 tegra_pcie_setup_translations(pcie);
2282 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2283 err = tegra_pcie_enable_msi(pcie);
2285 dev_err(dev, "failed to enable MSI support: %d\n", err);
2290 tegra_pcie_enable_ports(pcie);
2292 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
2293 host->busnr = pcie->busn.start;
2294 host->dev.parent = &pdev->dev;
2295 host->ops = &tegra_pcie_ops;
2296 host->map_irq = tegra_pcie_map_irq;
2297 host->swizzle_irq = pci_common_swizzle;
2299 err = pci_scan_root_bus_bridge(host);
2301 dev_err(dev, "failed to register host: %d\n", err);
2305 pci_bus_size_bridges(host->bus);
2306 pci_bus_assign_resources(host->bus);
2308 list_for_each_entry(child, &host->bus->children, node)
2309 pcie_bus_configure_settings(child);
2311 pci_bus_add_devices(host->bus);
2313 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2314 err = tegra_pcie_debugfs_init(pcie);
2316 dev_err(dev, "failed to setup debugfs: %d\n", err);
2322 if (IS_ENABLED(CONFIG_PCI_MSI))
2323 tegra_pcie_disable_msi(pcie);
2325 tegra_pcie_put_resources(pcie);
2329 static struct platform_driver tegra_pcie_driver = {
2331 .name = "tegra-pcie",
2332 .of_match_table = tegra_pcie_of_match,
2333 .suppress_bind_attrs = true,
2335 .probe = tegra_pcie_probe,
2337 builtin_platform_driver(tegra_pcie_driver);