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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <linux/clk.h>
28 #include <linux/clk/tegra.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/interrupt.h>
32 #include <linux/irq.h>
33 #include <linux/irqdomain.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/msi.h>
37 #include <linux/of_address.h>
38 #include <linux/of_pci.h>
39 #include <linux/of_platform.h>
40 #include <linux/pci.h>
41 #include <linux/platform_device.h>
42 #include <linux/sizes.h>
43 #include <linux/slab.h>
44 #include <linux/tegra-powergate.h>
45 #include <linux/vmalloc.h>
46 #include <linux/regulator/consumer.h>
48 #include <asm/mach/irq.h>
49 #include <asm/mach/map.h>
50 #include <asm/mach/pci.h>
52 #define INT_PCI_MSI_NR (8 * 32)
54 /* register definitions */
56 #define AFI_AXI_BAR0_SZ 0x00
57 #define AFI_AXI_BAR1_SZ 0x04
58 #define AFI_AXI_BAR2_SZ 0x08
59 #define AFI_AXI_BAR3_SZ 0x0c
60 #define AFI_AXI_BAR4_SZ 0x10
61 #define AFI_AXI_BAR5_SZ 0x14
63 #define AFI_AXI_BAR0_START 0x18
64 #define AFI_AXI_BAR1_START 0x1c
65 #define AFI_AXI_BAR2_START 0x20
66 #define AFI_AXI_BAR3_START 0x24
67 #define AFI_AXI_BAR4_START 0x28
68 #define AFI_AXI_BAR5_START 0x2c
70 #define AFI_FPCI_BAR0 0x30
71 #define AFI_FPCI_BAR1 0x34
72 #define AFI_FPCI_BAR2 0x38
73 #define AFI_FPCI_BAR3 0x3c
74 #define AFI_FPCI_BAR4 0x40
75 #define AFI_FPCI_BAR5 0x44
77 #define AFI_CACHE_BAR0_SZ 0x48
78 #define AFI_CACHE_BAR0_ST 0x4c
79 #define AFI_CACHE_BAR1_SZ 0x50
80 #define AFI_CACHE_BAR1_ST 0x54
82 #define AFI_MSI_BAR_SZ 0x60
83 #define AFI_MSI_FPCI_BAR_ST 0x64
84 #define AFI_MSI_AXI_BAR_ST 0x68
86 #define AFI_MSI_VEC0 0x6c
87 #define AFI_MSI_VEC1 0x70
88 #define AFI_MSI_VEC2 0x74
89 #define AFI_MSI_VEC3 0x78
90 #define AFI_MSI_VEC4 0x7c
91 #define AFI_MSI_VEC5 0x80
92 #define AFI_MSI_VEC6 0x84
93 #define AFI_MSI_VEC7 0x88
95 #define AFI_MSI_EN_VEC0 0x8c
96 #define AFI_MSI_EN_VEC1 0x90
97 #define AFI_MSI_EN_VEC2 0x94
98 #define AFI_MSI_EN_VEC3 0x98
99 #define AFI_MSI_EN_VEC4 0x9c
100 #define AFI_MSI_EN_VEC5 0xa0
101 #define AFI_MSI_EN_VEC6 0xa4
102 #define AFI_MSI_EN_VEC7 0xa8
104 #define AFI_CONFIGURATION 0xac
105 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
107 #define AFI_FPCI_ERROR_MASKS 0xb0
109 #define AFI_INTR_MASK 0xb4
110 #define AFI_INTR_MASK_INT_MASK (1 << 0)
111 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
113 #define AFI_INTR_CODE 0xb8
114 #define AFI_INTR_CODE_MASK 0xf
115 #define AFI_INTR_AXI_SLAVE_ERROR 1
116 #define AFI_INTR_AXI_DECODE_ERROR 2
117 #define AFI_INTR_TARGET_ABORT 3
118 #define AFI_INTR_MASTER_ABORT 4
119 #define AFI_INTR_INVALID_WRITE 5
120 #define AFI_INTR_LEGACY 6
121 #define AFI_INTR_FPCI_DECODE_ERROR 7
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_CONFIG 0x0f8
147 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
148 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
149 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
150 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
151 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
152 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
153 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
154 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
156 #define AFI_FUSE 0x104
157 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
159 #define AFI_PEX0_CTRL 0x110
160 #define AFI_PEX1_CTRL 0x118
161 #define AFI_PEX2_CTRL 0x128
162 #define AFI_PEX_CTRL_RST (1 << 0)
163 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
164 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
166 #define AFI_PEXBIAS_CTRL_0 0x168
168 #define RP_VEND_XP 0x00000F00
169 #define RP_VEND_XP_DL_UP (1 << 30)
171 #define RP_LINK_CONTROL_STATUS 0x00000090
172 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
173 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
175 #define PADS_CTL_SEL 0x0000009C
177 #define PADS_CTL 0x000000A0
178 #define PADS_CTL_IDDQ_1L (1 << 0)
179 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
180 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
182 #define PADS_PLL_CTL_TEGRA20 0x000000B8
183 #define PADS_PLL_CTL_TEGRA30 0x000000B4
184 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
185 #define PADS_PLL_CTL_LOCKDET (1 << 8)
186 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
187 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
188 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
189 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
190 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
191 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
192 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
193 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
195 #define PADS_REFCLK_CFG0 0x000000C8
196 #define PADS_REFCLK_CFG1 0x000000CC
199 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
200 * entries, one entry per PCIe port. These field definitions and desired
201 * values aren't in the TRM, but do come from NVIDIA.
203 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
204 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
205 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
206 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
208 /* Default value provided by HW engineering is 0xfa5c */
209 #define PADS_REFCLK_CFG_VALUE \
211 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
212 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
213 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
214 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
218 struct msi_chip chip;
219 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
220 struct irq_domain *domain;
226 /* used to differentiate between Tegra SoC generations */
227 struct tegra_pcie_soc_data {
228 unsigned int num_ports;
229 unsigned int msi_base_shift;
232 bool has_pex_clkreq_en;
233 bool has_pex_bias_ctrl;
234 bool has_intr_prsnt_sense;
235 bool has_avdd_supply;
239 static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
241 return container_of(chip, struct tegra_msi, chip);
251 struct list_head busses;
256 struct resource prefetch;
257 struct resource busn;
261 struct clk *pcie_xclk;
265 struct tegra_msi msi;
267 struct list_head ports;
268 unsigned int num_ports;
271 struct regulator *pex_clk_supply;
272 struct regulator *vdd_supply;
273 struct regulator *avdd_supply;
275 const struct tegra_pcie_soc_data *soc_data;
278 struct tegra_pcie_port {
279 struct tegra_pcie *pcie;
280 struct list_head list;
281 struct resource regs;
287 struct tegra_pcie_bus {
288 struct vm_struct *area;
289 struct list_head list;
293 static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
295 return sys->private_data;
298 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
299 unsigned long offset)
301 writel(value, pcie->afi + offset);
304 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
306 return readl(pcie->afi + offset);
309 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
310 unsigned long offset)
312 writel(value, pcie->pads + offset);
315 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
317 return readl(pcie->pads + offset);
321 * The configuration space mapping on Tegra is somewhat similar to the ECAM
322 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
323 * register accesses are mapped:
325 * [27:24] extended register number
327 * [15:11] device number
328 * [10: 8] function number
329 * [ 7: 0] register number
331 * Mapping the whole extended configuration space would require 256 MiB of
332 * virtual address space, only a small part of which will actually be used.
333 * To work around this, a 1 MiB of virtual addresses are allocated per bus
334 * when the bus is first accessed. When the physical range is mapped, the
335 * the bus number bits are hidden so that the extended register number bits
336 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
338 * [19:16] extended register number
339 * [15:11] device number
340 * [10: 8] function number
341 * [ 7: 0] register number
343 * This is achieved by stitching together 16 chunks of 64 KiB of physical
344 * address space via the MMU.
346 static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
348 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
349 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
352 static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
355 pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
356 L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
357 phys_addr_t cs = pcie->cs->start;
358 struct tegra_pcie_bus *bus;
362 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
364 return ERR_PTR(-ENOMEM);
366 INIT_LIST_HEAD(&bus->list);
369 /* allocate 1 MiB of virtual addresses */
370 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
376 /* map each of the 16 chunks of 64 KiB each */
377 for (i = 0; i < 16; i++) {
378 unsigned long virt = (unsigned long)bus->area->addr +
380 phys_addr_t phys = cs + i * SZ_1M + busnr * SZ_64K;
382 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
384 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
393 vunmap(bus->area->addr);
400 * Look up a virtual address mapping for the specified bus number. If no such
401 * mapping existis, try to create one.
403 static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
406 struct tegra_pcie_bus *bus;
408 list_for_each_entry(bus, &pcie->busses, list)
409 if (bus->nr == busnr)
410 return bus->area->addr;
412 bus = tegra_pcie_bus_alloc(pcie, busnr);
416 list_add_tail(&bus->list, &pcie->busses);
418 return bus->area->addr;
421 static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
425 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
426 void __iomem *addr = NULL;
428 if (bus->number == 0) {
429 unsigned int slot = PCI_SLOT(devfn);
430 struct tegra_pcie_port *port;
432 list_for_each_entry(port, &pcie->ports, list) {
433 if (port->index + 1 == slot) {
434 addr = port->base + (where & ~3);
439 addr = tegra_pcie_bus_map(pcie, bus->number);
442 "failed to map cfg. space for bus %u\n",
447 addr += tegra_pcie_conf_offset(devfn, where);
453 static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
454 int where, int size, u32 *value)
458 addr = tegra_pcie_conf_address(bus, devfn, where);
461 return PCIBIOS_DEVICE_NOT_FOUND;
464 *value = readl(addr);
467 *value = (*value >> (8 * (where & 3))) & 0xff;
469 *value = (*value >> (8 * (where & 3))) & 0xffff;
471 return PCIBIOS_SUCCESSFUL;
474 static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
475 int where, int size, u32 value)
480 addr = tegra_pcie_conf_address(bus, devfn, where);
482 return PCIBIOS_DEVICE_NOT_FOUND;
486 return PCIBIOS_SUCCESSFUL;
490 mask = ~(0xffff << ((where & 0x3) * 8));
492 mask = ~(0xff << ((where & 0x3) * 8));
494 return PCIBIOS_BAD_REGISTER_NUMBER;
496 tmp = readl(addr) & mask;
497 tmp |= value << ((where & 0x3) * 8);
500 return PCIBIOS_SUCCESSFUL;
503 static struct pci_ops tegra_pcie_ops = {
504 .read = tegra_pcie_read_conf,
505 .write = tegra_pcie_write_conf,
508 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
510 unsigned long ret = 0;
512 switch (port->index) {
529 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
531 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
534 /* pulse reset signal */
535 value = afi_readl(port->pcie, ctrl);
536 value &= ~AFI_PEX_CTRL_RST;
537 afi_writel(port->pcie, value, ctrl);
539 usleep_range(1000, 2000);
541 value = afi_readl(port->pcie, ctrl);
542 value |= AFI_PEX_CTRL_RST;
543 afi_writel(port->pcie, value, ctrl);
546 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
548 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
549 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
552 /* enable reference clock */
553 value = afi_readl(port->pcie, ctrl);
554 value |= AFI_PEX_CTRL_REFCLK_EN;
556 if (soc->has_pex_clkreq_en)
557 value |= AFI_PEX_CTRL_CLKREQ_EN;
559 afi_writel(port->pcie, value, ctrl);
561 tegra_pcie_port_reset(port);
564 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
566 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
569 /* assert port reset */
570 value = afi_readl(port->pcie, ctrl);
571 value &= ~AFI_PEX_CTRL_RST;
572 afi_writel(port->pcie, value, ctrl);
574 /* disable reference clock */
575 value = afi_readl(port->pcie, ctrl);
576 value &= ~AFI_PEX_CTRL_REFCLK_EN;
577 afi_writel(port->pcie, value, ctrl);
580 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
582 struct tegra_pcie *pcie = port->pcie;
584 devm_iounmap(pcie->dev, port->base);
585 devm_release_mem_region(pcie->dev, port->regs.start,
586 resource_size(&port->regs));
587 list_del(&port->list);
588 devm_kfree(pcie->dev, port);
591 static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
595 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
596 pci_read_config_word(dev, PCI_COMMAND, ®);
597 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
598 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
599 pci_write_config_word(dev, PCI_COMMAND, reg);
602 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
604 /* Tegra PCIE root complex wrongly reports device class */
605 static void tegra_pcie_fixup_class(struct pci_dev *dev)
607 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
609 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
610 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
611 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
612 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
614 /* Tegra PCIE requires relaxed ordering */
615 static void tegra_pcie_relax_enable(struct pci_dev *dev)
617 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
619 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
621 static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
623 struct tegra_pcie *pcie = sys_to_pcie(sys);
625 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
626 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
628 pci_add_resource(&sys->resources, &pcie->busn);
630 pci_ioremap_io(nr * SZ_64K, pcie->io.start);
635 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
637 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
642 static void tegra_pcie_add_bus(struct pci_bus *bus)
644 if (IS_ENABLED(CONFIG_PCI_MSI)) {
645 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
647 bus->msi = &pcie->msi.chip;
651 static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
653 struct tegra_pcie *pcie = sys_to_pcie(sys);
656 bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
661 pci_scan_child_bus(bus);
666 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
668 const char *err_msg[] = {
675 "Response decoding error",
676 "AXI response decoding error",
677 "Transaction timeout",
679 struct tegra_pcie *pcie = arg;
682 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
683 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
684 afi_writel(pcie, 0, AFI_INTR_CODE);
686 if (code == AFI_INTR_LEGACY)
689 if (code >= ARRAY_SIZE(err_msg))
693 * do not pollute kernel log with master abort reports since they
694 * happen a lot during enumeration
696 if (code == AFI_INTR_MASTER_ABORT)
697 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
700 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
703 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
704 code == AFI_INTR_FPCI_DECODE_ERROR) {
705 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
706 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
708 if (code == AFI_INTR_MASTER_ABORT)
709 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
711 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
718 * FPCI map is as follows:
719 * - 0xfdfc000000: I/O space
720 * - 0xfdfe000000: type 0 configuration space
721 * - 0xfdff000000: type 1 configuration space
722 * - 0xfe00000000: type 0 extended configuration space
723 * - 0xfe10000000: type 1 extended configuration space
725 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
727 u32 fpci_bar, size, axi_address;
729 /* Bar 0: type 1 extended configuration space */
730 fpci_bar = 0xfe100000;
731 size = resource_size(pcie->cs);
732 axi_address = pcie->cs->start;
733 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
734 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
735 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
737 /* Bar 1: downstream IO bar */
738 fpci_bar = 0xfdfc0000;
739 size = resource_size(&pcie->io);
740 axi_address = pcie->io.start;
741 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
742 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
743 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
745 /* Bar 2: prefetchable memory BAR */
746 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
747 size = resource_size(&pcie->prefetch);
748 axi_address = pcie->prefetch.start;
749 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
750 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
751 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
753 /* Bar 3: non prefetchable memory BAR */
754 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
755 size = resource_size(&pcie->mem);
756 axi_address = pcie->mem.start;
757 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
758 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
759 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
761 /* NULL out the remaining BARs as they are not used */
762 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
763 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
764 afi_writel(pcie, 0, AFI_FPCI_BAR4);
766 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
767 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
768 afi_writel(pcie, 0, AFI_FPCI_BAR5);
770 /* map all upstream transactions as uncached */
771 afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
772 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
773 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
774 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
776 /* MSI translations are setup only when needed */
777 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
778 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
779 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
780 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
783 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
785 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
786 struct tegra_pcie_port *port;
787 unsigned int timeout;
790 /* power down PCIe slot clock bias pad */
791 if (soc->has_pex_bias_ctrl)
792 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
794 /* configure mode and disable all ports */
795 value = afi_readl(pcie, AFI_PCIE_CONFIG);
796 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
797 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
799 list_for_each_entry(port, &pcie->ports, list)
800 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
802 afi_writel(pcie, value, AFI_PCIE_CONFIG);
804 value = afi_readl(pcie, AFI_FUSE);
805 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
806 afi_writel(pcie, value, AFI_FUSE);
808 /* initialze internal PHY, enable up to 16 PCIE lanes */
809 pads_writel(pcie, 0x0, PADS_CTL_SEL);
811 /* override IDDQ to 1 on all 4 lanes */
812 value = pads_readl(pcie, PADS_CTL);
813 value |= PADS_CTL_IDDQ_1L;
814 pads_writel(pcie, value, PADS_CTL);
817 * Set up PHY PLL inputs select PLLE output as refclock,
818 * set TX ref sel to div10 (not div5).
820 value = pads_readl(pcie, soc->pads_pll_ctl);
821 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
822 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
823 pads_writel(pcie, value, soc->pads_pll_ctl);
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 /* Configure the reference clock driver */
831 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
832 pads_writel(pcie, value, PADS_REFCLK_CFG0);
833 if (soc->num_ports > 2)
834 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
836 /* wait for the PLL to lock */
839 value = pads_readl(pcie, soc->pads_pll_ctl);
840 usleep_range(1000, 2000);
841 if (--timeout == 0) {
842 pr_err("Tegra PCIe error: timeout waiting for PLL\n");
845 } while (!(value & PADS_PLL_CTL_LOCKDET));
847 /* turn off IDDQ override */
848 value = pads_readl(pcie, PADS_CTL);
849 value &= ~PADS_CTL_IDDQ_1L;
850 pads_writel(pcie, value, PADS_CTL);
852 /* enable TX/RX data */
853 value = pads_readl(pcie, PADS_CTL);
854 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
855 pads_writel(pcie, value, PADS_CTL);
857 /* take the PCIe interface module out of reset */
858 tegra_periph_reset_deassert(pcie->pcie_xclk);
860 /* finally enable PCIe */
861 value = afi_readl(pcie, AFI_CONFIGURATION);
862 value |= AFI_CONFIGURATION_EN_FPCI;
863 afi_writel(pcie, value, AFI_CONFIGURATION);
865 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
866 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
867 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
869 if (soc->has_intr_prsnt_sense)
870 value |= AFI_INTR_EN_PRSNT_SENSE;
872 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
873 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
875 /* don't enable MSI for now, only when needed */
876 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
878 /* disable all exceptions */
879 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
884 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
886 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
889 /* TODO: disable and unprepare clocks? */
891 tegra_periph_reset_assert(pcie->pcie_xclk);
892 tegra_periph_reset_assert(pcie->afi_clk);
893 tegra_periph_reset_assert(pcie->pex_clk);
895 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
897 if (soc->has_avdd_supply) {
898 err = regulator_disable(pcie->avdd_supply);
901 "failed to disable AVDD regulator: %d\n",
905 err = regulator_disable(pcie->pex_clk_supply);
907 dev_warn(pcie->dev, "failed to disable pex-clk regulator: %d\n",
910 err = regulator_disable(pcie->vdd_supply);
912 dev_warn(pcie->dev, "failed to disable VDD regulator: %d\n",
916 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
918 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
921 tegra_periph_reset_assert(pcie->pcie_xclk);
922 tegra_periph_reset_assert(pcie->afi_clk);
923 tegra_periph_reset_assert(pcie->pex_clk);
925 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
927 /* enable regulators */
928 err = regulator_enable(pcie->vdd_supply);
930 dev_err(pcie->dev, "failed to enable VDD regulator: %d\n", err);
934 err = regulator_enable(pcie->pex_clk_supply);
936 dev_err(pcie->dev, "failed to enable pex-clk regulator: %d\n",
941 if (soc->has_avdd_supply) {
942 err = regulator_enable(pcie->avdd_supply);
945 "failed to enable AVDD regulator: %d\n",
951 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
954 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
958 tegra_periph_reset_deassert(pcie->afi_clk);
960 err = clk_prepare_enable(pcie->afi_clk);
962 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
966 if (soc->has_cml_clk) {
967 err = clk_prepare_enable(pcie->cml_clk);
969 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
975 err = clk_prepare_enable(pcie->pll_e);
977 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
984 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
986 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
988 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
989 if (IS_ERR(pcie->pex_clk))
990 return PTR_ERR(pcie->pex_clk);
992 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
993 if (IS_ERR(pcie->afi_clk))
994 return PTR_ERR(pcie->afi_clk);
996 pcie->pcie_xclk = devm_clk_get(pcie->dev, "pcie_xclk");
997 if (IS_ERR(pcie->pcie_xclk))
998 return PTR_ERR(pcie->pcie_xclk);
1000 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1001 if (IS_ERR(pcie->pll_e))
1002 return PTR_ERR(pcie->pll_e);
1004 if (soc->has_cml_clk) {
1005 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1006 if (IS_ERR(pcie->cml_clk))
1007 return PTR_ERR(pcie->cml_clk);
1013 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1015 struct platform_device *pdev = to_platform_device(pcie->dev);
1016 struct resource *pads, *afi, *res;
1019 err = tegra_pcie_clocks_get(pcie);
1021 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1025 err = tegra_pcie_power_on(pcie);
1027 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1031 /* request and remap controller registers */
1032 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1034 err = -EADDRNOTAVAIL;
1038 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1040 err = -EADDRNOTAVAIL;
1044 pcie->pads = devm_request_and_ioremap(&pdev->dev, pads);
1046 err = -EADDRNOTAVAIL;
1050 pcie->afi = devm_request_and_ioremap(&pdev->dev, afi);
1052 err = -EADDRNOTAVAIL;
1056 /* request and remap configuration space */
1057 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1059 err = -EADDRNOTAVAIL;
1063 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1064 resource_size(res), res->name);
1066 err = -EADDRNOTAVAIL;
1070 /* request interrupt */
1071 err = platform_get_irq_byname(pdev, "intr");
1073 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1079 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1081 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1088 tegra_pcie_power_off(pcie);
1092 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1095 free_irq(pcie->irq, pcie);
1097 tegra_pcie_power_off(pcie);
1101 static int tegra_msi_alloc(struct tegra_msi *chip)
1105 mutex_lock(&chip->lock);
1107 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1108 if (msi < INT_PCI_MSI_NR)
1109 set_bit(msi, chip->used);
1113 mutex_unlock(&chip->lock);
1118 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1120 struct device *dev = chip->chip.dev;
1122 mutex_lock(&chip->lock);
1124 if (!test_bit(irq, chip->used))
1125 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1127 clear_bit(irq, chip->used);
1129 mutex_unlock(&chip->lock);
1132 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1134 struct tegra_pcie *pcie = data;
1135 struct tegra_msi *msi = &pcie->msi;
1136 unsigned int i, processed = 0;
1138 for (i = 0; i < 8; i++) {
1139 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1142 unsigned int offset = find_first_bit(®, 32);
1143 unsigned int index = i * 32 + offset;
1146 /* clear the interrupt */
1147 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1149 irq = irq_find_mapping(msi->domain, index);
1151 if (test_bit(index, msi->used))
1152 generic_handle_irq(irq);
1154 dev_info(pcie->dev, "unhandled MSI\n");
1157 * that's weird who triggered this?
1160 dev_info(pcie->dev, "unexpected MSI\n");
1163 /* see if there's any more pending in this vector */
1164 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1170 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1173 static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
1174 struct msi_desc *desc)
1176 struct tegra_msi *msi = to_tegra_msi(chip);
1181 hwirq = tegra_msi_alloc(msi);
1185 irq = irq_create_mapping(msi->domain, hwirq);
1189 irq_set_msi_desc(irq, desc);
1191 msg.address_lo = virt_to_phys((void *)msi->pages);
1192 /* 32 bit address only */
1196 write_msi_msg(irq, &msg);
1201 static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
1203 struct tegra_msi *msi = to_tegra_msi(chip);
1204 struct irq_data *d = irq_get_irq_data(irq);
1206 tegra_msi_free(msi, d->hwirq);
1209 static struct irq_chip tegra_msi_irq_chip = {
1210 .name = "Tegra PCIe MSI",
1211 .irq_enable = unmask_msi_irq,
1212 .irq_disable = mask_msi_irq,
1213 .irq_mask = mask_msi_irq,
1214 .irq_unmask = unmask_msi_irq,
1217 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1218 irq_hw_number_t hwirq)
1220 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1221 irq_set_chip_data(irq, domain->host_data);
1222 set_irq_flags(irq, IRQF_VALID);
1227 static const struct irq_domain_ops msi_domain_ops = {
1228 .map = tegra_msi_map,
1231 static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1233 struct platform_device *pdev = to_platform_device(pcie->dev);
1234 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1235 struct tegra_msi *msi = &pcie->msi;
1240 mutex_init(&msi->lock);
1242 msi->chip.dev = pcie->dev;
1243 msi->chip.setup_irq = tegra_msi_setup_irq;
1244 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1246 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1247 &msi_domain_ops, &msi->chip);
1249 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1253 err = platform_get_irq_byname(pdev, "msi");
1255 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1261 err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
1262 tegra_msi_irq_chip.name, pcie);
1264 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1268 /* setup AFI/FPCI range */
1269 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1270 base = virt_to_phys((void *)msi->pages);
1272 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1273 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1274 /* this register is in 4K increments */
1275 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1277 /* enable all MSI vectors */
1278 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1279 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1280 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1281 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1282 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1283 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1284 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1285 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1287 /* and unmask the MSI interrupt */
1288 reg = afi_readl(pcie, AFI_INTR_MASK);
1289 reg |= AFI_INTR_MASK_MSI_MASK;
1290 afi_writel(pcie, reg, AFI_INTR_MASK);
1295 irq_domain_remove(msi->domain);
1299 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1301 struct tegra_msi *msi = &pcie->msi;
1302 unsigned int i, irq;
1305 /* mask the MSI interrupt */
1306 value = afi_readl(pcie, AFI_INTR_MASK);
1307 value &= ~AFI_INTR_MASK_MSI_MASK;
1308 afi_writel(pcie, value, AFI_INTR_MASK);
1310 /* disable all MSI vectors */
1311 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1312 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1313 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1314 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1315 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1316 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1317 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1318 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1320 free_pages(msi->pages, 0);
1323 free_irq(msi->irq, pcie);
1325 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1326 irq = irq_find_mapping(msi->domain, i);
1328 irq_dispose_mapping(irq);
1331 irq_domain_remove(msi->domain);
1336 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1339 struct device_node *np = pcie->dev->of_node;
1341 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1344 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1345 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1349 dev_info(pcie->dev, "2x3 configuration\n");
1350 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1354 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1355 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1358 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1361 dev_info(pcie->dev, "single-mode configuration\n");
1362 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1366 dev_info(pcie->dev, "dual-mode configuration\n");
1367 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1375 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1377 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1378 struct device_node *np = pcie->dev->of_node, *port;
1379 struct of_pci_range_parser parser;
1380 struct of_pci_range range;
1381 struct resource res;
1385 if (of_pci_range_parser_init(&parser, np)) {
1386 dev_err(pcie->dev, "missing \"ranges\" property\n");
1390 pcie->vdd_supply = devm_regulator_get(pcie->dev, "vdd");
1391 if (IS_ERR(pcie->vdd_supply))
1392 return PTR_ERR(pcie->vdd_supply);
1394 pcie->pex_clk_supply = devm_regulator_get(pcie->dev, "pex-clk");
1395 if (IS_ERR(pcie->pex_clk_supply))
1396 return PTR_ERR(pcie->pex_clk_supply);
1398 if (soc->has_avdd_supply) {
1399 pcie->avdd_supply = devm_regulator_get(pcie->dev, "avdd");
1400 if (IS_ERR(pcie->avdd_supply))
1401 return PTR_ERR(pcie->avdd_supply);
1404 for_each_of_pci_range(&parser, &range) {
1405 of_pci_range_to_resource(&range, np, &res);
1407 switch (res.flags & IORESOURCE_TYPE_BITS) {
1409 memcpy(&pcie->io, &res, sizeof(res));
1410 pcie->io.name = "I/O";
1413 case IORESOURCE_MEM:
1414 if (res.flags & IORESOURCE_PREFETCH) {
1415 memcpy(&pcie->prefetch, &res, sizeof(res));
1416 pcie->prefetch.name = "PREFETCH";
1418 memcpy(&pcie->mem, &res, sizeof(res));
1419 pcie->mem.name = "MEM";
1425 err = of_pci_parse_bus_range(np, &pcie->busn);
1427 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1429 pcie->busn.name = np->name;
1430 pcie->busn.start = 0;
1431 pcie->busn.end = 0xff;
1432 pcie->busn.flags = IORESOURCE_BUS;
1435 /* parse root ports */
1436 for_each_child_of_node(np, port) {
1437 struct tegra_pcie_port *rp;
1441 err = of_pci_get_devfn(port);
1443 dev_err(pcie->dev, "failed to parse address: %d\n",
1448 index = PCI_SLOT(err);
1450 if (index < 1 || index > soc->num_ports) {
1451 dev_err(pcie->dev, "invalid port number: %d\n", index);
1457 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1459 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1465 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1469 lanes |= value << (index << 3);
1471 if (!of_device_is_available(port))
1474 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1478 err = of_address_to_resource(port, 0, &rp->regs);
1480 dev_err(pcie->dev, "failed to parse address: %d\n",
1485 INIT_LIST_HEAD(&rp->list);
1490 rp->base = devm_request_and_ioremap(pcie->dev, &rp->regs);
1492 return -EADDRNOTAVAIL;
1494 list_add_tail(&rp->list, &pcie->ports);
1497 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1499 dev_err(pcie->dev, "invalid lane configuration\n");
1507 * FIXME: If there are no PCIe cards attached, then calling this function
1508 * can result in the increase of the bootup time as there are big timeout
1511 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1512 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1514 unsigned int retries = 3;
1515 unsigned long value;
1518 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1521 value = readl(port->base + RP_VEND_XP);
1523 if (value & RP_VEND_XP_DL_UP)
1526 usleep_range(1000, 2000);
1527 } while (--timeout);
1530 dev_err(port->pcie->dev, "link %u down, retrying\n",
1535 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1538 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1540 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1543 usleep_range(1000, 2000);
1544 } while (--timeout);
1547 tegra_pcie_port_reset(port);
1548 } while (--retries);
1553 static int tegra_pcie_enable(struct tegra_pcie *pcie)
1555 struct tegra_pcie_port *port, *tmp;
1558 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1559 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
1560 port->index, port->lanes);
1562 tegra_pcie_port_enable(port);
1564 if (tegra_pcie_port_check_link(port))
1567 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
1569 tegra_pcie_port_disable(port);
1570 tegra_pcie_port_free(port);
1573 memset(&hw, 0, sizeof(hw));
1575 hw.nr_controllers = 1;
1576 hw.private_data = (void **)&pcie;
1577 hw.setup = tegra_pcie_setup;
1578 hw.map_irq = tegra_pcie_map_irq;
1579 hw.add_bus = tegra_pcie_add_bus;
1580 hw.scan = tegra_pcie_scan_bus;
1581 hw.ops = &tegra_pcie_ops;
1583 pci_common_init_dev(pcie->dev, &hw);
1588 static const struct tegra_pcie_soc_data tegra20_pcie_data = {
1590 .msi_base_shift = 0,
1591 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1592 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1593 .has_pex_clkreq_en = false,
1594 .has_pex_bias_ctrl = false,
1595 .has_intr_prsnt_sense = false,
1596 .has_avdd_supply = false,
1597 .has_cml_clk = false,
1600 static const struct tegra_pcie_soc_data tegra30_pcie_data = {
1602 .msi_base_shift = 8,
1603 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1604 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1605 .has_pex_clkreq_en = true,
1606 .has_pex_bias_ctrl = true,
1607 .has_intr_prsnt_sense = true,
1608 .has_avdd_supply = true,
1609 .has_cml_clk = true,
1612 static const struct of_device_id tegra_pcie_of_match[] = {
1613 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
1614 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
1617 MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
1619 static int tegra_pcie_probe(struct platform_device *pdev)
1621 const struct of_device_id *match;
1622 struct tegra_pcie *pcie;
1625 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
1629 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1633 INIT_LIST_HEAD(&pcie->busses);
1634 INIT_LIST_HEAD(&pcie->ports);
1635 pcie->soc_data = match->data;
1636 pcie->dev = &pdev->dev;
1638 err = tegra_pcie_parse_dt(pcie);
1642 pcibios_min_mem = 0;
1644 err = tegra_pcie_get_resources(pcie);
1646 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
1650 err = tegra_pcie_enable_controller(pcie);
1654 /* setup the AFI address translations */
1655 tegra_pcie_setup_translations(pcie);
1657 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1658 err = tegra_pcie_enable_msi(pcie);
1661 "failed to enable MSI support: %d\n",
1667 err = tegra_pcie_enable(pcie);
1669 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
1673 platform_set_drvdata(pdev, pcie);
1677 if (IS_ENABLED(CONFIG_PCI_MSI))
1678 tegra_pcie_disable_msi(pcie);
1680 tegra_pcie_put_resources(pcie);
1684 static struct platform_driver tegra_pcie_driver = {
1686 .name = "tegra-pcie",
1687 .owner = THIS_MODULE,
1688 .of_match_table = tegra_pcie_of_match,
1689 .suppress_bind_attrs = true,
1691 .probe = tegra_pcie_probe,
1693 module_platform_driver(tegra_pcie_driver);
1695 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1696 MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
1697 MODULE_LICENSE("GPLv2");