1 // SPDX-License-Identifier: GPL-2.0+
3 * PCIe host controller driver for Tegra SoCs
5 * Copyright (c) 2010, CompuLab, Ltd.
6 * Author: Mike Rapoport <mike@compulab.co.il>
8 * Based on NVIDIA PCIe driver
9 * Copyright (c) 2008-2009, NVIDIA Corporation.
11 * Bits taken from arch/arm/mach-dove/pcie.c
13 * Author: Thierry Reding <treding@nvidia.com>
16 #include <linux/clk.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/export.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/irqdomain.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/msi.h>
26 #include <linux/of_address.h>
27 #include <linux/of_pci.h>
28 #include <linux/of_platform.h>
29 #include <linux/pci.h>
30 #include <linux/phy/phy.h>
31 #include <linux/platform_device.h>
32 #include <linux/reset.h>
33 #include <linux/sizes.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <linux/regulator/consumer.h>
38 #include <soc/tegra/cpuidle.h>
39 #include <soc/tegra/pmc.h>
41 #define INT_PCI_MSI_NR (8 * 32)
43 /* register definitions */
45 #define AFI_AXI_BAR0_SZ 0x00
46 #define AFI_AXI_BAR1_SZ 0x04
47 #define AFI_AXI_BAR2_SZ 0x08
48 #define AFI_AXI_BAR3_SZ 0x0c
49 #define AFI_AXI_BAR4_SZ 0x10
50 #define AFI_AXI_BAR5_SZ 0x14
52 #define AFI_AXI_BAR0_START 0x18
53 #define AFI_AXI_BAR1_START 0x1c
54 #define AFI_AXI_BAR2_START 0x20
55 #define AFI_AXI_BAR3_START 0x24
56 #define AFI_AXI_BAR4_START 0x28
57 #define AFI_AXI_BAR5_START 0x2c
59 #define AFI_FPCI_BAR0 0x30
60 #define AFI_FPCI_BAR1 0x34
61 #define AFI_FPCI_BAR2 0x38
62 #define AFI_FPCI_BAR3 0x3c
63 #define AFI_FPCI_BAR4 0x40
64 #define AFI_FPCI_BAR5 0x44
66 #define AFI_CACHE_BAR0_SZ 0x48
67 #define AFI_CACHE_BAR0_ST 0x4c
68 #define AFI_CACHE_BAR1_SZ 0x50
69 #define AFI_CACHE_BAR1_ST 0x54
71 #define AFI_MSI_BAR_SZ 0x60
72 #define AFI_MSI_FPCI_BAR_ST 0x64
73 #define AFI_MSI_AXI_BAR_ST 0x68
75 #define AFI_MSI_VEC0 0x6c
76 #define AFI_MSI_VEC1 0x70
77 #define AFI_MSI_VEC2 0x74
78 #define AFI_MSI_VEC3 0x78
79 #define AFI_MSI_VEC4 0x7c
80 #define AFI_MSI_VEC5 0x80
81 #define AFI_MSI_VEC6 0x84
82 #define AFI_MSI_VEC7 0x88
84 #define AFI_MSI_EN_VEC0 0x8c
85 #define AFI_MSI_EN_VEC1 0x90
86 #define AFI_MSI_EN_VEC2 0x94
87 #define AFI_MSI_EN_VEC3 0x98
88 #define AFI_MSI_EN_VEC4 0x9c
89 #define AFI_MSI_EN_VEC5 0xa0
90 #define AFI_MSI_EN_VEC6 0xa4
91 #define AFI_MSI_EN_VEC7 0xa8
93 #define AFI_CONFIGURATION 0xac
94 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
96 #define AFI_FPCI_ERROR_MASKS 0xb0
98 #define AFI_INTR_MASK 0xb4
99 #define AFI_INTR_MASK_INT_MASK (1 << 0)
100 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
102 #define AFI_INTR_CODE 0xb8
103 #define AFI_INTR_CODE_MASK 0xf
104 #define AFI_INTR_INI_SLAVE_ERROR 1
105 #define AFI_INTR_INI_DECODE_ERROR 2
106 #define AFI_INTR_TARGET_ABORT 3
107 #define AFI_INTR_MASTER_ABORT 4
108 #define AFI_INTR_INVALID_WRITE 5
109 #define AFI_INTR_LEGACY 6
110 #define AFI_INTR_FPCI_DECODE_ERROR 7
111 #define AFI_INTR_AXI_DECODE_ERROR 8
112 #define AFI_INTR_FPCI_TIMEOUT 9
113 #define AFI_INTR_PE_PRSNT_SENSE 10
114 #define AFI_INTR_PE_CLKREQ_SENSE 11
115 #define AFI_INTR_CLKCLAMP_SENSE 12
116 #define AFI_INTR_RDY4PD_SENSE 13
117 #define AFI_INTR_P2P_ERROR 14
119 #define AFI_INTR_SIGNATURE 0xbc
120 #define AFI_UPPER_FPCI_ADDRESS 0xc0
121 #define AFI_SM_INTR_ENABLE 0xc4
122 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
123 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
124 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
125 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
126 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
127 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
128 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
129 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
131 #define AFI_AFI_INTR_ENABLE 0xc8
132 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
133 #define AFI_INTR_EN_INI_DECERR (1 << 1)
134 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
135 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
136 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
137 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
138 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
139 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
140 #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
142 #define AFI_PCIE_CONFIG 0x0f8
143 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
144 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
145 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
146 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
147 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
148 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
149 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401 (0x0 << 20)
150 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
151 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
152 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
153 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211 (0x1 << 20)
154 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
155 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111 (0x2 << 20)
157 #define AFI_FUSE 0x104
158 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
160 #define AFI_PEX0_CTRL 0x110
161 #define AFI_PEX1_CTRL 0x118
162 #define AFI_PEX2_CTRL 0x128
163 #define AFI_PEX_CTRL_RST (1 << 0)
164 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
165 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
166 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
168 #define AFI_PLLE_CONTROL 0x160
169 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
170 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
172 #define AFI_PEXBIAS_CTRL_0 0x168
174 #define RP_VEND_XP 0x00000f00
175 #define RP_VEND_XP_DL_UP (1 << 30)
177 #define RP_VEND_CTL2 0x00000fa8
178 #define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
180 #define RP_PRIV_MISC 0x00000fe0
181 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
182 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
184 #define RP_LINK_CONTROL_STATUS 0x00000090
185 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
186 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
188 #define PADS_CTL_SEL 0x0000009c
190 #define PADS_CTL 0x000000a0
191 #define PADS_CTL_IDDQ_1L (1 << 0)
192 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
193 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
195 #define PADS_PLL_CTL_TEGRA20 0x000000b8
196 #define PADS_PLL_CTL_TEGRA30 0x000000b4
197 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
198 #define PADS_PLL_CTL_LOCKDET (1 << 8)
199 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
200 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
201 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
202 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
203 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
204 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
205 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
206 #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
208 #define PADS_REFCLK_CFG0 0x000000c8
209 #define PADS_REFCLK_CFG1 0x000000cc
210 #define PADS_REFCLK_BIAS 0x000000d0
213 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
214 * entries, one entry per PCIe port. These field definitions and desired
215 * values aren't in the TRM, but do come from NVIDIA.
217 #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
218 #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
219 #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
220 #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
223 struct msi_controller chip
;
224 DECLARE_BITMAP(used
, INT_PCI_MSI_NR
);
225 struct irq_domain
*domain
;
232 /* used to differentiate between Tegra SoC generations */
233 struct tegra_pcie_soc
{
234 unsigned int num_ports
;
235 unsigned int msi_base_shift
;
238 u32 pads_refclk_cfg0
;
239 u32 pads_refclk_cfg1
;
240 bool has_pex_clkreq_en
;
241 bool has_pex_bias_ctrl
;
242 bool has_intr_prsnt_sense
;
245 bool force_pca_enable
;
249 static inline struct tegra_msi
*to_tegra_msi(struct msi_controller
*chip
)
251 return container_of(chip
, struct tegra_msi
, chip
);
266 struct resource prefetch
;
267 struct resource busn
;
279 struct reset_control
*pex_rst
;
280 struct reset_control
*afi_rst
;
281 struct reset_control
*pcie_xrst
;
286 struct tegra_msi msi
;
288 struct list_head ports
;
291 struct regulator_bulk_data
*supplies
;
292 unsigned int num_supplies
;
294 const struct tegra_pcie_soc
*soc
;
295 struct dentry
*debugfs
;
298 struct tegra_pcie_port
{
299 struct tegra_pcie
*pcie
;
300 struct device_node
*np
;
301 struct list_head list
;
302 struct resource regs
;
310 struct tegra_pcie_bus
{
311 struct list_head list
;
315 static inline void afi_writel(struct tegra_pcie
*pcie
, u32 value
,
316 unsigned long offset
)
318 writel(value
, pcie
->afi
+ offset
);
321 static inline u32
afi_readl(struct tegra_pcie
*pcie
, unsigned long offset
)
323 return readl(pcie
->afi
+ offset
);
326 static inline void pads_writel(struct tegra_pcie
*pcie
, u32 value
,
327 unsigned long offset
)
329 writel(value
, pcie
->pads
+ offset
);
332 static inline u32
pads_readl(struct tegra_pcie
*pcie
, unsigned long offset
)
334 return readl(pcie
->pads
+ offset
);
338 * The configuration space mapping on Tegra is somewhat similar to the ECAM
339 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
340 * register accesses are mapped:
342 * [27:24] extended register number
344 * [15:11] device number
345 * [10: 8] function number
346 * [ 7: 0] register number
348 * Mapping the whole extended configuration space would require 256 MiB of
349 * virtual address space, only a small part of which will actually be used.
351 * To work around this, a 4 KiB region is used to generate the required
352 * configuration transaction with relevant B:D:F and register offset values.
353 * This is achieved by dynamically programming base address and size of
354 * AFI_AXI_BAR used for end point config space mapping to make sure that the
355 * address (access to which generates correct config transaction) falls in
358 static unsigned int tegra_pcie_conf_offset(u8 bus
, unsigned int devfn
,
361 return ((where
& 0xf00) << 16) | (bus
<< 16) | (PCI_SLOT(devfn
) << 11) |
362 (PCI_FUNC(devfn
) << 8) | (where
& 0xff);
365 static void __iomem
*tegra_pcie_map_bus(struct pci_bus
*bus
,
369 struct tegra_pcie
*pcie
= bus
->sysdata
;
370 void __iomem
*addr
= NULL
;
372 if (bus
->number
== 0) {
373 unsigned int slot
= PCI_SLOT(devfn
);
374 struct tegra_pcie_port
*port
;
376 list_for_each_entry(port
, &pcie
->ports
, list
) {
377 if (port
->index
+ 1 == slot
) {
378 addr
= port
->base
+ (where
& ~3);
386 offset
= tegra_pcie_conf_offset(bus
->number
, devfn
, where
);
388 /* move 4 KiB window to offset within the FPCI region */
389 base
= 0xfe100000 + ((offset
& ~(SZ_4K
- 1)) >> 8);
390 afi_writel(pcie
, base
, AFI_FPCI_BAR0
);
392 /* move to correct offset within the 4 KiB page */
393 addr
= pcie
->cfg
+ (offset
& (SZ_4K
- 1));
399 static int tegra_pcie_config_read(struct pci_bus
*bus
, unsigned int devfn
,
400 int where
, int size
, u32
*value
)
402 if (bus
->number
== 0)
403 return pci_generic_config_read32(bus
, devfn
, where
, size
,
406 return pci_generic_config_read(bus
, devfn
, where
, size
, value
);
409 static int tegra_pcie_config_write(struct pci_bus
*bus
, unsigned int devfn
,
410 int where
, int size
, u32 value
)
412 if (bus
->number
== 0)
413 return pci_generic_config_write32(bus
, devfn
, where
, size
,
416 return pci_generic_config_write(bus
, devfn
, where
, size
, value
);
419 static struct pci_ops tegra_pcie_ops
= {
420 .map_bus
= tegra_pcie_map_bus
,
421 .read
= tegra_pcie_config_read
,
422 .write
= tegra_pcie_config_write
,
425 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port
*port
)
427 unsigned long ret
= 0;
429 switch (port
->index
) {
446 static void tegra_pcie_port_reset(struct tegra_pcie_port
*port
)
448 unsigned long ctrl
= tegra_pcie_port_get_pex_ctrl(port
);
451 /* pulse reset signal */
452 value
= afi_readl(port
->pcie
, ctrl
);
453 value
&= ~AFI_PEX_CTRL_RST
;
454 afi_writel(port
->pcie
, value
, ctrl
);
456 usleep_range(1000, 2000);
458 value
= afi_readl(port
->pcie
, ctrl
);
459 value
|= AFI_PEX_CTRL_RST
;
460 afi_writel(port
->pcie
, value
, ctrl
);
463 static void tegra_pcie_port_enable(struct tegra_pcie_port
*port
)
465 unsigned long ctrl
= tegra_pcie_port_get_pex_ctrl(port
);
466 const struct tegra_pcie_soc
*soc
= port
->pcie
->soc
;
469 /* enable reference clock */
470 value
= afi_readl(port
->pcie
, ctrl
);
471 value
|= AFI_PEX_CTRL_REFCLK_EN
;
473 if (soc
->has_pex_clkreq_en
)
474 value
|= AFI_PEX_CTRL_CLKREQ_EN
;
476 value
|= AFI_PEX_CTRL_OVERRIDE_EN
;
478 afi_writel(port
->pcie
, value
, ctrl
);
480 tegra_pcie_port_reset(port
);
482 if (soc
->force_pca_enable
) {
483 value
= readl(port
->base
+ RP_VEND_CTL2
);
484 value
|= RP_VEND_CTL2_PCA_ENABLE
;
485 writel(value
, port
->base
+ RP_VEND_CTL2
);
489 static void tegra_pcie_port_disable(struct tegra_pcie_port
*port
)
491 unsigned long ctrl
= tegra_pcie_port_get_pex_ctrl(port
);
492 const struct tegra_pcie_soc
*soc
= port
->pcie
->soc
;
495 /* assert port reset */
496 value
= afi_readl(port
->pcie
, ctrl
);
497 value
&= ~AFI_PEX_CTRL_RST
;
498 afi_writel(port
->pcie
, value
, ctrl
);
500 /* disable reference clock */
501 value
= afi_readl(port
->pcie
, ctrl
);
503 if (soc
->has_pex_clkreq_en
)
504 value
&= ~AFI_PEX_CTRL_CLKREQ_EN
;
506 value
&= ~AFI_PEX_CTRL_REFCLK_EN
;
507 afi_writel(port
->pcie
, value
, ctrl
);
510 static void tegra_pcie_port_free(struct tegra_pcie_port
*port
)
512 struct tegra_pcie
*pcie
= port
->pcie
;
513 struct device
*dev
= pcie
->dev
;
515 devm_iounmap(dev
, port
->base
);
516 devm_release_mem_region(dev
, port
->regs
.start
,
517 resource_size(&port
->regs
));
518 list_del(&port
->list
);
519 devm_kfree(dev
, port
);
522 /* Tegra PCIE root complex wrongly reports device class */
523 static void tegra_pcie_fixup_class(struct pci_dev
*dev
)
525 dev
->class = PCI_CLASS_BRIDGE_PCI
<< 8;
527 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA
, 0x0bf0, tegra_pcie_fixup_class
);
528 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA
, 0x0bf1, tegra_pcie_fixup_class
);
529 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA
, 0x0e1c, tegra_pcie_fixup_class
);
530 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA
, 0x0e1d, tegra_pcie_fixup_class
);
532 /* Tegra PCIE requires relaxed ordering */
533 static void tegra_pcie_relax_enable(struct pci_dev
*dev
)
535 pcie_capability_set_word(dev
, PCI_EXP_DEVCTL
, PCI_EXP_DEVCTL_RELAX_EN
);
537 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID
, PCI_ANY_ID
, tegra_pcie_relax_enable
);
539 static int tegra_pcie_request_resources(struct tegra_pcie
*pcie
)
541 struct pci_host_bridge
*host
= pci_host_bridge_from_priv(pcie
);
542 struct list_head
*windows
= &host
->windows
;
543 struct device
*dev
= pcie
->dev
;
546 pci_add_resource_offset(windows
, &pcie
->pio
, pcie
->offset
.io
);
547 pci_add_resource_offset(windows
, &pcie
->mem
, pcie
->offset
.mem
);
548 pci_add_resource_offset(windows
, &pcie
->prefetch
, pcie
->offset
.mem
);
549 pci_add_resource(windows
, &pcie
->busn
);
551 err
= devm_request_pci_bus_resources(dev
, windows
);
555 pci_remap_iospace(&pcie
->pio
, pcie
->io
.start
);
560 static int tegra_pcie_map_irq(const struct pci_dev
*pdev
, u8 slot
, u8 pin
)
562 struct tegra_pcie
*pcie
= pdev
->bus
->sysdata
;
565 tegra_cpuidle_pcie_irqs_in_use();
567 irq
= of_irq_parse_and_map_pci(pdev
, slot
, pin
);
574 static irqreturn_t
tegra_pcie_isr(int irq
, void *arg
)
576 const char *err_msg
[] = {
584 "Response decoding error",
585 "AXI response decoding error",
586 "Transaction timeout",
587 "Slot present pin change",
588 "Slot clock request change",
589 "TMS clock ramp change",
590 "TMS ready for power down",
593 struct tegra_pcie
*pcie
= arg
;
594 struct device
*dev
= pcie
->dev
;
597 code
= afi_readl(pcie
, AFI_INTR_CODE
) & AFI_INTR_CODE_MASK
;
598 signature
= afi_readl(pcie
, AFI_INTR_SIGNATURE
);
599 afi_writel(pcie
, 0, AFI_INTR_CODE
);
601 if (code
== AFI_INTR_LEGACY
)
604 if (code
>= ARRAY_SIZE(err_msg
))
608 * do not pollute kernel log with master abort reports since they
609 * happen a lot during enumeration
611 if (code
== AFI_INTR_MASTER_ABORT
)
612 dev_dbg(dev
, "%s, signature: %08x\n", err_msg
[code
], signature
);
614 dev_err(dev
, "%s, signature: %08x\n", err_msg
[code
], signature
);
616 if (code
== AFI_INTR_TARGET_ABORT
|| code
== AFI_INTR_MASTER_ABORT
||
617 code
== AFI_INTR_FPCI_DECODE_ERROR
) {
618 u32 fpci
= afi_readl(pcie
, AFI_UPPER_FPCI_ADDRESS
) & 0xff;
619 u64 address
= (u64
)fpci
<< 32 | (signature
& 0xfffffffc);
621 if (code
== AFI_INTR_MASTER_ABORT
)
622 dev_dbg(dev
, " FPCI address: %10llx\n", address
);
624 dev_err(dev
, " FPCI address: %10llx\n", address
);
631 * FPCI map is as follows:
632 * - 0xfdfc000000: I/O space
633 * - 0xfdfe000000: type 0 configuration space
634 * - 0xfdff000000: type 1 configuration space
635 * - 0xfe00000000: type 0 extended configuration space
636 * - 0xfe10000000: type 1 extended configuration space
638 static void tegra_pcie_setup_translations(struct tegra_pcie
*pcie
)
640 u32 fpci_bar
, size
, axi_address
;
642 /* Bar 0: type 1 extended configuration space */
643 size
= resource_size(&pcie
->cs
);
644 afi_writel(pcie
, pcie
->cs
.start
, AFI_AXI_BAR0_START
);
645 afi_writel(pcie
, size
>> 12, AFI_AXI_BAR0_SZ
);
647 /* Bar 1: downstream IO bar */
648 fpci_bar
= 0xfdfc0000;
649 size
= resource_size(&pcie
->io
);
650 axi_address
= pcie
->io
.start
;
651 afi_writel(pcie
, axi_address
, AFI_AXI_BAR1_START
);
652 afi_writel(pcie
, size
>> 12, AFI_AXI_BAR1_SZ
);
653 afi_writel(pcie
, fpci_bar
, AFI_FPCI_BAR1
);
655 /* Bar 2: prefetchable memory BAR */
656 fpci_bar
= (((pcie
->prefetch
.start
>> 12) & 0x0fffffff) << 4) | 0x1;
657 size
= resource_size(&pcie
->prefetch
);
658 axi_address
= pcie
->prefetch
.start
;
659 afi_writel(pcie
, axi_address
, AFI_AXI_BAR2_START
);
660 afi_writel(pcie
, size
>> 12, AFI_AXI_BAR2_SZ
);
661 afi_writel(pcie
, fpci_bar
, AFI_FPCI_BAR2
);
663 /* Bar 3: non prefetchable memory BAR */
664 fpci_bar
= (((pcie
->mem
.start
>> 12) & 0x0fffffff) << 4) | 0x1;
665 size
= resource_size(&pcie
->mem
);
666 axi_address
= pcie
->mem
.start
;
667 afi_writel(pcie
, axi_address
, AFI_AXI_BAR3_START
);
668 afi_writel(pcie
, size
>> 12, AFI_AXI_BAR3_SZ
);
669 afi_writel(pcie
, fpci_bar
, AFI_FPCI_BAR3
);
671 /* NULL out the remaining BARs as they are not used */
672 afi_writel(pcie
, 0, AFI_AXI_BAR4_START
);
673 afi_writel(pcie
, 0, AFI_AXI_BAR4_SZ
);
674 afi_writel(pcie
, 0, AFI_FPCI_BAR4
);
676 afi_writel(pcie
, 0, AFI_AXI_BAR5_START
);
677 afi_writel(pcie
, 0, AFI_AXI_BAR5_SZ
);
678 afi_writel(pcie
, 0, AFI_FPCI_BAR5
);
680 /* map all upstream transactions as uncached */
681 afi_writel(pcie
, 0, AFI_CACHE_BAR0_ST
);
682 afi_writel(pcie
, 0, AFI_CACHE_BAR0_SZ
);
683 afi_writel(pcie
, 0, AFI_CACHE_BAR1_ST
);
684 afi_writel(pcie
, 0, AFI_CACHE_BAR1_SZ
);
686 /* MSI translations are setup only when needed */
687 afi_writel(pcie
, 0, AFI_MSI_FPCI_BAR_ST
);
688 afi_writel(pcie
, 0, AFI_MSI_BAR_SZ
);
689 afi_writel(pcie
, 0, AFI_MSI_AXI_BAR_ST
);
690 afi_writel(pcie
, 0, AFI_MSI_BAR_SZ
);
693 static int tegra_pcie_pll_wait(struct tegra_pcie
*pcie
, unsigned long timeout
)
695 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
698 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
700 while (time_before(jiffies
, timeout
)) {
701 value
= pads_readl(pcie
, soc
->pads_pll_ctl
);
702 if (value
& PADS_PLL_CTL_LOCKDET
)
709 static int tegra_pcie_phy_enable(struct tegra_pcie
*pcie
)
711 struct device
*dev
= pcie
->dev
;
712 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
716 /* initialize internal PHY, enable up to 16 PCIE lanes */
717 pads_writel(pcie
, 0x0, PADS_CTL_SEL
);
719 /* override IDDQ to 1 on all 4 lanes */
720 value
= pads_readl(pcie
, PADS_CTL
);
721 value
|= PADS_CTL_IDDQ_1L
;
722 pads_writel(pcie
, value
, PADS_CTL
);
725 * Set up PHY PLL inputs select PLLE output as refclock,
726 * set TX ref sel to div10 (not div5).
728 value
= pads_readl(pcie
, soc
->pads_pll_ctl
);
729 value
&= ~(PADS_PLL_CTL_REFCLK_MASK
| PADS_PLL_CTL_TXCLKREF_MASK
);
730 value
|= PADS_PLL_CTL_REFCLK_INTERNAL_CML
| soc
->tx_ref_sel
;
731 pads_writel(pcie
, value
, soc
->pads_pll_ctl
);
734 value
= pads_readl(pcie
, soc
->pads_pll_ctl
);
735 value
&= ~PADS_PLL_CTL_RST_B4SM
;
736 pads_writel(pcie
, value
, soc
->pads_pll_ctl
);
738 usleep_range(20, 100);
740 /* take PLL out of reset */
741 value
= pads_readl(pcie
, soc
->pads_pll_ctl
);
742 value
|= PADS_PLL_CTL_RST_B4SM
;
743 pads_writel(pcie
, value
, soc
->pads_pll_ctl
);
745 /* wait for the PLL to lock */
746 err
= tegra_pcie_pll_wait(pcie
, 500);
748 dev_err(dev
, "PLL failed to lock: %d\n", err
);
752 /* turn off IDDQ override */
753 value
= pads_readl(pcie
, PADS_CTL
);
754 value
&= ~PADS_CTL_IDDQ_1L
;
755 pads_writel(pcie
, value
, PADS_CTL
);
757 /* enable TX/RX data */
758 value
= pads_readl(pcie
, PADS_CTL
);
759 value
|= PADS_CTL_TX_DATA_EN_1L
| PADS_CTL_RX_DATA_EN_1L
;
760 pads_writel(pcie
, value
, PADS_CTL
);
765 static int tegra_pcie_phy_disable(struct tegra_pcie
*pcie
)
767 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
770 /* disable TX/RX data */
771 value
= pads_readl(pcie
, PADS_CTL
);
772 value
&= ~(PADS_CTL_TX_DATA_EN_1L
| PADS_CTL_RX_DATA_EN_1L
);
773 pads_writel(pcie
, value
, PADS_CTL
);
776 value
= pads_readl(pcie
, PADS_CTL
);
777 value
|= PADS_CTL_IDDQ_1L
;
778 pads_writel(pcie
, value
, PADS_CTL
);
781 value
= pads_readl(pcie
, soc
->pads_pll_ctl
);
782 value
&= ~PADS_PLL_CTL_RST_B4SM
;
783 pads_writel(pcie
, value
, soc
->pads_pll_ctl
);
785 usleep_range(20, 100);
790 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port
*port
)
792 struct device
*dev
= port
->pcie
->dev
;
796 for (i
= 0; i
< port
->lanes
; i
++) {
797 err
= phy_power_on(port
->phys
[i
]);
799 dev_err(dev
, "failed to power on PHY#%u: %d\n", i
, err
);
807 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port
*port
)
809 struct device
*dev
= port
->pcie
->dev
;
813 for (i
= 0; i
< port
->lanes
; i
++) {
814 err
= phy_power_off(port
->phys
[i
]);
816 dev_err(dev
, "failed to power off PHY#%u: %d\n", i
,
825 static int tegra_pcie_phy_power_on(struct tegra_pcie
*pcie
)
827 struct device
*dev
= pcie
->dev
;
828 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
829 struct tegra_pcie_port
*port
;
832 if (pcie
->legacy_phy
) {
834 err
= phy_power_on(pcie
->phy
);
836 err
= tegra_pcie_phy_enable(pcie
);
839 dev_err(dev
, "failed to power on PHY: %d\n", err
);
844 list_for_each_entry(port
, &pcie
->ports
, list
) {
845 err
= tegra_pcie_port_phy_power_on(port
);
848 "failed to power on PCIe port %u PHY: %d\n",
854 /* Configure the reference clock driver */
855 pads_writel(pcie
, soc
->pads_refclk_cfg0
, PADS_REFCLK_CFG0
);
857 if (soc
->num_ports
> 2)
858 pads_writel(pcie
, soc
->pads_refclk_cfg1
, PADS_REFCLK_CFG1
);
863 static int tegra_pcie_phy_power_off(struct tegra_pcie
*pcie
)
865 struct device
*dev
= pcie
->dev
;
866 struct tegra_pcie_port
*port
;
869 if (pcie
->legacy_phy
) {
871 err
= phy_power_off(pcie
->phy
);
873 err
= tegra_pcie_phy_disable(pcie
);
876 dev_err(dev
, "failed to power off PHY: %d\n", err
);
881 list_for_each_entry(port
, &pcie
->ports
, list
) {
882 err
= tegra_pcie_port_phy_power_off(port
);
885 "failed to power off PCIe port %u PHY: %d\n",
894 static int tegra_pcie_enable_controller(struct tegra_pcie
*pcie
)
896 struct device
*dev
= pcie
->dev
;
897 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
898 struct tegra_pcie_port
*port
;
902 /* enable PLL power down */
904 value
= afi_readl(pcie
, AFI_PLLE_CONTROL
);
905 value
&= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL
;
906 value
|= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN
;
907 afi_writel(pcie
, value
, AFI_PLLE_CONTROL
);
910 /* power down PCIe slot clock bias pad */
911 if (soc
->has_pex_bias_ctrl
)
912 afi_writel(pcie
, 0, AFI_PEXBIAS_CTRL_0
);
914 /* configure mode and disable all ports */
915 value
= afi_readl(pcie
, AFI_PCIE_CONFIG
);
916 value
&= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK
;
917 value
|= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL
| pcie
->xbar_config
;
919 list_for_each_entry(port
, &pcie
->ports
, list
)
920 value
&= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port
->index
);
922 afi_writel(pcie
, value
, AFI_PCIE_CONFIG
);
925 value
= afi_readl(pcie
, AFI_FUSE
);
926 value
&= ~AFI_FUSE_PCIE_T0_GEN2_DIS
;
927 afi_writel(pcie
, value
, AFI_FUSE
);
929 value
= afi_readl(pcie
, AFI_FUSE
);
930 value
|= AFI_FUSE_PCIE_T0_GEN2_DIS
;
931 afi_writel(pcie
, value
, AFI_FUSE
);
934 if (soc
->program_uphy
) {
935 err
= tegra_pcie_phy_power_on(pcie
);
937 dev_err(dev
, "failed to power on PHY(s): %d\n", err
);
942 /* take the PCIe interface module out of reset */
943 reset_control_deassert(pcie
->pcie_xrst
);
945 /* finally enable PCIe */
946 value
= afi_readl(pcie
, AFI_CONFIGURATION
);
947 value
|= AFI_CONFIGURATION_EN_FPCI
;
948 afi_writel(pcie
, value
, AFI_CONFIGURATION
);
950 value
= AFI_INTR_EN_INI_SLVERR
| AFI_INTR_EN_INI_DECERR
|
951 AFI_INTR_EN_TGT_SLVERR
| AFI_INTR_EN_TGT_DECERR
|
952 AFI_INTR_EN_TGT_WRERR
| AFI_INTR_EN_DFPCI_DECERR
;
954 if (soc
->has_intr_prsnt_sense
)
955 value
|= AFI_INTR_EN_PRSNT_SENSE
;
957 afi_writel(pcie
, value
, AFI_AFI_INTR_ENABLE
);
958 afi_writel(pcie
, 0xffffffff, AFI_SM_INTR_ENABLE
);
960 /* don't enable MSI for now, only when needed */
961 afi_writel(pcie
, AFI_INTR_MASK_INT_MASK
, AFI_INTR_MASK
);
963 /* disable all exceptions */
964 afi_writel(pcie
, 0, AFI_FPCI_ERROR_MASKS
);
969 static void tegra_pcie_power_off(struct tegra_pcie
*pcie
)
971 struct device
*dev
= pcie
->dev
;
972 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
975 /* TODO: disable and unprepare clocks? */
977 if (soc
->program_uphy
) {
978 err
= tegra_pcie_phy_power_off(pcie
);
980 dev_err(dev
, "failed to power off PHY(s): %d\n", err
);
983 reset_control_assert(pcie
->pcie_xrst
);
984 reset_control_assert(pcie
->afi_rst
);
985 reset_control_assert(pcie
->pex_rst
);
988 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE
);
990 err
= regulator_bulk_disable(pcie
->num_supplies
, pcie
->supplies
);
992 dev_warn(dev
, "failed to disable regulators: %d\n", err
);
995 static int tegra_pcie_power_on(struct tegra_pcie
*pcie
)
997 struct device
*dev
= pcie
->dev
;
998 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1001 reset_control_assert(pcie
->pcie_xrst
);
1002 reset_control_assert(pcie
->afi_rst
);
1003 reset_control_assert(pcie
->pex_rst
);
1005 if (!dev
->pm_domain
)
1006 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE
);
1008 /* enable regulators */
1009 err
= regulator_bulk_enable(pcie
->num_supplies
, pcie
->supplies
);
1011 dev_err(dev
, "failed to enable regulators: %d\n", err
);
1013 if (dev
->pm_domain
) {
1014 err
= clk_prepare_enable(pcie
->pex_clk
);
1016 dev_err(dev
, "failed to enable PEX clock: %d\n", err
);
1019 reset_control_deassert(pcie
->pex_rst
);
1021 err
= tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE
,
1025 dev_err(dev
, "powerup sequence failed: %d\n", err
);
1030 reset_control_deassert(pcie
->afi_rst
);
1032 err
= clk_prepare_enable(pcie
->afi_clk
);
1034 dev_err(dev
, "failed to enable AFI clock: %d\n", err
);
1038 if (soc
->has_cml_clk
) {
1039 err
= clk_prepare_enable(pcie
->cml_clk
);
1041 dev_err(dev
, "failed to enable CML clock: %d\n", err
);
1046 err
= clk_prepare_enable(pcie
->pll_e
);
1048 dev_err(dev
, "failed to enable PLLE clock: %d\n", err
);
1055 static int tegra_pcie_clocks_get(struct tegra_pcie
*pcie
)
1057 struct device
*dev
= pcie
->dev
;
1058 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1060 pcie
->pex_clk
= devm_clk_get(dev
, "pex");
1061 if (IS_ERR(pcie
->pex_clk
))
1062 return PTR_ERR(pcie
->pex_clk
);
1064 pcie
->afi_clk
= devm_clk_get(dev
, "afi");
1065 if (IS_ERR(pcie
->afi_clk
))
1066 return PTR_ERR(pcie
->afi_clk
);
1068 pcie
->pll_e
= devm_clk_get(dev
, "pll_e");
1069 if (IS_ERR(pcie
->pll_e
))
1070 return PTR_ERR(pcie
->pll_e
);
1072 if (soc
->has_cml_clk
) {
1073 pcie
->cml_clk
= devm_clk_get(dev
, "cml");
1074 if (IS_ERR(pcie
->cml_clk
))
1075 return PTR_ERR(pcie
->cml_clk
);
1081 static int tegra_pcie_resets_get(struct tegra_pcie
*pcie
)
1083 struct device
*dev
= pcie
->dev
;
1085 pcie
->pex_rst
= devm_reset_control_get_exclusive(dev
, "pex");
1086 if (IS_ERR(pcie
->pex_rst
))
1087 return PTR_ERR(pcie
->pex_rst
);
1089 pcie
->afi_rst
= devm_reset_control_get_exclusive(dev
, "afi");
1090 if (IS_ERR(pcie
->afi_rst
))
1091 return PTR_ERR(pcie
->afi_rst
);
1093 pcie
->pcie_xrst
= devm_reset_control_get_exclusive(dev
, "pcie_x");
1094 if (IS_ERR(pcie
->pcie_xrst
))
1095 return PTR_ERR(pcie
->pcie_xrst
);
1100 static int tegra_pcie_phys_get_legacy(struct tegra_pcie
*pcie
)
1102 struct device
*dev
= pcie
->dev
;
1105 pcie
->phy
= devm_phy_optional_get(dev
, "pcie");
1106 if (IS_ERR(pcie
->phy
)) {
1107 err
= PTR_ERR(pcie
->phy
);
1108 dev_err(dev
, "failed to get PHY: %d\n", err
);
1112 err
= phy_init(pcie
->phy
);
1114 dev_err(dev
, "failed to initialize PHY: %d\n", err
);
1118 pcie
->legacy_phy
= true;
1123 static struct phy
*devm_of_phy_optional_get_index(struct device
*dev
,
1124 struct device_node
*np
,
1125 const char *consumer
,
1131 name
= kasprintf(GFP_KERNEL
, "%s-%u", consumer
, index
);
1133 return ERR_PTR(-ENOMEM
);
1135 phy
= devm_of_phy_get(dev
, np
, name
);
1138 if (IS_ERR(phy
) && PTR_ERR(phy
) == -ENODEV
)
1144 static int tegra_pcie_port_get_phys(struct tegra_pcie_port
*port
)
1146 struct device
*dev
= port
->pcie
->dev
;
1151 port
->phys
= devm_kcalloc(dev
, sizeof(phy
), port
->lanes
, GFP_KERNEL
);
1155 for (i
= 0; i
< port
->lanes
; i
++) {
1156 phy
= devm_of_phy_optional_get_index(dev
, port
->np
, "pcie", i
);
1158 dev_err(dev
, "failed to get PHY#%u: %ld\n", i
,
1160 return PTR_ERR(phy
);
1163 err
= phy_init(phy
);
1165 dev_err(dev
, "failed to initialize PHY#%u: %d\n", i
,
1170 port
->phys
[i
] = phy
;
1176 static int tegra_pcie_phys_get(struct tegra_pcie
*pcie
)
1178 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1179 struct device_node
*np
= pcie
->dev
->of_node
;
1180 struct tegra_pcie_port
*port
;
1183 if (!soc
->has_gen2
|| of_find_property(np
, "phys", NULL
) != NULL
)
1184 return tegra_pcie_phys_get_legacy(pcie
);
1186 list_for_each_entry(port
, &pcie
->ports
, list
) {
1187 err
= tegra_pcie_port_get_phys(port
);
1195 static int tegra_pcie_get_resources(struct tegra_pcie
*pcie
)
1197 struct device
*dev
= pcie
->dev
;
1198 struct platform_device
*pdev
= to_platform_device(dev
);
1199 struct resource
*pads
, *afi
, *res
;
1200 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1203 err
= tegra_pcie_clocks_get(pcie
);
1205 dev_err(dev
, "failed to get clocks: %d\n", err
);
1209 err
= tegra_pcie_resets_get(pcie
);
1211 dev_err(dev
, "failed to get resets: %d\n", err
);
1215 if (soc
->program_uphy
) {
1216 err
= tegra_pcie_phys_get(pcie
);
1218 dev_err(dev
, "failed to get PHYs: %d\n", err
);
1223 err
= tegra_pcie_power_on(pcie
);
1225 dev_err(dev
, "failed to power up: %d\n", err
);
1229 pads
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pads");
1230 pcie
->pads
= devm_ioremap_resource(dev
, pads
);
1231 if (IS_ERR(pcie
->pads
)) {
1232 err
= PTR_ERR(pcie
->pads
);
1236 afi
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "afi");
1237 pcie
->afi
= devm_ioremap_resource(dev
, afi
);
1238 if (IS_ERR(pcie
->afi
)) {
1239 err
= PTR_ERR(pcie
->afi
);
1243 /* request configuration space, but remap later, on demand */
1244 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cs");
1246 err
= -EADDRNOTAVAIL
;
1252 /* constrain configuration space to 4 KiB */
1253 pcie
->cs
.end
= pcie
->cs
.start
+ SZ_4K
- 1;
1255 pcie
->cfg
= devm_ioremap_resource(dev
, &pcie
->cs
);
1256 if (IS_ERR(pcie
->cfg
)) {
1257 err
= PTR_ERR(pcie
->cfg
);
1261 /* request interrupt */
1262 err
= platform_get_irq_byname(pdev
, "intr");
1264 dev_err(dev
, "failed to get IRQ: %d\n", err
);
1270 err
= request_irq(pcie
->irq
, tegra_pcie_isr
, IRQF_SHARED
, "PCIE", pcie
);
1272 dev_err(dev
, "failed to register IRQ: %d\n", err
);
1279 tegra_pcie_power_off(pcie
);
1283 static int tegra_pcie_put_resources(struct tegra_pcie
*pcie
)
1285 struct device
*dev
= pcie
->dev
;
1286 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1290 free_irq(pcie
->irq
, pcie
);
1292 tegra_pcie_power_off(pcie
);
1294 if (soc
->program_uphy
) {
1295 err
= phy_exit(pcie
->phy
);
1297 dev_err(dev
, "failed to teardown PHY: %d\n", err
);
1303 static int tegra_msi_alloc(struct tegra_msi
*chip
)
1307 mutex_lock(&chip
->lock
);
1309 msi
= find_first_zero_bit(chip
->used
, INT_PCI_MSI_NR
);
1310 if (msi
< INT_PCI_MSI_NR
)
1311 set_bit(msi
, chip
->used
);
1315 mutex_unlock(&chip
->lock
);
1320 static void tegra_msi_free(struct tegra_msi
*chip
, unsigned long irq
)
1322 struct device
*dev
= chip
->chip
.dev
;
1324 mutex_lock(&chip
->lock
);
1326 if (!test_bit(irq
, chip
->used
))
1327 dev_err(dev
, "trying to free unused MSI#%lu\n", irq
);
1329 clear_bit(irq
, chip
->used
);
1331 mutex_unlock(&chip
->lock
);
1334 static irqreturn_t
tegra_pcie_msi_irq(int irq
, void *data
)
1336 struct tegra_pcie
*pcie
= data
;
1337 struct device
*dev
= pcie
->dev
;
1338 struct tegra_msi
*msi
= &pcie
->msi
;
1339 unsigned int i
, processed
= 0;
1341 for (i
= 0; i
< 8; i
++) {
1342 unsigned long reg
= afi_readl(pcie
, AFI_MSI_VEC0
+ i
* 4);
1345 unsigned int offset
= find_first_bit(®
, 32);
1346 unsigned int index
= i
* 32 + offset
;
1349 /* clear the interrupt */
1350 afi_writel(pcie
, 1 << offset
, AFI_MSI_VEC0
+ i
* 4);
1352 irq
= irq_find_mapping(msi
->domain
, index
);
1354 if (test_bit(index
, msi
->used
))
1355 generic_handle_irq(irq
);
1357 dev_info(dev
, "unhandled MSI\n");
1360 * that's weird who triggered this?
1363 dev_info(dev
, "unexpected MSI\n");
1366 /* see if there's any more pending in this vector */
1367 reg
= afi_readl(pcie
, AFI_MSI_VEC0
+ i
* 4);
1373 return processed
> 0 ? IRQ_HANDLED
: IRQ_NONE
;
1376 static int tegra_msi_setup_irq(struct msi_controller
*chip
,
1377 struct pci_dev
*pdev
, struct msi_desc
*desc
)
1379 struct tegra_msi
*msi
= to_tegra_msi(chip
);
1384 hwirq
= tegra_msi_alloc(msi
);
1388 irq
= irq_create_mapping(msi
->domain
, hwirq
);
1390 tegra_msi_free(msi
, hwirq
);
1394 irq_set_msi_desc(irq
, desc
);
1396 msg
.address_lo
= lower_32_bits(msi
->phys
);
1397 msg
.address_hi
= upper_32_bits(msi
->phys
);
1400 pci_write_msi_msg(irq
, &msg
);
1405 static void tegra_msi_teardown_irq(struct msi_controller
*chip
,
1408 struct tegra_msi
*msi
= to_tegra_msi(chip
);
1409 struct irq_data
*d
= irq_get_irq_data(irq
);
1410 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
1412 irq_dispose_mapping(irq
);
1413 tegra_msi_free(msi
, hwirq
);
1416 static struct irq_chip tegra_msi_irq_chip
= {
1417 .name
= "Tegra PCIe MSI",
1418 .irq_enable
= pci_msi_unmask_irq
,
1419 .irq_disable
= pci_msi_mask_irq
,
1420 .irq_mask
= pci_msi_mask_irq
,
1421 .irq_unmask
= pci_msi_unmask_irq
,
1424 static int tegra_msi_map(struct irq_domain
*domain
, unsigned int irq
,
1425 irq_hw_number_t hwirq
)
1427 irq_set_chip_and_handler(irq
, &tegra_msi_irq_chip
, handle_simple_irq
);
1428 irq_set_chip_data(irq
, domain
->host_data
);
1430 tegra_cpuidle_pcie_irqs_in_use();
1435 static const struct irq_domain_ops msi_domain_ops
= {
1436 .map
= tegra_msi_map
,
1439 static int tegra_pcie_enable_msi(struct tegra_pcie
*pcie
)
1441 struct pci_host_bridge
*host
= pci_host_bridge_from_priv(pcie
);
1442 struct platform_device
*pdev
= to_platform_device(pcie
->dev
);
1443 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1444 struct tegra_msi
*msi
= &pcie
->msi
;
1445 struct device
*dev
= pcie
->dev
;
1449 mutex_init(&msi
->lock
);
1451 msi
->chip
.dev
= dev
;
1452 msi
->chip
.setup_irq
= tegra_msi_setup_irq
;
1453 msi
->chip
.teardown_irq
= tegra_msi_teardown_irq
;
1455 msi
->domain
= irq_domain_add_linear(dev
->of_node
, INT_PCI_MSI_NR
,
1456 &msi_domain_ops
, &msi
->chip
);
1458 dev_err(dev
, "failed to create IRQ domain\n");
1462 err
= platform_get_irq_byname(pdev
, "msi");
1464 dev_err(dev
, "failed to get IRQ: %d\n", err
);
1470 err
= request_irq(msi
->irq
, tegra_pcie_msi_irq
, IRQF_NO_THREAD
,
1471 tegra_msi_irq_chip
.name
, pcie
);
1473 dev_err(dev
, "failed to request IRQ: %d\n", err
);
1477 /* setup AFI/FPCI range */
1478 msi
->pages
= __get_free_pages(GFP_KERNEL
, 0);
1479 msi
->phys
= virt_to_phys((void *)msi
->pages
);
1481 afi_writel(pcie
, msi
->phys
>> soc
->msi_base_shift
, AFI_MSI_FPCI_BAR_ST
);
1482 afi_writel(pcie
, msi
->phys
, AFI_MSI_AXI_BAR_ST
);
1483 /* this register is in 4K increments */
1484 afi_writel(pcie
, 1, AFI_MSI_BAR_SZ
);
1486 /* enable all MSI vectors */
1487 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC0
);
1488 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC1
);
1489 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC2
);
1490 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC3
);
1491 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC4
);
1492 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC5
);
1493 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC6
);
1494 afi_writel(pcie
, 0xffffffff, AFI_MSI_EN_VEC7
);
1496 /* and unmask the MSI interrupt */
1497 reg
= afi_readl(pcie
, AFI_INTR_MASK
);
1498 reg
|= AFI_INTR_MASK_MSI_MASK
;
1499 afi_writel(pcie
, reg
, AFI_INTR_MASK
);
1501 host
->msi
= &msi
->chip
;
1506 irq_domain_remove(msi
->domain
);
1510 static int tegra_pcie_disable_msi(struct tegra_pcie
*pcie
)
1512 struct tegra_msi
*msi
= &pcie
->msi
;
1513 unsigned int i
, irq
;
1516 /* mask the MSI interrupt */
1517 value
= afi_readl(pcie
, AFI_INTR_MASK
);
1518 value
&= ~AFI_INTR_MASK_MSI_MASK
;
1519 afi_writel(pcie
, value
, AFI_INTR_MASK
);
1521 /* disable all MSI vectors */
1522 afi_writel(pcie
, 0, AFI_MSI_EN_VEC0
);
1523 afi_writel(pcie
, 0, AFI_MSI_EN_VEC1
);
1524 afi_writel(pcie
, 0, AFI_MSI_EN_VEC2
);
1525 afi_writel(pcie
, 0, AFI_MSI_EN_VEC3
);
1526 afi_writel(pcie
, 0, AFI_MSI_EN_VEC4
);
1527 afi_writel(pcie
, 0, AFI_MSI_EN_VEC5
);
1528 afi_writel(pcie
, 0, AFI_MSI_EN_VEC6
);
1529 afi_writel(pcie
, 0, AFI_MSI_EN_VEC7
);
1531 free_pages(msi
->pages
, 0);
1534 free_irq(msi
->irq
, pcie
);
1536 for (i
= 0; i
< INT_PCI_MSI_NR
; i
++) {
1537 irq
= irq_find_mapping(msi
->domain
, i
);
1539 irq_dispose_mapping(irq
);
1542 irq_domain_remove(msi
->domain
);
1547 static int tegra_pcie_get_xbar_config(struct tegra_pcie
*pcie
, u32 lanes
,
1550 struct device
*dev
= pcie
->dev
;
1551 struct device_node
*np
= dev
->of_node
;
1553 if (of_device_is_compatible(np
, "nvidia,tegra186-pcie")) {
1556 dev_info(dev
, "4x1, 1x1 configuration\n");
1557 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_401
;
1561 dev_info(dev
, "2x1, 1X1, 1x1 configuration\n");
1562 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211
;
1566 dev_info(dev
, "1x1, 1x1, 1x1 configuration\n");
1567 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_111
;
1571 dev_info(dev
, "wrong configuration updated in DT, "
1572 "switching to default 2x1, 1x1, 1x1 "
1574 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_211
;
1577 } else if (of_device_is_compatible(np
, "nvidia,tegra124-pcie") ||
1578 of_device_is_compatible(np
, "nvidia,tegra210-pcie")) {
1581 dev_info(dev
, "4x1, 1x1 configuration\n");
1582 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1
;
1586 dev_info(dev
, "2x1, 1x1 configuration\n");
1587 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1
;
1590 } else if (of_device_is_compatible(np
, "nvidia,tegra30-pcie")) {
1593 dev_info(dev
, "4x1, 2x1 configuration\n");
1594 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420
;
1598 dev_info(dev
, "2x3 configuration\n");
1599 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222
;
1603 dev_info(dev
, "4x1, 1x2 configuration\n");
1604 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411
;
1607 } else if (of_device_is_compatible(np
, "nvidia,tegra20-pcie")) {
1610 dev_info(dev
, "single-mode configuration\n");
1611 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE
;
1615 dev_info(dev
, "dual-mode configuration\n");
1616 *xbar
= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL
;
1625 * Check whether a given set of supplies is available in a device tree node.
1626 * This is used to check whether the new or the legacy device tree bindings
1629 static bool of_regulator_bulk_available(struct device_node
*np
,
1630 struct regulator_bulk_data
*supplies
,
1631 unsigned int num_supplies
)
1636 for (i
= 0; i
< num_supplies
; i
++) {
1637 snprintf(property
, 32, "%s-supply", supplies
[i
].supply
);
1639 if (of_find_property(np
, property
, NULL
) == NULL
)
1647 * Old versions of the device tree binding for this device used a set of power
1648 * supplies that didn't match the hardware inputs. This happened to work for a
1649 * number of cases but is not future proof. However to preserve backwards-
1650 * compatibility with old device trees, this function will try to use the old
1653 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie
*pcie
)
1655 struct device
*dev
= pcie
->dev
;
1656 struct device_node
*np
= dev
->of_node
;
1658 if (of_device_is_compatible(np
, "nvidia,tegra30-pcie"))
1659 pcie
->num_supplies
= 3;
1660 else if (of_device_is_compatible(np
, "nvidia,tegra20-pcie"))
1661 pcie
->num_supplies
= 2;
1663 if (pcie
->num_supplies
== 0) {
1664 dev_err(dev
, "device %pOF not supported in legacy mode\n", np
);
1668 pcie
->supplies
= devm_kcalloc(dev
, pcie
->num_supplies
,
1669 sizeof(*pcie
->supplies
),
1671 if (!pcie
->supplies
)
1674 pcie
->supplies
[0].supply
= "pex-clk";
1675 pcie
->supplies
[1].supply
= "vdd";
1677 if (pcie
->num_supplies
> 2)
1678 pcie
->supplies
[2].supply
= "avdd";
1680 return devm_regulator_bulk_get(dev
, pcie
->num_supplies
, pcie
->supplies
);
1684 * Obtains the list of regulators required for a particular generation of the
1687 * This would've been nice to do simply by providing static tables for use
1688 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1689 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1690 * and either seems to be optional depending on which ports are being used.
1692 static int tegra_pcie_get_regulators(struct tegra_pcie
*pcie
, u32 lane_mask
)
1694 struct device
*dev
= pcie
->dev
;
1695 struct device_node
*np
= dev
->of_node
;
1698 if (of_device_is_compatible(np
, "nvidia,tegra186-pcie")) {
1699 pcie
->num_supplies
= 4;
1701 pcie
->supplies
= devm_kcalloc(pcie
->dev
, pcie
->num_supplies
,
1702 sizeof(*pcie
->supplies
),
1704 if (!pcie
->supplies
)
1707 pcie
->supplies
[i
++].supply
= "dvdd-pex";
1708 pcie
->supplies
[i
++].supply
= "hvdd-pex-pll";
1709 pcie
->supplies
[i
++].supply
= "hvdd-pex";
1710 pcie
->supplies
[i
++].supply
= "vddio-pexctl-aud";
1711 } else if (of_device_is_compatible(np
, "nvidia,tegra210-pcie")) {
1712 pcie
->num_supplies
= 6;
1714 pcie
->supplies
= devm_kcalloc(pcie
->dev
, pcie
->num_supplies
,
1715 sizeof(*pcie
->supplies
),
1717 if (!pcie
->supplies
)
1720 pcie
->supplies
[i
++].supply
= "avdd-pll-uerefe";
1721 pcie
->supplies
[i
++].supply
= "hvddio-pex";
1722 pcie
->supplies
[i
++].supply
= "dvddio-pex";
1723 pcie
->supplies
[i
++].supply
= "dvdd-pex-pll";
1724 pcie
->supplies
[i
++].supply
= "hvdd-pex-pll-e";
1725 pcie
->supplies
[i
++].supply
= "vddio-pex-ctl";
1726 } else if (of_device_is_compatible(np
, "nvidia,tegra124-pcie")) {
1727 pcie
->num_supplies
= 7;
1729 pcie
->supplies
= devm_kcalloc(dev
, pcie
->num_supplies
,
1730 sizeof(*pcie
->supplies
),
1732 if (!pcie
->supplies
)
1735 pcie
->supplies
[i
++].supply
= "avddio-pex";
1736 pcie
->supplies
[i
++].supply
= "dvddio-pex";
1737 pcie
->supplies
[i
++].supply
= "avdd-pex-pll";
1738 pcie
->supplies
[i
++].supply
= "hvdd-pex";
1739 pcie
->supplies
[i
++].supply
= "hvdd-pex-pll-e";
1740 pcie
->supplies
[i
++].supply
= "vddio-pex-ctl";
1741 pcie
->supplies
[i
++].supply
= "avdd-pll-erefe";
1742 } else if (of_device_is_compatible(np
, "nvidia,tegra30-pcie")) {
1743 bool need_pexa
= false, need_pexb
= false;
1745 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1746 if (lane_mask
& 0x0f)
1749 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1750 if (lane_mask
& 0x30)
1753 pcie
->num_supplies
= 4 + (need_pexa
? 2 : 0) +
1754 (need_pexb
? 2 : 0);
1756 pcie
->supplies
= devm_kcalloc(dev
, pcie
->num_supplies
,
1757 sizeof(*pcie
->supplies
),
1759 if (!pcie
->supplies
)
1762 pcie
->supplies
[i
++].supply
= "avdd-pex-pll";
1763 pcie
->supplies
[i
++].supply
= "hvdd-pex";
1764 pcie
->supplies
[i
++].supply
= "vddio-pex-ctl";
1765 pcie
->supplies
[i
++].supply
= "avdd-plle";
1768 pcie
->supplies
[i
++].supply
= "avdd-pexa";
1769 pcie
->supplies
[i
++].supply
= "vdd-pexa";
1773 pcie
->supplies
[i
++].supply
= "avdd-pexb";
1774 pcie
->supplies
[i
++].supply
= "vdd-pexb";
1776 } else if (of_device_is_compatible(np
, "nvidia,tegra20-pcie")) {
1777 pcie
->num_supplies
= 5;
1779 pcie
->supplies
= devm_kcalloc(dev
, pcie
->num_supplies
,
1780 sizeof(*pcie
->supplies
),
1782 if (!pcie
->supplies
)
1785 pcie
->supplies
[0].supply
= "avdd-pex";
1786 pcie
->supplies
[1].supply
= "vdd-pex";
1787 pcie
->supplies
[2].supply
= "avdd-pex-pll";
1788 pcie
->supplies
[3].supply
= "avdd-plle";
1789 pcie
->supplies
[4].supply
= "vddio-pex-clk";
1792 if (of_regulator_bulk_available(dev
->of_node
, pcie
->supplies
,
1793 pcie
->num_supplies
))
1794 return devm_regulator_bulk_get(dev
, pcie
->num_supplies
,
1798 * If not all regulators are available for this new scheme, assume
1799 * that the device tree complies with an older version of the device
1802 dev_info(dev
, "using legacy DT binding for power supplies\n");
1804 devm_kfree(dev
, pcie
->supplies
);
1805 pcie
->num_supplies
= 0;
1807 return tegra_pcie_get_legacy_regulators(pcie
);
1810 static int tegra_pcie_parse_dt(struct tegra_pcie
*pcie
)
1812 struct device
*dev
= pcie
->dev
;
1813 struct device_node
*np
= dev
->of_node
, *port
;
1814 const struct tegra_pcie_soc
*soc
= pcie
->soc
;
1815 struct of_pci_range_parser parser
;
1816 struct of_pci_range range
;
1817 u32 lanes
= 0, mask
= 0;
1818 unsigned int lane
= 0;
1819 struct resource res
;
1822 if (of_pci_range_parser_init(&parser
, np
)) {
1823 dev_err(dev
, "missing \"ranges\" property\n");
1827 for_each_of_pci_range(&parser
, &range
) {
1828 err
= of_pci_range_to_resource(&range
, np
, &res
);
1832 switch (res
.flags
& IORESOURCE_TYPE_BITS
) {
1834 /* Track the bus -> CPU I/O mapping offset. */
1835 pcie
->offset
.io
= res
.start
- range
.pci_addr
;
1837 memcpy(&pcie
->pio
, &res
, sizeof(res
));
1838 pcie
->pio
.name
= np
->full_name
;
1841 * The Tegra PCIe host bridge uses this to program the
1842 * mapping of the I/O space to the physical address,
1843 * so we override the .start and .end fields here that
1844 * of_pci_range_to_resource() converted to I/O space.
1845 * We also set the IORESOURCE_MEM type to clarify that
1846 * the resource is in the physical memory space.
1848 pcie
->io
.start
= range
.cpu_addr
;
1849 pcie
->io
.end
= range
.cpu_addr
+ range
.size
- 1;
1850 pcie
->io
.flags
= IORESOURCE_MEM
;
1851 pcie
->io
.name
= "I/O";
1853 memcpy(&res
, &pcie
->io
, sizeof(res
));
1856 case IORESOURCE_MEM
:
1858 * Track the bus -> CPU memory mapping offset. This
1859 * assumes that the prefetchable and non-prefetchable
1860 * regions will be the last of type IORESOURCE_MEM in
1861 * the ranges property.
1863 pcie
->offset
.mem
= res
.start
- range
.pci_addr
;
1865 if (res
.flags
& IORESOURCE_PREFETCH
) {
1866 memcpy(&pcie
->prefetch
, &res
, sizeof(res
));
1867 pcie
->prefetch
.name
= "prefetchable";
1869 memcpy(&pcie
->mem
, &res
, sizeof(res
));
1870 pcie
->mem
.name
= "non-prefetchable";
1876 err
= of_pci_parse_bus_range(np
, &pcie
->busn
);
1878 dev_err(dev
, "failed to parse ranges property: %d\n", err
);
1879 pcie
->busn
.name
= np
->name
;
1880 pcie
->busn
.start
= 0;
1881 pcie
->busn
.end
= 0xff;
1882 pcie
->busn
.flags
= IORESOURCE_BUS
;
1885 /* parse root ports */
1886 for_each_child_of_node(np
, port
) {
1887 struct tegra_pcie_port
*rp
;
1891 err
= of_pci_get_devfn(port
);
1893 dev_err(dev
, "failed to parse address: %d\n", err
);
1897 index
= PCI_SLOT(err
);
1899 if (index
< 1 || index
> soc
->num_ports
) {
1900 dev_err(dev
, "invalid port number: %d\n", index
);
1906 err
= of_property_read_u32(port
, "nvidia,num-lanes", &value
);
1908 dev_err(dev
, "failed to parse # of lanes: %d\n",
1914 dev_err(dev
, "invalid # of lanes: %u\n", value
);
1918 lanes
|= value
<< (index
<< 3);
1920 if (!of_device_is_available(port
)) {
1925 mask
|= ((1 << value
) - 1) << lane
;
1928 rp
= devm_kzalloc(dev
, sizeof(*rp
), GFP_KERNEL
);
1932 err
= of_address_to_resource(port
, 0, &rp
->regs
);
1934 dev_err(dev
, "failed to parse address: %d\n", err
);
1938 INIT_LIST_HEAD(&rp
->list
);
1944 rp
->base
= devm_pci_remap_cfg_resource(dev
, &rp
->regs
);
1945 if (IS_ERR(rp
->base
))
1946 return PTR_ERR(rp
->base
);
1948 list_add_tail(&rp
->list
, &pcie
->ports
);
1951 err
= tegra_pcie_get_xbar_config(pcie
, lanes
, &pcie
->xbar_config
);
1953 dev_err(dev
, "invalid lane configuration\n");
1957 err
= tegra_pcie_get_regulators(pcie
, mask
);
1965 * FIXME: If there are no PCIe cards attached, then calling this function
1966 * can result in the increase of the bootup time as there are big timeout
1969 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1970 static bool tegra_pcie_port_check_link(struct tegra_pcie_port
*port
)
1972 struct device
*dev
= port
->pcie
->dev
;
1973 unsigned int retries
= 3;
1974 unsigned long value
;
1976 /* override presence detection */
1977 value
= readl(port
->base
+ RP_PRIV_MISC
);
1978 value
&= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT
;
1979 value
|= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT
;
1980 writel(value
, port
->base
+ RP_PRIV_MISC
);
1983 unsigned int timeout
= TEGRA_PCIE_LINKUP_TIMEOUT
;
1986 value
= readl(port
->base
+ RP_VEND_XP
);
1988 if (value
& RP_VEND_XP_DL_UP
)
1991 usleep_range(1000, 2000);
1992 } while (--timeout
);
1995 dev_err(dev
, "link %u down, retrying\n", port
->index
);
1999 timeout
= TEGRA_PCIE_LINKUP_TIMEOUT
;
2002 value
= readl(port
->base
+ RP_LINK_CONTROL_STATUS
);
2004 if (value
& RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE
)
2007 usleep_range(1000, 2000);
2008 } while (--timeout
);
2011 tegra_pcie_port_reset(port
);
2012 } while (--retries
);
2017 static void tegra_pcie_enable_ports(struct tegra_pcie
*pcie
)
2019 struct device
*dev
= pcie
->dev
;
2020 struct tegra_pcie_port
*port
, *tmp
;
2022 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
) {
2023 dev_info(dev
, "probing port %u, using %u lanes\n",
2024 port
->index
, port
->lanes
);
2026 tegra_pcie_port_enable(port
);
2028 if (tegra_pcie_port_check_link(port
))
2031 dev_info(dev
, "link %u down, ignoring\n", port
->index
);
2033 tegra_pcie_port_disable(port
);
2034 tegra_pcie_port_free(port
);
2038 static const struct tegra_pcie_soc tegra20_pcie
= {
2040 .msi_base_shift
= 0,
2041 .pads_pll_ctl
= PADS_PLL_CTL_TEGRA20
,
2042 .tx_ref_sel
= PADS_PLL_CTL_TXCLKREF_DIV10
,
2043 .pads_refclk_cfg0
= 0xfa5cfa5c,
2044 .has_pex_clkreq_en
= false,
2045 .has_pex_bias_ctrl
= false,
2046 .has_intr_prsnt_sense
= false,
2047 .has_cml_clk
= false,
2049 .force_pca_enable
= false,
2050 .program_uphy
= true,
2053 static const struct tegra_pcie_soc tegra30_pcie
= {
2055 .msi_base_shift
= 8,
2056 .pads_pll_ctl
= PADS_PLL_CTL_TEGRA30
,
2057 .tx_ref_sel
= PADS_PLL_CTL_TXCLKREF_BUF_EN
,
2058 .pads_refclk_cfg0
= 0xfa5cfa5c,
2059 .pads_refclk_cfg1
= 0xfa5cfa5c,
2060 .has_pex_clkreq_en
= true,
2061 .has_pex_bias_ctrl
= true,
2062 .has_intr_prsnt_sense
= true,
2063 .has_cml_clk
= true,
2065 .force_pca_enable
= false,
2066 .program_uphy
= true,
2069 static const struct tegra_pcie_soc tegra124_pcie
= {
2071 .msi_base_shift
= 8,
2072 .pads_pll_ctl
= PADS_PLL_CTL_TEGRA30
,
2073 .tx_ref_sel
= PADS_PLL_CTL_TXCLKREF_BUF_EN
,
2074 .pads_refclk_cfg0
= 0x44ac44ac,
2075 .has_pex_clkreq_en
= true,
2076 .has_pex_bias_ctrl
= true,
2077 .has_intr_prsnt_sense
= true,
2078 .has_cml_clk
= true,
2080 .force_pca_enable
= false,
2081 .program_uphy
= true,
2084 static const struct tegra_pcie_soc tegra210_pcie
= {
2086 .msi_base_shift
= 8,
2087 .pads_pll_ctl
= PADS_PLL_CTL_TEGRA30
,
2088 .tx_ref_sel
= PADS_PLL_CTL_TXCLKREF_BUF_EN
,
2089 .pads_refclk_cfg0
= 0x90b890b8,
2090 .has_pex_clkreq_en
= true,
2091 .has_pex_bias_ctrl
= true,
2092 .has_intr_prsnt_sense
= true,
2093 .has_cml_clk
= true,
2095 .force_pca_enable
= true,
2096 .program_uphy
= true,
2099 static const struct tegra_pcie_soc tegra186_pcie
= {
2101 .msi_base_shift
= 8,
2102 .pads_pll_ctl
= PADS_PLL_CTL_TEGRA30
,
2103 .tx_ref_sel
= PADS_PLL_CTL_TXCLKREF_BUF_EN
,
2104 .pads_refclk_cfg0
= 0x80b880b8,
2105 .pads_refclk_cfg1
= 0x000480b8,
2106 .has_pex_clkreq_en
= true,
2107 .has_pex_bias_ctrl
= true,
2108 .has_intr_prsnt_sense
= true,
2109 .has_cml_clk
= false,
2111 .force_pca_enable
= false,
2112 .program_uphy
= false,
2115 static const struct of_device_id tegra_pcie_of_match
[] = {
2116 { .compatible
= "nvidia,tegra186-pcie", .data
= &tegra186_pcie
},
2117 { .compatible
= "nvidia,tegra210-pcie", .data
= &tegra210_pcie
},
2118 { .compatible
= "nvidia,tegra124-pcie", .data
= &tegra124_pcie
},
2119 { .compatible
= "nvidia,tegra30-pcie", .data
= &tegra30_pcie
},
2120 { .compatible
= "nvidia,tegra20-pcie", .data
= &tegra20_pcie
},
2124 static void *tegra_pcie_ports_seq_start(struct seq_file
*s
, loff_t
*pos
)
2126 struct tegra_pcie
*pcie
= s
->private;
2128 if (list_empty(&pcie
->ports
))
2131 seq_printf(s
, "Index Status\n");
2133 return seq_list_start(&pcie
->ports
, *pos
);
2136 static void *tegra_pcie_ports_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2138 struct tegra_pcie
*pcie
= s
->private;
2140 return seq_list_next(v
, &pcie
->ports
, pos
);
2143 static void tegra_pcie_ports_seq_stop(struct seq_file
*s
, void *v
)
2147 static int tegra_pcie_ports_seq_show(struct seq_file
*s
, void *v
)
2149 bool up
= false, active
= false;
2150 struct tegra_pcie_port
*port
;
2153 port
= list_entry(v
, struct tegra_pcie_port
, list
);
2155 value
= readl(port
->base
+ RP_VEND_XP
);
2157 if (value
& RP_VEND_XP_DL_UP
)
2160 value
= readl(port
->base
+ RP_LINK_CONTROL_STATUS
);
2162 if (value
& RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE
)
2165 seq_printf(s
, "%2u ", port
->index
);
2168 seq_printf(s
, "up");
2172 seq_printf(s
, ", ");
2174 seq_printf(s
, "active");
2177 seq_printf(s
, "\n");
2181 static const struct seq_operations tegra_pcie_ports_seq_ops
= {
2182 .start
= tegra_pcie_ports_seq_start
,
2183 .next
= tegra_pcie_ports_seq_next
,
2184 .stop
= tegra_pcie_ports_seq_stop
,
2185 .show
= tegra_pcie_ports_seq_show
,
2188 static int tegra_pcie_ports_open(struct inode
*inode
, struct file
*file
)
2190 struct tegra_pcie
*pcie
= inode
->i_private
;
2194 err
= seq_open(file
, &tegra_pcie_ports_seq_ops
);
2198 s
= file
->private_data
;
2204 static const struct file_operations tegra_pcie_ports_ops
= {
2205 .owner
= THIS_MODULE
,
2206 .open
= tegra_pcie_ports_open
,
2208 .llseek
= seq_lseek
,
2209 .release
= seq_release
,
2212 static int tegra_pcie_debugfs_init(struct tegra_pcie
*pcie
)
2214 struct dentry
*file
;
2216 pcie
->debugfs
= debugfs_create_dir("pcie", NULL
);
2220 file
= debugfs_create_file("ports", S_IFREG
| S_IRUGO
, pcie
->debugfs
,
2221 pcie
, &tegra_pcie_ports_ops
);
2228 debugfs_remove_recursive(pcie
->debugfs
);
2229 pcie
->debugfs
= NULL
;
2233 static int tegra_pcie_probe(struct platform_device
*pdev
)
2235 struct device
*dev
= &pdev
->dev
;
2236 struct pci_host_bridge
*host
;
2237 struct tegra_pcie
*pcie
;
2238 struct pci_bus
*child
;
2241 host
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
2245 pcie
= pci_host_bridge_priv(host
);
2246 host
->sysdata
= pcie
;
2248 pcie
->soc
= of_device_get_match_data(dev
);
2249 INIT_LIST_HEAD(&pcie
->ports
);
2252 err
= tegra_pcie_parse_dt(pcie
);
2256 err
= tegra_pcie_get_resources(pcie
);
2258 dev_err(dev
, "failed to request resources: %d\n", err
);
2262 err
= tegra_pcie_enable_controller(pcie
);
2266 err
= tegra_pcie_request_resources(pcie
);
2270 /* setup the AFI address translations */
2271 tegra_pcie_setup_translations(pcie
);
2273 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
2274 err
= tegra_pcie_enable_msi(pcie
);
2276 dev_err(dev
, "failed to enable MSI support: %d\n", err
);
2281 tegra_pcie_enable_ports(pcie
);
2283 host
->busnr
= pcie
->busn
.start
;
2284 host
->dev
.parent
= &pdev
->dev
;
2285 host
->ops
= &tegra_pcie_ops
;
2286 host
->map_irq
= tegra_pcie_map_irq
;
2287 host
->swizzle_irq
= pci_common_swizzle
;
2289 err
= pci_scan_root_bus_bridge(host
);
2291 dev_err(dev
, "failed to register host: %d\n", err
);
2295 pci_bus_size_bridges(host
->bus
);
2296 pci_bus_assign_resources(host
->bus
);
2298 list_for_each_entry(child
, &host
->bus
->children
, node
)
2299 pcie_bus_configure_settings(child
);
2301 pci_bus_add_devices(host
->bus
);
2303 if (IS_ENABLED(CONFIG_DEBUG_FS
)) {
2304 err
= tegra_pcie_debugfs_init(pcie
);
2306 dev_err(dev
, "failed to setup debugfs: %d\n", err
);
2312 if (IS_ENABLED(CONFIG_PCI_MSI
))
2313 tegra_pcie_disable_msi(pcie
);
2315 tegra_pcie_put_resources(pcie
);
2319 static struct platform_driver tegra_pcie_driver
= {
2321 .name
= "tegra-pcie",
2322 .of_match_table
= tegra_pcie_of_match
,
2323 .suppress_bind_attrs
= true,
2325 .probe
= tegra_pcie_probe
,
2327 builtin_platform_driver(tegra_pcie_driver
);