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 #include <asm/mach/irq.h>
55 #include <asm/mach/map.h>
56 #include <asm/mach/pci.h>
58 #define INT_PCI_MSI_NR (8 * 32)
60 /* register definitions */
62 #define AFI_AXI_BAR0_SZ 0x00
63 #define AFI_AXI_BAR1_SZ 0x04
64 #define AFI_AXI_BAR2_SZ 0x08
65 #define AFI_AXI_BAR3_SZ 0x0c
66 #define AFI_AXI_BAR4_SZ 0x10
67 #define AFI_AXI_BAR5_SZ 0x14
69 #define AFI_AXI_BAR0_START 0x18
70 #define AFI_AXI_BAR1_START 0x1c
71 #define AFI_AXI_BAR2_START 0x20
72 #define AFI_AXI_BAR3_START 0x24
73 #define AFI_AXI_BAR4_START 0x28
74 #define AFI_AXI_BAR5_START 0x2c
76 #define AFI_FPCI_BAR0 0x30
77 #define AFI_FPCI_BAR1 0x34
78 #define AFI_FPCI_BAR2 0x38
79 #define AFI_FPCI_BAR3 0x3c
80 #define AFI_FPCI_BAR4 0x40
81 #define AFI_FPCI_BAR5 0x44
83 #define AFI_CACHE_BAR0_SZ 0x48
84 #define AFI_CACHE_BAR0_ST 0x4c
85 #define AFI_CACHE_BAR1_SZ 0x50
86 #define AFI_CACHE_BAR1_ST 0x54
88 #define AFI_MSI_BAR_SZ 0x60
89 #define AFI_MSI_FPCI_BAR_ST 0x64
90 #define AFI_MSI_AXI_BAR_ST 0x68
92 #define AFI_MSI_VEC0 0x6c
93 #define AFI_MSI_VEC1 0x70
94 #define AFI_MSI_VEC2 0x74
95 #define AFI_MSI_VEC3 0x78
96 #define AFI_MSI_VEC4 0x7c
97 #define AFI_MSI_VEC5 0x80
98 #define AFI_MSI_VEC6 0x84
99 #define AFI_MSI_VEC7 0x88
101 #define AFI_MSI_EN_VEC0 0x8c
102 #define AFI_MSI_EN_VEC1 0x90
103 #define AFI_MSI_EN_VEC2 0x94
104 #define AFI_MSI_EN_VEC3 0x98
105 #define AFI_MSI_EN_VEC4 0x9c
106 #define AFI_MSI_EN_VEC5 0xa0
107 #define AFI_MSI_EN_VEC6 0xa4
108 #define AFI_MSI_EN_VEC7 0xa8
110 #define AFI_CONFIGURATION 0xac
111 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
113 #define AFI_FPCI_ERROR_MASKS 0xb0
115 #define AFI_INTR_MASK 0xb4
116 #define AFI_INTR_MASK_INT_MASK (1 << 0)
117 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
119 #define AFI_INTR_CODE 0xb8
120 #define AFI_INTR_CODE_MASK 0xf
121 #define AFI_INTR_INI_SLAVE_ERROR 1
122 #define AFI_INTR_INI_DECODE_ERROR 2
123 #define AFI_INTR_TARGET_ABORT 3
124 #define AFI_INTR_MASTER_ABORT 4
125 #define AFI_INTR_INVALID_WRITE 5
126 #define AFI_INTR_LEGACY 6
127 #define AFI_INTR_FPCI_DECODE_ERROR 7
128 #define AFI_INTR_AXI_DECODE_ERROR 8
129 #define AFI_INTR_FPCI_TIMEOUT 9
130 #define AFI_INTR_PE_PRSNT_SENSE 10
131 #define AFI_INTR_PE_CLKREQ_SENSE 11
132 #define AFI_INTR_CLKCLAMP_SENSE 12
133 #define AFI_INTR_RDY4PD_SENSE 13
134 #define AFI_INTR_P2P_ERROR 14
136 #define AFI_INTR_SIGNATURE 0xbc
137 #define AFI_UPPER_FPCI_ADDRESS 0xc0
138 #define AFI_SM_INTR_ENABLE 0xc4
139 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
140 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
141 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
142 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
143 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
144 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
145 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
146 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
148 #define AFI_AFI_INTR_ENABLE 0xc8
149 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
150 #define AFI_INTR_EN_INI_DECERR (1 << 1)
151 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
152 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
153 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
154 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
155 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
156 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
157 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
159 #define AFI_PCIE_CONFIG 0x0f8
160 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
161 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
162 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
163 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
164 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
165 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
166 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
167 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
168 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
169 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
171 #define AFI_FUSE 0x104
172 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
174 #define AFI_PEX0_CTRL 0x110
175 #define AFI_PEX1_CTRL 0x118
176 #define AFI_PEX2_CTRL 0x128
177 #define AFI_PEX_CTRL_RST (1 << 0)
178 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
179 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
180 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
182 #define AFI_PLLE_CONTROL 0x160
183 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
184 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
186 #define AFI_PEXBIAS_CTRL_0 0x168
188 #define RP_VEND_XP 0x00000F00
189 #define RP_VEND_XP_DL_UP (1 << 30)
191 #define RP_PRIV_MISC 0x00000FE0
192 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
193 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
195 #define RP_LINK_CONTROL_STATUS 0x00000090
196 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
197 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
199 #define PADS_CTL_SEL 0x0000009C
201 #define PADS_CTL 0x000000A0
202 #define PADS_CTL_IDDQ_1L (1 << 0)
203 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
204 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
206 #define PADS_PLL_CTL_TEGRA20 0x000000B8
207 #define PADS_PLL_CTL_TEGRA30 0x000000B4
208 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
209 #define PADS_PLL_CTL_LOCKDET (1 << 8)
210 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
211 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
212 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
213 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
214 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
215 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
216 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
217 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
219 #define PADS_REFCLK_CFG0 0x000000C8
220 #define PADS_REFCLK_CFG1 0x000000CC
221 #define PADS_REFCLK_BIAS 0x000000D0
224 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
225 * entries, one entry per PCIe port. These field definitions and desired
226 * values aren't in the TRM, but do come from NVIDIA.
228 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
229 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
230 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
231 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
233 /* Default value provided by HW engineering is 0xfa5c */
234 #define PADS_REFCLK_CFG_VALUE \
236 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
237 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
238 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
239 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
243 struct msi_controller chip;
244 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
245 struct irq_domain *domain;
251 /* used to differentiate between Tegra SoC generations */
252 struct tegra_pcie_soc_data {
253 unsigned int num_ports;
254 unsigned int msi_base_shift;
257 bool has_pex_clkreq_en;
258 bool has_pex_bias_ctrl;
259 bool has_intr_prsnt_sense;
264 static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
266 return container_of(chip, struct tegra_msi, chip);
276 struct list_head buses;
283 struct resource prefetch;
284 struct resource busn;
296 struct reset_control *pex_rst;
297 struct reset_control *afi_rst;
298 struct reset_control *pcie_xrst;
303 struct tegra_msi msi;
305 struct list_head ports;
308 struct regulator_bulk_data *supplies;
309 unsigned int num_supplies;
311 const struct tegra_pcie_soc_data *soc_data;
312 struct dentry *debugfs;
315 struct tegra_pcie_port {
316 struct tegra_pcie *pcie;
317 struct device_node *np;
318 struct list_head list;
319 struct resource regs;
327 struct tegra_pcie_bus {
328 struct vm_struct *area;
329 struct list_head list;
333 static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
335 return sys->private_data;
338 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
339 unsigned long offset)
341 writel(value, pcie->afi + offset);
344 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
346 return readl(pcie->afi + offset);
349 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
350 unsigned long offset)
352 writel(value, pcie->pads + offset);
355 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
357 return readl(pcie->pads + offset);
361 * The configuration space mapping on Tegra is somewhat similar to the ECAM
362 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
363 * register accesses are mapped:
365 * [27:24] extended register number
367 * [15:11] device number
368 * [10: 8] function number
369 * [ 7: 0] register number
371 * Mapping the whole extended configuration space would require 256 MiB of
372 * virtual address space, only a small part of which will actually be used.
373 * To work around this, a 1 MiB of virtual addresses are allocated per bus
374 * when the bus is first accessed. When the physical range is mapped, the
375 * the bus number bits are hidden so that the extended register number bits
376 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
378 * [19:16] extended register number
379 * [15:11] device number
380 * [10: 8] function number
381 * [ 7: 0] register number
383 * This is achieved by stitching together 16 chunks of 64 KiB of physical
384 * address space via the MMU.
386 static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
388 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
389 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
392 static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
395 pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
396 L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
397 phys_addr_t cs = pcie->cs->start;
398 struct tegra_pcie_bus *bus;
402 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
404 return ERR_PTR(-ENOMEM);
406 INIT_LIST_HEAD(&bus->list);
409 /* allocate 1 MiB of virtual addresses */
410 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
416 /* map each of the 16 chunks of 64 KiB each */
417 for (i = 0; i < 16; i++) {
418 unsigned long virt = (unsigned long)bus->area->addr +
420 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
422 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
424 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
433 vunmap(bus->area->addr);
439 static int tegra_pcie_add_bus(struct pci_bus *bus)
441 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
442 struct tegra_pcie_bus *b;
444 b = tegra_pcie_bus_alloc(pcie, bus->number);
448 list_add_tail(&b->list, &pcie->buses);
453 static void tegra_pcie_remove_bus(struct pci_bus *child)
455 struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
456 struct tegra_pcie_bus *bus, *tmp;
458 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
459 if (bus->nr == child->number) {
460 vunmap(bus->area->addr);
461 list_del(&bus->list);
468 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
472 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
473 void __iomem *addr = NULL;
475 if (bus->number == 0) {
476 unsigned int slot = PCI_SLOT(devfn);
477 struct tegra_pcie_port *port;
479 list_for_each_entry(port, &pcie->ports, list) {
480 if (port->index + 1 == slot) {
481 addr = port->base + (where & ~3);
486 struct tegra_pcie_bus *b;
488 list_for_each_entry(b, &pcie->buses, list)
489 if (b->nr == bus->number)
490 addr = (void __iomem *)b->area->addr;
494 "failed to map cfg. space for bus %u\n",
499 addr += tegra_pcie_conf_offset(devfn, where);
505 static struct pci_ops tegra_pcie_ops = {
506 .add_bus = tegra_pcie_add_bus,
507 .remove_bus = tegra_pcie_remove_bus,
508 .map_bus = tegra_pcie_map_bus,
509 .read = pci_generic_config_read32,
510 .write = pci_generic_config_write32,
513 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
515 unsigned long ret = 0;
517 switch (port->index) {
534 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
536 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
539 /* pulse reset signal */
540 value = afi_readl(port->pcie, ctrl);
541 value &= ~AFI_PEX_CTRL_RST;
542 afi_writel(port->pcie, value, ctrl);
544 usleep_range(1000, 2000);
546 value = afi_readl(port->pcie, ctrl);
547 value |= AFI_PEX_CTRL_RST;
548 afi_writel(port->pcie, value, ctrl);
551 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
553 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
554 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
557 /* enable reference clock */
558 value = afi_readl(port->pcie, ctrl);
559 value |= AFI_PEX_CTRL_REFCLK_EN;
561 if (soc->has_pex_clkreq_en)
562 value |= AFI_PEX_CTRL_CLKREQ_EN;
564 value |= AFI_PEX_CTRL_OVERRIDE_EN;
566 afi_writel(port->pcie, value, ctrl);
568 tegra_pcie_port_reset(port);
571 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
573 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
574 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
577 /* assert port reset */
578 value = afi_readl(port->pcie, ctrl);
579 value &= ~AFI_PEX_CTRL_RST;
580 afi_writel(port->pcie, value, ctrl);
582 /* disable reference clock */
583 value = afi_readl(port->pcie, ctrl);
585 if (soc->has_pex_clkreq_en)
586 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
588 value &= ~AFI_PEX_CTRL_REFCLK_EN;
589 afi_writel(port->pcie, value, ctrl);
592 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
594 struct tegra_pcie *pcie = port->pcie;
596 devm_iounmap(pcie->dev, port->base);
597 devm_release_mem_region(pcie->dev, port->regs.start,
598 resource_size(&port->regs));
599 list_del(&port->list);
600 devm_kfree(pcie->dev, port);
603 /* Tegra PCIE root complex wrongly reports device class */
604 static void tegra_pcie_fixup_class(struct pci_dev *dev)
606 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
608 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
609 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
610 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
611 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
613 /* Tegra PCIE requires relaxed ordering */
614 static void tegra_pcie_relax_enable(struct pci_dev *dev)
616 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
618 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
620 static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
622 struct tegra_pcie *pcie = sys_to_pcie(sys);
625 sys->mem_offset = pcie->offset.mem;
626 sys->io_offset = pcie->offset.io;
628 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->io);
632 err = devm_request_resource(pcie->dev, &ioport_resource, &pcie->pio);
636 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
640 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
644 pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
645 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
646 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
648 pci_add_resource(&sys->resources, &pcie->busn);
650 pci_ioremap_io(pcie->pio.start, pcie->io.start);
655 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
657 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
660 tegra_cpuidle_pcie_irqs_in_use();
662 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
669 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
671 const char *err_msg[] = {
679 "Response decoding error",
680 "AXI response decoding error",
681 "Transaction timeout",
682 "Slot present pin change",
683 "Slot clock request change",
684 "TMS clock ramp change",
685 "TMS ready for power down",
688 struct tegra_pcie *pcie = arg;
691 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
692 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
693 afi_writel(pcie, 0, AFI_INTR_CODE);
695 if (code == AFI_INTR_LEGACY)
698 if (code >= ARRAY_SIZE(err_msg))
702 * do not pollute kernel log with master abort reports since they
703 * happen a lot during enumeration
705 if (code == AFI_INTR_MASTER_ABORT)
706 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
709 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
712 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
713 code == AFI_INTR_FPCI_DECODE_ERROR) {
714 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
715 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
717 if (code == AFI_INTR_MASTER_ABORT)
718 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
720 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
727 * FPCI map is as follows:
728 * - 0xfdfc000000: I/O space
729 * - 0xfdfe000000: type 0 configuration space
730 * - 0xfdff000000: type 1 configuration space
731 * - 0xfe00000000: type 0 extended configuration space
732 * - 0xfe10000000: type 1 extended configuration space
734 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
736 u32 fpci_bar, size, axi_address;
738 /* Bar 0: type 1 extended configuration space */
739 fpci_bar = 0xfe100000;
740 size = resource_size(pcie->cs);
741 axi_address = pcie->cs->start;
742 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
743 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
744 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
746 /* Bar 1: downstream IO bar */
747 fpci_bar = 0xfdfc0000;
748 size = resource_size(&pcie->io);
749 axi_address = pcie->io.start;
750 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
751 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
752 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
754 /* Bar 2: prefetchable memory BAR */
755 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
756 size = resource_size(&pcie->prefetch);
757 axi_address = pcie->prefetch.start;
758 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
759 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
760 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
762 /* Bar 3: non prefetchable memory BAR */
763 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
764 size = resource_size(&pcie->mem);
765 axi_address = pcie->mem.start;
766 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
767 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
768 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
770 /* NULL out the remaining BARs as they are not used */
771 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
772 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
773 afi_writel(pcie, 0, AFI_FPCI_BAR4);
775 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
776 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
777 afi_writel(pcie, 0, AFI_FPCI_BAR5);
779 /* map all upstream transactions as uncached */
780 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
781 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
782 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
783 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
785 /* MSI translations are setup only when needed */
786 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
787 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
788 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
789 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
792 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
794 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
797 timeout = jiffies + msecs_to_jiffies(timeout);
799 while (time_before(jiffies, timeout)) {
800 value = pads_readl(pcie, soc->pads_pll_ctl);
801 if (value & PADS_PLL_CTL_LOCKDET)
808 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
810 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
814 /* initialize internal PHY, enable up to 16 PCIE lanes */
815 pads_writel(pcie, 0x0, PADS_CTL_SEL);
817 /* override IDDQ to 1 on all 4 lanes */
818 value = pads_readl(pcie, PADS_CTL);
819 value |= PADS_CTL_IDDQ_1L;
820 pads_writel(pcie, value, PADS_CTL);
823 * Set up PHY PLL inputs select PLLE output as refclock,
824 * set TX ref sel to div10 (not div5).
826 value = pads_readl(pcie, soc->pads_pll_ctl);
827 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
828 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
829 pads_writel(pcie, value, soc->pads_pll_ctl);
832 value = pads_readl(pcie, soc->pads_pll_ctl);
833 value &= ~PADS_PLL_CTL_RST_B4SM;
834 pads_writel(pcie, value, soc->pads_pll_ctl);
836 usleep_range(20, 100);
838 /* take PLL out of reset */
839 value = pads_readl(pcie, soc->pads_pll_ctl);
840 value |= PADS_PLL_CTL_RST_B4SM;
841 pads_writel(pcie, value, soc->pads_pll_ctl);
843 /* Configure the reference clock driver */
844 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
845 pads_writel(pcie, value, PADS_REFCLK_CFG0);
846 if (soc->num_ports > 2)
847 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
849 /* wait for the PLL to lock */
850 err = tegra_pcie_pll_wait(pcie, 500);
852 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
856 /* turn off IDDQ override */
857 value = pads_readl(pcie, PADS_CTL);
858 value &= ~PADS_CTL_IDDQ_1L;
859 pads_writel(pcie, value, PADS_CTL);
861 /* enable TX/RX data */
862 value = pads_readl(pcie, PADS_CTL);
863 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
864 pads_writel(pcie, value, PADS_CTL);
869 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
871 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
874 /* disable 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);
880 value = pads_readl(pcie, PADS_CTL);
881 value |= PADS_CTL_IDDQ_1L;
882 pads_writel(pcie, PADS_CTL, value);
885 value = pads_readl(pcie, soc->pads_pll_ctl);
886 value &= ~PADS_PLL_CTL_RST_B4SM;
887 pads_writel(pcie, value, soc->pads_pll_ctl);
889 usleep_range(20, 100);
894 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
896 struct device *dev = port->pcie->dev;
900 for (i = 0; i < port->lanes; i++) {
901 err = phy_power_on(port->phys[i]);
903 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
912 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
914 struct device *dev = port->pcie->dev;
918 for (i = 0; i < port->lanes; i++) {
919 err = phy_power_off(port->phys[i]);
921 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
930 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
932 struct tegra_pcie_port *port;
935 if (pcie->legacy_phy) {
937 err = phy_power_on(pcie->phy);
939 err = tegra_pcie_phy_enable(pcie);
942 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
947 list_for_each_entry(port, &pcie->ports, list) {
948 err = tegra_pcie_port_phy_power_on(port);
951 "failed to power on PCIe port %u PHY: %d\n",
960 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
962 struct tegra_pcie_port *port;
965 if (pcie->legacy_phy) {
967 err = phy_power_off(pcie->phy);
969 err = tegra_pcie_phy_disable(pcie);
972 dev_err(pcie->dev, "failed to power off PHY: %d\n",
978 list_for_each_entry(port, &pcie->ports, list) {
979 err = tegra_pcie_port_phy_power_off(port);
982 "failed to power off PCIe port %u PHY: %d\n",
991 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
993 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
994 struct tegra_pcie_port *port;
998 /* enable PLL power down */
1000 value = afi_readl(pcie, AFI_PLLE_CONTROL);
1001 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
1002 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1003 afi_writel(pcie, value, AFI_PLLE_CONTROL);
1006 /* power down PCIe slot clock bias pad */
1007 if (soc->has_pex_bias_ctrl)
1008 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1010 /* configure mode and disable all ports */
1011 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1012 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1013 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1015 list_for_each_entry(port, &pcie->ports, list)
1016 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1018 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1020 if (soc->has_gen2) {
1021 value = afi_readl(pcie, AFI_FUSE);
1022 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1023 afi_writel(pcie, value, AFI_FUSE);
1025 value = afi_readl(pcie, AFI_FUSE);
1026 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1027 afi_writel(pcie, value, AFI_FUSE);
1030 err = tegra_pcie_phy_power_on(pcie);
1032 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
1036 /* take the PCIe interface module out of reset */
1037 reset_control_deassert(pcie->pcie_xrst);
1039 /* finally enable PCIe */
1040 value = afi_readl(pcie, AFI_CONFIGURATION);
1041 value |= AFI_CONFIGURATION_EN_FPCI;
1042 afi_writel(pcie, value, AFI_CONFIGURATION);
1044 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1045 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1046 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
1048 if (soc->has_intr_prsnt_sense)
1049 value |= AFI_INTR_EN_PRSNT_SENSE;
1051 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1052 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1054 /* don't enable MSI for now, only when needed */
1055 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1057 /* disable all exceptions */
1058 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1063 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1067 /* TODO: disable and unprepare clocks? */
1069 err = tegra_pcie_phy_power_off(pcie);
1071 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
1073 reset_control_assert(pcie->pcie_xrst);
1074 reset_control_assert(pcie->afi_rst);
1075 reset_control_assert(pcie->pex_rst);
1077 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1079 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1081 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
1084 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1086 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1089 reset_control_assert(pcie->pcie_xrst);
1090 reset_control_assert(pcie->afi_rst);
1091 reset_control_assert(pcie->pex_rst);
1093 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1095 /* enable regulators */
1096 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1098 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
1100 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
1104 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1108 reset_control_deassert(pcie->afi_rst);
1110 err = clk_prepare_enable(pcie->afi_clk);
1112 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1116 if (soc->has_cml_clk) {
1117 err = clk_prepare_enable(pcie->cml_clk);
1119 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1125 err = clk_prepare_enable(pcie->pll_e);
1127 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1134 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1136 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1138 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1139 if (IS_ERR(pcie->pex_clk))
1140 return PTR_ERR(pcie->pex_clk);
1142 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1143 if (IS_ERR(pcie->afi_clk))
1144 return PTR_ERR(pcie->afi_clk);
1146 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1147 if (IS_ERR(pcie->pll_e))
1148 return PTR_ERR(pcie->pll_e);
1150 if (soc->has_cml_clk) {
1151 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1152 if (IS_ERR(pcie->cml_clk))
1153 return PTR_ERR(pcie->cml_clk);
1159 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1161 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1162 if (IS_ERR(pcie->pex_rst))
1163 return PTR_ERR(pcie->pex_rst);
1165 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1166 if (IS_ERR(pcie->afi_rst))
1167 return PTR_ERR(pcie->afi_rst);
1169 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1170 if (IS_ERR(pcie->pcie_xrst))
1171 return PTR_ERR(pcie->pcie_xrst);
1176 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1180 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1181 if (IS_ERR(pcie->phy)) {
1182 err = PTR_ERR(pcie->phy);
1183 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1187 err = phy_init(pcie->phy);
1189 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1193 pcie->legacy_phy = true;
1198 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1199 struct device_node *np,
1200 const char *consumer,
1206 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1208 return ERR_PTR(-ENOMEM);
1210 phy = devm_of_phy_get(dev, np, name);
1213 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1219 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1221 struct device *dev = port->pcie->dev;
1226 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1230 for (i = 0; i < port->lanes; i++) {
1231 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1233 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1235 return PTR_ERR(phy);
1238 err = phy_init(phy);
1240 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1245 port->phys[i] = phy;
1251 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1253 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1254 struct device_node *np = pcie->dev->of_node;
1255 struct tegra_pcie_port *port;
1258 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1259 return tegra_pcie_phys_get_legacy(pcie);
1261 list_for_each_entry(port, &pcie->ports, list) {
1262 err = tegra_pcie_port_get_phys(port);
1270 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1272 struct platform_device *pdev = to_platform_device(pcie->dev);
1273 struct resource *pads, *afi, *res;
1276 err = tegra_pcie_clocks_get(pcie);
1278 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1282 err = tegra_pcie_resets_get(pcie);
1284 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1288 err = tegra_pcie_phys_get(pcie);
1290 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
1294 err = tegra_pcie_power_on(pcie);
1296 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1300 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1301 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1302 if (IS_ERR(pcie->pads)) {
1303 err = PTR_ERR(pcie->pads);
1307 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1308 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1309 if (IS_ERR(pcie->afi)) {
1310 err = PTR_ERR(pcie->afi);
1314 /* request configuration space, but remap later, on demand */
1315 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1317 err = -EADDRNOTAVAIL;
1321 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1322 resource_size(res), res->name);
1324 err = -EADDRNOTAVAIL;
1328 /* request interrupt */
1329 err = platform_get_irq_byname(pdev, "intr");
1331 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1337 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1339 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1346 tegra_pcie_power_off(pcie);
1350 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1355 free_irq(pcie->irq, pcie);
1357 tegra_pcie_power_off(pcie);
1359 err = phy_exit(pcie->phy);
1361 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1366 static int tegra_msi_alloc(struct tegra_msi *chip)
1370 mutex_lock(&chip->lock);
1372 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1373 if (msi < INT_PCI_MSI_NR)
1374 set_bit(msi, chip->used);
1378 mutex_unlock(&chip->lock);
1383 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1385 struct device *dev = chip->chip.dev;
1387 mutex_lock(&chip->lock);
1389 if (!test_bit(irq, chip->used))
1390 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1392 clear_bit(irq, chip->used);
1394 mutex_unlock(&chip->lock);
1397 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1399 struct tegra_pcie *pcie = data;
1400 struct tegra_msi *msi = &pcie->msi;
1401 unsigned int i, processed = 0;
1403 for (i = 0; i < 8; i++) {
1404 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1407 unsigned int offset = find_first_bit(®, 32);
1408 unsigned int index = i * 32 + offset;
1411 /* clear the interrupt */
1412 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1414 irq = irq_find_mapping(msi->domain, index);
1416 if (test_bit(index, msi->used))
1417 generic_handle_irq(irq);
1419 dev_info(pcie->dev, "unhandled MSI\n");
1422 * that's weird who triggered this?
1425 dev_info(pcie->dev, "unexpected MSI\n");
1428 /* see if there's any more pending in this vector */
1429 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1435 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1438 static int tegra_msi_setup_irq(struct msi_controller *chip,
1439 struct pci_dev *pdev, struct msi_desc *desc)
1441 struct tegra_msi *msi = to_tegra_msi(chip);
1446 hwirq = tegra_msi_alloc(msi);
1450 irq = irq_create_mapping(msi->domain, hwirq);
1452 tegra_msi_free(msi, hwirq);
1456 irq_set_msi_desc(irq, desc);
1458 msg.address_lo = virt_to_phys((void *)msi->pages);
1459 /* 32 bit address only */
1463 pci_write_msi_msg(irq, &msg);
1468 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1471 struct tegra_msi *msi = to_tegra_msi(chip);
1472 struct irq_data *d = irq_get_irq_data(irq);
1473 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1475 irq_dispose_mapping(irq);
1476 tegra_msi_free(msi, hwirq);
1479 static struct irq_chip tegra_msi_irq_chip = {
1480 .name = "Tegra PCIe MSI",
1481 .irq_enable = pci_msi_unmask_irq,
1482 .irq_disable = pci_msi_mask_irq,
1483 .irq_mask = pci_msi_mask_irq,
1484 .irq_unmask = pci_msi_unmask_irq,
1487 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1488 irq_hw_number_t hwirq)
1490 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1491 irq_set_chip_data(irq, domain->host_data);
1493 tegra_cpuidle_pcie_irqs_in_use();
1498 static const struct irq_domain_ops msi_domain_ops = {
1499 .map = tegra_msi_map,
1502 static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1504 struct platform_device *pdev = to_platform_device(pcie->dev);
1505 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1506 struct tegra_msi *msi = &pcie->msi;
1511 mutex_init(&msi->lock);
1513 msi->chip.dev = pcie->dev;
1514 msi->chip.setup_irq = tegra_msi_setup_irq;
1515 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1517 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1518 &msi_domain_ops, &msi->chip);
1520 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1524 err = platform_get_irq_byname(pdev, "msi");
1526 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1532 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1533 tegra_msi_irq_chip.name, pcie);
1535 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1539 /* setup AFI/FPCI range */
1540 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1541 base = virt_to_phys((void *)msi->pages);
1543 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1544 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1545 /* this register is in 4K increments */
1546 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1548 /* enable all MSI vectors */
1549 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1550 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1551 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1552 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1553 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1554 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1555 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1556 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1558 /* and unmask the MSI interrupt */
1559 reg = afi_readl(pcie, AFI_INTR_MASK);
1560 reg |= AFI_INTR_MASK_MSI_MASK;
1561 afi_writel(pcie, reg, AFI_INTR_MASK);
1566 irq_domain_remove(msi->domain);
1570 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1572 struct tegra_msi *msi = &pcie->msi;
1573 unsigned int i, irq;
1576 /* mask the MSI interrupt */
1577 value = afi_readl(pcie, AFI_INTR_MASK);
1578 value &= ~AFI_INTR_MASK_MSI_MASK;
1579 afi_writel(pcie, value, AFI_INTR_MASK);
1581 /* disable all MSI vectors */
1582 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1583 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1584 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1585 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1586 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1587 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1588 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1589 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1591 free_pages(msi->pages, 0);
1594 free_irq(msi->irq, pcie);
1596 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1597 irq = irq_find_mapping(msi->domain, i);
1599 irq_dispose_mapping(irq);
1602 irq_domain_remove(msi->domain);
1607 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1610 struct device_node *np = pcie->dev->of_node;
1612 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1615 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1616 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1620 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1621 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1624 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1627 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1628 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1632 dev_info(pcie->dev, "2x3 configuration\n");
1633 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1637 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1638 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1641 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1644 dev_info(pcie->dev, "single-mode configuration\n");
1645 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1649 dev_info(pcie->dev, "dual-mode configuration\n");
1650 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1659 * Check whether a given set of supplies is available in a device tree node.
1660 * This is used to check whether the new or the legacy device tree bindings
1663 static bool of_regulator_bulk_available(struct device_node *np,
1664 struct regulator_bulk_data *supplies,
1665 unsigned int num_supplies)
1670 for (i = 0; i < num_supplies; i++) {
1671 snprintf(property, 32, "%s-supply", supplies[i].supply);
1673 if (of_find_property(np, property, NULL) == NULL)
1681 * Old versions of the device tree binding for this device used a set of power
1682 * supplies that didn't match the hardware inputs. This happened to work for a
1683 * number of cases but is not future proof. However to preserve backwards-
1684 * compatibility with old device trees, this function will try to use the old
1687 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1689 struct device_node *np = pcie->dev->of_node;
1691 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1692 pcie->num_supplies = 3;
1693 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1694 pcie->num_supplies = 2;
1696 if (pcie->num_supplies == 0) {
1697 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1702 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1703 sizeof(*pcie->supplies),
1705 if (!pcie->supplies)
1708 pcie->supplies[0].supply = "pex-clk";
1709 pcie->supplies[1].supply = "vdd";
1711 if (pcie->num_supplies > 2)
1712 pcie->supplies[2].supply = "avdd";
1714 return devm_regulator_bulk_get(pcie->dev, pcie->num_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_node *np = pcie->dev->of_node;
1732 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1733 pcie->num_supplies = 7;
1735 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1736 sizeof(*pcie->supplies),
1738 if (!pcie->supplies)
1741 pcie->supplies[i++].supply = "avddio-pex";
1742 pcie->supplies[i++].supply = "dvddio-pex";
1743 pcie->supplies[i++].supply = "avdd-pex-pll";
1744 pcie->supplies[i++].supply = "hvdd-pex";
1745 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1746 pcie->supplies[i++].supply = "vddio-pex-ctl";
1747 pcie->supplies[i++].supply = "avdd-pll-erefe";
1748 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1749 bool need_pexa = false, need_pexb = false;
1751 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1752 if (lane_mask & 0x0f)
1755 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1756 if (lane_mask & 0x30)
1759 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1760 (need_pexb ? 2 : 0);
1762 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1763 sizeof(*pcie->supplies),
1765 if (!pcie->supplies)
1768 pcie->supplies[i++].supply = "avdd-pex-pll";
1769 pcie->supplies[i++].supply = "hvdd-pex";
1770 pcie->supplies[i++].supply = "vddio-pex-ctl";
1771 pcie->supplies[i++].supply = "avdd-plle";
1774 pcie->supplies[i++].supply = "avdd-pexa";
1775 pcie->supplies[i++].supply = "vdd-pexa";
1779 pcie->supplies[i++].supply = "avdd-pexb";
1780 pcie->supplies[i++].supply = "vdd-pexb";
1782 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1783 pcie->num_supplies = 5;
1785 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1786 sizeof(*pcie->supplies),
1788 if (!pcie->supplies)
1791 pcie->supplies[0].supply = "avdd-pex";
1792 pcie->supplies[1].supply = "vdd-pex";
1793 pcie->supplies[2].supply = "avdd-pex-pll";
1794 pcie->supplies[3].supply = "avdd-plle";
1795 pcie->supplies[4].supply = "vddio-pex-clk";
1798 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1799 pcie->num_supplies))
1800 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1804 * If not all regulators are available for this new scheme, assume
1805 * that the device tree complies with an older version of the device
1808 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1810 devm_kfree(pcie->dev, pcie->supplies);
1811 pcie->num_supplies = 0;
1813 return tegra_pcie_get_legacy_regulators(pcie);
1816 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1818 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1819 struct device_node *np = pcie->dev->of_node, *port;
1820 struct of_pci_range_parser parser;
1821 struct of_pci_range range;
1822 u32 lanes = 0, mask = 0;
1823 unsigned int lane = 0;
1824 struct resource res;
1827 memset(&pcie->all, 0, sizeof(pcie->all));
1828 pcie->all.flags = IORESOURCE_MEM;
1829 pcie->all.name = np->full_name;
1830 pcie->all.start = ~0;
1833 if (of_pci_range_parser_init(&parser, np)) {
1834 dev_err(pcie->dev, "missing \"ranges\" property\n");
1838 for_each_of_pci_range(&parser, &range) {
1839 err = of_pci_range_to_resource(&range, np, &res);
1843 switch (res.flags & IORESOURCE_TYPE_BITS) {
1845 /* Track the bus -> CPU I/O mapping offset. */
1846 pcie->offset.io = res.start - range.pci_addr;
1848 memcpy(&pcie->pio, &res, sizeof(res));
1849 pcie->pio.name = np->full_name;
1852 * The Tegra PCIe host bridge uses this to program the
1853 * mapping of the I/O space to the physical address,
1854 * so we override the .start and .end fields here that
1855 * of_pci_range_to_resource() converted to I/O space.
1856 * We also set the IORESOURCE_MEM type to clarify that
1857 * the resource is in the physical memory space.
1859 pcie->io.start = range.cpu_addr;
1860 pcie->io.end = range.cpu_addr + range.size - 1;
1861 pcie->io.flags = IORESOURCE_MEM;
1862 pcie->io.name = "I/O";
1864 memcpy(&res, &pcie->io, sizeof(res));
1867 case IORESOURCE_MEM:
1869 * Track the bus -> CPU memory mapping offset. This
1870 * assumes that the prefetchable and non-prefetchable
1871 * regions will be the last of type IORESOURCE_MEM in
1872 * the ranges property.
1874 pcie->offset.mem = res.start - range.pci_addr;
1876 if (res.flags & IORESOURCE_PREFETCH) {
1877 memcpy(&pcie->prefetch, &res, sizeof(res));
1878 pcie->prefetch.name = "prefetchable";
1880 memcpy(&pcie->mem, &res, sizeof(res));
1881 pcie->mem.name = "non-prefetchable";
1886 if (res.start <= pcie->all.start)
1887 pcie->all.start = res.start;
1889 if (res.end >= pcie->all.end)
1890 pcie->all.end = res.end;
1893 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1897 err = of_pci_parse_bus_range(np, &pcie->busn);
1899 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
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(pcie->dev, "failed to parse address: %d\n",
1920 index = PCI_SLOT(err);
1922 if (index < 1 || index > soc->num_ports) {
1923 dev_err(pcie->dev, "invalid port number: %d\n", index);
1929 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1931 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1937 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1941 lanes |= value << (index << 3);
1943 if (!of_device_is_available(port)) {
1948 mask |= ((1 << value) - 1) << lane;
1951 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1955 err = of_address_to_resource(port, 0, &rp->regs);
1957 dev_err(pcie->dev, "failed to parse address: %d\n",
1962 INIT_LIST_HEAD(&rp->list);
1968 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1969 if (IS_ERR(rp->base))
1970 return PTR_ERR(rp->base);
1972 list_add_tail(&rp->list, &pcie->ports);
1975 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1977 dev_err(pcie->dev, "invalid lane configuration\n");
1981 err = tegra_pcie_get_regulators(pcie, mask);
1989 * FIXME: If there are no PCIe cards attached, then calling this function
1990 * can result in the increase of the bootup time as there are big timeout
1993 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1994 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1996 unsigned int retries = 3;
1997 unsigned long value;
1999 /* override presence detection */
2000 value = readl(port->base + RP_PRIV_MISC);
2001 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2002 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2003 writel(value, port->base + RP_PRIV_MISC);
2006 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2009 value = readl(port->base + RP_VEND_XP);
2011 if (value & RP_VEND_XP_DL_UP)
2014 usleep_range(1000, 2000);
2015 } while (--timeout);
2018 dev_err(port->pcie->dev, "link %u down, retrying\n",
2023 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2026 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2028 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2031 usleep_range(1000, 2000);
2032 } while (--timeout);
2035 tegra_pcie_port_reset(port);
2036 } while (--retries);
2041 static int tegra_pcie_enable(struct tegra_pcie *pcie)
2043 struct tegra_pcie_port *port, *tmp;
2046 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2047 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2048 port->index, port->lanes);
2050 tegra_pcie_port_enable(port);
2052 if (tegra_pcie_port_check_link(port))
2055 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2057 tegra_pcie_port_disable(port);
2058 tegra_pcie_port_free(port);
2061 memset(&hw, 0, sizeof(hw));
2063 #ifdef CONFIG_PCI_MSI
2064 hw.msi_ctrl = &pcie->msi.chip;
2067 hw.nr_controllers = 1;
2068 hw.private_data = (void **)&pcie;
2069 hw.setup = tegra_pcie_setup;
2070 hw.map_irq = tegra_pcie_map_irq;
2071 hw.ops = &tegra_pcie_ops;
2073 pci_common_init_dev(pcie->dev, &hw);
2078 static const struct tegra_pcie_soc_data tegra20_pcie_data = {
2080 .msi_base_shift = 0,
2081 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2082 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2083 .has_pex_clkreq_en = false,
2084 .has_pex_bias_ctrl = false,
2085 .has_intr_prsnt_sense = false,
2086 .has_cml_clk = false,
2090 static const struct tegra_pcie_soc_data tegra30_pcie_data = {
2092 .msi_base_shift = 8,
2093 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2094 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2095 .has_pex_clkreq_en = true,
2096 .has_pex_bias_ctrl = true,
2097 .has_intr_prsnt_sense = true,
2098 .has_cml_clk = true,
2102 static const struct tegra_pcie_soc_data tegra124_pcie_data = {
2104 .msi_base_shift = 8,
2105 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2106 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2107 .has_pex_clkreq_en = true,
2108 .has_pex_bias_ctrl = true,
2109 .has_intr_prsnt_sense = true,
2110 .has_cml_clk = true,
2114 static const struct of_device_id tegra_pcie_of_match[] = {
2115 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
2116 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
2117 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
2121 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2123 struct tegra_pcie *pcie = s->private;
2125 if (list_empty(&pcie->ports))
2128 seq_printf(s, "Index Status\n");
2130 return seq_list_start(&pcie->ports, *pos);
2133 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2135 struct tegra_pcie *pcie = s->private;
2137 return seq_list_next(v, &pcie->ports, pos);
2140 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2144 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2146 bool up = false, active = false;
2147 struct tegra_pcie_port *port;
2150 port = list_entry(v, struct tegra_pcie_port, list);
2152 value = readl(port->base + RP_VEND_XP);
2154 if (value & RP_VEND_XP_DL_UP)
2157 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2159 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2162 seq_printf(s, "%2u ", port->index);
2165 seq_printf(s, "up");
2169 seq_printf(s, ", ");
2171 seq_printf(s, "active");
2174 seq_printf(s, "\n");
2178 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2179 .start = tegra_pcie_ports_seq_start,
2180 .next = tegra_pcie_ports_seq_next,
2181 .stop = tegra_pcie_ports_seq_stop,
2182 .show = tegra_pcie_ports_seq_show,
2185 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2187 struct tegra_pcie *pcie = inode->i_private;
2191 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2195 s = file->private_data;
2201 static const struct file_operations tegra_pcie_ports_ops = {
2202 .owner = THIS_MODULE,
2203 .open = tegra_pcie_ports_open,
2205 .llseek = seq_lseek,
2206 .release = seq_release,
2209 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2211 struct dentry *file;
2213 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2217 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2218 pcie, &tegra_pcie_ports_ops);
2225 debugfs_remove_recursive(pcie->debugfs);
2226 pcie->debugfs = NULL;
2230 static int tegra_pcie_probe(struct platform_device *pdev)
2232 const struct of_device_id *match;
2233 struct tegra_pcie *pcie;
2236 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2240 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2244 INIT_LIST_HEAD(&pcie->buses);
2245 INIT_LIST_HEAD(&pcie->ports);
2246 pcie->soc_data = match->data;
2247 pcie->dev = &pdev->dev;
2249 err = tegra_pcie_parse_dt(pcie);
2253 pcibios_min_mem = 0;
2255 err = tegra_pcie_get_resources(pcie);
2257 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2261 err = tegra_pcie_enable_controller(pcie);
2265 /* setup the AFI address translations */
2266 tegra_pcie_setup_translations(pcie);
2268 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2269 err = tegra_pcie_enable_msi(pcie);
2272 "failed to enable MSI support: %d\n",
2278 err = tegra_pcie_enable(pcie);
2280 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2284 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2285 err = tegra_pcie_debugfs_init(pcie);
2287 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2291 platform_set_drvdata(pdev, pcie);
2295 if (IS_ENABLED(CONFIG_PCI_MSI))
2296 tegra_pcie_disable_msi(pcie);
2298 tegra_pcie_put_resources(pcie);
2302 static struct platform_driver tegra_pcie_driver = {
2304 .name = "tegra-pcie",
2305 .of_match_table = tegra_pcie_of_match,
2306 .suppress_bind_attrs = true,
2308 .probe = tegra_pcie_probe,
2310 builtin_platform_driver(tegra_pcie_driver);