blob: 44fd31b9fa2db372a52262b8750611fc0e072e1b [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 Redingd1523b52013-08-09 16:49:19 +0200186#define RP_VEND_XP 0x00000F00
187#define RP_VEND_XP_DL_UP (1 << 30)
188
Thierry Reding7f1f0542014-08-26 17:11:38 +0200189#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)
192
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
197#define PADS_CTL_SEL 0x0000009C
198
199#define PADS_CTL 0x000000A0
200#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
Jay Agarwal94716cd2013-08-09 16:49:24 +0200204#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
217#define PADS_REFCLK_CFG0 0x000000C8
218#define PADS_REFCLK_CFG1 0x000000CC
Thierry Reding7f1f0542014-08-26 17:11:38 +0200219#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
231/* Default value provided by HW engineering is 0xfa5c */
232#define PADS_REFCLK_CFG_VALUE \
233 ( \
234 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
235 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
236 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
237 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
238 )
239
Thierry Redingd1523b52013-08-09 16:49:19 +0200240struct tegra_msi {
Yijing Wangc2791b82014-11-11 17:45:45 -0700241 struct msi_controller chip;
Thierry Redingd1523b52013-08-09 16:49:19 +0200242 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
243 struct irq_domain *domain;
244 unsigned long pages;
245 struct mutex lock;
246 int irq;
247};
248
Jay Agarwal94716cd2013-08-09 16:49:24 +0200249/* used to differentiate between Tegra SoC generations */
250struct tegra_pcie_soc_data {
251 unsigned int num_ports;
252 unsigned int msi_base_shift;
253 u32 pads_pll_ctl;
254 u32 tx_ref_sel;
255 bool has_pex_clkreq_en;
256 bool has_pex_bias_ctrl;
257 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200258 bool has_cml_clk;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200259 bool has_gen2;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200260};
261
Yijing Wangc2791b82014-11-11 17:45:45 -0700262static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
Thierry Redingd1523b52013-08-09 16:49:19 +0200263{
264 return container_of(chip, struct tegra_msi, chip);
265}
266
267struct tegra_pcie {
268 struct device *dev;
269
270 void __iomem *pads;
271 void __iomem *afi;
272 int irq;
273
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700274 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200275 struct resource *cs;
276
Thierry Reding41534e52014-08-01 14:15:11 +0200277 struct resource all;
Thierry Redingd1523b52013-08-09 16:49:19 +0200278 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100279 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200280 struct resource mem;
281 struct resource prefetch;
282 struct resource busn;
283
284 struct clk *pex_clk;
285 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200286 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200287 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200288
Stephen Warren3127a6b2013-11-06 15:56:58 -0700289 struct reset_control *pex_rst;
290 struct reset_control *afi_rst;
291 struct reset_control *pcie_xrst;
292
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;
298 unsigned int num_ports;
299 u32 xbar_config;
300
Thierry Reding077fb152014-05-28 16:49:13 +0200301 struct regulator_bulk_data *supplies;
302 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200303
304 const struct tegra_pcie_soc_data *soc_data;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600305 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200306};
307
308struct tegra_pcie_port {
309 struct tegra_pcie *pcie;
310 struct list_head list;
311 struct resource regs;
312 void __iomem *base;
313 unsigned int index;
314 unsigned int lanes;
315};
316
317struct tegra_pcie_bus {
318 struct vm_struct *area;
319 struct list_head list;
320 unsigned int nr;
321};
322
323static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
324{
325 return sys->private_data;
326}
327
328static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
329 unsigned long offset)
330{
331 writel(value, pcie->afi + offset);
332}
333
334static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
335{
336 return readl(pcie->afi + offset);
337}
338
339static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
340 unsigned long offset)
341{
342 writel(value, pcie->pads + offset);
343}
344
345static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
346{
347 return readl(pcie->pads + offset);
348}
349
350/*
351 * The configuration space mapping on Tegra is somewhat similar to the ECAM
352 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
353 * register accesses are mapped:
354 *
355 * [27:24] extended register number
356 * [23:16] bus number
357 * [15:11] device number
358 * [10: 8] function number
359 * [ 7: 0] register number
360 *
361 * Mapping the whole extended configuration space would require 256 MiB of
362 * virtual address space, only a small part of which will actually be used.
363 * To work around this, a 1 MiB of virtual addresses are allocated per bus
364 * when the bus is first accessed. When the physical range is mapped, the
365 * the bus number bits are hidden so that the extended register number bits
366 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
367 *
368 * [19:16] extended register number
369 * [15:11] device number
370 * [10: 8] function number
371 * [ 7: 0] register number
372 *
373 * This is achieved by stitching together 16 chunks of 64 KiB of physical
374 * address space via the MMU.
375 */
376static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
377{
378 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
379 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
380}
381
382static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
383 unsigned int busnr)
384{
385 pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
386 L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
387 phys_addr_t cs = pcie->cs->start;
388 struct tegra_pcie_bus *bus;
389 unsigned int i;
390 int err;
391
392 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
393 if (!bus)
394 return ERR_PTR(-ENOMEM);
395
396 INIT_LIST_HEAD(&bus->list);
397 bus->nr = busnr;
398
399 /* allocate 1 MiB of virtual addresses */
400 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
401 if (!bus->area) {
402 err = -ENOMEM;
403 goto free;
404 }
405
406 /* map each of the 16 chunks of 64 KiB each */
407 for (i = 0; i < 16; i++) {
408 unsigned long virt = (unsigned long)bus->area->addr +
409 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200410 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200411
412 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
413 if (err < 0) {
414 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
415 err);
416 goto unmap;
417 }
418 }
419
420 return bus;
421
422unmap:
423 vunmap(bus->area->addr);
424free:
425 kfree(bus);
426 return ERR_PTR(err);
427}
428
429/*
430 * Look up a virtual address mapping for the specified bus number. If no such
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700431 * mapping exists, try to create one.
Thierry Redingd1523b52013-08-09 16:49:19 +0200432 */
433static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
434 unsigned int busnr)
435{
436 struct tegra_pcie_bus *bus;
437
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700438 list_for_each_entry(bus, &pcie->buses, list)
Thierry Redingd1523b52013-08-09 16:49:19 +0200439 if (bus->nr == busnr)
Jingoo Han1e652492013-09-25 16:40:54 -0600440 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200441
442 bus = tegra_pcie_bus_alloc(pcie, busnr);
443 if (IS_ERR(bus))
444 return NULL;
445
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700446 list_add_tail(&bus->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200447
Jingoo Han1e652492013-09-25 16:40:54 -0600448 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200449}
450
451static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
452 unsigned int devfn,
453 int where)
454{
455 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
456 void __iomem *addr = NULL;
457
458 if (bus->number == 0) {
459 unsigned int slot = PCI_SLOT(devfn);
460 struct tegra_pcie_port *port;
461
462 list_for_each_entry(port, &pcie->ports, list) {
463 if (port->index + 1 == slot) {
464 addr = port->base + (where & ~3);
465 break;
466 }
467 }
468 } else {
469 addr = tegra_pcie_bus_map(pcie, bus->number);
470 if (!addr) {
471 dev_err(pcie->dev,
472 "failed to map cfg. space for bus %u\n",
473 bus->number);
474 return NULL;
475 }
476
477 addr += tegra_pcie_conf_offset(devfn, where);
478 }
479
480 return addr;
481}
482
Thierry Redingd1523b52013-08-09 16:49:19 +0200483static struct pci_ops tegra_pcie_ops = {
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600484 .map_bus = tegra_pcie_conf_address,
485 .read = pci_generic_config_read32,
486 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200487};
488
489static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
490{
491 unsigned long ret = 0;
492
493 switch (port->index) {
494 case 0:
495 ret = AFI_PEX0_CTRL;
496 break;
497
498 case 1:
499 ret = AFI_PEX1_CTRL;
500 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200501
502 case 2:
503 ret = AFI_PEX2_CTRL;
504 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200505 }
506
507 return ret;
508}
509
510static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
511{
512 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
513 unsigned long value;
514
515 /* pulse reset signal */
516 value = afi_readl(port->pcie, ctrl);
517 value &= ~AFI_PEX_CTRL_RST;
518 afi_writel(port->pcie, value, ctrl);
519
520 usleep_range(1000, 2000);
521
522 value = afi_readl(port->pcie, ctrl);
523 value |= AFI_PEX_CTRL_RST;
524 afi_writel(port->pcie, value, ctrl);
525}
526
527static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
528{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200529 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200530 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
531 unsigned long value;
532
533 /* enable reference clock */
534 value = afi_readl(port->pcie, ctrl);
535 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200536
537 if (soc->has_pex_clkreq_en)
538 value |= AFI_PEX_CTRL_CLKREQ_EN;
539
Thierry Reding7f1f0542014-08-26 17:11:38 +0200540 value |= AFI_PEX_CTRL_OVERRIDE_EN;
541
Thierry Redingd1523b52013-08-09 16:49:19 +0200542 afi_writel(port->pcie, value, ctrl);
543
544 tegra_pcie_port_reset(port);
545}
546
547static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
548{
Thierry Reding0d20d622014-08-26 17:11:35 +0200549 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200550 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
551 unsigned long value;
552
553 /* assert port reset */
554 value = afi_readl(port->pcie, ctrl);
555 value &= ~AFI_PEX_CTRL_RST;
556 afi_writel(port->pcie, value, ctrl);
557
558 /* disable reference clock */
559 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200560
561 if (soc->has_pex_clkreq_en)
562 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
563
Thierry Redingd1523b52013-08-09 16:49:19 +0200564 value &= ~AFI_PEX_CTRL_REFCLK_EN;
565 afi_writel(port->pcie, value, ctrl);
566}
567
568static void tegra_pcie_port_free(struct tegra_pcie_port *port)
569{
570 struct tegra_pcie *pcie = port->pcie;
571
572 devm_iounmap(pcie->dev, port->base);
573 devm_release_mem_region(pcie->dev, port->regs.start,
574 resource_size(&port->regs));
575 list_del(&port->list);
576 devm_kfree(pcie->dev, port);
577}
578
579static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
580{
581 u16 reg;
582
583 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
584 pci_read_config_word(dev, PCI_COMMAND, &reg);
585 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
586 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
587 pci_write_config_word(dev, PCI_COMMAND, reg);
588 }
589}
590DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
591
592/* Tegra PCIE root complex wrongly reports device class */
593static void tegra_pcie_fixup_class(struct pci_dev *dev)
594{
595 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
596}
597DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
598DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200599DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
600DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200601
602/* Tegra PCIE requires relaxed ordering */
603static void tegra_pcie_relax_enable(struct pci_dev *dev)
604{
605 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
606}
607DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
608
609static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
610{
611 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200612 int err;
613
614 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
615 if (err < 0)
616 return err;
617
618 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
619 if (err)
620 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200621
622 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
623 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
624 sys->mem_offset);
625 pci_add_resource(&sys->resources, &pcie->busn);
626
Thierry Reding51067872014-11-27 09:54:09 +0100627 pci_ioremap_io(pcie->pio.start, pcie->io.start);
Thierry Redingd1523b52013-08-09 16:49:19 +0200628
629 return 1;
630}
631
632static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
633{
634 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600635 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200636
Stephen Warrenb4f17372013-05-06 14:19:19 -0600637 tegra_cpuidle_pcie_irqs_in_use();
638
Lucas Stachf5d33522014-04-16 10:24:32 -0600639 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
640 if (!irq)
641 irq = pcie->irq;
642
643 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200644}
645
Thierry Redingd1523b52013-08-09 16:49:19 +0200646static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
647{
648 struct tegra_pcie *pcie = sys_to_pcie(sys);
649 struct pci_bus *bus;
650
651 bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
652 &sys->resources);
653 if (!bus)
654 return NULL;
655
656 pci_scan_child_bus(bus);
657
658 return bus;
659}
660
661static irqreturn_t tegra_pcie_isr(int irq, void *arg)
662{
663 const char *err_msg[] = {
664 "Unknown",
665 "AXI slave error",
666 "AXI decode error",
667 "Target abort",
668 "Master abort",
669 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200670 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200671 "Response decoding error",
672 "AXI response decoding error",
673 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200674 "Slot present pin change",
675 "Slot clock request change",
676 "TMS clock ramp change",
677 "TMS ready for power down",
678 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200679 };
680 struct tegra_pcie *pcie = arg;
681 u32 code, signature;
682
683 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
684 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
685 afi_writel(pcie, 0, AFI_INTR_CODE);
686
687 if (code == AFI_INTR_LEGACY)
688 return IRQ_NONE;
689
690 if (code >= ARRAY_SIZE(err_msg))
691 code = 0;
692
693 /*
694 * do not pollute kernel log with master abort reports since they
695 * happen a lot during enumeration
696 */
697 if (code == AFI_INTR_MASTER_ABORT)
698 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
699 signature);
700 else
701 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
702 signature);
703
704 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
705 code == AFI_INTR_FPCI_DECODE_ERROR) {
706 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
707 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
708
709 if (code == AFI_INTR_MASTER_ABORT)
710 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
711 else
712 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
713 }
714
715 return IRQ_HANDLED;
716}
717
718/*
719 * FPCI map is as follows:
720 * - 0xfdfc000000: I/O space
721 * - 0xfdfe000000: type 0 configuration space
722 * - 0xfdff000000: type 1 configuration space
723 * - 0xfe00000000: type 0 extended configuration space
724 * - 0xfe10000000: type 1 extended configuration space
725 */
726static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
727{
728 u32 fpci_bar, size, axi_address;
729
730 /* Bar 0: type 1 extended configuration space */
731 fpci_bar = 0xfe100000;
732 size = resource_size(pcie->cs);
733 axi_address = pcie->cs->start;
734 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
735 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
736 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
737
738 /* Bar 1: downstream IO bar */
739 fpci_bar = 0xfdfc0000;
740 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100741 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200742 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
743 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
744 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
745
746 /* Bar 2: prefetchable memory BAR */
747 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
748 size = resource_size(&pcie->prefetch);
749 axi_address = pcie->prefetch.start;
750 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
751 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
752 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
753
754 /* Bar 3: non prefetchable memory BAR */
755 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
756 size = resource_size(&pcie->mem);
757 axi_address = pcie->mem.start;
758 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
759 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
760 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
761
762 /* NULL out the remaining BARs as they are not used */
763 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
764 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
765 afi_writel(pcie, 0, AFI_FPCI_BAR4);
766
767 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
768 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
769 afi_writel(pcie, 0, AFI_FPCI_BAR5);
770
771 /* map all upstream transactions as uncached */
772 afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
773 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
774 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
775 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
776
777 /* MSI translations are setup only when needed */
778 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
779 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
780 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
781 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
782}
783
Thierry Reding7f1f0542014-08-26 17:11:38 +0200784static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200785{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200786 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200787 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200788
Thierry Reding7f1f0542014-08-26 17:11:38 +0200789 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200790
Thierry Reding7f1f0542014-08-26 17:11:38 +0200791 while (time_before(jiffies, timeout)) {
792 value = pads_readl(pcie, soc->pads_pll_ctl);
793 if (value & PADS_PLL_CTL_LOCKDET)
794 return 0;
795 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200796
Thierry Reding7f1f0542014-08-26 17:11:38 +0200797 return -ETIMEDOUT;
798}
Thierry Redingd1523b52013-08-09 16:49:19 +0200799
Thierry Reding7f1f0542014-08-26 17:11:38 +0200800static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
801{
802 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
803 u32 value;
804 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200805
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700806 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200807 pads_writel(pcie, 0x0, PADS_CTL_SEL);
808
809 /* override IDDQ to 1 on all 4 lanes */
810 value = pads_readl(pcie, PADS_CTL);
811 value |= PADS_CTL_IDDQ_1L;
812 pads_writel(pcie, value, PADS_CTL);
813
814 /*
815 * Set up PHY PLL inputs select PLLE output as refclock,
816 * set TX ref sel to div10 (not div5).
817 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200818 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200819 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200820 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
821 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200822
Eric Yuenec732762014-08-26 17:11:37 +0200823 /* reset PLL */
824 value = pads_readl(pcie, soc->pads_pll_ctl);
825 value &= ~PADS_PLL_CTL_RST_B4SM;
826 pads_writel(pcie, value, soc->pads_pll_ctl);
827
828 usleep_range(20, 100);
829
Thierry Redingd1523b52013-08-09 16:49:19 +0200830 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200831 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200832 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200833 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200834
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200835 /* Configure the reference clock driver */
836 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
837 pads_writel(pcie, value, PADS_REFCLK_CFG0);
838 if (soc->num_ports > 2)
839 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200840
841 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200842 err = tegra_pcie_pll_wait(pcie, 500);
843 if (err < 0) {
844 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
845 return err;
846 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200847
848 /* turn off IDDQ override */
849 value = pads_readl(pcie, PADS_CTL);
850 value &= ~PADS_CTL_IDDQ_1L;
851 pads_writel(pcie, value, PADS_CTL);
852
853 /* enable TX/RX data */
854 value = pads_readl(pcie, PADS_CTL);
855 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
856 pads_writel(pcie, value, PADS_CTL);
857
Thierry Reding7f1f0542014-08-26 17:11:38 +0200858 return 0;
859}
860
861static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
862{
863 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
864 struct tegra_pcie_port *port;
865 unsigned long value;
866 int err;
867
868 /* enable PLL power down */
869 if (pcie->phy) {
870 value = afi_readl(pcie, AFI_PLLE_CONTROL);
871 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
872 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
873 afi_writel(pcie, value, AFI_PLLE_CONTROL);
874 }
875
876 /* power down PCIe slot clock bias pad */
877 if (soc->has_pex_bias_ctrl)
878 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
879
880 /* configure mode and disable all ports */
881 value = afi_readl(pcie, AFI_PCIE_CONFIG);
882 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
883 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
884
885 list_for_each_entry(port, &pcie->ports, list)
886 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
887
888 afi_writel(pcie, value, AFI_PCIE_CONFIG);
889
890 if (soc->has_gen2) {
891 value = afi_readl(pcie, AFI_FUSE);
892 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
893 afi_writel(pcie, value, AFI_FUSE);
894 } else {
895 value = afi_readl(pcie, AFI_FUSE);
896 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
897 afi_writel(pcie, value, AFI_FUSE);
898 }
899
900 if (!pcie->phy)
901 err = tegra_pcie_phy_enable(pcie);
902 else
903 err = phy_power_on(pcie->phy);
904
905 if (err < 0) {
906 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
907 return err;
908 }
909
Thierry Redingd1523b52013-08-09 16:49:19 +0200910 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -0700911 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200912
913 /* finally enable PCIe */
914 value = afi_readl(pcie, AFI_CONFIGURATION);
915 value |= AFI_CONFIGURATION_EN_FPCI;
916 afi_writel(pcie, value, AFI_CONFIGURATION);
917
918 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
919 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
920 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200921
922 if (soc->has_intr_prsnt_sense)
923 value |= AFI_INTR_EN_PRSNT_SENSE;
924
Thierry Redingd1523b52013-08-09 16:49:19 +0200925 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
926 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
927
928 /* don't enable MSI for now, only when needed */
929 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
930
931 /* disable all exceptions */
932 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
933
934 return 0;
935}
936
937static void tegra_pcie_power_off(struct tegra_pcie *pcie)
938{
939 int err;
940
941 /* TODO: disable and unprepare clocks? */
942
Thierry Reding7f1f0542014-08-26 17:11:38 +0200943 err = phy_power_off(pcie->phy);
944 if (err < 0)
945 dev_warn(pcie->dev, "failed to power off PHY: %d\n", err);
946
Stephen Warren3127a6b2013-11-06 15:56:58 -0700947 reset_control_assert(pcie->pcie_xrst);
948 reset_control_assert(pcie->afi_rst);
949 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200950
951 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
952
Thierry Reding077fb152014-05-28 16:49:13 +0200953 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +0200954 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +0200955 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +0200956}
957
958static int tegra_pcie_power_on(struct tegra_pcie *pcie)
959{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200960 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200961 int err;
962
Stephen Warren3127a6b2013-11-06 15:56:58 -0700963 reset_control_assert(pcie->pcie_xrst);
964 reset_control_assert(pcie->afi_rst);
965 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200966
967 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
968
969 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +0200970 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
971 if (err < 0)
972 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200973
Thierry Redingd1523b52013-08-09 16:49:19 +0200974 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -0700975 pcie->pex_clk,
976 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200977 if (err) {
978 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
979 return err;
980 }
981
Stephen Warren3127a6b2013-11-06 15:56:58 -0700982 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200983
984 err = clk_prepare_enable(pcie->afi_clk);
985 if (err < 0) {
986 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
987 return err;
988 }
989
Jay Agarwal94716cd2013-08-09 16:49:24 +0200990 if (soc->has_cml_clk) {
991 err = clk_prepare_enable(pcie->cml_clk);
992 if (err < 0) {
993 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
994 err);
995 return err;
996 }
997 }
998
Thierry Redingd1523b52013-08-09 16:49:19 +0200999 err = clk_prepare_enable(pcie->pll_e);
1000 if (err < 0) {
1001 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1002 return err;
1003 }
1004
1005 return 0;
1006}
1007
1008static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1009{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001010 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1011
Thierry Redingd1523b52013-08-09 16:49:19 +02001012 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1013 if (IS_ERR(pcie->pex_clk))
1014 return PTR_ERR(pcie->pex_clk);
1015
1016 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1017 if (IS_ERR(pcie->afi_clk))
1018 return PTR_ERR(pcie->afi_clk);
1019
Thierry Redingd1523b52013-08-09 16:49:19 +02001020 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1021 if (IS_ERR(pcie->pll_e))
1022 return PTR_ERR(pcie->pll_e);
1023
Jay Agarwal94716cd2013-08-09 16:49:24 +02001024 if (soc->has_cml_clk) {
1025 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1026 if (IS_ERR(pcie->cml_clk))
1027 return PTR_ERR(pcie->cml_clk);
1028 }
1029
Thierry Redingd1523b52013-08-09 16:49:19 +02001030 return 0;
1031}
1032
Stephen Warren3127a6b2013-11-06 15:56:58 -07001033static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1034{
1035 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1036 if (IS_ERR(pcie->pex_rst))
1037 return PTR_ERR(pcie->pex_rst);
1038
1039 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1040 if (IS_ERR(pcie->afi_rst))
1041 return PTR_ERR(pcie->afi_rst);
1042
1043 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1044 if (IS_ERR(pcie->pcie_xrst))
1045 return PTR_ERR(pcie->pcie_xrst);
1046
1047 return 0;
1048}
1049
Thierry Redingd1523b52013-08-09 16:49:19 +02001050static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1051{
1052 struct platform_device *pdev = to_platform_device(pcie->dev);
1053 struct resource *pads, *afi, *res;
1054 int err;
1055
1056 err = tegra_pcie_clocks_get(pcie);
1057 if (err) {
1058 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1059 return err;
1060 }
1061
Stephen Warren3127a6b2013-11-06 15:56:58 -07001062 err = tegra_pcie_resets_get(pcie);
1063 if (err) {
1064 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1065 return err;
1066 }
1067
Thierry Reding7f1f0542014-08-26 17:11:38 +02001068 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1069 if (IS_ERR(pcie->phy)) {
1070 err = PTR_ERR(pcie->phy);
1071 dev_err(&pdev->dev, "failed to get PHY: %d\n", err);
1072 return err;
1073 }
1074
1075 err = phy_init(pcie->phy);
1076 if (err < 0) {
1077 dev_err(&pdev->dev, "failed to initialize PHY: %d\n", err);
1078 return err;
1079 }
1080
Thierry Redingd1523b52013-08-09 16:49:19 +02001081 err = tegra_pcie_power_on(pcie);
1082 if (err) {
1083 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1084 return err;
1085 }
1086
Thierry Redingd1523b52013-08-09 16:49:19 +02001087 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001088 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1089 if (IS_ERR(pcie->pads)) {
1090 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001091 goto poweroff;
1092 }
1093
1094 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001095 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1096 if (IS_ERR(pcie->afi)) {
1097 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001098 goto poweroff;
1099 }
1100
Julia Lawalldc05ee32013-08-26 11:11:09 +02001101 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001102 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1103 if (!res) {
1104 err = -EADDRNOTAVAIL;
1105 goto poweroff;
1106 }
1107
1108 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1109 resource_size(res), res->name);
1110 if (!pcie->cs) {
1111 err = -EADDRNOTAVAIL;
1112 goto poweroff;
1113 }
1114
1115 /* request interrupt */
1116 err = platform_get_irq_byname(pdev, "intr");
1117 if (err < 0) {
1118 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1119 goto poweroff;
1120 }
1121
1122 pcie->irq = err;
1123
1124 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1125 if (err) {
1126 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1127 goto poweroff;
1128 }
1129
1130 return 0;
1131
1132poweroff:
1133 tegra_pcie_power_off(pcie);
1134 return err;
1135}
1136
1137static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1138{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001139 int err;
1140
Thierry Redingd1523b52013-08-09 16:49:19 +02001141 if (pcie->irq > 0)
1142 free_irq(pcie->irq, pcie);
1143
1144 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001145
1146 err = phy_exit(pcie->phy);
1147 if (err < 0)
1148 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1149
Thierry Redingd1523b52013-08-09 16:49:19 +02001150 return 0;
1151}
1152
1153static int tegra_msi_alloc(struct tegra_msi *chip)
1154{
1155 int msi;
1156
1157 mutex_lock(&chip->lock);
1158
1159 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1160 if (msi < INT_PCI_MSI_NR)
1161 set_bit(msi, chip->used);
1162 else
1163 msi = -ENOSPC;
1164
1165 mutex_unlock(&chip->lock);
1166
1167 return msi;
1168}
1169
1170static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1171{
1172 struct device *dev = chip->chip.dev;
1173
1174 mutex_lock(&chip->lock);
1175
1176 if (!test_bit(irq, chip->used))
1177 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1178 else
1179 clear_bit(irq, chip->used);
1180
1181 mutex_unlock(&chip->lock);
1182}
1183
1184static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1185{
1186 struct tegra_pcie *pcie = data;
1187 struct tegra_msi *msi = &pcie->msi;
1188 unsigned int i, processed = 0;
1189
1190 for (i = 0; i < 8; i++) {
1191 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1192
1193 while (reg) {
1194 unsigned int offset = find_first_bit(&reg, 32);
1195 unsigned int index = i * 32 + offset;
1196 unsigned int irq;
1197
1198 /* clear the interrupt */
1199 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1200
1201 irq = irq_find_mapping(msi->domain, index);
1202 if (irq) {
1203 if (test_bit(index, msi->used))
1204 generic_handle_irq(irq);
1205 else
1206 dev_info(pcie->dev, "unhandled MSI\n");
1207 } else {
1208 /*
1209 * that's weird who triggered this?
1210 * just clear it
1211 */
1212 dev_info(pcie->dev, "unexpected MSI\n");
1213 }
1214
1215 /* see if there's any more pending in this vector */
1216 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1217
1218 processed++;
1219 }
1220 }
1221
1222 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1223}
1224
Yijing Wangc2791b82014-11-11 17:45:45 -07001225static int tegra_msi_setup_irq(struct msi_controller *chip,
1226 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001227{
1228 struct tegra_msi *msi = to_tegra_msi(chip);
1229 struct msi_msg msg;
1230 unsigned int irq;
1231 int hwirq;
1232
1233 hwirq = tegra_msi_alloc(msi);
1234 if (hwirq < 0)
1235 return hwirq;
1236
1237 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001238 if (!irq) {
1239 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001240 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001241 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001242
1243 irq_set_msi_desc(irq, desc);
1244
1245 msg.address_lo = virt_to_phys((void *)msi->pages);
1246 /* 32 bit address only */
1247 msg.address_hi = 0;
1248 msg.data = hwirq;
1249
Jiang Liu83a18912014-11-09 23:10:34 +08001250 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001251
1252 return 0;
1253}
1254
Yijing Wangc2791b82014-11-11 17:45:45 -07001255static void tegra_msi_teardown_irq(struct msi_controller *chip,
1256 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001257{
1258 struct tegra_msi *msi = to_tegra_msi(chip);
1259 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001260 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001261
Jisheng Zhang019fa462014-07-29 09:33:30 +08001262 irq_dispose_mapping(irq);
1263 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001264}
1265
1266static struct irq_chip tegra_msi_irq_chip = {
1267 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001268 .irq_enable = pci_msi_unmask_irq,
1269 .irq_disable = pci_msi_mask_irq,
1270 .irq_mask = pci_msi_mask_irq,
1271 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001272};
1273
1274static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1275 irq_hw_number_t hwirq)
1276{
1277 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1278 irq_set_chip_data(irq, domain->host_data);
1279 set_irq_flags(irq, IRQF_VALID);
1280
Stephen Warrenb4f17372013-05-06 14:19:19 -06001281 tegra_cpuidle_pcie_irqs_in_use();
1282
Thierry Redingd1523b52013-08-09 16:49:19 +02001283 return 0;
1284}
1285
1286static const struct irq_domain_ops msi_domain_ops = {
1287 .map = tegra_msi_map,
1288};
1289
1290static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1291{
1292 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001293 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001294 struct tegra_msi *msi = &pcie->msi;
1295 unsigned long base;
1296 int err;
1297 u32 reg;
1298
1299 mutex_init(&msi->lock);
1300
1301 msi->chip.dev = pcie->dev;
1302 msi->chip.setup_irq = tegra_msi_setup_irq;
1303 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1304
1305 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1306 &msi_domain_ops, &msi->chip);
1307 if (!msi->domain) {
1308 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1309 return -ENOMEM;
1310 }
1311
1312 err = platform_get_irq_byname(pdev, "msi");
1313 if (err < 0) {
1314 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1315 goto err;
1316 }
1317
1318 msi->irq = err;
1319
1320 err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
1321 tegra_msi_irq_chip.name, pcie);
1322 if (err < 0) {
1323 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1324 goto err;
1325 }
1326
1327 /* setup AFI/FPCI range */
1328 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1329 base = virt_to_phys((void *)msi->pages);
1330
Jay Agarwal94716cd2013-08-09 16:49:24 +02001331 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001332 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1333 /* this register is in 4K increments */
1334 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1335
1336 /* enable all MSI vectors */
1337 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1338 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1339 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1340 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1341 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1342 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1343 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1344 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1345
1346 /* and unmask the MSI interrupt */
1347 reg = afi_readl(pcie, AFI_INTR_MASK);
1348 reg |= AFI_INTR_MASK_MSI_MASK;
1349 afi_writel(pcie, reg, AFI_INTR_MASK);
1350
1351 return 0;
1352
1353err:
1354 irq_domain_remove(msi->domain);
1355 return err;
1356}
1357
1358static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1359{
1360 struct tegra_msi *msi = &pcie->msi;
1361 unsigned int i, irq;
1362 u32 value;
1363
1364 /* mask the MSI interrupt */
1365 value = afi_readl(pcie, AFI_INTR_MASK);
1366 value &= ~AFI_INTR_MASK_MSI_MASK;
1367 afi_writel(pcie, value, AFI_INTR_MASK);
1368
1369 /* disable all MSI vectors */
1370 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1371 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1372 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1373 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1374 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1375 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1376 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1377 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1378
1379 free_pages(msi->pages, 0);
1380
1381 if (msi->irq > 0)
1382 free_irq(msi->irq, pcie);
1383
1384 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1385 irq = irq_find_mapping(msi->domain, i);
1386 if (irq > 0)
1387 irq_dispose_mapping(irq);
1388 }
1389
1390 irq_domain_remove(msi->domain);
1391
1392 return 0;
1393}
1394
1395static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1396 u32 *xbar)
1397{
1398 struct device_node *np = pcie->dev->of_node;
1399
Thierry Reding7f1f0542014-08-26 17:11:38 +02001400 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1401 switch (lanes) {
1402 case 0x0000104:
1403 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1404 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1405 return 0;
1406
1407 case 0x0000102:
1408 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1409 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1410 return 0;
1411 }
1412 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001413 switch (lanes) {
1414 case 0x00000204:
1415 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1416 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1417 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001418
Jay Agarwal94716cd2013-08-09 16:49:24 +02001419 case 0x00020202:
1420 dev_info(pcie->dev, "2x3 configuration\n");
1421 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1422 return 0;
1423
1424 case 0x00010104:
1425 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1426 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1427 return 0;
1428 }
1429 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1430 switch (lanes) {
1431 case 0x00000004:
1432 dev_info(pcie->dev, "single-mode configuration\n");
1433 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1434 return 0;
1435
1436 case 0x00000202:
1437 dev_info(pcie->dev, "dual-mode configuration\n");
1438 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1439 return 0;
1440 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001441 }
1442
1443 return -EINVAL;
1444}
1445
Thierry Reding077fb152014-05-28 16:49:13 +02001446/*
1447 * Check whether a given set of supplies is available in a device tree node.
1448 * This is used to check whether the new or the legacy device tree bindings
1449 * should be used.
1450 */
1451static bool of_regulator_bulk_available(struct device_node *np,
1452 struct regulator_bulk_data *supplies,
1453 unsigned int num_supplies)
1454{
1455 char property[32];
1456 unsigned int i;
1457
1458 for (i = 0; i < num_supplies; i++) {
1459 snprintf(property, 32, "%s-supply", supplies[i].supply);
1460
1461 if (of_find_property(np, property, NULL) == NULL)
1462 return false;
1463 }
1464
1465 return true;
1466}
1467
1468/*
1469 * Old versions of the device tree binding for this device used a set of power
1470 * supplies that didn't match the hardware inputs. This happened to work for a
1471 * number of cases but is not future proof. However to preserve backwards-
1472 * compatibility with old device trees, this function will try to use the old
1473 * set of supplies.
1474 */
1475static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1476{
1477 struct device_node *np = pcie->dev->of_node;
1478
1479 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1480 pcie->num_supplies = 3;
1481 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1482 pcie->num_supplies = 2;
1483
1484 if (pcie->num_supplies == 0) {
1485 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1486 np->full_name);
1487 return -ENODEV;
1488 }
1489
1490 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1491 sizeof(*pcie->supplies),
1492 GFP_KERNEL);
1493 if (!pcie->supplies)
1494 return -ENOMEM;
1495
1496 pcie->supplies[0].supply = "pex-clk";
1497 pcie->supplies[1].supply = "vdd";
1498
1499 if (pcie->num_supplies > 2)
1500 pcie->supplies[2].supply = "avdd";
1501
1502 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1503 pcie->supplies);
1504}
1505
1506/*
1507 * Obtains the list of regulators required for a particular generation of the
1508 * IP block.
1509 *
1510 * This would've been nice to do simply by providing static tables for use
1511 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1512 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1513 * and either seems to be optional depending on which ports are being used.
1514 */
1515static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1516{
1517 struct device_node *np = pcie->dev->of_node;
1518 unsigned int i = 0;
1519
Thierry Reding7f1f0542014-08-26 17:11:38 +02001520 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1521 pcie->num_supplies = 7;
1522
1523 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1524 sizeof(*pcie->supplies),
1525 GFP_KERNEL);
1526 if (!pcie->supplies)
1527 return -ENOMEM;
1528
1529 pcie->supplies[i++].supply = "avddio-pex";
1530 pcie->supplies[i++].supply = "dvddio-pex";
1531 pcie->supplies[i++].supply = "avdd-pex-pll";
1532 pcie->supplies[i++].supply = "hvdd-pex";
1533 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1534 pcie->supplies[i++].supply = "vddio-pex-ctl";
1535 pcie->supplies[i++].supply = "avdd-pll-erefe";
1536 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001537 bool need_pexa = false, need_pexb = false;
1538
1539 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1540 if (lane_mask & 0x0f)
1541 need_pexa = true;
1542
1543 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1544 if (lane_mask & 0x30)
1545 need_pexb = true;
1546
1547 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1548 (need_pexb ? 2 : 0);
1549
1550 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1551 sizeof(*pcie->supplies),
1552 GFP_KERNEL);
1553 if (!pcie->supplies)
1554 return -ENOMEM;
1555
1556 pcie->supplies[i++].supply = "avdd-pex-pll";
1557 pcie->supplies[i++].supply = "hvdd-pex";
1558 pcie->supplies[i++].supply = "vddio-pex-ctl";
1559 pcie->supplies[i++].supply = "avdd-plle";
1560
1561 if (need_pexa) {
1562 pcie->supplies[i++].supply = "avdd-pexa";
1563 pcie->supplies[i++].supply = "vdd-pexa";
1564 }
1565
1566 if (need_pexb) {
1567 pcie->supplies[i++].supply = "avdd-pexb";
1568 pcie->supplies[i++].supply = "vdd-pexb";
1569 }
1570 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1571 pcie->num_supplies = 5;
1572
1573 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1574 sizeof(*pcie->supplies),
1575 GFP_KERNEL);
1576 if (!pcie->supplies)
1577 return -ENOMEM;
1578
1579 pcie->supplies[0].supply = "avdd-pex";
1580 pcie->supplies[1].supply = "vdd-pex";
1581 pcie->supplies[2].supply = "avdd-pex-pll";
1582 pcie->supplies[3].supply = "avdd-plle";
1583 pcie->supplies[4].supply = "vddio-pex-clk";
1584 }
1585
1586 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1587 pcie->num_supplies))
1588 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1589 pcie->supplies);
1590
1591 /*
1592 * If not all regulators are available for this new scheme, assume
1593 * that the device tree complies with an older version of the device
1594 * tree binding.
1595 */
1596 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1597
1598 devm_kfree(pcie->dev, pcie->supplies);
1599 pcie->num_supplies = 0;
1600
1601 return tegra_pcie_get_legacy_regulators(pcie);
1602}
1603
Thierry Redingd1523b52013-08-09 16:49:19 +02001604static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1605{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001606 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001607 struct device_node *np = pcie->dev->of_node, *port;
1608 struct of_pci_range_parser parser;
1609 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001610 u32 lanes = 0, mask = 0;
1611 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001612 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001613 int err;
1614
Thierry Reding41534e52014-08-01 14:15:11 +02001615 memset(&pcie->all, 0, sizeof(pcie->all));
1616 pcie->all.flags = IORESOURCE_MEM;
1617 pcie->all.name = np->full_name;
1618 pcie->all.start = ~0;
1619 pcie->all.end = 0;
1620
Thierry Redingd1523b52013-08-09 16:49:19 +02001621 if (of_pci_range_parser_init(&parser, np)) {
1622 dev_err(pcie->dev, "missing \"ranges\" property\n");
1623 return -EINVAL;
1624 }
1625
Thierry Redingd1523b52013-08-09 16:49:19 +02001626 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001627 err = of_pci_range_to_resource(&range, np, &res);
1628 if (err < 0)
1629 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001630
1631 switch (res.flags & IORESOURCE_TYPE_BITS) {
1632 case IORESOURCE_IO:
Thierry Reding51067872014-11-27 09:54:09 +01001633 memcpy(&pcie->pio, &res, sizeof(res));
1634 pcie->pio.name = np->full_name;
1635
1636 /*
1637 * The Tegra PCIe host bridge uses this to program the
1638 * mapping of the I/O space to the physical address,
1639 * so we override the .start and .end fields here that
1640 * of_pci_range_to_resource() converted to I/O space.
1641 * We also set the IORESOURCE_MEM type to clarify that
1642 * the resource is in the physical memory space.
1643 */
1644 pcie->io.start = range.cpu_addr;
1645 pcie->io.end = range.cpu_addr + range.size - 1;
1646 pcie->io.flags = IORESOURCE_MEM;
1647 pcie->io.name = "I/O";
1648
1649 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001650 break;
1651
1652 case IORESOURCE_MEM:
1653 if (res.flags & IORESOURCE_PREFETCH) {
1654 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001655 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001656 } else {
1657 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001658 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001659 }
1660 break;
1661 }
Thierry Reding41534e52014-08-01 14:15:11 +02001662
1663 if (res.start <= pcie->all.start)
1664 pcie->all.start = res.start;
1665
1666 if (res.end >= pcie->all.end)
1667 pcie->all.end = res.end;
Thierry Redingd1523b52013-08-09 16:49:19 +02001668 }
1669
Thierry Reding41534e52014-08-01 14:15:11 +02001670 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1671 if (err < 0)
1672 return err;
1673
Thierry Redingd1523b52013-08-09 16:49:19 +02001674 err = of_pci_parse_bus_range(np, &pcie->busn);
1675 if (err < 0) {
1676 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1677 err);
1678 pcie->busn.name = np->name;
1679 pcie->busn.start = 0;
1680 pcie->busn.end = 0xff;
1681 pcie->busn.flags = IORESOURCE_BUS;
1682 }
1683
1684 /* parse root ports */
1685 for_each_child_of_node(np, port) {
1686 struct tegra_pcie_port *rp;
1687 unsigned int index;
1688 u32 value;
1689
1690 err = of_pci_get_devfn(port);
1691 if (err < 0) {
1692 dev_err(pcie->dev, "failed to parse address: %d\n",
1693 err);
1694 return err;
1695 }
1696
1697 index = PCI_SLOT(err);
1698
Jay Agarwal94716cd2013-08-09 16:49:24 +02001699 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001700 dev_err(pcie->dev, "invalid port number: %d\n", index);
1701 return -EINVAL;
1702 }
1703
1704 index--;
1705
1706 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1707 if (err < 0) {
1708 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1709 err);
1710 return err;
1711 }
1712
1713 if (value > 16) {
1714 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1715 return -EINVAL;
1716 }
1717
1718 lanes |= value << (index << 3);
1719
Thierry Reding077fb152014-05-28 16:49:13 +02001720 if (!of_device_is_available(port)) {
1721 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001722 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001723 }
1724
1725 mask |= ((1 << value) - 1) << lane;
1726 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001727
1728 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1729 if (!rp)
1730 return -ENOMEM;
1731
1732 err = of_address_to_resource(port, 0, &rp->regs);
1733 if (err < 0) {
1734 dev_err(pcie->dev, "failed to parse address: %d\n",
1735 err);
1736 return err;
1737 }
1738
1739 INIT_LIST_HEAD(&rp->list);
1740 rp->index = index;
1741 rp->lanes = value;
1742 rp->pcie = pcie;
1743
Julia Lawalldc05ee32013-08-26 11:11:09 +02001744 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1745 if (IS_ERR(rp->base))
1746 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001747
1748 list_add_tail(&rp->list, &pcie->ports);
1749 }
1750
1751 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1752 if (err < 0) {
1753 dev_err(pcie->dev, "invalid lane configuration\n");
1754 return err;
1755 }
1756
Thierry Reding077fb152014-05-28 16:49:13 +02001757 err = tegra_pcie_get_regulators(pcie, mask);
1758 if (err < 0)
1759 return err;
1760
Thierry Redingd1523b52013-08-09 16:49:19 +02001761 return 0;
1762}
1763
1764/*
1765 * FIXME: If there are no PCIe cards attached, then calling this function
1766 * can result in the increase of the bootup time as there are big timeout
1767 * loops.
1768 */
1769#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1770static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1771{
1772 unsigned int retries = 3;
1773 unsigned long value;
1774
Thierry Reding7f1f0542014-08-26 17:11:38 +02001775 /* override presence detection */
1776 value = readl(port->base + RP_PRIV_MISC);
1777 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
1778 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
1779 writel(value, port->base + RP_PRIV_MISC);
1780
Thierry Redingd1523b52013-08-09 16:49:19 +02001781 do {
1782 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1783
1784 do {
1785 value = readl(port->base + RP_VEND_XP);
1786
1787 if (value & RP_VEND_XP_DL_UP)
1788 break;
1789
1790 usleep_range(1000, 2000);
1791 } while (--timeout);
1792
1793 if (!timeout) {
1794 dev_err(port->pcie->dev, "link %u down, retrying\n",
1795 port->index);
1796 goto retry;
1797 }
1798
1799 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1800
1801 do {
1802 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1803
1804 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1805 return true;
1806
1807 usleep_range(1000, 2000);
1808 } while (--timeout);
1809
1810retry:
1811 tegra_pcie_port_reset(port);
1812 } while (--retries);
1813
1814 return false;
1815}
1816
1817static int tegra_pcie_enable(struct tegra_pcie *pcie)
1818{
1819 struct tegra_pcie_port *port, *tmp;
1820 struct hw_pci hw;
1821
1822 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1823 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
1824 port->index, port->lanes);
1825
1826 tegra_pcie_port_enable(port);
1827
1828 if (tegra_pcie_port_check_link(port))
1829 continue;
1830
1831 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
1832
1833 tegra_pcie_port_disable(port);
1834 tegra_pcie_port_free(port);
1835 }
1836
1837 memset(&hw, 0, sizeof(hw));
1838
Yijing Wang7ec725b2014-11-11 15:35:05 -07001839#ifdef CONFIG_PCI_MSI
1840 hw.msi_ctrl = &pcie->msi.chip;
1841#endif
1842
Thierry Redingd1523b52013-08-09 16:49:19 +02001843 hw.nr_controllers = 1;
1844 hw.private_data = (void **)&pcie;
1845 hw.setup = tegra_pcie_setup;
1846 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02001847 hw.scan = tegra_pcie_scan_bus;
1848 hw.ops = &tegra_pcie_ops;
1849
1850 pci_common_init_dev(pcie->dev, &hw);
1851
1852 return 0;
1853}
1854
Jay Agarwal94716cd2013-08-09 16:49:24 +02001855static const struct tegra_pcie_soc_data tegra20_pcie_data = {
1856 .num_ports = 2,
1857 .msi_base_shift = 0,
1858 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1859 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1860 .has_pex_clkreq_en = false,
1861 .has_pex_bias_ctrl = false,
1862 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001863 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02001864 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001865};
1866
1867static const struct tegra_pcie_soc_data tegra30_pcie_data = {
1868 .num_ports = 3,
1869 .msi_base_shift = 8,
1870 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1871 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1872 .has_pex_clkreq_en = true,
1873 .has_pex_bias_ctrl = true,
1874 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001875 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02001876 .has_gen2 = false,
1877};
1878
1879static const struct tegra_pcie_soc_data tegra124_pcie_data = {
1880 .num_ports = 2,
1881 .msi_base_shift = 8,
1882 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1883 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1884 .has_pex_clkreq_en = true,
1885 .has_pex_bias_ctrl = true,
1886 .has_intr_prsnt_sense = true,
1887 .has_cml_clk = true,
1888 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001889};
1890
1891static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02001892 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
Jay Agarwal94716cd2013-08-09 16:49:24 +02001893 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
1894 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
1895 { },
1896};
1897MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
1898
Thierry Reding2cb989f2014-07-22 12:30:46 -06001899static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
1900{
1901 struct tegra_pcie *pcie = s->private;
1902
1903 if (list_empty(&pcie->ports))
1904 return NULL;
1905
1906 seq_printf(s, "Index Status\n");
1907
1908 return seq_list_start(&pcie->ports, *pos);
1909}
1910
1911static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
1912{
1913 struct tegra_pcie *pcie = s->private;
1914
1915 return seq_list_next(v, &pcie->ports, pos);
1916}
1917
1918static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
1919{
1920}
1921
1922static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
1923{
1924 bool up = false, active = false;
1925 struct tegra_pcie_port *port;
1926 unsigned int value;
1927
1928 port = list_entry(v, struct tegra_pcie_port, list);
1929
1930 value = readl(port->base + RP_VEND_XP);
1931
1932 if (value & RP_VEND_XP_DL_UP)
1933 up = true;
1934
1935 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1936
1937 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1938 active = true;
1939
1940 seq_printf(s, "%2u ", port->index);
1941
1942 if (up)
1943 seq_printf(s, "up");
1944
1945 if (active) {
1946 if (up)
1947 seq_printf(s, ", ");
1948
1949 seq_printf(s, "active");
1950 }
1951
1952 seq_printf(s, "\n");
1953 return 0;
1954}
1955
1956static const struct seq_operations tegra_pcie_ports_seq_ops = {
1957 .start = tegra_pcie_ports_seq_start,
1958 .next = tegra_pcie_ports_seq_next,
1959 .stop = tegra_pcie_ports_seq_stop,
1960 .show = tegra_pcie_ports_seq_show,
1961};
1962
1963static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
1964{
1965 struct tegra_pcie *pcie = inode->i_private;
1966 struct seq_file *s;
1967 int err;
1968
1969 err = seq_open(file, &tegra_pcie_ports_seq_ops);
1970 if (err)
1971 return err;
1972
1973 s = file->private_data;
1974 s->private = pcie;
1975
1976 return 0;
1977}
1978
1979static const struct file_operations tegra_pcie_ports_ops = {
1980 .owner = THIS_MODULE,
1981 .open = tegra_pcie_ports_open,
1982 .read = seq_read,
1983 .llseek = seq_lseek,
1984 .release = seq_release,
1985};
1986
1987static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
1988{
1989 struct dentry *file;
1990
1991 pcie->debugfs = debugfs_create_dir("pcie", NULL);
1992 if (!pcie->debugfs)
1993 return -ENOMEM;
1994
1995 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
1996 pcie, &tegra_pcie_ports_ops);
1997 if (!file)
1998 goto remove;
1999
2000 return 0;
2001
2002remove:
2003 debugfs_remove_recursive(pcie->debugfs);
2004 pcie->debugfs = NULL;
2005 return -ENOMEM;
2006}
2007
Thierry Redingd1523b52013-08-09 16:49:19 +02002008static int tegra_pcie_probe(struct platform_device *pdev)
2009{
Jay Agarwal94716cd2013-08-09 16:49:24 +02002010 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02002011 struct tegra_pcie *pcie;
2012 int err;
2013
Jay Agarwal94716cd2013-08-09 16:49:24 +02002014 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2015 if (!match)
2016 return -ENODEV;
2017
Thierry Redingd1523b52013-08-09 16:49:19 +02002018 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2019 if (!pcie)
2020 return -ENOMEM;
2021
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002022 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002023 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02002024 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02002025 pcie->dev = &pdev->dev;
2026
2027 err = tegra_pcie_parse_dt(pcie);
2028 if (err < 0)
2029 return err;
2030
2031 pcibios_min_mem = 0;
2032
2033 err = tegra_pcie_get_resources(pcie);
2034 if (err < 0) {
2035 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2036 return err;
2037 }
2038
2039 err = tegra_pcie_enable_controller(pcie);
2040 if (err)
2041 goto put_resources;
2042
2043 /* setup the AFI address translations */
2044 tegra_pcie_setup_translations(pcie);
2045
2046 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2047 err = tegra_pcie_enable_msi(pcie);
2048 if (err < 0) {
2049 dev_err(&pdev->dev,
2050 "failed to enable MSI support: %d\n",
2051 err);
2052 goto put_resources;
2053 }
2054 }
2055
2056 err = tegra_pcie_enable(pcie);
2057 if (err < 0) {
2058 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2059 goto disable_msi;
2060 }
2061
Thierry Reding2cb989f2014-07-22 12:30:46 -06002062 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2063 err = tegra_pcie_debugfs_init(pcie);
2064 if (err < 0)
2065 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2066 err);
2067 }
2068
Thierry Redingd1523b52013-08-09 16:49:19 +02002069 platform_set_drvdata(pdev, pcie);
2070 return 0;
2071
2072disable_msi:
2073 if (IS_ENABLED(CONFIG_PCI_MSI))
2074 tegra_pcie_disable_msi(pcie);
2075put_resources:
2076 tegra_pcie_put_resources(pcie);
2077 return err;
2078}
2079
Thierry Redingd1523b52013-08-09 16:49:19 +02002080static struct platform_driver tegra_pcie_driver = {
2081 .driver = {
2082 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002083 .of_match_table = tegra_pcie_of_match,
2084 .suppress_bind_attrs = true,
2085 },
2086 .probe = tegra_pcie_probe,
2087};
2088module_platform_driver(tegra_pcie_driver);
2089
2090MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
2091MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
Thierry Redingd975cb52014-07-11 08:58:58 +02002092MODULE_LICENSE("GPL v2");