blob: 6e6ef0d3d739b50a635b56441488beea9ce7030d [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
277 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100278 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200279 struct resource mem;
280 struct resource prefetch;
281 struct resource busn;
282
Thierry Reding56e75e22016-02-09 15:52:32 +0100283 struct {
284 resource_size_t mem;
285 resource_size_t io;
286 } offset;
287
Thierry Redingd1523b52013-08-09 16:49:19 +0200288 struct clk *pex_clk;
289 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200290 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200291 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200292
Stephen Warren3127a6b2013-11-06 15:56:58 -0700293 struct reset_control *pex_rst;
294 struct reset_control *afi_rst;
295 struct reset_control *pcie_xrst;
296
Thierry Reding6fe7c182015-11-11 18:25:59 +0100297 bool legacy_phy;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200298 struct phy *phy;
299
Thierry Redingd1523b52013-08-09 16:49:19 +0200300 struct tegra_msi msi;
301
302 struct list_head ports;
Thierry Redingd1523b52013-08-09 16:49:19 +0200303 u32 xbar_config;
304
Thierry Reding077fb152014-05-28 16:49:13 +0200305 struct regulator_bulk_data *supplies;
306 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200307
308 const struct tegra_pcie_soc_data *soc_data;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600309 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200310};
311
312struct tegra_pcie_port {
313 struct tegra_pcie *pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100314 struct device_node *np;
Thierry Redingd1523b52013-08-09 16:49:19 +0200315 struct list_head list;
316 struct resource regs;
317 void __iomem *base;
318 unsigned int index;
319 unsigned int lanes;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100320
321 struct phy **phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200322};
323
324struct tegra_pcie_bus {
325 struct vm_struct *area;
326 struct list_head list;
327 unsigned int nr;
328};
329
330static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
331{
332 return sys->private_data;
333}
334
335static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
336 unsigned long offset)
337{
338 writel(value, pcie->afi + offset);
339}
340
341static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
342{
343 return readl(pcie->afi + offset);
344}
345
346static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
347 unsigned long offset)
348{
349 writel(value, pcie->pads + offset);
350}
351
352static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
353{
354 return readl(pcie->pads + offset);
355}
356
357/*
358 * The configuration space mapping on Tegra is somewhat similar to the ECAM
359 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
360 * register accesses are mapped:
361 *
362 * [27:24] extended register number
363 * [23:16] bus number
364 * [15:11] device number
365 * [10: 8] function number
366 * [ 7: 0] register number
367 *
368 * Mapping the whole extended configuration space would require 256 MiB of
369 * virtual address space, only a small part of which will actually be used.
370 * To work around this, a 1 MiB of virtual addresses are allocated per bus
371 * when the bus is first accessed. When the physical range is mapped, the
372 * the bus number bits are hidden so that the extended register number bits
373 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
374 *
375 * [19:16] extended register number
376 * [15:11] device number
377 * [10: 8] function number
378 * [ 7: 0] register number
379 *
380 * This is achieved by stitching together 16 chunks of 64 KiB of physical
381 * address space via the MMU.
382 */
383static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
384{
385 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
386 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
387}
388
389static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
390 unsigned int busnr)
391{
Ard Biesheuvel512ee2f2015-08-26 10:49:34 +0200392 pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
393 L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
Thierry Redingd1523b52013-08-09 16:49:19 +0200394 phys_addr_t cs = pcie->cs->start;
395 struct tegra_pcie_bus *bus;
396 unsigned int i;
397 int err;
398
399 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
400 if (!bus)
401 return ERR_PTR(-ENOMEM);
402
403 INIT_LIST_HEAD(&bus->list);
404 bus->nr = busnr;
405
406 /* allocate 1 MiB of virtual addresses */
407 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
408 if (!bus->area) {
409 err = -ENOMEM;
410 goto free;
411 }
412
413 /* map each of the 16 chunks of 64 KiB each */
414 for (i = 0; i < 16; i++) {
415 unsigned long virt = (unsigned long)bus->area->addr +
416 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200417 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200418
419 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
420 if (err < 0) {
421 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
422 err);
423 goto unmap;
424 }
425 }
426
427 return bus;
428
429unmap:
430 vunmap(bus->area->addr);
431free:
432 kfree(bus);
433 return ERR_PTR(err);
434}
435
Thierry Redingb4d18d72016-02-09 15:30:48 +0100436static int tegra_pcie_add_bus(struct pci_bus *bus)
Thierry Redingd1523b52013-08-09 16:49:19 +0200437{
Thierry Redingb4d18d72016-02-09 15:30:48 +0100438 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
439 struct tegra_pcie_bus *b;
Thierry Redingd1523b52013-08-09 16:49:19 +0200440
Thierry Redingb4d18d72016-02-09 15:30:48 +0100441 b = tegra_pcie_bus_alloc(pcie, bus->number);
442 if (IS_ERR(b))
443 return PTR_ERR(b);
Thierry Redingd1523b52013-08-09 16:49:19 +0200444
Thierry Redingb4d18d72016-02-09 15:30:48 +0100445 list_add_tail(&b->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200446
Thierry Redingb4d18d72016-02-09 15:30:48 +0100447 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200448}
449
Thierry Redingb4d18d72016-02-09 15:30:48 +0100450static void tegra_pcie_remove_bus(struct pci_bus *child)
451{
452 struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
453 struct tegra_pcie_bus *bus, *tmp;
454
455 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
456 if (bus->nr == child->number) {
457 vunmap(bus->area->addr);
458 list_del(&bus->list);
459 kfree(bus);
460 break;
461 }
462 }
463}
464
465static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
466 unsigned int devfn,
467 int where)
Thierry Redingd1523b52013-08-09 16:49:19 +0200468{
469 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
470 void __iomem *addr = NULL;
471
472 if (bus->number == 0) {
473 unsigned int slot = PCI_SLOT(devfn);
474 struct tegra_pcie_port *port;
475
476 list_for_each_entry(port, &pcie->ports, list) {
477 if (port->index + 1 == slot) {
478 addr = port->base + (where & ~3);
479 break;
480 }
481 }
482 } else {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100483 struct tegra_pcie_bus *b;
484
485 list_for_each_entry(b, &pcie->buses, list)
486 if (b->nr == bus->number)
487 addr = (void __iomem *)b->area->addr;
488
Thierry Redingd1523b52013-08-09 16:49:19 +0200489 if (!addr) {
490 dev_err(pcie->dev,
491 "failed to map cfg. space for bus %u\n",
492 bus->number);
493 return NULL;
494 }
495
496 addr += tegra_pcie_conf_offset(devfn, where);
497 }
498
499 return addr;
500}
501
Thierry Redingd1523b52013-08-09 16:49:19 +0200502static struct pci_ops tegra_pcie_ops = {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100503 .add_bus = tegra_pcie_add_bus,
504 .remove_bus = tegra_pcie_remove_bus,
505 .map_bus = tegra_pcie_map_bus,
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600506 .read = pci_generic_config_read32,
507 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200508};
509
510static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
511{
512 unsigned long ret = 0;
513
514 switch (port->index) {
515 case 0:
516 ret = AFI_PEX0_CTRL;
517 break;
518
519 case 1:
520 ret = AFI_PEX1_CTRL;
521 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200522
523 case 2:
524 ret = AFI_PEX2_CTRL;
525 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200526 }
527
528 return ret;
529}
530
531static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
532{
533 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
534 unsigned long value;
535
536 /* pulse reset signal */
537 value = afi_readl(port->pcie, ctrl);
538 value &= ~AFI_PEX_CTRL_RST;
539 afi_writel(port->pcie, value, ctrl);
540
541 usleep_range(1000, 2000);
542
543 value = afi_readl(port->pcie, ctrl);
544 value |= AFI_PEX_CTRL_RST;
545 afi_writel(port->pcie, value, ctrl);
546}
547
548static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
549{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200550 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200551 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
552 unsigned long value;
553
554 /* enable reference clock */
555 value = afi_readl(port->pcie, ctrl);
556 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200557
558 if (soc->has_pex_clkreq_en)
559 value |= AFI_PEX_CTRL_CLKREQ_EN;
560
Thierry Reding7f1f0542014-08-26 17:11:38 +0200561 value |= AFI_PEX_CTRL_OVERRIDE_EN;
562
Thierry Redingd1523b52013-08-09 16:49:19 +0200563 afi_writel(port->pcie, value, ctrl);
564
565 tegra_pcie_port_reset(port);
566}
567
568static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
569{
Thierry Reding0d20d622014-08-26 17:11:35 +0200570 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200571 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
572 unsigned long value;
573
574 /* assert port reset */
575 value = afi_readl(port->pcie, ctrl);
576 value &= ~AFI_PEX_CTRL_RST;
577 afi_writel(port->pcie, value, ctrl);
578
579 /* disable reference clock */
580 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200581
582 if (soc->has_pex_clkreq_en)
583 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
584
Thierry Redingd1523b52013-08-09 16:49:19 +0200585 value &= ~AFI_PEX_CTRL_REFCLK_EN;
586 afi_writel(port->pcie, value, ctrl);
587}
588
589static void tegra_pcie_port_free(struct tegra_pcie_port *port)
590{
591 struct tegra_pcie *pcie = port->pcie;
592
593 devm_iounmap(pcie->dev, port->base);
594 devm_release_mem_region(pcie->dev, port->regs.start,
595 resource_size(&port->regs));
596 list_del(&port->list);
597 devm_kfree(pcie->dev, port);
598}
599
Thierry Redingd1523b52013-08-09 16:49:19 +0200600/* Tegra PCIE root complex wrongly reports device class */
601static void tegra_pcie_fixup_class(struct pci_dev *dev)
602{
603 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
604}
605DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
606DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200607DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
608DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200609
610/* Tegra PCIE requires relaxed ordering */
611static void tegra_pcie_relax_enable(struct pci_dev *dev)
612{
613 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
614}
615DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
616
617static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
618{
619 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200620 int err;
621
Thierry Reding56e75e22016-02-09 15:52:32 +0100622 sys->mem_offset = pcie->offset.mem;
623 sys->io_offset = pcie->offset.io;
624
Bjorn Helgaasc4102c922016-06-06 15:55:04 -0500625 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->io);
Thierry Reding56e75e22016-02-09 15:52:32 +0100626 if (err < 0)
627 return err;
628
Bjorn Helgaas45c64b62016-06-06 15:47:24 -0500629 pci_ioremap_io(pcie->pio.start, pcie->io.start);
Thierry Redingd1523b52013-08-09 16:49:19 +0200630
Thierry Reding56e75e22016-02-09 15:52:32 +0100631 pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
Thierry Redingd1523b52013-08-09 16:49:19 +0200632 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
633 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
634 sys->mem_offset);
635 pci_add_resource(&sys->resources, &pcie->busn);
636
Bjorn Helgaas45c64b62016-06-06 15:47:24 -0500637 err = devm_request_pci_bus_resources(pcie->dev, &sys->resources);
638 if (err < 0)
639 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200640
641 return 1;
642}
643
644static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
645{
646 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600647 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200648
Stephen Warrenb4f17372013-05-06 14:19:19 -0600649 tegra_cpuidle_pcie_irqs_in_use();
650
Lucas Stachf5d33522014-04-16 10:24:32 -0600651 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
652 if (!irq)
653 irq = pcie->irq;
654
655 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200656}
657
Thierry Redingd1523b52013-08-09 16:49:19 +0200658static irqreturn_t tegra_pcie_isr(int irq, void *arg)
659{
660 const char *err_msg[] = {
661 "Unknown",
662 "AXI slave error",
663 "AXI decode error",
664 "Target abort",
665 "Master abort",
666 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200667 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200668 "Response decoding error",
669 "AXI response decoding error",
670 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200671 "Slot present pin change",
672 "Slot clock request change",
673 "TMS clock ramp change",
674 "TMS ready for power down",
675 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200676 };
677 struct tegra_pcie *pcie = arg;
678 u32 code, signature;
679
680 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
681 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
682 afi_writel(pcie, 0, AFI_INTR_CODE);
683
684 if (code == AFI_INTR_LEGACY)
685 return IRQ_NONE;
686
687 if (code >= ARRAY_SIZE(err_msg))
688 code = 0;
689
690 /*
691 * do not pollute kernel log with master abort reports since they
692 * happen a lot during enumeration
693 */
694 if (code == AFI_INTR_MASTER_ABORT)
695 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
696 signature);
697 else
698 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
699 signature);
700
701 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
702 code == AFI_INTR_FPCI_DECODE_ERROR) {
703 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
704 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
705
706 if (code == AFI_INTR_MASTER_ABORT)
707 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
708 else
709 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
710 }
711
712 return IRQ_HANDLED;
713}
714
715/*
716 * FPCI map is as follows:
717 * - 0xfdfc000000: I/O space
718 * - 0xfdfe000000: type 0 configuration space
719 * - 0xfdff000000: type 1 configuration space
720 * - 0xfe00000000: type 0 extended configuration space
721 * - 0xfe10000000: type 1 extended configuration space
722 */
723static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
724{
725 u32 fpci_bar, size, axi_address;
726
727 /* Bar 0: type 1 extended configuration space */
728 fpci_bar = 0xfe100000;
729 size = resource_size(pcie->cs);
730 axi_address = pcie->cs->start;
731 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
732 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
733 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
734
735 /* Bar 1: downstream IO bar */
736 fpci_bar = 0xfdfc0000;
737 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100738 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200739 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
740 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
741 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
742
743 /* Bar 2: prefetchable memory BAR */
744 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
745 size = resource_size(&pcie->prefetch);
746 axi_address = pcie->prefetch.start;
747 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
748 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
749 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
750
751 /* Bar 3: non prefetchable memory BAR */
752 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
753 size = resource_size(&pcie->mem);
754 axi_address = pcie->mem.start;
755 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
756 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
757 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
758
759 /* NULL out the remaining BARs as they are not used */
760 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
761 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
762 afi_writel(pcie, 0, AFI_FPCI_BAR4);
763
764 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
765 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
766 afi_writel(pcie, 0, AFI_FPCI_BAR5);
767
768 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100769 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200770 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
771 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
772 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
773
774 /* MSI translations are setup only when needed */
775 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
776 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
777 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
778 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
779}
780
Thierry Reding7f1f0542014-08-26 17:11:38 +0200781static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200782{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200783 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200784 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200785
Thierry Reding7f1f0542014-08-26 17:11:38 +0200786 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200787
Thierry Reding7f1f0542014-08-26 17:11:38 +0200788 while (time_before(jiffies, timeout)) {
789 value = pads_readl(pcie, soc->pads_pll_ctl);
790 if (value & PADS_PLL_CTL_LOCKDET)
791 return 0;
792 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200793
Thierry Reding7f1f0542014-08-26 17:11:38 +0200794 return -ETIMEDOUT;
795}
Thierry Redingd1523b52013-08-09 16:49:19 +0200796
Thierry Reding7f1f0542014-08-26 17:11:38 +0200797static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
798{
799 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
800 u32 value;
801 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200802
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700803 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200804 pads_writel(pcie, 0x0, PADS_CTL_SEL);
805
806 /* override IDDQ to 1 on all 4 lanes */
807 value = pads_readl(pcie, PADS_CTL);
808 value |= PADS_CTL_IDDQ_1L;
809 pads_writel(pcie, value, PADS_CTL);
810
811 /*
812 * Set up PHY PLL inputs select PLLE output as refclock,
813 * set TX ref sel to div10 (not div5).
814 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200815 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200816 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200817 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
818 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200819
Eric Yuenec732762014-08-26 17:11:37 +0200820 /* reset PLL */
821 value = pads_readl(pcie, soc->pads_pll_ctl);
822 value &= ~PADS_PLL_CTL_RST_B4SM;
823 pads_writel(pcie, value, soc->pads_pll_ctl);
824
825 usleep_range(20, 100);
826
Thierry Redingd1523b52013-08-09 16:49:19 +0200827 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200828 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200829 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200830 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200831
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200832 /* Configure the reference clock driver */
833 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
834 pads_writel(pcie, value, PADS_REFCLK_CFG0);
835 if (soc->num_ports > 2)
836 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200837
838 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200839 err = tegra_pcie_pll_wait(pcie, 500);
840 if (err < 0) {
841 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
842 return err;
843 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200844
845 /* turn off IDDQ override */
846 value = pads_readl(pcie, PADS_CTL);
847 value &= ~PADS_CTL_IDDQ_1L;
848 pads_writel(pcie, value, PADS_CTL);
849
850 /* enable TX/RX data */
851 value = pads_readl(pcie, PADS_CTL);
852 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
853 pads_writel(pcie, value, PADS_CTL);
854
Thierry Reding7f1f0542014-08-26 17:11:38 +0200855 return 0;
856}
857
Thierry Reding6fe7c182015-11-11 18:25:59 +0100858static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
859{
860 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
861 u32 value;
862
863 /* disable TX/RX data */
864 value = pads_readl(pcie, PADS_CTL);
865 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
866 pads_writel(pcie, value, PADS_CTL);
867
868 /* override IDDQ */
869 value = pads_readl(pcie, PADS_CTL);
870 value |= PADS_CTL_IDDQ_1L;
871 pads_writel(pcie, PADS_CTL, value);
872
873 /* reset PLL */
874 value = pads_readl(pcie, soc->pads_pll_ctl);
875 value &= ~PADS_PLL_CTL_RST_B4SM;
876 pads_writel(pcie, value, soc->pads_pll_ctl);
877
878 usleep_range(20, 100);
879
880 return 0;
881}
882
883static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
884{
885 struct device *dev = port->pcie->dev;
886 unsigned int i;
887 int err;
888
889 for (i = 0; i < port->lanes; i++) {
890 err = phy_power_on(port->phys[i]);
891 if (err < 0) {
892 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
893 err);
894 return err;
895 }
896 }
897
898 return 0;
899}
900
901static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
902{
903 struct device *dev = port->pcie->dev;
904 unsigned int i;
905 int err;
906
907 for (i = 0; i < port->lanes; i++) {
908 err = phy_power_off(port->phys[i]);
909 if (err < 0) {
910 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
911 err);
912 return err;
913 }
914 }
915
916 return 0;
917}
918
919static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
920{
921 struct tegra_pcie_port *port;
922 int err;
923
924 if (pcie->legacy_phy) {
925 if (pcie->phy)
926 err = phy_power_on(pcie->phy);
927 else
928 err = tegra_pcie_phy_enable(pcie);
929
930 if (err < 0)
931 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
932
933 return err;
934 }
935
936 list_for_each_entry(port, &pcie->ports, list) {
937 err = tegra_pcie_port_phy_power_on(port);
938 if (err < 0) {
939 dev_err(pcie->dev,
940 "failed to power on PCIe port %u PHY: %d\n",
941 port->index, err);
942 return err;
943 }
944 }
945
946 return 0;
947}
948
949static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
950{
951 struct tegra_pcie_port *port;
952 int err;
953
954 if (pcie->legacy_phy) {
955 if (pcie->phy)
956 err = phy_power_off(pcie->phy);
957 else
958 err = tegra_pcie_phy_disable(pcie);
959
960 if (err < 0)
961 dev_err(pcie->dev, "failed to power off PHY: %d\n",
962 err);
963
964 return err;
965 }
966
967 list_for_each_entry(port, &pcie->ports, list) {
968 err = tegra_pcie_port_phy_power_off(port);
969 if (err < 0) {
970 dev_err(pcie->dev,
971 "failed to power off PCIe port %u PHY: %d\n",
972 port->index, err);
973 return err;
974 }
975 }
976
977 return 0;
978}
979
Thierry Reding7f1f0542014-08-26 17:11:38 +0200980static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
981{
982 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
983 struct tegra_pcie_port *port;
984 unsigned long value;
985 int err;
986
987 /* enable PLL power down */
988 if (pcie->phy) {
989 value = afi_readl(pcie, AFI_PLLE_CONTROL);
990 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
991 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
992 afi_writel(pcie, value, AFI_PLLE_CONTROL);
993 }
994
995 /* power down PCIe slot clock bias pad */
996 if (soc->has_pex_bias_ctrl)
997 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
998
999 /* configure mode and disable all ports */
1000 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1001 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1002 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1003
1004 list_for_each_entry(port, &pcie->ports, list)
1005 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1006
1007 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1008
1009 if (soc->has_gen2) {
1010 value = afi_readl(pcie, AFI_FUSE);
1011 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1012 afi_writel(pcie, value, AFI_FUSE);
1013 } else {
1014 value = afi_readl(pcie, AFI_FUSE);
1015 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1016 afi_writel(pcie, value, AFI_FUSE);
1017 }
1018
Thierry Reding6fe7c182015-11-11 18:25:59 +01001019 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001020 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001021 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001022 return err;
1023 }
1024
Thierry Redingd1523b52013-08-09 16:49:19 +02001025 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001026 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001027
1028 /* finally enable PCIe */
1029 value = afi_readl(pcie, AFI_CONFIGURATION);
1030 value |= AFI_CONFIGURATION_EN_FPCI;
1031 afi_writel(pcie, value, AFI_CONFIGURATION);
1032
1033 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1034 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1035 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001036
1037 if (soc->has_intr_prsnt_sense)
1038 value |= AFI_INTR_EN_PRSNT_SENSE;
1039
Thierry Redingd1523b52013-08-09 16:49:19 +02001040 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1041 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1042
1043 /* don't enable MSI for now, only when needed */
1044 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1045
1046 /* disable all exceptions */
1047 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1048
1049 return 0;
1050}
1051
1052static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1053{
1054 int err;
1055
1056 /* TODO: disable and unprepare clocks? */
1057
Thierry Reding6fe7c182015-11-11 18:25:59 +01001058 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001059 if (err < 0)
Thierry Reding6fe7c182015-11-11 18:25:59 +01001060 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001061
Stephen Warren3127a6b2013-11-06 15:56:58 -07001062 reset_control_assert(pcie->pcie_xrst);
1063 reset_control_assert(pcie->afi_rst);
1064 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001065
1066 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1067
Thierry Reding077fb152014-05-28 16:49:13 +02001068 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001069 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +02001070 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001071}
1072
1073static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1074{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001075 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001076 int err;
1077
Stephen Warren3127a6b2013-11-06 15:56:58 -07001078 reset_control_assert(pcie->pcie_xrst);
1079 reset_control_assert(pcie->afi_rst);
1080 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001081
1082 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1083
1084 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001085 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1086 if (err < 0)
1087 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001088
Thierry Redingd1523b52013-08-09 16:49:19 +02001089 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001090 pcie->pex_clk,
1091 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001092 if (err) {
1093 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1094 return err;
1095 }
1096
Stephen Warren3127a6b2013-11-06 15:56:58 -07001097 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001098
1099 err = clk_prepare_enable(pcie->afi_clk);
1100 if (err < 0) {
1101 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1102 return err;
1103 }
1104
Jay Agarwal94716cd2013-08-09 16:49:24 +02001105 if (soc->has_cml_clk) {
1106 err = clk_prepare_enable(pcie->cml_clk);
1107 if (err < 0) {
1108 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1109 err);
1110 return err;
1111 }
1112 }
1113
Thierry Redingd1523b52013-08-09 16:49:19 +02001114 err = clk_prepare_enable(pcie->pll_e);
1115 if (err < 0) {
1116 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1117 return err;
1118 }
1119
1120 return 0;
1121}
1122
1123static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1124{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001125 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1126
Thierry Redingd1523b52013-08-09 16:49:19 +02001127 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1128 if (IS_ERR(pcie->pex_clk))
1129 return PTR_ERR(pcie->pex_clk);
1130
1131 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1132 if (IS_ERR(pcie->afi_clk))
1133 return PTR_ERR(pcie->afi_clk);
1134
Thierry Redingd1523b52013-08-09 16:49:19 +02001135 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1136 if (IS_ERR(pcie->pll_e))
1137 return PTR_ERR(pcie->pll_e);
1138
Jay Agarwal94716cd2013-08-09 16:49:24 +02001139 if (soc->has_cml_clk) {
1140 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1141 if (IS_ERR(pcie->cml_clk))
1142 return PTR_ERR(pcie->cml_clk);
1143 }
1144
Thierry Redingd1523b52013-08-09 16:49:19 +02001145 return 0;
1146}
1147
Stephen Warren3127a6b2013-11-06 15:56:58 -07001148static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1149{
1150 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1151 if (IS_ERR(pcie->pex_rst))
1152 return PTR_ERR(pcie->pex_rst);
1153
1154 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1155 if (IS_ERR(pcie->afi_rst))
1156 return PTR_ERR(pcie->afi_rst);
1157
1158 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1159 if (IS_ERR(pcie->pcie_xrst))
1160 return PTR_ERR(pcie->pcie_xrst);
1161
1162 return 0;
1163}
1164
Thierry Reding6fe7c182015-11-11 18:25:59 +01001165static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1166{
1167 int err;
1168
1169 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1170 if (IS_ERR(pcie->phy)) {
1171 err = PTR_ERR(pcie->phy);
1172 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1173 return err;
1174 }
1175
1176 err = phy_init(pcie->phy);
1177 if (err < 0) {
1178 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1179 return err;
1180 }
1181
1182 pcie->legacy_phy = true;
1183
1184 return 0;
1185}
1186
1187static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1188 struct device_node *np,
1189 const char *consumer,
1190 unsigned int index)
1191{
1192 struct phy *phy;
1193 char *name;
1194
1195 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1196 if (!name)
1197 return ERR_PTR(-ENOMEM);
1198
1199 phy = devm_of_phy_get(dev, np, name);
1200 kfree(name);
1201
1202 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1203 phy = NULL;
1204
1205 return phy;
1206}
1207
1208static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1209{
1210 struct device *dev = port->pcie->dev;
1211 struct phy *phy;
1212 unsigned int i;
1213 int err;
1214
1215 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1216 if (!port->phys)
1217 return -ENOMEM;
1218
1219 for (i = 0; i < port->lanes; i++) {
1220 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1221 if (IS_ERR(phy)) {
1222 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1223 PTR_ERR(phy));
1224 return PTR_ERR(phy);
1225 }
1226
1227 err = phy_init(phy);
1228 if (err < 0) {
1229 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1230 err);
1231 return err;
1232 }
1233
1234 port->phys[i] = phy;
1235 }
1236
1237 return 0;
1238}
1239
1240static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1241{
1242 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1243 struct device_node *np = pcie->dev->of_node;
1244 struct tegra_pcie_port *port;
1245 int err;
1246
1247 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1248 return tegra_pcie_phys_get_legacy(pcie);
1249
1250 list_for_each_entry(port, &pcie->ports, list) {
1251 err = tegra_pcie_port_get_phys(port);
1252 if (err < 0)
1253 return err;
1254 }
1255
1256 return 0;
1257}
1258
Thierry Redingd1523b52013-08-09 16:49:19 +02001259static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1260{
1261 struct platform_device *pdev = to_platform_device(pcie->dev);
1262 struct resource *pads, *afi, *res;
1263 int err;
1264
1265 err = tegra_pcie_clocks_get(pcie);
1266 if (err) {
1267 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1268 return err;
1269 }
1270
Stephen Warren3127a6b2013-11-06 15:56:58 -07001271 err = tegra_pcie_resets_get(pcie);
1272 if (err) {
1273 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1274 return err;
1275 }
1276
Thierry Reding6fe7c182015-11-11 18:25:59 +01001277 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001278 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001279 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001280 return err;
1281 }
1282
Thierry Redingd1523b52013-08-09 16:49:19 +02001283 err = tegra_pcie_power_on(pcie);
1284 if (err) {
1285 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1286 return err;
1287 }
1288
Thierry Redingd1523b52013-08-09 16:49:19 +02001289 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001290 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1291 if (IS_ERR(pcie->pads)) {
1292 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001293 goto poweroff;
1294 }
1295
1296 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001297 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1298 if (IS_ERR(pcie->afi)) {
1299 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001300 goto poweroff;
1301 }
1302
Julia Lawalldc05ee32013-08-26 11:11:09 +02001303 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001304 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1305 if (!res) {
1306 err = -EADDRNOTAVAIL;
1307 goto poweroff;
1308 }
1309
1310 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1311 resource_size(res), res->name);
1312 if (!pcie->cs) {
1313 err = -EADDRNOTAVAIL;
1314 goto poweroff;
1315 }
1316
1317 /* request interrupt */
1318 err = platform_get_irq_byname(pdev, "intr");
1319 if (err < 0) {
1320 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1321 goto poweroff;
1322 }
1323
1324 pcie->irq = err;
1325
1326 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1327 if (err) {
1328 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1329 goto poweroff;
1330 }
1331
1332 return 0;
1333
1334poweroff:
1335 tegra_pcie_power_off(pcie);
1336 return err;
1337}
1338
1339static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1340{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001341 int err;
1342
Thierry Redingd1523b52013-08-09 16:49:19 +02001343 if (pcie->irq > 0)
1344 free_irq(pcie->irq, pcie);
1345
1346 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001347
1348 err = phy_exit(pcie->phy);
1349 if (err < 0)
1350 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1351
Thierry Redingd1523b52013-08-09 16:49:19 +02001352 return 0;
1353}
1354
1355static int tegra_msi_alloc(struct tegra_msi *chip)
1356{
1357 int msi;
1358
1359 mutex_lock(&chip->lock);
1360
1361 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1362 if (msi < INT_PCI_MSI_NR)
1363 set_bit(msi, chip->used);
1364 else
1365 msi = -ENOSPC;
1366
1367 mutex_unlock(&chip->lock);
1368
1369 return msi;
1370}
1371
1372static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1373{
1374 struct device *dev = chip->chip.dev;
1375
1376 mutex_lock(&chip->lock);
1377
1378 if (!test_bit(irq, chip->used))
1379 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1380 else
1381 clear_bit(irq, chip->used);
1382
1383 mutex_unlock(&chip->lock);
1384}
1385
1386static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1387{
1388 struct tegra_pcie *pcie = data;
1389 struct tegra_msi *msi = &pcie->msi;
1390 unsigned int i, processed = 0;
1391
1392 for (i = 0; i < 8; i++) {
1393 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1394
1395 while (reg) {
1396 unsigned int offset = find_first_bit(&reg, 32);
1397 unsigned int index = i * 32 + offset;
1398 unsigned int irq;
1399
1400 /* clear the interrupt */
1401 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1402
1403 irq = irq_find_mapping(msi->domain, index);
1404 if (irq) {
1405 if (test_bit(index, msi->used))
1406 generic_handle_irq(irq);
1407 else
1408 dev_info(pcie->dev, "unhandled MSI\n");
1409 } else {
1410 /*
1411 * that's weird who triggered this?
1412 * just clear it
1413 */
1414 dev_info(pcie->dev, "unexpected MSI\n");
1415 }
1416
1417 /* see if there's any more pending in this vector */
1418 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1419
1420 processed++;
1421 }
1422 }
1423
1424 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1425}
1426
Yijing Wangc2791b82014-11-11 17:45:45 -07001427static int tegra_msi_setup_irq(struct msi_controller *chip,
1428 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001429{
1430 struct tegra_msi *msi = to_tegra_msi(chip);
1431 struct msi_msg msg;
1432 unsigned int irq;
1433 int hwirq;
1434
1435 hwirq = tegra_msi_alloc(msi);
1436 if (hwirq < 0)
1437 return hwirq;
1438
1439 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001440 if (!irq) {
1441 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001442 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001443 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001444
1445 irq_set_msi_desc(irq, desc);
1446
1447 msg.address_lo = virt_to_phys((void *)msi->pages);
1448 /* 32 bit address only */
1449 msg.address_hi = 0;
1450 msg.data = hwirq;
1451
Jiang Liu83a18912014-11-09 23:10:34 +08001452 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001453
1454 return 0;
1455}
1456
Yijing Wangc2791b82014-11-11 17:45:45 -07001457static void tegra_msi_teardown_irq(struct msi_controller *chip,
1458 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001459{
1460 struct tegra_msi *msi = to_tegra_msi(chip);
1461 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001462 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001463
Jisheng Zhang019fa462014-07-29 09:33:30 +08001464 irq_dispose_mapping(irq);
1465 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001466}
1467
1468static struct irq_chip tegra_msi_irq_chip = {
1469 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001470 .irq_enable = pci_msi_unmask_irq,
1471 .irq_disable = pci_msi_mask_irq,
1472 .irq_mask = pci_msi_mask_irq,
1473 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001474};
1475
1476static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1477 irq_hw_number_t hwirq)
1478{
1479 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1480 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001481
Stephen Warrenb4f17372013-05-06 14:19:19 -06001482 tegra_cpuidle_pcie_irqs_in_use();
1483
Thierry Redingd1523b52013-08-09 16:49:19 +02001484 return 0;
1485}
1486
1487static const struct irq_domain_ops msi_domain_ops = {
1488 .map = tegra_msi_map,
1489};
1490
1491static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1492{
1493 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001494 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001495 struct tegra_msi *msi = &pcie->msi;
1496 unsigned long base;
1497 int err;
1498 u32 reg;
1499
1500 mutex_init(&msi->lock);
1501
1502 msi->chip.dev = pcie->dev;
1503 msi->chip.setup_irq = tegra_msi_setup_irq;
1504 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1505
1506 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1507 &msi_domain_ops, &msi->chip);
1508 if (!msi->domain) {
1509 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1510 return -ENOMEM;
1511 }
1512
1513 err = platform_get_irq_byname(pdev, "msi");
1514 if (err < 0) {
1515 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1516 goto err;
1517 }
1518
1519 msi->irq = err;
1520
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001521 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001522 tegra_msi_irq_chip.name, pcie);
1523 if (err < 0) {
1524 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1525 goto err;
1526 }
1527
1528 /* setup AFI/FPCI range */
1529 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1530 base = virt_to_phys((void *)msi->pages);
1531
Jay Agarwal94716cd2013-08-09 16:49:24 +02001532 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001533 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1534 /* this register is in 4K increments */
1535 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1536
1537 /* enable all MSI vectors */
1538 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1539 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1540 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1541 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1542 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1543 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1544 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1545 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1546
1547 /* and unmask the MSI interrupt */
1548 reg = afi_readl(pcie, AFI_INTR_MASK);
1549 reg |= AFI_INTR_MASK_MSI_MASK;
1550 afi_writel(pcie, reg, AFI_INTR_MASK);
1551
1552 return 0;
1553
1554err:
1555 irq_domain_remove(msi->domain);
1556 return err;
1557}
1558
1559static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1560{
1561 struct tegra_msi *msi = &pcie->msi;
1562 unsigned int i, irq;
1563 u32 value;
1564
1565 /* mask the MSI interrupt */
1566 value = afi_readl(pcie, AFI_INTR_MASK);
1567 value &= ~AFI_INTR_MASK_MSI_MASK;
1568 afi_writel(pcie, value, AFI_INTR_MASK);
1569
1570 /* disable all MSI vectors */
1571 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1572 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1573 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1574 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1575 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1576 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1577 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1578 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1579
1580 free_pages(msi->pages, 0);
1581
1582 if (msi->irq > 0)
1583 free_irq(msi->irq, pcie);
1584
1585 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1586 irq = irq_find_mapping(msi->domain, i);
1587 if (irq > 0)
1588 irq_dispose_mapping(irq);
1589 }
1590
1591 irq_domain_remove(msi->domain);
1592
1593 return 0;
1594}
1595
1596static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1597 u32 *xbar)
1598{
1599 struct device_node *np = pcie->dev->of_node;
1600
Thierry Reding7f1f0542014-08-26 17:11:38 +02001601 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1602 switch (lanes) {
1603 case 0x0000104:
1604 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1605 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1606 return 0;
1607
1608 case 0x0000102:
1609 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1610 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1611 return 0;
1612 }
1613 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001614 switch (lanes) {
1615 case 0x00000204:
1616 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1617 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1618 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001619
Jay Agarwal94716cd2013-08-09 16:49:24 +02001620 case 0x00020202:
1621 dev_info(pcie->dev, "2x3 configuration\n");
1622 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1623 return 0;
1624
1625 case 0x00010104:
1626 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1627 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1628 return 0;
1629 }
1630 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1631 switch (lanes) {
1632 case 0x00000004:
1633 dev_info(pcie->dev, "single-mode configuration\n");
1634 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1635 return 0;
1636
1637 case 0x00000202:
1638 dev_info(pcie->dev, "dual-mode configuration\n");
1639 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1640 return 0;
1641 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001642 }
1643
1644 return -EINVAL;
1645}
1646
Thierry Reding077fb152014-05-28 16:49:13 +02001647/*
1648 * Check whether a given set of supplies is available in a device tree node.
1649 * This is used to check whether the new or the legacy device tree bindings
1650 * should be used.
1651 */
1652static bool of_regulator_bulk_available(struct device_node *np,
1653 struct regulator_bulk_data *supplies,
1654 unsigned int num_supplies)
1655{
1656 char property[32];
1657 unsigned int i;
1658
1659 for (i = 0; i < num_supplies; i++) {
1660 snprintf(property, 32, "%s-supply", supplies[i].supply);
1661
1662 if (of_find_property(np, property, NULL) == NULL)
1663 return false;
1664 }
1665
1666 return true;
1667}
1668
1669/*
1670 * Old versions of the device tree binding for this device used a set of power
1671 * supplies that didn't match the hardware inputs. This happened to work for a
1672 * number of cases but is not future proof. However to preserve backwards-
1673 * compatibility with old device trees, this function will try to use the old
1674 * set of supplies.
1675 */
1676static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1677{
1678 struct device_node *np = pcie->dev->of_node;
1679
1680 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1681 pcie->num_supplies = 3;
1682 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1683 pcie->num_supplies = 2;
1684
1685 if (pcie->num_supplies == 0) {
1686 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1687 np->full_name);
1688 return -ENODEV;
1689 }
1690
1691 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1692 sizeof(*pcie->supplies),
1693 GFP_KERNEL);
1694 if (!pcie->supplies)
1695 return -ENOMEM;
1696
1697 pcie->supplies[0].supply = "pex-clk";
1698 pcie->supplies[1].supply = "vdd";
1699
1700 if (pcie->num_supplies > 2)
1701 pcie->supplies[2].supply = "avdd";
1702
1703 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1704 pcie->supplies);
1705}
1706
1707/*
1708 * Obtains the list of regulators required for a particular generation of the
1709 * IP block.
1710 *
1711 * This would've been nice to do simply by providing static tables for use
1712 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1713 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1714 * and either seems to be optional depending on which ports are being used.
1715 */
1716static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1717{
1718 struct device_node *np = pcie->dev->of_node;
1719 unsigned int i = 0;
1720
Thierry Reding7f1f0542014-08-26 17:11:38 +02001721 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1722 pcie->num_supplies = 7;
1723
1724 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1725 sizeof(*pcie->supplies),
1726 GFP_KERNEL);
1727 if (!pcie->supplies)
1728 return -ENOMEM;
1729
1730 pcie->supplies[i++].supply = "avddio-pex";
1731 pcie->supplies[i++].supply = "dvddio-pex";
1732 pcie->supplies[i++].supply = "avdd-pex-pll";
1733 pcie->supplies[i++].supply = "hvdd-pex";
1734 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1735 pcie->supplies[i++].supply = "vddio-pex-ctl";
1736 pcie->supplies[i++].supply = "avdd-pll-erefe";
1737 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001738 bool need_pexa = false, need_pexb = false;
1739
1740 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1741 if (lane_mask & 0x0f)
1742 need_pexa = true;
1743
1744 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1745 if (lane_mask & 0x30)
1746 need_pexb = true;
1747
1748 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1749 (need_pexb ? 2 : 0);
1750
1751 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1752 sizeof(*pcie->supplies),
1753 GFP_KERNEL);
1754 if (!pcie->supplies)
1755 return -ENOMEM;
1756
1757 pcie->supplies[i++].supply = "avdd-pex-pll";
1758 pcie->supplies[i++].supply = "hvdd-pex";
1759 pcie->supplies[i++].supply = "vddio-pex-ctl";
1760 pcie->supplies[i++].supply = "avdd-plle";
1761
1762 if (need_pexa) {
1763 pcie->supplies[i++].supply = "avdd-pexa";
1764 pcie->supplies[i++].supply = "vdd-pexa";
1765 }
1766
1767 if (need_pexb) {
1768 pcie->supplies[i++].supply = "avdd-pexb";
1769 pcie->supplies[i++].supply = "vdd-pexb";
1770 }
1771 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1772 pcie->num_supplies = 5;
1773
1774 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1775 sizeof(*pcie->supplies),
1776 GFP_KERNEL);
1777 if (!pcie->supplies)
1778 return -ENOMEM;
1779
1780 pcie->supplies[0].supply = "avdd-pex";
1781 pcie->supplies[1].supply = "vdd-pex";
1782 pcie->supplies[2].supply = "avdd-pex-pll";
1783 pcie->supplies[3].supply = "avdd-plle";
1784 pcie->supplies[4].supply = "vddio-pex-clk";
1785 }
1786
1787 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1788 pcie->num_supplies))
1789 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1790 pcie->supplies);
1791
1792 /*
1793 * If not all regulators are available for this new scheme, assume
1794 * that the device tree complies with an older version of the device
1795 * tree binding.
1796 */
1797 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1798
1799 devm_kfree(pcie->dev, pcie->supplies);
1800 pcie->num_supplies = 0;
1801
1802 return tegra_pcie_get_legacy_regulators(pcie);
1803}
1804
Thierry Redingd1523b52013-08-09 16:49:19 +02001805static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1806{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001807 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001808 struct device_node *np = pcie->dev->of_node, *port;
1809 struct of_pci_range_parser parser;
1810 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001811 u32 lanes = 0, mask = 0;
1812 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001813 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001814 int err;
1815
1816 if (of_pci_range_parser_init(&parser, np)) {
1817 dev_err(pcie->dev, "missing \"ranges\" property\n");
1818 return -EINVAL;
1819 }
1820
Thierry Redingd1523b52013-08-09 16:49:19 +02001821 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001822 err = of_pci_range_to_resource(&range, np, &res);
1823 if (err < 0)
1824 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001825
1826 switch (res.flags & IORESOURCE_TYPE_BITS) {
1827 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001828 /* Track the bus -> CPU I/O mapping offset. */
1829 pcie->offset.io = res.start - range.pci_addr;
1830
Thierry Reding51067872014-11-27 09:54:09 +01001831 memcpy(&pcie->pio, &res, sizeof(res));
1832 pcie->pio.name = np->full_name;
1833
1834 /*
1835 * The Tegra PCIe host bridge uses this to program the
1836 * mapping of the I/O space to the physical address,
1837 * so we override the .start and .end fields here that
1838 * of_pci_range_to_resource() converted to I/O space.
1839 * We also set the IORESOURCE_MEM type to clarify that
1840 * the resource is in the physical memory space.
1841 */
1842 pcie->io.start = range.cpu_addr;
1843 pcie->io.end = range.cpu_addr + range.size - 1;
1844 pcie->io.flags = IORESOURCE_MEM;
1845 pcie->io.name = "I/O";
1846
1847 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001848 break;
1849
1850 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001851 /*
1852 * Track the bus -> CPU memory mapping offset. This
1853 * assumes that the prefetchable and non-prefetchable
1854 * regions will be the last of type IORESOURCE_MEM in
1855 * the ranges property.
1856 * */
1857 pcie->offset.mem = res.start - range.pci_addr;
1858
Thierry Redingd1523b52013-08-09 16:49:19 +02001859 if (res.flags & IORESOURCE_PREFETCH) {
1860 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001861 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001862 } else {
1863 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001864 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001865 }
1866 break;
1867 }
1868 }
1869
1870 err = of_pci_parse_bus_range(np, &pcie->busn);
1871 if (err < 0) {
1872 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1873 err);
1874 pcie->busn.name = np->name;
1875 pcie->busn.start = 0;
1876 pcie->busn.end = 0xff;
1877 pcie->busn.flags = IORESOURCE_BUS;
1878 }
1879
1880 /* parse root ports */
1881 for_each_child_of_node(np, port) {
1882 struct tegra_pcie_port *rp;
1883 unsigned int index;
1884 u32 value;
1885
1886 err = of_pci_get_devfn(port);
1887 if (err < 0) {
1888 dev_err(pcie->dev, "failed to parse address: %d\n",
1889 err);
1890 return err;
1891 }
1892
1893 index = PCI_SLOT(err);
1894
Jay Agarwal94716cd2013-08-09 16:49:24 +02001895 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001896 dev_err(pcie->dev, "invalid port number: %d\n", index);
1897 return -EINVAL;
1898 }
1899
1900 index--;
1901
1902 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1903 if (err < 0) {
1904 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1905 err);
1906 return err;
1907 }
1908
1909 if (value > 16) {
1910 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1911 return -EINVAL;
1912 }
1913
1914 lanes |= value << (index << 3);
1915
Thierry Reding077fb152014-05-28 16:49:13 +02001916 if (!of_device_is_available(port)) {
1917 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001918 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001919 }
1920
1921 mask |= ((1 << value) - 1) << lane;
1922 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001923
1924 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1925 if (!rp)
1926 return -ENOMEM;
1927
1928 err = of_address_to_resource(port, 0, &rp->regs);
1929 if (err < 0) {
1930 dev_err(pcie->dev, "failed to parse address: %d\n",
1931 err);
1932 return err;
1933 }
1934
1935 INIT_LIST_HEAD(&rp->list);
1936 rp->index = index;
1937 rp->lanes = value;
1938 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001939 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001940
Julia Lawalldc05ee32013-08-26 11:11:09 +02001941 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1942 if (IS_ERR(rp->base))
1943 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001944
1945 list_add_tail(&rp->list, &pcie->ports);
1946 }
1947
1948 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1949 if (err < 0) {
1950 dev_err(pcie->dev, "invalid lane configuration\n");
1951 return err;
1952 }
1953
Thierry Reding077fb152014-05-28 16:49:13 +02001954 err = tegra_pcie_get_regulators(pcie, mask);
1955 if (err < 0)
1956 return err;
1957
Thierry Redingd1523b52013-08-09 16:49:19 +02001958 return 0;
1959}
1960
1961/*
1962 * FIXME: If there are no PCIe cards attached, then calling this function
1963 * can result in the increase of the bootup time as there are big timeout
1964 * loops.
1965 */
1966#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1967static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1968{
1969 unsigned int retries = 3;
1970 unsigned long value;
1971
Thierry Reding7f1f0542014-08-26 17:11:38 +02001972 /* override presence detection */
1973 value = readl(port->base + RP_PRIV_MISC);
1974 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
1975 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
1976 writel(value, port->base + RP_PRIV_MISC);
1977
Thierry Redingd1523b52013-08-09 16:49:19 +02001978 do {
1979 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1980
1981 do {
1982 value = readl(port->base + RP_VEND_XP);
1983
1984 if (value & RP_VEND_XP_DL_UP)
1985 break;
1986
1987 usleep_range(1000, 2000);
1988 } while (--timeout);
1989
1990 if (!timeout) {
1991 dev_err(port->pcie->dev, "link %u down, retrying\n",
1992 port->index);
1993 goto retry;
1994 }
1995
1996 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1997
1998 do {
1999 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2000
2001 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2002 return true;
2003
2004 usleep_range(1000, 2000);
2005 } while (--timeout);
2006
2007retry:
2008 tegra_pcie_port_reset(port);
2009 } while (--retries);
2010
2011 return false;
2012}
2013
2014static int tegra_pcie_enable(struct tegra_pcie *pcie)
2015{
2016 struct tegra_pcie_port *port, *tmp;
2017 struct hw_pci hw;
2018
2019 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2020 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2021 port->index, port->lanes);
2022
2023 tegra_pcie_port_enable(port);
2024
2025 if (tegra_pcie_port_check_link(port))
2026 continue;
2027
2028 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2029
2030 tegra_pcie_port_disable(port);
2031 tegra_pcie_port_free(port);
2032 }
2033
2034 memset(&hw, 0, sizeof(hw));
2035
Yijing Wang7ec725b2014-11-11 15:35:05 -07002036#ifdef CONFIG_PCI_MSI
2037 hw.msi_ctrl = &pcie->msi.chip;
2038#endif
2039
Thierry Redingd1523b52013-08-09 16:49:19 +02002040 hw.nr_controllers = 1;
2041 hw.private_data = (void **)&pcie;
2042 hw.setup = tegra_pcie_setup;
2043 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02002044 hw.ops = &tegra_pcie_ops;
2045
2046 pci_common_init_dev(pcie->dev, &hw);
2047
2048 return 0;
2049}
2050
Jay Agarwal94716cd2013-08-09 16:49:24 +02002051static const struct tegra_pcie_soc_data tegra20_pcie_data = {
2052 .num_ports = 2,
2053 .msi_base_shift = 0,
2054 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2055 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2056 .has_pex_clkreq_en = false,
2057 .has_pex_bias_ctrl = false,
2058 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002059 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002060 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002061};
2062
2063static const struct tegra_pcie_soc_data tegra30_pcie_data = {
2064 .num_ports = 3,
2065 .msi_base_shift = 8,
2066 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2067 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2068 .has_pex_clkreq_en = true,
2069 .has_pex_bias_ctrl = true,
2070 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002071 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002072 .has_gen2 = false,
2073};
2074
2075static const struct tegra_pcie_soc_data tegra124_pcie_data = {
2076 .num_ports = 2,
2077 .msi_base_shift = 8,
2078 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2079 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2080 .has_pex_clkreq_en = true,
2081 .has_pex_bias_ctrl = true,
2082 .has_intr_prsnt_sense = true,
2083 .has_cml_clk = true,
2084 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002085};
2086
2087static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002088 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002089 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
2090 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
2091 { },
2092};
2093MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
2094
Thierry Reding2cb989f2014-07-22 12:30:46 -06002095static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2096{
2097 struct tegra_pcie *pcie = s->private;
2098
2099 if (list_empty(&pcie->ports))
2100 return NULL;
2101
2102 seq_printf(s, "Index Status\n");
2103
2104 return seq_list_start(&pcie->ports, *pos);
2105}
2106
2107static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2108{
2109 struct tegra_pcie *pcie = s->private;
2110
2111 return seq_list_next(v, &pcie->ports, pos);
2112}
2113
2114static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2115{
2116}
2117
2118static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2119{
2120 bool up = false, active = false;
2121 struct tegra_pcie_port *port;
2122 unsigned int value;
2123
2124 port = list_entry(v, struct tegra_pcie_port, list);
2125
2126 value = readl(port->base + RP_VEND_XP);
2127
2128 if (value & RP_VEND_XP_DL_UP)
2129 up = true;
2130
2131 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2132
2133 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2134 active = true;
2135
2136 seq_printf(s, "%2u ", port->index);
2137
2138 if (up)
2139 seq_printf(s, "up");
2140
2141 if (active) {
2142 if (up)
2143 seq_printf(s, ", ");
2144
2145 seq_printf(s, "active");
2146 }
2147
2148 seq_printf(s, "\n");
2149 return 0;
2150}
2151
2152static const struct seq_operations tegra_pcie_ports_seq_ops = {
2153 .start = tegra_pcie_ports_seq_start,
2154 .next = tegra_pcie_ports_seq_next,
2155 .stop = tegra_pcie_ports_seq_stop,
2156 .show = tegra_pcie_ports_seq_show,
2157};
2158
2159static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2160{
2161 struct tegra_pcie *pcie = inode->i_private;
2162 struct seq_file *s;
2163 int err;
2164
2165 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2166 if (err)
2167 return err;
2168
2169 s = file->private_data;
2170 s->private = pcie;
2171
2172 return 0;
2173}
2174
2175static const struct file_operations tegra_pcie_ports_ops = {
2176 .owner = THIS_MODULE,
2177 .open = tegra_pcie_ports_open,
2178 .read = seq_read,
2179 .llseek = seq_lseek,
2180 .release = seq_release,
2181};
2182
2183static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2184{
2185 struct dentry *file;
2186
2187 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2188 if (!pcie->debugfs)
2189 return -ENOMEM;
2190
2191 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2192 pcie, &tegra_pcie_ports_ops);
2193 if (!file)
2194 goto remove;
2195
2196 return 0;
2197
2198remove:
2199 debugfs_remove_recursive(pcie->debugfs);
2200 pcie->debugfs = NULL;
2201 return -ENOMEM;
2202}
2203
Thierry Redingd1523b52013-08-09 16:49:19 +02002204static int tegra_pcie_probe(struct platform_device *pdev)
2205{
Jay Agarwal94716cd2013-08-09 16:49:24 +02002206 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02002207 struct tegra_pcie *pcie;
2208 int err;
2209
Jay Agarwal94716cd2013-08-09 16:49:24 +02002210 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2211 if (!match)
2212 return -ENODEV;
2213
Thierry Redingd1523b52013-08-09 16:49:19 +02002214 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2215 if (!pcie)
2216 return -ENOMEM;
2217
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002218 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002219 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02002220 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02002221 pcie->dev = &pdev->dev;
2222
2223 err = tegra_pcie_parse_dt(pcie);
2224 if (err < 0)
2225 return err;
2226
2227 pcibios_min_mem = 0;
2228
2229 err = tegra_pcie_get_resources(pcie);
2230 if (err < 0) {
2231 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2232 return err;
2233 }
2234
2235 err = tegra_pcie_enable_controller(pcie);
2236 if (err)
2237 goto put_resources;
2238
2239 /* setup the AFI address translations */
2240 tegra_pcie_setup_translations(pcie);
2241
2242 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2243 err = tegra_pcie_enable_msi(pcie);
2244 if (err < 0) {
2245 dev_err(&pdev->dev,
2246 "failed to enable MSI support: %d\n",
2247 err);
2248 goto put_resources;
2249 }
2250 }
2251
2252 err = tegra_pcie_enable(pcie);
2253 if (err < 0) {
2254 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2255 goto disable_msi;
2256 }
2257
Thierry Reding2cb989f2014-07-22 12:30:46 -06002258 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2259 err = tegra_pcie_debugfs_init(pcie);
2260 if (err < 0)
2261 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2262 err);
2263 }
2264
Thierry Redingd1523b52013-08-09 16:49:19 +02002265 platform_set_drvdata(pdev, pcie);
2266 return 0;
2267
2268disable_msi:
2269 if (IS_ENABLED(CONFIG_PCI_MSI))
2270 tegra_pcie_disable_msi(pcie);
2271put_resources:
2272 tegra_pcie_put_resources(pcie);
2273 return err;
2274}
2275
Thierry Redingd1523b52013-08-09 16:49:19 +02002276static struct platform_driver tegra_pcie_driver = {
2277 .driver = {
2278 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002279 .of_match_table = tegra_pcie_of_match,
2280 .suppress_bind_attrs = true,
2281 },
2282 .probe = tegra_pcie_probe,
2283};
2284module_platform_driver(tegra_pcie_driver);
2285
2286MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
2287MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
Thierry Redingd975cb52014-07-11 08:58:58 +02002288MODULE_LICENSE("GPL v2");