blob: 920a8990c596eed19a33be452c50f9bc22a2dd44 [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
629 err = devm_request_resource(pcie->dev, &ioport_resource, &pcie->pio);
630 if (err < 0)
631 return err;
632
Bjorn Helgaasc4102c922016-06-06 15:55:04 -0500633 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->mem);
Thierry Reding41534e52014-08-01 14:15:11 +0200634 if (err < 0)
635 return err;
636
Bjorn Helgaasc4102c922016-06-06 15:55:04 -0500637 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->prefetch);
Thierry Reding41534e52014-08-01 14:15:11 +0200638 if (err)
639 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200640
Thierry Reding56e75e22016-02-09 15:52:32 +0100641 pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
Thierry Redingd1523b52013-08-09 16:49:19 +0200642 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
643 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
644 sys->mem_offset);
645 pci_add_resource(&sys->resources, &pcie->busn);
646
Thierry Reding51067872014-11-27 09:54:09 +0100647 pci_ioremap_io(pcie->pio.start, pcie->io.start);
Thierry Redingd1523b52013-08-09 16:49:19 +0200648
649 return 1;
650}
651
652static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
653{
654 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600655 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200656
Stephen Warrenb4f17372013-05-06 14:19:19 -0600657 tegra_cpuidle_pcie_irqs_in_use();
658
Lucas Stachf5d33522014-04-16 10:24:32 -0600659 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
660 if (!irq)
661 irq = pcie->irq;
662
663 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200664}
665
Thierry Redingd1523b52013-08-09 16:49:19 +0200666static irqreturn_t tegra_pcie_isr(int irq, void *arg)
667{
668 const char *err_msg[] = {
669 "Unknown",
670 "AXI slave error",
671 "AXI decode error",
672 "Target abort",
673 "Master abort",
674 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200675 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200676 "Response decoding error",
677 "AXI response decoding error",
678 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200679 "Slot present pin change",
680 "Slot clock request change",
681 "TMS clock ramp change",
682 "TMS ready for power down",
683 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200684 };
685 struct tegra_pcie *pcie = arg;
686 u32 code, signature;
687
688 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
689 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
690 afi_writel(pcie, 0, AFI_INTR_CODE);
691
692 if (code == AFI_INTR_LEGACY)
693 return IRQ_NONE;
694
695 if (code >= ARRAY_SIZE(err_msg))
696 code = 0;
697
698 /*
699 * do not pollute kernel log with master abort reports since they
700 * happen a lot during enumeration
701 */
702 if (code == AFI_INTR_MASTER_ABORT)
703 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
704 signature);
705 else
706 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
707 signature);
708
709 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
710 code == AFI_INTR_FPCI_DECODE_ERROR) {
711 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
712 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
713
714 if (code == AFI_INTR_MASTER_ABORT)
715 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
716 else
717 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
718 }
719
720 return IRQ_HANDLED;
721}
722
723/*
724 * FPCI map is as follows:
725 * - 0xfdfc000000: I/O space
726 * - 0xfdfe000000: type 0 configuration space
727 * - 0xfdff000000: type 1 configuration space
728 * - 0xfe00000000: type 0 extended configuration space
729 * - 0xfe10000000: type 1 extended configuration space
730 */
731static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
732{
733 u32 fpci_bar, size, axi_address;
734
735 /* Bar 0: type 1 extended configuration space */
736 fpci_bar = 0xfe100000;
737 size = resource_size(pcie->cs);
738 axi_address = pcie->cs->start;
739 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
740 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
741 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
742
743 /* Bar 1: downstream IO bar */
744 fpci_bar = 0xfdfc0000;
745 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100746 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200747 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
748 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
749 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
750
751 /* Bar 2: prefetchable memory BAR */
752 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
753 size = resource_size(&pcie->prefetch);
754 axi_address = pcie->prefetch.start;
755 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
756 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
757 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
758
759 /* Bar 3: non prefetchable memory BAR */
760 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
761 size = resource_size(&pcie->mem);
762 axi_address = pcie->mem.start;
763 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
764 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
765 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
766
767 /* NULL out the remaining BARs as they are not used */
768 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
769 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
770 afi_writel(pcie, 0, AFI_FPCI_BAR4);
771
772 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
773 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
774 afi_writel(pcie, 0, AFI_FPCI_BAR5);
775
776 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100777 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200778 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
779 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
780 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
781
782 /* MSI translations are setup only when needed */
783 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
784 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
785 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
786 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
787}
788
Thierry Reding7f1f0542014-08-26 17:11:38 +0200789static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200790{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200791 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200792 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200793
Thierry Reding7f1f0542014-08-26 17:11:38 +0200794 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200795
Thierry Reding7f1f0542014-08-26 17:11:38 +0200796 while (time_before(jiffies, timeout)) {
797 value = pads_readl(pcie, soc->pads_pll_ctl);
798 if (value & PADS_PLL_CTL_LOCKDET)
799 return 0;
800 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200801
Thierry Reding7f1f0542014-08-26 17:11:38 +0200802 return -ETIMEDOUT;
803}
Thierry Redingd1523b52013-08-09 16:49:19 +0200804
Thierry Reding7f1f0542014-08-26 17:11:38 +0200805static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
806{
807 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
808 u32 value;
809 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200810
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700811 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200812 pads_writel(pcie, 0x0, PADS_CTL_SEL);
813
814 /* override IDDQ to 1 on all 4 lanes */
815 value = pads_readl(pcie, PADS_CTL);
816 value |= PADS_CTL_IDDQ_1L;
817 pads_writel(pcie, value, PADS_CTL);
818
819 /*
820 * Set up PHY PLL inputs select PLLE output as refclock,
821 * set TX ref sel to div10 (not div5).
822 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200823 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200824 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200825 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
826 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200827
Eric Yuenec732762014-08-26 17:11:37 +0200828 /* reset PLL */
829 value = pads_readl(pcie, soc->pads_pll_ctl);
830 value &= ~PADS_PLL_CTL_RST_B4SM;
831 pads_writel(pcie, value, soc->pads_pll_ctl);
832
833 usleep_range(20, 100);
834
Thierry Redingd1523b52013-08-09 16:49:19 +0200835 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200836 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200837 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200838 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200839
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200840 /* Configure the reference clock driver */
841 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
842 pads_writel(pcie, value, PADS_REFCLK_CFG0);
843 if (soc->num_ports > 2)
844 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200845
846 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200847 err = tegra_pcie_pll_wait(pcie, 500);
848 if (err < 0) {
849 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
850 return err;
851 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200852
853 /* turn off IDDQ override */
854 value = pads_readl(pcie, PADS_CTL);
855 value &= ~PADS_CTL_IDDQ_1L;
856 pads_writel(pcie, value, PADS_CTL);
857
858 /* enable TX/RX data */
859 value = pads_readl(pcie, PADS_CTL);
860 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
861 pads_writel(pcie, value, PADS_CTL);
862
Thierry Reding7f1f0542014-08-26 17:11:38 +0200863 return 0;
864}
865
Thierry Reding6fe7c182015-11-11 18:25:59 +0100866static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
867{
868 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
869 u32 value;
870
871 /* disable TX/RX data */
872 value = pads_readl(pcie, PADS_CTL);
873 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
874 pads_writel(pcie, value, PADS_CTL);
875
876 /* override IDDQ */
877 value = pads_readl(pcie, PADS_CTL);
878 value |= PADS_CTL_IDDQ_1L;
879 pads_writel(pcie, PADS_CTL, value);
880
881 /* reset PLL */
882 value = pads_readl(pcie, soc->pads_pll_ctl);
883 value &= ~PADS_PLL_CTL_RST_B4SM;
884 pads_writel(pcie, value, soc->pads_pll_ctl);
885
886 usleep_range(20, 100);
887
888 return 0;
889}
890
891static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
892{
893 struct device *dev = port->pcie->dev;
894 unsigned int i;
895 int err;
896
897 for (i = 0; i < port->lanes; i++) {
898 err = phy_power_on(port->phys[i]);
899 if (err < 0) {
900 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
901 err);
902 return err;
903 }
904 }
905
906 return 0;
907}
908
909static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
910{
911 struct device *dev = port->pcie->dev;
912 unsigned int i;
913 int err;
914
915 for (i = 0; i < port->lanes; i++) {
916 err = phy_power_off(port->phys[i]);
917 if (err < 0) {
918 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
919 err);
920 return err;
921 }
922 }
923
924 return 0;
925}
926
927static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
928{
929 struct tegra_pcie_port *port;
930 int err;
931
932 if (pcie->legacy_phy) {
933 if (pcie->phy)
934 err = phy_power_on(pcie->phy);
935 else
936 err = tegra_pcie_phy_enable(pcie);
937
938 if (err < 0)
939 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
940
941 return err;
942 }
943
944 list_for_each_entry(port, &pcie->ports, list) {
945 err = tegra_pcie_port_phy_power_on(port);
946 if (err < 0) {
947 dev_err(pcie->dev,
948 "failed to power on PCIe port %u PHY: %d\n",
949 port->index, err);
950 return err;
951 }
952 }
953
954 return 0;
955}
956
957static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
958{
959 struct tegra_pcie_port *port;
960 int err;
961
962 if (pcie->legacy_phy) {
963 if (pcie->phy)
964 err = phy_power_off(pcie->phy);
965 else
966 err = tegra_pcie_phy_disable(pcie);
967
968 if (err < 0)
969 dev_err(pcie->dev, "failed to power off PHY: %d\n",
970 err);
971
972 return err;
973 }
974
975 list_for_each_entry(port, &pcie->ports, list) {
976 err = tegra_pcie_port_phy_power_off(port);
977 if (err < 0) {
978 dev_err(pcie->dev,
979 "failed to power off PCIe port %u PHY: %d\n",
980 port->index, err);
981 return err;
982 }
983 }
984
985 return 0;
986}
987
Thierry Reding7f1f0542014-08-26 17:11:38 +0200988static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
989{
990 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
991 struct tegra_pcie_port *port;
992 unsigned long value;
993 int err;
994
995 /* enable PLL power down */
996 if (pcie->phy) {
997 value = afi_readl(pcie, AFI_PLLE_CONTROL);
998 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
999 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1000 afi_writel(pcie, value, AFI_PLLE_CONTROL);
1001 }
1002
1003 /* power down PCIe slot clock bias pad */
1004 if (soc->has_pex_bias_ctrl)
1005 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1006
1007 /* configure mode and disable all ports */
1008 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1009 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1010 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1011
1012 list_for_each_entry(port, &pcie->ports, list)
1013 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1014
1015 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1016
1017 if (soc->has_gen2) {
1018 value = afi_readl(pcie, AFI_FUSE);
1019 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1020 afi_writel(pcie, value, AFI_FUSE);
1021 } else {
1022 value = afi_readl(pcie, AFI_FUSE);
1023 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1024 afi_writel(pcie, value, AFI_FUSE);
1025 }
1026
Thierry Reding6fe7c182015-11-11 18:25:59 +01001027 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001028 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001029 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001030 return err;
1031 }
1032
Thierry Redingd1523b52013-08-09 16:49:19 +02001033 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001034 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001035
1036 /* finally enable PCIe */
1037 value = afi_readl(pcie, AFI_CONFIGURATION);
1038 value |= AFI_CONFIGURATION_EN_FPCI;
1039 afi_writel(pcie, value, AFI_CONFIGURATION);
1040
1041 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1042 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1043 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001044
1045 if (soc->has_intr_prsnt_sense)
1046 value |= AFI_INTR_EN_PRSNT_SENSE;
1047
Thierry Redingd1523b52013-08-09 16:49:19 +02001048 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1049 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1050
1051 /* don't enable MSI for now, only when needed */
1052 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1053
1054 /* disable all exceptions */
1055 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1056
1057 return 0;
1058}
1059
1060static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1061{
1062 int err;
1063
1064 /* TODO: disable and unprepare clocks? */
1065
Thierry Reding6fe7c182015-11-11 18:25:59 +01001066 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001067 if (err < 0)
Thierry Reding6fe7c182015-11-11 18:25:59 +01001068 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001069
Stephen Warren3127a6b2013-11-06 15:56:58 -07001070 reset_control_assert(pcie->pcie_xrst);
1071 reset_control_assert(pcie->afi_rst);
1072 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001073
1074 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1075
Thierry Reding077fb152014-05-28 16:49:13 +02001076 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001077 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +02001078 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001079}
1080
1081static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1082{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001083 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001084 int err;
1085
Stephen Warren3127a6b2013-11-06 15:56:58 -07001086 reset_control_assert(pcie->pcie_xrst);
1087 reset_control_assert(pcie->afi_rst);
1088 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001089
1090 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1091
1092 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001093 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1094 if (err < 0)
1095 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001096
Thierry Redingd1523b52013-08-09 16:49:19 +02001097 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001098 pcie->pex_clk,
1099 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001100 if (err) {
1101 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1102 return err;
1103 }
1104
Stephen Warren3127a6b2013-11-06 15:56:58 -07001105 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001106
1107 err = clk_prepare_enable(pcie->afi_clk);
1108 if (err < 0) {
1109 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1110 return err;
1111 }
1112
Jay Agarwal94716cd2013-08-09 16:49:24 +02001113 if (soc->has_cml_clk) {
1114 err = clk_prepare_enable(pcie->cml_clk);
1115 if (err < 0) {
1116 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1117 err);
1118 return err;
1119 }
1120 }
1121
Thierry Redingd1523b52013-08-09 16:49:19 +02001122 err = clk_prepare_enable(pcie->pll_e);
1123 if (err < 0) {
1124 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1125 return err;
1126 }
1127
1128 return 0;
1129}
1130
1131static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1132{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001133 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1134
Thierry Redingd1523b52013-08-09 16:49:19 +02001135 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1136 if (IS_ERR(pcie->pex_clk))
1137 return PTR_ERR(pcie->pex_clk);
1138
1139 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1140 if (IS_ERR(pcie->afi_clk))
1141 return PTR_ERR(pcie->afi_clk);
1142
Thierry Redingd1523b52013-08-09 16:49:19 +02001143 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1144 if (IS_ERR(pcie->pll_e))
1145 return PTR_ERR(pcie->pll_e);
1146
Jay Agarwal94716cd2013-08-09 16:49:24 +02001147 if (soc->has_cml_clk) {
1148 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1149 if (IS_ERR(pcie->cml_clk))
1150 return PTR_ERR(pcie->cml_clk);
1151 }
1152
Thierry Redingd1523b52013-08-09 16:49:19 +02001153 return 0;
1154}
1155
Stephen Warren3127a6b2013-11-06 15:56:58 -07001156static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1157{
1158 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1159 if (IS_ERR(pcie->pex_rst))
1160 return PTR_ERR(pcie->pex_rst);
1161
1162 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1163 if (IS_ERR(pcie->afi_rst))
1164 return PTR_ERR(pcie->afi_rst);
1165
1166 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1167 if (IS_ERR(pcie->pcie_xrst))
1168 return PTR_ERR(pcie->pcie_xrst);
1169
1170 return 0;
1171}
1172
Thierry Reding6fe7c182015-11-11 18:25:59 +01001173static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1174{
1175 int err;
1176
1177 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1178 if (IS_ERR(pcie->phy)) {
1179 err = PTR_ERR(pcie->phy);
1180 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1181 return err;
1182 }
1183
1184 err = phy_init(pcie->phy);
1185 if (err < 0) {
1186 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1187 return err;
1188 }
1189
1190 pcie->legacy_phy = true;
1191
1192 return 0;
1193}
1194
1195static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1196 struct device_node *np,
1197 const char *consumer,
1198 unsigned int index)
1199{
1200 struct phy *phy;
1201 char *name;
1202
1203 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1204 if (!name)
1205 return ERR_PTR(-ENOMEM);
1206
1207 phy = devm_of_phy_get(dev, np, name);
1208 kfree(name);
1209
1210 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1211 phy = NULL;
1212
1213 return phy;
1214}
1215
1216static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1217{
1218 struct device *dev = port->pcie->dev;
1219 struct phy *phy;
1220 unsigned int i;
1221 int err;
1222
1223 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1224 if (!port->phys)
1225 return -ENOMEM;
1226
1227 for (i = 0; i < port->lanes; i++) {
1228 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1229 if (IS_ERR(phy)) {
1230 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1231 PTR_ERR(phy));
1232 return PTR_ERR(phy);
1233 }
1234
1235 err = phy_init(phy);
1236 if (err < 0) {
1237 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1238 err);
1239 return err;
1240 }
1241
1242 port->phys[i] = phy;
1243 }
1244
1245 return 0;
1246}
1247
1248static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1249{
1250 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1251 struct device_node *np = pcie->dev->of_node;
1252 struct tegra_pcie_port *port;
1253 int err;
1254
1255 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1256 return tegra_pcie_phys_get_legacy(pcie);
1257
1258 list_for_each_entry(port, &pcie->ports, list) {
1259 err = tegra_pcie_port_get_phys(port);
1260 if (err < 0)
1261 return err;
1262 }
1263
1264 return 0;
1265}
1266
Thierry Redingd1523b52013-08-09 16:49:19 +02001267static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1268{
1269 struct platform_device *pdev = to_platform_device(pcie->dev);
1270 struct resource *pads, *afi, *res;
1271 int err;
1272
1273 err = tegra_pcie_clocks_get(pcie);
1274 if (err) {
1275 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1276 return err;
1277 }
1278
Stephen Warren3127a6b2013-11-06 15:56:58 -07001279 err = tegra_pcie_resets_get(pcie);
1280 if (err) {
1281 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1282 return err;
1283 }
1284
Thierry Reding6fe7c182015-11-11 18:25:59 +01001285 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001286 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001287 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001288 return err;
1289 }
1290
Thierry Redingd1523b52013-08-09 16:49:19 +02001291 err = tegra_pcie_power_on(pcie);
1292 if (err) {
1293 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1294 return err;
1295 }
1296
Thierry Redingd1523b52013-08-09 16:49:19 +02001297 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001298 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1299 if (IS_ERR(pcie->pads)) {
1300 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001301 goto poweroff;
1302 }
1303
1304 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001305 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1306 if (IS_ERR(pcie->afi)) {
1307 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001308 goto poweroff;
1309 }
1310
Julia Lawalldc05ee32013-08-26 11:11:09 +02001311 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001312 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1313 if (!res) {
1314 err = -EADDRNOTAVAIL;
1315 goto poweroff;
1316 }
1317
1318 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1319 resource_size(res), res->name);
1320 if (!pcie->cs) {
1321 err = -EADDRNOTAVAIL;
1322 goto poweroff;
1323 }
1324
1325 /* request interrupt */
1326 err = platform_get_irq_byname(pdev, "intr");
1327 if (err < 0) {
1328 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1329 goto poweroff;
1330 }
1331
1332 pcie->irq = err;
1333
1334 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1335 if (err) {
1336 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1337 goto poweroff;
1338 }
1339
1340 return 0;
1341
1342poweroff:
1343 tegra_pcie_power_off(pcie);
1344 return err;
1345}
1346
1347static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1348{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001349 int err;
1350
Thierry Redingd1523b52013-08-09 16:49:19 +02001351 if (pcie->irq > 0)
1352 free_irq(pcie->irq, pcie);
1353
1354 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001355
1356 err = phy_exit(pcie->phy);
1357 if (err < 0)
1358 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1359
Thierry Redingd1523b52013-08-09 16:49:19 +02001360 return 0;
1361}
1362
1363static int tegra_msi_alloc(struct tegra_msi *chip)
1364{
1365 int msi;
1366
1367 mutex_lock(&chip->lock);
1368
1369 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1370 if (msi < INT_PCI_MSI_NR)
1371 set_bit(msi, chip->used);
1372 else
1373 msi = -ENOSPC;
1374
1375 mutex_unlock(&chip->lock);
1376
1377 return msi;
1378}
1379
1380static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1381{
1382 struct device *dev = chip->chip.dev;
1383
1384 mutex_lock(&chip->lock);
1385
1386 if (!test_bit(irq, chip->used))
1387 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1388 else
1389 clear_bit(irq, chip->used);
1390
1391 mutex_unlock(&chip->lock);
1392}
1393
1394static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1395{
1396 struct tegra_pcie *pcie = data;
1397 struct tegra_msi *msi = &pcie->msi;
1398 unsigned int i, processed = 0;
1399
1400 for (i = 0; i < 8; i++) {
1401 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1402
1403 while (reg) {
1404 unsigned int offset = find_first_bit(&reg, 32);
1405 unsigned int index = i * 32 + offset;
1406 unsigned int irq;
1407
1408 /* clear the interrupt */
1409 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1410
1411 irq = irq_find_mapping(msi->domain, index);
1412 if (irq) {
1413 if (test_bit(index, msi->used))
1414 generic_handle_irq(irq);
1415 else
1416 dev_info(pcie->dev, "unhandled MSI\n");
1417 } else {
1418 /*
1419 * that's weird who triggered this?
1420 * just clear it
1421 */
1422 dev_info(pcie->dev, "unexpected MSI\n");
1423 }
1424
1425 /* see if there's any more pending in this vector */
1426 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1427
1428 processed++;
1429 }
1430 }
1431
1432 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1433}
1434
Yijing Wangc2791b82014-11-11 17:45:45 -07001435static int tegra_msi_setup_irq(struct msi_controller *chip,
1436 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001437{
1438 struct tegra_msi *msi = to_tegra_msi(chip);
1439 struct msi_msg msg;
1440 unsigned int irq;
1441 int hwirq;
1442
1443 hwirq = tegra_msi_alloc(msi);
1444 if (hwirq < 0)
1445 return hwirq;
1446
1447 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001448 if (!irq) {
1449 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001450 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001451 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001452
1453 irq_set_msi_desc(irq, desc);
1454
1455 msg.address_lo = virt_to_phys((void *)msi->pages);
1456 /* 32 bit address only */
1457 msg.address_hi = 0;
1458 msg.data = hwirq;
1459
Jiang Liu83a18912014-11-09 23:10:34 +08001460 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001461
1462 return 0;
1463}
1464
Yijing Wangc2791b82014-11-11 17:45:45 -07001465static void tegra_msi_teardown_irq(struct msi_controller *chip,
1466 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001467{
1468 struct tegra_msi *msi = to_tegra_msi(chip);
1469 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001470 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001471
Jisheng Zhang019fa462014-07-29 09:33:30 +08001472 irq_dispose_mapping(irq);
1473 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001474}
1475
1476static struct irq_chip tegra_msi_irq_chip = {
1477 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001478 .irq_enable = pci_msi_unmask_irq,
1479 .irq_disable = pci_msi_mask_irq,
1480 .irq_mask = pci_msi_mask_irq,
1481 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001482};
1483
1484static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1485 irq_hw_number_t hwirq)
1486{
1487 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1488 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001489
Stephen Warrenb4f17372013-05-06 14:19:19 -06001490 tegra_cpuidle_pcie_irqs_in_use();
1491
Thierry Redingd1523b52013-08-09 16:49:19 +02001492 return 0;
1493}
1494
1495static const struct irq_domain_ops msi_domain_ops = {
1496 .map = tegra_msi_map,
1497};
1498
1499static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1500{
1501 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001502 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001503 struct tegra_msi *msi = &pcie->msi;
1504 unsigned long base;
1505 int err;
1506 u32 reg;
1507
1508 mutex_init(&msi->lock);
1509
1510 msi->chip.dev = pcie->dev;
1511 msi->chip.setup_irq = tegra_msi_setup_irq;
1512 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1513
1514 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1515 &msi_domain_ops, &msi->chip);
1516 if (!msi->domain) {
1517 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1518 return -ENOMEM;
1519 }
1520
1521 err = platform_get_irq_byname(pdev, "msi");
1522 if (err < 0) {
1523 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1524 goto err;
1525 }
1526
1527 msi->irq = err;
1528
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001529 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001530 tegra_msi_irq_chip.name, pcie);
1531 if (err < 0) {
1532 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1533 goto err;
1534 }
1535
1536 /* setup AFI/FPCI range */
1537 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1538 base = virt_to_phys((void *)msi->pages);
1539
Jay Agarwal94716cd2013-08-09 16:49:24 +02001540 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001541 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1542 /* this register is in 4K increments */
1543 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1544
1545 /* enable all MSI vectors */
1546 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1547 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1548 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1549 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1550 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1551 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1552 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1553 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1554
1555 /* and unmask the MSI interrupt */
1556 reg = afi_readl(pcie, AFI_INTR_MASK);
1557 reg |= AFI_INTR_MASK_MSI_MASK;
1558 afi_writel(pcie, reg, AFI_INTR_MASK);
1559
1560 return 0;
1561
1562err:
1563 irq_domain_remove(msi->domain);
1564 return err;
1565}
1566
1567static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1568{
1569 struct tegra_msi *msi = &pcie->msi;
1570 unsigned int i, irq;
1571 u32 value;
1572
1573 /* mask the MSI interrupt */
1574 value = afi_readl(pcie, AFI_INTR_MASK);
1575 value &= ~AFI_INTR_MASK_MSI_MASK;
1576 afi_writel(pcie, value, AFI_INTR_MASK);
1577
1578 /* disable all MSI vectors */
1579 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1580 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1581 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1582 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1583 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1584 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1585 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1586 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1587
1588 free_pages(msi->pages, 0);
1589
1590 if (msi->irq > 0)
1591 free_irq(msi->irq, pcie);
1592
1593 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1594 irq = irq_find_mapping(msi->domain, i);
1595 if (irq > 0)
1596 irq_dispose_mapping(irq);
1597 }
1598
1599 irq_domain_remove(msi->domain);
1600
1601 return 0;
1602}
1603
1604static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1605 u32 *xbar)
1606{
1607 struct device_node *np = pcie->dev->of_node;
1608
Thierry Reding7f1f0542014-08-26 17:11:38 +02001609 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1610 switch (lanes) {
1611 case 0x0000104:
1612 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1613 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1614 return 0;
1615
1616 case 0x0000102:
1617 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1618 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1619 return 0;
1620 }
1621 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001622 switch (lanes) {
1623 case 0x00000204:
1624 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1625 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1626 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001627
Jay Agarwal94716cd2013-08-09 16:49:24 +02001628 case 0x00020202:
1629 dev_info(pcie->dev, "2x3 configuration\n");
1630 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1631 return 0;
1632
1633 case 0x00010104:
1634 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1635 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1636 return 0;
1637 }
1638 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1639 switch (lanes) {
1640 case 0x00000004:
1641 dev_info(pcie->dev, "single-mode configuration\n");
1642 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1643 return 0;
1644
1645 case 0x00000202:
1646 dev_info(pcie->dev, "dual-mode configuration\n");
1647 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1648 return 0;
1649 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001650 }
1651
1652 return -EINVAL;
1653}
1654
Thierry Reding077fb152014-05-28 16:49:13 +02001655/*
1656 * Check whether a given set of supplies is available in a device tree node.
1657 * This is used to check whether the new or the legacy device tree bindings
1658 * should be used.
1659 */
1660static bool of_regulator_bulk_available(struct device_node *np,
1661 struct regulator_bulk_data *supplies,
1662 unsigned int num_supplies)
1663{
1664 char property[32];
1665 unsigned int i;
1666
1667 for (i = 0; i < num_supplies; i++) {
1668 snprintf(property, 32, "%s-supply", supplies[i].supply);
1669
1670 if (of_find_property(np, property, NULL) == NULL)
1671 return false;
1672 }
1673
1674 return true;
1675}
1676
1677/*
1678 * Old versions of the device tree binding for this device used a set of power
1679 * supplies that didn't match the hardware inputs. This happened to work for a
1680 * number of cases but is not future proof. However to preserve backwards-
1681 * compatibility with old device trees, this function will try to use the old
1682 * set of supplies.
1683 */
1684static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1685{
1686 struct device_node *np = pcie->dev->of_node;
1687
1688 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1689 pcie->num_supplies = 3;
1690 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1691 pcie->num_supplies = 2;
1692
1693 if (pcie->num_supplies == 0) {
1694 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1695 np->full_name);
1696 return -ENODEV;
1697 }
1698
1699 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1700 sizeof(*pcie->supplies),
1701 GFP_KERNEL);
1702 if (!pcie->supplies)
1703 return -ENOMEM;
1704
1705 pcie->supplies[0].supply = "pex-clk";
1706 pcie->supplies[1].supply = "vdd";
1707
1708 if (pcie->num_supplies > 2)
1709 pcie->supplies[2].supply = "avdd";
1710
1711 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1712 pcie->supplies);
1713}
1714
1715/*
1716 * Obtains the list of regulators required for a particular generation of the
1717 * IP block.
1718 *
1719 * This would've been nice to do simply by providing static tables for use
1720 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1721 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1722 * and either seems to be optional depending on which ports are being used.
1723 */
1724static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1725{
1726 struct device_node *np = pcie->dev->of_node;
1727 unsigned int i = 0;
1728
Thierry Reding7f1f0542014-08-26 17:11:38 +02001729 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1730 pcie->num_supplies = 7;
1731
1732 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1733 sizeof(*pcie->supplies),
1734 GFP_KERNEL);
1735 if (!pcie->supplies)
1736 return -ENOMEM;
1737
1738 pcie->supplies[i++].supply = "avddio-pex";
1739 pcie->supplies[i++].supply = "dvddio-pex";
1740 pcie->supplies[i++].supply = "avdd-pex-pll";
1741 pcie->supplies[i++].supply = "hvdd-pex";
1742 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1743 pcie->supplies[i++].supply = "vddio-pex-ctl";
1744 pcie->supplies[i++].supply = "avdd-pll-erefe";
1745 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001746 bool need_pexa = false, need_pexb = false;
1747
1748 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1749 if (lane_mask & 0x0f)
1750 need_pexa = true;
1751
1752 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1753 if (lane_mask & 0x30)
1754 need_pexb = true;
1755
1756 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1757 (need_pexb ? 2 : 0);
1758
1759 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1760 sizeof(*pcie->supplies),
1761 GFP_KERNEL);
1762 if (!pcie->supplies)
1763 return -ENOMEM;
1764
1765 pcie->supplies[i++].supply = "avdd-pex-pll";
1766 pcie->supplies[i++].supply = "hvdd-pex";
1767 pcie->supplies[i++].supply = "vddio-pex-ctl";
1768 pcie->supplies[i++].supply = "avdd-plle";
1769
1770 if (need_pexa) {
1771 pcie->supplies[i++].supply = "avdd-pexa";
1772 pcie->supplies[i++].supply = "vdd-pexa";
1773 }
1774
1775 if (need_pexb) {
1776 pcie->supplies[i++].supply = "avdd-pexb";
1777 pcie->supplies[i++].supply = "vdd-pexb";
1778 }
1779 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1780 pcie->num_supplies = 5;
1781
1782 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1783 sizeof(*pcie->supplies),
1784 GFP_KERNEL);
1785 if (!pcie->supplies)
1786 return -ENOMEM;
1787
1788 pcie->supplies[0].supply = "avdd-pex";
1789 pcie->supplies[1].supply = "vdd-pex";
1790 pcie->supplies[2].supply = "avdd-pex-pll";
1791 pcie->supplies[3].supply = "avdd-plle";
1792 pcie->supplies[4].supply = "vddio-pex-clk";
1793 }
1794
1795 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1796 pcie->num_supplies))
1797 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1798 pcie->supplies);
1799
1800 /*
1801 * If not all regulators are available for this new scheme, assume
1802 * that the device tree complies with an older version of the device
1803 * tree binding.
1804 */
1805 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1806
1807 devm_kfree(pcie->dev, pcie->supplies);
1808 pcie->num_supplies = 0;
1809
1810 return tegra_pcie_get_legacy_regulators(pcie);
1811}
1812
Thierry Redingd1523b52013-08-09 16:49:19 +02001813static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1814{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001815 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001816 struct device_node *np = pcie->dev->of_node, *port;
1817 struct of_pci_range_parser parser;
1818 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001819 u32 lanes = 0, mask = 0;
1820 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001821 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001822 int err;
1823
1824 if (of_pci_range_parser_init(&parser, np)) {
1825 dev_err(pcie->dev, "missing \"ranges\" property\n");
1826 return -EINVAL;
1827 }
1828
Thierry Redingd1523b52013-08-09 16:49:19 +02001829 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001830 err = of_pci_range_to_resource(&range, np, &res);
1831 if (err < 0)
1832 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001833
1834 switch (res.flags & IORESOURCE_TYPE_BITS) {
1835 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001836 /* Track the bus -> CPU I/O mapping offset. */
1837 pcie->offset.io = res.start - range.pci_addr;
1838
Thierry Reding51067872014-11-27 09:54:09 +01001839 memcpy(&pcie->pio, &res, sizeof(res));
1840 pcie->pio.name = np->full_name;
1841
1842 /*
1843 * The Tegra PCIe host bridge uses this to program the
1844 * mapping of the I/O space to the physical address,
1845 * so we override the .start and .end fields here that
1846 * of_pci_range_to_resource() converted to I/O space.
1847 * We also set the IORESOURCE_MEM type to clarify that
1848 * the resource is in the physical memory space.
1849 */
1850 pcie->io.start = range.cpu_addr;
1851 pcie->io.end = range.cpu_addr + range.size - 1;
1852 pcie->io.flags = IORESOURCE_MEM;
1853 pcie->io.name = "I/O";
1854
1855 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001856 break;
1857
1858 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001859 /*
1860 * Track the bus -> CPU memory mapping offset. This
1861 * assumes that the prefetchable and non-prefetchable
1862 * regions will be the last of type IORESOURCE_MEM in
1863 * the ranges property.
1864 * */
1865 pcie->offset.mem = res.start - range.pci_addr;
1866
Thierry Redingd1523b52013-08-09 16:49:19 +02001867 if (res.flags & IORESOURCE_PREFETCH) {
1868 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001869 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001870 } else {
1871 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001872 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001873 }
1874 break;
1875 }
1876 }
1877
1878 err = of_pci_parse_bus_range(np, &pcie->busn);
1879 if (err < 0) {
1880 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1881 err);
1882 pcie->busn.name = np->name;
1883 pcie->busn.start = 0;
1884 pcie->busn.end = 0xff;
1885 pcie->busn.flags = IORESOURCE_BUS;
1886 }
1887
1888 /* parse root ports */
1889 for_each_child_of_node(np, port) {
1890 struct tegra_pcie_port *rp;
1891 unsigned int index;
1892 u32 value;
1893
1894 err = of_pci_get_devfn(port);
1895 if (err < 0) {
1896 dev_err(pcie->dev, "failed to parse address: %d\n",
1897 err);
1898 return err;
1899 }
1900
1901 index = PCI_SLOT(err);
1902
Jay Agarwal94716cd2013-08-09 16:49:24 +02001903 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001904 dev_err(pcie->dev, "invalid port number: %d\n", index);
1905 return -EINVAL;
1906 }
1907
1908 index--;
1909
1910 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1911 if (err < 0) {
1912 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1913 err);
1914 return err;
1915 }
1916
1917 if (value > 16) {
1918 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1919 return -EINVAL;
1920 }
1921
1922 lanes |= value << (index << 3);
1923
Thierry Reding077fb152014-05-28 16:49:13 +02001924 if (!of_device_is_available(port)) {
1925 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001926 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001927 }
1928
1929 mask |= ((1 << value) - 1) << lane;
1930 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001931
1932 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1933 if (!rp)
1934 return -ENOMEM;
1935
1936 err = of_address_to_resource(port, 0, &rp->regs);
1937 if (err < 0) {
1938 dev_err(pcie->dev, "failed to parse address: %d\n",
1939 err);
1940 return err;
1941 }
1942
1943 INIT_LIST_HEAD(&rp->list);
1944 rp->index = index;
1945 rp->lanes = value;
1946 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001947 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001948
Julia Lawalldc05ee32013-08-26 11:11:09 +02001949 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1950 if (IS_ERR(rp->base))
1951 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001952
1953 list_add_tail(&rp->list, &pcie->ports);
1954 }
1955
1956 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1957 if (err < 0) {
1958 dev_err(pcie->dev, "invalid lane configuration\n");
1959 return err;
1960 }
1961
Thierry Reding077fb152014-05-28 16:49:13 +02001962 err = tegra_pcie_get_regulators(pcie, mask);
1963 if (err < 0)
1964 return err;
1965
Thierry Redingd1523b52013-08-09 16:49:19 +02001966 return 0;
1967}
1968
1969/*
1970 * FIXME: If there are no PCIe cards attached, then calling this function
1971 * can result in the increase of the bootup time as there are big timeout
1972 * loops.
1973 */
1974#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1975static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1976{
1977 unsigned int retries = 3;
1978 unsigned long value;
1979
Thierry Reding7f1f0542014-08-26 17:11:38 +02001980 /* override presence detection */
1981 value = readl(port->base + RP_PRIV_MISC);
1982 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
1983 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
1984 writel(value, port->base + RP_PRIV_MISC);
1985
Thierry Redingd1523b52013-08-09 16:49:19 +02001986 do {
1987 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1988
1989 do {
1990 value = readl(port->base + RP_VEND_XP);
1991
1992 if (value & RP_VEND_XP_DL_UP)
1993 break;
1994
1995 usleep_range(1000, 2000);
1996 } while (--timeout);
1997
1998 if (!timeout) {
1999 dev_err(port->pcie->dev, "link %u down, retrying\n",
2000 port->index);
2001 goto retry;
2002 }
2003
2004 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2005
2006 do {
2007 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2008
2009 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2010 return true;
2011
2012 usleep_range(1000, 2000);
2013 } while (--timeout);
2014
2015retry:
2016 tegra_pcie_port_reset(port);
2017 } while (--retries);
2018
2019 return false;
2020}
2021
2022static int tegra_pcie_enable(struct tegra_pcie *pcie)
2023{
2024 struct tegra_pcie_port *port, *tmp;
2025 struct hw_pci hw;
2026
2027 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2028 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2029 port->index, port->lanes);
2030
2031 tegra_pcie_port_enable(port);
2032
2033 if (tegra_pcie_port_check_link(port))
2034 continue;
2035
2036 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2037
2038 tegra_pcie_port_disable(port);
2039 tegra_pcie_port_free(port);
2040 }
2041
2042 memset(&hw, 0, sizeof(hw));
2043
Yijing Wang7ec725b2014-11-11 15:35:05 -07002044#ifdef CONFIG_PCI_MSI
2045 hw.msi_ctrl = &pcie->msi.chip;
2046#endif
2047
Thierry Redingd1523b52013-08-09 16:49:19 +02002048 hw.nr_controllers = 1;
2049 hw.private_data = (void **)&pcie;
2050 hw.setup = tegra_pcie_setup;
2051 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02002052 hw.ops = &tegra_pcie_ops;
2053
2054 pci_common_init_dev(pcie->dev, &hw);
2055
2056 return 0;
2057}
2058
Jay Agarwal94716cd2013-08-09 16:49:24 +02002059static const struct tegra_pcie_soc_data tegra20_pcie_data = {
2060 .num_ports = 2,
2061 .msi_base_shift = 0,
2062 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2063 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2064 .has_pex_clkreq_en = false,
2065 .has_pex_bias_ctrl = false,
2066 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002067 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002068 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002069};
2070
2071static const struct tegra_pcie_soc_data tegra30_pcie_data = {
2072 .num_ports = 3,
2073 .msi_base_shift = 8,
2074 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2075 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2076 .has_pex_clkreq_en = true,
2077 .has_pex_bias_ctrl = true,
2078 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002079 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002080 .has_gen2 = false,
2081};
2082
2083static const struct tegra_pcie_soc_data tegra124_pcie_data = {
2084 .num_ports = 2,
2085 .msi_base_shift = 8,
2086 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2087 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2088 .has_pex_clkreq_en = true,
2089 .has_pex_bias_ctrl = true,
2090 .has_intr_prsnt_sense = true,
2091 .has_cml_clk = true,
2092 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002093};
2094
2095static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002096 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002097 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
2098 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
2099 { },
2100};
2101MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
2102
Thierry Reding2cb989f2014-07-22 12:30:46 -06002103static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2104{
2105 struct tegra_pcie *pcie = s->private;
2106
2107 if (list_empty(&pcie->ports))
2108 return NULL;
2109
2110 seq_printf(s, "Index Status\n");
2111
2112 return seq_list_start(&pcie->ports, *pos);
2113}
2114
2115static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2116{
2117 struct tegra_pcie *pcie = s->private;
2118
2119 return seq_list_next(v, &pcie->ports, pos);
2120}
2121
2122static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2123{
2124}
2125
2126static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2127{
2128 bool up = false, active = false;
2129 struct tegra_pcie_port *port;
2130 unsigned int value;
2131
2132 port = list_entry(v, struct tegra_pcie_port, list);
2133
2134 value = readl(port->base + RP_VEND_XP);
2135
2136 if (value & RP_VEND_XP_DL_UP)
2137 up = true;
2138
2139 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2140
2141 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2142 active = true;
2143
2144 seq_printf(s, "%2u ", port->index);
2145
2146 if (up)
2147 seq_printf(s, "up");
2148
2149 if (active) {
2150 if (up)
2151 seq_printf(s, ", ");
2152
2153 seq_printf(s, "active");
2154 }
2155
2156 seq_printf(s, "\n");
2157 return 0;
2158}
2159
2160static const struct seq_operations tegra_pcie_ports_seq_ops = {
2161 .start = tegra_pcie_ports_seq_start,
2162 .next = tegra_pcie_ports_seq_next,
2163 .stop = tegra_pcie_ports_seq_stop,
2164 .show = tegra_pcie_ports_seq_show,
2165};
2166
2167static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2168{
2169 struct tegra_pcie *pcie = inode->i_private;
2170 struct seq_file *s;
2171 int err;
2172
2173 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2174 if (err)
2175 return err;
2176
2177 s = file->private_data;
2178 s->private = pcie;
2179
2180 return 0;
2181}
2182
2183static const struct file_operations tegra_pcie_ports_ops = {
2184 .owner = THIS_MODULE,
2185 .open = tegra_pcie_ports_open,
2186 .read = seq_read,
2187 .llseek = seq_lseek,
2188 .release = seq_release,
2189};
2190
2191static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2192{
2193 struct dentry *file;
2194
2195 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2196 if (!pcie->debugfs)
2197 return -ENOMEM;
2198
2199 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2200 pcie, &tegra_pcie_ports_ops);
2201 if (!file)
2202 goto remove;
2203
2204 return 0;
2205
2206remove:
2207 debugfs_remove_recursive(pcie->debugfs);
2208 pcie->debugfs = NULL;
2209 return -ENOMEM;
2210}
2211
Thierry Redingd1523b52013-08-09 16:49:19 +02002212static int tegra_pcie_probe(struct platform_device *pdev)
2213{
Jay Agarwal94716cd2013-08-09 16:49:24 +02002214 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02002215 struct tegra_pcie *pcie;
2216 int err;
2217
Jay Agarwal94716cd2013-08-09 16:49:24 +02002218 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2219 if (!match)
2220 return -ENODEV;
2221
Thierry Redingd1523b52013-08-09 16:49:19 +02002222 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2223 if (!pcie)
2224 return -ENOMEM;
2225
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002226 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002227 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02002228 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02002229 pcie->dev = &pdev->dev;
2230
2231 err = tegra_pcie_parse_dt(pcie);
2232 if (err < 0)
2233 return err;
2234
2235 pcibios_min_mem = 0;
2236
2237 err = tegra_pcie_get_resources(pcie);
2238 if (err < 0) {
2239 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2240 return err;
2241 }
2242
2243 err = tegra_pcie_enable_controller(pcie);
2244 if (err)
2245 goto put_resources;
2246
2247 /* setup the AFI address translations */
2248 tegra_pcie_setup_translations(pcie);
2249
2250 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2251 err = tegra_pcie_enable_msi(pcie);
2252 if (err < 0) {
2253 dev_err(&pdev->dev,
2254 "failed to enable MSI support: %d\n",
2255 err);
2256 goto put_resources;
2257 }
2258 }
2259
2260 err = tegra_pcie_enable(pcie);
2261 if (err < 0) {
2262 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2263 goto disable_msi;
2264 }
2265
Thierry Reding2cb989f2014-07-22 12:30:46 -06002266 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2267 err = tegra_pcie_debugfs_init(pcie);
2268 if (err < 0)
2269 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2270 err);
2271 }
2272
Thierry Redingd1523b52013-08-09 16:49:19 +02002273 platform_set_drvdata(pdev, pcie);
2274 return 0;
2275
2276disable_msi:
2277 if (IS_ENABLED(CONFIG_PCI_MSI))
2278 tegra_pcie_disable_msi(pcie);
2279put_resources:
2280 tegra_pcie_put_resources(pcie);
2281 return err;
2282}
2283
Thierry Redingd1523b52013-08-09 16:49:19 +02002284static struct platform_driver tegra_pcie_driver = {
2285 .driver = {
2286 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002287 .of_match_table = tegra_pcie_of_match,
2288 .suppress_bind_attrs = true,
2289 },
2290 .probe = tegra_pcie_probe,
2291};
2292module_platform_driver(tegra_pcie_driver);
2293
2294MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
2295MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
Thierry Redingd975cb52014-07-11 08:58:58 +02002296MODULE_LICENSE("GPL v2");