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/debugfs.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/reset.h>
43 #include <linux/sizes.h>
44 #include <linux/slab.h>
45 #include <linux/vmalloc.h>
46 #include <linux/regulator/consumer.h>
48 #include <soc/tegra/cpuidle.h>
49 #include <soc/tegra/pmc.h>
51 #include <asm/mach/irq.h>
52 #include <asm/mach/map.h>
53 #include <asm/mach/pci.h>
55 #define INT_PCI_MSI_NR (8 * 32)
57 /* register definitions */
59 #define AFI_AXI_BAR0_SZ 0x00
60 #define AFI_AXI_BAR1_SZ 0x04
61 #define AFI_AXI_BAR2_SZ 0x08
62 #define AFI_AXI_BAR3_SZ 0x0c
63 #define AFI_AXI_BAR4_SZ 0x10
64 #define AFI_AXI_BAR5_SZ 0x14
66 #define AFI_AXI_BAR0_START 0x18
67 #define AFI_AXI_BAR1_START 0x1c
68 #define AFI_AXI_BAR2_START 0x20
69 #define AFI_AXI_BAR3_START 0x24
70 #define AFI_AXI_BAR4_START 0x28
71 #define AFI_AXI_BAR5_START 0x2c
73 #define AFI_FPCI_BAR0 0x30
74 #define AFI_FPCI_BAR1 0x34
75 #define AFI_FPCI_BAR2 0x38
76 #define AFI_FPCI_BAR3 0x3c
77 #define AFI_FPCI_BAR4 0x40
78 #define AFI_FPCI_BAR5 0x44
80 #define AFI_CACHE_BAR0_SZ 0x48
81 #define AFI_CACHE_BAR0_ST 0x4c
82 #define AFI_CACHE_BAR1_SZ 0x50
83 #define AFI_CACHE_BAR1_ST 0x54
85 #define AFI_MSI_BAR_SZ 0x60
86 #define AFI_MSI_FPCI_BAR_ST 0x64
87 #define AFI_MSI_AXI_BAR_ST 0x68
89 #define AFI_MSI_VEC0 0x6c
90 #define AFI_MSI_VEC1 0x70
91 #define AFI_MSI_VEC2 0x74
92 #define AFI_MSI_VEC3 0x78
93 #define AFI_MSI_VEC4 0x7c
94 #define AFI_MSI_VEC5 0x80
95 #define AFI_MSI_VEC6 0x84
96 #define AFI_MSI_VEC7 0x88
98 #define AFI_MSI_EN_VEC0 0x8c
99 #define AFI_MSI_EN_VEC1 0x90
100 #define AFI_MSI_EN_VEC2 0x94
101 #define AFI_MSI_EN_VEC3 0x98
102 #define AFI_MSI_EN_VEC4 0x9c
103 #define AFI_MSI_EN_VEC5 0xa0
104 #define AFI_MSI_EN_VEC6 0xa4
105 #define AFI_MSI_EN_VEC7 0xa8
107 #define AFI_CONFIGURATION 0xac
108 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
110 #define AFI_FPCI_ERROR_MASKS 0xb0
112 #define AFI_INTR_MASK 0xb4
113 #define AFI_INTR_MASK_INT_MASK (1 << 0)
114 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
116 #define AFI_INTR_CODE 0xb8
117 #define AFI_INTR_CODE_MASK 0xf
118 #define AFI_INTR_AXI_SLAVE_ERROR 1
119 #define AFI_INTR_AXI_DECODE_ERROR 2
120 #define AFI_INTR_TARGET_ABORT 3
121 #define AFI_INTR_MASTER_ABORT 4
122 #define AFI_INTR_INVALID_WRITE 5
123 #define AFI_INTR_LEGACY 6
124 #define AFI_INTR_FPCI_DECODE_ERROR 7
126 #define AFI_INTR_SIGNATURE 0xbc
127 #define AFI_UPPER_FPCI_ADDRESS 0xc0
128 #define AFI_SM_INTR_ENABLE 0xc4
129 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
130 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
131 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
132 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
133 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
134 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
135 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
136 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
138 #define AFI_AFI_INTR_ENABLE 0xc8
139 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
140 #define AFI_INTR_EN_INI_DECERR (1 << 1)
141 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
142 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
143 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
144 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
145 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
146 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
147 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
149 #define AFI_PCIE_CONFIG 0x0f8
150 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
151 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
152 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
153 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
154 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
155 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
156 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
157 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
159 #define AFI_FUSE 0x104
160 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
162 #define AFI_PEX0_CTRL 0x110
163 #define AFI_PEX1_CTRL 0x118
164 #define AFI_PEX2_CTRL 0x128
165 #define AFI_PEX_CTRL_RST (1 << 0)
166 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
167 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
169 #define AFI_PEXBIAS_CTRL_0 0x168
171 #define RP_VEND_XP 0x00000F00
172 #define RP_VEND_XP_DL_UP (1 << 30)
174 #define RP_LINK_CONTROL_STATUS 0x00000090
175 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
176 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
178 #define PADS_CTL_SEL 0x0000009C
180 #define PADS_CTL 0x000000A0
181 #define PADS_CTL_IDDQ_1L (1 << 0)
182 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
183 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
185 #define PADS_PLL_CTL_TEGRA20 0x000000B8
186 #define PADS_PLL_CTL_TEGRA30 0x000000B4
187 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
188 #define PADS_PLL_CTL_LOCKDET (1 << 8)
189 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
190 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
191 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
192 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
193 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
194 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
195 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
196 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
198 #define PADS_REFCLK_CFG0 0x000000C8
199 #define PADS_REFCLK_CFG1 0x000000CC
202 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
203 * entries, one entry per PCIe port. These field definitions and desired
204 * values aren't in the TRM, but do come from NVIDIA.
206 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
207 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
208 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
209 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
211 /* Default value provided by HW engineering is 0xfa5c */
212 #define PADS_REFCLK_CFG_VALUE \
214 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
215 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
216 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
217 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
221 struct msi_chip chip;
222 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
223 struct irq_domain *domain;
229 /* used to differentiate between Tegra SoC generations */
230 struct tegra_pcie_soc_data {
231 unsigned int num_ports;
232 unsigned int msi_base_shift;
235 bool has_pex_clkreq_en;
236 bool has_pex_bias_ctrl;
237 bool has_intr_prsnt_sense;
241 static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
243 return container_of(chip, struct tegra_msi, chip);
253 struct list_head buses;
259 struct resource prefetch;
260 struct resource busn;
267 struct reset_control *pex_rst;
268 struct reset_control *afi_rst;
269 struct reset_control *pcie_xrst;
271 struct tegra_msi msi;
273 struct list_head ports;
274 unsigned int num_ports;
277 struct regulator_bulk_data *supplies;
278 unsigned int num_supplies;
280 const struct tegra_pcie_soc_data *soc_data;
281 struct dentry *debugfs;
284 struct tegra_pcie_port {
285 struct tegra_pcie *pcie;
286 struct list_head list;
287 struct resource regs;
293 struct tegra_pcie_bus {
294 struct vm_struct *area;
295 struct list_head list;
299 static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
301 return sys->private_data;
304 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
305 unsigned long offset)
307 writel(value, pcie->afi + offset);
310 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
312 return readl(pcie->afi + offset);
315 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
316 unsigned long offset)
318 writel(value, pcie->pads + offset);
321 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
323 return readl(pcie->pads + offset);
327 * The configuration space mapping on Tegra is somewhat similar to the ECAM
328 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
329 * register accesses are mapped:
331 * [27:24] extended register number
333 * [15:11] device number
334 * [10: 8] function number
335 * [ 7: 0] register number
337 * Mapping the whole extended configuration space would require 256 MiB of
338 * virtual address space, only a small part of which will actually be used.
339 * To work around this, a 1 MiB of virtual addresses are allocated per bus
340 * when the bus is first accessed. When the physical range is mapped, the
341 * the bus number bits are hidden so that the extended register number bits
342 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
344 * [19:16] extended register number
345 * [15:11] device number
346 * [10: 8] function number
347 * [ 7: 0] register number
349 * This is achieved by stitching together 16 chunks of 64 KiB of physical
350 * address space via the MMU.
352 static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
354 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
355 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
358 static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
361 pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
362 L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
363 phys_addr_t cs = pcie->cs->start;
364 struct tegra_pcie_bus *bus;
368 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
370 return ERR_PTR(-ENOMEM);
372 INIT_LIST_HEAD(&bus->list);
375 /* allocate 1 MiB of virtual addresses */
376 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
382 /* map each of the 16 chunks of 64 KiB each */
383 for (i = 0; i < 16; i++) {
384 unsigned long virt = (unsigned long)bus->area->addr +
386 phys_addr_t phys = cs + i * SZ_1M + busnr * SZ_64K;
388 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
390 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
399 vunmap(bus->area->addr);
406 * Look up a virtual address mapping for the specified bus number. If no such
407 * mapping exists, try to create one.
409 static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
412 struct tegra_pcie_bus *bus;
414 list_for_each_entry(bus, &pcie->buses, list)
415 if (bus->nr == busnr)
416 return (void __iomem *)bus->area->addr;
418 bus = tegra_pcie_bus_alloc(pcie, busnr);
422 list_add_tail(&bus->list, &pcie->buses);
424 return (void __iomem *)bus->area->addr;
427 static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
431 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
432 void __iomem *addr = NULL;
434 if (bus->number == 0) {
435 unsigned int slot = PCI_SLOT(devfn);
436 struct tegra_pcie_port *port;
438 list_for_each_entry(port, &pcie->ports, list) {
439 if (port->index + 1 == slot) {
440 addr = port->base + (where & ~3);
445 addr = tegra_pcie_bus_map(pcie, bus->number);
448 "failed to map cfg. space for bus %u\n",
453 addr += tegra_pcie_conf_offset(devfn, where);
459 static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
460 int where, int size, u32 *value)
464 addr = tegra_pcie_conf_address(bus, devfn, where);
467 return PCIBIOS_DEVICE_NOT_FOUND;
470 *value = readl(addr);
473 *value = (*value >> (8 * (where & 3))) & 0xff;
475 *value = (*value >> (8 * (where & 3))) & 0xffff;
477 return PCIBIOS_SUCCESSFUL;
480 static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
481 int where, int size, u32 value)
486 addr = tegra_pcie_conf_address(bus, devfn, where);
488 return PCIBIOS_DEVICE_NOT_FOUND;
492 return PCIBIOS_SUCCESSFUL;
496 mask = ~(0xffff << ((where & 0x3) * 8));
498 mask = ~(0xff << ((where & 0x3) * 8));
500 return PCIBIOS_BAD_REGISTER_NUMBER;
502 tmp = readl(addr) & mask;
503 tmp |= value << ((where & 0x3) * 8);
506 return PCIBIOS_SUCCESSFUL;
509 static struct pci_ops tegra_pcie_ops = {
510 .read = tegra_pcie_read_conf,
511 .write = tegra_pcie_write_conf,
514 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
516 unsigned long ret = 0;
518 switch (port->index) {
535 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
537 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
540 /* pulse reset signal */
541 value = afi_readl(port->pcie, ctrl);
542 value &= ~AFI_PEX_CTRL_RST;
543 afi_writel(port->pcie, value, ctrl);
545 usleep_range(1000, 2000);
547 value = afi_readl(port->pcie, ctrl);
548 value |= AFI_PEX_CTRL_RST;
549 afi_writel(port->pcie, value, ctrl);
552 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
554 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
555 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
558 /* enable reference clock */
559 value = afi_readl(port->pcie, ctrl);
560 value |= AFI_PEX_CTRL_REFCLK_EN;
562 if (soc->has_pex_clkreq_en)
563 value |= AFI_PEX_CTRL_CLKREQ_EN;
565 afi_writel(port->pcie, value, ctrl);
567 tegra_pcie_port_reset(port);
570 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
572 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
575 /* assert port reset */
576 value = afi_readl(port->pcie, ctrl);
577 value &= ~AFI_PEX_CTRL_RST;
578 afi_writel(port->pcie, value, ctrl);
580 /* disable reference clock */
581 value = afi_readl(port->pcie, ctrl);
582 value &= ~AFI_PEX_CTRL_REFCLK_EN;
583 afi_writel(port->pcie, value, ctrl);
586 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
588 struct tegra_pcie *pcie = port->pcie;
590 devm_iounmap(pcie->dev, port->base);
591 devm_release_mem_region(pcie->dev, port->regs.start,
592 resource_size(&port->regs));
593 list_del(&port->list);
594 devm_kfree(pcie->dev, port);
597 static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
601 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
602 pci_read_config_word(dev, PCI_COMMAND, ®);
603 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
604 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
605 pci_write_config_word(dev, PCI_COMMAND, reg);
608 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
610 /* Tegra PCIE root complex wrongly reports device class */
611 static void tegra_pcie_fixup_class(struct pci_dev *dev)
613 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
615 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
616 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
617 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
618 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
620 /* Tegra PCIE requires relaxed ordering */
621 static void tegra_pcie_relax_enable(struct pci_dev *dev)
623 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
625 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
627 static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
629 struct tegra_pcie *pcie = sys_to_pcie(sys);
632 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
636 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
640 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
641 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
643 pci_add_resource(&sys->resources, &pcie->busn);
645 pci_ioremap_io(nr * SZ_64K, pcie->io.start);
650 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
652 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
655 tegra_cpuidle_pcie_irqs_in_use();
657 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
664 static void tegra_pcie_add_bus(struct pci_bus *bus)
666 if (IS_ENABLED(CONFIG_PCI_MSI)) {
667 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
669 bus->msi = &pcie->msi.chip;
673 static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
675 struct tegra_pcie *pcie = sys_to_pcie(sys);
678 bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
683 pci_scan_child_bus(bus);
688 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
690 const char *err_msg[] = {
697 "Response decoding error",
698 "AXI response decoding error",
699 "Transaction timeout",
701 struct tegra_pcie *pcie = arg;
704 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
705 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
706 afi_writel(pcie, 0, AFI_INTR_CODE);
708 if (code == AFI_INTR_LEGACY)
711 if (code >= ARRAY_SIZE(err_msg))
715 * do not pollute kernel log with master abort reports since they
716 * happen a lot during enumeration
718 if (code == AFI_INTR_MASTER_ABORT)
719 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
722 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
725 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
726 code == AFI_INTR_FPCI_DECODE_ERROR) {
727 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
728 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
730 if (code == AFI_INTR_MASTER_ABORT)
731 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
733 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
740 * FPCI map is as follows:
741 * - 0xfdfc000000: I/O space
742 * - 0xfdfe000000: type 0 configuration space
743 * - 0xfdff000000: type 1 configuration space
744 * - 0xfe00000000: type 0 extended configuration space
745 * - 0xfe10000000: type 1 extended configuration space
747 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
749 u32 fpci_bar, size, axi_address;
751 /* Bar 0: type 1 extended configuration space */
752 fpci_bar = 0xfe100000;
753 size = resource_size(pcie->cs);
754 axi_address = pcie->cs->start;
755 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
756 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
757 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
759 /* Bar 1: downstream IO bar */
760 fpci_bar = 0xfdfc0000;
761 size = resource_size(&pcie->io);
762 axi_address = pcie->io.start;
763 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
764 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
765 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
767 /* Bar 2: prefetchable memory BAR */
768 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
769 size = resource_size(&pcie->prefetch);
770 axi_address = pcie->prefetch.start;
771 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
772 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
773 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
775 /* Bar 3: non prefetchable memory BAR */
776 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
777 size = resource_size(&pcie->mem);
778 axi_address = pcie->mem.start;
779 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
780 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
781 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
783 /* NULL out the remaining BARs as they are not used */
784 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
785 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
786 afi_writel(pcie, 0, AFI_FPCI_BAR4);
788 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
789 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
790 afi_writel(pcie, 0, AFI_FPCI_BAR5);
792 /* map all upstream transactions as uncached */
793 afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
794 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
795 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
796 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
798 /* MSI translations are setup only when needed */
799 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
800 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
801 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
802 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
805 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
807 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
808 struct tegra_pcie_port *port;
809 unsigned int timeout;
812 /* power down PCIe slot clock bias pad */
813 if (soc->has_pex_bias_ctrl)
814 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
816 /* configure mode and disable all ports */
817 value = afi_readl(pcie, AFI_PCIE_CONFIG);
818 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
819 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
821 list_for_each_entry(port, &pcie->ports, list)
822 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
824 afi_writel(pcie, value, AFI_PCIE_CONFIG);
826 value = afi_readl(pcie, AFI_FUSE);
827 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
828 afi_writel(pcie, value, AFI_FUSE);
830 /* initialize internal PHY, enable up to 16 PCIE lanes */
831 pads_writel(pcie, 0x0, PADS_CTL_SEL);
833 /* override IDDQ to 1 on all 4 lanes */
834 value = pads_readl(pcie, PADS_CTL);
835 value |= PADS_CTL_IDDQ_1L;
836 pads_writel(pcie, value, PADS_CTL);
839 * Set up PHY PLL inputs select PLLE output as refclock,
840 * set TX ref sel to div10 (not div5).
842 value = pads_readl(pcie, soc->pads_pll_ctl);
843 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
844 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
845 pads_writel(pcie, value, soc->pads_pll_ctl);
847 /* take PLL out of reset */
848 value = pads_readl(pcie, soc->pads_pll_ctl);
849 value |= PADS_PLL_CTL_RST_B4SM;
850 pads_writel(pcie, value, soc->pads_pll_ctl);
852 /* Configure the reference clock driver */
853 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
854 pads_writel(pcie, value, PADS_REFCLK_CFG0);
855 if (soc->num_ports > 2)
856 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
858 /* wait for the PLL to lock */
861 value = pads_readl(pcie, soc->pads_pll_ctl);
862 usleep_range(1000, 2000);
863 if (--timeout == 0) {
864 pr_err("Tegra PCIe error: timeout waiting for PLL\n");
867 } while (!(value & PADS_PLL_CTL_LOCKDET));
869 /* turn off IDDQ override */
870 value = pads_readl(pcie, PADS_CTL);
871 value &= ~PADS_CTL_IDDQ_1L;
872 pads_writel(pcie, value, PADS_CTL);
874 /* enable TX/RX data */
875 value = pads_readl(pcie, PADS_CTL);
876 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
877 pads_writel(pcie, value, PADS_CTL);
879 /* take the PCIe interface module out of reset */
880 reset_control_deassert(pcie->pcie_xrst);
882 /* finally enable PCIe */
883 value = afi_readl(pcie, AFI_CONFIGURATION);
884 value |= AFI_CONFIGURATION_EN_FPCI;
885 afi_writel(pcie, value, AFI_CONFIGURATION);
887 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
888 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
889 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
891 if (soc->has_intr_prsnt_sense)
892 value |= AFI_INTR_EN_PRSNT_SENSE;
894 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
895 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
897 /* don't enable MSI for now, only when needed */
898 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
900 /* disable all exceptions */
901 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
906 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
910 /* TODO: disable and unprepare clocks? */
912 reset_control_assert(pcie->pcie_xrst);
913 reset_control_assert(pcie->afi_rst);
914 reset_control_assert(pcie->pex_rst);
916 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
918 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
920 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
923 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
925 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
928 reset_control_assert(pcie->pcie_xrst);
929 reset_control_assert(pcie->afi_rst);
930 reset_control_assert(pcie->pex_rst);
932 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
934 /* enable regulators */
935 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
937 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
939 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
943 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
947 reset_control_deassert(pcie->afi_rst);
949 err = clk_prepare_enable(pcie->afi_clk);
951 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
955 if (soc->has_cml_clk) {
956 err = clk_prepare_enable(pcie->cml_clk);
958 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
964 err = clk_prepare_enable(pcie->pll_e);
966 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
973 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
975 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
977 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
978 if (IS_ERR(pcie->pex_clk))
979 return PTR_ERR(pcie->pex_clk);
981 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
982 if (IS_ERR(pcie->afi_clk))
983 return PTR_ERR(pcie->afi_clk);
985 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
986 if (IS_ERR(pcie->pll_e))
987 return PTR_ERR(pcie->pll_e);
989 if (soc->has_cml_clk) {
990 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
991 if (IS_ERR(pcie->cml_clk))
992 return PTR_ERR(pcie->cml_clk);
998 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1000 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1001 if (IS_ERR(pcie->pex_rst))
1002 return PTR_ERR(pcie->pex_rst);
1004 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1005 if (IS_ERR(pcie->afi_rst))
1006 return PTR_ERR(pcie->afi_rst);
1008 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1009 if (IS_ERR(pcie->pcie_xrst))
1010 return PTR_ERR(pcie->pcie_xrst);
1015 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1017 struct platform_device *pdev = to_platform_device(pcie->dev);
1018 struct resource *pads, *afi, *res;
1021 err = tegra_pcie_clocks_get(pcie);
1023 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1027 err = tegra_pcie_resets_get(pcie);
1029 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1033 err = tegra_pcie_power_on(pcie);
1035 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1039 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1040 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1041 if (IS_ERR(pcie->pads)) {
1042 err = PTR_ERR(pcie->pads);
1046 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1047 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1048 if (IS_ERR(pcie->afi)) {
1049 err = PTR_ERR(pcie->afi);
1053 /* request configuration space, but remap later, on demand */
1054 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1056 err = -EADDRNOTAVAIL;
1060 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1061 resource_size(res), res->name);
1063 err = -EADDRNOTAVAIL;
1067 /* request interrupt */
1068 err = platform_get_irq_byname(pdev, "intr");
1070 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1076 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1078 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1085 tegra_pcie_power_off(pcie);
1089 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1092 free_irq(pcie->irq, pcie);
1094 tegra_pcie_power_off(pcie);
1098 static int tegra_msi_alloc(struct tegra_msi *chip)
1102 mutex_lock(&chip->lock);
1104 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1105 if (msi < INT_PCI_MSI_NR)
1106 set_bit(msi, chip->used);
1110 mutex_unlock(&chip->lock);
1115 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1117 struct device *dev = chip->chip.dev;
1119 mutex_lock(&chip->lock);
1121 if (!test_bit(irq, chip->used))
1122 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1124 clear_bit(irq, chip->used);
1126 mutex_unlock(&chip->lock);
1129 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1131 struct tegra_pcie *pcie = data;
1132 struct tegra_msi *msi = &pcie->msi;
1133 unsigned int i, processed = 0;
1135 for (i = 0; i < 8; i++) {
1136 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1139 unsigned int offset = find_first_bit(®, 32);
1140 unsigned int index = i * 32 + offset;
1143 /* clear the interrupt */
1144 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1146 irq = irq_find_mapping(msi->domain, index);
1148 if (test_bit(index, msi->used))
1149 generic_handle_irq(irq);
1151 dev_info(pcie->dev, "unhandled MSI\n");
1154 * that's weird who triggered this?
1157 dev_info(pcie->dev, "unexpected MSI\n");
1160 /* see if there's any more pending in this vector */
1161 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1167 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1170 static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
1171 struct msi_desc *desc)
1173 struct tegra_msi *msi = to_tegra_msi(chip);
1178 hwirq = tegra_msi_alloc(msi);
1182 irq = irq_create_mapping(msi->domain, hwirq);
1184 tegra_msi_free(msi, hwirq);
1188 irq_set_msi_desc(irq, desc);
1190 msg.address_lo = virt_to_phys((void *)msi->pages);
1191 /* 32 bit address only */
1195 write_msi_msg(irq, &msg);
1200 static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
1202 struct tegra_msi *msi = to_tegra_msi(chip);
1203 struct irq_data *d = irq_get_irq_data(irq);
1204 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1206 irq_dispose_mapping(irq);
1207 tegra_msi_free(msi, hwirq);
1210 static struct irq_chip tegra_msi_irq_chip = {
1211 .name = "Tegra PCIe MSI",
1212 .irq_enable = unmask_msi_irq,
1213 .irq_disable = mask_msi_irq,
1214 .irq_mask = mask_msi_irq,
1215 .irq_unmask = unmask_msi_irq,
1218 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1219 irq_hw_number_t hwirq)
1221 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1222 irq_set_chip_data(irq, domain->host_data);
1223 set_irq_flags(irq, IRQF_VALID);
1225 tegra_cpuidle_pcie_irqs_in_use();
1230 static const struct irq_domain_ops msi_domain_ops = {
1231 .map = tegra_msi_map,
1234 static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1236 struct platform_device *pdev = to_platform_device(pcie->dev);
1237 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1238 struct tegra_msi *msi = &pcie->msi;
1243 mutex_init(&msi->lock);
1245 msi->chip.dev = pcie->dev;
1246 msi->chip.setup_irq = tegra_msi_setup_irq;
1247 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1249 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1250 &msi_domain_ops, &msi->chip);
1252 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1256 err = platform_get_irq_byname(pdev, "msi");
1258 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1264 err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
1265 tegra_msi_irq_chip.name, pcie);
1267 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1271 /* setup AFI/FPCI range */
1272 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1273 base = virt_to_phys((void *)msi->pages);
1275 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1276 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1277 /* this register is in 4K increments */
1278 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1280 /* enable all MSI vectors */
1281 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1282 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1283 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1284 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1285 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1286 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1287 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1288 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1290 /* and unmask the MSI interrupt */
1291 reg = afi_readl(pcie, AFI_INTR_MASK);
1292 reg |= AFI_INTR_MASK_MSI_MASK;
1293 afi_writel(pcie, reg, AFI_INTR_MASK);
1298 irq_domain_remove(msi->domain);
1302 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1304 struct tegra_msi *msi = &pcie->msi;
1305 unsigned int i, irq;
1308 /* mask the MSI interrupt */
1309 value = afi_readl(pcie, AFI_INTR_MASK);
1310 value &= ~AFI_INTR_MASK_MSI_MASK;
1311 afi_writel(pcie, value, AFI_INTR_MASK);
1313 /* disable all MSI vectors */
1314 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1315 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1316 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1317 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1318 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1319 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1320 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1321 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1323 free_pages(msi->pages, 0);
1326 free_irq(msi->irq, pcie);
1328 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1329 irq = irq_find_mapping(msi->domain, i);
1331 irq_dispose_mapping(irq);
1334 irq_domain_remove(msi->domain);
1339 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1342 struct device_node *np = pcie->dev->of_node;
1344 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1347 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1348 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1352 dev_info(pcie->dev, "2x3 configuration\n");
1353 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1357 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1358 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1361 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1364 dev_info(pcie->dev, "single-mode configuration\n");
1365 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1369 dev_info(pcie->dev, "dual-mode configuration\n");
1370 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1379 * Check whether a given set of supplies is available in a device tree node.
1380 * This is used to check whether the new or the legacy device tree bindings
1383 static bool of_regulator_bulk_available(struct device_node *np,
1384 struct regulator_bulk_data *supplies,
1385 unsigned int num_supplies)
1390 for (i = 0; i < num_supplies; i++) {
1391 snprintf(property, 32, "%s-supply", supplies[i].supply);
1393 if (of_find_property(np, property, NULL) == NULL)
1401 * Old versions of the device tree binding for this device used a set of power
1402 * supplies that didn't match the hardware inputs. This happened to work for a
1403 * number of cases but is not future proof. However to preserve backwards-
1404 * compatibility with old device trees, this function will try to use the old
1407 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1409 struct device_node *np = pcie->dev->of_node;
1411 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1412 pcie->num_supplies = 3;
1413 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1414 pcie->num_supplies = 2;
1416 if (pcie->num_supplies == 0) {
1417 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1422 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1423 sizeof(*pcie->supplies),
1425 if (!pcie->supplies)
1428 pcie->supplies[0].supply = "pex-clk";
1429 pcie->supplies[1].supply = "vdd";
1431 if (pcie->num_supplies > 2)
1432 pcie->supplies[2].supply = "avdd";
1434 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1439 * Obtains the list of regulators required for a particular generation of the
1442 * This would've been nice to do simply by providing static tables for use
1443 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1444 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1445 * and either seems to be optional depending on which ports are being used.
1447 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1449 struct device_node *np = pcie->dev->of_node;
1452 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1453 bool need_pexa = false, need_pexb = false;
1455 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1456 if (lane_mask & 0x0f)
1459 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1460 if (lane_mask & 0x30)
1463 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1464 (need_pexb ? 2 : 0);
1466 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1467 sizeof(*pcie->supplies),
1469 if (!pcie->supplies)
1472 pcie->supplies[i++].supply = "avdd-pex-pll";
1473 pcie->supplies[i++].supply = "hvdd-pex";
1474 pcie->supplies[i++].supply = "vddio-pex-ctl";
1475 pcie->supplies[i++].supply = "avdd-plle";
1478 pcie->supplies[i++].supply = "avdd-pexa";
1479 pcie->supplies[i++].supply = "vdd-pexa";
1483 pcie->supplies[i++].supply = "avdd-pexb";
1484 pcie->supplies[i++].supply = "vdd-pexb";
1486 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1487 pcie->num_supplies = 5;
1489 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1490 sizeof(*pcie->supplies),
1492 if (!pcie->supplies)
1495 pcie->supplies[0].supply = "avdd-pex";
1496 pcie->supplies[1].supply = "vdd-pex";
1497 pcie->supplies[2].supply = "avdd-pex-pll";
1498 pcie->supplies[3].supply = "avdd-plle";
1499 pcie->supplies[4].supply = "vddio-pex-clk";
1502 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1503 pcie->num_supplies))
1504 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1508 * If not all regulators are available for this new scheme, assume
1509 * that the device tree complies with an older version of the device
1512 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1514 devm_kfree(pcie->dev, pcie->supplies);
1515 pcie->num_supplies = 0;
1517 return tegra_pcie_get_legacy_regulators(pcie);
1520 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1522 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1523 struct device_node *np = pcie->dev->of_node, *port;
1524 struct of_pci_range_parser parser;
1525 struct of_pci_range range;
1526 u32 lanes = 0, mask = 0;
1527 unsigned int lane = 0;
1528 struct resource res;
1531 memset(&pcie->all, 0, sizeof(pcie->all));
1532 pcie->all.flags = IORESOURCE_MEM;
1533 pcie->all.name = np->full_name;
1534 pcie->all.start = ~0;
1537 if (of_pci_range_parser_init(&parser, np)) {
1538 dev_err(pcie->dev, "missing \"ranges\" property\n");
1542 for_each_of_pci_range(&parser, &range) {
1543 of_pci_range_to_resource(&range, np, &res);
1545 switch (res.flags & IORESOURCE_TYPE_BITS) {
1547 memcpy(&pcie->io, &res, sizeof(res));
1548 pcie->io.name = np->full_name;
1551 case IORESOURCE_MEM:
1552 if (res.flags & IORESOURCE_PREFETCH) {
1553 memcpy(&pcie->prefetch, &res, sizeof(res));
1554 pcie->prefetch.name = "prefetchable";
1556 memcpy(&pcie->mem, &res, sizeof(res));
1557 pcie->mem.name = "non-prefetchable";
1562 if (res.start <= pcie->all.start)
1563 pcie->all.start = res.start;
1565 if (res.end >= pcie->all.end)
1566 pcie->all.end = res.end;
1569 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1573 err = of_pci_parse_bus_range(np, &pcie->busn);
1575 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1577 pcie->busn.name = np->name;
1578 pcie->busn.start = 0;
1579 pcie->busn.end = 0xff;
1580 pcie->busn.flags = IORESOURCE_BUS;
1583 /* parse root ports */
1584 for_each_child_of_node(np, port) {
1585 struct tegra_pcie_port *rp;
1589 err = of_pci_get_devfn(port);
1591 dev_err(pcie->dev, "failed to parse address: %d\n",
1596 index = PCI_SLOT(err);
1598 if (index < 1 || index > soc->num_ports) {
1599 dev_err(pcie->dev, "invalid port number: %d\n", index);
1605 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1607 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1613 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1617 lanes |= value << (index << 3);
1619 if (!of_device_is_available(port)) {
1624 mask |= ((1 << value) - 1) << lane;
1627 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1631 err = of_address_to_resource(port, 0, &rp->regs);
1633 dev_err(pcie->dev, "failed to parse address: %d\n",
1638 INIT_LIST_HEAD(&rp->list);
1643 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1644 if (IS_ERR(rp->base))
1645 return PTR_ERR(rp->base);
1647 list_add_tail(&rp->list, &pcie->ports);
1650 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1652 dev_err(pcie->dev, "invalid lane configuration\n");
1656 err = tegra_pcie_get_regulators(pcie, mask);
1664 * FIXME: If there are no PCIe cards attached, then calling this function
1665 * can result in the increase of the bootup time as there are big timeout
1668 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1669 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1671 unsigned int retries = 3;
1672 unsigned long value;
1675 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1678 value = readl(port->base + RP_VEND_XP);
1680 if (value & RP_VEND_XP_DL_UP)
1683 usleep_range(1000, 2000);
1684 } while (--timeout);
1687 dev_err(port->pcie->dev, "link %u down, retrying\n",
1692 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1695 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1697 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1700 usleep_range(1000, 2000);
1701 } while (--timeout);
1704 tegra_pcie_port_reset(port);
1705 } while (--retries);
1710 static int tegra_pcie_enable(struct tegra_pcie *pcie)
1712 struct tegra_pcie_port *port, *tmp;
1715 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1716 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
1717 port->index, port->lanes);
1719 tegra_pcie_port_enable(port);
1721 if (tegra_pcie_port_check_link(port))
1724 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
1726 tegra_pcie_port_disable(port);
1727 tegra_pcie_port_free(port);
1730 memset(&hw, 0, sizeof(hw));
1732 hw.nr_controllers = 1;
1733 hw.private_data = (void **)&pcie;
1734 hw.setup = tegra_pcie_setup;
1735 hw.map_irq = tegra_pcie_map_irq;
1736 hw.add_bus = tegra_pcie_add_bus;
1737 hw.scan = tegra_pcie_scan_bus;
1738 hw.ops = &tegra_pcie_ops;
1740 pci_common_init_dev(pcie->dev, &hw);
1745 static const struct tegra_pcie_soc_data tegra20_pcie_data = {
1747 .msi_base_shift = 0,
1748 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1749 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1750 .has_pex_clkreq_en = false,
1751 .has_pex_bias_ctrl = false,
1752 .has_intr_prsnt_sense = false,
1753 .has_cml_clk = false,
1756 static const struct tegra_pcie_soc_data tegra30_pcie_data = {
1758 .msi_base_shift = 8,
1759 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1760 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1761 .has_pex_clkreq_en = true,
1762 .has_pex_bias_ctrl = true,
1763 .has_intr_prsnt_sense = true,
1764 .has_cml_clk = true,
1767 static const struct of_device_id tegra_pcie_of_match[] = {
1768 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
1769 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
1772 MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
1774 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
1776 struct tegra_pcie *pcie = s->private;
1778 if (list_empty(&pcie->ports))
1781 seq_printf(s, "Index Status\n");
1783 return seq_list_start(&pcie->ports, *pos);
1786 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
1788 struct tegra_pcie *pcie = s->private;
1790 return seq_list_next(v, &pcie->ports, pos);
1793 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
1797 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
1799 bool up = false, active = false;
1800 struct tegra_pcie_port *port;
1803 port = list_entry(v, struct tegra_pcie_port, list);
1805 value = readl(port->base + RP_VEND_XP);
1807 if (value & RP_VEND_XP_DL_UP)
1810 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1812 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1815 seq_printf(s, "%2u ", port->index);
1818 seq_printf(s, "up");
1822 seq_printf(s, ", ");
1824 seq_printf(s, "active");
1827 seq_printf(s, "\n");
1831 static const struct seq_operations tegra_pcie_ports_seq_ops = {
1832 .start = tegra_pcie_ports_seq_start,
1833 .next = tegra_pcie_ports_seq_next,
1834 .stop = tegra_pcie_ports_seq_stop,
1835 .show = tegra_pcie_ports_seq_show,
1838 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
1840 struct tegra_pcie *pcie = inode->i_private;
1844 err = seq_open(file, &tegra_pcie_ports_seq_ops);
1848 s = file->private_data;
1854 static const struct file_operations tegra_pcie_ports_ops = {
1855 .owner = THIS_MODULE,
1856 .open = tegra_pcie_ports_open,
1858 .llseek = seq_lseek,
1859 .release = seq_release,
1862 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
1864 struct dentry *file;
1866 pcie->debugfs = debugfs_create_dir("pcie", NULL);
1870 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
1871 pcie, &tegra_pcie_ports_ops);
1878 debugfs_remove_recursive(pcie->debugfs);
1879 pcie->debugfs = NULL;
1883 static int tegra_pcie_probe(struct platform_device *pdev)
1885 const struct of_device_id *match;
1886 struct tegra_pcie *pcie;
1889 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
1893 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1897 INIT_LIST_HEAD(&pcie->buses);
1898 INIT_LIST_HEAD(&pcie->ports);
1899 pcie->soc_data = match->data;
1900 pcie->dev = &pdev->dev;
1902 err = tegra_pcie_parse_dt(pcie);
1906 pcibios_min_mem = 0;
1908 err = tegra_pcie_get_resources(pcie);
1910 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
1914 err = tegra_pcie_enable_controller(pcie);
1918 /* setup the AFI address translations */
1919 tegra_pcie_setup_translations(pcie);
1921 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1922 err = tegra_pcie_enable_msi(pcie);
1925 "failed to enable MSI support: %d\n",
1931 err = tegra_pcie_enable(pcie);
1933 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
1937 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1938 err = tegra_pcie_debugfs_init(pcie);
1940 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
1944 platform_set_drvdata(pdev, pcie);
1948 if (IS_ENABLED(CONFIG_PCI_MSI))
1949 tegra_pcie_disable_msi(pcie);
1951 tegra_pcie_put_resources(pcie);
1955 static struct platform_driver tegra_pcie_driver = {
1957 .name = "tegra-pcie",
1958 .owner = THIS_MODULE,
1959 .of_match_table = tegra_pcie_of_match,
1960 .suppress_bind_attrs = true,
1962 .probe = tegra_pcie_probe,
1964 module_platform_driver(tegra_pcie_driver);
1966 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1967 MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
1968 MODULE_LICENSE("GPL v2");