blob: 9c40da54f88a10aab4dde9ba705b187912101909 [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#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020055
56/* register definitions */
57
58#define AFI_AXI_BAR0_SZ 0x00
59#define AFI_AXI_BAR1_SZ 0x04
60#define AFI_AXI_BAR2_SZ 0x08
61#define AFI_AXI_BAR3_SZ 0x0c
62#define AFI_AXI_BAR4_SZ 0x10
63#define AFI_AXI_BAR5_SZ 0x14
64
65#define AFI_AXI_BAR0_START 0x18
66#define AFI_AXI_BAR1_START 0x1c
67#define AFI_AXI_BAR2_START 0x20
68#define AFI_AXI_BAR3_START 0x24
69#define AFI_AXI_BAR4_START 0x28
70#define AFI_AXI_BAR5_START 0x2c
71
72#define AFI_FPCI_BAR0 0x30
73#define AFI_FPCI_BAR1 0x34
74#define AFI_FPCI_BAR2 0x38
75#define AFI_FPCI_BAR3 0x3c
76#define AFI_FPCI_BAR4 0x40
77#define AFI_FPCI_BAR5 0x44
78
79#define AFI_CACHE_BAR0_SZ 0x48
80#define AFI_CACHE_BAR0_ST 0x4c
81#define AFI_CACHE_BAR1_SZ 0x50
82#define AFI_CACHE_BAR1_ST 0x54
83
84#define AFI_MSI_BAR_SZ 0x60
85#define AFI_MSI_FPCI_BAR_ST 0x64
86#define AFI_MSI_AXI_BAR_ST 0x68
87
88#define AFI_MSI_VEC0 0x6c
89#define AFI_MSI_VEC1 0x70
90#define AFI_MSI_VEC2 0x74
91#define AFI_MSI_VEC3 0x78
92#define AFI_MSI_VEC4 0x7c
93#define AFI_MSI_VEC5 0x80
94#define AFI_MSI_VEC6 0x84
95#define AFI_MSI_VEC7 0x88
96
97#define AFI_MSI_EN_VEC0 0x8c
98#define AFI_MSI_EN_VEC1 0x90
99#define AFI_MSI_EN_VEC2 0x94
100#define AFI_MSI_EN_VEC3 0x98
101#define AFI_MSI_EN_VEC4 0x9c
102#define AFI_MSI_EN_VEC5 0xa0
103#define AFI_MSI_EN_VEC6 0xa4
104#define AFI_MSI_EN_VEC7 0xa8
105
106#define AFI_CONFIGURATION 0xac
107#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
108
109#define AFI_FPCI_ERROR_MASKS 0xb0
110
111#define AFI_INTR_MASK 0xb4
112#define AFI_INTR_MASK_INT_MASK (1 << 0)
113#define AFI_INTR_MASK_MSI_MASK (1 << 8)
114
115#define AFI_INTR_CODE 0xb8
116#define AFI_INTR_CODE_MASK 0xf
Thierry Reding7f1f0542014-08-26 17:11:38 +0200117#define AFI_INTR_INI_SLAVE_ERROR 1
118#define AFI_INTR_INI_DECODE_ERROR 2
Thierry Redingd1523b52013-08-09 16:49:19 +0200119#define AFI_INTR_TARGET_ABORT 3
120#define AFI_INTR_MASTER_ABORT 4
121#define AFI_INTR_INVALID_WRITE 5
122#define AFI_INTR_LEGACY 6
123#define AFI_INTR_FPCI_DECODE_ERROR 7
Thierry Reding7f1f0542014-08-26 17:11:38 +0200124#define AFI_INTR_AXI_DECODE_ERROR 8
125#define AFI_INTR_FPCI_TIMEOUT 9
126#define AFI_INTR_PE_PRSNT_SENSE 10
127#define AFI_INTR_PE_CLKREQ_SENSE 11
128#define AFI_INTR_CLKCLAMP_SENSE 12
129#define AFI_INTR_RDY4PD_SENSE 13
130#define AFI_INTR_P2P_ERROR 14
Thierry Redingd1523b52013-08-09 16:49:19 +0200131
132#define AFI_INTR_SIGNATURE 0xbc
133#define AFI_UPPER_FPCI_ADDRESS 0xc0
134#define AFI_SM_INTR_ENABLE 0xc4
135#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
136#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
137#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
138#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
139#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
140#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
141#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
142#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
143
144#define AFI_AFI_INTR_ENABLE 0xc8
145#define AFI_INTR_EN_INI_SLVERR (1 << 0)
146#define AFI_INTR_EN_INI_DECERR (1 << 1)
147#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
148#define AFI_INTR_EN_TGT_DECERR (1 << 3)
149#define AFI_INTR_EN_TGT_WRERR (1 << 4)
150#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
151#define AFI_INTR_EN_AXI_DECERR (1 << 6)
152#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200153#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200154
155#define AFI_PCIE_CONFIG 0x0f8
156#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
157#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
158#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
159#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200160#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200161#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200162#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200163#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200164#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200165#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200166
167#define AFI_FUSE 0x104
168#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
169
170#define AFI_PEX0_CTRL 0x110
171#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200172#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200173#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200174#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200175#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200176#define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
177
178#define AFI_PLLE_CONTROL 0x160
179#define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
180#define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200181
Jay Agarwal94716cd2013-08-09 16:49:24 +0200182#define AFI_PEXBIAS_CTRL_0 0x168
183
Thierry Redingacb341e2016-07-25 16:02:05 -0500184#define RP_VEND_XP 0x00000f00
Thierry Redingd1523b52013-08-09 16:49:19 +0200185#define RP_VEND_XP_DL_UP (1 << 30)
186
Thierry Reding76245ca2016-11-25 11:57:14 +0100187#define RP_VEND_CTL2 0x00000fa8
188#define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
189
Thierry Redingacb341e2016-07-25 16:02:05 -0500190#define RP_PRIV_MISC 0x00000fe0
191#define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
192#define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
Thierry Reding7f1f0542014-08-26 17:11:38 +0200193
Thierry Redingd1523b52013-08-09 16:49:19 +0200194#define RP_LINK_CONTROL_STATUS 0x00000090
195#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
196#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
197
Thierry Redingacb341e2016-07-25 16:02:05 -0500198#define PADS_CTL_SEL 0x0000009c
Thierry Redingd1523b52013-08-09 16:49:19 +0200199
Thierry Redingacb341e2016-07-25 16:02:05 -0500200#define PADS_CTL 0x000000a0
Thierry Redingd1523b52013-08-09 16:49:19 +0200201#define PADS_CTL_IDDQ_1L (1 << 0)
202#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
203#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
204
Thierry Redingacb341e2016-07-25 16:02:05 -0500205#define PADS_PLL_CTL_TEGRA20 0x000000b8
206#define PADS_PLL_CTL_TEGRA30 0x000000b4
Thierry Redingd1523b52013-08-09 16:49:19 +0200207#define PADS_PLL_CTL_RST_B4SM (1 << 1)
208#define PADS_PLL_CTL_LOCKDET (1 << 8)
209#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
210#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
211#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
212#define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
213#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
214#define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
215#define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200216#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
217
Thierry Redingacb341e2016-07-25 16:02:05 -0500218#define PADS_REFCLK_CFG0 0x000000c8
219#define PADS_REFCLK_CFG1 0x000000cc
220#define PADS_REFCLK_BIAS 0x000000d0
Thierry Redingd1523b52013-08-09 16:49:19 +0200221
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200222/*
223 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
224 * entries, one entry per PCIe port. These field definitions and desired
225 * values aren't in the TRM, but do come from NVIDIA.
226 */
227#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
228#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
229#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
230#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
231
Thierry Redingd1523b52013-08-09 16:49:19 +0200232struct tegra_msi {
Yijing Wangc2791b82014-11-11 17:45:45 -0700233 struct msi_controller chip;
Thierry Redingd1523b52013-08-09 16:49:19 +0200234 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
235 struct irq_domain *domain;
Thierry Redingd1523b52013-08-09 16:49:19 +0200236 struct mutex lock;
Thierry Redingc0165552017-05-04 22:10:31 +0200237 u64 phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200238 int irq;
239};
240
Jay Agarwal94716cd2013-08-09 16:49:24 +0200241/* used to differentiate between Tegra SoC generations */
Thierry Redinga7fbae22016-08-15 17:31:31 +0200242struct tegra_pcie_soc {
Jay Agarwal94716cd2013-08-09 16:49:24 +0200243 unsigned int num_ports;
244 unsigned int msi_base_shift;
245 u32 pads_pll_ctl;
246 u32 tx_ref_sel;
Stephen Warrenf8144302016-07-25 16:02:27 -0500247 u32 pads_refclk_cfg0;
248 u32 pads_refclk_cfg1;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200249 bool has_pex_clkreq_en;
250 bool has_pex_bias_ctrl;
251 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200252 bool has_cml_clk;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200253 bool has_gen2;
Thierry Reding76245ca2016-11-25 11:57:14 +0100254 bool force_pca_enable;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200255};
256
Yijing Wangc2791b82014-11-11 17:45:45 -0700257static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
Thierry Redingd1523b52013-08-09 16:49:19 +0200258{
259 return container_of(chip, struct tegra_msi, chip);
260}
261
262struct tegra_pcie {
263 struct device *dev;
264
265 void __iomem *pads;
266 void __iomem *afi;
267 int irq;
268
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700269 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200270 struct resource *cs;
271
272 struct resource io;
Thierry Reding51067872014-11-27 09:54:09 +0100273 struct resource pio;
Thierry Redingd1523b52013-08-09 16:49:19 +0200274 struct resource mem;
275 struct resource prefetch;
276 struct resource busn;
277
Thierry Reding56e75e22016-02-09 15:52:32 +0100278 struct {
279 resource_size_t mem;
280 resource_size_t io;
281 } offset;
282
Thierry Redingd1523b52013-08-09 16:49:19 +0200283 struct clk *pex_clk;
284 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200285 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200286 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200287
Stephen Warren3127a6b2013-11-06 15:56:58 -0700288 struct reset_control *pex_rst;
289 struct reset_control *afi_rst;
290 struct reset_control *pcie_xrst;
291
Thierry Reding6fe7c182015-11-11 18:25:59 +0100292 bool legacy_phy;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200293 struct phy *phy;
294
Thierry Redingd1523b52013-08-09 16:49:19 +0200295 struct tegra_msi msi;
296
297 struct list_head ports;
Thierry Redingd1523b52013-08-09 16:49:19 +0200298 u32 xbar_config;
299
Thierry Reding077fb152014-05-28 16:49:13 +0200300 struct regulator_bulk_data *supplies;
301 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200302
Thierry Redinga7fbae22016-08-15 17:31:31 +0200303 const struct tegra_pcie_soc *soc;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600304 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200305};
306
307struct tegra_pcie_port {
308 struct tegra_pcie *pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100309 struct device_node *np;
Thierry Redingd1523b52013-08-09 16:49:19 +0200310 struct list_head list;
311 struct resource regs;
312 void __iomem *base;
313 unsigned int index;
314 unsigned int lanes;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100315
316 struct phy **phys;
Thierry Redingd1523b52013-08-09 16:49:19 +0200317};
318
319struct tegra_pcie_bus {
320 struct vm_struct *area;
321 struct list_head list;
322 unsigned int nr;
323};
324
Thierry Redingd1523b52013-08-09 16:49:19 +0200325static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
326 unsigned long offset)
327{
328 writel(value, pcie->afi + offset);
329}
330
331static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
332{
333 return readl(pcie->afi + offset);
334}
335
336static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
337 unsigned long offset)
338{
339 writel(value, pcie->pads + offset);
340}
341
342static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
343{
344 return readl(pcie->pads + offset);
345}
346
347/*
348 * The configuration space mapping on Tegra is somewhat similar to the ECAM
349 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
350 * register accesses are mapped:
351 *
352 * [27:24] extended register number
353 * [23:16] bus number
354 * [15:11] device number
355 * [10: 8] function number
356 * [ 7: 0] register number
357 *
358 * Mapping the whole extended configuration space would require 256 MiB of
359 * virtual address space, only a small part of which will actually be used.
360 * To work around this, a 1 MiB of virtual addresses are allocated per bus
361 * when the bus is first accessed. When the physical range is mapped, the
362 * the bus number bits are hidden so that the extended register number bits
363 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
364 *
365 * [19:16] extended register number
366 * [15:11] device number
367 * [10: 8] function number
368 * [ 7: 0] register number
369 *
370 * This is achieved by stitching together 16 chunks of 64 KiB of physical
371 * address space via the MMU.
372 */
373static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
374{
375 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
376 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
377}
378
379static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
380 unsigned int busnr)
381{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500382 struct device *dev = pcie->dev;
Lorenzo Pieralisi3e02dc42017-04-19 17:49:06 +0100383 pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
Thierry Redingd1523b52013-08-09 16:49:19 +0200384 phys_addr_t cs = pcie->cs->start;
385 struct tegra_pcie_bus *bus;
386 unsigned int i;
387 int err;
388
389 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
390 if (!bus)
391 return ERR_PTR(-ENOMEM);
392
393 INIT_LIST_HEAD(&bus->list);
394 bus->nr = busnr;
395
396 /* allocate 1 MiB of virtual addresses */
397 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
398 if (!bus->area) {
399 err = -ENOMEM;
400 goto free;
401 }
402
403 /* map each of the 16 chunks of 64 KiB each */
404 for (i = 0; i < 16; i++) {
405 unsigned long virt = (unsigned long)bus->area->addr +
406 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200407 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200408
409 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
410 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500411 dev_err(dev, "ioremap_page_range() failed: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +0200412 goto unmap;
413 }
414 }
415
416 return bus;
417
418unmap:
419 vunmap(bus->area->addr);
420free:
421 kfree(bus);
422 return ERR_PTR(err);
423}
424
Thierry Redingb4d18d72016-02-09 15:30:48 +0100425static int tegra_pcie_add_bus(struct pci_bus *bus)
Thierry Redingd1523b52013-08-09 16:49:19 +0200426{
Arnd Bergmann76f25412016-11-25 11:57:12 +0100427 struct pci_host_bridge *host = pci_find_host_bridge(bus);
428 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
Thierry Redingb4d18d72016-02-09 15:30:48 +0100429 struct tegra_pcie_bus *b;
Thierry Redingd1523b52013-08-09 16:49:19 +0200430
Thierry Redingb4d18d72016-02-09 15:30:48 +0100431 b = tegra_pcie_bus_alloc(pcie, bus->number);
432 if (IS_ERR(b))
433 return PTR_ERR(b);
Thierry Redingd1523b52013-08-09 16:49:19 +0200434
Thierry Redingb4d18d72016-02-09 15:30:48 +0100435 list_add_tail(&b->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200436
Thierry Redingb4d18d72016-02-09 15:30:48 +0100437 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200438}
439
Thierry Redingb4d18d72016-02-09 15:30:48 +0100440static void tegra_pcie_remove_bus(struct pci_bus *child)
441{
Arnd Bergmann76f25412016-11-25 11:57:12 +0100442 struct pci_host_bridge *host = pci_find_host_bridge(child);
443 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
Thierry Redingb4d18d72016-02-09 15:30:48 +0100444 struct tegra_pcie_bus *bus, *tmp;
445
446 list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
447 if (bus->nr == child->number) {
448 vunmap(bus->area->addr);
449 list_del(&bus->list);
450 kfree(bus);
451 break;
452 }
453 }
454}
455
456static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
457 unsigned int devfn,
458 int where)
Thierry Redingd1523b52013-08-09 16:49:19 +0200459{
Arnd Bergmann76f25412016-11-25 11:57:12 +0100460 struct pci_host_bridge *host = pci_find_host_bridge(bus);
461 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500462 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +0200463 void __iomem *addr = NULL;
464
465 if (bus->number == 0) {
466 unsigned int slot = PCI_SLOT(devfn);
467 struct tegra_pcie_port *port;
468
469 list_for_each_entry(port, &pcie->ports, list) {
470 if (port->index + 1 == slot) {
471 addr = port->base + (where & ~3);
472 break;
473 }
474 }
475 } else {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100476 struct tegra_pcie_bus *b;
477
478 list_for_each_entry(b, &pcie->buses, list)
479 if (b->nr == bus->number)
480 addr = (void __iomem *)b->area->addr;
481
Thierry Redingd1523b52013-08-09 16:49:19 +0200482 if (!addr) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500483 dev_err(dev, "failed to map cfg. space for bus %u\n",
Thierry Redingd1523b52013-08-09 16:49:19 +0200484 bus->number);
485 return NULL;
486 }
487
488 addr += tegra_pcie_conf_offset(devfn, where);
489 }
490
491 return addr;
492}
493
Thierry Redingd1523b52013-08-09 16:49:19 +0200494static struct pci_ops tegra_pcie_ops = {
Thierry Redingb4d18d72016-02-09 15:30:48 +0100495 .add_bus = tegra_pcie_add_bus,
496 .remove_bus = tegra_pcie_remove_bus,
497 .map_bus = tegra_pcie_map_bus,
Rob Herring0e7ac8d2015-01-09 20:34:48 -0600498 .read = pci_generic_config_read32,
499 .write = pci_generic_config_write32,
Thierry Redingd1523b52013-08-09 16:49:19 +0200500};
501
502static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
503{
504 unsigned long ret = 0;
505
506 switch (port->index) {
507 case 0:
508 ret = AFI_PEX0_CTRL;
509 break;
510
511 case 1:
512 ret = AFI_PEX1_CTRL;
513 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200514
515 case 2:
516 ret = AFI_PEX2_CTRL;
517 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200518 }
519
520 return ret;
521}
522
523static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
524{
525 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
526 unsigned long value;
527
528 /* pulse reset signal */
529 value = afi_readl(port->pcie, ctrl);
530 value &= ~AFI_PEX_CTRL_RST;
531 afi_writel(port->pcie, value, ctrl);
532
533 usleep_range(1000, 2000);
534
535 value = afi_readl(port->pcie, ctrl);
536 value |= AFI_PEX_CTRL_RST;
537 afi_writel(port->pcie, value, ctrl);
538}
539
540static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
541{
542 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
Thierry Redinga7fbae22016-08-15 17:31:31 +0200543 const struct tegra_pcie_soc *soc = port->pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +0200544 unsigned long value;
545
546 /* enable reference clock */
547 value = afi_readl(port->pcie, ctrl);
548 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200549
550 if (soc->has_pex_clkreq_en)
551 value |= AFI_PEX_CTRL_CLKREQ_EN;
552
Thierry Reding7f1f0542014-08-26 17:11:38 +0200553 value |= AFI_PEX_CTRL_OVERRIDE_EN;
554
Thierry Redingd1523b52013-08-09 16:49:19 +0200555 afi_writel(port->pcie, value, ctrl);
556
557 tegra_pcie_port_reset(port);
Thierry Reding76245ca2016-11-25 11:57:14 +0100558
559 if (soc->force_pca_enable) {
560 value = readl(port->base + RP_VEND_CTL2);
561 value |= RP_VEND_CTL2_PCA_ENABLE;
562 writel(value, port->base + RP_VEND_CTL2);
563 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200564}
565
566static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
567{
568 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
Thierry Redinga7fbae22016-08-15 17:31:31 +0200569 const struct tegra_pcie_soc *soc = port->pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +0200570 unsigned long value;
571
572 /* assert port reset */
573 value = afi_readl(port->pcie, ctrl);
574 value &= ~AFI_PEX_CTRL_RST;
575 afi_writel(port->pcie, value, ctrl);
576
577 /* disable reference clock */
578 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200579
580 if (soc->has_pex_clkreq_en)
581 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
582
Thierry Redingd1523b52013-08-09 16:49:19 +0200583 value &= ~AFI_PEX_CTRL_REFCLK_EN;
584 afi_writel(port->pcie, value, ctrl);
585}
586
587static void tegra_pcie_port_free(struct tegra_pcie_port *port)
588{
589 struct tegra_pcie *pcie = port->pcie;
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500590 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +0200591
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500592 devm_iounmap(dev, port->base);
593 devm_release_mem_region(dev, port->regs.start,
Thierry Redingd1523b52013-08-09 16:49:19 +0200594 resource_size(&port->regs));
595 list_del(&port->list);
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500596 devm_kfree(dev, port);
Thierry Redingd1523b52013-08-09 16:49:19 +0200597}
598
Thierry Redingd1523b52013-08-09 16:49:19 +0200599/* Tegra PCIE root complex wrongly reports device class */
600static void tegra_pcie_fixup_class(struct pci_dev *dev)
601{
602 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
603}
604DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
605DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200606DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
607DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200608
609/* Tegra PCIE requires relaxed ordering */
610static void tegra_pcie_relax_enable(struct pci_dev *dev)
611{
612 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
613}
614DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
615
Arnd Bergmann76f25412016-11-25 11:57:12 +0100616static int tegra_pcie_request_resources(struct tegra_pcie *pcie)
Thierry Redingd1523b52013-08-09 16:49:19 +0200617{
Arnd Bergmann76f25412016-11-25 11:57:12 +0100618 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
619 struct list_head *windows = &host->windows;
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500620 struct device *dev = pcie->dev;
Thierry Reding41534e52014-08-01 14:15:11 +0200621 int err;
622
Arnd Bergmann76f25412016-11-25 11:57:12 +0100623 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
624 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
625 pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem);
626 pci_add_resource(windows, &pcie->busn);
Thierry Reding56e75e22016-02-09 15:52:32 +0100627
Arnd Bergmann76f25412016-11-25 11:57:12 +0100628 err = devm_request_pci_bus_resources(dev, windows);
Thierry Reding56e75e22016-02-09 15:52:32 +0100629 if (err < 0)
630 return err;
631
Arnd Bergmann76f25412016-11-25 11:57:12 +0100632 pci_remap_iospace(&pcie->pio, pcie->io.start);
Lorenzo Pieralisi13f392e2016-08-15 17:50:46 +0100633
Arnd Bergmann76f25412016-11-25 11:57:12 +0100634 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +0200635}
636
637static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
638{
Arnd Bergmann76f25412016-11-25 11:57:12 +0100639 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
640 struct tegra_pcie *pcie = pci_host_bridge_priv(host);
Lucas Stachf5d33522014-04-16 10:24:32 -0600641 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200642
Stephen Warrenb4f17372013-05-06 14:19:19 -0600643 tegra_cpuidle_pcie_irqs_in_use();
644
Lucas Stachf5d33522014-04-16 10:24:32 -0600645 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
646 if (!irq)
647 irq = pcie->irq;
648
649 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200650}
651
Thierry Redingd1523b52013-08-09 16:49:19 +0200652static irqreturn_t tegra_pcie_isr(int irq, void *arg)
653{
654 const char *err_msg[] = {
655 "Unknown",
656 "AXI slave error",
657 "AXI decode error",
658 "Target abort",
659 "Master abort",
660 "Invalid write",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200661 "Legacy interrupt",
Thierry Redingd1523b52013-08-09 16:49:19 +0200662 "Response decoding error",
663 "AXI response decoding error",
664 "Transaction timeout",
Thierry Reding7f1f0542014-08-26 17:11:38 +0200665 "Slot present pin change",
666 "Slot clock request change",
667 "TMS clock ramp change",
668 "TMS ready for power down",
669 "Peer2Peer error",
Thierry Redingd1523b52013-08-09 16:49:19 +0200670 };
671 struct tegra_pcie *pcie = arg;
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500672 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +0200673 u32 code, signature;
674
675 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
676 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
677 afi_writel(pcie, 0, AFI_INTR_CODE);
678
679 if (code == AFI_INTR_LEGACY)
680 return IRQ_NONE;
681
682 if (code >= ARRAY_SIZE(err_msg))
683 code = 0;
684
685 /*
686 * do not pollute kernel log with master abort reports since they
687 * happen a lot during enumeration
688 */
689 if (code == AFI_INTR_MASTER_ABORT)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500690 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
Thierry Redingd1523b52013-08-09 16:49:19 +0200691 else
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500692 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
Thierry Redingd1523b52013-08-09 16:49:19 +0200693
694 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
695 code == AFI_INTR_FPCI_DECODE_ERROR) {
696 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
697 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
698
699 if (code == AFI_INTR_MASTER_ABORT)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500700 dev_dbg(dev, " FPCI address: %10llx\n", address);
Thierry Redingd1523b52013-08-09 16:49:19 +0200701 else
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500702 dev_err(dev, " FPCI address: %10llx\n", address);
Thierry Redingd1523b52013-08-09 16:49:19 +0200703 }
704
705 return IRQ_HANDLED;
706}
707
708/*
709 * FPCI map is as follows:
710 * - 0xfdfc000000: I/O space
711 * - 0xfdfe000000: type 0 configuration space
712 * - 0xfdff000000: type 1 configuration space
713 * - 0xfe00000000: type 0 extended configuration space
714 * - 0xfe10000000: type 1 extended configuration space
715 */
716static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
717{
718 u32 fpci_bar, size, axi_address;
719
720 /* Bar 0: type 1 extended configuration space */
721 fpci_bar = 0xfe100000;
722 size = resource_size(pcie->cs);
723 axi_address = pcie->cs->start;
724 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
725 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
726 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
727
728 /* Bar 1: downstream IO bar */
729 fpci_bar = 0xfdfc0000;
730 size = resource_size(&pcie->io);
Thierry Reding51067872014-11-27 09:54:09 +0100731 axi_address = pcie->io.start;
Thierry Redingd1523b52013-08-09 16:49:19 +0200732 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
733 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
734 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
735
736 /* Bar 2: prefetchable memory BAR */
737 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
738 size = resource_size(&pcie->prefetch);
739 axi_address = pcie->prefetch.start;
740 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
741 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
742 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
743
744 /* Bar 3: non prefetchable memory BAR */
745 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
746 size = resource_size(&pcie->mem);
747 axi_address = pcie->mem.start;
748 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
749 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
750 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
751
752 /* NULL out the remaining BARs as they are not used */
753 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
754 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
755 afi_writel(pcie, 0, AFI_FPCI_BAR4);
756
757 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
758 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
759 afi_writel(pcie, 0, AFI_FPCI_BAR5);
760
761 /* map all upstream transactions as uncached */
Thierry Redinge32faa32016-02-09 15:52:33 +0100762 afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +0200763 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
764 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
765 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
766
767 /* MSI translations are setup only when needed */
768 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
769 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
770 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
771 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
772}
773
Thierry Reding7f1f0542014-08-26 17:11:38 +0200774static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
Thierry Redingd1523b52013-08-09 16:49:19 +0200775{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200776 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200777 u32 value;
Thierry Redingd1523b52013-08-09 16:49:19 +0200778
Thierry Reding7f1f0542014-08-26 17:11:38 +0200779 timeout = jiffies + msecs_to_jiffies(timeout);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200780
Thierry Reding7f1f0542014-08-26 17:11:38 +0200781 while (time_before(jiffies, timeout)) {
782 value = pads_readl(pcie, soc->pads_pll_ctl);
783 if (value & PADS_PLL_CTL_LOCKDET)
784 return 0;
785 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200786
Thierry Reding7f1f0542014-08-26 17:11:38 +0200787 return -ETIMEDOUT;
788}
Thierry Redingd1523b52013-08-09 16:49:19 +0200789
Thierry Reding7f1f0542014-08-26 17:11:38 +0200790static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
791{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500792 struct device *dev = pcie->dev;
Thierry Redinga7fbae22016-08-15 17:31:31 +0200793 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200794 u32 value;
795 int err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200796
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700797 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200798 pads_writel(pcie, 0x0, PADS_CTL_SEL);
799
800 /* override IDDQ to 1 on all 4 lanes */
801 value = pads_readl(pcie, PADS_CTL);
802 value |= PADS_CTL_IDDQ_1L;
803 pads_writel(pcie, value, PADS_CTL);
804
805 /*
806 * Set up PHY PLL inputs select PLLE output as refclock,
807 * set TX ref sel to div10 (not div5).
808 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200809 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200810 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200811 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
812 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200813
Eric Yuenec732762014-08-26 17:11:37 +0200814 /* reset PLL */
815 value = pads_readl(pcie, soc->pads_pll_ctl);
816 value &= ~PADS_PLL_CTL_RST_B4SM;
817 pads_writel(pcie, value, soc->pads_pll_ctl);
818
819 usleep_range(20, 100);
820
Thierry Redingd1523b52013-08-09 16:49:19 +0200821 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200822 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200823 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200824 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200825
Thierry Redingd1523b52013-08-09 16:49:19 +0200826 /* wait for the PLL to lock */
Thierry Reding7f1f0542014-08-26 17:11:38 +0200827 err = tegra_pcie_pll_wait(pcie, 500);
828 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500829 dev_err(dev, "PLL failed to lock: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +0200830 return err;
831 }
Thierry Redingd1523b52013-08-09 16:49:19 +0200832
833 /* turn off IDDQ override */
834 value = pads_readl(pcie, PADS_CTL);
835 value &= ~PADS_CTL_IDDQ_1L;
836 pads_writel(pcie, value, PADS_CTL);
837
838 /* enable TX/RX data */
839 value = pads_readl(pcie, PADS_CTL);
840 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
841 pads_writel(pcie, value, PADS_CTL);
842
Thierry Reding7f1f0542014-08-26 17:11:38 +0200843 return 0;
844}
845
Thierry Reding6fe7c182015-11-11 18:25:59 +0100846static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
847{
Thierry Redinga7fbae22016-08-15 17:31:31 +0200848 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100849 u32 value;
850
851 /* disable TX/RX data */
852 value = pads_readl(pcie, PADS_CTL);
853 value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
854 pads_writel(pcie, value, PADS_CTL);
855
856 /* override IDDQ */
857 value = pads_readl(pcie, PADS_CTL);
858 value |= PADS_CTL_IDDQ_1L;
Bjorn Helgaas8dd99bc2016-10-05 16:04:13 -0500859 pads_writel(pcie, value, PADS_CTL);
Thierry Reding6fe7c182015-11-11 18:25:59 +0100860
861 /* reset PLL */
862 value = pads_readl(pcie, soc->pads_pll_ctl);
863 value &= ~PADS_PLL_CTL_RST_B4SM;
864 pads_writel(pcie, value, soc->pads_pll_ctl);
865
866 usleep_range(20, 100);
867
868 return 0;
869}
870
871static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
872{
873 struct device *dev = port->pcie->dev;
874 unsigned int i;
875 int err;
876
877 for (i = 0; i < port->lanes; i++) {
878 err = phy_power_on(port->phys[i]);
879 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500880 dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
Thierry Reding6fe7c182015-11-11 18:25:59 +0100881 return err;
882 }
883 }
884
885 return 0;
886}
887
888static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
889{
890 struct device *dev = port->pcie->dev;
891 unsigned int i;
892 int err;
893
894 for (i = 0; i < port->lanes; i++) {
895 err = phy_power_off(port->phys[i]);
896 if (err < 0) {
897 dev_err(dev, "failed to power off PHY#%u: %d\n", i,
898 err);
899 return err;
900 }
901 }
902
903 return 0;
904}
905
906static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
907{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500908 struct device *dev = pcie->dev;
Thierry Redinga7fbae22016-08-15 17:31:31 +0200909 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100910 struct tegra_pcie_port *port;
911 int err;
912
913 if (pcie->legacy_phy) {
914 if (pcie->phy)
915 err = phy_power_on(pcie->phy);
916 else
917 err = tegra_pcie_phy_enable(pcie);
918
919 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500920 dev_err(dev, "failed to power on PHY: %d\n", err);
Thierry Reding6fe7c182015-11-11 18:25:59 +0100921
922 return err;
923 }
924
925 list_for_each_entry(port, &pcie->ports, list) {
926 err = tegra_pcie_port_phy_power_on(port);
927 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500928 dev_err(dev,
Thierry Reding6fe7c182015-11-11 18:25:59 +0100929 "failed to power on PCIe port %u PHY: %d\n",
930 port->index, err);
931 return err;
932 }
933 }
934
Stephen Warrencf5d31802016-07-25 16:02:21 -0500935 /* Configure the reference clock driver */
Stephen Warrenf8144302016-07-25 16:02:27 -0500936 pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500937
938 if (soc->num_ports > 2)
Stephen Warrenf8144302016-07-25 16:02:27 -0500939 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
Stephen Warrencf5d31802016-07-25 16:02:21 -0500940
Thierry Reding6fe7c182015-11-11 18:25:59 +0100941 return 0;
942}
943
944static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
945{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500946 struct device *dev = pcie->dev;
Thierry Reding6fe7c182015-11-11 18:25:59 +0100947 struct tegra_pcie_port *port;
948 int err;
949
950 if (pcie->legacy_phy) {
951 if (pcie->phy)
952 err = phy_power_off(pcie->phy);
953 else
954 err = tegra_pcie_phy_disable(pcie);
955
956 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500957 dev_err(dev, "failed to power off PHY: %d\n", err);
Thierry Reding6fe7c182015-11-11 18:25:59 +0100958
959 return err;
960 }
961
962 list_for_each_entry(port, &pcie->ports, list) {
963 err = tegra_pcie_port_phy_power_off(port);
964 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500965 dev_err(dev,
Thierry Reding6fe7c182015-11-11 18:25:59 +0100966 "failed to power off PCIe port %u PHY: %d\n",
967 port->index, err);
968 return err;
969 }
970 }
971
972 return 0;
973}
974
Thierry Reding7f1f0542014-08-26 17:11:38 +0200975static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
976{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -0500977 struct device *dev = pcie->dev;
Thierry Redinga7fbae22016-08-15 17:31:31 +0200978 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding7f1f0542014-08-26 17:11:38 +0200979 struct tegra_pcie_port *port;
980 unsigned long value;
981 int err;
982
983 /* enable PLL power down */
984 if (pcie->phy) {
985 value = afi_readl(pcie, AFI_PLLE_CONTROL);
986 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
987 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
988 afi_writel(pcie, value, AFI_PLLE_CONTROL);
989 }
990
991 /* power down PCIe slot clock bias pad */
992 if (soc->has_pex_bias_ctrl)
993 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
994
995 /* configure mode and disable all ports */
996 value = afi_readl(pcie, AFI_PCIE_CONFIG);
997 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
998 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
999
1000 list_for_each_entry(port, &pcie->ports, list)
1001 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1002
1003 afi_writel(pcie, value, AFI_PCIE_CONFIG);
1004
1005 if (soc->has_gen2) {
1006 value = afi_readl(pcie, AFI_FUSE);
1007 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1008 afi_writel(pcie, value, AFI_FUSE);
1009 } else {
1010 value = afi_readl(pcie, AFI_FUSE);
1011 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1012 afi_writel(pcie, value, AFI_FUSE);
1013 }
1014
Thierry Reding6fe7c182015-11-11 18:25:59 +01001015 err = tegra_pcie_phy_power_on(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001016 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001017 dev_err(dev, "failed to power on PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001018 return err;
1019 }
1020
Thierry Redingd1523b52013-08-09 16:49:19 +02001021 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -07001022 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001023
1024 /* finally enable PCIe */
1025 value = afi_readl(pcie, AFI_CONFIGURATION);
1026 value |= AFI_CONFIGURATION_EN_FPCI;
1027 afi_writel(pcie, value, AFI_CONFIGURATION);
1028
1029 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1030 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1031 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001032
1033 if (soc->has_intr_prsnt_sense)
1034 value |= AFI_INTR_EN_PRSNT_SENSE;
1035
Thierry Redingd1523b52013-08-09 16:49:19 +02001036 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1037 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1038
1039 /* don't enable MSI for now, only when needed */
1040 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1041
1042 /* disable all exceptions */
1043 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1044
1045 return 0;
1046}
1047
1048static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1049{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001050 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02001051 int err;
1052
1053 /* TODO: disable and unprepare clocks? */
1054
Thierry Reding6fe7c182015-11-11 18:25:59 +01001055 err = tegra_pcie_phy_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001056 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001057 dev_err(dev, "failed to power off PHY(s): %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001058
Stephen Warren3127a6b2013-11-06 15:56:58 -07001059 reset_control_assert(pcie->pcie_xrst);
1060 reset_control_assert(pcie->afi_rst);
1061 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001062
1063 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1064
Thierry Reding077fb152014-05-28 16:49:13 +02001065 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +02001066 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001067 dev_warn(dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001068}
1069
1070static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1071{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001072 struct device *dev = pcie->dev;
Thierry Redinga7fbae22016-08-15 17:31:31 +02001073 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001074 int err;
1075
Stephen Warren3127a6b2013-11-06 15:56:58 -07001076 reset_control_assert(pcie->pcie_xrst);
1077 reset_control_assert(pcie->afi_rst);
1078 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001079
1080 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1081
1082 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +02001083 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1084 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001085 dev_err(dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001086
Thierry Redingd1523b52013-08-09 16:49:19 +02001087 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -07001088 pcie->pex_clk,
1089 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001090 if (err) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001091 dev_err(dev, "powerup sequence failed: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001092 return err;
1093 }
1094
Stephen Warren3127a6b2013-11-06 15:56:58 -07001095 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +02001096
1097 err = clk_prepare_enable(pcie->afi_clk);
1098 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001099 dev_err(dev, "failed to enable AFI clock: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001100 return err;
1101 }
1102
Jay Agarwal94716cd2013-08-09 16:49:24 +02001103 if (soc->has_cml_clk) {
1104 err = clk_prepare_enable(pcie->cml_clk);
1105 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001106 dev_err(dev, "failed to enable CML clock: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001107 return err;
1108 }
1109 }
1110
Thierry Redingd1523b52013-08-09 16:49:19 +02001111 err = clk_prepare_enable(pcie->pll_e);
1112 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001113 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001114 return err;
1115 }
1116
1117 return 0;
1118}
1119
1120static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1121{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001122 struct device *dev = pcie->dev;
Thierry Redinga7fbae22016-08-15 17:31:31 +02001123 const struct tegra_pcie_soc *soc = pcie->soc;
Jay Agarwal94716cd2013-08-09 16:49:24 +02001124
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001125 pcie->pex_clk = devm_clk_get(dev, "pex");
Thierry Redingd1523b52013-08-09 16:49:19 +02001126 if (IS_ERR(pcie->pex_clk))
1127 return PTR_ERR(pcie->pex_clk);
1128
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001129 pcie->afi_clk = devm_clk_get(dev, "afi");
Thierry Redingd1523b52013-08-09 16:49:19 +02001130 if (IS_ERR(pcie->afi_clk))
1131 return PTR_ERR(pcie->afi_clk);
1132
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001133 pcie->pll_e = devm_clk_get(dev, "pll_e");
Thierry Redingd1523b52013-08-09 16:49:19 +02001134 if (IS_ERR(pcie->pll_e))
1135 return PTR_ERR(pcie->pll_e);
1136
Jay Agarwal94716cd2013-08-09 16:49:24 +02001137 if (soc->has_cml_clk) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001138 pcie->cml_clk = devm_clk_get(dev, "cml");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001139 if (IS_ERR(pcie->cml_clk))
1140 return PTR_ERR(pcie->cml_clk);
1141 }
1142
Thierry Redingd1523b52013-08-09 16:49:19 +02001143 return 0;
1144}
1145
Stephen Warren3127a6b2013-11-06 15:56:58 -07001146static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1147{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001148 struct device *dev = pcie->dev;
1149
Philipp Zabel4b9cc2c2017-07-19 17:25:57 +02001150 pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
Stephen Warren3127a6b2013-11-06 15:56:58 -07001151 if (IS_ERR(pcie->pex_rst))
1152 return PTR_ERR(pcie->pex_rst);
1153
Philipp Zabel4b9cc2c2017-07-19 17:25:57 +02001154 pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
Stephen Warren3127a6b2013-11-06 15:56:58 -07001155 if (IS_ERR(pcie->afi_rst))
1156 return PTR_ERR(pcie->afi_rst);
1157
Philipp Zabel4b9cc2c2017-07-19 17:25:57 +02001158 pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
Stephen Warren3127a6b2013-11-06 15:56:58 -07001159 if (IS_ERR(pcie->pcie_xrst))
1160 return PTR_ERR(pcie->pcie_xrst);
1161
1162 return 0;
1163}
1164
Thierry Reding6fe7c182015-11-11 18:25:59 +01001165static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1166{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001167 struct device *dev = pcie->dev;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001168 int err;
1169
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001170 pcie->phy = devm_phy_optional_get(dev, "pcie");
Thierry Reding6fe7c182015-11-11 18:25:59 +01001171 if (IS_ERR(pcie->phy)) {
1172 err = PTR_ERR(pcie->phy);
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001173 dev_err(dev, "failed to get PHY: %d\n", err);
Thierry Reding6fe7c182015-11-11 18:25:59 +01001174 return err;
1175 }
1176
1177 err = phy_init(pcie->phy);
1178 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001179 dev_err(dev, "failed to initialize PHY: %d\n", err);
Thierry Reding6fe7c182015-11-11 18:25:59 +01001180 return err;
1181 }
1182
1183 pcie->legacy_phy = true;
1184
1185 return 0;
1186}
1187
1188static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1189 struct device_node *np,
1190 const char *consumer,
1191 unsigned int index)
1192{
1193 struct phy *phy;
1194 char *name;
1195
1196 name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1197 if (!name)
1198 return ERR_PTR(-ENOMEM);
1199
1200 phy = devm_of_phy_get(dev, np, name);
1201 kfree(name);
1202
1203 if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1204 phy = NULL;
1205
1206 return phy;
1207}
1208
1209static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1210{
1211 struct device *dev = port->pcie->dev;
1212 struct phy *phy;
1213 unsigned int i;
1214 int err;
1215
1216 port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1217 if (!port->phys)
1218 return -ENOMEM;
1219
1220 for (i = 0; i < port->lanes; i++) {
1221 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1222 if (IS_ERR(phy)) {
1223 dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1224 PTR_ERR(phy));
1225 return PTR_ERR(phy);
1226 }
1227
1228 err = phy_init(phy);
1229 if (err < 0) {
1230 dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1231 err);
1232 return err;
1233 }
1234
1235 port->phys[i] = phy;
1236 }
1237
1238 return 0;
1239}
1240
1241static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1242{
Thierry Redinga7fbae22016-08-15 17:31:31 +02001243 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001244 struct device_node *np = pcie->dev->of_node;
1245 struct tegra_pcie_port *port;
1246 int err;
1247
1248 if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1249 return tegra_pcie_phys_get_legacy(pcie);
1250
1251 list_for_each_entry(port, &pcie->ports, list) {
1252 err = tegra_pcie_port_get_phys(port);
1253 if (err < 0)
1254 return err;
1255 }
1256
1257 return 0;
1258}
1259
Thierry Redingd1523b52013-08-09 16:49:19 +02001260static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1261{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001262 struct device *dev = pcie->dev;
1263 struct platform_device *pdev = to_platform_device(dev);
Thierry Redingd1523b52013-08-09 16:49:19 +02001264 struct resource *pads, *afi, *res;
1265 int err;
1266
1267 err = tegra_pcie_clocks_get(pcie);
1268 if (err) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001269 dev_err(dev, "failed to get clocks: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001270 return err;
1271 }
1272
Stephen Warren3127a6b2013-11-06 15:56:58 -07001273 err = tegra_pcie_resets_get(pcie);
1274 if (err) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001275 dev_err(dev, "failed to get resets: %d\n", err);
Stephen Warren3127a6b2013-11-06 15:56:58 -07001276 return err;
1277 }
1278
Thierry Reding6fe7c182015-11-11 18:25:59 +01001279 err = tegra_pcie_phys_get(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001280 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001281 dev_err(dev, "failed to get PHYs: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001282 return err;
1283 }
1284
Thierry Redingd1523b52013-08-09 16:49:19 +02001285 err = tegra_pcie_power_on(pcie);
1286 if (err) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001287 dev_err(dev, "failed to power up: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001288 return err;
1289 }
1290
Thierry Redingd1523b52013-08-09 16:49:19 +02001291 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001292 pcie->pads = devm_ioremap_resource(dev, pads);
Julia Lawalldc05ee32013-08-26 11:11:09 +02001293 if (IS_ERR(pcie->pads)) {
1294 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001295 goto poweroff;
1296 }
1297
1298 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001299 pcie->afi = devm_ioremap_resource(dev, afi);
Julia Lawalldc05ee32013-08-26 11:11:09 +02001300 if (IS_ERR(pcie->afi)) {
1301 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001302 goto poweroff;
1303 }
1304
Julia Lawalldc05ee32013-08-26 11:11:09 +02001305 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001306 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1307 if (!res) {
1308 err = -EADDRNOTAVAIL;
1309 goto poweroff;
1310 }
1311
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001312 pcie->cs = devm_request_mem_region(dev, res->start,
Thierry Redingd1523b52013-08-09 16:49:19 +02001313 resource_size(res), res->name);
1314 if (!pcie->cs) {
1315 err = -EADDRNOTAVAIL;
1316 goto poweroff;
1317 }
1318
1319 /* request interrupt */
1320 err = platform_get_irq_byname(pdev, "intr");
1321 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001322 dev_err(dev, "failed to get IRQ: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001323 goto poweroff;
1324 }
1325
1326 pcie->irq = err;
1327
1328 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1329 if (err) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001330 dev_err(dev, "failed to register IRQ: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001331 goto poweroff;
1332 }
1333
1334 return 0;
1335
1336poweroff:
1337 tegra_pcie_power_off(pcie);
1338 return err;
1339}
1340
1341static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1342{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001343 struct device *dev = pcie->dev;
Thierry Reding7f1f0542014-08-26 17:11:38 +02001344 int err;
1345
Thierry Redingd1523b52013-08-09 16:49:19 +02001346 if (pcie->irq > 0)
1347 free_irq(pcie->irq, pcie);
1348
1349 tegra_pcie_power_off(pcie);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001350
1351 err = phy_exit(pcie->phy);
1352 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001353 dev_err(dev, "failed to teardown PHY: %d\n", err);
Thierry Reding7f1f0542014-08-26 17:11:38 +02001354
Thierry Redingd1523b52013-08-09 16:49:19 +02001355 return 0;
1356}
1357
1358static int tegra_msi_alloc(struct tegra_msi *chip)
1359{
1360 int msi;
1361
1362 mutex_lock(&chip->lock);
1363
1364 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1365 if (msi < INT_PCI_MSI_NR)
1366 set_bit(msi, chip->used);
1367 else
1368 msi = -ENOSPC;
1369
1370 mutex_unlock(&chip->lock);
1371
1372 return msi;
1373}
1374
1375static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1376{
1377 struct device *dev = chip->chip.dev;
1378
1379 mutex_lock(&chip->lock);
1380
1381 if (!test_bit(irq, chip->used))
1382 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1383 else
1384 clear_bit(irq, chip->used);
1385
1386 mutex_unlock(&chip->lock);
1387}
1388
1389static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1390{
1391 struct tegra_pcie *pcie = data;
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001392 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02001393 struct tegra_msi *msi = &pcie->msi;
1394 unsigned int i, processed = 0;
1395
1396 for (i = 0; i < 8; i++) {
1397 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1398
1399 while (reg) {
1400 unsigned int offset = find_first_bit(&reg, 32);
1401 unsigned int index = i * 32 + offset;
1402 unsigned int irq;
1403
1404 /* clear the interrupt */
1405 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1406
1407 irq = irq_find_mapping(msi->domain, index);
1408 if (irq) {
1409 if (test_bit(index, msi->used))
1410 generic_handle_irq(irq);
1411 else
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001412 dev_info(dev, "unhandled MSI\n");
Thierry Redingd1523b52013-08-09 16:49:19 +02001413 } else {
1414 /*
1415 * that's weird who triggered this?
1416 * just clear it
1417 */
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001418 dev_info(dev, "unexpected MSI\n");
Thierry Redingd1523b52013-08-09 16:49:19 +02001419 }
1420
1421 /* see if there's any more pending in this vector */
1422 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1423
1424 processed++;
1425 }
1426 }
1427
1428 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1429}
1430
Yijing Wangc2791b82014-11-11 17:45:45 -07001431static int tegra_msi_setup_irq(struct msi_controller *chip,
1432 struct pci_dev *pdev, struct msi_desc *desc)
Thierry Redingd1523b52013-08-09 16:49:19 +02001433{
1434 struct tegra_msi *msi = to_tegra_msi(chip);
1435 struct msi_msg msg;
1436 unsigned int irq;
1437 int hwirq;
1438
1439 hwirq = tegra_msi_alloc(msi);
1440 if (hwirq < 0)
1441 return hwirq;
1442
1443 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001444 if (!irq) {
1445 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001446 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001447 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001448
1449 irq_set_msi_desc(irq, desc);
1450
Thierry Redingc0165552017-05-04 22:10:31 +02001451 msg.address_lo = lower_32_bits(msi->phys);
1452 msg.address_hi = upper_32_bits(msi->phys);
Thierry Redingd1523b52013-08-09 16:49:19 +02001453 msg.data = hwirq;
1454
Jiang Liu83a18912014-11-09 23:10:34 +08001455 pci_write_msi_msg(irq, &msg);
Thierry Redingd1523b52013-08-09 16:49:19 +02001456
1457 return 0;
1458}
1459
Yijing Wangc2791b82014-11-11 17:45:45 -07001460static void tegra_msi_teardown_irq(struct msi_controller *chip,
1461 unsigned int irq)
Thierry Redingd1523b52013-08-09 16:49:19 +02001462{
1463 struct tegra_msi *msi = to_tegra_msi(chip);
1464 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001465 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001466
Jisheng Zhang019fa462014-07-29 09:33:30 +08001467 irq_dispose_mapping(irq);
1468 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001469}
1470
1471static struct irq_chip tegra_msi_irq_chip = {
1472 .name = "Tegra PCIe MSI",
Thomas Gleixner280510f2014-11-23 12:23:20 +01001473 .irq_enable = pci_msi_unmask_irq,
1474 .irq_disable = pci_msi_mask_irq,
1475 .irq_mask = pci_msi_mask_irq,
1476 .irq_unmask = pci_msi_unmask_irq,
Thierry Redingd1523b52013-08-09 16:49:19 +02001477};
1478
1479static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1480 irq_hw_number_t hwirq)
1481{
1482 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1483 irq_set_chip_data(irq, domain->host_data);
Thierry Redingd1523b52013-08-09 16:49:19 +02001484
Stephen Warrenb4f17372013-05-06 14:19:19 -06001485 tegra_cpuidle_pcie_irqs_in_use();
1486
Thierry Redingd1523b52013-08-09 16:49:19 +02001487 return 0;
1488}
1489
1490static const struct irq_domain_ops msi_domain_ops = {
1491 .map = tegra_msi_map,
1492};
1493
1494static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1495{
Arnd Bergmann76f25412016-11-25 11:57:12 +01001496 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1497 struct platform_device *pdev = to_platform_device(pcie->dev);
Thierry Redinga7fbae22016-08-15 17:31:31 +02001498 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001499 struct tegra_msi *msi = &pcie->msi;
Arnd Bergmann76f25412016-11-25 11:57:12 +01001500 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02001501 int err;
1502 u32 reg;
1503
1504 mutex_init(&msi->lock);
1505
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001506 msi->chip.dev = dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02001507 msi->chip.setup_irq = tegra_msi_setup_irq;
1508 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1509
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001510 msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
Thierry Redingd1523b52013-08-09 16:49:19 +02001511 &msi_domain_ops, &msi->chip);
1512 if (!msi->domain) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001513 dev_err(dev, "failed to create IRQ domain\n");
Thierry Redingd1523b52013-08-09 16:49:19 +02001514 return -ENOMEM;
1515 }
1516
1517 err = platform_get_irq_byname(pdev, "msi");
1518 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001519 dev_err(dev, "failed to get IRQ: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001520 goto err;
1521 }
1522
1523 msi->irq = err;
1524
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +02001525 err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
Thierry Redingd1523b52013-08-09 16:49:19 +02001526 tegra_msi_irq_chip.name, pcie);
1527 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001528 dev_err(dev, "failed to request IRQ: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001529 goto err;
1530 }
1531
Thierry Redingd7bd5542017-05-04 22:10:32 +02001532 /*
1533 * The PCI host bridge on Tegra contains some logic that intercepts
1534 * MSI writes, which means that the MSI target address doesn't have
1535 * to point to actual physical memory. Rather than allocating one 4
1536 * KiB page of system memory that's never used, we can simply pick
1537 * an arbitrary address within an area reserved for system memory
1538 * in the FPCI address map.
1539 *
1540 * However, in order to avoid confusion, we pick an address that
1541 * doesn't map to physical memory. The FPCI address map reserves a
1542 * 1012 GiB region for system memory and memory-mapped I/O. Since
1543 * none of the Tegra SoCs that contain this PCI host bridge can
1544 * address more than 16 GiB of system memory, the last 4 KiB of
1545 * these 1012 GiB is a good candidate.
1546 */
1547 msi->phys = 0xfcfffff000;
Thierry Redingd1523b52013-08-09 16:49:19 +02001548
Thierry Redingc0165552017-05-04 22:10:31 +02001549 afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1550 afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001551 /* this register is in 4K increments */
1552 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1553
1554 /* enable all MSI vectors */
1555 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1556 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1557 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1558 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1559 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1560 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1561 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1562 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1563
1564 /* and unmask the MSI interrupt */
1565 reg = afi_readl(pcie, AFI_INTR_MASK);
1566 reg |= AFI_INTR_MASK_MSI_MASK;
1567 afi_writel(pcie, reg, AFI_INTR_MASK);
1568
Arnd Bergmann76f25412016-11-25 11:57:12 +01001569 host->msi = &msi->chip;
1570
Thierry Redingd1523b52013-08-09 16:49:19 +02001571 return 0;
1572
1573err:
1574 irq_domain_remove(msi->domain);
1575 return err;
1576}
1577
1578static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1579{
1580 struct tegra_msi *msi = &pcie->msi;
1581 unsigned int i, irq;
1582 u32 value;
1583
1584 /* mask the MSI interrupt */
1585 value = afi_readl(pcie, AFI_INTR_MASK);
1586 value &= ~AFI_INTR_MASK_MSI_MASK;
1587 afi_writel(pcie, value, AFI_INTR_MASK);
1588
1589 /* disable all MSI vectors */
1590 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1591 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1592 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1593 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1594 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1595 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1596 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1597 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1598
Thierry Redingd1523b52013-08-09 16:49:19 +02001599 if (msi->irq > 0)
1600 free_irq(msi->irq, pcie);
1601
1602 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1603 irq = irq_find_mapping(msi->domain, i);
1604 if (irq > 0)
1605 irq_dispose_mapping(irq);
1606 }
1607
1608 irq_domain_remove(msi->domain);
1609
1610 return 0;
1611}
1612
1613static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1614 u32 *xbar)
1615{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001616 struct device *dev = pcie->dev;
1617 struct device_node *np = dev->of_node;
Thierry Redingd1523b52013-08-09 16:49:19 +02001618
Thierry Redingc7a091c2016-11-25 11:57:15 +01001619 if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1620 of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
Thierry Reding7f1f0542014-08-26 17:11:38 +02001621 switch (lanes) {
1622 case 0x0000104:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001623 dev_info(dev, "4x1, 1x1 configuration\n");
Thierry Reding7f1f0542014-08-26 17:11:38 +02001624 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1625 return 0;
1626
1627 case 0x0000102:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001628 dev_info(dev, "2x1, 1x1 configuration\n");
Thierry Reding7f1f0542014-08-26 17:11:38 +02001629 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1630 return 0;
1631 }
1632 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Jay Agarwal94716cd2013-08-09 16:49:24 +02001633 switch (lanes) {
1634 case 0x00000204:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001635 dev_info(dev, "4x1, 2x1 configuration\n");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001636 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1637 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001638
Jay Agarwal94716cd2013-08-09 16:49:24 +02001639 case 0x00020202:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001640 dev_info(dev, "2x3 configuration\n");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001641 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1642 return 0;
1643
1644 case 0x00010104:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001645 dev_info(dev, "4x1, 1x2 configuration\n");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001646 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1647 return 0;
1648 }
1649 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1650 switch (lanes) {
1651 case 0x00000004:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001652 dev_info(dev, "single-mode configuration\n");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001653 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1654 return 0;
1655
1656 case 0x00000202:
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001657 dev_info(dev, "dual-mode configuration\n");
Jay Agarwal94716cd2013-08-09 16:49:24 +02001658 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1659 return 0;
1660 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001661 }
1662
1663 return -EINVAL;
1664}
1665
Thierry Reding077fb152014-05-28 16:49:13 +02001666/*
1667 * Check whether a given set of supplies is available in a device tree node.
1668 * This is used to check whether the new or the legacy device tree bindings
1669 * should be used.
1670 */
1671static bool of_regulator_bulk_available(struct device_node *np,
1672 struct regulator_bulk_data *supplies,
1673 unsigned int num_supplies)
1674{
1675 char property[32];
1676 unsigned int i;
1677
1678 for (i = 0; i < num_supplies; i++) {
1679 snprintf(property, 32, "%s-supply", supplies[i].supply);
1680
1681 if (of_find_property(np, property, NULL) == NULL)
1682 return false;
1683 }
1684
1685 return true;
1686}
1687
1688/*
1689 * Old versions of the device tree binding for this device used a set of power
1690 * supplies that didn't match the hardware inputs. This happened to work for a
1691 * number of cases but is not future proof. However to preserve backwards-
1692 * compatibility with old device trees, this function will try to use the old
1693 * set of supplies.
1694 */
1695static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1696{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001697 struct device *dev = pcie->dev;
1698 struct device_node *np = dev->of_node;
Thierry Reding077fb152014-05-28 16:49:13 +02001699
1700 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1701 pcie->num_supplies = 3;
1702 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1703 pcie->num_supplies = 2;
1704
1705 if (pcie->num_supplies == 0) {
Rob Herringb63773a2017-07-18 16:43:21 -05001706 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
Thierry Reding077fb152014-05-28 16:49:13 +02001707 return -ENODEV;
1708 }
1709
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001710 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
Thierry Reding077fb152014-05-28 16:49:13 +02001711 sizeof(*pcie->supplies),
1712 GFP_KERNEL);
1713 if (!pcie->supplies)
1714 return -ENOMEM;
1715
1716 pcie->supplies[0].supply = "pex-clk";
1717 pcie->supplies[1].supply = "vdd";
1718
1719 if (pcie->num_supplies > 2)
1720 pcie->supplies[2].supply = "avdd";
1721
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001722 return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
Thierry Reding077fb152014-05-28 16:49:13 +02001723}
1724
1725/*
1726 * Obtains the list of regulators required for a particular generation of the
1727 * IP block.
1728 *
1729 * This would've been nice to do simply by providing static tables for use
1730 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1731 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1732 * and either seems to be optional depending on which ports are being used.
1733 */
1734static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1735{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001736 struct device *dev = pcie->dev;
1737 struct device_node *np = dev->of_node;
Thierry Reding077fb152014-05-28 16:49:13 +02001738 unsigned int i = 0;
1739
Thierry Redingc7a091c2016-11-25 11:57:15 +01001740 if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1741 pcie->num_supplies = 6;
1742
1743 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1744 sizeof(*pcie->supplies),
1745 GFP_KERNEL);
1746 if (!pcie->supplies)
1747 return -ENOMEM;
1748
1749 pcie->supplies[i++].supply = "avdd-pll-uerefe";
1750 pcie->supplies[i++].supply = "hvddio-pex";
1751 pcie->supplies[i++].supply = "dvddio-pex";
1752 pcie->supplies[i++].supply = "dvdd-pex-pll";
1753 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1754 pcie->supplies[i++].supply = "vddio-pex-ctl";
1755 } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
Thierry Reding7f1f0542014-08-26 17:11:38 +02001756 pcie->num_supplies = 7;
1757
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001758 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
Thierry Reding7f1f0542014-08-26 17:11:38 +02001759 sizeof(*pcie->supplies),
1760 GFP_KERNEL);
1761 if (!pcie->supplies)
1762 return -ENOMEM;
1763
1764 pcie->supplies[i++].supply = "avddio-pex";
1765 pcie->supplies[i++].supply = "dvddio-pex";
1766 pcie->supplies[i++].supply = "avdd-pex-pll";
1767 pcie->supplies[i++].supply = "hvdd-pex";
1768 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1769 pcie->supplies[i++].supply = "vddio-pex-ctl";
1770 pcie->supplies[i++].supply = "avdd-pll-erefe";
1771 } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
Thierry Reding077fb152014-05-28 16:49:13 +02001772 bool need_pexa = false, need_pexb = false;
1773
1774 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1775 if (lane_mask & 0x0f)
1776 need_pexa = true;
1777
1778 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1779 if (lane_mask & 0x30)
1780 need_pexb = true;
1781
1782 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1783 (need_pexb ? 2 : 0);
1784
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001785 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
Thierry Reding077fb152014-05-28 16:49:13 +02001786 sizeof(*pcie->supplies),
1787 GFP_KERNEL);
1788 if (!pcie->supplies)
1789 return -ENOMEM;
1790
1791 pcie->supplies[i++].supply = "avdd-pex-pll";
1792 pcie->supplies[i++].supply = "hvdd-pex";
1793 pcie->supplies[i++].supply = "vddio-pex-ctl";
1794 pcie->supplies[i++].supply = "avdd-plle";
1795
1796 if (need_pexa) {
1797 pcie->supplies[i++].supply = "avdd-pexa";
1798 pcie->supplies[i++].supply = "vdd-pexa";
1799 }
1800
1801 if (need_pexb) {
1802 pcie->supplies[i++].supply = "avdd-pexb";
1803 pcie->supplies[i++].supply = "vdd-pexb";
1804 }
1805 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1806 pcie->num_supplies = 5;
1807
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001808 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
Thierry Reding077fb152014-05-28 16:49:13 +02001809 sizeof(*pcie->supplies),
1810 GFP_KERNEL);
1811 if (!pcie->supplies)
1812 return -ENOMEM;
1813
1814 pcie->supplies[0].supply = "avdd-pex";
1815 pcie->supplies[1].supply = "vdd-pex";
1816 pcie->supplies[2].supply = "avdd-pex-pll";
1817 pcie->supplies[3].supply = "avdd-plle";
1818 pcie->supplies[4].supply = "vddio-pex-clk";
1819 }
1820
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001821 if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
Thierry Reding077fb152014-05-28 16:49:13 +02001822 pcie->num_supplies))
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001823 return devm_regulator_bulk_get(dev, pcie->num_supplies,
Thierry Reding077fb152014-05-28 16:49:13 +02001824 pcie->supplies);
1825
1826 /*
1827 * If not all regulators are available for this new scheme, assume
1828 * that the device tree complies with an older version of the device
1829 * tree binding.
1830 */
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001831 dev_info(dev, "using legacy DT binding for power supplies\n");
Thierry Reding077fb152014-05-28 16:49:13 +02001832
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001833 devm_kfree(dev, pcie->supplies);
Thierry Reding077fb152014-05-28 16:49:13 +02001834 pcie->num_supplies = 0;
1835
1836 return tegra_pcie_get_legacy_regulators(pcie);
1837}
1838
Thierry Redingd1523b52013-08-09 16:49:19 +02001839static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1840{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001841 struct device *dev = pcie->dev;
1842 struct device_node *np = dev->of_node, *port;
Thierry Redinga7fbae22016-08-15 17:31:31 +02001843 const struct tegra_pcie_soc *soc = pcie->soc;
Thierry Redingd1523b52013-08-09 16:49:19 +02001844 struct of_pci_range_parser parser;
1845 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001846 u32 lanes = 0, mask = 0;
1847 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001848 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001849 int err;
1850
1851 if (of_pci_range_parser_init(&parser, np)) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001852 dev_err(dev, "missing \"ranges\" property\n");
Thierry Redingd1523b52013-08-09 16:49:19 +02001853 return -EINVAL;
1854 }
1855
Thierry Redingd1523b52013-08-09 16:49:19 +02001856 for_each_of_pci_range(&parser, &range) {
Liviu Dudau0b0b0892014-09-29 15:29:25 +01001857 err = of_pci_range_to_resource(&range, np, &res);
1858 if (err < 0)
1859 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +02001860
1861 switch (res.flags & IORESOURCE_TYPE_BITS) {
1862 case IORESOURCE_IO:
Thierry Reding56e75e22016-02-09 15:52:32 +01001863 /* Track the bus -> CPU I/O mapping offset. */
1864 pcie->offset.io = res.start - range.pci_addr;
1865
Thierry Reding51067872014-11-27 09:54:09 +01001866 memcpy(&pcie->pio, &res, sizeof(res));
1867 pcie->pio.name = np->full_name;
1868
1869 /*
1870 * The Tegra PCIe host bridge uses this to program the
1871 * mapping of the I/O space to the physical address,
1872 * so we override the .start and .end fields here that
1873 * of_pci_range_to_resource() converted to I/O space.
1874 * We also set the IORESOURCE_MEM type to clarify that
1875 * the resource is in the physical memory space.
1876 */
1877 pcie->io.start = range.cpu_addr;
1878 pcie->io.end = range.cpu_addr + range.size - 1;
1879 pcie->io.flags = IORESOURCE_MEM;
1880 pcie->io.name = "I/O";
1881
1882 memcpy(&res, &pcie->io, sizeof(res));
Thierry Redingd1523b52013-08-09 16:49:19 +02001883 break;
1884
1885 case IORESOURCE_MEM:
Thierry Reding56e75e22016-02-09 15:52:32 +01001886 /*
1887 * Track the bus -> CPU memory mapping offset. This
1888 * assumes that the prefetchable and non-prefetchable
1889 * regions will be the last of type IORESOURCE_MEM in
1890 * the ranges property.
1891 * */
1892 pcie->offset.mem = res.start - range.pci_addr;
1893
Thierry Redingd1523b52013-08-09 16:49:19 +02001894 if (res.flags & IORESOURCE_PREFETCH) {
1895 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001896 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001897 } else {
1898 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001899 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001900 }
1901 break;
1902 }
1903 }
1904
1905 err = of_pci_parse_bus_range(np, &pcie->busn);
1906 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001907 dev_err(dev, "failed to parse ranges property: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001908 pcie->busn.name = np->name;
1909 pcie->busn.start = 0;
1910 pcie->busn.end = 0xff;
1911 pcie->busn.flags = IORESOURCE_BUS;
1912 }
1913
1914 /* parse root ports */
1915 for_each_child_of_node(np, port) {
1916 struct tegra_pcie_port *rp;
1917 unsigned int index;
1918 u32 value;
1919
1920 err = of_pci_get_devfn(port);
1921 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001922 dev_err(dev, "failed to parse address: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001923 return err;
1924 }
1925
1926 index = PCI_SLOT(err);
1927
Jay Agarwal94716cd2013-08-09 16:49:24 +02001928 if (index < 1 || index > soc->num_ports) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001929 dev_err(dev, "invalid port number: %d\n", index);
Thierry Redingd1523b52013-08-09 16:49:19 +02001930 return -EINVAL;
1931 }
1932
1933 index--;
1934
1935 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1936 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001937 dev_err(dev, "failed to parse # of lanes: %d\n",
Thierry Redingd1523b52013-08-09 16:49:19 +02001938 err);
1939 return err;
1940 }
1941
1942 if (value > 16) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001943 dev_err(dev, "invalid # of lanes: %u\n", value);
Thierry Redingd1523b52013-08-09 16:49:19 +02001944 return -EINVAL;
1945 }
1946
1947 lanes |= value << (index << 3);
1948
Thierry Reding077fb152014-05-28 16:49:13 +02001949 if (!of_device_is_available(port)) {
1950 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001951 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001952 }
1953
1954 mask |= ((1 << value) - 1) << lane;
1955 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001956
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001957 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
Thierry Redingd1523b52013-08-09 16:49:19 +02001958 if (!rp)
1959 return -ENOMEM;
1960
1961 err = of_address_to_resource(port, 0, &rp->regs);
1962 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001963 dev_err(dev, "failed to parse address: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02001964 return err;
1965 }
1966
1967 INIT_LIST_HEAD(&rp->list);
1968 rp->index = index;
1969 rp->lanes = value;
1970 rp->pcie = pcie;
Thierry Reding6fe7c182015-11-11 18:25:59 +01001971 rp->np = port;
Thierry Redingd1523b52013-08-09 16:49:19 +02001972
Lorenzo Pieralisi3e02dc42017-04-19 17:49:06 +01001973 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
Julia Lawalldc05ee32013-08-26 11:11:09 +02001974 if (IS_ERR(rp->base))
1975 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001976
1977 list_add_tail(&rp->list, &pcie->ports);
1978 }
1979
1980 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1981 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05001982 dev_err(dev, "invalid lane configuration\n");
Thierry Redingd1523b52013-08-09 16:49:19 +02001983 return err;
1984 }
1985
Thierry Reding077fb152014-05-28 16:49:13 +02001986 err = tegra_pcie_get_regulators(pcie, mask);
1987 if (err < 0)
1988 return err;
1989
Thierry Redingd1523b52013-08-09 16:49:19 +02001990 return 0;
1991}
1992
1993/*
1994 * FIXME: If there are no PCIe cards attached, then calling this function
1995 * can result in the increase of the bootup time as there are big timeout
1996 * loops.
1997 */
1998#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1999static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2000{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002001 struct device *dev = port->pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02002002 unsigned int retries = 3;
2003 unsigned long value;
2004
Thierry Reding7f1f0542014-08-26 17:11:38 +02002005 /* override presence detection */
2006 value = readl(port->base + RP_PRIV_MISC);
2007 value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2008 value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2009 writel(value, port->base + RP_PRIV_MISC);
2010
Thierry Redingd1523b52013-08-09 16:49:19 +02002011 do {
2012 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2013
2014 do {
2015 value = readl(port->base + RP_VEND_XP);
2016
2017 if (value & RP_VEND_XP_DL_UP)
2018 break;
2019
2020 usleep_range(1000, 2000);
2021 } while (--timeout);
2022
2023 if (!timeout) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002024 dev_err(dev, "link %u down, retrying\n", port->index);
Thierry Redingd1523b52013-08-09 16:49:19 +02002025 goto retry;
2026 }
2027
2028 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2029
2030 do {
2031 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2032
2033 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2034 return true;
2035
2036 usleep_range(1000, 2000);
2037 } while (--timeout);
2038
2039retry:
2040 tegra_pcie_port_reset(port);
2041 } while (--retries);
2042
2043 return false;
2044}
2045
Arnd Bergmann76f25412016-11-25 11:57:12 +01002046static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
Thierry Redingd1523b52013-08-09 16:49:19 +02002047{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002048 struct device *dev = pcie->dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02002049 struct tegra_pcie_port *port, *tmp;
Thierry Redingd1523b52013-08-09 16:49:19 +02002050
2051 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002052 dev_info(dev, "probing port %u, using %u lanes\n",
Thierry Redingd1523b52013-08-09 16:49:19 +02002053 port->index, port->lanes);
2054
2055 tegra_pcie_port_enable(port);
2056
2057 if (tegra_pcie_port_check_link(port))
2058 continue;
2059
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002060 dev_info(dev, "link %u down, ignoring\n", port->index);
Thierry Redingd1523b52013-08-09 16:49:19 +02002061
2062 tegra_pcie_port_disable(port);
2063 tegra_pcie_port_free(port);
2064 }
Thierry Redingd1523b52013-08-09 16:49:19 +02002065}
2066
Thierry Redinga7fbae22016-08-15 17:31:31 +02002067static const struct tegra_pcie_soc tegra20_pcie = {
Jay Agarwal94716cd2013-08-09 16:49:24 +02002068 .num_ports = 2,
2069 .msi_base_shift = 0,
2070 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2071 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
Stephen Warrenf8144302016-07-25 16:02:27 -05002072 .pads_refclk_cfg0 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002073 .has_pex_clkreq_en = false,
2074 .has_pex_bias_ctrl = false,
2075 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002076 .has_cml_clk = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002077 .has_gen2 = false,
Thierry Reding76245ca2016-11-25 11:57:14 +01002078 .force_pca_enable = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002079};
2080
Thierry Redinga7fbae22016-08-15 17:31:31 +02002081static const struct tegra_pcie_soc tegra30_pcie = {
Jay Agarwal94716cd2013-08-09 16:49:24 +02002082 .num_ports = 3,
2083 .msi_base_shift = 8,
2084 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2085 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002086 .pads_refclk_cfg0 = 0xfa5cfa5c,
2087 .pads_refclk_cfg1 = 0xfa5cfa5c,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002088 .has_pex_clkreq_en = true,
2089 .has_pex_bias_ctrl = true,
2090 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002091 .has_cml_clk = true,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002092 .has_gen2 = false,
Thierry Reding76245ca2016-11-25 11:57:14 +01002093 .force_pca_enable = false,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002094};
2095
Thierry Redinga7fbae22016-08-15 17:31:31 +02002096static const struct tegra_pcie_soc tegra124_pcie = {
Thierry Reding7f1f0542014-08-26 17:11:38 +02002097 .num_ports = 2,
2098 .msi_base_shift = 8,
2099 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2100 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
Stephen Warrenf8144302016-07-25 16:02:27 -05002101 .pads_refclk_cfg0 = 0x44ac44ac,
Thierry Reding7f1f0542014-08-26 17:11:38 +02002102 .has_pex_clkreq_en = true,
2103 .has_pex_bias_ctrl = true,
2104 .has_intr_prsnt_sense = true,
2105 .has_cml_clk = true,
2106 .has_gen2 = true,
Thierry Reding76245ca2016-11-25 11:57:14 +01002107 .force_pca_enable = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02002108};
2109
Thierry Redingc7a091c2016-11-25 11:57:15 +01002110static const struct tegra_pcie_soc tegra210_pcie = {
2111 .num_ports = 2,
2112 .msi_base_shift = 8,
2113 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2114 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2115 .pads_refclk_cfg0 = 0x90b890b8,
2116 .has_pex_clkreq_en = true,
2117 .has_pex_bias_ctrl = true,
2118 .has_intr_prsnt_sense = true,
2119 .has_cml_clk = true,
2120 .has_gen2 = true,
2121 .force_pca_enable = true,
2122};
2123
Jay Agarwal94716cd2013-08-09 16:49:24 +02002124static const struct of_device_id tegra_pcie_of_match[] = {
Thierry Redingc7a091c2016-11-25 11:57:15 +01002125 { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
Thierry Redinga7fbae22016-08-15 17:31:31 +02002126 { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2127 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2128 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
Jay Agarwal94716cd2013-08-09 16:49:24 +02002129 { },
2130};
Jay Agarwal94716cd2013-08-09 16:49:24 +02002131
Thierry Reding2cb989f2014-07-22 12:30:46 -06002132static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2133{
2134 struct tegra_pcie *pcie = s->private;
2135
2136 if (list_empty(&pcie->ports))
2137 return NULL;
2138
2139 seq_printf(s, "Index Status\n");
2140
2141 return seq_list_start(&pcie->ports, *pos);
2142}
2143
2144static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2145{
2146 struct tegra_pcie *pcie = s->private;
2147
2148 return seq_list_next(v, &pcie->ports, pos);
2149}
2150
2151static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2152{
2153}
2154
2155static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2156{
2157 bool up = false, active = false;
2158 struct tegra_pcie_port *port;
2159 unsigned int value;
2160
2161 port = list_entry(v, struct tegra_pcie_port, list);
2162
2163 value = readl(port->base + RP_VEND_XP);
2164
2165 if (value & RP_VEND_XP_DL_UP)
2166 up = true;
2167
2168 value = readl(port->base + RP_LINK_CONTROL_STATUS);
2169
2170 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2171 active = true;
2172
2173 seq_printf(s, "%2u ", port->index);
2174
2175 if (up)
2176 seq_printf(s, "up");
2177
2178 if (active) {
2179 if (up)
2180 seq_printf(s, ", ");
2181
2182 seq_printf(s, "active");
2183 }
2184
2185 seq_printf(s, "\n");
2186 return 0;
2187}
2188
2189static const struct seq_operations tegra_pcie_ports_seq_ops = {
2190 .start = tegra_pcie_ports_seq_start,
2191 .next = tegra_pcie_ports_seq_next,
2192 .stop = tegra_pcie_ports_seq_stop,
2193 .show = tegra_pcie_ports_seq_show,
2194};
2195
2196static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2197{
2198 struct tegra_pcie *pcie = inode->i_private;
2199 struct seq_file *s;
2200 int err;
2201
2202 err = seq_open(file, &tegra_pcie_ports_seq_ops);
2203 if (err)
2204 return err;
2205
2206 s = file->private_data;
2207 s->private = pcie;
2208
2209 return 0;
2210}
2211
2212static const struct file_operations tegra_pcie_ports_ops = {
2213 .owner = THIS_MODULE,
2214 .open = tegra_pcie_ports_open,
2215 .read = seq_read,
2216 .llseek = seq_lseek,
2217 .release = seq_release,
2218};
2219
2220static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2221{
2222 struct dentry *file;
2223
2224 pcie->debugfs = debugfs_create_dir("pcie", NULL);
2225 if (!pcie->debugfs)
2226 return -ENOMEM;
2227
2228 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2229 pcie, &tegra_pcie_ports_ops);
2230 if (!file)
2231 goto remove;
2232
2233 return 0;
2234
2235remove:
2236 debugfs_remove_recursive(pcie->debugfs);
2237 pcie->debugfs = NULL;
2238 return -ENOMEM;
2239}
2240
Thierry Redingd1523b52013-08-09 16:49:19 +02002241static int tegra_pcie_probe(struct platform_device *pdev)
2242{
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002243 struct device *dev = &pdev->dev;
Arnd Bergmann76f25412016-11-25 11:57:12 +01002244 struct pci_host_bridge *host;
Thierry Redingd1523b52013-08-09 16:49:19 +02002245 struct tegra_pcie *pcie;
Arnd Bergmann76f25412016-11-25 11:57:12 +01002246 struct pci_bus *child;
Thierry Redingd1523b52013-08-09 16:49:19 +02002247 int err;
2248
Lorenzo Pieralisi792abc62017-06-28 15:13:54 -05002249 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
Arnd Bergmann76f25412016-11-25 11:57:12 +01002250 if (!host)
Thierry Redingd1523b52013-08-09 16:49:19 +02002251 return -ENOMEM;
2252
Arnd Bergmann76f25412016-11-25 11:57:12 +01002253 pcie = pci_host_bridge_priv(host);
2254
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002255 pcie->soc = of_device_get_match_data(dev);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002256 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02002257 INIT_LIST_HEAD(&pcie->ports);
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002258 pcie->dev = dev;
Thierry Redingd1523b52013-08-09 16:49:19 +02002259
2260 err = tegra_pcie_parse_dt(pcie);
2261 if (err < 0)
2262 return err;
2263
Thierry Redingd1523b52013-08-09 16:49:19 +02002264 err = tegra_pcie_get_resources(pcie);
2265 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002266 dev_err(dev, "failed to request resources: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02002267 return err;
2268 }
2269
2270 err = tegra_pcie_enable_controller(pcie);
2271 if (err)
2272 goto put_resources;
2273
Arnd Bergmann76f25412016-11-25 11:57:12 +01002274 err = tegra_pcie_request_resources(pcie);
2275 if (err)
2276 goto put_resources;
2277
Thierry Redingd1523b52013-08-09 16:49:19 +02002278 /* setup the AFI address translations */
2279 tegra_pcie_setup_translations(pcie);
2280
2281 if (IS_ENABLED(CONFIG_PCI_MSI)) {
2282 err = tegra_pcie_enable_msi(pcie);
2283 if (err < 0) {
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002284 dev_err(dev, "failed to enable MSI support: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02002285 goto put_resources;
2286 }
2287 }
2288
Arnd Bergmann76f25412016-11-25 11:57:12 +01002289 tegra_pcie_enable_ports(pcie);
2290
2291 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
2292 host->busnr = pcie->busn.start;
2293 host->dev.parent = &pdev->dev;
2294 host->ops = &tegra_pcie_ops;
Lorenzo Pieralisidd5fcce2017-06-28 15:14:05 -05002295 host->map_irq = tegra_pcie_map_irq;
2296 host->swizzle_irq = pci_common_swizzle;
Arnd Bergmann76f25412016-11-25 11:57:12 +01002297
Lorenzo Pieralisicea9bc02017-06-28 15:13:55 -05002298 err = pci_scan_root_bus_bridge(host);
Thierry Redingd1523b52013-08-09 16:49:19 +02002299 if (err < 0) {
Arnd Bergmann76f25412016-11-25 11:57:12 +01002300 dev_err(dev, "failed to register host: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +02002301 goto disable_msi;
2302 }
2303
Arnd Bergmann76f25412016-11-25 11:57:12 +01002304 pci_bus_size_bridges(host->bus);
2305 pci_bus_assign_resources(host->bus);
2306
2307 list_for_each_entry(child, &host->bus->children, node)
2308 pcie_bus_configure_settings(child);
2309
2310 pci_bus_add_devices(host->bus);
2311
Thierry Reding2cb989f2014-07-22 12:30:46 -06002312 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2313 err = tegra_pcie_debugfs_init(pcie);
2314 if (err < 0)
Bjorn Helgaasa581fa92016-10-06 13:43:04 -05002315 dev_err(dev, "failed to setup debugfs: %d\n", err);
Thierry Reding2cb989f2014-07-22 12:30:46 -06002316 }
2317
Thierry Redingd1523b52013-08-09 16:49:19 +02002318 return 0;
2319
2320disable_msi:
2321 if (IS_ENABLED(CONFIG_PCI_MSI))
2322 tegra_pcie_disable_msi(pcie);
2323put_resources:
2324 tegra_pcie_put_resources(pcie);
2325 return err;
2326}
2327
Thierry Redingd1523b52013-08-09 16:49:19 +02002328static struct platform_driver tegra_pcie_driver = {
2329 .driver = {
2330 .name = "tegra-pcie",
Thierry Redingd1523b52013-08-09 16:49:19 +02002331 .of_match_table = tegra_pcie_of_match,
2332 .suppress_bind_attrs = true,
2333 },
2334 .probe = tegra_pcie_probe,
2335};
Paul Gortmakerad183272016-07-02 19:13:31 -04002336builtin_platform_driver(tegra_pcie_driver);