blob: 7689f448ff924e02ab38521b26709e7e084d9e82 [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 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26
27#include <linux/clk.h>
Thierry Reding2cb989f2014-07-22 12:30:46 -060028#include <linux/debugfs.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020029#include <linux/delay.h>
30#include <linux/export.h>
31#include <linux/interrupt.h>
32#include <linux/irq.h>
33#include <linux/irqdomain.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/msi.h>
37#include <linux/of_address.h>
38#include <linux/of_pci.h>
39#include <linux/of_platform.h>
40#include <linux/pci.h>
Thierry Reding7f1f0542014-08-26 17:11:38 +020041#include <linux/phy/phy.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020042#include <linux/platform_device.h>
Stephen Warren3127a6b2013-11-06 15:56:58 -070043#include <linux/reset.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020044#include <linux/sizes.h>
45#include <linux/slab.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020046#include <linux/vmalloc.h>
47#include <linux/regulator/consumer.h>
48
Thierry Reding306a7f92014-07-17 13:17:24 +020049#include <soc/tegra/cpuidle.h>
Thierry Reding72323982014-07-11 13:19:06 +020050#include <soc/tegra/pmc.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020051
Thierry Redingd1523b52013-08-09 16:49:19 +020052#include <asm/mach/irq.h>
53#include <asm/mach/map.h>
54#include <asm/mach/pci.h>
55
56#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020057
58/* register definitions */
59
60#define AFI_AXI_BAR0_SZ 0x00
61#define AFI_AXI_BAR1_SZ 0x04
62#define AFI_AXI_BAR2_SZ 0x08
63#define AFI_AXI_BAR3_SZ 0x0c
64#define AFI_AXI_BAR4_SZ 0x10
65#define AFI_AXI_BAR5_SZ 0x14
66
67#define AFI_AXI_BAR0_START 0x18
68#define AFI_AXI_BAR1_START 0x1c
69#define AFI_AXI_BAR2_START 0x20
70#define AFI_AXI_BAR3_START 0x24
71#define AFI_AXI_BAR4_START 0x28
72#define AFI_AXI_BAR5_START 0x2c
73
74#define AFI_FPCI_BAR0 0x30
75#define AFI_FPCI_BAR1 0x34
76#define AFI_FPCI_BAR2 0x38
77#define AFI_FPCI_BAR3 0x3c
78#define AFI_FPCI_BAR4 0x40
79#define AFI_FPCI_BAR5 0x44
80
81#define AFI_CACHE_BAR0_SZ 0x48
82#define AFI_CACHE_BAR0_ST 0x4c
83#define AFI_CACHE_BAR1_SZ 0x50
84#define AFI_CACHE_BAR1_ST 0x54
85
86#define AFI_MSI_BAR_SZ 0x60
87#define AFI_MSI_FPCI_BAR_ST 0x64
88#define AFI_MSI_AXI_BAR_ST 0x68
89
90#define AFI_MSI_VEC0 0x6c
91#define AFI_MSI_VEC1 0x70
92#define AFI_MSI_VEC2 0x74
93#define AFI_MSI_VEC3 0x78
94#define AFI_MSI_VEC4 0x7c
95#define AFI_MSI_VEC5 0x80
96#define AFI_MSI_VEC6 0x84
97#define AFI_MSI_VEC7 0x88
98
99#define AFI_MSI_EN_VEC0 0x8c
100#define AFI_MSI_EN_VEC1 0x90
101#define AFI_MSI_EN_VEC2 0x94
102#define AFI_MSI_EN_VEC3 0x98
103#define AFI_MSI_EN_VEC4 0x9c
104#define AFI_MSI_EN_VEC5 0xa0
105#define AFI_MSI_EN_VEC6 0xa4
106#define AFI_MSI_EN_VEC7 0xa8
107
108#define AFI_CONFIGURATION 0xac
109#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
110
111#define AFI_FPCI_ERROR_MASKS 0xb0
112
113#define AFI_INTR_MASK 0xb4
114#define AFI_INTR_MASK_INT_MASK (1 << 0)
115#define AFI_INTR_MASK_MSI_MASK (1 << 8)
116
117#define AFI_INTR_CODE 0xb8
118#define AFI_INTR_CODE_MASK 0xf
Thierry Reding7f1f0542014-08-26 17:11:38 +0200119#define AFI_INTR_INI_SLAVE_ERROR 1
120#define AFI_INTR_INI_DECODE_ERROR 2
Thierry Redingd1523b52013-08-09 16:49:19 +0200121#define AFI_INTR_TARGET_ABORT 3
122#define AFI_INTR_MASTER_ABORT 4
123#define AFI_INTR_INVALID_WRITE 5
124#define AFI_INTR_LEGACY 6
125#define AFI_INTR_FPCI_DECODE_ERROR 7
Thierry Reding7f1f0542014-08-26 17:11:38 +0200126#define AFI_INTR_AXI_DECODE_ERROR 8
127#define AFI_INTR_FPCI_TIMEOUT 9
128#define AFI_INTR_PE_PRSNT_SENSE 10
129#define AFI_INTR_PE_CLKREQ_SENSE 11
130#define AFI_INTR_CLKCLAMP_SENSE 12
131#define AFI_INTR_RDY4PD_SENSE 13
132#define AFI_INTR_P2P_ERROR 14
Thierry Redingd1523b52013-08-09 16:49:19 +0200133
134#define AFI_INTR_SIGNATURE 0xbc
135#define AFI_UPPER_FPCI_ADDRESS 0xc0
136#define AFI_SM_INTR_ENABLE 0xc4
137#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
138#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
139#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
140#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
141#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
142#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
143#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
144#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
145
146#define AFI_AFI_INTR_ENABLE 0xc8
147#define AFI_INTR_EN_INI_SLVERR (1 << 0)
148#define AFI_INTR_EN_INI_DECERR (1 << 1)
149#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
150#define AFI_INTR_EN_TGT_DECERR (1 << 3)
151#define AFI_INTR_EN_TGT_WRERR (1 << 4)
152#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
153#define AFI_INTR_EN_AXI_DECERR (1 << 6)
154#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200155#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200156
157#define AFI_PCIE_CONFIG 0x0f8
158#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
159#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
160#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
161#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200162#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200163#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200164#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200165#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200166#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200167#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200168
169#define AFI_FUSE 0x104
170#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
171
172#define AFI_PEX0_CTRL 0x110
173#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200174#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200175#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200176#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200177#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200178#define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
179
180#define AFI_PLLE_CONTROL 0x160
181#define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
182#define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200183
Jay Agarwal94716cd2013-08-09 16:49:24 +0200184#define AFI_PEXBIAS_CTRL_0 0x168
185
Thierry Redingacb341e2016-07-25 16:02:05 -0500186#define RP_VEND_XP 0x00000f00
Thierry Redingd1523b52013-08-09 16:49:19 +0200187#define RP_VEND_XP_DL_UP (1 << 30)
188
Thierry Redingacb341e2016-07-25 16:02:05 -0500189#define RP_PRIV_MISC 0x00000fe0
190#define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
191#define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200192
Thierry Redingd1523b52013-08-09 16:49:19 +0200193#define RP_LINK_CONTROL_STATUS 0x00000090
194#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
195#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
196
Thierry Redingacb341e2016-07-25 16:02:05 -0500197#define PADS_CTL_SEL 0x0000009c
Thierry Redingd1523b52013-08-09 16:49:19 +0200198
Thierry Redingacb341e2016-07-25 16:02:05 -0500199#define PADS_CTL 0x000000a0
Thierry Redingd1523b52013-08-09 16:49:19 +0200200#define PADS_CTL_IDDQ_1L (1 << 0)
201#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
202#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
203
Thierry Redingacb341e2016-07-25 16:02:05 -0500204#define PADS_PLL_CTL_TEGRA20 0x000000b8
205#define PADS_PLL_CTL_TEGRA30 0x000000b4
Thierry Redingd1523b52013-08-09 16:49:19 +0200206#define PADS_PLL_CTL_RST_B4SM (1 << 1)
207#define PADS_PLL_CTL_LOCKDET (1 << 8)
208#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
209#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
210#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
211#define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
212#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
213#define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
214#define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200215#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
216
Thierry Redingacb341e2016-07-25 16:02:05 -0500217#define PADS_REFCLK_CFG0 0x000000c8
218#define PADS_REFCLK_CFG1 0x000000cc
219#define PADS_REFCLK_BIAS 0x000000d0
Thierry Redingd1523b52013-08-09 16:49:19 +0200220
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200221/*
222 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
223 * entries, one entry per PCIe port. These field definitions and desired
224 * values aren't in the TRM, but do come from NVIDIA.
225 */
226#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
227#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
228#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
229#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
230
Thierry Redingd1523b52013-08-09 16:49:19 +0200231struct tegra_msi {
Yijing Wangc2791b82014-11-11 17:45:45 -0700232 struct msi_controller chip;
Thierry Redingd1523b52013-08-09 16:49:19 +0200233 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
234 struct irq_domain *domain;
235 unsigned long pages;
236 struct mutex lock;
237 int irq;
238};
239
Jay Agarwal94716cd2013-08-09 16:49:24 +0200240/* used to differentiate between Tegra SoC generations */
241struct tegra_pcie_soc_data {
242 unsigned int num_ports;
243 unsigned int msi_base_shift;
244 u32 pads_pll_ctl;
245 u32 tx_ref_sel;
Stephen Warrenf8144302016-07-25 16:02:27 -0500246 u32 pads_refclk_cfg0;
247 u32 pads_refclk_cfg1;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200248 bool has_pex_clkreq_en;
249 bool has_pex_bias_ctrl;
250 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200251 bool has_cml_clk;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200252 bool has_gen2;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200253};
254
Yijing Wangc2791b82014-11-11 17:45:45 -0700255static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
Thierry Redingd1523b52013-08-09 16:49:19 +0200256{
257 return container_of(chip, struct tegra_msi, chip);
258}
259
260struct tegra_pcie {
261 struct device *dev;
262
263 void __iomem *pads;
264 void __iomem *afi;
265 int irq;
266
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700267 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200268 struct resource *cs;
269
Thierry Reding41534e52014-08-01 14:15:11 +0200270 struct resource all;
Thierry Redingd1523b52013-08-09 16:49:19 +0200271 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100272 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200273 struct resource mem;
274 struct resource prefetch;
275 struct resource busn;
276
Thierry Reding56e75e22016-02-09 15:52:32 +0100277 struct {
278 resource_size_t mem;
279 resource_size_t io;
280 } offset;
281
Thierry Redingd1523b52013-08-09 16:49:19 +0200282 struct clk *pex_clk;
283 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200284 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200285 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200286
Stephen Warren3127a6b2013-11-06 15:56:58 -0700287 struct reset_control *pex_rst;
288 struct reset_control *afi_rst;
289 struct reset_control *pcie_xrst;
290
Thierry Reding6fe7c182015-11-11 18:25:59 +0100291 bool legacy_phy;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200292 struct phy *phy;
293
Thierry Redingd1523b52013-08-09 16:49:19 +0200294 struct tegra_msi msi;
295
296 struct list_head ports;
Thierry Redingd1523b52013-08-09 16:49:19 +0200297 u32 xbar_config;
298
Thierry Reding077fb152014-05-28 16:49:13 +0200299 struct regulator_bulk_data *supplies;
300 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200301
302 const struct tegra_pcie_soc_data *soc_data;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600303 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200304};
305
306struct tegra_pcie_port {
307 struct tegra_pcie *pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100308 struct device_node *np;
Thierry Redingd1523b52013-08-09 16:49:19 +0200309 struct list_head list;
310 struct resource regs;
311 void __iomem *base;
312 unsigned int index;
313 unsigned int lanes;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100314
315 struct phy **phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200316};
317
318struct tegra_pcie_bus {
319 struct vm_struct *area;
320 struct list_head list;
321 unsigned int nr;
322};
323
324static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
325{
326 return sys->private_data;
327}
328
329static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
330 unsigned long offset)
331{
332 writel(value, pcie->afi + offset);
333}
334
335static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
336{
337 return readl(pcie->afi + offset);
338}
339
340static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
341 unsigned long offset)
342{
343 writel(value, pcie->pads + offset);
344}
345
346static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
347{
348 return readl(pcie->pads + offset);
349}
350
351/*
352 * The configuration space mapping on Tegra is somewhat similar to the ECAM
353 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
354 * register accesses are mapped:
355 *
356 * [27:24] extended register number
357 * [23:16] bus number
358 * [15:11] device number
359 * [10: 8] function number
360 * [ 7: 0] register number
361 *
362 * Mapping the whole extended configuration space would require 256 MiB of
363 * virtual address space, only a small part of which will actually be used.
364 * To work around this, a 1 MiB of virtual addresses are allocated per bus
365 * when the bus is first accessed. When the physical range is mapped, the
366 * the bus number bits are hidden so that the extended register number bits
367 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
368 *
369 * [19:16] extended register number
370 * [15:11] device number
371 * [10: 8] function number
372 * [ 7: 0] register number
373 *
374 * This is achieved by stitching together 16 chunks of 64 KiB of physical
375 * address space via the MMU.
376 */
377static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
378{
379 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
380 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
381}
382
383static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
384 unsigned int busnr)
385{
Ard Biesheuvel512ee2f2015-08-26 10:49:34 +0200386 pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
387 L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
Thierry Redingd1523b52013-08-09 16:49:19 +0200388 phys_addr_t cs = pcie->cs->start;
389 struct tegra_pcie_bus *bus;
390 unsigned int i;
391 int err;
392
393 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
394 if (!bus)
395 return ERR_PTR(-ENOMEM);
396
397 INIT_LIST_HEAD(&bus->list);
398 bus->nr = busnr;
399
400 /* allocate 1 MiB of virtual addresses */
401 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
402 if (!bus->area) {
403 err = -ENOMEM;
404 goto free;
405 }
406
407 /* map each of the 16 chunks of 64 KiB each */
408 for (i = 0; i < 16; i++) {
409 unsigned long virt = (unsigned long)bus->area->addr +
410 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200411 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200412
413 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
414 if (err < 0) {
415 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
416 err);
417 goto unmap;
418 }
419 }
420
421 return bus;
422
423unmap:
424 vunmap(bus->area->addr);
425free:
426 kfree(bus);
427 return ERR_PTR(err);
428}
429
Thierry Redingb4d18d72016-02-09 15:30:48 +0100430static int tegra_pcie_add_bus(struct pci_bus *bus)
Thierry Redingd1523b52013-08-09 16:49:19 +0200431{
Thierry Redingb4d18d72016-02-09 15:30:48 +0100432 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
433 struct tegra_pcie_bus *b;
Thierry Redingd1523b52013-08-09 16:49:19 +0200434
Thierry Redingb4d18d72016-02-09 15:30:48 +0100435 b = tegra_pcie_bus_alloc(pcie, bus->number);
436 if (IS_ERR(b))
437 return PTR_ERR(b);
Thierry Redingd1523b52013-08-09 16:49:19 +0200438
Thierry Redingb4d18d72016-02-09 15:30:48 +0100439 list_add_tail(&b->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200440
Thierry Redingb4d18d72016-02-09 15:30:48 +0100441 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200442}
443
Thierry Redingb4d18d72016-02-09 15:30:48 +0100444static void tegra_pcie_remove_bus(struct pci_bus *child)
445{
446 struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
447 struct tegra_pcie_bus *bus, *tmp;
448
449 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
450 if (bus->nr == child->number) {
451 vunmap(bus->area->addr);
452 list_del(&bus->list);
453 kfree(bus);
454 break;
455 }
456 }
457}
458
459static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
460 unsigned int devfn,
461 int where)
Thierry Redingd1523b52013-08-09 16:49:19 +0200462{
463 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
464 void __iomem *addr = NULL;
465
466 if (bus->number == 0) {
467 unsigned int slot = PCI_SLOT(devfn);
468 struct tegra_pcie_port *port;
469
470 list_for_each_entry(port, &pcie->ports, list) {
471 if (port->index + 1 == slot) {
472 addr = port->base + (where & ~3);
473 break;
474 }
475 }
476 } else {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100477 struct tegra_pcie_bus *b;
478
479 list_for_each_entry(b, &pcie->buses, list)
480 if (b->nr == bus->number)
481 addr = (void __iomem *)b->area->addr;
482
Thierry Redingd1523b52013-08-09 16:49:19 +0200483 if (!addr) {
484 dev_err(pcie->dev,
485 "failed to map cfg. space for bus %u\n",
486 bus->number);
487 return NULL;
488 }
489
490 addr += tegra_pcie_conf_offset(devfn, where);
491 }
492
493 return addr;
494}
495
Thierry Redingd1523b52013-08-09 16:49:19 +0200496static struct pci_ops tegra_pcie_ops = {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100497 .add_bus = tegra_pcie_add_bus,
498 .remove_bus = tegra_pcie_remove_bus,
499 .map_bus = tegra_pcie_map_bus,
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600500 .read = pci_generic_config_read32,
501 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200502};
503
504static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
505{
506 unsigned long ret = 0;
507
508 switch (port->index) {
509 case 0:
510 ret = AFI_PEX0_CTRL;
511 break;
512
513 case 1:
514 ret = AFI_PEX1_CTRL;
515 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200516
517 case 2:
518 ret = AFI_PEX2_CTRL;
519 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200520 }
521
522 return ret;
523}
524
525static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
526{
527 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
528 unsigned long value;
529
530 /* pulse reset signal */
531 value = afi_readl(port->pcie, ctrl);
532 value &= ~AFI_PEX_CTRL_RST;
533 afi_writel(port->pcie, value, ctrl);
534
535 usleep_range(1000, 2000);
536
537 value = afi_readl(port->pcie, ctrl);
538 value |= AFI_PEX_CTRL_RST;
539 afi_writel(port->pcie, value, ctrl);
540}
541
542static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
543{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200544 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200545 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
546 unsigned long value;
547
548 /* enable reference clock */
549 value = afi_readl(port->pcie, ctrl);
550 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200551
552 if (soc->has_pex_clkreq_en)
553 value |= AFI_PEX_CTRL_CLKREQ_EN;
554
Thierry Reding7f1f0542014-08-26 17:11:38 +0200555 value |= AFI_PEX_CTRL_OVERRIDE_EN;
556
Thierry Redingd1523b52013-08-09 16:49:19 +0200557 afi_writel(port->pcie, value, ctrl);
558
559 tegra_pcie_port_reset(port);
560}
561
562static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
563{
Thierry Reding0d20d622014-08-26 17:11:35 +0200564 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200565 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
566 unsigned long value;
567
568 /* assert port reset */
569 value = afi_readl(port->pcie, ctrl);
570 value &= ~AFI_PEX_CTRL_RST;
571 afi_writel(port->pcie, value, ctrl);
572
573 /* disable reference clock */
574 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200575
576 if (soc->has_pex_clkreq_en)
577 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
578
Thierry Redingd1523b52013-08-09 16:49:19 +0200579 value &= ~AFI_PEX_CTRL_REFCLK_EN;
580 afi_writel(port->pcie, value, ctrl);
581}
582
583static void tegra_pcie_port_free(struct tegra_pcie_port *port)
584{
585 struct tegra_pcie *pcie = port->pcie;
586
587 devm_iounmap(pcie->dev, port->base);
588 devm_release_mem_region(pcie->dev, port->regs.start,
589 resource_size(&port->regs));
590 list_del(&port->list);
591 devm_kfree(pcie->dev, port);
592}
593
Thierry Redingd1523b52013-08-09 16:49:19 +0200594/* Tegra PCIE root complex wrongly reports device class */
595static void tegra_pcie_fixup_class(struct pci_dev *dev)
596{
597 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
598}
599DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
600DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200601DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
602DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200603
604/* Tegra PCIE requires relaxed ordering */
605static void tegra_pcie_relax_enable(struct pci_dev *dev)
606{
607 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
608}
609DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
610
611static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
612{
613 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200614 int err;
615
Thierry Reding56e75e22016-02-09 15:52:32 +0100616 sys->mem_offset = pcie->offset.mem;
617 sys->io_offset = pcie->offset.io;
618
619 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->io);
620 if (err < 0)
621 return err;
622
623 err = devm_request_resource(pcie->dev, &ioport_resource, &pcie->pio);
624 if (err < 0)
625 return err;
626
Thierry Reding41534e52014-08-01 14:15:11 +0200627 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
628 if (err < 0)
629 return err;
630
631 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
632 if (err)
633 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200634
Thierry Reding56e75e22016-02-09 15:52:32 +0100635 pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
Thierry Redingd1523b52013-08-09 16:49:19 +0200636 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
637 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
638 sys->mem_offset);
639 pci_add_resource(&sys->resources, &pcie->busn);
640
Thierry Redinge6e9f472016-07-25 16:02:12 -0500641 pci_remap_iospace(&pcie->pio, pcie->io.start);
Thierry Redingd1523b52013-08-09 16:49:19 +0200642
643 return 1;
644}
645
646static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
647{
648 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600649 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200650
Stephen Warrenb4f17372013-05-06 14:19:19 -0600651 tegra_cpuidle_pcie_irqs_in_use();
652
Lucas Stachf5d33522014-04-16 10:24:32 -0600653 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
654 if (!irq)
655 irq = pcie->irq;
656
657 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200658}
659
Thierry Redingd1523b52013-08-09 16:49:19 +0200660static irqreturn_t tegra_pcie_isr(int irq, void *arg)
661{
662 const char *err_msg[] = {
663 "Unknown",
664 "AXI slave error",
665 "AXI decode error",
666 "Target abort",
667 "Master abort",
668 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200669 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200670 "Response decoding error",
671 "AXI response decoding error",
672 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200673 "Slot present pin change",
674 "Slot clock request change",
675 "TMS clock ramp change",
676 "TMS ready for power down",
677 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200678 };
679 struct tegra_pcie *pcie = arg;
680 u32 code, signature;
681
682 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
683 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
684 afi_writel(pcie, 0, AFI_INTR_CODE);
685
686 if (code == AFI_INTR_LEGACY)
687 return IRQ_NONE;
688
689 if (code >= ARRAY_SIZE(err_msg))
690 code = 0;
691
692 /*
693 * do not pollute kernel log with master abort reports since they
694 * happen a lot during enumeration
695 */
696 if (code == AFI_INTR_MASTER_ABORT)
697 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
698 signature);
699 else
700 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
701 signature);
702
703 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
704 code == AFI_INTR_FPCI_DECODE_ERROR) {
705 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
706 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
707
708 if (code == AFI_INTR_MASTER_ABORT)
709 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
710 else
711 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
712 }
713
714 return IRQ_HANDLED;
715}
716
717/*
718 * FPCI map is as follows:
719 * - 0xfdfc000000: I/O space
720 * - 0xfdfe000000: type 0 configuration space
721 * - 0xfdff000000: type 1 configuration space
722 * - 0xfe00000000: type 0 extended configuration space
723 * - 0xfe10000000: type 1 extended configuration space
724 */
725static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
726{
727 u32 fpci_bar, size, axi_address;
728
729 /* Bar 0: type 1 extended configuration space */
730 fpci_bar = 0xfe100000;
731 size = resource_size(pcie->cs);
732 axi_address = pcie->cs->start;
733 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
734 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
735 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
736
737 /* Bar 1: downstream IO bar */
738 fpci_bar = 0xfdfc0000;
739 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100740 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200741 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
742 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
743 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
744
745 /* Bar 2: prefetchable memory BAR */
746 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
747 size = resource_size(&pcie->prefetch);
748 axi_address = pcie->prefetch.start;
749 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
750 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
751 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
752
753 /* Bar 3: non prefetchable memory BAR */
754 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
755 size = resource_size(&pcie->mem);
756 axi_address = pcie->mem.start;
757 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
758 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
759 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
760
761 /* NULL out the remaining BARs as they are not used */
762 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
763 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
764 afi_writel(pcie, 0, AFI_FPCI_BAR4);
765
766 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
767 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
768 afi_writel(pcie, 0, AFI_FPCI_BAR5);
769
770 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100771 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200772 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
773 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
774 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
775
776 /* MSI translations are setup only when needed */
777 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
778 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
779 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
780 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
781}
782
Thierry Reding7f1f0542014-08-26 17:11:38 +0200783static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200784{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200785 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200786 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200787
Thierry Reding7f1f0542014-08-26 17:11:38 +0200788 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200789
Thierry Reding7f1f0542014-08-26 17:11:38 +0200790 while (time_before(jiffies, timeout)) {
791 value = pads_readl(pcie, soc->pads_pll_ctl);
792 if (value & PADS_PLL_CTL_LOCKDET)
793 return 0;
794 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200795
Thierry Reding7f1f0542014-08-26 17:11:38 +0200796 return -ETIMEDOUT;
797}
Thierry Redingd1523b52013-08-09 16:49:19 +0200798
Thierry Reding7f1f0542014-08-26 17:11:38 +0200799static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
800{
801 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
802 u32 value;
803 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200804
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700805 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200806 pads_writel(pcie, 0x0, PADS_CTL_SEL);
807
808 /* override IDDQ to 1 on all 4 lanes */
809 value = pads_readl(pcie, PADS_CTL);
810 value |= PADS_CTL_IDDQ_1L;
811 pads_writel(pcie, value, PADS_CTL);
812
813 /*
814 * Set up PHY PLL inputs select PLLE output as refclock,
815 * set TX ref sel to div10 (not div5).
816 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200817 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200818 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200819 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
820 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200821
Eric Yuenec732762014-08-26 17:11:37 +0200822 /* reset PLL */
823 value = pads_readl(pcie, soc->pads_pll_ctl);
824 value &= ~PADS_PLL_CTL_RST_B4SM;
825 pads_writel(pcie, value, soc->pads_pll_ctl);
826
827 usleep_range(20, 100);
828
Thierry Redingd1523b52013-08-09 16:49:19 +0200829 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200830 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200831 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200832 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200833
Thierry Redingd1523b52013-08-09 16:49:19 +0200834 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200835 err = tegra_pcie_pll_wait(pcie, 500);
836 if (err < 0) {
837 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
838 return err;
839 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200840
841 /* turn off IDDQ override */
842 value = pads_readl(pcie, PADS_CTL);
843 value &= ~PADS_CTL_IDDQ_1L;
844 pads_writel(pcie, value, PADS_CTL);
845
846 /* enable TX/RX data */
847 value = pads_readl(pcie, PADS_CTL);
848 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
849 pads_writel(pcie, value, PADS_CTL);
850
Thierry Reding7f1f0542014-08-26 17:11:38 +0200851 return 0;
852}
853
Thierry Reding6fe7c182015-11-11 18:25:59 +0100854static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
855{
856 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
857 u32 value;
858
859 /* disable TX/RX data */
860 value = pads_readl(pcie, PADS_CTL);
861 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
862 pads_writel(pcie, value, PADS_CTL);
863
864 /* override IDDQ */
865 value = pads_readl(pcie, PADS_CTL);
866 value |= PADS_CTL_IDDQ_1L;
867 pads_writel(pcie, PADS_CTL, value);
868
869 /* reset PLL */
870 value = pads_readl(pcie, soc->pads_pll_ctl);
871 value &= ~PADS_PLL_CTL_RST_B4SM;
872 pads_writel(pcie, value, soc->pads_pll_ctl);
873
874 usleep_range(20, 100);
875
876 return 0;
877}
878
879static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
880{
881 struct device *dev = port->pcie->dev;
882 unsigned int i;
883 int err;
884
885 for (i = 0; i < port->lanes; i++) {
886 err = phy_power_on(port->phys[i]);
887 if (err < 0) {
888 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
889 err);
890 return err;
891 }
892 }
893
894 return 0;
895}
896
897static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
898{
899 struct device *dev = port->pcie->dev;
900 unsigned int i;
901 int err;
902
903 for (i = 0; i < port->lanes; i++) {
904 err = phy_power_off(port->phys[i]);
905 if (err < 0) {
906 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
907 err);
908 return err;
909 }
910 }
911
912 return 0;
913}
914
915static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
916{
Stephen Warrencf5d31802016-07-25 16:02:21 -0500917 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100918 struct tegra_pcie_port *port;
919 int err;
920
921 if (pcie->legacy_phy) {
922 if (pcie->phy)
923 err = phy_power_on(pcie->phy);
924 else
925 err = tegra_pcie_phy_enable(pcie);
926
927 if (err < 0)
928 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
929
930 return err;
931 }
932
933 list_for_each_entry(port, &pcie->ports, list) {
934 err = tegra_pcie_port_phy_power_on(port);
935 if (err < 0) {
936 dev_err(pcie->dev,
937 "failed to power on PCIe port %u PHY: %d\n",
938 port->index, err);
939 return err;
940 }
941 }
942
Stephen Warrencf5d31802016-07-25 16:02:21 -0500943 /* Configure the reference clock driver */
Stephen Warrenf8144302016-07-25 16:02:27 -0500944 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500945
946 if (soc->num_ports > 2)
Stephen Warrenf8144302016-07-25 16:02:27 -0500947 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500948
Thierry Reding6fe7c182015-11-11 18:25:59 +0100949 return 0;
950}
951
952static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
953{
954 struct tegra_pcie_port *port;
955 int err;
956
957 if (pcie->legacy_phy) {
958 if (pcie->phy)
959 err = phy_power_off(pcie->phy);
960 else
961 err = tegra_pcie_phy_disable(pcie);
962
963 if (err < 0)
964 dev_err(pcie->dev, "failed to power off PHY: %d\n",
965 err);
966
967 return err;
968 }
969
970 list_for_each_entry(port, &pcie->ports, list) {
971 err = tegra_pcie_port_phy_power_off(port);
972 if (err < 0) {
973 dev_err(pcie->dev,
974 "failed to power off PCIe port %u PHY: %d\n",
975 port->index, err);
976 return err;
977 }
978 }
979
980 return 0;
981}
982
Thierry Reding7f1f0542014-08-26 17:11:38 +0200983static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
984{
985 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
986 struct tegra_pcie_port *port;
987 unsigned long value;
988 int err;
989
990 /* enable PLL power down */
991 if (pcie->phy) {
992 value = afi_readl(pcie, AFI_PLLE_CONTROL);
993 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
994 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
995 afi_writel(pcie, value, AFI_PLLE_CONTROL);
996 }
997
998 /* power down PCIe slot clock bias pad */
999 if (soc->has_pex_bias_ctrl)
1000 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1001
1002 /* configure mode and disable all ports */
1003 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1004 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1005 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1006
1007 list_for_each_entry(port, &pcie->ports, list)
1008 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1009
1010 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1011
1012 if (soc->has_gen2) {
1013 value = afi_readl(pcie, AFI_FUSE);
1014 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1015 afi_writel(pcie, value, AFI_FUSE);
1016 } else {
1017 value = afi_readl(pcie, AFI_FUSE);
1018 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1019 afi_writel(pcie, value, AFI_FUSE);
1020 }
1021
Thierry Reding6fe7c182015-11-11 18:25:59 +01001022 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001023 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001024 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001025 return err;
1026 }
1027
Thierry Redingd1523b52013-08-09 16:49:19 +02001028 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001029 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001030
1031 /* finally enable PCIe */
1032 value = afi_readl(pcie, AFI_CONFIGURATION);
1033 value |= AFI_CONFIGURATION_EN_FPCI;
1034 afi_writel(pcie, value, AFI_CONFIGURATION);
1035
1036 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1037 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1038 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001039
1040 if (soc->has_intr_prsnt_sense)
1041 value |= AFI_INTR_EN_PRSNT_SENSE;
1042
Thierry Redingd1523b52013-08-09 16:49:19 +02001043 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1044 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1045
1046 /* don't enable MSI for now, only when needed */
1047 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1048
1049 /* disable all exceptions */
1050 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1051
1052 return 0;
1053}
1054
1055static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1056{
1057 int err;
1058
1059 /* TODO: disable and unprepare clocks? */
1060
Thierry Reding6fe7c182015-11-11 18:25:59 +01001061 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001062 if (err < 0)
Thierry Reding6fe7c182015-11-11 18:25:59 +01001063 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001064
Stephen Warren3127a6b2013-11-06 15:56:58 -07001065 reset_control_assert(pcie->pcie_xrst);
1066 reset_control_assert(pcie->afi_rst);
1067 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001068
1069 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1070
Thierry Reding077fb152014-05-28 16:49:13 +02001071 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001072 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +02001073 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001074}
1075
1076static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1077{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001078 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001079 int err;
1080
Stephen Warren3127a6b2013-11-06 15:56:58 -07001081 reset_control_assert(pcie->pcie_xrst);
1082 reset_control_assert(pcie->afi_rst);
1083 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001084
1085 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1086
1087 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001088 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1089 if (err < 0)
1090 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001091
Thierry Redingd1523b52013-08-09 16:49:19 +02001092 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001093 pcie->pex_clk,
1094 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001095 if (err) {
1096 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1097 return err;
1098 }
1099
Stephen Warren3127a6b2013-11-06 15:56:58 -07001100 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001101
1102 err = clk_prepare_enable(pcie->afi_clk);
1103 if (err < 0) {
1104 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1105 return err;
1106 }
1107
Jay Agarwal94716cd2013-08-09 16:49:24 +02001108 if (soc->has_cml_clk) {
1109 err = clk_prepare_enable(pcie->cml_clk);
1110 if (err < 0) {
1111 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1112 err);
1113 return err;
1114 }
1115 }
1116
Thierry Redingd1523b52013-08-09 16:49:19 +02001117 err = clk_prepare_enable(pcie->pll_e);
1118 if (err < 0) {
1119 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1120 return err;
1121 }
1122
1123 return 0;
1124}
1125
1126static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1127{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001128 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1129
Thierry Redingd1523b52013-08-09 16:49:19 +02001130 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1131 if (IS_ERR(pcie->pex_clk))
1132 return PTR_ERR(pcie->pex_clk);
1133
1134 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1135 if (IS_ERR(pcie->afi_clk))
1136 return PTR_ERR(pcie->afi_clk);
1137
Thierry Redingd1523b52013-08-09 16:49:19 +02001138 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1139 if (IS_ERR(pcie->pll_e))
1140 return PTR_ERR(pcie->pll_e);
1141
Jay Agarwal94716cd2013-08-09 16:49:24 +02001142 if (soc->has_cml_clk) {
1143 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1144 if (IS_ERR(pcie->cml_clk))
1145 return PTR_ERR(pcie->cml_clk);
1146 }
1147
Thierry Redingd1523b52013-08-09 16:49:19 +02001148 return 0;
1149}
1150
Stephen Warren3127a6b2013-11-06 15:56:58 -07001151static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1152{
1153 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1154 if (IS_ERR(pcie->pex_rst))
1155 return PTR_ERR(pcie->pex_rst);
1156
1157 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1158 if (IS_ERR(pcie->afi_rst))
1159 return PTR_ERR(pcie->afi_rst);
1160
1161 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1162 if (IS_ERR(pcie->pcie_xrst))
1163 return PTR_ERR(pcie->pcie_xrst);
1164
1165 return 0;
1166}
1167
Thierry Reding6fe7c182015-11-11 18:25:59 +01001168static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1169{
1170 int err;
1171
1172 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1173 if (IS_ERR(pcie->phy)) {
1174 err = PTR_ERR(pcie->phy);
1175 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1176 return err;
1177 }
1178
1179 err = phy_init(pcie->phy);
1180 if (err < 0) {
1181 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1182 return err;
1183 }
1184
1185 pcie->legacy_phy = true;
1186
1187 return 0;
1188}
1189
1190static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1191 struct device_node *np,
1192 const char *consumer,
1193 unsigned int index)
1194{
1195 struct phy *phy;
1196 char *name;
1197
1198 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1199 if (!name)
1200 return ERR_PTR(-ENOMEM);
1201
1202 phy = devm_of_phy_get(dev, np, name);
1203 kfree(name);
1204
1205 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1206 phy = NULL;
1207
1208 return phy;
1209}
1210
1211static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1212{
1213 struct device *dev = port->pcie->dev;
1214 struct phy *phy;
1215 unsigned int i;
1216 int err;
1217
1218 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1219 if (!port->phys)
1220 return -ENOMEM;
1221
1222 for (i = 0; i < port->lanes; i++) {
1223 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1224 if (IS_ERR(phy)) {
1225 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1226 PTR_ERR(phy));
1227 return PTR_ERR(phy);
1228 }
1229
1230 err = phy_init(phy);
1231 if (err < 0) {
1232 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1233 err);
1234 return err;
1235 }
1236
1237 port->phys[i] = phy;
1238 }
1239
1240 return 0;
1241}
1242
1243static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1244{
1245 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1246 struct device_node *np = pcie->dev->of_node;
1247 struct tegra_pcie_port *port;
1248 int err;
1249
1250 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1251 return tegra_pcie_phys_get_legacy(pcie);
1252
1253 list_for_each_entry(port, &pcie->ports, list) {
1254 err = tegra_pcie_port_get_phys(port);
1255 if (err < 0)
1256 return err;
1257 }
1258
1259 return 0;
1260}
1261
Thierry Redingd1523b52013-08-09 16:49:19 +02001262static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1263{
1264 struct platform_device *pdev = to_platform_device(pcie->dev);
1265 struct resource *pads, *afi, *res;
1266 int err;
1267
1268 err = tegra_pcie_clocks_get(pcie);
1269 if (err) {
1270 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1271 return err;
1272 }
1273
Stephen Warren3127a6b2013-11-06 15:56:58 -07001274 err = tegra_pcie_resets_get(pcie);
1275 if (err) {
1276 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1277 return err;
1278 }
1279
Thierry Reding6fe7c182015-11-11 18:25:59 +01001280 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001281 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001282 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001283 return err;
1284 }
1285
Thierry Redingd1523b52013-08-09 16:49:19 +02001286 err = tegra_pcie_power_on(pcie);
1287 if (err) {
1288 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1289 return err;
1290 }
1291
Thierry Redingd1523b52013-08-09 16:49:19 +02001292 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001293 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1294 if (IS_ERR(pcie->pads)) {
1295 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001296 goto poweroff;
1297 }
1298
1299 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001300 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1301 if (IS_ERR(pcie->afi)) {
1302 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001303 goto poweroff;
1304 }
1305
Julia Lawalldc05ee32013-08-26 11:11:09 +02001306 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001307 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1308 if (!res) {
1309 err = -EADDRNOTAVAIL;
1310 goto poweroff;
1311 }
1312
1313 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1314 resource_size(res), res->name);
1315 if (!pcie->cs) {
1316 err = -EADDRNOTAVAIL;
1317 goto poweroff;
1318 }
1319
1320 /* request interrupt */
1321 err = platform_get_irq_byname(pdev, "intr");
1322 if (err < 0) {
1323 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1324 goto poweroff;
1325 }
1326
1327 pcie->irq = err;
1328
1329 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1330 if (err) {
1331 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1332 goto poweroff;
1333 }
1334
1335 return 0;
1336
1337poweroff:
1338 tegra_pcie_power_off(pcie);
1339 return err;
1340}
1341
1342static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1343{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001344 int err;
1345
Thierry Redingd1523b52013-08-09 16:49:19 +02001346 if (pcie->irq > 0)
1347 free_irq(pcie->irq, pcie);
1348
1349 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001350
1351 err = phy_exit(pcie->phy);
1352 if (err < 0)
1353 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1354
Thierry Redingd1523b52013-08-09 16:49:19 +02001355 return 0;
1356}
1357
1358static int tegra_msi_alloc(struct tegra_msi *chip)
1359{
1360 int msi;
1361
1362 mutex_lock(&chip->lock);
1363
1364 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1365 if (msi < INT_PCI_MSI_NR)
1366 set_bit(msi, chip->used);
1367 else
1368 msi = -ENOSPC;
1369
1370 mutex_unlock(&chip->lock);
1371
1372 return msi;
1373}
1374
1375static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1376{
1377 struct device *dev = chip->chip.dev;
1378
1379 mutex_lock(&chip->lock);
1380
1381 if (!test_bit(irq, chip->used))
1382 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1383 else
1384 clear_bit(irq, chip->used);
1385
1386 mutex_unlock(&chip->lock);
1387}
1388
1389static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1390{
1391 struct tegra_pcie *pcie = data;
1392 struct tegra_msi *msi = &pcie->msi;
1393 unsigned int i, processed = 0;
1394
1395 for (i = 0; i < 8; i++) {
1396 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1397
1398 while (reg) {
1399 unsigned int offset = find_first_bit(&reg, 32);
1400 unsigned int index = i * 32 + offset;
1401 unsigned int irq;
1402
1403 /* clear the interrupt */
1404 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1405
1406 irq = irq_find_mapping(msi->domain, index);
1407 if (irq) {
1408 if (test_bit(index, msi->used))
1409 generic_handle_irq(irq);
1410 else
1411 dev_info(pcie->dev, "unhandled MSI\n");
1412 } else {
1413 /*
1414 * that's weird who triggered this?
1415 * just clear it
1416 */
1417 dev_info(pcie->dev, "unexpected MSI\n");
1418 }
1419
1420 /* see if there's any more pending in this vector */
1421 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1422
1423 processed++;
1424 }
1425 }
1426
1427 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1428}
1429
Yijing Wangc2791b82014-11-11 17:45:45 -07001430static int tegra_msi_setup_irq(struct msi_controller *chip,
1431 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001432{
1433 struct tegra_msi *msi = to_tegra_msi(chip);
1434 struct msi_msg msg;
1435 unsigned int irq;
1436 int hwirq;
1437
1438 hwirq = tegra_msi_alloc(msi);
1439 if (hwirq < 0)
1440 return hwirq;
1441
1442 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001443 if (!irq) {
1444 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001445 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001446 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001447
1448 irq_set_msi_desc(irq, desc);
1449
1450 msg.address_lo = virt_to_phys((void *)msi->pages);
1451 /* 32 bit address only */
1452 msg.address_hi = 0;
1453 msg.data = hwirq;
1454
Jiang Liu83a18912014-11-09 23:10:34 +08001455 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001456
1457 return 0;
1458}
1459
Yijing Wangc2791b82014-11-11 17:45:45 -07001460static void tegra_msi_teardown_irq(struct msi_controller *chip,
1461 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001462{
1463 struct tegra_msi *msi = to_tegra_msi(chip);
1464 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001465 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001466
Jisheng Zhang019fa462014-07-29 09:33:30 +08001467 irq_dispose_mapping(irq);
1468 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001469}
1470
1471static struct irq_chip tegra_msi_irq_chip = {
1472 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001473 .irq_enable = pci_msi_unmask_irq,
1474 .irq_disable = pci_msi_mask_irq,
1475 .irq_mask = pci_msi_mask_irq,
1476 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001477};
1478
1479static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1480 irq_hw_number_t hwirq)
1481{
1482 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1483 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001484
Stephen Warrenb4f17372013-05-06 14:19:19 -06001485 tegra_cpuidle_pcie_irqs_in_use();
1486
Thierry Redingd1523b52013-08-09 16:49:19 +02001487 return 0;
1488}
1489
1490static const struct irq_domain_ops msi_domain_ops = {
1491 .map = tegra_msi_map,
1492};
1493
1494static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1495{
1496 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001497 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001498 struct tegra_msi *msi = &pcie->msi;
1499 unsigned long base;
1500 int err;
1501 u32 reg;
1502
1503 mutex_init(&msi->lock);
1504
1505 msi->chip.dev = pcie->dev;
1506 msi->chip.setup_irq = tegra_msi_setup_irq;
1507 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1508
1509 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1510 &msi_domain_ops, &msi->chip);
1511 if (!msi->domain) {
1512 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1513 return -ENOMEM;
1514 }
1515
1516 err = platform_get_irq_byname(pdev, "msi");
1517 if (err < 0) {
1518 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1519 goto err;
1520 }
1521
1522 msi->irq = err;
1523
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001524 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001525 tegra_msi_irq_chip.name, pcie);
1526 if (err < 0) {
1527 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1528 goto err;
1529 }
1530
1531 /* setup AFI/FPCI range */
1532 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1533 base = virt_to_phys((void *)msi->pages);
1534
Jay Agarwal94716cd2013-08-09 16:49:24 +02001535 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001536 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1537 /* this register is in 4K increments */
1538 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1539
1540 /* enable all MSI vectors */
1541 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1542 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1543 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1544 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1545 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1546 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1547 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1548 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1549
1550 /* and unmask the MSI interrupt */
1551 reg = afi_readl(pcie, AFI_INTR_MASK);
1552 reg |= AFI_INTR_MASK_MSI_MASK;
1553 afi_writel(pcie, reg, AFI_INTR_MASK);
1554
1555 return 0;
1556
1557err:
1558 irq_domain_remove(msi->domain);
1559 return err;
1560}
1561
1562static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1563{
1564 struct tegra_msi *msi = &pcie->msi;
1565 unsigned int i, irq;
1566 u32 value;
1567
1568 /* mask the MSI interrupt */
1569 value = afi_readl(pcie, AFI_INTR_MASK);
1570 value &= ~AFI_INTR_MASK_MSI_MASK;
1571 afi_writel(pcie, value, AFI_INTR_MASK);
1572
1573 /* disable all MSI vectors */
1574 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1575 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1576 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1577 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1578 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1579 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1580 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1581 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1582
1583 free_pages(msi->pages, 0);
1584
1585 if (msi->irq > 0)
1586 free_irq(msi->irq, pcie);
1587
1588 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1589 irq = irq_find_mapping(msi->domain, i);
1590 if (irq > 0)
1591 irq_dispose_mapping(irq);
1592 }
1593
1594 irq_domain_remove(msi->domain);
1595
1596 return 0;
1597}
1598
1599static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1600 u32 *xbar)
1601{
1602 struct device_node *np = pcie->dev->of_node;
1603
Thierry Reding7f1f0542014-08-26 17:11:38 +02001604 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1605 switch (lanes) {
1606 case 0x0000104:
1607 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1608 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1609 return 0;
1610
1611 case 0x0000102:
1612 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1613 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1614 return 0;
1615 }
1616 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001617 switch (lanes) {
1618 case 0x00000204:
1619 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1620 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1621 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001622
Jay Agarwal94716cd2013-08-09 16:49:24 +02001623 case 0x00020202:
1624 dev_info(pcie->dev, "2x3 configuration\n");
1625 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1626 return 0;
1627
1628 case 0x00010104:
1629 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1630 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1631 return 0;
1632 }
1633 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1634 switch (lanes) {
1635 case 0x00000004:
1636 dev_info(pcie->dev, "single-mode configuration\n");
1637 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1638 return 0;
1639
1640 case 0x00000202:
1641 dev_info(pcie->dev, "dual-mode configuration\n");
1642 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1643 return 0;
1644 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001645 }
1646
1647 return -EINVAL;
1648}
1649
Thierry Reding077fb152014-05-28 16:49:13 +02001650/*
1651 * Check whether a given set of supplies is available in a device tree node.
1652 * This is used to check whether the new or the legacy device tree bindings
1653 * should be used.
1654 */
1655static bool of_regulator_bulk_available(struct device_node *np,
1656 struct regulator_bulk_data *supplies,
1657 unsigned int num_supplies)
1658{
1659 char property[32];
1660 unsigned int i;
1661
1662 for (i = 0; i < num_supplies; i++) {
1663 snprintf(property, 32, "%s-supply", supplies[i].supply);
1664
1665 if (of_find_property(np, property, NULL) == NULL)
1666 return false;
1667 }
1668
1669 return true;
1670}
1671
1672/*
1673 * Old versions of the device tree binding for this device used a set of power
1674 * supplies that didn't match the hardware inputs. This happened to work for a
1675 * number of cases but is not future proof. However to preserve backwards-
1676 * compatibility with old device trees, this function will try to use the old
1677 * set of supplies.
1678 */
1679static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1680{
1681 struct device_node *np = pcie->dev->of_node;
1682
1683 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1684 pcie->num_supplies = 3;
1685 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1686 pcie->num_supplies = 2;
1687
1688 if (pcie->num_supplies == 0) {
1689 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1690 np->full_name);
1691 return -ENODEV;
1692 }
1693
1694 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1695 sizeof(*pcie->supplies),
1696 GFP_KERNEL);
1697 if (!pcie->supplies)
1698 return -ENOMEM;
1699
1700 pcie->supplies[0].supply = "pex-clk";
1701 pcie->supplies[1].supply = "vdd";
1702
1703 if (pcie->num_supplies > 2)
1704 pcie->supplies[2].supply = "avdd";
1705
1706 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1707 pcie->supplies);
1708}
1709
1710/*
1711 * Obtains the list of regulators required for a particular generation of the
1712 * IP block.
1713 *
1714 * This would've been nice to do simply by providing static tables for use
1715 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1716 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1717 * and either seems to be optional depending on which ports are being used.
1718 */
1719static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1720{
1721 struct device_node *np = pcie->dev->of_node;
1722 unsigned int i = 0;
1723
Thierry Reding7f1f0542014-08-26 17:11:38 +02001724 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1725 pcie->num_supplies = 7;
1726
1727 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1728 sizeof(*pcie->supplies),
1729 GFP_KERNEL);
1730 if (!pcie->supplies)
1731 return -ENOMEM;
1732
1733 pcie->supplies[i++].supply = "avddio-pex";
1734 pcie->supplies[i++].supply = "dvddio-pex";
1735 pcie->supplies[i++].supply = "avdd-pex-pll";
1736 pcie->supplies[i++].supply = "hvdd-pex";
1737 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1738 pcie->supplies[i++].supply = "vddio-pex-ctl";
1739 pcie->supplies[i++].supply = "avdd-pll-erefe";
1740 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001741 bool need_pexa = false, need_pexb = false;
1742
1743 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1744 if (lane_mask & 0x0f)
1745 need_pexa = true;
1746
1747 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1748 if (lane_mask & 0x30)
1749 need_pexb = true;
1750
1751 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1752 (need_pexb ? 2 : 0);
1753
1754 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1755 sizeof(*pcie->supplies),
1756 GFP_KERNEL);
1757 if (!pcie->supplies)
1758 return -ENOMEM;
1759
1760 pcie->supplies[i++].supply = "avdd-pex-pll";
1761 pcie->supplies[i++].supply = "hvdd-pex";
1762 pcie->supplies[i++].supply = "vddio-pex-ctl";
1763 pcie->supplies[i++].supply = "avdd-plle";
1764
1765 if (need_pexa) {
1766 pcie->supplies[i++].supply = "avdd-pexa";
1767 pcie->supplies[i++].supply = "vdd-pexa";
1768 }
1769
1770 if (need_pexb) {
1771 pcie->supplies[i++].supply = "avdd-pexb";
1772 pcie->supplies[i++].supply = "vdd-pexb";
1773 }
1774 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1775 pcie->num_supplies = 5;
1776
1777 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1778 sizeof(*pcie->supplies),
1779 GFP_KERNEL);
1780 if (!pcie->supplies)
1781 return -ENOMEM;
1782
1783 pcie->supplies[0].supply = "avdd-pex";
1784 pcie->supplies[1].supply = "vdd-pex";
1785 pcie->supplies[2].supply = "avdd-pex-pll";
1786 pcie->supplies[3].supply = "avdd-plle";
1787 pcie->supplies[4].supply = "vddio-pex-clk";
1788 }
1789
1790 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1791 pcie->num_supplies))
1792 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1793 pcie->supplies);
1794
1795 /*
1796 * If not all regulators are available for this new scheme, assume
1797 * that the device tree complies with an older version of the device
1798 * tree binding.
1799 */
1800 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1801
1802 devm_kfree(pcie->dev, pcie->supplies);
1803 pcie->num_supplies = 0;
1804
1805 return tegra_pcie_get_legacy_regulators(pcie);
1806}
1807
Thierry Redingd1523b52013-08-09 16:49:19 +02001808static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1809{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001810 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001811 struct device_node *np = pcie->dev->of_node, *port;
1812 struct of_pci_range_parser parser;
1813 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001814 u32 lanes = 0, mask = 0;
1815 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001816 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001817 int err;
1818
Thierry Reding41534e52014-08-01 14:15:11 +02001819 memset(&pcie->all, 0, sizeof(pcie->all));
1820 pcie->all.flags = IORESOURCE_MEM;
1821 pcie->all.name = np->full_name;
1822 pcie->all.start = ~0;
1823 pcie->all.end = 0;
1824
Thierry Redingd1523b52013-08-09 16:49:19 +02001825 if (of_pci_range_parser_init(&parser, np)) {
1826 dev_err(pcie->dev, "missing \"ranges\" property\n");
1827 return -EINVAL;
1828 }
1829
Thierry Redingd1523b52013-08-09 16:49:19 +02001830 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001831 err = of_pci_range_to_resource(&range, np, &res);
1832 if (err < 0)
1833 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001834
1835 switch (res.flags & IORESOURCE_TYPE_BITS) {
1836 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001837 /* Track the bus -> CPU I/O mapping offset. */
1838 pcie->offset.io = res.start - range.pci_addr;
1839
Thierry Reding51067872014-11-27 09:54:09 +01001840 memcpy(&pcie->pio, &res, sizeof(res));
1841 pcie->pio.name = np->full_name;
1842
1843 /*
1844 * The Tegra PCIe host bridge uses this to program the
1845 * mapping of the I/O space to the physical address,
1846 * so we override the .start and .end fields here that
1847 * of_pci_range_to_resource() converted to I/O space.
1848 * We also set the IORESOURCE_MEM type to clarify that
1849 * the resource is in the physical memory space.
1850 */
1851 pcie->io.start = range.cpu_addr;
1852 pcie->io.end = range.cpu_addr + range.size - 1;
1853 pcie->io.flags = IORESOURCE_MEM;
1854 pcie->io.name = "I/O";
1855
1856 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001857 break;
1858
1859 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001860 /*
1861 * Track the bus -> CPU memory mapping offset. This
1862 * assumes that the prefetchable and non-prefetchable
1863 * regions will be the last of type IORESOURCE_MEM in
1864 * the ranges property.
1865 * */
1866 pcie->offset.mem = res.start - range.pci_addr;
1867
Thierry Redingd1523b52013-08-09 16:49:19 +02001868 if (res.flags & IORESOURCE_PREFETCH) {
1869 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001870 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001871 } else {
1872 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001873 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001874 }
1875 break;
1876 }
Thierry Reding41534e52014-08-01 14:15:11 +02001877
1878 if (res.start <= pcie->all.start)
1879 pcie->all.start = res.start;
1880
1881 if (res.end >= pcie->all.end)
1882 pcie->all.end = res.end;
Thierry Redingd1523b52013-08-09 16:49:19 +02001883 }
1884
Thierry Reding41534e52014-08-01 14:15:11 +02001885 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1886 if (err < 0)
1887 return err;
1888
Thierry Redingd1523b52013-08-09 16:49:19 +02001889 err = of_pci_parse_bus_range(np, &pcie->busn);
1890 if (err < 0) {
1891 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1892 err);
1893 pcie->busn.name = np->name;
1894 pcie->busn.start = 0;
1895 pcie->busn.end = 0xff;
1896 pcie->busn.flags = IORESOURCE_BUS;
1897 }
1898
1899 /* parse root ports */
1900 for_each_child_of_node(np, port) {
1901 struct tegra_pcie_port *rp;
1902 unsigned int index;
1903 u32 value;
1904
1905 err = of_pci_get_devfn(port);
1906 if (err < 0) {
1907 dev_err(pcie->dev, "failed to parse address: %d\n",
1908 err);
1909 return err;
1910 }
1911
1912 index = PCI_SLOT(err);
1913
Jay Agarwal94716cd2013-08-09 16:49:24 +02001914 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001915 dev_err(pcie->dev, "invalid port number: %d\n", index);
1916 return -EINVAL;
1917 }
1918
1919 index--;
1920
1921 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1922 if (err < 0) {
1923 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1924 err);
1925 return err;
1926 }
1927
1928 if (value > 16) {
1929 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1930 return -EINVAL;
1931 }
1932
1933 lanes |= value << (index << 3);
1934
Thierry Reding077fb152014-05-28 16:49:13 +02001935 if (!of_device_is_available(port)) {
1936 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001937 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001938 }
1939
1940 mask |= ((1 << value) - 1) << lane;
1941 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001942
1943 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1944 if (!rp)
1945 return -ENOMEM;
1946
1947 err = of_address_to_resource(port, 0, &rp->regs);
1948 if (err < 0) {
1949 dev_err(pcie->dev, "failed to parse address: %d\n",
1950 err);
1951 return err;
1952 }
1953
1954 INIT_LIST_HEAD(&rp->list);
1955 rp->index = index;
1956 rp->lanes = value;
1957 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001958 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001959
Julia Lawalldc05ee32013-08-26 11:11:09 +02001960 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1961 if (IS_ERR(rp->base))
1962 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001963
1964 list_add_tail(&rp->list, &pcie->ports);
1965 }
1966
1967 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1968 if (err < 0) {
1969 dev_err(pcie->dev, "invalid lane configuration\n");
1970 return err;
1971 }
1972
Thierry Reding077fb152014-05-28 16:49:13 +02001973 err = tegra_pcie_get_regulators(pcie, mask);
1974 if (err < 0)
1975 return err;
1976
Thierry Redingd1523b52013-08-09 16:49:19 +02001977 return 0;
1978}
1979
1980/*
1981 * FIXME: If there are no PCIe cards attached, then calling this function
1982 * can result in the increase of the bootup time as there are big timeout
1983 * loops.
1984 */
1985#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1986static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1987{
1988 unsigned int retries = 3;
1989 unsigned long value;
1990
Thierry Reding7f1f0542014-08-26 17:11:38 +02001991 /* override presence detection */
1992 value = readl(port->base + RP_PRIV_MISC);
1993 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
1994 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
1995 writel(value, port->base + RP_PRIV_MISC);
1996
Thierry Redingd1523b52013-08-09 16:49:19 +02001997 do {
1998 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1999
2000 do {
2001 value = readl(port->base + RP_VEND_XP);
2002
2003 if (value & RP_VEND_XP_DL_UP)
2004 break;
2005
2006 usleep_range(1000, 2000);
2007 } while (--timeout);
2008
2009 if (!timeout) {
2010 dev_err(port->pcie->dev, "link %u down, retrying\n",
2011 port->index);
2012 goto retry;
2013 }
2014
2015 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2016
2017 do {
2018 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2019
2020 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2021 return true;
2022
2023 usleep_range(1000, 2000);
2024 } while (--timeout);
2025
2026retry:
2027 tegra_pcie_port_reset(port);
2028 } while (--retries);
2029
2030 return false;
2031}
2032
2033static int tegra_pcie_enable(struct tegra_pcie *pcie)
2034{
2035 struct tegra_pcie_port *port, *tmp;
2036 struct hw_pci hw;
2037
2038 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2039 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2040 port->index, port->lanes);
2041
2042 tegra_pcie_port_enable(port);
2043
2044 if (tegra_pcie_port_check_link(port))
2045 continue;
2046
2047 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2048
2049 tegra_pcie_port_disable(port);
2050 tegra_pcie_port_free(port);
2051 }
2052
2053 memset(&hw, 0, sizeof(hw));
2054
Yijing Wang7ec725b2014-11-11 15:35:05 -07002055#ifdef CONFIG_PCI_MSI
2056 hw.msi_ctrl = &pcie->msi.chip;
2057#endif
2058
Thierry Redingd1523b52013-08-09 16:49:19 +02002059 hw.nr_controllers = 1;
2060 hw.private_data = (void **)&pcie;
2061 hw.setup = tegra_pcie_setup;
2062 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02002063 hw.ops = &tegra_pcie_ops;
2064
2065 pci_common_init_dev(pcie->dev, &hw);
2066
2067 return 0;
2068}
2069
Jay Agarwal94716cd2013-08-09 16:49:24 +02002070static const struct tegra_pcie_soc_data tegra20_pcie_data = {
2071 .num_ports = 2,
2072 .msi_base_shift = 0,
2073 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2074 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
Stephen Warrenf8144302016-07-25 16:02:27 -05002075 .pads_refclk_cfg0 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002076 .has_pex_clkreq_en = false,
2077 .has_pex_bias_ctrl = false,
2078 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002079 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002080 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002081};
2082
2083static const struct tegra_pcie_soc_data tegra30_pcie_data = {
2084 .num_ports = 3,
2085 .msi_base_shift = 8,
2086 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2087 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002088 .pads_refclk_cfg0 = 0xfa5cfa5c,
2089 .pads_refclk_cfg1 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002090 .has_pex_clkreq_en = true,
2091 .has_pex_bias_ctrl = true,
2092 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002093 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002094 .has_gen2 = false,
2095};
2096
2097static const struct tegra_pcie_soc_data tegra124_pcie_data = {
2098 .num_ports = 2,
2099 .msi_base_shift = 8,
2100 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2101 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002102 .pads_refclk_cfg0 = 0x44ac44ac,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002103 .has_pex_clkreq_en = true,
2104 .has_pex_bias_ctrl = true,
2105 .has_intr_prsnt_sense = true,
2106 .has_cml_clk = true,
2107 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002108};
2109
2110static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002111 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002112 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
2113 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
2114 { },
2115};
2116MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
2117
Thierry Reding2cb989f2014-07-22 12:30:46 -06002118static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2119{
2120 struct tegra_pcie *pcie = s->private;
2121
2122 if (list_empty(&pcie->ports))
2123 return NULL;
2124
2125 seq_printf(s, "Index Status\n");
2126
2127 return seq_list_start(&pcie->ports, *pos);
2128}
2129
2130static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2131{
2132 struct tegra_pcie *pcie = s->private;
2133
2134 return seq_list_next(v, &pcie->ports, pos);
2135}
2136
2137static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2138{
2139}
2140
2141static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2142{
2143 bool up = false, active = false;
2144 struct tegra_pcie_port *port;
2145 unsigned int value;
2146
2147 port = list_entry(v, struct tegra_pcie_port, list);
2148
2149 value = readl(port->base + RP_VEND_XP);
2150
2151 if (value & RP_VEND_XP_DL_UP)
2152 up = true;
2153
2154 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2155
2156 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2157 active = true;
2158
2159 seq_printf(s, "%2u ", port->index);
2160
2161 if (up)
2162 seq_printf(s, "up");
2163
2164 if (active) {
2165 if (up)
2166 seq_printf(s, ", ");
2167
2168 seq_printf(s, "active");
2169 }
2170
2171 seq_printf(s, "\n");
2172 return 0;
2173}
2174
2175static const struct seq_operations tegra_pcie_ports_seq_ops = {
2176 .start = tegra_pcie_ports_seq_start,
2177 .next = tegra_pcie_ports_seq_next,
2178 .stop = tegra_pcie_ports_seq_stop,
2179 .show = tegra_pcie_ports_seq_show,
2180};
2181
2182static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2183{
2184 struct tegra_pcie *pcie = inode->i_private;
2185 struct seq_file *s;
2186 int err;
2187
2188 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2189 if (err)
2190 return err;
2191
2192 s = file->private_data;
2193 s->private = pcie;
2194
2195 return 0;
2196}
2197
2198static const struct file_operations tegra_pcie_ports_ops = {
2199 .owner = THIS_MODULE,
2200 .open = tegra_pcie_ports_open,
2201 .read = seq_read,
2202 .llseek = seq_lseek,
2203 .release = seq_release,
2204};
2205
2206static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2207{
2208 struct dentry *file;
2209
2210 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2211 if (!pcie->debugfs)
2212 return -ENOMEM;
2213
2214 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2215 pcie, &tegra_pcie_ports_ops);
2216 if (!file)
2217 goto remove;
2218
2219 return 0;
2220
2221remove:
2222 debugfs_remove_recursive(pcie->debugfs);
2223 pcie->debugfs = NULL;
2224 return -ENOMEM;
2225}
2226
Thierry Redingd1523b52013-08-09 16:49:19 +02002227static int tegra_pcie_probe(struct platform_device *pdev)
2228{
Jay Agarwal94716cd2013-08-09 16:49:24 +02002229 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02002230 struct tegra_pcie *pcie;
2231 int err;
2232
Jay Agarwal94716cd2013-08-09 16:49:24 +02002233 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2234 if (!match)
2235 return -ENODEV;
2236
Thierry Redingd1523b52013-08-09 16:49:19 +02002237 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2238 if (!pcie)
2239 return -ENOMEM;
2240
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002241 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002242 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02002243 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02002244 pcie->dev = &pdev->dev;
2245
2246 err = tegra_pcie_parse_dt(pcie);
2247 if (err < 0)
2248 return err;
2249
Thierry Redingd1523b52013-08-09 16:49:19 +02002250 err = tegra_pcie_get_resources(pcie);
2251 if (err < 0) {
2252 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2253 return err;
2254 }
2255
2256 err = tegra_pcie_enable_controller(pcie);
2257 if (err)
2258 goto put_resources;
2259
2260 /* setup the AFI address translations */
2261 tegra_pcie_setup_translations(pcie);
2262
2263 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2264 err = tegra_pcie_enable_msi(pcie);
2265 if (err < 0) {
2266 dev_err(&pdev->dev,
2267 "failed to enable MSI support: %d\n",
2268 err);
2269 goto put_resources;
2270 }
2271 }
2272
2273 err = tegra_pcie_enable(pcie);
2274 if (err < 0) {
2275 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2276 goto disable_msi;
2277 }
2278
Thierry Reding2cb989f2014-07-22 12:30:46 -06002279 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2280 err = tegra_pcie_debugfs_init(pcie);
2281 if (err < 0)
2282 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2283 err);
2284 }
2285
Thierry Redingd1523b52013-08-09 16:49:19 +02002286 platform_set_drvdata(pdev, pcie);
2287 return 0;
2288
2289disable_msi:
2290 if (IS_ENABLED(CONFIG_PCI_MSI))
2291 tegra_pcie_disable_msi(pcie);
2292put_resources:
2293 tegra_pcie_put_resources(pcie);
2294 return err;
2295}
2296
Thierry Redingd1523b52013-08-09 16:49:19 +02002297static struct platform_driver tegra_pcie_driver = {
2298 .driver = {
2299 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002300 .of_match_table = tegra_pcie_of_match,
2301 .suppress_bind_attrs = true,
2302 },
2303 .probe = tegra_pcie_probe,
2304};
2305module_platform_driver(tegra_pcie_driver);
2306
2307MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
2308MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
Thierry Redingd975cb52014-07-11 08:58:58 +02002309MODULE_LICENSE("GPL v2");