29 #include <linux/kernel.h>
30 #include <linux/pci.h>
35 #include <linux/export.h>
37 #include <asm/sizes.h>
47 #define AFI_OFFSET 0x3800
48 #define PADS_OFFSET 0x3000
49 #define RP0_OFFSET 0x0000
50 #define RP1_OFFSET 0x1000
52 #define AFI_AXI_BAR0_SZ 0x00
53 #define AFI_AXI_BAR1_SZ 0x04
54 #define AFI_AXI_BAR2_SZ 0x08
55 #define AFI_AXI_BAR3_SZ 0x0c
56 #define AFI_AXI_BAR4_SZ 0x10
57 #define AFI_AXI_BAR5_SZ 0x14
59 #define AFI_AXI_BAR0_START 0x18
60 #define AFI_AXI_BAR1_START 0x1c
61 #define AFI_AXI_BAR2_START 0x20
62 #define AFI_AXI_BAR3_START 0x24
63 #define AFI_AXI_BAR4_START 0x28
64 #define AFI_AXI_BAR5_START 0x2c
66 #define AFI_FPCI_BAR0 0x30
67 #define AFI_FPCI_BAR1 0x34
68 #define AFI_FPCI_BAR2 0x38
69 #define AFI_FPCI_BAR3 0x3c
70 #define AFI_FPCI_BAR4 0x40
71 #define AFI_FPCI_BAR5 0x44
73 #define AFI_CACHE_BAR0_SZ 0x48
74 #define AFI_CACHE_BAR0_ST 0x4c
75 #define AFI_CACHE_BAR1_SZ 0x50
76 #define AFI_CACHE_BAR1_ST 0x54
78 #define AFI_MSI_BAR_SZ 0x60
79 #define AFI_MSI_FPCI_BAR_ST 0x64
80 #define AFI_MSI_AXI_BAR_ST 0x68
82 #define AFI_CONFIGURATION 0xac
83 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
85 #define AFI_FPCI_ERROR_MASKS 0xb0
87 #define AFI_INTR_MASK 0xb4
88 #define AFI_INTR_MASK_INT_MASK (1 << 0)
89 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
91 #define AFI_INTR_CODE 0xb8
92 #define AFI_INTR_CODE_MASK 0xf
93 #define AFI_INTR_MASTER_ABORT 4
94 #define AFI_INTR_LEGACY 6
96 #define AFI_INTR_SIGNATURE 0xbc
97 #define AFI_SM_INTR_ENABLE 0xc4
99 #define AFI_AFI_INTR_ENABLE 0xc8
100 #define AFI_INTR_EN_INI_SLVERR (1 << 0)
101 #define AFI_INTR_EN_INI_DECERR (1 << 1)
102 #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
103 #define AFI_INTR_EN_TGT_DECERR (1 << 3)
104 #define AFI_INTR_EN_TGT_WRERR (1 << 4)
105 #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
106 #define AFI_INTR_EN_AXI_DECERR (1 << 6)
107 #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
109 #define AFI_PCIE_CONFIG 0x0f8
110 #define AFI_PCIE_CONFIG_PCIEC0_DISABLE_DEVICE (1 << 1)
111 #define AFI_PCIE_CONFIG_PCIEC1_DISABLE_DEVICE (1 << 2)
112 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
113 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
114 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
116 #define AFI_FUSE 0x104
117 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
119 #define AFI_PEX0_CTRL 0x110
120 #define AFI_PEX1_CTRL 0x118
121 #define AFI_PEX_CTRL_RST (1 << 0)
122 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
124 #define RP_VEND_XP 0x00000F00
125 #define RP_VEND_XP_DL_UP (1 << 30)
127 #define RP_LINK_CONTROL_STATUS 0x00000090
128 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
130 #define PADS_CTL_SEL 0x0000009C
132 #define PADS_CTL 0x000000A0
133 #define PADS_CTL_IDDQ_1L (1 << 0)
134 #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
135 #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
137 #define PADS_PLL_CTL 0x000000B8
138 #define PADS_PLL_CTL_RST_B4SM (1 << 1)
139 #define PADS_PLL_CTL_LOCKDET (1 << 8)
140 #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
141 #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
142 #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
143 #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
144 #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
145 #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
146 #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
149 #define PMC_SCRATCH42 0x144
150 #define PMC_SCRATCH42_PCX_CLAMP (1 << 0)
154 #define pmc_writel(value, reg) \
155 __raw_writel(value, reg_pmc_base + (reg))
156 #define pmc_readl(reg) \
157 __raw_readl(reg_pmc_base + (reg))
174 #define PCIE_REGS_SZ SZ_16K
175 #define PCIE_CFG_OFF PCIE_REGS_SZ
176 #define PCIE_CFG_SZ SZ_1M
177 #define PCIE_EXT_CFG_OFF (PCIE_CFG_SZ + PCIE_CFG_OFF)
178 #define PCIE_EXT_CFG_SZ SZ_1M
179 #define PCIE_IOMAP_SZ (PCIE_REGS_SZ + PCIE_CFG_SZ + PCIE_EXT_CFG_SZ)
181 #define MEM_BASE_0 (TEGRA_PCIE_BASE + SZ_256M)
182 #define MEM_SIZE_0 SZ_128M
183 #define MEM_BASE_1 (MEM_BASE_0 + MEM_SIZE_0)
184 #define MEM_SIZE_1 SZ_128M
185 #define PREFETCH_MEM_BASE_0 (MEM_BASE_1 + MEM_SIZE_1)
186 #define PREFETCH_MEM_SIZE_0 SZ_128M
187 #define PREFETCH_MEM_BASE_1 (PREFETCH_MEM_BASE_0 + PREFETCH_MEM_SIZE_0)
188 #define PREFETCH_MEM_SIZE_1 SZ_128M
190 #define PCIE_CONF_BUS(b) ((b) << 16)
191 #define PCIE_CONF_DEV(d) ((d) << 11)
192 #define PCIE_CONF_FUNC(f) ((f) << 8)
193 #define PCIE_CONF_REG(r) \
194 (((r) & ~0x3) | (((r) < 256) ? PCIE_CFG_OFF : PCIE_EXT_CFG_OFF))
228 static inline u32 afi_readl(
unsigned long offset)
238 static inline u32 pads_readl(
unsigned long offset)
247 for (i = tegra_pcie.num_ports - 1; i >= 0; i--) {
248 int rbus = tegra_pcie.port[
i].root_bus_nr;
249 if (rbus != -1 && rbus == bus)
253 return i >= 0 ? tegra_pcie.port + i :
NULL;
256 static int tegra_pcie_read_conf(
struct pci_bus *bus,
unsigned int devfn,
268 addr = pp->
base + (where & ~0x3);
279 *val = (*val >> (8 * (where & 3))) & 0xff;
281 *val = (*val >> (8 * (where & 3))) & 0xffff;
286 static int tegra_pcie_write_conf(
struct pci_bus *bus,
unsigned int devfn,
287 int where,
int size,
u32 val)
299 addr = pp->
base + (where & ~0x3);
313 mask = ~(0xffff << ((where & 0x3) * 8));
315 mask = ~(0xff << ((where & 0x3) * 8));
320 tmp |= val << ((where & 0x3) * 8);
326 static struct pci_ops tegra_pcie_ops = {
327 .read = tegra_pcie_read_conf,
328 .write = tegra_pcie_write_conf,
363 if (nr >= tegra_pcie.num_ports)
366 pp = tegra_pcie.port +
nr;
375 "PCIe %d MEM", pp->
index);
378 if (pp->
index == 0) {
387 panic(
"Request PCIe Memory resource failed\n");
394 "PCIe %d PREFETCH MEM", pp->
index);
397 if (pp->
index == 0) {
406 panic(
"Request PCIe Prefetch Memory resource failed\n");
422 if (nr >= tegra_pcie.num_ports)
425 pp = tegra_pcie.port +
nr;
434 .setup = tegra_pcie_setup,
435 .scan = tegra_pcie_scan_bus,
436 .map_irq = tegra_pcie_map_irq,
449 "Response decoding error",
450 "AXI response decoding error",
451 "Transcation timeout",
471 pr_debug(
"PCIE: %s, signature: %08x\n", err_msg[code], signature);
473 pr_err(
"PCIE: %s, signature: %08x\n", err_msg[code], signature);
478 static void tegra_pcie_setup_translations(
void)
485 fpci_bar = ((
u32)0xfdff << 16);
493 fpci_bar = ((
u32)0xfe1 << 20);
501 fpci_bar = ((
__u32)0xfdfc << 16);
517 fpci_bar = (((
MEM_BASE_0 >> 12) & 0x0FFFFFFF) << 4) | 0x1;
545 static int tegra_pcie_enable_controller(
void)
553 afi_writel(val, reg);
555 afi_writel(val, reg);
558 afi_writel(val, reg);
596 pads_writel(0xfa5cfa5c, 0xc8);
603 if (--timeout == 0) {
604 pr_err(
"Tegra PCIe error: timeout waiting for PLL\n");
640 static void tegra_pcie_xclk_clamp(
bool clamp)
652 static void tegra_pcie_power_off(
void)
659 tegra_pcie_xclk_clamp(
true);
662 static int tegra_pcie_power_regate(
void)
666 tegra_pcie_power_off();
668 tegra_pcie_xclk_clamp(
true);
676 pr_err(
"PCIE: powerup sequence failed: %d\n", err);
682 tegra_pcie_xclk_clamp(
false);
684 clk_prepare_enable(tegra_pcie.afi_clk);
685 clk_prepare_enable(tegra_pcie.pex_clk);
686 return clk_prepare_enable(tegra_pcie.pll_e);
689 static int tegra_pcie_clocks_get(
void)
694 if (IS_ERR(tegra_pcie.pex_clk))
695 return PTR_ERR(tegra_pcie.pex_clk);
698 if (IS_ERR(tegra_pcie.afi_clk)) {
699 err = PTR_ERR(tegra_pcie.afi_clk);
704 if (IS_ERR(tegra_pcie.pcie_xclk)) {
705 err = PTR_ERR(tegra_pcie.pcie_xclk);
710 if (IS_ERR(tegra_pcie.pll_e)) {
711 err = PTR_ERR(tegra_pcie.pll_e);
727 static void tegra_pcie_clocks_put(
void)
735 static int __init tegra_pcie_get_resources(
void)
739 err = tegra_pcie_clocks_get();
741 pr_err(
"PCIE: failed to get clocks: %d\n", err);
745 err = tegra_pcie_power_regate();
747 pr_err(
"PCIE: failed to power up: %d\n", err);
752 if (tegra_pcie.regs ==
NULL) {
753 pr_err(
"PCIE: Failed to map PCI/AFI registers\n");
761 pr_err(
"PCIE: Failed to register IRQ: %d\n", err);
771 tegra_pcie_power_off();
773 tegra_pcie_clocks_put();
783 #define TEGRA_PCIE_LINKUP_TIMEOUT 200
804 pr_err(
"PCIE: port %d: link down, retrying\n", idx);
812 if (reg & 0x20000000)
822 afi_writel(reg, reset_reg);
825 afi_writel(reg, reset_reg);
837 pp = tegra_pcie.port + tegra_pcie.num_ports;
841 pp->
link_up = tegra_pcie_check_link(pp, index, reset_reg);
849 tegra_pcie.num_ports++;
859 if (!(init_port0 || init_port1))
864 err = tegra_pcie_get_resources();
868 err = tegra_pcie_enable_controller();
873 tegra_pcie_setup_translations();