blob: e2a8e4cab22eb37af1535092e3365315be1eee79 [file] [log] [blame]
Thierry Redingd1523b52013-08-09 16:49:19 +02001/*
Jay Agarwal94716cd2013-08-09 16:49:24 +02002 * PCIe host controller driver for Tegra SoCs
Thierry Redingd1523b52013-08-09 16:49:19 +02003 *
4 * Copyright (c) 2010, CompuLab, Ltd.
5 * Author: Mike Rapoport <mike@compulab.co.il>
6 *
7 * Based on NVIDIA PCIe driver
8 * Copyright (c) 2008-2009, NVIDIA Corporation.
9 *
10 * Bits taken from arch/arm/mach-dove/pcie.c
11 *
Paul Gortmakerad183272016-07-02 19:13:31 -040012 * Author: Thierry Reding <treding@nvidia.com>
13 *
Thierry Redingd1523b52013-08-09 16:49:19 +020014 * 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.
18 *
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
22 * more details.
23 *
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.
27 */
28
29#include <linux/clk.h>
Thierry Reding2cb989f2014-07-22 12:30:46 -060030#include <linux/debugfs.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020031#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>
Paul Gortmakerad183272016-07-02 19:13:31 -040037#include <linux/init.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020038#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>
Thierry Reding7f1f0542014-08-26 17:11:38 +020043#include <linux/phy/phy.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020044#include <linux/platform_device.h>
Stephen Warren3127a6b2013-11-06 15:56:58 -070045#include <linux/reset.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020046#include <linux/sizes.h>
47#include <linux/slab.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020048#include <linux/vmalloc.h>
49#include <linux/regulator/consumer.h>
50
Thierry Reding306a7f92014-07-17 13:17:24 +020051#include <soc/tegra/cpuidle.h>
Thierry Reding72323982014-07-11 13:19:06 +020052#include <soc/tegra/pmc.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020053
Thierry Redingd1523b52013-08-09 16:49:19 +020054#include <asm/mach/irq.h>
55#include <asm/mach/map.h>
56#include <asm/mach/pci.h>
57
58#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020059
60/* register definitions */
61
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
68
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
75
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
82
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
87
88#define AFI_MSI_BAR_SZ 0x60
89#define AFI_MSI_FPCI_BAR_ST 0x64
90#define AFI_MSI_AXI_BAR_ST 0x68
91
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
100
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
109
110#define AFI_CONFIGURATION 0xac
111#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
112
113#define AFI_FPCI_ERROR_MASKS 0xb0
114
115#define AFI_INTR_MASK 0xb4
116#define AFI_INTR_MASK_INT_MASK (1 << 0)
117#define AFI_INTR_MASK_MSI_MASK (1 << 8)
118
119#define AFI_INTR_CODE 0xb8
120#define AFI_INTR_CODE_MASK 0xf
Thierry Reding7f1f0542014-08-26 17:11:38 +0200121#define AFI_INTR_INI_SLAVE_ERROR 1
122#define AFI_INTR_INI_DECODE_ERROR 2
Thierry Redingd1523b52013-08-09 16:49:19 +0200123#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
Thierry Reding7f1f0542014-08-26 17:11:38 +0200128#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
Thierry Redingd1523b52013-08-09 16:49:19 +0200135
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)
147
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)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200157#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200158
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)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200164#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200165#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200166#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200167#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200168#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200169#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200170
171#define AFI_FUSE 0x104
172#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
173
174#define AFI_PEX0_CTRL 0x110
175#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200176#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200177#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200178#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200179#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200180#define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
181
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)
Thierry Redingd1523b52013-08-09 16:49:19 +0200185
Jay Agarwal94716cd2013-08-09 16:49:24 +0200186#define AFI_PEXBIAS_CTRL_0 0x168
187
Thierry Redingacb341e2016-07-25 16:02:05 -0500188#define RP_VEND_XP 0x00000f00
Thierry Redingd1523b52013-08-09 16:49:19 +0200189#define RP_VEND_XP_DL_UP (1 << 30)
190
Thierry Redingacb341e2016-07-25 16:02:05 -0500191#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)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200194
Thierry Redingd1523b52013-08-09 16:49:19 +0200195#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
198
Thierry Redingacb341e2016-07-25 16:02:05 -0500199#define PADS_CTL_SEL 0x0000009c
Thierry Redingd1523b52013-08-09 16:49:19 +0200200
Thierry Redingacb341e2016-07-25 16:02:05 -0500201#define PADS_CTL 0x000000a0
Thierry Redingd1523b52013-08-09 16:49:19 +0200202#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)
205
Thierry Redingacb341e2016-07-25 16:02:05 -0500206#define PADS_PLL_CTL_TEGRA20 0x000000b8
207#define PADS_PLL_CTL_TEGRA30 0x000000b4
Thierry Redingd1523b52013-08-09 16:49:19 +0200208#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)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200217#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
218
Thierry Redingacb341e2016-07-25 16:02:05 -0500219#define PADS_REFCLK_CFG0 0x000000c8
220#define PADS_REFCLK_CFG1 0x000000cc
221#define PADS_REFCLK_BIAS 0x000000d0
Thierry Redingd1523b52013-08-09 16:49:19 +0200222
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200223/*
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.
227 */
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 */
232
Thierry Redingd1523b52013-08-09 16:49:19 +0200233struct tegra_msi {
Yijing Wangc2791b82014-11-11 17:45:45 -0700234 struct msi_controller chip;
Thierry Redingd1523b52013-08-09 16:49:19 +0200235 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
236 struct irq_domain *domain;
237 unsigned long pages;
238 struct mutex lock;
239 int irq;
240};
241
Jay Agarwal94716cd2013-08-09 16:49:24 +0200242/* used to differentiate between Tegra SoC generations */
Thierry Redinga7fbae22016-08-15 17:31:31 +0200243struct tegra_pcie_soc {
Jay Agarwal94716cd2013-08-09 16:49:24 +0200244 unsigned int num_ports;
245 unsigned int msi_base_shift;
246 u32 pads_pll_ctl;
247 u32 tx_ref_sel;
Stephen Warrenf8144302016-07-25 16:02:27 -0500248 u32 pads_refclk_cfg0;
249 u32 pads_refclk_cfg1;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200250 bool has_pex_clkreq_en;
251 bool has_pex_bias_ctrl;
252 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200253 bool has_cml_clk;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200254 bool has_gen2;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200255};
256
Yijing Wangc2791b82014-11-11 17:45:45 -0700257static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
Thierry Redingd1523b52013-08-09 16:49:19 +0200258{
259 return container_of(chip, struct tegra_msi, chip);
260}
261
262struct tegra_pcie {
263 struct device *dev;
264
265 void __iomem *pads;
266 void __iomem *afi;
267 int irq;
268
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700269 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200270 struct resource *cs;
271
272 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100273 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200274 struct resource mem;
275 struct resource prefetch;
276 struct resource busn;
277
Thierry Reding56e75e22016-02-09 15:52:32 +0100278 struct {
279 resource_size_t mem;
280 resource_size_t io;
281 } offset;
282
Thierry Redingd1523b52013-08-09 16:49:19 +0200283 struct clk *pex_clk;
284 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200285 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200286 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200287
Stephen Warren3127a6b2013-11-06 15:56:58 -0700288 struct reset_control *pex_rst;
289 struct reset_control *afi_rst;
290 struct reset_control *pcie_xrst;
291
Thierry Reding6fe7c182015-11-11 18:25:59 +0100292 bool legacy_phy;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200293 struct phy *phy;
294
Thierry Redingd1523b52013-08-09 16:49:19 +0200295 struct tegra_msi msi;
296
297 struct list_head ports;
Thierry Redingd1523b52013-08-09 16:49:19 +0200298 u32 xbar_config;
299
Thierry Reding077fb152014-05-28 16:49:13 +0200300 struct regulator_bulk_data *supplies;
301 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200302
Thierry Redinga7fbae22016-08-15 17:31:31 +0200303 const struct tegra_pcie_soc *soc;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600304 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200305};
306
307struct tegra_pcie_port {
308 struct tegra_pcie *pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100309 struct device_node *np;
Thierry Redingd1523b52013-08-09 16:49:19 +0200310 struct list_head list;
311 struct resource regs;
312 void __iomem *base;
313 unsigned int index;
314 unsigned int lanes;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100315
316 struct phy **phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200317};
318
319struct tegra_pcie_bus {
320 struct vm_struct *area;
321 struct list_head list;
322 unsigned int nr;
323};
324
325static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
326{
327 return sys->private_data;
328}
329
330static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
331 unsigned long offset)
332{
333 writel(value, pcie->afi + offset);
334}
335
336static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
337{
338 return readl(pcie->afi + offset);
339}
340
341static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
342 unsigned long offset)
343{
344 writel(value, pcie->pads + offset);
345}
346
347static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
348{
349 return readl(pcie->pads + offset);
350}
351
352/*
353 * The configuration space mapping on Tegra is somewhat similar to the ECAM
354 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
355 * register accesses are mapped:
356 *
357 * [27:24] extended register number
358 * [23:16] bus number
359 * [15:11] device number
360 * [10: 8] function number
361 * [ 7: 0] register number
362 *
363 * Mapping the whole extended configuration space would require 256 MiB of
364 * virtual address space, only a small part of which will actually be used.
365 * To work around this, a 1 MiB of virtual addresses are allocated per bus
366 * when the bus is first accessed. When the physical range is mapped, the
367 * the bus number bits are hidden so that the extended register number bits
368 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
369 *
370 * [19:16] extended register number
371 * [15:11] device number
372 * [10: 8] function number
373 * [ 7: 0] register number
374 *
375 * This is achieved by stitching together 16 chunks of 64 KiB of physical
376 * address space via the MMU.
377 */
378static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
379{
380 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
381 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
382}
383
384static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
385 unsigned int busnr)
386{
Ard Biesheuvel512ee2f2015-08-26 10:49:34 +0200387 pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
388 L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
Thierry Redingd1523b52013-08-09 16:49:19 +0200389 phys_addr_t cs = pcie->cs->start;
390 struct tegra_pcie_bus *bus;
391 unsigned int i;
392 int err;
393
394 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
395 if (!bus)
396 return ERR_PTR(-ENOMEM);
397
398 INIT_LIST_HEAD(&bus->list);
399 bus->nr = busnr;
400
401 /* allocate 1 MiB of virtual addresses */
402 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
403 if (!bus->area) {
404 err = -ENOMEM;
405 goto free;
406 }
407
408 /* map each of the 16 chunks of 64 KiB each */
409 for (i = 0; i < 16; i++) {
410 unsigned long virt = (unsigned long)bus->area->addr +
411 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200412 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200413
414 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
415 if (err < 0) {
416 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
417 err);
418 goto unmap;
419 }
420 }
421
422 return bus;
423
424unmap:
425 vunmap(bus->area->addr);
426free:
427 kfree(bus);
428 return ERR_PTR(err);
429}
430
Thierry Redingb4d18d72016-02-09 15:30:48 +0100431static int tegra_pcie_add_bus(struct pci_bus *bus)
Thierry Redingd1523b52013-08-09 16:49:19 +0200432{
Thierry Redingb4d18d72016-02-09 15:30:48 +0100433 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
434 struct tegra_pcie_bus *b;
Thierry Redingd1523b52013-08-09 16:49:19 +0200435
Thierry Redingb4d18d72016-02-09 15:30:48 +0100436 b = tegra_pcie_bus_alloc(pcie, bus->number);
437 if (IS_ERR(b))
438 return PTR_ERR(b);
Thierry Redingd1523b52013-08-09 16:49:19 +0200439
Thierry Redingb4d18d72016-02-09 15:30:48 +0100440 list_add_tail(&b->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200441
Thierry Redingb4d18d72016-02-09 15:30:48 +0100442 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200443}
444
Thierry Redingb4d18d72016-02-09 15:30:48 +0100445static void tegra_pcie_remove_bus(struct pci_bus *child)
446{
447 struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
448 struct tegra_pcie_bus *bus, *tmp;
449
450 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
451 if (bus->nr == child->number) {
452 vunmap(bus->area->addr);
453 list_del(&bus->list);
454 kfree(bus);
455 break;
456 }
457 }
458}
459
460static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
461 unsigned int devfn,
462 int where)
Thierry Redingd1523b52013-08-09 16:49:19 +0200463{
464 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
465 void __iomem *addr = NULL;
466
467 if (bus->number == 0) {
468 unsigned int slot = PCI_SLOT(devfn);
469 struct tegra_pcie_port *port;
470
471 list_for_each_entry(port, &pcie->ports, list) {
472 if (port->index + 1 == slot) {
473 addr = port->base + (where & ~3);
474 break;
475 }
476 }
477 } else {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100478 struct tegra_pcie_bus *b;
479
480 list_for_each_entry(b, &pcie->buses, list)
481 if (b->nr == bus->number)
482 addr = (void __iomem *)b->area->addr;
483
Thierry Redingd1523b52013-08-09 16:49:19 +0200484 if (!addr) {
485 dev_err(pcie->dev,
486 "failed to map cfg. space for bus %u\n",
487 bus->number);
488 return NULL;
489 }
490
491 addr += tegra_pcie_conf_offset(devfn, where);
492 }
493
494 return addr;
495}
496
Thierry Redingd1523b52013-08-09 16:49:19 +0200497static struct pci_ops tegra_pcie_ops = {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100498 .add_bus = tegra_pcie_add_bus,
499 .remove_bus = tegra_pcie_remove_bus,
500 .map_bus = tegra_pcie_map_bus,
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600501 .read = pci_generic_config_read32,
502 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200503};
504
505static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
506{
507 unsigned long ret = 0;
508
509 switch (port->index) {
510 case 0:
511 ret = AFI_PEX0_CTRL;
512 break;
513
514 case 1:
515 ret = AFI_PEX1_CTRL;
516 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200517
518 case 2:
519 ret = AFI_PEX2_CTRL;
520 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200521 }
522
523 return ret;
524}
525
526static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
527{
528 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
529 unsigned long value;
530
531 /* pulse reset signal */
532 value = afi_readl(port->pcie, ctrl);
533 value &= ~AFI_PEX_CTRL_RST;
534 afi_writel(port->pcie, value, ctrl);
535
536 usleep_range(1000, 2000);
537
538 value = afi_readl(port->pcie, ctrl);
539 value |= AFI_PEX_CTRL_RST;
540 afi_writel(port->pcie, value, ctrl);
541}
542
543static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
544{
545 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
Thierry Redinga7fbae22016-08-15 17:31:31 +0200546 const struct tegra_pcie_soc *soc = port->pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +0200547 unsigned long value;
548
549 /* enable reference clock */
550 value = afi_readl(port->pcie, ctrl);
551 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200552
553 if (soc->has_pex_clkreq_en)
554 value |= AFI_PEX_CTRL_CLKREQ_EN;
555
Thierry Reding7f1f0542014-08-26 17:11:38 +0200556 value |= AFI_PEX_CTRL_OVERRIDE_EN;
557
Thierry Redingd1523b52013-08-09 16:49:19 +0200558 afi_writel(port->pcie, value, ctrl);
559
560 tegra_pcie_port_reset(port);
561}
562
563static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
564{
565 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
Thierry Redinga7fbae22016-08-15 17:31:31 +0200566 const struct tegra_pcie_soc *soc = port->pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +0200567 unsigned long value;
568
569 /* assert port reset */
570 value = afi_readl(port->pcie, ctrl);
571 value &= ~AFI_PEX_CTRL_RST;
572 afi_writel(port->pcie, value, ctrl);
573
574 /* disable reference clock */
575 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200576
577 if (soc->has_pex_clkreq_en)
578 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
579
Thierry Redingd1523b52013-08-09 16:49:19 +0200580 value &= ~AFI_PEX_CTRL_REFCLK_EN;
581 afi_writel(port->pcie, value, ctrl);
582}
583
584static void tegra_pcie_port_free(struct tegra_pcie_port *port)
585{
586 struct tegra_pcie *pcie = port->pcie;
587
588 devm_iounmap(pcie->dev, port->base);
589 devm_release_mem_region(pcie->dev, port->regs.start,
590 resource_size(&port->regs));
591 list_del(&port->list);
592 devm_kfree(pcie->dev, port);
593}
594
Thierry Redingd1523b52013-08-09 16:49:19 +0200595/* Tegra PCIE root complex wrongly reports device class */
596static void tegra_pcie_fixup_class(struct pci_dev *dev)
597{
598 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
599}
600DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
601DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200602DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
603DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200604
605/* Tegra PCIE requires relaxed ordering */
606static void tegra_pcie_relax_enable(struct pci_dev *dev)
607{
608 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
609}
610DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
611
612static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
613{
614 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200615 int err;
616
Thierry Reding56e75e22016-02-09 15:52:32 +0100617 sys->mem_offset = pcie->offset.mem;
618 sys->io_offset = pcie->offset.io;
619
Bjorn Helgaasc4102c922016-06-06 15:55:04 -0500620 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->io);
Thierry Reding56e75e22016-02-09 15:52:32 +0100621 if (err < 0)
622 return err;
623
Lorenzo Pieralisi13f392e2016-08-15 17:50:46 +0100624 err = pci_remap_iospace(&pcie->pio, pcie->io.start);
625 if (!err)
626 pci_add_resource_offset(&sys->resources, &pcie->pio,
627 sys->io_offset);
628
Thierry Redingd1523b52013-08-09 16:49:19 +0200629 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
630 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
631 sys->mem_offset);
632 pci_add_resource(&sys->resources, &pcie->busn);
633
Bjorn Helgaas45c64b62016-06-06 15:47:24 -0500634 err = devm_request_pci_bus_resources(pcie->dev, &sys->resources);
635 if (err < 0)
636 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200637
638 return 1;
639}
640
641static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
642{
643 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600644 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200645
Stephen Warrenb4f17372013-05-06 14:19:19 -0600646 tegra_cpuidle_pcie_irqs_in_use();
647
Lucas Stachf5d33522014-04-16 10:24:32 -0600648 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
649 if (!irq)
650 irq = pcie->irq;
651
652 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200653}
654
Thierry Redingd1523b52013-08-09 16:49:19 +0200655static irqreturn_t tegra_pcie_isr(int irq, void *arg)
656{
657 const char *err_msg[] = {
658 "Unknown",
659 "AXI slave error",
660 "AXI decode error",
661 "Target abort",
662 "Master abort",
663 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200664 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200665 "Response decoding error",
666 "AXI response decoding error",
667 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200668 "Slot present pin change",
669 "Slot clock request change",
670 "TMS clock ramp change",
671 "TMS ready for power down",
672 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200673 };
674 struct tegra_pcie *pcie = arg;
675 u32 code, signature;
676
677 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
678 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
679 afi_writel(pcie, 0, AFI_INTR_CODE);
680
681 if (code == AFI_INTR_LEGACY)
682 return IRQ_NONE;
683
684 if (code >= ARRAY_SIZE(err_msg))
685 code = 0;
686
687 /*
688 * do not pollute kernel log with master abort reports since they
689 * happen a lot during enumeration
690 */
691 if (code == AFI_INTR_MASTER_ABORT)
692 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
693 signature);
694 else
695 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
696 signature);
697
698 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
699 code == AFI_INTR_FPCI_DECODE_ERROR) {
700 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
701 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
702
703 if (code == AFI_INTR_MASTER_ABORT)
704 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
705 else
706 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
707 }
708
709 return IRQ_HANDLED;
710}
711
712/*
713 * FPCI map is as follows:
714 * - 0xfdfc000000: I/O space
715 * - 0xfdfe000000: type 0 configuration space
716 * - 0xfdff000000: type 1 configuration space
717 * - 0xfe00000000: type 0 extended configuration space
718 * - 0xfe10000000: type 1 extended configuration space
719 */
720static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
721{
722 u32 fpci_bar, size, axi_address;
723
724 /* Bar 0: type 1 extended configuration space */
725 fpci_bar = 0xfe100000;
726 size = resource_size(pcie->cs);
727 axi_address = pcie->cs->start;
728 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
729 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
730 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
731
732 /* Bar 1: downstream IO bar */
733 fpci_bar = 0xfdfc0000;
734 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100735 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200736 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
737 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
738 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
739
740 /* Bar 2: prefetchable memory BAR */
741 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
742 size = resource_size(&pcie->prefetch);
743 axi_address = pcie->prefetch.start;
744 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
745 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
746 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
747
748 /* Bar 3: non prefetchable memory BAR */
749 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
750 size = resource_size(&pcie->mem);
751 axi_address = pcie->mem.start;
752 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
753 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
754 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
755
756 /* NULL out the remaining BARs as they are not used */
757 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
758 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
759 afi_writel(pcie, 0, AFI_FPCI_BAR4);
760
761 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
762 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
763 afi_writel(pcie, 0, AFI_FPCI_BAR5);
764
765 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100766 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200767 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
768 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
769 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
770
771 /* MSI translations are setup only when needed */
772 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
773 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
774 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
775 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
776}
777
Thierry Reding7f1f0542014-08-26 17:11:38 +0200778static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200779{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200780 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200781 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200782
Thierry Reding7f1f0542014-08-26 17:11:38 +0200783 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200784
Thierry Reding7f1f0542014-08-26 17:11:38 +0200785 while (time_before(jiffies, timeout)) {
786 value = pads_readl(pcie, soc->pads_pll_ctl);
787 if (value & PADS_PLL_CTL_LOCKDET)
788 return 0;
789 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200790
Thierry Reding7f1f0542014-08-26 17:11:38 +0200791 return -ETIMEDOUT;
792}
Thierry Redingd1523b52013-08-09 16:49:19 +0200793
Thierry Reding7f1f0542014-08-26 17:11:38 +0200794static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
795{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200796 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200797 u32 value;
798 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200799
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700800 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200801 pads_writel(pcie, 0x0, PADS_CTL_SEL);
802
803 /* override IDDQ to 1 on all 4 lanes */
804 value = pads_readl(pcie, PADS_CTL);
805 value |= PADS_CTL_IDDQ_1L;
806 pads_writel(pcie, value, PADS_CTL);
807
808 /*
809 * Set up PHY PLL inputs select PLLE output as refclock,
810 * set TX ref sel to div10 (not div5).
811 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200812 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200813 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200814 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
815 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200816
Eric Yuenec732762014-08-26 17:11:37 +0200817 /* reset PLL */
818 value = pads_readl(pcie, soc->pads_pll_ctl);
819 value &= ~PADS_PLL_CTL_RST_B4SM;
820 pads_writel(pcie, value, soc->pads_pll_ctl);
821
822 usleep_range(20, 100);
823
Thierry Redingd1523b52013-08-09 16:49:19 +0200824 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200825 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200826 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200827 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200828
Thierry Redingd1523b52013-08-09 16:49:19 +0200829 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200830 err = tegra_pcie_pll_wait(pcie, 500);
831 if (err < 0) {
832 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
833 return err;
834 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200835
836 /* turn off IDDQ override */
837 value = pads_readl(pcie, PADS_CTL);
838 value &= ~PADS_CTL_IDDQ_1L;
839 pads_writel(pcie, value, PADS_CTL);
840
841 /* enable TX/RX data */
842 value = pads_readl(pcie, PADS_CTL);
843 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
844 pads_writel(pcie, value, PADS_CTL);
845
Thierry Reding7f1f0542014-08-26 17:11:38 +0200846 return 0;
847}
848
Thierry Reding6fe7c182015-11-11 18:25:59 +0100849static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
850{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200851 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100852 u32 value;
853
854 /* disable TX/RX data */
855 value = pads_readl(pcie, PADS_CTL);
856 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
857 pads_writel(pcie, value, PADS_CTL);
858
859 /* override IDDQ */
860 value = pads_readl(pcie, PADS_CTL);
861 value |= PADS_CTL_IDDQ_1L;
862 pads_writel(pcie, PADS_CTL, value);
863
864 /* reset PLL */
865 value = pads_readl(pcie, soc->pads_pll_ctl);
866 value &= ~PADS_PLL_CTL_RST_B4SM;
867 pads_writel(pcie, value, soc->pads_pll_ctl);
868
869 usleep_range(20, 100);
870
871 return 0;
872}
873
874static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
875{
876 struct device *dev = port->pcie->dev;
877 unsigned int i;
878 int err;
879
880 for (i = 0; i < port->lanes; i++) {
881 err = phy_power_on(port->phys[i]);
882 if (err < 0) {
883 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
884 err);
885 return err;
886 }
887 }
888
889 return 0;
890}
891
892static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
893{
894 struct device *dev = port->pcie->dev;
895 unsigned int i;
896 int err;
897
898 for (i = 0; i < port->lanes; i++) {
899 err = phy_power_off(port->phys[i]);
900 if (err < 0) {
901 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
902 err);
903 return err;
904 }
905 }
906
907 return 0;
908}
909
910static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
911{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200912 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100913 struct tegra_pcie_port *port;
914 int err;
915
916 if (pcie->legacy_phy) {
917 if (pcie->phy)
918 err = phy_power_on(pcie->phy);
919 else
920 err = tegra_pcie_phy_enable(pcie);
921
922 if (err < 0)
923 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
924
925 return err;
926 }
927
928 list_for_each_entry(port, &pcie->ports, list) {
929 err = tegra_pcie_port_phy_power_on(port);
930 if (err < 0) {
931 dev_err(pcie->dev,
932 "failed to power on PCIe port %u PHY: %d\n",
933 port->index, err);
934 return err;
935 }
936 }
937
Stephen Warrencf5d31802016-07-25 16:02:21 -0500938 /* Configure the reference clock driver */
Stephen Warrenf8144302016-07-25 16:02:27 -0500939 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500940
941 if (soc->num_ports > 2)
Stephen Warrenf8144302016-07-25 16:02:27 -0500942 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500943
Thierry Reding6fe7c182015-11-11 18:25:59 +0100944 return 0;
945}
946
947static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
948{
949 struct tegra_pcie_port *port;
950 int err;
951
952 if (pcie->legacy_phy) {
953 if (pcie->phy)
954 err = phy_power_off(pcie->phy);
955 else
956 err = tegra_pcie_phy_disable(pcie);
957
958 if (err < 0)
959 dev_err(pcie->dev, "failed to power off PHY: %d\n",
960 err);
961
962 return err;
963 }
964
965 list_for_each_entry(port, &pcie->ports, list) {
966 err = tegra_pcie_port_phy_power_off(port);
967 if (err < 0) {
968 dev_err(pcie->dev,
969 "failed to power off PCIe port %u PHY: %d\n",
970 port->index, err);
971 return err;
972 }
973 }
974
975 return 0;
976}
977
Thierry Reding7f1f0542014-08-26 17:11:38 +0200978static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
979{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200980 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200981 struct tegra_pcie_port *port;
982 unsigned long value;
983 int err;
984
985 /* enable PLL power down */
986 if (pcie->phy) {
987 value = afi_readl(pcie, AFI_PLLE_CONTROL);
988 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
989 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
990 afi_writel(pcie, value, AFI_PLLE_CONTROL);
991 }
992
993 /* power down PCIe slot clock bias pad */
994 if (soc->has_pex_bias_ctrl)
995 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
996
997 /* configure mode and disable all ports */
998 value = afi_readl(pcie, AFI_PCIE_CONFIG);
999 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1000 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1001
1002 list_for_each_entry(port, &pcie->ports, list)
1003 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1004
1005 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1006
1007 if (soc->has_gen2) {
1008 value = afi_readl(pcie, AFI_FUSE);
1009 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1010 afi_writel(pcie, value, AFI_FUSE);
1011 } else {
1012 value = afi_readl(pcie, AFI_FUSE);
1013 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1014 afi_writel(pcie, value, AFI_FUSE);
1015 }
1016
Thierry Reding6fe7c182015-11-11 18:25:59 +01001017 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001018 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001019 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001020 return err;
1021 }
1022
Thierry Redingd1523b52013-08-09 16:49:19 +02001023 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001024 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001025
1026 /* finally enable PCIe */
1027 value = afi_readl(pcie, AFI_CONFIGURATION);
1028 value |= AFI_CONFIGURATION_EN_FPCI;
1029 afi_writel(pcie, value, AFI_CONFIGURATION);
1030
1031 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1032 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1033 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001034
1035 if (soc->has_intr_prsnt_sense)
1036 value |= AFI_INTR_EN_PRSNT_SENSE;
1037
Thierry Redingd1523b52013-08-09 16:49:19 +02001038 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1039 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1040
1041 /* don't enable MSI for now, only when needed */
1042 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1043
1044 /* disable all exceptions */
1045 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1046
1047 return 0;
1048}
1049
1050static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1051{
1052 int err;
1053
1054 /* TODO: disable and unprepare clocks? */
1055
Thierry Reding6fe7c182015-11-11 18:25:59 +01001056 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001057 if (err < 0)
Thierry Reding6fe7c182015-11-11 18:25:59 +01001058 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001059
Stephen Warren3127a6b2013-11-06 15:56:58 -07001060 reset_control_assert(pcie->pcie_xrst);
1061 reset_control_assert(pcie->afi_rst);
1062 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001063
1064 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1065
Thierry Reding077fb152014-05-28 16:49:13 +02001066 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001067 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +02001068 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001069}
1070
1071static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1072{
Thierry Redinga7fbae22016-08-15 17:31:31 +02001073 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001074 int err;
1075
Stephen Warren3127a6b2013-11-06 15:56:58 -07001076 reset_control_assert(pcie->pcie_xrst);
1077 reset_control_assert(pcie->afi_rst);
1078 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001079
1080 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1081
1082 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001083 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1084 if (err < 0)
1085 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001086
Thierry Redingd1523b52013-08-09 16:49:19 +02001087 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001088 pcie->pex_clk,
1089 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001090 if (err) {
1091 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1092 return err;
1093 }
1094
Stephen Warren3127a6b2013-11-06 15:56:58 -07001095 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001096
1097 err = clk_prepare_enable(pcie->afi_clk);
1098 if (err < 0) {
1099 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1100 return err;
1101 }
1102
Jay Agarwal94716cd2013-08-09 16:49:24 +02001103 if (soc->has_cml_clk) {
1104 err = clk_prepare_enable(pcie->cml_clk);
1105 if (err < 0) {
1106 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1107 err);
1108 return err;
1109 }
1110 }
1111
Thierry Redingd1523b52013-08-09 16:49:19 +02001112 err = clk_prepare_enable(pcie->pll_e);
1113 if (err < 0) {
1114 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1115 return err;
1116 }
1117
1118 return 0;
1119}
1120
1121static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1122{
Thierry Redinga7fbae22016-08-15 17:31:31 +02001123 const struct tegra_pcie_soc *soc = pcie->soc;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001124
Thierry Redingd1523b52013-08-09 16:49:19 +02001125 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1126 if (IS_ERR(pcie->pex_clk))
1127 return PTR_ERR(pcie->pex_clk);
1128
1129 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1130 if (IS_ERR(pcie->afi_clk))
1131 return PTR_ERR(pcie->afi_clk);
1132
Thierry Redingd1523b52013-08-09 16:49:19 +02001133 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1134 if (IS_ERR(pcie->pll_e))
1135 return PTR_ERR(pcie->pll_e);
1136
Jay Agarwal94716cd2013-08-09 16:49:24 +02001137 if (soc->has_cml_clk) {
1138 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1139 if (IS_ERR(pcie->cml_clk))
1140 return PTR_ERR(pcie->cml_clk);
1141 }
1142
Thierry Redingd1523b52013-08-09 16:49:19 +02001143 return 0;
1144}
1145
Stephen Warren3127a6b2013-11-06 15:56:58 -07001146static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1147{
1148 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1149 if (IS_ERR(pcie->pex_rst))
1150 return PTR_ERR(pcie->pex_rst);
1151
1152 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1153 if (IS_ERR(pcie->afi_rst))
1154 return PTR_ERR(pcie->afi_rst);
1155
1156 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1157 if (IS_ERR(pcie->pcie_xrst))
1158 return PTR_ERR(pcie->pcie_xrst);
1159
1160 return 0;
1161}
1162
Thierry Reding6fe7c182015-11-11 18:25:59 +01001163static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1164{
1165 int err;
1166
1167 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1168 if (IS_ERR(pcie->phy)) {
1169 err = PTR_ERR(pcie->phy);
1170 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1171 return err;
1172 }
1173
1174 err = phy_init(pcie->phy);
1175 if (err < 0) {
1176 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1177 return err;
1178 }
1179
1180 pcie->legacy_phy = true;
1181
1182 return 0;
1183}
1184
1185static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1186 struct device_node *np,
1187 const char *consumer,
1188 unsigned int index)
1189{
1190 struct phy *phy;
1191 char *name;
1192
1193 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1194 if (!name)
1195 return ERR_PTR(-ENOMEM);
1196
1197 phy = devm_of_phy_get(dev, np, name);
1198 kfree(name);
1199
1200 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1201 phy = NULL;
1202
1203 return phy;
1204}
1205
1206static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1207{
1208 struct device *dev = port->pcie->dev;
1209 struct phy *phy;
1210 unsigned int i;
1211 int err;
1212
1213 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1214 if (!port->phys)
1215 return -ENOMEM;
1216
1217 for (i = 0; i < port->lanes; i++) {
1218 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1219 if (IS_ERR(phy)) {
1220 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1221 PTR_ERR(phy));
1222 return PTR_ERR(phy);
1223 }
1224
1225 err = phy_init(phy);
1226 if (err < 0) {
1227 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1228 err);
1229 return err;
1230 }
1231
1232 port->phys[i] = phy;
1233 }
1234
1235 return 0;
1236}
1237
1238static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1239{
Thierry Redinga7fbae22016-08-15 17:31:31 +02001240 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001241 struct device_node *np = pcie->dev->of_node;
1242 struct tegra_pcie_port *port;
1243 int err;
1244
1245 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1246 return tegra_pcie_phys_get_legacy(pcie);
1247
1248 list_for_each_entry(port, &pcie->ports, list) {
1249 err = tegra_pcie_port_get_phys(port);
1250 if (err < 0)
1251 return err;
1252 }
1253
1254 return 0;
1255}
1256
Thierry Redingd1523b52013-08-09 16:49:19 +02001257static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1258{
1259 struct platform_device *pdev = to_platform_device(pcie->dev);
1260 struct resource *pads, *afi, *res;
1261 int err;
1262
1263 err = tegra_pcie_clocks_get(pcie);
1264 if (err) {
1265 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1266 return err;
1267 }
1268
Stephen Warren3127a6b2013-11-06 15:56:58 -07001269 err = tegra_pcie_resets_get(pcie);
1270 if (err) {
1271 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1272 return err;
1273 }
1274
Thierry Reding6fe7c182015-11-11 18:25:59 +01001275 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001276 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001277 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001278 return err;
1279 }
1280
Thierry Redingd1523b52013-08-09 16:49:19 +02001281 err = tegra_pcie_power_on(pcie);
1282 if (err) {
1283 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1284 return err;
1285 }
1286
Thierry Redingd1523b52013-08-09 16:49:19 +02001287 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001288 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1289 if (IS_ERR(pcie->pads)) {
1290 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001291 goto poweroff;
1292 }
1293
1294 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001295 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1296 if (IS_ERR(pcie->afi)) {
1297 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001298 goto poweroff;
1299 }
1300
Julia Lawalldc05ee32013-08-26 11:11:09 +02001301 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001302 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1303 if (!res) {
1304 err = -EADDRNOTAVAIL;
1305 goto poweroff;
1306 }
1307
1308 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1309 resource_size(res), res->name);
1310 if (!pcie->cs) {
1311 err = -EADDRNOTAVAIL;
1312 goto poweroff;
1313 }
1314
1315 /* request interrupt */
1316 err = platform_get_irq_byname(pdev, "intr");
1317 if (err < 0) {
1318 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1319 goto poweroff;
1320 }
1321
1322 pcie->irq = err;
1323
1324 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1325 if (err) {
1326 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1327 goto poweroff;
1328 }
1329
1330 return 0;
1331
1332poweroff:
1333 tegra_pcie_power_off(pcie);
1334 return err;
1335}
1336
1337static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1338{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001339 int err;
1340
Thierry Redingd1523b52013-08-09 16:49:19 +02001341 if (pcie->irq > 0)
1342 free_irq(pcie->irq, pcie);
1343
1344 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001345
1346 err = phy_exit(pcie->phy);
1347 if (err < 0)
1348 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1349
Thierry Redingd1523b52013-08-09 16:49:19 +02001350 return 0;
1351}
1352
1353static int tegra_msi_alloc(struct tegra_msi *chip)
1354{
1355 int msi;
1356
1357 mutex_lock(&chip->lock);
1358
1359 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1360 if (msi < INT_PCI_MSI_NR)
1361 set_bit(msi, chip->used);
1362 else
1363 msi = -ENOSPC;
1364
1365 mutex_unlock(&chip->lock);
1366
1367 return msi;
1368}
1369
1370static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1371{
1372 struct device *dev = chip->chip.dev;
1373
1374 mutex_lock(&chip->lock);
1375
1376 if (!test_bit(irq, chip->used))
1377 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1378 else
1379 clear_bit(irq, chip->used);
1380
1381 mutex_unlock(&chip->lock);
1382}
1383
1384static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1385{
1386 struct tegra_pcie *pcie = data;
1387 struct tegra_msi *msi = &pcie->msi;
1388 unsigned int i, processed = 0;
1389
1390 for (i = 0; i < 8; i++) {
1391 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1392
1393 while (reg) {
1394 unsigned int offset = find_first_bit(&reg, 32);
1395 unsigned int index = i * 32 + offset;
1396 unsigned int irq;
1397
1398 /* clear the interrupt */
1399 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1400
1401 irq = irq_find_mapping(msi->domain, index);
1402 if (irq) {
1403 if (test_bit(index, msi->used))
1404 generic_handle_irq(irq);
1405 else
1406 dev_info(pcie->dev, "unhandled MSI\n");
1407 } else {
1408 /*
1409 * that's weird who triggered this?
1410 * just clear it
1411 */
1412 dev_info(pcie->dev, "unexpected MSI\n");
1413 }
1414
1415 /* see if there's any more pending in this vector */
1416 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1417
1418 processed++;
1419 }
1420 }
1421
1422 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1423}
1424
Yijing Wangc2791b82014-11-11 17:45:45 -07001425static int tegra_msi_setup_irq(struct msi_controller *chip,
1426 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001427{
1428 struct tegra_msi *msi = to_tegra_msi(chip);
1429 struct msi_msg msg;
1430 unsigned int irq;
1431 int hwirq;
1432
1433 hwirq = tegra_msi_alloc(msi);
1434 if (hwirq < 0)
1435 return hwirq;
1436
1437 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001438 if (!irq) {
1439 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001440 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001441 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001442
1443 irq_set_msi_desc(irq, desc);
1444
1445 msg.address_lo = virt_to_phys((void *)msi->pages);
1446 /* 32 bit address only */
1447 msg.address_hi = 0;
1448 msg.data = hwirq;
1449
Jiang Liu83a18912014-11-09 23:10:34 +08001450 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001451
1452 return 0;
1453}
1454
Yijing Wangc2791b82014-11-11 17:45:45 -07001455static void tegra_msi_teardown_irq(struct msi_controller *chip,
1456 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001457{
1458 struct tegra_msi *msi = to_tegra_msi(chip);
1459 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001460 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001461
Jisheng Zhang019fa462014-07-29 09:33:30 +08001462 irq_dispose_mapping(irq);
1463 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001464}
1465
1466static struct irq_chip tegra_msi_irq_chip = {
1467 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001468 .irq_enable = pci_msi_unmask_irq,
1469 .irq_disable = pci_msi_mask_irq,
1470 .irq_mask = pci_msi_mask_irq,
1471 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001472};
1473
1474static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1475 irq_hw_number_t hwirq)
1476{
1477 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1478 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001479
Stephen Warrenb4f17372013-05-06 14:19:19 -06001480 tegra_cpuidle_pcie_irqs_in_use();
1481
Thierry Redingd1523b52013-08-09 16:49:19 +02001482 return 0;
1483}
1484
1485static const struct irq_domain_ops msi_domain_ops = {
1486 .map = tegra_msi_map,
1487};
1488
1489static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1490{
1491 struct platform_device *pdev = to_platform_device(pcie->dev);
Thierry Redinga7fbae22016-08-15 17:31:31 +02001492 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001493 struct tegra_msi *msi = &pcie->msi;
1494 unsigned long base;
1495 int err;
1496 u32 reg;
1497
1498 mutex_init(&msi->lock);
1499
1500 msi->chip.dev = pcie->dev;
1501 msi->chip.setup_irq = tegra_msi_setup_irq;
1502 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1503
1504 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1505 &msi_domain_ops, &msi->chip);
1506 if (!msi->domain) {
1507 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1508 return -ENOMEM;
1509 }
1510
1511 err = platform_get_irq_byname(pdev, "msi");
1512 if (err < 0) {
1513 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1514 goto err;
1515 }
1516
1517 msi->irq = err;
1518
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001519 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001520 tegra_msi_irq_chip.name, pcie);
1521 if (err < 0) {
1522 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1523 goto err;
1524 }
1525
1526 /* setup AFI/FPCI range */
1527 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1528 base = virt_to_phys((void *)msi->pages);
1529
Jay Agarwal94716cd2013-08-09 16:49:24 +02001530 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001531 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1532 /* this register is in 4K increments */
1533 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1534
1535 /* enable all MSI vectors */
1536 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1537 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1538 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1539 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1540 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1541 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1542 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1543 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1544
1545 /* and unmask the MSI interrupt */
1546 reg = afi_readl(pcie, AFI_INTR_MASK);
1547 reg |= AFI_INTR_MASK_MSI_MASK;
1548 afi_writel(pcie, reg, AFI_INTR_MASK);
1549
1550 return 0;
1551
1552err:
1553 irq_domain_remove(msi->domain);
1554 return err;
1555}
1556
1557static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1558{
1559 struct tegra_msi *msi = &pcie->msi;
1560 unsigned int i, irq;
1561 u32 value;
1562
1563 /* mask the MSI interrupt */
1564 value = afi_readl(pcie, AFI_INTR_MASK);
1565 value &= ~AFI_INTR_MASK_MSI_MASK;
1566 afi_writel(pcie, value, AFI_INTR_MASK);
1567
1568 /* disable all MSI vectors */
1569 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1570 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1571 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1572 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1573 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1574 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1575 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1576 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1577
1578 free_pages(msi->pages, 0);
1579
1580 if (msi->irq > 0)
1581 free_irq(msi->irq, pcie);
1582
1583 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1584 irq = irq_find_mapping(msi->domain, i);
1585 if (irq > 0)
1586 irq_dispose_mapping(irq);
1587 }
1588
1589 irq_domain_remove(msi->domain);
1590
1591 return 0;
1592}
1593
1594static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1595 u32 *xbar)
1596{
1597 struct device_node *np = pcie->dev->of_node;
1598
Thierry Reding7f1f0542014-08-26 17:11:38 +02001599 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1600 switch (lanes) {
1601 case 0x0000104:
1602 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1603 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1604 return 0;
1605
1606 case 0x0000102:
1607 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1608 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1609 return 0;
1610 }
1611 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001612 switch (lanes) {
1613 case 0x00000204:
1614 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1615 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1616 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001617
Jay Agarwal94716cd2013-08-09 16:49:24 +02001618 case 0x00020202:
1619 dev_info(pcie->dev, "2x3 configuration\n");
1620 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1621 return 0;
1622
1623 case 0x00010104:
1624 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1625 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1626 return 0;
1627 }
1628 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1629 switch (lanes) {
1630 case 0x00000004:
1631 dev_info(pcie->dev, "single-mode configuration\n");
1632 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1633 return 0;
1634
1635 case 0x00000202:
1636 dev_info(pcie->dev, "dual-mode configuration\n");
1637 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1638 return 0;
1639 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001640 }
1641
1642 return -EINVAL;
1643}
1644
Thierry Reding077fb152014-05-28 16:49:13 +02001645/*
1646 * Check whether a given set of supplies is available in a device tree node.
1647 * This is used to check whether the new or the legacy device tree bindings
1648 * should be used.
1649 */
1650static bool of_regulator_bulk_available(struct device_node *np,
1651 struct regulator_bulk_data *supplies,
1652 unsigned int num_supplies)
1653{
1654 char property[32];
1655 unsigned int i;
1656
1657 for (i = 0; i < num_supplies; i++) {
1658 snprintf(property, 32, "%s-supply", supplies[i].supply);
1659
1660 if (of_find_property(np, property, NULL) == NULL)
1661 return false;
1662 }
1663
1664 return true;
1665}
1666
1667/*
1668 * Old versions of the device tree binding for this device used a set of power
1669 * supplies that didn't match the hardware inputs. This happened to work for a
1670 * number of cases but is not future proof. However to preserve backwards-
1671 * compatibility with old device trees, this function will try to use the old
1672 * set of supplies.
1673 */
1674static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1675{
1676 struct device_node *np = pcie->dev->of_node;
1677
1678 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1679 pcie->num_supplies = 3;
1680 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1681 pcie->num_supplies = 2;
1682
1683 if (pcie->num_supplies == 0) {
1684 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1685 np->full_name);
1686 return -ENODEV;
1687 }
1688
1689 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1690 sizeof(*pcie->supplies),
1691 GFP_KERNEL);
1692 if (!pcie->supplies)
1693 return -ENOMEM;
1694
1695 pcie->supplies[0].supply = "pex-clk";
1696 pcie->supplies[1].supply = "vdd";
1697
1698 if (pcie->num_supplies > 2)
1699 pcie->supplies[2].supply = "avdd";
1700
1701 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1702 pcie->supplies);
1703}
1704
1705/*
1706 * Obtains the list of regulators required for a particular generation of the
1707 * IP block.
1708 *
1709 * This would've been nice to do simply by providing static tables for use
1710 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1711 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1712 * and either seems to be optional depending on which ports are being used.
1713 */
1714static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1715{
1716 struct device_node *np = pcie->dev->of_node;
1717 unsigned int i = 0;
1718
Thierry Reding7f1f0542014-08-26 17:11:38 +02001719 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1720 pcie->num_supplies = 7;
1721
1722 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1723 sizeof(*pcie->supplies),
1724 GFP_KERNEL);
1725 if (!pcie->supplies)
1726 return -ENOMEM;
1727
1728 pcie->supplies[i++].supply = "avddio-pex";
1729 pcie->supplies[i++].supply = "dvddio-pex";
1730 pcie->supplies[i++].supply = "avdd-pex-pll";
1731 pcie->supplies[i++].supply = "hvdd-pex";
1732 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1733 pcie->supplies[i++].supply = "vddio-pex-ctl";
1734 pcie->supplies[i++].supply = "avdd-pll-erefe";
1735 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001736 bool need_pexa = false, need_pexb = false;
1737
1738 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1739 if (lane_mask & 0x0f)
1740 need_pexa = true;
1741
1742 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1743 if (lane_mask & 0x30)
1744 need_pexb = true;
1745
1746 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1747 (need_pexb ? 2 : 0);
1748
1749 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1750 sizeof(*pcie->supplies),
1751 GFP_KERNEL);
1752 if (!pcie->supplies)
1753 return -ENOMEM;
1754
1755 pcie->supplies[i++].supply = "avdd-pex-pll";
1756 pcie->supplies[i++].supply = "hvdd-pex";
1757 pcie->supplies[i++].supply = "vddio-pex-ctl";
1758 pcie->supplies[i++].supply = "avdd-plle";
1759
1760 if (need_pexa) {
1761 pcie->supplies[i++].supply = "avdd-pexa";
1762 pcie->supplies[i++].supply = "vdd-pexa";
1763 }
1764
1765 if (need_pexb) {
1766 pcie->supplies[i++].supply = "avdd-pexb";
1767 pcie->supplies[i++].supply = "vdd-pexb";
1768 }
1769 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1770 pcie->num_supplies = 5;
1771
1772 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1773 sizeof(*pcie->supplies),
1774 GFP_KERNEL);
1775 if (!pcie->supplies)
1776 return -ENOMEM;
1777
1778 pcie->supplies[0].supply = "avdd-pex";
1779 pcie->supplies[1].supply = "vdd-pex";
1780 pcie->supplies[2].supply = "avdd-pex-pll";
1781 pcie->supplies[3].supply = "avdd-plle";
1782 pcie->supplies[4].supply = "vddio-pex-clk";
1783 }
1784
1785 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1786 pcie->num_supplies))
1787 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1788 pcie->supplies);
1789
1790 /*
1791 * If not all regulators are available for this new scheme, assume
1792 * that the device tree complies with an older version of the device
1793 * tree binding.
1794 */
1795 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1796
1797 devm_kfree(pcie->dev, pcie->supplies);
1798 pcie->num_supplies = 0;
1799
1800 return tegra_pcie_get_legacy_regulators(pcie);
1801}
1802
Thierry Redingd1523b52013-08-09 16:49:19 +02001803static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1804{
1805 struct device_node *np = pcie->dev->of_node, *port;
Thierry Redinga7fbae22016-08-15 17:31:31 +02001806 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001807 struct of_pci_range_parser parser;
1808 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001809 u32 lanes = 0, mask = 0;
1810 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001811 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001812 int err;
1813
1814 if (of_pci_range_parser_init(&parser, np)) {
1815 dev_err(pcie->dev, "missing \"ranges\" property\n");
1816 return -EINVAL;
1817 }
1818
Thierry Redingd1523b52013-08-09 16:49:19 +02001819 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001820 err = of_pci_range_to_resource(&range, np, &res);
1821 if (err < 0)
1822 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001823
1824 switch (res.flags & IORESOURCE_TYPE_BITS) {
1825 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001826 /* Track the bus -> CPU I/O mapping offset. */
1827 pcie->offset.io = res.start - range.pci_addr;
1828
Thierry Reding51067872014-11-27 09:54:09 +01001829 memcpy(&pcie->pio, &res, sizeof(res));
1830 pcie->pio.name = np->full_name;
1831
1832 /*
1833 * The Tegra PCIe host bridge uses this to program the
1834 * mapping of the I/O space to the physical address,
1835 * so we override the .start and .end fields here that
1836 * of_pci_range_to_resource() converted to I/O space.
1837 * We also set the IORESOURCE_MEM type to clarify that
1838 * the resource is in the physical memory space.
1839 */
1840 pcie->io.start = range.cpu_addr;
1841 pcie->io.end = range.cpu_addr + range.size - 1;
1842 pcie->io.flags = IORESOURCE_MEM;
1843 pcie->io.name = "I/O";
1844
1845 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001846 break;
1847
1848 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001849 /*
1850 * Track the bus -> CPU memory mapping offset. This
1851 * assumes that the prefetchable and non-prefetchable
1852 * regions will be the last of type IORESOURCE_MEM in
1853 * the ranges property.
1854 * */
1855 pcie->offset.mem = res.start - range.pci_addr;
1856
Thierry Redingd1523b52013-08-09 16:49:19 +02001857 if (res.flags & IORESOURCE_PREFETCH) {
1858 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001859 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001860 } else {
1861 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001862 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001863 }
1864 break;
1865 }
1866 }
1867
1868 err = of_pci_parse_bus_range(np, &pcie->busn);
1869 if (err < 0) {
1870 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1871 err);
1872 pcie->busn.name = np->name;
1873 pcie->busn.start = 0;
1874 pcie->busn.end = 0xff;
1875 pcie->busn.flags = IORESOURCE_BUS;
1876 }
1877
1878 /* parse root ports */
1879 for_each_child_of_node(np, port) {
1880 struct tegra_pcie_port *rp;
1881 unsigned int index;
1882 u32 value;
1883
1884 err = of_pci_get_devfn(port);
1885 if (err < 0) {
1886 dev_err(pcie->dev, "failed to parse address: %d\n",
1887 err);
1888 return err;
1889 }
1890
1891 index = PCI_SLOT(err);
1892
Jay Agarwal94716cd2013-08-09 16:49:24 +02001893 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001894 dev_err(pcie->dev, "invalid port number: %d\n", index);
1895 return -EINVAL;
1896 }
1897
1898 index--;
1899
1900 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1901 if (err < 0) {
1902 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1903 err);
1904 return err;
1905 }
1906
1907 if (value > 16) {
1908 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1909 return -EINVAL;
1910 }
1911
1912 lanes |= value << (index << 3);
1913
Thierry Reding077fb152014-05-28 16:49:13 +02001914 if (!of_device_is_available(port)) {
1915 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001916 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001917 }
1918
1919 mask |= ((1 << value) - 1) << lane;
1920 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001921
1922 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1923 if (!rp)
1924 return -ENOMEM;
1925
1926 err = of_address_to_resource(port, 0, &rp->regs);
1927 if (err < 0) {
1928 dev_err(pcie->dev, "failed to parse address: %d\n",
1929 err);
1930 return err;
1931 }
1932
1933 INIT_LIST_HEAD(&rp->list);
1934 rp->index = index;
1935 rp->lanes = value;
1936 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001937 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001938
Julia Lawalldc05ee32013-08-26 11:11:09 +02001939 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1940 if (IS_ERR(rp->base))
1941 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001942
1943 list_add_tail(&rp->list, &pcie->ports);
1944 }
1945
1946 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1947 if (err < 0) {
1948 dev_err(pcie->dev, "invalid lane configuration\n");
1949 return err;
1950 }
1951
Thierry Reding077fb152014-05-28 16:49:13 +02001952 err = tegra_pcie_get_regulators(pcie, mask);
1953 if (err < 0)
1954 return err;
1955
Thierry Redingd1523b52013-08-09 16:49:19 +02001956 return 0;
1957}
1958
1959/*
1960 * FIXME: If there are no PCIe cards attached, then calling this function
1961 * can result in the increase of the bootup time as there are big timeout
1962 * loops.
1963 */
1964#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1965static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1966{
1967 unsigned int retries = 3;
1968 unsigned long value;
1969
Thierry Reding7f1f0542014-08-26 17:11:38 +02001970 /* override presence detection */
1971 value = readl(port->base + RP_PRIV_MISC);
1972 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
1973 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
1974 writel(value, port->base + RP_PRIV_MISC);
1975
Thierry Redingd1523b52013-08-09 16:49:19 +02001976 do {
1977 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1978
1979 do {
1980 value = readl(port->base + RP_VEND_XP);
1981
1982 if (value & RP_VEND_XP_DL_UP)
1983 break;
1984
1985 usleep_range(1000, 2000);
1986 } while (--timeout);
1987
1988 if (!timeout) {
1989 dev_err(port->pcie->dev, "link %u down, retrying\n",
1990 port->index);
1991 goto retry;
1992 }
1993
1994 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1995
1996 do {
1997 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1998
1999 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2000 return true;
2001
2002 usleep_range(1000, 2000);
2003 } while (--timeout);
2004
2005retry:
2006 tegra_pcie_port_reset(port);
2007 } while (--retries);
2008
2009 return false;
2010}
2011
2012static int tegra_pcie_enable(struct tegra_pcie *pcie)
2013{
2014 struct tegra_pcie_port *port, *tmp;
2015 struct hw_pci hw;
2016
2017 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2018 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2019 port->index, port->lanes);
2020
2021 tegra_pcie_port_enable(port);
2022
2023 if (tegra_pcie_port_check_link(port))
2024 continue;
2025
2026 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2027
2028 tegra_pcie_port_disable(port);
2029 tegra_pcie_port_free(port);
2030 }
2031
2032 memset(&hw, 0, sizeof(hw));
2033
Yijing Wang7ec725b2014-11-11 15:35:05 -07002034#ifdef CONFIG_PCI_MSI
2035 hw.msi_ctrl = &pcie->msi.chip;
2036#endif
2037
Thierry Redingd1523b52013-08-09 16:49:19 +02002038 hw.nr_controllers = 1;
2039 hw.private_data = (void **)&pcie;
2040 hw.setup = tegra_pcie_setup;
2041 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02002042 hw.ops = &tegra_pcie_ops;
2043
2044 pci_common_init_dev(pcie->dev, &hw);
2045
2046 return 0;
2047}
2048
Thierry Redinga7fbae22016-08-15 17:31:31 +02002049static const struct tegra_pcie_soc tegra20_pcie = {
Jay Agarwal94716cd2013-08-09 16:49:24 +02002050 .num_ports = 2,
2051 .msi_base_shift = 0,
2052 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2053 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
Stephen Warrenf8144302016-07-25 16:02:27 -05002054 .pads_refclk_cfg0 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002055 .has_pex_clkreq_en = false,
2056 .has_pex_bias_ctrl = false,
2057 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002058 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002059 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002060};
2061
Thierry Redinga7fbae22016-08-15 17:31:31 +02002062static const struct tegra_pcie_soc tegra30_pcie = {
Jay Agarwal94716cd2013-08-09 16:49:24 +02002063 .num_ports = 3,
2064 .msi_base_shift = 8,
2065 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2066 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002067 .pads_refclk_cfg0 = 0xfa5cfa5c,
2068 .pads_refclk_cfg1 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002069 .has_pex_clkreq_en = true,
2070 .has_pex_bias_ctrl = true,
2071 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002072 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002073 .has_gen2 = false,
2074};
2075
Thierry Redinga7fbae22016-08-15 17:31:31 +02002076static const struct tegra_pcie_soc tegra124_pcie = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002077 .num_ports = 2,
2078 .msi_base_shift = 8,
2079 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2080 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002081 .pads_refclk_cfg0 = 0x44ac44ac,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002082 .has_pex_clkreq_en = true,
2083 .has_pex_bias_ctrl = true,
2084 .has_intr_prsnt_sense = true,
2085 .has_cml_clk = true,
2086 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002087};
2088
2089static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Redinga7fbae22016-08-15 17:31:31 +02002090 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2091 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2092 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002093 { },
2094};
Jay Agarwal94716cd2013-08-09 16:49:24 +02002095
Thierry Reding2cb989f2014-07-22 12:30:46 -06002096static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2097{
2098 struct tegra_pcie *pcie = s->private;
2099
2100 if (list_empty(&pcie->ports))
2101 return NULL;
2102
2103 seq_printf(s, "Index Status\n");
2104
2105 return seq_list_start(&pcie->ports, *pos);
2106}
2107
2108static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2109{
2110 struct tegra_pcie *pcie = s->private;
2111
2112 return seq_list_next(v, &pcie->ports, pos);
2113}
2114
2115static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2116{
2117}
2118
2119static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2120{
2121 bool up = false, active = false;
2122 struct tegra_pcie_port *port;
2123 unsigned int value;
2124
2125 port = list_entry(v, struct tegra_pcie_port, list);
2126
2127 value = readl(port->base + RP_VEND_XP);
2128
2129 if (value & RP_VEND_XP_DL_UP)
2130 up = true;
2131
2132 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2133
2134 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2135 active = true;
2136
2137 seq_printf(s, "%2u ", port->index);
2138
2139 if (up)
2140 seq_printf(s, "up");
2141
2142 if (active) {
2143 if (up)
2144 seq_printf(s, ", ");
2145
2146 seq_printf(s, "active");
2147 }
2148
2149 seq_printf(s, "\n");
2150 return 0;
2151}
2152
2153static const struct seq_operations tegra_pcie_ports_seq_ops = {
2154 .start = tegra_pcie_ports_seq_start,
2155 .next = tegra_pcie_ports_seq_next,
2156 .stop = tegra_pcie_ports_seq_stop,
2157 .show = tegra_pcie_ports_seq_show,
2158};
2159
2160static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2161{
2162 struct tegra_pcie *pcie = inode->i_private;
2163 struct seq_file *s;
2164 int err;
2165
2166 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2167 if (err)
2168 return err;
2169
2170 s = file->private_data;
2171 s->private = pcie;
2172
2173 return 0;
2174}
2175
2176static const struct file_operations tegra_pcie_ports_ops = {
2177 .owner = THIS_MODULE,
2178 .open = tegra_pcie_ports_open,
2179 .read = seq_read,
2180 .llseek = seq_lseek,
2181 .release = seq_release,
2182};
2183
2184static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2185{
2186 struct dentry *file;
2187
2188 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2189 if (!pcie->debugfs)
2190 return -ENOMEM;
2191
2192 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2193 pcie, &tegra_pcie_ports_ops);
2194 if (!file)
2195 goto remove;
2196
2197 return 0;
2198
2199remove:
2200 debugfs_remove_recursive(pcie->debugfs);
2201 pcie->debugfs = NULL;
2202 return -ENOMEM;
2203}
2204
Thierry Redingd1523b52013-08-09 16:49:19 +02002205static int tegra_pcie_probe(struct platform_device *pdev)
2206{
2207 struct tegra_pcie *pcie;
2208 int err;
2209
2210 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2211 if (!pcie)
2212 return -ENOMEM;
2213
Thierry Redingc460af92016-08-15 17:31:32 +02002214 pcie->soc = of_device_get_match_data(&pdev->dev);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002215 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002216 INIT_LIST_HEAD(&pcie->ports);
2217 pcie->dev = &pdev->dev;
2218
2219 err = tegra_pcie_parse_dt(pcie);
2220 if (err < 0)
2221 return err;
2222
Thierry Redingd1523b52013-08-09 16:49:19 +02002223 err = tegra_pcie_get_resources(pcie);
2224 if (err < 0) {
2225 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2226 return err;
2227 }
2228
2229 err = tegra_pcie_enable_controller(pcie);
2230 if (err)
2231 goto put_resources;
2232
2233 /* setup the AFI address translations */
2234 tegra_pcie_setup_translations(pcie);
2235
2236 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2237 err = tegra_pcie_enable_msi(pcie);
2238 if (err < 0) {
2239 dev_err(&pdev->dev,
2240 "failed to enable MSI support: %d\n",
2241 err);
2242 goto put_resources;
2243 }
2244 }
2245
2246 err = tegra_pcie_enable(pcie);
2247 if (err < 0) {
2248 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2249 goto disable_msi;
2250 }
2251
Thierry Reding2cb989f2014-07-22 12:30:46 -06002252 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2253 err = tegra_pcie_debugfs_init(pcie);
2254 if (err < 0)
2255 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2256 err);
2257 }
2258
Thierry Redingd1523b52013-08-09 16:49:19 +02002259 platform_set_drvdata(pdev, pcie);
2260 return 0;
2261
2262disable_msi:
2263 if (IS_ENABLED(CONFIG_PCI_MSI))
2264 tegra_pcie_disable_msi(pcie);
2265put_resources:
2266 tegra_pcie_put_resources(pcie);
2267 return err;
2268}
2269
Thierry Redingd1523b52013-08-09 16:49:19 +02002270static struct platform_driver tegra_pcie_driver = {
2271 .driver = {
2272 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002273 .of_match_table = tegra_pcie_of_match,
2274 .suppress_bind_attrs = true,
2275 },
2276 .probe = tegra_pcie_probe,
2277};
Paul Gortmakerad183272016-07-02 19:13:31 -04002278builtin_platform_driver(tegra_pcie_driver);