blob: 54098a822714735163232c6e23e88895b761c41f [file] [log] [blame]
Thierry Redingd1523b52013-08-09 16:49:19 +02001/*
Jay Agarwal94716cd2013-08-09 16:49:24 +02002 * PCIe host controller driver for Tegra SoCs
Thierry Redingd1523b52013-08-09 16:49:19 +02003 *
4 * Copyright (c) 2010, CompuLab, Ltd.
5 * Author: Mike Rapoport <mike@compulab.co.il>
6 *
7 * Based on NVIDIA PCIe driver
8 * Copyright (c) 2008-2009, NVIDIA Corporation.
9 *
10 * Bits taken from arch/arm/mach-dove/pcie.c
11 *
Paul Gortmakerad183272016-07-02 19:13:31 -040012 * Author: Thierry Reding <treding@nvidia.com>
13 *
Thierry Redingd1523b52013-08-09 16:49:19 +020014 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 */
28
29#include <linux/clk.h>
Thierry Reding2cb989f2014-07-22 12:30:46 -060030#include <linux/debugfs.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020031#include <linux/delay.h>
32#include <linux/export.h>
33#include <linux/interrupt.h>
34#include <linux/irq.h>
35#include <linux/irqdomain.h>
36#include <linux/kernel.h>
Paul Gortmakerad183272016-07-02 19:13:31 -040037#include <linux/init.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020038#include <linux/msi.h>
39#include <linux/of_address.h>
40#include <linux/of_pci.h>
41#include <linux/of_platform.h>
42#include <linux/pci.h>
Thierry Reding7f1f0542014-08-26 17:11:38 +020043#include <linux/phy/phy.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020044#include <linux/platform_device.h>
Stephen Warren3127a6b2013-11-06 15:56:58 -070045#include <linux/reset.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020046#include <linux/sizes.h>
47#include <linux/slab.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020048#include <linux/vmalloc.h>
49#include <linux/regulator/consumer.h>
50
Thierry Reding306a7f92014-07-17 13:17:24 +020051#include <soc/tegra/cpuidle.h>
Thierry Reding72323982014-07-11 13:19:06 +020052#include <soc/tegra/pmc.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020053
Thierry Redingd1523b52013-08-09 16:49:19 +020054#include <asm/mach/irq.h>
55#include <asm/mach/map.h>
56#include <asm/mach/pci.h>
57
58#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020059
60/* register definitions */
61
62#define AFI_AXI_BAR0_SZ 0x00
63#define AFI_AXI_BAR1_SZ 0x04
64#define AFI_AXI_BAR2_SZ 0x08
65#define AFI_AXI_BAR3_SZ 0x0c
66#define AFI_AXI_BAR4_SZ 0x10
67#define AFI_AXI_BAR5_SZ 0x14
68
69#define AFI_AXI_BAR0_START 0x18
70#define AFI_AXI_BAR1_START 0x1c
71#define AFI_AXI_BAR2_START 0x20
72#define AFI_AXI_BAR3_START 0x24
73#define AFI_AXI_BAR4_START 0x28
74#define AFI_AXI_BAR5_START 0x2c
75
76#define AFI_FPCI_BAR0 0x30
77#define AFI_FPCI_BAR1 0x34
78#define AFI_FPCI_BAR2 0x38
79#define AFI_FPCI_BAR3 0x3c
80#define AFI_FPCI_BAR4 0x40
81#define AFI_FPCI_BAR5 0x44
82
83#define AFI_CACHE_BAR0_SZ 0x48
84#define AFI_CACHE_BAR0_ST 0x4c
85#define AFI_CACHE_BAR1_SZ 0x50
86#define AFI_CACHE_BAR1_ST 0x54
87
88#define AFI_MSI_BAR_SZ 0x60
89#define AFI_MSI_FPCI_BAR_ST 0x64
90#define AFI_MSI_AXI_BAR_ST 0x68
91
92#define AFI_MSI_VEC0 0x6c
93#define AFI_MSI_VEC1 0x70
94#define AFI_MSI_VEC2 0x74
95#define AFI_MSI_VEC3 0x78
96#define AFI_MSI_VEC4 0x7c
97#define AFI_MSI_VEC5 0x80
98#define AFI_MSI_VEC6 0x84
99#define AFI_MSI_VEC7 0x88
100
101#define AFI_MSI_EN_VEC0 0x8c
102#define AFI_MSI_EN_VEC1 0x90
103#define AFI_MSI_EN_VEC2 0x94
104#define AFI_MSI_EN_VEC3 0x98
105#define AFI_MSI_EN_VEC4 0x9c
106#define AFI_MSI_EN_VEC5 0xa0
107#define AFI_MSI_EN_VEC6 0xa4
108#define AFI_MSI_EN_VEC7 0xa8
109
110#define AFI_CONFIGURATION 0xac
111#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
112
113#define AFI_FPCI_ERROR_MASKS 0xb0
114
115#define AFI_INTR_MASK 0xb4
116#define AFI_INTR_MASK_INT_MASK (1 << 0)
117#define AFI_INTR_MASK_MSI_MASK (1 << 8)
118
119#define AFI_INTR_CODE 0xb8
120#define AFI_INTR_CODE_MASK 0xf
Thierry Reding7f1f0542014-08-26 17:11:38 +0200121#define AFI_INTR_INI_SLAVE_ERROR 1
122#define AFI_INTR_INI_DECODE_ERROR 2
Thierry Redingd1523b52013-08-09 16:49:19 +0200123#define AFI_INTR_TARGET_ABORT 3
124#define AFI_INTR_MASTER_ABORT 4
125#define AFI_INTR_INVALID_WRITE 5
126#define AFI_INTR_LEGACY 6
127#define AFI_INTR_FPCI_DECODE_ERROR 7
Thierry Reding7f1f0542014-08-26 17:11:38 +0200128#define AFI_INTR_AXI_DECODE_ERROR 8
129#define AFI_INTR_FPCI_TIMEOUT 9
130#define AFI_INTR_PE_PRSNT_SENSE 10
131#define AFI_INTR_PE_CLKREQ_SENSE 11
132#define AFI_INTR_CLKCLAMP_SENSE 12
133#define AFI_INTR_RDY4PD_SENSE 13
134#define AFI_INTR_P2P_ERROR 14
Thierry Redingd1523b52013-08-09 16:49:19 +0200135
136#define AFI_INTR_SIGNATURE 0xbc
137#define AFI_UPPER_FPCI_ADDRESS 0xc0
138#define AFI_SM_INTR_ENABLE 0xc4
139#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
140#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
141#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
142#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
143#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
144#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
145#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
146#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
147
148#define AFI_AFI_INTR_ENABLE 0xc8
149#define AFI_INTR_EN_INI_SLVERR (1 << 0)
150#define AFI_INTR_EN_INI_DECERR (1 << 1)
151#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
152#define AFI_INTR_EN_TGT_DECERR (1 << 3)
153#define AFI_INTR_EN_TGT_WRERR (1 << 4)
154#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
155#define AFI_INTR_EN_AXI_DECERR (1 << 6)
156#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200157#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200158
159#define AFI_PCIE_CONFIG 0x0f8
160#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
161#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
162#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
163#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200164#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200165#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200166#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200167#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200168#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200169#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200170
171#define AFI_FUSE 0x104
172#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
173
174#define AFI_PEX0_CTRL 0x110
175#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200176#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200177#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200178#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200179#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200180#define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
181
182#define AFI_PLLE_CONTROL 0x160
183#define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
184#define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200185
Jay Agarwal94716cd2013-08-09 16:49:24 +0200186#define AFI_PEXBIAS_CTRL_0 0x168
187
Thierry Redingd1523b52013-08-09 16:49:19 +0200188#define RP_VEND_XP 0x00000F00
189#define RP_VEND_XP_DL_UP (1 << 30)
190
Thierry Reding7f1f0542014-08-26 17:11:38 +0200191#define RP_PRIV_MISC 0x00000FE0
192#define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
193#define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
194
Thierry Redingd1523b52013-08-09 16:49:19 +0200195#define RP_LINK_CONTROL_STATUS 0x00000090
196#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
197#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
198
199#define PADS_CTL_SEL 0x0000009C
200
201#define PADS_CTL 0x000000A0
202#define PADS_CTL_IDDQ_1L (1 << 0)
203#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
204#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
205
Jay Agarwal94716cd2013-08-09 16:49:24 +0200206#define PADS_PLL_CTL_TEGRA20 0x000000B8
207#define PADS_PLL_CTL_TEGRA30 0x000000B4
Thierry Redingd1523b52013-08-09 16:49:19 +0200208#define PADS_PLL_CTL_RST_B4SM (1 << 1)
209#define PADS_PLL_CTL_LOCKDET (1 << 8)
210#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
211#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
212#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
213#define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
214#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
215#define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
216#define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200217#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
218
219#define PADS_REFCLK_CFG0 0x000000C8
220#define PADS_REFCLK_CFG1 0x000000CC
Thierry Reding7f1f0542014-08-26 17:11:38 +0200221#define PADS_REFCLK_BIAS 0x000000D0
Thierry Redingd1523b52013-08-09 16:49:19 +0200222
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200223/*
224 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
225 * entries, one entry per PCIe port. These field definitions and desired
226 * values aren't in the TRM, but do come from NVIDIA.
227 */
228#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
229#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
230#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
231#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
232
233/* Default value provided by HW engineering is 0xfa5c */
234#define PADS_REFCLK_CFG_VALUE \
235 ( \
236 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
237 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
238 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
239 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
240 )
241
Thierry Redingd1523b52013-08-09 16:49:19 +0200242struct tegra_msi {
Yijing Wangc2791b82014-11-11 17:45:45 -0700243 struct msi_controller chip;
Thierry Redingd1523b52013-08-09 16:49:19 +0200244 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
245 struct irq_domain *domain;
246 unsigned long pages;
247 struct mutex lock;
248 int irq;
249};
250
Jay Agarwal94716cd2013-08-09 16:49:24 +0200251/* used to differentiate between Tegra SoC generations */
252struct tegra_pcie_soc_data {
253 unsigned int num_ports;
254 unsigned int msi_base_shift;
255 u32 pads_pll_ctl;
256 u32 tx_ref_sel;
257 bool has_pex_clkreq_en;
258 bool has_pex_bias_ctrl;
259 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200260 bool has_cml_clk;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200261 bool has_gen2;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200262};
263
Yijing Wangc2791b82014-11-11 17:45:45 -0700264static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
Thierry Redingd1523b52013-08-09 16:49:19 +0200265{
266 return container_of(chip, struct tegra_msi, chip);
267}
268
269struct tegra_pcie {
270 struct device *dev;
271
272 void __iomem *pads;
273 void __iomem *afi;
274 int irq;
275
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700276 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200277 struct resource *cs;
278
Thierry Reding41534e52014-08-01 14:15:11 +0200279 struct resource all;
Thierry Redingd1523b52013-08-09 16:49:19 +0200280 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100281 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200282 struct resource mem;
283 struct resource prefetch;
284 struct resource busn;
285
Thierry Reding56e75e22016-02-09 15:52:32 +0100286 struct {
287 resource_size_t mem;
288 resource_size_t io;
289 } offset;
290
Thierry Redingd1523b52013-08-09 16:49:19 +0200291 struct clk *pex_clk;
292 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200293 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200294 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200295
Stephen Warren3127a6b2013-11-06 15:56:58 -0700296 struct reset_control *pex_rst;
297 struct reset_control *afi_rst;
298 struct reset_control *pcie_xrst;
299
Thierry Reding6fe7c182015-11-11 18:25:59 +0100300 bool legacy_phy;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200301 struct phy *phy;
302
Thierry Redingd1523b52013-08-09 16:49:19 +0200303 struct tegra_msi msi;
304
305 struct list_head ports;
Thierry Redingd1523b52013-08-09 16:49:19 +0200306 u32 xbar_config;
307
Thierry Reding077fb152014-05-28 16:49:13 +0200308 struct regulator_bulk_data *supplies;
309 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200310
311 const struct tegra_pcie_soc_data *soc_data;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600312 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200313};
314
315struct tegra_pcie_port {
316 struct tegra_pcie *pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100317 struct device_node *np;
Thierry Redingd1523b52013-08-09 16:49:19 +0200318 struct list_head list;
319 struct resource regs;
320 void __iomem *base;
321 unsigned int index;
322 unsigned int lanes;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100323
324 struct phy **phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200325};
326
327struct tegra_pcie_bus {
328 struct vm_struct *area;
329 struct list_head list;
330 unsigned int nr;
331};
332
333static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
334{
335 return sys->private_data;
336}
337
338static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
339 unsigned long offset)
340{
341 writel(value, pcie->afi + offset);
342}
343
344static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
345{
346 return readl(pcie->afi + offset);
347}
348
349static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
350 unsigned long offset)
351{
352 writel(value, pcie->pads + offset);
353}
354
355static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
356{
357 return readl(pcie->pads + offset);
358}
359
360/*
361 * The configuration space mapping on Tegra is somewhat similar to the ECAM
362 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
363 * register accesses are mapped:
364 *
365 * [27:24] extended register number
366 * [23:16] bus number
367 * [15:11] device number
368 * [10: 8] function number
369 * [ 7: 0] register number
370 *
371 * Mapping the whole extended configuration space would require 256 MiB of
372 * virtual address space, only a small part of which will actually be used.
373 * To work around this, a 1 MiB of virtual addresses are allocated per bus
374 * when the bus is first accessed. When the physical range is mapped, the
375 * the bus number bits are hidden so that the extended register number bits
376 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
377 *
378 * [19:16] extended register number
379 * [15:11] device number
380 * [10: 8] function number
381 * [ 7: 0] register number
382 *
383 * This is achieved by stitching together 16 chunks of 64 KiB of physical
384 * address space via the MMU.
385 */
386static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
387{
388 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
389 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
390}
391
392static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
393 unsigned int busnr)
394{
Ard Biesheuvel512ee2f2015-08-26 10:49:34 +0200395 pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
396 L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
Thierry Redingd1523b52013-08-09 16:49:19 +0200397 phys_addr_t cs = pcie->cs->start;
398 struct tegra_pcie_bus *bus;
399 unsigned int i;
400 int err;
401
402 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
403 if (!bus)
404 return ERR_PTR(-ENOMEM);
405
406 INIT_LIST_HEAD(&bus->list);
407 bus->nr = busnr;
408
409 /* allocate 1 MiB of virtual addresses */
410 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
411 if (!bus->area) {
412 err = -ENOMEM;
413 goto free;
414 }
415
416 /* map each of the 16 chunks of 64 KiB each */
417 for (i = 0; i < 16; i++) {
418 unsigned long virt = (unsigned long)bus->area->addr +
419 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200420 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200421
422 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
423 if (err < 0) {
424 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
425 err);
426 goto unmap;
427 }
428 }
429
430 return bus;
431
432unmap:
433 vunmap(bus->area->addr);
434free:
435 kfree(bus);
436 return ERR_PTR(err);
437}
438
Thierry Redingb4d18d72016-02-09 15:30:48 +0100439static int tegra_pcie_add_bus(struct pci_bus *bus)
Thierry Redingd1523b52013-08-09 16:49:19 +0200440{
Thierry Redingb4d18d72016-02-09 15:30:48 +0100441 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
442 struct tegra_pcie_bus *b;
Thierry Redingd1523b52013-08-09 16:49:19 +0200443
Thierry Redingb4d18d72016-02-09 15:30:48 +0100444 b = tegra_pcie_bus_alloc(pcie, bus->number);
445 if (IS_ERR(b))
446 return PTR_ERR(b);
Thierry Redingd1523b52013-08-09 16:49:19 +0200447
Thierry Redingb4d18d72016-02-09 15:30:48 +0100448 list_add_tail(&b->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200449
Thierry Redingb4d18d72016-02-09 15:30:48 +0100450 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200451}
452
Thierry Redingb4d18d72016-02-09 15:30:48 +0100453static void tegra_pcie_remove_bus(struct pci_bus *child)
454{
455 struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
456 struct tegra_pcie_bus *bus, *tmp;
457
458 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
459 if (bus->nr == child->number) {
460 vunmap(bus->area->addr);
461 list_del(&bus->list);
462 kfree(bus);
463 break;
464 }
465 }
466}
467
468static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
469 unsigned int devfn,
470 int where)
Thierry Redingd1523b52013-08-09 16:49:19 +0200471{
472 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
473 void __iomem *addr = NULL;
474
475 if (bus->number == 0) {
476 unsigned int slot = PCI_SLOT(devfn);
477 struct tegra_pcie_port *port;
478
479 list_for_each_entry(port, &pcie->ports, list) {
480 if (port->index + 1 == slot) {
481 addr = port->base + (where & ~3);
482 break;
483 }
484 }
485 } else {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100486 struct tegra_pcie_bus *b;
487
488 list_for_each_entry(b, &pcie->buses, list)
489 if (b->nr == bus->number)
490 addr = (void __iomem *)b->area->addr;
491
Thierry Redingd1523b52013-08-09 16:49:19 +0200492 if (!addr) {
493 dev_err(pcie->dev,
494 "failed to map cfg. space for bus %u\n",
495 bus->number);
496 return NULL;
497 }
498
499 addr += tegra_pcie_conf_offset(devfn, where);
500 }
501
502 return addr;
503}
504
Thierry Redingd1523b52013-08-09 16:49:19 +0200505static struct pci_ops tegra_pcie_ops = {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100506 .add_bus = tegra_pcie_add_bus,
507 .remove_bus = tegra_pcie_remove_bus,
508 .map_bus = tegra_pcie_map_bus,
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600509 .read = pci_generic_config_read32,
510 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200511};
512
513static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
514{
515 unsigned long ret = 0;
516
517 switch (port->index) {
518 case 0:
519 ret = AFI_PEX0_CTRL;
520 break;
521
522 case 1:
523 ret = AFI_PEX1_CTRL;
524 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200525
526 case 2:
527 ret = AFI_PEX2_CTRL;
528 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200529 }
530
531 return ret;
532}
533
534static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
535{
536 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
537 unsigned long value;
538
539 /* pulse reset signal */
540 value = afi_readl(port->pcie, ctrl);
541 value &= ~AFI_PEX_CTRL_RST;
542 afi_writel(port->pcie, value, ctrl);
543
544 usleep_range(1000, 2000);
545
546 value = afi_readl(port->pcie, ctrl);
547 value |= AFI_PEX_CTRL_RST;
548 afi_writel(port->pcie, value, ctrl);
549}
550
551static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
552{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200553 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200554 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
555 unsigned long value;
556
557 /* enable reference clock */
558 value = afi_readl(port->pcie, ctrl);
559 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200560
561 if (soc->has_pex_clkreq_en)
562 value |= AFI_PEX_CTRL_CLKREQ_EN;
563
Thierry Reding7f1f0542014-08-26 17:11:38 +0200564 value |= AFI_PEX_CTRL_OVERRIDE_EN;
565
Thierry Redingd1523b52013-08-09 16:49:19 +0200566 afi_writel(port->pcie, value, ctrl);
567
568 tegra_pcie_port_reset(port);
569}
570
571static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
572{
Thierry Reding0d20d622014-08-26 17:11:35 +0200573 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200574 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
575 unsigned long value;
576
577 /* assert port reset */
578 value = afi_readl(port->pcie, ctrl);
579 value &= ~AFI_PEX_CTRL_RST;
580 afi_writel(port->pcie, value, ctrl);
581
582 /* disable reference clock */
583 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200584
585 if (soc->has_pex_clkreq_en)
586 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
587
Thierry Redingd1523b52013-08-09 16:49:19 +0200588 value &= ~AFI_PEX_CTRL_REFCLK_EN;
589 afi_writel(port->pcie, value, ctrl);
590}
591
592static void tegra_pcie_port_free(struct tegra_pcie_port *port)
593{
594 struct tegra_pcie *pcie = port->pcie;
595
596 devm_iounmap(pcie->dev, port->base);
597 devm_release_mem_region(pcie->dev, port->regs.start,
598 resource_size(&port->regs));
599 list_del(&port->list);
600 devm_kfree(pcie->dev, port);
601}
602
Thierry Redingd1523b52013-08-09 16:49:19 +0200603/* Tegra PCIE root complex wrongly reports device class */
604static void tegra_pcie_fixup_class(struct pci_dev *dev)
605{
606 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
607}
608DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
609DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200610DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
611DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200612
613/* Tegra PCIE requires relaxed ordering */
614static void tegra_pcie_relax_enable(struct pci_dev *dev)
615{
616 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
617}
618DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
619
620static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
621{
622 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200623 int err;
624
Thierry Reding56e75e22016-02-09 15:52:32 +0100625 sys->mem_offset = pcie->offset.mem;
626 sys->io_offset = pcie->offset.io;
627
628 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->io);
629 if (err < 0)
630 return err;
631
632 err = devm_request_resource(pcie->dev, &ioport_resource, &pcie->pio);
633 if (err < 0)
634 return err;
635
Thierry Reding41534e52014-08-01 14:15:11 +0200636 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
637 if (err < 0)
638 return err;
639
640 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
641 if (err)
642 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200643
Thierry Reding56e75e22016-02-09 15:52:32 +0100644 pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
Thierry Redingd1523b52013-08-09 16:49:19 +0200645 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
646 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
647 sys->mem_offset);
648 pci_add_resource(&sys->resources, &pcie->busn);
649
Thierry Reding51067872014-11-27 09:54:09 +0100650 pci_ioremap_io(pcie->pio.start, pcie->io.start);
Thierry Redingd1523b52013-08-09 16:49:19 +0200651
652 return 1;
653}
654
655static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
656{
657 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600658 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200659
Stephen Warrenb4f17372013-05-06 14:19:19 -0600660 tegra_cpuidle_pcie_irqs_in_use();
661
Lucas Stachf5d33522014-04-16 10:24:32 -0600662 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
663 if (!irq)
664 irq = pcie->irq;
665
666 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200667}
668
Thierry Redingd1523b52013-08-09 16:49:19 +0200669static irqreturn_t tegra_pcie_isr(int irq, void *arg)
670{
671 const char *err_msg[] = {
672 "Unknown",
673 "AXI slave error",
674 "AXI decode error",
675 "Target abort",
676 "Master abort",
677 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200678 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200679 "Response decoding error",
680 "AXI response decoding error",
681 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200682 "Slot present pin change",
683 "Slot clock request change",
684 "TMS clock ramp change",
685 "TMS ready for power down",
686 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200687 };
688 struct tegra_pcie *pcie = arg;
689 u32 code, signature;
690
691 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
692 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
693 afi_writel(pcie, 0, AFI_INTR_CODE);
694
695 if (code == AFI_INTR_LEGACY)
696 return IRQ_NONE;
697
698 if (code >= ARRAY_SIZE(err_msg))
699 code = 0;
700
701 /*
702 * do not pollute kernel log with master abort reports since they
703 * happen a lot during enumeration
704 */
705 if (code == AFI_INTR_MASTER_ABORT)
706 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
707 signature);
708 else
709 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
710 signature);
711
712 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
713 code == AFI_INTR_FPCI_DECODE_ERROR) {
714 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
715 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
716
717 if (code == AFI_INTR_MASTER_ABORT)
718 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
719 else
720 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
721 }
722
723 return IRQ_HANDLED;
724}
725
726/*
727 * FPCI map is as follows:
728 * - 0xfdfc000000: I/O space
729 * - 0xfdfe000000: type 0 configuration space
730 * - 0xfdff000000: type 1 configuration space
731 * - 0xfe00000000: type 0 extended configuration space
732 * - 0xfe10000000: type 1 extended configuration space
733 */
734static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
735{
736 u32 fpci_bar, size, axi_address;
737
738 /* Bar 0: type 1 extended configuration space */
739 fpci_bar = 0xfe100000;
740 size = resource_size(pcie->cs);
741 axi_address = pcie->cs->start;
742 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
743 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
744 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
745
746 /* Bar 1: downstream IO bar */
747 fpci_bar = 0xfdfc0000;
748 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100749 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200750 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
751 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
752 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
753
754 /* Bar 2: prefetchable memory BAR */
755 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
756 size = resource_size(&pcie->prefetch);
757 axi_address = pcie->prefetch.start;
758 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
759 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
760 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
761
762 /* Bar 3: non prefetchable memory BAR */
763 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
764 size = resource_size(&pcie->mem);
765 axi_address = pcie->mem.start;
766 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
767 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
768 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
769
770 /* NULL out the remaining BARs as they are not used */
771 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
772 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
773 afi_writel(pcie, 0, AFI_FPCI_BAR4);
774
775 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
776 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
777 afi_writel(pcie, 0, AFI_FPCI_BAR5);
778
779 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100780 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200781 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
782 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
783 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
784
785 /* MSI translations are setup only when needed */
786 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
787 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
788 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
789 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
790}
791
Thierry Reding7f1f0542014-08-26 17:11:38 +0200792static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200793{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200794 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200795 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200796
Thierry Reding7f1f0542014-08-26 17:11:38 +0200797 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200798
Thierry Reding7f1f0542014-08-26 17:11:38 +0200799 while (time_before(jiffies, timeout)) {
800 value = pads_readl(pcie, soc->pads_pll_ctl);
801 if (value & PADS_PLL_CTL_LOCKDET)
802 return 0;
803 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200804
Thierry Reding7f1f0542014-08-26 17:11:38 +0200805 return -ETIMEDOUT;
806}
Thierry Redingd1523b52013-08-09 16:49:19 +0200807
Thierry Reding7f1f0542014-08-26 17:11:38 +0200808static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
809{
810 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
811 u32 value;
812 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200813
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700814 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200815 pads_writel(pcie, 0x0, PADS_CTL_SEL);
816
817 /* override IDDQ to 1 on all 4 lanes */
818 value = pads_readl(pcie, PADS_CTL);
819 value |= PADS_CTL_IDDQ_1L;
820 pads_writel(pcie, value, PADS_CTL);
821
822 /*
823 * Set up PHY PLL inputs select PLLE output as refclock,
824 * set TX ref sel to div10 (not div5).
825 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200826 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200827 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200828 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
829 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200830
Eric Yuenec732762014-08-26 17:11:37 +0200831 /* reset PLL */
832 value = pads_readl(pcie, soc->pads_pll_ctl);
833 value &= ~PADS_PLL_CTL_RST_B4SM;
834 pads_writel(pcie, value, soc->pads_pll_ctl);
835
836 usleep_range(20, 100);
837
Thierry Redingd1523b52013-08-09 16:49:19 +0200838 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200839 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200840 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200841 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200842
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200843 /* Configure the reference clock driver */
844 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
845 pads_writel(pcie, value, PADS_REFCLK_CFG0);
846 if (soc->num_ports > 2)
847 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200848
849 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200850 err = tegra_pcie_pll_wait(pcie, 500);
851 if (err < 0) {
852 dev_err(pcie->dev, "PLL failed to lock: %d\n", err);
853 return err;
854 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200855
856 /* turn off IDDQ override */
857 value = pads_readl(pcie, PADS_CTL);
858 value &= ~PADS_CTL_IDDQ_1L;
859 pads_writel(pcie, value, PADS_CTL);
860
861 /* enable TX/RX data */
862 value = pads_readl(pcie, PADS_CTL);
863 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
864 pads_writel(pcie, value, PADS_CTL);
865
Thierry Reding7f1f0542014-08-26 17:11:38 +0200866 return 0;
867}
868
Thierry Reding6fe7c182015-11-11 18:25:59 +0100869static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
870{
871 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
872 u32 value;
873
874 /* disable TX/RX data */
875 value = pads_readl(pcie, PADS_CTL);
876 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
877 pads_writel(pcie, value, PADS_CTL);
878
879 /* override IDDQ */
880 value = pads_readl(pcie, PADS_CTL);
881 value |= PADS_CTL_IDDQ_1L;
882 pads_writel(pcie, PADS_CTL, value);
883
884 /* reset PLL */
885 value = pads_readl(pcie, soc->pads_pll_ctl);
886 value &= ~PADS_PLL_CTL_RST_B4SM;
887 pads_writel(pcie, value, soc->pads_pll_ctl);
888
889 usleep_range(20, 100);
890
891 return 0;
892}
893
894static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
895{
896 struct device *dev = port->pcie->dev;
897 unsigned int i;
898 int err;
899
900 for (i = 0; i < port->lanes; i++) {
901 err = phy_power_on(port->phys[i]);
902 if (err < 0) {
903 dev_err(dev, "failed to power on PHY#%u: %d\n", i,
904 err);
905 return err;
906 }
907 }
908
909 return 0;
910}
911
912static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
913{
914 struct device *dev = port->pcie->dev;
915 unsigned int i;
916 int err;
917
918 for (i = 0; i < port->lanes; i++) {
919 err = phy_power_off(port->phys[i]);
920 if (err < 0) {
921 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
922 err);
923 return err;
924 }
925 }
926
927 return 0;
928}
929
930static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
931{
932 struct tegra_pcie_port *port;
933 int err;
934
935 if (pcie->legacy_phy) {
936 if (pcie->phy)
937 err = phy_power_on(pcie->phy);
938 else
939 err = tegra_pcie_phy_enable(pcie);
940
941 if (err < 0)
942 dev_err(pcie->dev, "failed to power on PHY: %d\n", err);
943
944 return err;
945 }
946
947 list_for_each_entry(port, &pcie->ports, list) {
948 err = tegra_pcie_port_phy_power_on(port);
949 if (err < 0) {
950 dev_err(pcie->dev,
951 "failed to power on PCIe port %u PHY: %d\n",
952 port->index, err);
953 return err;
954 }
955 }
956
957 return 0;
958}
959
960static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
961{
962 struct tegra_pcie_port *port;
963 int err;
964
965 if (pcie->legacy_phy) {
966 if (pcie->phy)
967 err = phy_power_off(pcie->phy);
968 else
969 err = tegra_pcie_phy_disable(pcie);
970
971 if (err < 0)
972 dev_err(pcie->dev, "failed to power off PHY: %d\n",
973 err);
974
975 return err;
976 }
977
978 list_for_each_entry(port, &pcie->ports, list) {
979 err = tegra_pcie_port_phy_power_off(port);
980 if (err < 0) {
981 dev_err(pcie->dev,
982 "failed to power off PCIe port %u PHY: %d\n",
983 port->index, err);
984 return err;
985 }
986 }
987
988 return 0;
989}
990
Thierry Reding7f1f0542014-08-26 17:11:38 +0200991static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
992{
993 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
994 struct tegra_pcie_port *port;
995 unsigned long value;
996 int err;
997
998 /* enable PLL power down */
999 if (pcie->phy) {
1000 value = afi_readl(pcie, AFI_PLLE_CONTROL);
1001 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
1002 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1003 afi_writel(pcie, value, AFI_PLLE_CONTROL);
1004 }
1005
1006 /* power down PCIe slot clock bias pad */
1007 if (soc->has_pex_bias_ctrl)
1008 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
1009
1010 /* configure mode and disable all ports */
1011 value = afi_readl(pcie, AFI_PCIE_CONFIG);
1012 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1013 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1014
1015 list_for_each_entry(port, &pcie->ports, list)
1016 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1017
1018 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1019
1020 if (soc->has_gen2) {
1021 value = afi_readl(pcie, AFI_FUSE);
1022 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1023 afi_writel(pcie, value, AFI_FUSE);
1024 } else {
1025 value = afi_readl(pcie, AFI_FUSE);
1026 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1027 afi_writel(pcie, value, AFI_FUSE);
1028 }
1029
Thierry Reding6fe7c182015-11-11 18:25:59 +01001030 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001031 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001032 dev_err(pcie->dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001033 return err;
1034 }
1035
Thierry Redingd1523b52013-08-09 16:49:19 +02001036 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001037 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001038
1039 /* finally enable PCIe */
1040 value = afi_readl(pcie, AFI_CONFIGURATION);
1041 value |= AFI_CONFIGURATION_EN_FPCI;
1042 afi_writel(pcie, value, AFI_CONFIGURATION);
1043
1044 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1045 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1046 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001047
1048 if (soc->has_intr_prsnt_sense)
1049 value |= AFI_INTR_EN_PRSNT_SENSE;
1050
Thierry Redingd1523b52013-08-09 16:49:19 +02001051 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1052 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1053
1054 /* don't enable MSI for now, only when needed */
1055 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1056
1057 /* disable all exceptions */
1058 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1059
1060 return 0;
1061}
1062
1063static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1064{
1065 int err;
1066
1067 /* TODO: disable and unprepare clocks? */
1068
Thierry Reding6fe7c182015-11-11 18:25:59 +01001069 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001070 if (err < 0)
Thierry Reding6fe7c182015-11-11 18:25:59 +01001071 dev_err(pcie->dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001072
Stephen Warren3127a6b2013-11-06 15:56:58 -07001073 reset_control_assert(pcie->pcie_xrst);
1074 reset_control_assert(pcie->afi_rst);
1075 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001076
1077 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1078
Thierry Reding077fb152014-05-28 16:49:13 +02001079 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001080 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +02001081 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001082}
1083
1084static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1085{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001086 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001087 int err;
1088
Stephen Warren3127a6b2013-11-06 15:56:58 -07001089 reset_control_assert(pcie->pcie_xrst);
1090 reset_control_assert(pcie->afi_rst);
1091 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001092
1093 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1094
1095 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001096 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1097 if (err < 0)
1098 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001099
Thierry Redingd1523b52013-08-09 16:49:19 +02001100 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001101 pcie->pex_clk,
1102 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001103 if (err) {
1104 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
1105 return err;
1106 }
1107
Stephen Warren3127a6b2013-11-06 15:56:58 -07001108 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001109
1110 err = clk_prepare_enable(pcie->afi_clk);
1111 if (err < 0) {
1112 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
1113 return err;
1114 }
1115
Jay Agarwal94716cd2013-08-09 16:49:24 +02001116 if (soc->has_cml_clk) {
1117 err = clk_prepare_enable(pcie->cml_clk);
1118 if (err < 0) {
1119 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
1120 err);
1121 return err;
1122 }
1123 }
1124
Thierry Redingd1523b52013-08-09 16:49:19 +02001125 err = clk_prepare_enable(pcie->pll_e);
1126 if (err < 0) {
1127 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
1128 return err;
1129 }
1130
1131 return 0;
1132}
1133
1134static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1135{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001136 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1137
Thierry Redingd1523b52013-08-09 16:49:19 +02001138 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
1139 if (IS_ERR(pcie->pex_clk))
1140 return PTR_ERR(pcie->pex_clk);
1141
1142 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
1143 if (IS_ERR(pcie->afi_clk))
1144 return PTR_ERR(pcie->afi_clk);
1145
Thierry Redingd1523b52013-08-09 16:49:19 +02001146 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1147 if (IS_ERR(pcie->pll_e))
1148 return PTR_ERR(pcie->pll_e);
1149
Jay Agarwal94716cd2013-08-09 16:49:24 +02001150 if (soc->has_cml_clk) {
1151 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1152 if (IS_ERR(pcie->cml_clk))
1153 return PTR_ERR(pcie->cml_clk);
1154 }
1155
Thierry Redingd1523b52013-08-09 16:49:19 +02001156 return 0;
1157}
1158
Stephen Warren3127a6b2013-11-06 15:56:58 -07001159static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1160{
1161 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1162 if (IS_ERR(pcie->pex_rst))
1163 return PTR_ERR(pcie->pex_rst);
1164
1165 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1166 if (IS_ERR(pcie->afi_rst))
1167 return PTR_ERR(pcie->afi_rst);
1168
1169 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1170 if (IS_ERR(pcie->pcie_xrst))
1171 return PTR_ERR(pcie->pcie_xrst);
1172
1173 return 0;
1174}
1175
Thierry Reding6fe7c182015-11-11 18:25:59 +01001176static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1177{
1178 int err;
1179
1180 pcie->phy = devm_phy_optional_get(pcie->dev, "pcie");
1181 if (IS_ERR(pcie->phy)) {
1182 err = PTR_ERR(pcie->phy);
1183 dev_err(pcie->dev, "failed to get PHY: %d\n", err);
1184 return err;
1185 }
1186
1187 err = phy_init(pcie->phy);
1188 if (err < 0) {
1189 dev_err(pcie->dev, "failed to initialize PHY: %d\n", err);
1190 return err;
1191 }
1192
1193 pcie->legacy_phy = true;
1194
1195 return 0;
1196}
1197
1198static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1199 struct device_node *np,
1200 const char *consumer,
1201 unsigned int index)
1202{
1203 struct phy *phy;
1204 char *name;
1205
1206 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1207 if (!name)
1208 return ERR_PTR(-ENOMEM);
1209
1210 phy = devm_of_phy_get(dev, np, name);
1211 kfree(name);
1212
1213 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1214 phy = NULL;
1215
1216 return phy;
1217}
1218
1219static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1220{
1221 struct device *dev = port->pcie->dev;
1222 struct phy *phy;
1223 unsigned int i;
1224 int err;
1225
1226 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1227 if (!port->phys)
1228 return -ENOMEM;
1229
1230 for (i = 0; i < port->lanes; i++) {
1231 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1232 if (IS_ERR(phy)) {
1233 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1234 PTR_ERR(phy));
1235 return PTR_ERR(phy);
1236 }
1237
1238 err = phy_init(phy);
1239 if (err < 0) {
1240 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1241 err);
1242 return err;
1243 }
1244
1245 port->phys[i] = phy;
1246 }
1247
1248 return 0;
1249}
1250
1251static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1252{
1253 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
1254 struct device_node *np = pcie->dev->of_node;
1255 struct tegra_pcie_port *port;
1256 int err;
1257
1258 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1259 return tegra_pcie_phys_get_legacy(pcie);
1260
1261 list_for_each_entry(port, &pcie->ports, list) {
1262 err = tegra_pcie_port_get_phys(port);
1263 if (err < 0)
1264 return err;
1265 }
1266
1267 return 0;
1268}
1269
Thierry Redingd1523b52013-08-09 16:49:19 +02001270static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1271{
1272 struct platform_device *pdev = to_platform_device(pcie->dev);
1273 struct resource *pads, *afi, *res;
1274 int err;
1275
1276 err = tegra_pcie_clocks_get(pcie);
1277 if (err) {
1278 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1279 return err;
1280 }
1281
Stephen Warren3127a6b2013-11-06 15:56:58 -07001282 err = tegra_pcie_resets_get(pcie);
1283 if (err) {
1284 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1285 return err;
1286 }
1287
Thierry Reding6fe7c182015-11-11 18:25:59 +01001288 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001289 if (err < 0) {
Thierry Reding6fe7c182015-11-11 18:25:59 +01001290 dev_err(&pdev->dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001291 return err;
1292 }
1293
Thierry Redingd1523b52013-08-09 16:49:19 +02001294 err = tegra_pcie_power_on(pcie);
1295 if (err) {
1296 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1297 return err;
1298 }
1299
Thierry Redingd1523b52013-08-09 16:49:19 +02001300 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001301 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1302 if (IS_ERR(pcie->pads)) {
1303 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001304 goto poweroff;
1305 }
1306
1307 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001308 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1309 if (IS_ERR(pcie->afi)) {
1310 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001311 goto poweroff;
1312 }
1313
Julia Lawalldc05ee32013-08-26 11:11:09 +02001314 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001315 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1316 if (!res) {
1317 err = -EADDRNOTAVAIL;
1318 goto poweroff;
1319 }
1320
1321 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1322 resource_size(res), res->name);
1323 if (!pcie->cs) {
1324 err = -EADDRNOTAVAIL;
1325 goto poweroff;
1326 }
1327
1328 /* request interrupt */
1329 err = platform_get_irq_byname(pdev, "intr");
1330 if (err < 0) {
1331 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1332 goto poweroff;
1333 }
1334
1335 pcie->irq = err;
1336
1337 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1338 if (err) {
1339 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1340 goto poweroff;
1341 }
1342
1343 return 0;
1344
1345poweroff:
1346 tegra_pcie_power_off(pcie);
1347 return err;
1348}
1349
1350static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1351{
Thierry Reding7f1f0542014-08-26 17:11:38 +02001352 int err;
1353
Thierry Redingd1523b52013-08-09 16:49:19 +02001354 if (pcie->irq > 0)
1355 free_irq(pcie->irq, pcie);
1356
1357 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001358
1359 err = phy_exit(pcie->phy);
1360 if (err < 0)
1361 dev_err(pcie->dev, "failed to teardown PHY: %d\n", err);
1362
Thierry Redingd1523b52013-08-09 16:49:19 +02001363 return 0;
1364}
1365
1366static int tegra_msi_alloc(struct tegra_msi *chip)
1367{
1368 int msi;
1369
1370 mutex_lock(&chip->lock);
1371
1372 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1373 if (msi < INT_PCI_MSI_NR)
1374 set_bit(msi, chip->used);
1375 else
1376 msi = -ENOSPC;
1377
1378 mutex_unlock(&chip->lock);
1379
1380 return msi;
1381}
1382
1383static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1384{
1385 struct device *dev = chip->chip.dev;
1386
1387 mutex_lock(&chip->lock);
1388
1389 if (!test_bit(irq, chip->used))
1390 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1391 else
1392 clear_bit(irq, chip->used);
1393
1394 mutex_unlock(&chip->lock);
1395}
1396
1397static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1398{
1399 struct tegra_pcie *pcie = data;
1400 struct tegra_msi *msi = &pcie->msi;
1401 unsigned int i, processed = 0;
1402
1403 for (i = 0; i < 8; i++) {
1404 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1405
1406 while (reg) {
1407 unsigned int offset = find_first_bit(&reg, 32);
1408 unsigned int index = i * 32 + offset;
1409 unsigned int irq;
1410
1411 /* clear the interrupt */
1412 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1413
1414 irq = irq_find_mapping(msi->domain, index);
1415 if (irq) {
1416 if (test_bit(index, msi->used))
1417 generic_handle_irq(irq);
1418 else
1419 dev_info(pcie->dev, "unhandled MSI\n");
1420 } else {
1421 /*
1422 * that's weird who triggered this?
1423 * just clear it
1424 */
1425 dev_info(pcie->dev, "unexpected MSI\n");
1426 }
1427
1428 /* see if there's any more pending in this vector */
1429 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1430
1431 processed++;
1432 }
1433 }
1434
1435 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1436}
1437
Yijing Wangc2791b82014-11-11 17:45:45 -07001438static int tegra_msi_setup_irq(struct msi_controller *chip,
1439 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001440{
1441 struct tegra_msi *msi = to_tegra_msi(chip);
1442 struct msi_msg msg;
1443 unsigned int irq;
1444 int hwirq;
1445
1446 hwirq = tegra_msi_alloc(msi);
1447 if (hwirq < 0)
1448 return hwirq;
1449
1450 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001451 if (!irq) {
1452 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001453 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001454 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001455
1456 irq_set_msi_desc(irq, desc);
1457
1458 msg.address_lo = virt_to_phys((void *)msi->pages);
1459 /* 32 bit address only */
1460 msg.address_hi = 0;
1461 msg.data = hwirq;
1462
Jiang Liu83a18912014-11-09 23:10:34 +08001463 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001464
1465 return 0;
1466}
1467
Yijing Wangc2791b82014-11-11 17:45:45 -07001468static void tegra_msi_teardown_irq(struct msi_controller *chip,
1469 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001470{
1471 struct tegra_msi *msi = to_tegra_msi(chip);
1472 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001473 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001474
Jisheng Zhang019fa462014-07-29 09:33:30 +08001475 irq_dispose_mapping(irq);
1476 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001477}
1478
1479static struct irq_chip tegra_msi_irq_chip = {
1480 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001481 .irq_enable = pci_msi_unmask_irq,
1482 .irq_disable = pci_msi_mask_irq,
1483 .irq_mask = pci_msi_mask_irq,
1484 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001485};
1486
1487static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1488 irq_hw_number_t hwirq)
1489{
1490 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1491 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001492
Stephen Warrenb4f17372013-05-06 14:19:19 -06001493 tegra_cpuidle_pcie_irqs_in_use();
1494
Thierry Redingd1523b52013-08-09 16:49:19 +02001495 return 0;
1496}
1497
1498static const struct irq_domain_ops msi_domain_ops = {
1499 .map = tegra_msi_map,
1500};
1501
1502static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1503{
1504 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001505 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001506 struct tegra_msi *msi = &pcie->msi;
1507 unsigned long base;
1508 int err;
1509 u32 reg;
1510
1511 mutex_init(&msi->lock);
1512
1513 msi->chip.dev = pcie->dev;
1514 msi->chip.setup_irq = tegra_msi_setup_irq;
1515 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1516
1517 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1518 &msi_domain_ops, &msi->chip);
1519 if (!msi->domain) {
1520 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1521 return -ENOMEM;
1522 }
1523
1524 err = platform_get_irq_byname(pdev, "msi");
1525 if (err < 0) {
1526 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1527 goto err;
1528 }
1529
1530 msi->irq = err;
1531
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001532 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001533 tegra_msi_irq_chip.name, pcie);
1534 if (err < 0) {
1535 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1536 goto err;
1537 }
1538
1539 /* setup AFI/FPCI range */
1540 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1541 base = virt_to_phys((void *)msi->pages);
1542
Jay Agarwal94716cd2013-08-09 16:49:24 +02001543 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001544 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1545 /* this register is in 4K increments */
1546 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1547
1548 /* enable all MSI vectors */
1549 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1550 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1551 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1552 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1553 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1554 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1555 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1556 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1557
1558 /* and unmask the MSI interrupt */
1559 reg = afi_readl(pcie, AFI_INTR_MASK);
1560 reg |= AFI_INTR_MASK_MSI_MASK;
1561 afi_writel(pcie, reg, AFI_INTR_MASK);
1562
1563 return 0;
1564
1565err:
1566 irq_domain_remove(msi->domain);
1567 return err;
1568}
1569
1570static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1571{
1572 struct tegra_msi *msi = &pcie->msi;
1573 unsigned int i, irq;
1574 u32 value;
1575
1576 /* mask the MSI interrupt */
1577 value = afi_readl(pcie, AFI_INTR_MASK);
1578 value &= ~AFI_INTR_MASK_MSI_MASK;
1579 afi_writel(pcie, value, AFI_INTR_MASK);
1580
1581 /* disable all MSI vectors */
1582 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1583 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1584 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1585 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1586 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1587 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1588 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1589 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1590
1591 free_pages(msi->pages, 0);
1592
1593 if (msi->irq > 0)
1594 free_irq(msi->irq, pcie);
1595
1596 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1597 irq = irq_find_mapping(msi->domain, i);
1598 if (irq > 0)
1599 irq_dispose_mapping(irq);
1600 }
1601
1602 irq_domain_remove(msi->domain);
1603
1604 return 0;
1605}
1606
1607static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1608 u32 *xbar)
1609{
1610 struct device_node *np = pcie->dev->of_node;
1611
Thierry Reding7f1f0542014-08-26 17:11:38 +02001612 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1613 switch (lanes) {
1614 case 0x0000104:
1615 dev_info(pcie->dev, "4x1, 1x1 configuration\n");
1616 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1617 return 0;
1618
1619 case 0x0000102:
1620 dev_info(pcie->dev, "2x1, 1x1 configuration\n");
1621 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1622 return 0;
1623 }
1624 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001625 switch (lanes) {
1626 case 0x00000204:
1627 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1628 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1629 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001630
Jay Agarwal94716cd2013-08-09 16:49:24 +02001631 case 0x00020202:
1632 dev_info(pcie->dev, "2x3 configuration\n");
1633 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1634 return 0;
1635
1636 case 0x00010104:
1637 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1638 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1639 return 0;
1640 }
1641 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1642 switch (lanes) {
1643 case 0x00000004:
1644 dev_info(pcie->dev, "single-mode configuration\n");
1645 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1646 return 0;
1647
1648 case 0x00000202:
1649 dev_info(pcie->dev, "dual-mode configuration\n");
1650 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1651 return 0;
1652 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001653 }
1654
1655 return -EINVAL;
1656}
1657
Thierry Reding077fb152014-05-28 16:49:13 +02001658/*
1659 * Check whether a given set of supplies is available in a device tree node.
1660 * This is used to check whether the new or the legacy device tree bindings
1661 * should be used.
1662 */
1663static bool of_regulator_bulk_available(struct device_node *np,
1664 struct regulator_bulk_data *supplies,
1665 unsigned int num_supplies)
1666{
1667 char property[32];
1668 unsigned int i;
1669
1670 for (i = 0; i < num_supplies; i++) {
1671 snprintf(property, 32, "%s-supply", supplies[i].supply);
1672
1673 if (of_find_property(np, property, NULL) == NULL)
1674 return false;
1675 }
1676
1677 return true;
1678}
1679
1680/*
1681 * Old versions of the device tree binding for this device used a set of power
1682 * supplies that didn't match the hardware inputs. This happened to work for a
1683 * number of cases but is not future proof. However to preserve backwards-
1684 * compatibility with old device trees, this function will try to use the old
1685 * set of supplies.
1686 */
1687static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1688{
1689 struct device_node *np = pcie->dev->of_node;
1690
1691 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1692 pcie->num_supplies = 3;
1693 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1694 pcie->num_supplies = 2;
1695
1696 if (pcie->num_supplies == 0) {
1697 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1698 np->full_name);
1699 return -ENODEV;
1700 }
1701
1702 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1703 sizeof(*pcie->supplies),
1704 GFP_KERNEL);
1705 if (!pcie->supplies)
1706 return -ENOMEM;
1707
1708 pcie->supplies[0].supply = "pex-clk";
1709 pcie->supplies[1].supply = "vdd";
1710
1711 if (pcie->num_supplies > 2)
1712 pcie->supplies[2].supply = "avdd";
1713
1714 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1715 pcie->supplies);
1716}
1717
1718/*
1719 * Obtains the list of regulators required for a particular generation of the
1720 * IP block.
1721 *
1722 * This would've been nice to do simply by providing static tables for use
1723 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1724 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1725 * and either seems to be optional depending on which ports are being used.
1726 */
1727static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1728{
1729 struct device_node *np = pcie->dev->of_node;
1730 unsigned int i = 0;
1731
Thierry Reding7f1f0542014-08-26 17:11:38 +02001732 if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1733 pcie->num_supplies = 7;
1734
1735 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1736 sizeof(*pcie->supplies),
1737 GFP_KERNEL);
1738 if (!pcie->supplies)
1739 return -ENOMEM;
1740
1741 pcie->supplies[i++].supply = "avddio-pex";
1742 pcie->supplies[i++].supply = "dvddio-pex";
1743 pcie->supplies[i++].supply = "avdd-pex-pll";
1744 pcie->supplies[i++].supply = "hvdd-pex";
1745 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1746 pcie->supplies[i++].supply = "vddio-pex-ctl";
1747 pcie->supplies[i++].supply = "avdd-pll-erefe";
1748 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001749 bool need_pexa = false, need_pexb = false;
1750
1751 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1752 if (lane_mask & 0x0f)
1753 need_pexa = true;
1754
1755 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1756 if (lane_mask & 0x30)
1757 need_pexb = true;
1758
1759 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1760 (need_pexb ? 2 : 0);
1761
1762 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1763 sizeof(*pcie->supplies),
1764 GFP_KERNEL);
1765 if (!pcie->supplies)
1766 return -ENOMEM;
1767
1768 pcie->supplies[i++].supply = "avdd-pex-pll";
1769 pcie->supplies[i++].supply = "hvdd-pex";
1770 pcie->supplies[i++].supply = "vddio-pex-ctl";
1771 pcie->supplies[i++].supply = "avdd-plle";
1772
1773 if (need_pexa) {
1774 pcie->supplies[i++].supply = "avdd-pexa";
1775 pcie->supplies[i++].supply = "vdd-pexa";
1776 }
1777
1778 if (need_pexb) {
1779 pcie->supplies[i++].supply = "avdd-pexb";
1780 pcie->supplies[i++].supply = "vdd-pexb";
1781 }
1782 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1783 pcie->num_supplies = 5;
1784
1785 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1786 sizeof(*pcie->supplies),
1787 GFP_KERNEL);
1788 if (!pcie->supplies)
1789 return -ENOMEM;
1790
1791 pcie->supplies[0].supply = "avdd-pex";
1792 pcie->supplies[1].supply = "vdd-pex";
1793 pcie->supplies[2].supply = "avdd-pex-pll";
1794 pcie->supplies[3].supply = "avdd-plle";
1795 pcie->supplies[4].supply = "vddio-pex-clk";
1796 }
1797
1798 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1799 pcie->num_supplies))
1800 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1801 pcie->supplies);
1802
1803 /*
1804 * If not all regulators are available for this new scheme, assume
1805 * that the device tree complies with an older version of the device
1806 * tree binding.
1807 */
1808 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1809
1810 devm_kfree(pcie->dev, pcie->supplies);
1811 pcie->num_supplies = 0;
1812
1813 return tegra_pcie_get_legacy_regulators(pcie);
1814}
1815
Thierry Redingd1523b52013-08-09 16:49:19 +02001816static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1817{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001818 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001819 struct device_node *np = pcie->dev->of_node, *port;
1820 struct of_pci_range_parser parser;
1821 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001822 u32 lanes = 0, mask = 0;
1823 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001824 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001825 int err;
1826
Thierry Reding41534e52014-08-01 14:15:11 +02001827 memset(&pcie->all, 0, sizeof(pcie->all));
1828 pcie->all.flags = IORESOURCE_MEM;
1829 pcie->all.name = np->full_name;
1830 pcie->all.start = ~0;
1831 pcie->all.end = 0;
1832
Thierry Redingd1523b52013-08-09 16:49:19 +02001833 if (of_pci_range_parser_init(&parser, np)) {
1834 dev_err(pcie->dev, "missing \"ranges\" property\n");
1835 return -EINVAL;
1836 }
1837
Thierry Redingd1523b52013-08-09 16:49:19 +02001838 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001839 err = of_pci_range_to_resource(&range, np, &res);
1840 if (err < 0)
1841 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001842
1843 switch (res.flags & IORESOURCE_TYPE_BITS) {
1844 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001845 /* Track the bus -> CPU I/O mapping offset. */
1846 pcie->offset.io = res.start - range.pci_addr;
1847
Thierry Reding51067872014-11-27 09:54:09 +01001848 memcpy(&pcie->pio, &res, sizeof(res));
1849 pcie->pio.name = np->full_name;
1850
1851 /*
1852 * The Tegra PCIe host bridge uses this to program the
1853 * mapping of the I/O space to the physical address,
1854 * so we override the .start and .end fields here that
1855 * of_pci_range_to_resource() converted to I/O space.
1856 * We also set the IORESOURCE_MEM type to clarify that
1857 * the resource is in the physical memory space.
1858 */
1859 pcie->io.start = range.cpu_addr;
1860 pcie->io.end = range.cpu_addr + range.size - 1;
1861 pcie->io.flags = IORESOURCE_MEM;
1862 pcie->io.name = "I/O";
1863
1864 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001865 break;
1866
1867 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001868 /*
1869 * Track the bus -> CPU memory mapping offset. This
1870 * assumes that the prefetchable and non-prefetchable
1871 * regions will be the last of type IORESOURCE_MEM in
1872 * the ranges property.
1873 * */
1874 pcie->offset.mem = res.start - range.pci_addr;
1875
Thierry Redingd1523b52013-08-09 16:49:19 +02001876 if (res.flags & IORESOURCE_PREFETCH) {
1877 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001878 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001879 } else {
1880 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001881 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001882 }
1883 break;
1884 }
Thierry Reding41534e52014-08-01 14:15:11 +02001885
1886 if (res.start <= pcie->all.start)
1887 pcie->all.start = res.start;
1888
1889 if (res.end >= pcie->all.end)
1890 pcie->all.end = res.end;
Thierry Redingd1523b52013-08-09 16:49:19 +02001891 }
1892
Thierry Reding41534e52014-08-01 14:15:11 +02001893 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1894 if (err < 0)
1895 return err;
1896
Thierry Redingd1523b52013-08-09 16:49:19 +02001897 err = of_pci_parse_bus_range(np, &pcie->busn);
1898 if (err < 0) {
1899 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1900 err);
1901 pcie->busn.name = np->name;
1902 pcie->busn.start = 0;
1903 pcie->busn.end = 0xff;
1904 pcie->busn.flags = IORESOURCE_BUS;
1905 }
1906
1907 /* parse root ports */
1908 for_each_child_of_node(np, port) {
1909 struct tegra_pcie_port *rp;
1910 unsigned int index;
1911 u32 value;
1912
1913 err = of_pci_get_devfn(port);
1914 if (err < 0) {
1915 dev_err(pcie->dev, "failed to parse address: %d\n",
1916 err);
1917 return err;
1918 }
1919
1920 index = PCI_SLOT(err);
1921
Jay Agarwal94716cd2013-08-09 16:49:24 +02001922 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001923 dev_err(pcie->dev, "invalid port number: %d\n", index);
1924 return -EINVAL;
1925 }
1926
1927 index--;
1928
1929 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1930 if (err < 0) {
1931 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1932 err);
1933 return err;
1934 }
1935
1936 if (value > 16) {
1937 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1938 return -EINVAL;
1939 }
1940
1941 lanes |= value << (index << 3);
1942
Thierry Reding077fb152014-05-28 16:49:13 +02001943 if (!of_device_is_available(port)) {
1944 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001945 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001946 }
1947
1948 mask |= ((1 << value) - 1) << lane;
1949 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001950
1951 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1952 if (!rp)
1953 return -ENOMEM;
1954
1955 err = of_address_to_resource(port, 0, &rp->regs);
1956 if (err < 0) {
1957 dev_err(pcie->dev, "failed to parse address: %d\n",
1958 err);
1959 return err;
1960 }
1961
1962 INIT_LIST_HEAD(&rp->list);
1963 rp->index = index;
1964 rp->lanes = value;
1965 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001966 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001967
Julia Lawalldc05ee32013-08-26 11:11:09 +02001968 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1969 if (IS_ERR(rp->base))
1970 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001971
1972 list_add_tail(&rp->list, &pcie->ports);
1973 }
1974
1975 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1976 if (err < 0) {
1977 dev_err(pcie->dev, "invalid lane configuration\n");
1978 return err;
1979 }
1980
Thierry Reding077fb152014-05-28 16:49:13 +02001981 err = tegra_pcie_get_regulators(pcie, mask);
1982 if (err < 0)
1983 return err;
1984
Thierry Redingd1523b52013-08-09 16:49:19 +02001985 return 0;
1986}
1987
1988/*
1989 * FIXME: If there are no PCIe cards attached, then calling this function
1990 * can result in the increase of the bootup time as there are big timeout
1991 * loops.
1992 */
1993#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1994static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1995{
1996 unsigned int retries = 3;
1997 unsigned long value;
1998
Thierry Reding7f1f0542014-08-26 17:11:38 +02001999 /* override presence detection */
2000 value = readl(port->base + RP_PRIV_MISC);
2001 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2002 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2003 writel(value, port->base + RP_PRIV_MISC);
2004
Thierry Redingd1523b52013-08-09 16:49:19 +02002005 do {
2006 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2007
2008 do {
2009 value = readl(port->base + RP_VEND_XP);
2010
2011 if (value & RP_VEND_XP_DL_UP)
2012 break;
2013
2014 usleep_range(1000, 2000);
2015 } while (--timeout);
2016
2017 if (!timeout) {
2018 dev_err(port->pcie->dev, "link %u down, retrying\n",
2019 port->index);
2020 goto retry;
2021 }
2022
2023 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2024
2025 do {
2026 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2027
2028 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2029 return true;
2030
2031 usleep_range(1000, 2000);
2032 } while (--timeout);
2033
2034retry:
2035 tegra_pcie_port_reset(port);
2036 } while (--retries);
2037
2038 return false;
2039}
2040
2041static int tegra_pcie_enable(struct tegra_pcie *pcie)
2042{
2043 struct tegra_pcie_port *port, *tmp;
2044 struct hw_pci hw;
2045
2046 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2047 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
2048 port->index, port->lanes);
2049
2050 tegra_pcie_port_enable(port);
2051
2052 if (tegra_pcie_port_check_link(port))
2053 continue;
2054
2055 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
2056
2057 tegra_pcie_port_disable(port);
2058 tegra_pcie_port_free(port);
2059 }
2060
2061 memset(&hw, 0, sizeof(hw));
2062
Yijing Wang7ec725b2014-11-11 15:35:05 -07002063#ifdef CONFIG_PCI_MSI
2064 hw.msi_ctrl = &pcie->msi.chip;
2065#endif
2066
Thierry Redingd1523b52013-08-09 16:49:19 +02002067 hw.nr_controllers = 1;
2068 hw.private_data = (void **)&pcie;
2069 hw.setup = tegra_pcie_setup;
2070 hw.map_irq = tegra_pcie_map_irq;
Thierry Redingd1523b52013-08-09 16:49:19 +02002071 hw.ops = &tegra_pcie_ops;
2072
2073 pci_common_init_dev(pcie->dev, &hw);
2074
2075 return 0;
2076}
2077
Jay Agarwal94716cd2013-08-09 16:49:24 +02002078static const struct tegra_pcie_soc_data tegra20_pcie_data = {
2079 .num_ports = 2,
2080 .msi_base_shift = 0,
2081 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2082 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2083 .has_pex_clkreq_en = false,
2084 .has_pex_bias_ctrl = false,
2085 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002086 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002087 .has_gen2 = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002088};
2089
2090static const struct tegra_pcie_soc_data tegra30_pcie_data = {
2091 .num_ports = 3,
2092 .msi_base_shift = 8,
2093 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2094 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2095 .has_pex_clkreq_en = true,
2096 .has_pex_bias_ctrl = true,
2097 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002098 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002099 .has_gen2 = false,
2100};
2101
2102static const struct tegra_pcie_soc_data tegra124_pcie_data = {
2103 .num_ports = 2,
2104 .msi_base_shift = 8,
2105 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2106 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2107 .has_pex_clkreq_en = true,
2108 .has_pex_bias_ctrl = true,
2109 .has_intr_prsnt_sense = true,
2110 .has_cml_clk = true,
2111 .has_gen2 = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002112};
2113
2114static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002115 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie_data },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002116 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
2117 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
2118 { },
2119};
Jay Agarwal94716cd2013-08-09 16:49:24 +02002120
Thierry Reding2cb989f2014-07-22 12:30:46 -06002121static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2122{
2123 struct tegra_pcie *pcie = s->private;
2124
2125 if (list_empty(&pcie->ports))
2126 return NULL;
2127
2128 seq_printf(s, "Index Status\n");
2129
2130 return seq_list_start(&pcie->ports, *pos);
2131}
2132
2133static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2134{
2135 struct tegra_pcie *pcie = s->private;
2136
2137 return seq_list_next(v, &pcie->ports, pos);
2138}
2139
2140static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2141{
2142}
2143
2144static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2145{
2146 bool up = false, active = false;
2147 struct tegra_pcie_port *port;
2148 unsigned int value;
2149
2150 port = list_entry(v, struct tegra_pcie_port, list);
2151
2152 value = readl(port->base + RP_VEND_XP);
2153
2154 if (value & RP_VEND_XP_DL_UP)
2155 up = true;
2156
2157 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2158
2159 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2160 active = true;
2161
2162 seq_printf(s, "%2u ", port->index);
2163
2164 if (up)
2165 seq_printf(s, "up");
2166
2167 if (active) {
2168 if (up)
2169 seq_printf(s, ", ");
2170
2171 seq_printf(s, "active");
2172 }
2173
2174 seq_printf(s, "\n");
2175 return 0;
2176}
2177
2178static const struct seq_operations tegra_pcie_ports_seq_ops = {
2179 .start = tegra_pcie_ports_seq_start,
2180 .next = tegra_pcie_ports_seq_next,
2181 .stop = tegra_pcie_ports_seq_stop,
2182 .show = tegra_pcie_ports_seq_show,
2183};
2184
2185static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2186{
2187 struct tegra_pcie *pcie = inode->i_private;
2188 struct seq_file *s;
2189 int err;
2190
2191 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2192 if (err)
2193 return err;
2194
2195 s = file->private_data;
2196 s->private = pcie;
2197
2198 return 0;
2199}
2200
2201static const struct file_operations tegra_pcie_ports_ops = {
2202 .owner = THIS_MODULE,
2203 .open = tegra_pcie_ports_open,
2204 .read = seq_read,
2205 .llseek = seq_lseek,
2206 .release = seq_release,
2207};
2208
2209static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2210{
2211 struct dentry *file;
2212
2213 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2214 if (!pcie->debugfs)
2215 return -ENOMEM;
2216
2217 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2218 pcie, &tegra_pcie_ports_ops);
2219 if (!file)
2220 goto remove;
2221
2222 return 0;
2223
2224remove:
2225 debugfs_remove_recursive(pcie->debugfs);
2226 pcie->debugfs = NULL;
2227 return -ENOMEM;
2228}
2229
Thierry Redingd1523b52013-08-09 16:49:19 +02002230static int tegra_pcie_probe(struct platform_device *pdev)
2231{
Jay Agarwal94716cd2013-08-09 16:49:24 +02002232 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02002233 struct tegra_pcie *pcie;
2234 int err;
2235
Jay Agarwal94716cd2013-08-09 16:49:24 +02002236 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
2237 if (!match)
2238 return -ENODEV;
2239
Thierry Redingd1523b52013-08-09 16:49:19 +02002240 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
2241 if (!pcie)
2242 return -ENOMEM;
2243
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002244 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002245 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02002246 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02002247 pcie->dev = &pdev->dev;
2248
2249 err = tegra_pcie_parse_dt(pcie);
2250 if (err < 0)
2251 return err;
2252
2253 pcibios_min_mem = 0;
2254
2255 err = tegra_pcie_get_resources(pcie);
2256 if (err < 0) {
2257 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
2258 return err;
2259 }
2260
2261 err = tegra_pcie_enable_controller(pcie);
2262 if (err)
2263 goto put_resources;
2264
2265 /* setup the AFI address translations */
2266 tegra_pcie_setup_translations(pcie);
2267
2268 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2269 err = tegra_pcie_enable_msi(pcie);
2270 if (err < 0) {
2271 dev_err(&pdev->dev,
2272 "failed to enable MSI support: %d\n",
2273 err);
2274 goto put_resources;
2275 }
2276 }
2277
2278 err = tegra_pcie_enable(pcie);
2279 if (err < 0) {
2280 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
2281 goto disable_msi;
2282 }
2283
Thierry Reding2cb989f2014-07-22 12:30:46 -06002284 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2285 err = tegra_pcie_debugfs_init(pcie);
2286 if (err < 0)
2287 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
2288 err);
2289 }
2290
Thierry Redingd1523b52013-08-09 16:49:19 +02002291 platform_set_drvdata(pdev, pcie);
2292 return 0;
2293
2294disable_msi:
2295 if (IS_ENABLED(CONFIG_PCI_MSI))
2296 tegra_pcie_disable_msi(pcie);
2297put_resources:
2298 tegra_pcie_put_resources(pcie);
2299 return err;
2300}
2301
Thierry Redingd1523b52013-08-09 16:49:19 +02002302static struct platform_driver tegra_pcie_driver = {
2303 .driver = {
2304 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002305 .of_match_table = tegra_pcie_of_match,
2306 .suppress_bind_attrs = true,
2307 },
2308 .probe = tegra_pcie_probe,
2309};
Paul Gortmakerad183272016-07-02 19:13:31 -04002310builtin_platform_driver(tegra_pcie_driver);