blob: 79fd92c969a971c0e8be65902d02220c1211cb3b [file] [log] [blame]
Thierry Redingd1523b52013-08-09 16:49:19 +02001/*
Jay Agarwal94716cd2013-08-09 16:49:24 +02002 * PCIe host controller driver for Tegra SoCs
Thierry Redingd1523b52013-08-09 16:49:19 +02003 *
4 * Copyright (c) 2010, CompuLab, Ltd.
5 * Author: Mike Rapoport <mike@compulab.co.il>
6 *
7 * Based on NVIDIA PCIe driver
8 * Copyright (c) 2008-2009, NVIDIA Corporation.
9 *
10 * Bits taken from arch/arm/mach-dove/pcie.c
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26
27#include <linux/clk.h>
Thierry Reding2cb989f2014-07-22 12:30:46 -060028#include <linux/debugfs.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020029#include <linux/delay.h>
30#include <linux/export.h>
31#include <linux/interrupt.h>
32#include <linux/irq.h>
33#include <linux/irqdomain.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/msi.h>
37#include <linux/of_address.h>
38#include <linux/of_pci.h>
39#include <linux/of_platform.h>
40#include <linux/pci.h>
41#include <linux/platform_device.h>
Stephen Warren3127a6b2013-11-06 15:56:58 -070042#include <linux/reset.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020043#include <linux/sizes.h>
44#include <linux/slab.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020045#include <linux/vmalloc.h>
46#include <linux/regulator/consumer.h>
47
Thierry Reding306a7f92014-07-17 13:17:24 +020048#include <soc/tegra/cpuidle.h>
Thierry Reding72323982014-07-11 13:19:06 +020049#include <soc/tegra/pmc.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020050
Thierry Redingd1523b52013-08-09 16:49:19 +020051#include <asm/mach/irq.h>
52#include <asm/mach/map.h>
53#include <asm/mach/pci.h>
54
55#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020056
57/* register definitions */
58
59#define AFI_AXI_BAR0_SZ 0x00
60#define AFI_AXI_BAR1_SZ 0x04
61#define AFI_AXI_BAR2_SZ 0x08
62#define AFI_AXI_BAR3_SZ 0x0c
63#define AFI_AXI_BAR4_SZ 0x10
64#define AFI_AXI_BAR5_SZ 0x14
65
66#define AFI_AXI_BAR0_START 0x18
67#define AFI_AXI_BAR1_START 0x1c
68#define AFI_AXI_BAR2_START 0x20
69#define AFI_AXI_BAR3_START 0x24
70#define AFI_AXI_BAR4_START 0x28
71#define AFI_AXI_BAR5_START 0x2c
72
73#define AFI_FPCI_BAR0 0x30
74#define AFI_FPCI_BAR1 0x34
75#define AFI_FPCI_BAR2 0x38
76#define AFI_FPCI_BAR3 0x3c
77#define AFI_FPCI_BAR4 0x40
78#define AFI_FPCI_BAR5 0x44
79
80#define AFI_CACHE_BAR0_SZ 0x48
81#define AFI_CACHE_BAR0_ST 0x4c
82#define AFI_CACHE_BAR1_SZ 0x50
83#define AFI_CACHE_BAR1_ST 0x54
84
85#define AFI_MSI_BAR_SZ 0x60
86#define AFI_MSI_FPCI_BAR_ST 0x64
87#define AFI_MSI_AXI_BAR_ST 0x68
88
89#define AFI_MSI_VEC0 0x6c
90#define AFI_MSI_VEC1 0x70
91#define AFI_MSI_VEC2 0x74
92#define AFI_MSI_VEC3 0x78
93#define AFI_MSI_VEC4 0x7c
94#define AFI_MSI_VEC5 0x80
95#define AFI_MSI_VEC6 0x84
96#define AFI_MSI_VEC7 0x88
97
98#define AFI_MSI_EN_VEC0 0x8c
99#define AFI_MSI_EN_VEC1 0x90
100#define AFI_MSI_EN_VEC2 0x94
101#define AFI_MSI_EN_VEC3 0x98
102#define AFI_MSI_EN_VEC4 0x9c
103#define AFI_MSI_EN_VEC5 0xa0
104#define AFI_MSI_EN_VEC6 0xa4
105#define AFI_MSI_EN_VEC7 0xa8
106
107#define AFI_CONFIGURATION 0xac
108#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
109
110#define AFI_FPCI_ERROR_MASKS 0xb0
111
112#define AFI_INTR_MASK 0xb4
113#define AFI_INTR_MASK_INT_MASK (1 << 0)
114#define AFI_INTR_MASK_MSI_MASK (1 << 8)
115
116#define AFI_INTR_CODE 0xb8
117#define AFI_INTR_CODE_MASK 0xf
118#define AFI_INTR_AXI_SLAVE_ERROR 1
119#define AFI_INTR_AXI_DECODE_ERROR 2
120#define AFI_INTR_TARGET_ABORT 3
121#define AFI_INTR_MASTER_ABORT 4
122#define AFI_INTR_INVALID_WRITE 5
123#define AFI_INTR_LEGACY 6
124#define AFI_INTR_FPCI_DECODE_ERROR 7
125
126#define AFI_INTR_SIGNATURE 0xbc
127#define AFI_UPPER_FPCI_ADDRESS 0xc0
128#define AFI_SM_INTR_ENABLE 0xc4
129#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
130#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
131#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
132#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
133#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
134#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
135#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
136#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
137
138#define AFI_AFI_INTR_ENABLE 0xc8
139#define AFI_INTR_EN_INI_SLVERR (1 << 0)
140#define AFI_INTR_EN_INI_DECERR (1 << 1)
141#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
142#define AFI_INTR_EN_TGT_DECERR (1 << 3)
143#define AFI_INTR_EN_TGT_WRERR (1 << 4)
144#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
145#define AFI_INTR_EN_AXI_DECERR (1 << 6)
146#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200147#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200148
149#define AFI_PCIE_CONFIG 0x0f8
150#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
151#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
152#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
153#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200154#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200155#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200156#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
157#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200158
159#define AFI_FUSE 0x104
160#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
161
162#define AFI_PEX0_CTRL 0x110
163#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200164#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200165#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200166#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200167#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
168
Jay Agarwal94716cd2013-08-09 16:49:24 +0200169#define AFI_PEXBIAS_CTRL_0 0x168
170
Thierry Redingd1523b52013-08-09 16:49:19 +0200171#define RP_VEND_XP 0x00000F00
172#define RP_VEND_XP_DL_UP (1 << 30)
173
174#define RP_LINK_CONTROL_STATUS 0x00000090
175#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
176#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
177
178#define PADS_CTL_SEL 0x0000009C
179
180#define PADS_CTL 0x000000A0
181#define PADS_CTL_IDDQ_1L (1 << 0)
182#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
183#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
184
Jay Agarwal94716cd2013-08-09 16:49:24 +0200185#define PADS_PLL_CTL_TEGRA20 0x000000B8
186#define PADS_PLL_CTL_TEGRA30 0x000000B4
Thierry Redingd1523b52013-08-09 16:49:19 +0200187#define PADS_PLL_CTL_RST_B4SM (1 << 1)
188#define PADS_PLL_CTL_LOCKDET (1 << 8)
189#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
190#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
191#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
192#define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
193#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
194#define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
195#define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200196#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
197
198#define PADS_REFCLK_CFG0 0x000000C8
199#define PADS_REFCLK_CFG1 0x000000CC
Thierry Redingd1523b52013-08-09 16:49:19 +0200200
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200201/*
202 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
203 * entries, one entry per PCIe port. These field definitions and desired
204 * values aren't in the TRM, but do come from NVIDIA.
205 */
206#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
207#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
208#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
209#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
210
211/* Default value provided by HW engineering is 0xfa5c */
212#define PADS_REFCLK_CFG_VALUE \
213 ( \
214 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
215 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
216 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
217 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
218 )
219
Thierry Redingd1523b52013-08-09 16:49:19 +0200220struct tegra_msi {
221 struct msi_chip chip;
222 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
223 struct irq_domain *domain;
224 unsigned long pages;
225 struct mutex lock;
226 int irq;
227};
228
Jay Agarwal94716cd2013-08-09 16:49:24 +0200229/* used to differentiate between Tegra SoC generations */
230struct tegra_pcie_soc_data {
231 unsigned int num_ports;
232 unsigned int msi_base_shift;
233 u32 pads_pll_ctl;
234 u32 tx_ref_sel;
235 bool has_pex_clkreq_en;
236 bool has_pex_bias_ctrl;
237 bool has_intr_prsnt_sense;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200238 bool has_cml_clk;
239};
240
Thierry Redingd1523b52013-08-09 16:49:19 +0200241static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
242{
243 return container_of(chip, struct tegra_msi, chip);
244}
245
246struct tegra_pcie {
247 struct device *dev;
248
249 void __iomem *pads;
250 void __iomem *afi;
251 int irq;
252
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700253 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200254 struct resource *cs;
255
Thierry Reding41534e52014-08-01 14:15:11 +0200256 struct resource all;
Thierry Redingd1523b52013-08-09 16:49:19 +0200257 struct resource io;
258 struct resource mem;
259 struct resource prefetch;
260 struct resource busn;
261
262 struct clk *pex_clk;
263 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200264 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200265 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200266
Stephen Warren3127a6b2013-11-06 15:56:58 -0700267 struct reset_control *pex_rst;
268 struct reset_control *afi_rst;
269 struct reset_control *pcie_xrst;
270
Thierry Redingd1523b52013-08-09 16:49:19 +0200271 struct tegra_msi msi;
272
273 struct list_head ports;
274 unsigned int num_ports;
275 u32 xbar_config;
276
Thierry Reding077fb152014-05-28 16:49:13 +0200277 struct regulator_bulk_data *supplies;
278 unsigned int num_supplies;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200279
280 const struct tegra_pcie_soc_data *soc_data;
Thierry Reding2cb989f2014-07-22 12:30:46 -0600281 struct dentry *debugfs;
Thierry Redingd1523b52013-08-09 16:49:19 +0200282};
283
284struct tegra_pcie_port {
285 struct tegra_pcie *pcie;
286 struct list_head list;
287 struct resource regs;
288 void __iomem *base;
289 unsigned int index;
290 unsigned int lanes;
291};
292
293struct tegra_pcie_bus {
294 struct vm_struct *area;
295 struct list_head list;
296 unsigned int nr;
297};
298
299static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
300{
301 return sys->private_data;
302}
303
304static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
305 unsigned long offset)
306{
307 writel(value, pcie->afi + offset);
308}
309
310static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
311{
312 return readl(pcie->afi + offset);
313}
314
315static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
316 unsigned long offset)
317{
318 writel(value, pcie->pads + offset);
319}
320
321static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
322{
323 return readl(pcie->pads + offset);
324}
325
326/*
327 * The configuration space mapping on Tegra is somewhat similar to the ECAM
328 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
329 * register accesses are mapped:
330 *
331 * [27:24] extended register number
332 * [23:16] bus number
333 * [15:11] device number
334 * [10: 8] function number
335 * [ 7: 0] register number
336 *
337 * Mapping the whole extended configuration space would require 256 MiB of
338 * virtual address space, only a small part of which will actually be used.
339 * To work around this, a 1 MiB of virtual addresses are allocated per bus
340 * when the bus is first accessed. When the physical range is mapped, the
341 * the bus number bits are hidden so that the extended register number bits
342 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
343 *
344 * [19:16] extended register number
345 * [15:11] device number
346 * [10: 8] function number
347 * [ 7: 0] register number
348 *
349 * This is achieved by stitching together 16 chunks of 64 KiB of physical
350 * address space via the MMU.
351 */
352static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
353{
354 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
355 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
356}
357
358static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
359 unsigned int busnr)
360{
361 pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
362 L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
363 phys_addr_t cs = pcie->cs->start;
364 struct tegra_pcie_bus *bus;
365 unsigned int i;
366 int err;
367
368 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
369 if (!bus)
370 return ERR_PTR(-ENOMEM);
371
372 INIT_LIST_HEAD(&bus->list);
373 bus->nr = busnr;
374
375 /* allocate 1 MiB of virtual addresses */
376 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
377 if (!bus->area) {
378 err = -ENOMEM;
379 goto free;
380 }
381
382 /* map each of the 16 chunks of 64 KiB each */
383 for (i = 0; i < 16; i++) {
384 unsigned long virt = (unsigned long)bus->area->addr +
385 i * SZ_64K;
Peter Daifuku8d417942014-08-26 17:11:36 +0200386 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
Thierry Redingd1523b52013-08-09 16:49:19 +0200387
388 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
389 if (err < 0) {
390 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
391 err);
392 goto unmap;
393 }
394 }
395
396 return bus;
397
398unmap:
399 vunmap(bus->area->addr);
400free:
401 kfree(bus);
402 return ERR_PTR(err);
403}
404
405/*
406 * Look up a virtual address mapping for the specified bus number. If no such
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700407 * mapping exists, try to create one.
Thierry Redingd1523b52013-08-09 16:49:19 +0200408 */
409static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
410 unsigned int busnr)
411{
412 struct tegra_pcie_bus *bus;
413
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700414 list_for_each_entry(bus, &pcie->buses, list)
Thierry Redingd1523b52013-08-09 16:49:19 +0200415 if (bus->nr == busnr)
Jingoo Han1e652492013-09-25 16:40:54 -0600416 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200417
418 bus = tegra_pcie_bus_alloc(pcie, busnr);
419 if (IS_ERR(bus))
420 return NULL;
421
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700422 list_add_tail(&bus->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200423
Jingoo Han1e652492013-09-25 16:40:54 -0600424 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200425}
426
427static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
428 unsigned int devfn,
429 int where)
430{
431 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
432 void __iomem *addr = NULL;
433
434 if (bus->number == 0) {
435 unsigned int slot = PCI_SLOT(devfn);
436 struct tegra_pcie_port *port;
437
438 list_for_each_entry(port, &pcie->ports, list) {
439 if (port->index + 1 == slot) {
440 addr = port->base + (where & ~3);
441 break;
442 }
443 }
444 } else {
445 addr = tegra_pcie_bus_map(pcie, bus->number);
446 if (!addr) {
447 dev_err(pcie->dev,
448 "failed to map cfg. space for bus %u\n",
449 bus->number);
450 return NULL;
451 }
452
453 addr += tegra_pcie_conf_offset(devfn, where);
454 }
455
456 return addr;
457}
458
459static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
460 int where, int size, u32 *value)
461{
462 void __iomem *addr;
463
464 addr = tegra_pcie_conf_address(bus, devfn, where);
465 if (!addr) {
466 *value = 0xffffffff;
467 return PCIBIOS_DEVICE_NOT_FOUND;
468 }
469
470 *value = readl(addr);
471
472 if (size == 1)
473 *value = (*value >> (8 * (where & 3))) & 0xff;
474 else if (size == 2)
475 *value = (*value >> (8 * (where & 3))) & 0xffff;
476
477 return PCIBIOS_SUCCESSFUL;
478}
479
480static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
481 int where, int size, u32 value)
482{
483 void __iomem *addr;
484 u32 mask, tmp;
485
486 addr = tegra_pcie_conf_address(bus, devfn, where);
487 if (!addr)
488 return PCIBIOS_DEVICE_NOT_FOUND;
489
490 if (size == 4) {
491 writel(value, addr);
492 return PCIBIOS_SUCCESSFUL;
493 }
494
495 if (size == 2)
496 mask = ~(0xffff << ((where & 0x3) * 8));
497 else if (size == 1)
498 mask = ~(0xff << ((where & 0x3) * 8));
499 else
500 return PCIBIOS_BAD_REGISTER_NUMBER;
501
502 tmp = readl(addr) & mask;
503 tmp |= value << ((where & 0x3) * 8);
504 writel(tmp, addr);
505
506 return PCIBIOS_SUCCESSFUL;
507}
508
509static struct pci_ops tegra_pcie_ops = {
510 .read = tegra_pcie_read_conf,
511 .write = tegra_pcie_write_conf,
512};
513
514static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
515{
516 unsigned long ret = 0;
517
518 switch (port->index) {
519 case 0:
520 ret = AFI_PEX0_CTRL;
521 break;
522
523 case 1:
524 ret = AFI_PEX1_CTRL;
525 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200526
527 case 2:
528 ret = AFI_PEX2_CTRL;
529 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200530 }
531
532 return ret;
533}
534
535static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
536{
537 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
538 unsigned long value;
539
540 /* pulse reset signal */
541 value = afi_readl(port->pcie, ctrl);
542 value &= ~AFI_PEX_CTRL_RST;
543 afi_writel(port->pcie, value, ctrl);
544
545 usleep_range(1000, 2000);
546
547 value = afi_readl(port->pcie, ctrl);
548 value |= AFI_PEX_CTRL_RST;
549 afi_writel(port->pcie, value, ctrl);
550}
551
552static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
553{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200554 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200555 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
556 unsigned long value;
557
558 /* enable reference clock */
559 value = afi_readl(port->pcie, ctrl);
560 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200561
562 if (soc->has_pex_clkreq_en)
563 value |= AFI_PEX_CTRL_CLKREQ_EN;
564
Thierry Redingd1523b52013-08-09 16:49:19 +0200565 afi_writel(port->pcie, value, ctrl);
566
567 tegra_pcie_port_reset(port);
568}
569
570static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
571{
Thierry Reding0d20d622014-08-26 17:11:35 +0200572 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200573 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
574 unsigned long value;
575
576 /* assert port reset */
577 value = afi_readl(port->pcie, ctrl);
578 value &= ~AFI_PEX_CTRL_RST;
579 afi_writel(port->pcie, value, ctrl);
580
581 /* disable reference clock */
582 value = afi_readl(port->pcie, ctrl);
Thierry Reding0d20d622014-08-26 17:11:35 +0200583
584 if (soc->has_pex_clkreq_en)
585 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
586
Thierry Redingd1523b52013-08-09 16:49:19 +0200587 value &= ~AFI_PEX_CTRL_REFCLK_EN;
588 afi_writel(port->pcie, value, ctrl);
589}
590
591static void tegra_pcie_port_free(struct tegra_pcie_port *port)
592{
593 struct tegra_pcie *pcie = port->pcie;
594
595 devm_iounmap(pcie->dev, port->base);
596 devm_release_mem_region(pcie->dev, port->regs.start,
597 resource_size(&port->regs));
598 list_del(&port->list);
599 devm_kfree(pcie->dev, port);
600}
601
602static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
603{
604 u16 reg;
605
606 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
607 pci_read_config_word(dev, PCI_COMMAND, &reg);
608 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
609 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
610 pci_write_config_word(dev, PCI_COMMAND, reg);
611 }
612}
613DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
614
615/* Tegra PCIE root complex wrongly reports device class */
616static void tegra_pcie_fixup_class(struct pci_dev *dev)
617{
618 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
619}
620DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
621DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200622DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
623DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200624
625/* Tegra PCIE requires relaxed ordering */
626static void tegra_pcie_relax_enable(struct pci_dev *dev)
627{
628 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
629}
630DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
631
632static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
633{
634 struct tegra_pcie *pcie = sys_to_pcie(sys);
Thierry Reding41534e52014-08-01 14:15:11 +0200635 int err;
636
637 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->mem);
638 if (err < 0)
639 return err;
640
641 err = devm_request_resource(pcie->dev, &pcie->all, &pcie->prefetch);
642 if (err)
643 return err;
Thierry Redingd1523b52013-08-09 16:49:19 +0200644
645 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
646 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
647 sys->mem_offset);
648 pci_add_resource(&sys->resources, &pcie->busn);
649
650 pci_ioremap_io(nr * SZ_64K, pcie->io.start);
651
652 return 1;
653}
654
655static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
656{
657 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
Lucas Stachf5d33522014-04-16 10:24:32 -0600658 int irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200659
Stephen Warrenb4f17372013-05-06 14:19:19 -0600660 tegra_cpuidle_pcie_irqs_in_use();
661
Lucas Stachf5d33522014-04-16 10:24:32 -0600662 irq = of_irq_parse_and_map_pci(pdev, slot, pin);
663 if (!irq)
664 irq = pcie->irq;
665
666 return irq;
Thierry Redingd1523b52013-08-09 16:49:19 +0200667}
668
669static void tegra_pcie_add_bus(struct pci_bus *bus)
670{
671 if (IS_ENABLED(CONFIG_PCI_MSI)) {
672 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
673
674 bus->msi = &pcie->msi.chip;
675 }
676}
677
678static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
679{
680 struct tegra_pcie *pcie = sys_to_pcie(sys);
681 struct pci_bus *bus;
682
683 bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
684 &sys->resources);
685 if (!bus)
686 return NULL;
687
688 pci_scan_child_bus(bus);
689
690 return bus;
691}
692
693static irqreturn_t tegra_pcie_isr(int irq, void *arg)
694{
695 const char *err_msg[] = {
696 "Unknown",
697 "AXI slave error",
698 "AXI decode error",
699 "Target abort",
700 "Master abort",
701 "Invalid write",
702 "Response decoding error",
703 "AXI response decoding error",
704 "Transaction timeout",
705 };
706 struct tegra_pcie *pcie = arg;
707 u32 code, signature;
708
709 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
710 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
711 afi_writel(pcie, 0, AFI_INTR_CODE);
712
713 if (code == AFI_INTR_LEGACY)
714 return IRQ_NONE;
715
716 if (code >= ARRAY_SIZE(err_msg))
717 code = 0;
718
719 /*
720 * do not pollute kernel log with master abort reports since they
721 * happen a lot during enumeration
722 */
723 if (code == AFI_INTR_MASTER_ABORT)
724 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
725 signature);
726 else
727 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
728 signature);
729
730 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
731 code == AFI_INTR_FPCI_DECODE_ERROR) {
732 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
733 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
734
735 if (code == AFI_INTR_MASTER_ABORT)
736 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
737 else
738 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
739 }
740
741 return IRQ_HANDLED;
742}
743
744/*
745 * FPCI map is as follows:
746 * - 0xfdfc000000: I/O space
747 * - 0xfdfe000000: type 0 configuration space
748 * - 0xfdff000000: type 1 configuration space
749 * - 0xfe00000000: type 0 extended configuration space
750 * - 0xfe10000000: type 1 extended configuration space
751 */
752static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
753{
754 u32 fpci_bar, size, axi_address;
755
756 /* Bar 0: type 1 extended configuration space */
757 fpci_bar = 0xfe100000;
758 size = resource_size(pcie->cs);
759 axi_address = pcie->cs->start;
760 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
761 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
762 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
763
764 /* Bar 1: downstream IO bar */
765 fpci_bar = 0xfdfc0000;
766 size = resource_size(&pcie->io);
767 axi_address = pcie->io.start;
768 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
769 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
770 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
771
772 /* Bar 2: prefetchable memory BAR */
773 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
774 size = resource_size(&pcie->prefetch);
775 axi_address = pcie->prefetch.start;
776 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
777 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
778 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
779
780 /* Bar 3: non prefetchable memory BAR */
781 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
782 size = resource_size(&pcie->mem);
783 axi_address = pcie->mem.start;
784 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
785 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
786 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
787
788 /* NULL out the remaining BARs as they are not used */
789 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
790 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
791 afi_writel(pcie, 0, AFI_FPCI_BAR4);
792
793 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
794 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
795 afi_writel(pcie, 0, AFI_FPCI_BAR5);
796
797 /* map all upstream transactions as uncached */
798 afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
799 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
800 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
801 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
802
803 /* MSI translations are setup only when needed */
804 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
805 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
806 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
807 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
808}
809
810static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
811{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200812 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200813 struct tegra_pcie_port *port;
814 unsigned int timeout;
815 unsigned long value;
816
Jay Agarwal94716cd2013-08-09 16:49:24 +0200817 /* power down PCIe slot clock bias pad */
818 if (soc->has_pex_bias_ctrl)
819 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
820
Thierry Redingd1523b52013-08-09 16:49:19 +0200821 /* configure mode and disable all ports */
822 value = afi_readl(pcie, AFI_PCIE_CONFIG);
823 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
824 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
825
826 list_for_each_entry(port, &pcie->ports, list)
827 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
828
829 afi_writel(pcie, value, AFI_PCIE_CONFIG);
830
831 value = afi_readl(pcie, AFI_FUSE);
Eric Brower17bd86c2013-11-18 14:55:06 -0800832 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
Thierry Redingd1523b52013-08-09 16:49:19 +0200833 afi_writel(pcie, value, AFI_FUSE);
834
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700835 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200836 pads_writel(pcie, 0x0, PADS_CTL_SEL);
837
838 /* override IDDQ to 1 on all 4 lanes */
839 value = pads_readl(pcie, PADS_CTL);
840 value |= PADS_CTL_IDDQ_1L;
841 pads_writel(pcie, value, PADS_CTL);
842
843 /*
844 * Set up PHY PLL inputs select PLLE output as refclock,
845 * set TX ref sel to div10 (not div5).
846 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200847 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200848 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200849 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
850 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200851
Eric Yuenec732762014-08-26 17:11:37 +0200852 /* reset PLL */
853 value = pads_readl(pcie, soc->pads_pll_ctl);
854 value &= ~PADS_PLL_CTL_RST_B4SM;
855 pads_writel(pcie, value, soc->pads_pll_ctl);
856
857 usleep_range(20, 100);
858
Thierry Redingd1523b52013-08-09 16:49:19 +0200859 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200860 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200861 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200862 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200863
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200864 /* Configure the reference clock driver */
865 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
866 pads_writel(pcie, value, PADS_REFCLK_CFG0);
867 if (soc->num_ports > 2)
868 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200869
870 /* wait for the PLL to lock */
871 timeout = 300;
872 do {
Jay Agarwal94716cd2013-08-09 16:49:24 +0200873 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200874 usleep_range(1000, 2000);
875 if (--timeout == 0) {
876 pr_err("Tegra PCIe error: timeout waiting for PLL\n");
877 return -EBUSY;
878 }
879 } while (!(value & PADS_PLL_CTL_LOCKDET));
880
881 /* turn off IDDQ override */
882 value = pads_readl(pcie, PADS_CTL);
883 value &= ~PADS_CTL_IDDQ_1L;
884 pads_writel(pcie, value, PADS_CTL);
885
886 /* enable TX/RX data */
887 value = pads_readl(pcie, PADS_CTL);
888 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
889 pads_writel(pcie, value, PADS_CTL);
890
891 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -0700892 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200893
894 /* finally enable PCIe */
895 value = afi_readl(pcie, AFI_CONFIGURATION);
896 value |= AFI_CONFIGURATION_EN_FPCI;
897 afi_writel(pcie, value, AFI_CONFIGURATION);
898
899 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
900 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
901 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200902
903 if (soc->has_intr_prsnt_sense)
904 value |= AFI_INTR_EN_PRSNT_SENSE;
905
Thierry Redingd1523b52013-08-09 16:49:19 +0200906 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
907 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
908
909 /* don't enable MSI for now, only when needed */
910 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
911
912 /* disable all exceptions */
913 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
914
915 return 0;
916}
917
918static void tegra_pcie_power_off(struct tegra_pcie *pcie)
919{
920 int err;
921
922 /* TODO: disable and unprepare clocks? */
923
Stephen Warren3127a6b2013-11-06 15:56:58 -0700924 reset_control_assert(pcie->pcie_xrst);
925 reset_control_assert(pcie->afi_rst);
926 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200927
928 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
929
Thierry Reding077fb152014-05-28 16:49:13 +0200930 err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
Thierry Redingd1523b52013-08-09 16:49:19 +0200931 if (err < 0)
Thierry Reding077fb152014-05-28 16:49:13 +0200932 dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
Thierry Redingd1523b52013-08-09 16:49:19 +0200933}
934
935static int tegra_pcie_power_on(struct tegra_pcie *pcie)
936{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200937 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200938 int err;
939
Stephen Warren3127a6b2013-11-06 15:56:58 -0700940 reset_control_assert(pcie->pcie_xrst);
941 reset_control_assert(pcie->afi_rst);
942 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200943
944 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
945
946 /* enable regulators */
Thierry Reding077fb152014-05-28 16:49:13 +0200947 err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
948 if (err < 0)
949 dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200950
Thierry Redingd1523b52013-08-09 16:49:19 +0200951 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
Stephen Warren80b28792013-11-06 15:45:46 -0700952 pcie->pex_clk,
953 pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200954 if (err) {
955 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
956 return err;
957 }
958
Stephen Warren3127a6b2013-11-06 15:56:58 -0700959 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200960
961 err = clk_prepare_enable(pcie->afi_clk);
962 if (err < 0) {
963 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
964 return err;
965 }
966
Jay Agarwal94716cd2013-08-09 16:49:24 +0200967 if (soc->has_cml_clk) {
968 err = clk_prepare_enable(pcie->cml_clk);
969 if (err < 0) {
970 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
971 err);
972 return err;
973 }
974 }
975
Thierry Redingd1523b52013-08-09 16:49:19 +0200976 err = clk_prepare_enable(pcie->pll_e);
977 if (err < 0) {
978 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
979 return err;
980 }
981
982 return 0;
983}
984
985static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
986{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200987 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
988
Thierry Redingd1523b52013-08-09 16:49:19 +0200989 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
990 if (IS_ERR(pcie->pex_clk))
991 return PTR_ERR(pcie->pex_clk);
992
993 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
994 if (IS_ERR(pcie->afi_clk))
995 return PTR_ERR(pcie->afi_clk);
996
Thierry Redingd1523b52013-08-09 16:49:19 +0200997 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
998 if (IS_ERR(pcie->pll_e))
999 return PTR_ERR(pcie->pll_e);
1000
Jay Agarwal94716cd2013-08-09 16:49:24 +02001001 if (soc->has_cml_clk) {
1002 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1003 if (IS_ERR(pcie->cml_clk))
1004 return PTR_ERR(pcie->cml_clk);
1005 }
1006
Thierry Redingd1523b52013-08-09 16:49:19 +02001007 return 0;
1008}
1009
Stephen Warren3127a6b2013-11-06 15:56:58 -07001010static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1011{
1012 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1013 if (IS_ERR(pcie->pex_rst))
1014 return PTR_ERR(pcie->pex_rst);
1015
1016 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1017 if (IS_ERR(pcie->afi_rst))
1018 return PTR_ERR(pcie->afi_rst);
1019
1020 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1021 if (IS_ERR(pcie->pcie_xrst))
1022 return PTR_ERR(pcie->pcie_xrst);
1023
1024 return 0;
1025}
1026
Thierry Redingd1523b52013-08-09 16:49:19 +02001027static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1028{
1029 struct platform_device *pdev = to_platform_device(pcie->dev);
1030 struct resource *pads, *afi, *res;
1031 int err;
1032
1033 err = tegra_pcie_clocks_get(pcie);
1034 if (err) {
1035 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1036 return err;
1037 }
1038
Stephen Warren3127a6b2013-11-06 15:56:58 -07001039 err = tegra_pcie_resets_get(pcie);
1040 if (err) {
1041 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1042 return err;
1043 }
1044
Thierry Redingd1523b52013-08-09 16:49:19 +02001045 err = tegra_pcie_power_on(pcie);
1046 if (err) {
1047 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1048 return err;
1049 }
1050
Thierry Redingd1523b52013-08-09 16:49:19 +02001051 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001052 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1053 if (IS_ERR(pcie->pads)) {
1054 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001055 goto poweroff;
1056 }
1057
1058 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001059 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1060 if (IS_ERR(pcie->afi)) {
1061 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001062 goto poweroff;
1063 }
1064
Julia Lawalldc05ee32013-08-26 11:11:09 +02001065 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001066 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1067 if (!res) {
1068 err = -EADDRNOTAVAIL;
1069 goto poweroff;
1070 }
1071
1072 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1073 resource_size(res), res->name);
1074 if (!pcie->cs) {
1075 err = -EADDRNOTAVAIL;
1076 goto poweroff;
1077 }
1078
1079 /* request interrupt */
1080 err = platform_get_irq_byname(pdev, "intr");
1081 if (err < 0) {
1082 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1083 goto poweroff;
1084 }
1085
1086 pcie->irq = err;
1087
1088 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1089 if (err) {
1090 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1091 goto poweroff;
1092 }
1093
1094 return 0;
1095
1096poweroff:
1097 tegra_pcie_power_off(pcie);
1098 return err;
1099}
1100
1101static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1102{
1103 if (pcie->irq > 0)
1104 free_irq(pcie->irq, pcie);
1105
1106 tegra_pcie_power_off(pcie);
1107 return 0;
1108}
1109
1110static int tegra_msi_alloc(struct tegra_msi *chip)
1111{
1112 int msi;
1113
1114 mutex_lock(&chip->lock);
1115
1116 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1117 if (msi < INT_PCI_MSI_NR)
1118 set_bit(msi, chip->used);
1119 else
1120 msi = -ENOSPC;
1121
1122 mutex_unlock(&chip->lock);
1123
1124 return msi;
1125}
1126
1127static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1128{
1129 struct device *dev = chip->chip.dev;
1130
1131 mutex_lock(&chip->lock);
1132
1133 if (!test_bit(irq, chip->used))
1134 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1135 else
1136 clear_bit(irq, chip->used);
1137
1138 mutex_unlock(&chip->lock);
1139}
1140
1141static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1142{
1143 struct tegra_pcie *pcie = data;
1144 struct tegra_msi *msi = &pcie->msi;
1145 unsigned int i, processed = 0;
1146
1147 for (i = 0; i < 8; i++) {
1148 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1149
1150 while (reg) {
1151 unsigned int offset = find_first_bit(&reg, 32);
1152 unsigned int index = i * 32 + offset;
1153 unsigned int irq;
1154
1155 /* clear the interrupt */
1156 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1157
1158 irq = irq_find_mapping(msi->domain, index);
1159 if (irq) {
1160 if (test_bit(index, msi->used))
1161 generic_handle_irq(irq);
1162 else
1163 dev_info(pcie->dev, "unhandled MSI\n");
1164 } else {
1165 /*
1166 * that's weird who triggered this?
1167 * just clear it
1168 */
1169 dev_info(pcie->dev, "unexpected MSI\n");
1170 }
1171
1172 /* see if there's any more pending in this vector */
1173 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1174
1175 processed++;
1176 }
1177 }
1178
1179 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1180}
1181
1182static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
1183 struct msi_desc *desc)
1184{
1185 struct tegra_msi *msi = to_tegra_msi(chip);
1186 struct msi_msg msg;
1187 unsigned int irq;
1188 int hwirq;
1189
1190 hwirq = tegra_msi_alloc(msi);
1191 if (hwirq < 0)
1192 return hwirq;
1193
1194 irq = irq_create_mapping(msi->domain, hwirq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001195 if (!irq) {
1196 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001197 return -EINVAL;
Jisheng Zhang019fa462014-07-29 09:33:30 +08001198 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001199
1200 irq_set_msi_desc(irq, desc);
1201
1202 msg.address_lo = virt_to_phys((void *)msi->pages);
1203 /* 32 bit address only */
1204 msg.address_hi = 0;
1205 msg.data = hwirq;
1206
1207 write_msi_msg(irq, &msg);
1208
1209 return 0;
1210}
1211
1212static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
1213{
1214 struct tegra_msi *msi = to_tegra_msi(chip);
1215 struct irq_data *d = irq_get_irq_data(irq);
Jisheng Zhang019fa462014-07-29 09:33:30 +08001216 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Thierry Redingd1523b52013-08-09 16:49:19 +02001217
Jisheng Zhang019fa462014-07-29 09:33:30 +08001218 irq_dispose_mapping(irq);
1219 tegra_msi_free(msi, hwirq);
Thierry Redingd1523b52013-08-09 16:49:19 +02001220}
1221
1222static struct irq_chip tegra_msi_irq_chip = {
1223 .name = "Tegra PCIe MSI",
1224 .irq_enable = unmask_msi_irq,
1225 .irq_disable = mask_msi_irq,
1226 .irq_mask = mask_msi_irq,
1227 .irq_unmask = unmask_msi_irq,
1228};
1229
1230static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1231 irq_hw_number_t hwirq)
1232{
1233 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1234 irq_set_chip_data(irq, domain->host_data);
1235 set_irq_flags(irq, IRQF_VALID);
1236
Stephen Warrenb4f17372013-05-06 14:19:19 -06001237 tegra_cpuidle_pcie_irqs_in_use();
1238
Thierry Redingd1523b52013-08-09 16:49:19 +02001239 return 0;
1240}
1241
1242static const struct irq_domain_ops msi_domain_ops = {
1243 .map = tegra_msi_map,
1244};
1245
1246static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1247{
1248 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001249 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001250 struct tegra_msi *msi = &pcie->msi;
1251 unsigned long base;
1252 int err;
1253 u32 reg;
1254
1255 mutex_init(&msi->lock);
1256
1257 msi->chip.dev = pcie->dev;
1258 msi->chip.setup_irq = tegra_msi_setup_irq;
1259 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1260
1261 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1262 &msi_domain_ops, &msi->chip);
1263 if (!msi->domain) {
1264 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1265 return -ENOMEM;
1266 }
1267
1268 err = platform_get_irq_byname(pdev, "msi");
1269 if (err < 0) {
1270 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1271 goto err;
1272 }
1273
1274 msi->irq = err;
1275
1276 err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
1277 tegra_msi_irq_chip.name, pcie);
1278 if (err < 0) {
1279 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1280 goto err;
1281 }
1282
1283 /* setup AFI/FPCI range */
1284 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1285 base = virt_to_phys((void *)msi->pages);
1286
Jay Agarwal94716cd2013-08-09 16:49:24 +02001287 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001288 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1289 /* this register is in 4K increments */
1290 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1291
1292 /* enable all MSI vectors */
1293 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1294 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1295 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1296 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1297 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1298 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1299 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1300 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1301
1302 /* and unmask the MSI interrupt */
1303 reg = afi_readl(pcie, AFI_INTR_MASK);
1304 reg |= AFI_INTR_MASK_MSI_MASK;
1305 afi_writel(pcie, reg, AFI_INTR_MASK);
1306
1307 return 0;
1308
1309err:
1310 irq_domain_remove(msi->domain);
1311 return err;
1312}
1313
1314static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1315{
1316 struct tegra_msi *msi = &pcie->msi;
1317 unsigned int i, irq;
1318 u32 value;
1319
1320 /* mask the MSI interrupt */
1321 value = afi_readl(pcie, AFI_INTR_MASK);
1322 value &= ~AFI_INTR_MASK_MSI_MASK;
1323 afi_writel(pcie, value, AFI_INTR_MASK);
1324
1325 /* disable all MSI vectors */
1326 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1327 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1328 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1329 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1330 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1331 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1332 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1333 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1334
1335 free_pages(msi->pages, 0);
1336
1337 if (msi->irq > 0)
1338 free_irq(msi->irq, pcie);
1339
1340 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1341 irq = irq_find_mapping(msi->domain, i);
1342 if (irq > 0)
1343 irq_dispose_mapping(irq);
1344 }
1345
1346 irq_domain_remove(msi->domain);
1347
1348 return 0;
1349}
1350
1351static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1352 u32 *xbar)
1353{
1354 struct device_node *np = pcie->dev->of_node;
1355
Jay Agarwal94716cd2013-08-09 16:49:24 +02001356 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1357 switch (lanes) {
1358 case 0x00000204:
1359 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1360 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1361 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001362
Jay Agarwal94716cd2013-08-09 16:49:24 +02001363 case 0x00020202:
1364 dev_info(pcie->dev, "2x3 configuration\n");
1365 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1366 return 0;
1367
1368 case 0x00010104:
1369 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1370 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1371 return 0;
1372 }
1373 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1374 switch (lanes) {
1375 case 0x00000004:
1376 dev_info(pcie->dev, "single-mode configuration\n");
1377 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1378 return 0;
1379
1380 case 0x00000202:
1381 dev_info(pcie->dev, "dual-mode configuration\n");
1382 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1383 return 0;
1384 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001385 }
1386
1387 return -EINVAL;
1388}
1389
Thierry Reding077fb152014-05-28 16:49:13 +02001390/*
1391 * Check whether a given set of supplies is available in a device tree node.
1392 * This is used to check whether the new or the legacy device tree bindings
1393 * should be used.
1394 */
1395static bool of_regulator_bulk_available(struct device_node *np,
1396 struct regulator_bulk_data *supplies,
1397 unsigned int num_supplies)
1398{
1399 char property[32];
1400 unsigned int i;
1401
1402 for (i = 0; i < num_supplies; i++) {
1403 snprintf(property, 32, "%s-supply", supplies[i].supply);
1404
1405 if (of_find_property(np, property, NULL) == NULL)
1406 return false;
1407 }
1408
1409 return true;
1410}
1411
1412/*
1413 * Old versions of the device tree binding for this device used a set of power
1414 * supplies that didn't match the hardware inputs. This happened to work for a
1415 * number of cases but is not future proof. However to preserve backwards-
1416 * compatibility with old device trees, this function will try to use the old
1417 * set of supplies.
1418 */
1419static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1420{
1421 struct device_node *np = pcie->dev->of_node;
1422
1423 if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1424 pcie->num_supplies = 3;
1425 else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1426 pcie->num_supplies = 2;
1427
1428 if (pcie->num_supplies == 0) {
1429 dev_err(pcie->dev, "device %s not supported in legacy mode\n",
1430 np->full_name);
1431 return -ENODEV;
1432 }
1433
1434 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1435 sizeof(*pcie->supplies),
1436 GFP_KERNEL);
1437 if (!pcie->supplies)
1438 return -ENOMEM;
1439
1440 pcie->supplies[0].supply = "pex-clk";
1441 pcie->supplies[1].supply = "vdd";
1442
1443 if (pcie->num_supplies > 2)
1444 pcie->supplies[2].supply = "avdd";
1445
1446 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1447 pcie->supplies);
1448}
1449
1450/*
1451 * Obtains the list of regulators required for a particular generation of the
1452 * IP block.
1453 *
1454 * This would've been nice to do simply by providing static tables for use
1455 * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1456 * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1457 * and either seems to be optional depending on which ports are being used.
1458 */
1459static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1460{
1461 struct device_node *np = pcie->dev->of_node;
1462 unsigned int i = 0;
1463
1464 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1465 bool need_pexa = false, need_pexb = false;
1466
1467 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1468 if (lane_mask & 0x0f)
1469 need_pexa = true;
1470
1471 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1472 if (lane_mask & 0x30)
1473 need_pexb = true;
1474
1475 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1476 (need_pexb ? 2 : 0);
1477
1478 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1479 sizeof(*pcie->supplies),
1480 GFP_KERNEL);
1481 if (!pcie->supplies)
1482 return -ENOMEM;
1483
1484 pcie->supplies[i++].supply = "avdd-pex-pll";
1485 pcie->supplies[i++].supply = "hvdd-pex";
1486 pcie->supplies[i++].supply = "vddio-pex-ctl";
1487 pcie->supplies[i++].supply = "avdd-plle";
1488
1489 if (need_pexa) {
1490 pcie->supplies[i++].supply = "avdd-pexa";
1491 pcie->supplies[i++].supply = "vdd-pexa";
1492 }
1493
1494 if (need_pexb) {
1495 pcie->supplies[i++].supply = "avdd-pexb";
1496 pcie->supplies[i++].supply = "vdd-pexb";
1497 }
1498 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1499 pcie->num_supplies = 5;
1500
1501 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1502 sizeof(*pcie->supplies),
1503 GFP_KERNEL);
1504 if (!pcie->supplies)
1505 return -ENOMEM;
1506
1507 pcie->supplies[0].supply = "avdd-pex";
1508 pcie->supplies[1].supply = "vdd-pex";
1509 pcie->supplies[2].supply = "avdd-pex-pll";
1510 pcie->supplies[3].supply = "avdd-plle";
1511 pcie->supplies[4].supply = "vddio-pex-clk";
1512 }
1513
1514 if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
1515 pcie->num_supplies))
1516 return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
1517 pcie->supplies);
1518
1519 /*
1520 * If not all regulators are available for this new scheme, assume
1521 * that the device tree complies with an older version of the device
1522 * tree binding.
1523 */
1524 dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
1525
1526 devm_kfree(pcie->dev, pcie->supplies);
1527 pcie->num_supplies = 0;
1528
1529 return tegra_pcie_get_legacy_regulators(pcie);
1530}
1531
Thierry Redingd1523b52013-08-09 16:49:19 +02001532static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1533{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001534 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001535 struct device_node *np = pcie->dev->of_node, *port;
1536 struct of_pci_range_parser parser;
1537 struct of_pci_range range;
Thierry Reding077fb152014-05-28 16:49:13 +02001538 u32 lanes = 0, mask = 0;
1539 unsigned int lane = 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001540 struct resource res;
Thierry Redingd1523b52013-08-09 16:49:19 +02001541 int err;
1542
Thierry Reding41534e52014-08-01 14:15:11 +02001543 memset(&pcie->all, 0, sizeof(pcie->all));
1544 pcie->all.flags = IORESOURCE_MEM;
1545 pcie->all.name = np->full_name;
1546 pcie->all.start = ~0;
1547 pcie->all.end = 0;
1548
Thierry Redingd1523b52013-08-09 16:49:19 +02001549 if (of_pci_range_parser_init(&parser, np)) {
1550 dev_err(pcie->dev, "missing \"ranges\" property\n");
1551 return -EINVAL;
1552 }
1553
Thierry Redingd1523b52013-08-09 16:49:19 +02001554 for_each_of_pci_range(&parser, &range) {
1555 of_pci_range_to_resource(&range, np, &res);
1556
1557 switch (res.flags & IORESOURCE_TYPE_BITS) {
1558 case IORESOURCE_IO:
1559 memcpy(&pcie->io, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001560 pcie->io.name = np->full_name;
Thierry Redingd1523b52013-08-09 16:49:19 +02001561 break;
1562
1563 case IORESOURCE_MEM:
1564 if (res.flags & IORESOURCE_PREFETCH) {
1565 memcpy(&pcie->prefetch, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001566 pcie->prefetch.name = "prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001567 } else {
1568 memcpy(&pcie->mem, &res, sizeof(res));
Thierry Reding41534e52014-08-01 14:15:11 +02001569 pcie->mem.name = "non-prefetchable";
Thierry Redingd1523b52013-08-09 16:49:19 +02001570 }
1571 break;
1572 }
Thierry Reding41534e52014-08-01 14:15:11 +02001573
1574 if (res.start <= pcie->all.start)
1575 pcie->all.start = res.start;
1576
1577 if (res.end >= pcie->all.end)
1578 pcie->all.end = res.end;
Thierry Redingd1523b52013-08-09 16:49:19 +02001579 }
1580
Thierry Reding41534e52014-08-01 14:15:11 +02001581 err = devm_request_resource(pcie->dev, &iomem_resource, &pcie->all);
1582 if (err < 0)
1583 return err;
1584
Thierry Redingd1523b52013-08-09 16:49:19 +02001585 err = of_pci_parse_bus_range(np, &pcie->busn);
1586 if (err < 0) {
1587 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1588 err);
1589 pcie->busn.name = np->name;
1590 pcie->busn.start = 0;
1591 pcie->busn.end = 0xff;
1592 pcie->busn.flags = IORESOURCE_BUS;
1593 }
1594
1595 /* parse root ports */
1596 for_each_child_of_node(np, port) {
1597 struct tegra_pcie_port *rp;
1598 unsigned int index;
1599 u32 value;
1600
1601 err = of_pci_get_devfn(port);
1602 if (err < 0) {
1603 dev_err(pcie->dev, "failed to parse address: %d\n",
1604 err);
1605 return err;
1606 }
1607
1608 index = PCI_SLOT(err);
1609
Jay Agarwal94716cd2013-08-09 16:49:24 +02001610 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001611 dev_err(pcie->dev, "invalid port number: %d\n", index);
1612 return -EINVAL;
1613 }
1614
1615 index--;
1616
1617 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1618 if (err < 0) {
1619 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1620 err);
1621 return err;
1622 }
1623
1624 if (value > 16) {
1625 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1626 return -EINVAL;
1627 }
1628
1629 lanes |= value << (index << 3);
1630
Thierry Reding077fb152014-05-28 16:49:13 +02001631 if (!of_device_is_available(port)) {
1632 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001633 continue;
Thierry Reding077fb152014-05-28 16:49:13 +02001634 }
1635
1636 mask |= ((1 << value) - 1) << lane;
1637 lane += value;
Thierry Redingd1523b52013-08-09 16:49:19 +02001638
1639 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1640 if (!rp)
1641 return -ENOMEM;
1642
1643 err = of_address_to_resource(port, 0, &rp->regs);
1644 if (err < 0) {
1645 dev_err(pcie->dev, "failed to parse address: %d\n",
1646 err);
1647 return err;
1648 }
1649
1650 INIT_LIST_HEAD(&rp->list);
1651 rp->index = index;
1652 rp->lanes = value;
1653 rp->pcie = pcie;
1654
Julia Lawalldc05ee32013-08-26 11:11:09 +02001655 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1656 if (IS_ERR(rp->base))
1657 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001658
1659 list_add_tail(&rp->list, &pcie->ports);
1660 }
1661
1662 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1663 if (err < 0) {
1664 dev_err(pcie->dev, "invalid lane configuration\n");
1665 return err;
1666 }
1667
Thierry Reding077fb152014-05-28 16:49:13 +02001668 err = tegra_pcie_get_regulators(pcie, mask);
1669 if (err < 0)
1670 return err;
1671
Thierry Redingd1523b52013-08-09 16:49:19 +02001672 return 0;
1673}
1674
1675/*
1676 * FIXME: If there are no PCIe cards attached, then calling this function
1677 * can result in the increase of the bootup time as there are big timeout
1678 * loops.
1679 */
1680#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1681static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1682{
1683 unsigned int retries = 3;
1684 unsigned long value;
1685
1686 do {
1687 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1688
1689 do {
1690 value = readl(port->base + RP_VEND_XP);
1691
1692 if (value & RP_VEND_XP_DL_UP)
1693 break;
1694
1695 usleep_range(1000, 2000);
1696 } while (--timeout);
1697
1698 if (!timeout) {
1699 dev_err(port->pcie->dev, "link %u down, retrying\n",
1700 port->index);
1701 goto retry;
1702 }
1703
1704 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1705
1706 do {
1707 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1708
1709 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1710 return true;
1711
1712 usleep_range(1000, 2000);
1713 } while (--timeout);
1714
1715retry:
1716 tegra_pcie_port_reset(port);
1717 } while (--retries);
1718
1719 return false;
1720}
1721
1722static int tegra_pcie_enable(struct tegra_pcie *pcie)
1723{
1724 struct tegra_pcie_port *port, *tmp;
1725 struct hw_pci hw;
1726
1727 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1728 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
1729 port->index, port->lanes);
1730
1731 tegra_pcie_port_enable(port);
1732
1733 if (tegra_pcie_port_check_link(port))
1734 continue;
1735
1736 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
1737
1738 tegra_pcie_port_disable(port);
1739 tegra_pcie_port_free(port);
1740 }
1741
1742 memset(&hw, 0, sizeof(hw));
1743
1744 hw.nr_controllers = 1;
1745 hw.private_data = (void **)&pcie;
1746 hw.setup = tegra_pcie_setup;
1747 hw.map_irq = tegra_pcie_map_irq;
1748 hw.add_bus = tegra_pcie_add_bus;
1749 hw.scan = tegra_pcie_scan_bus;
1750 hw.ops = &tegra_pcie_ops;
1751
1752 pci_common_init_dev(pcie->dev, &hw);
1753
1754 return 0;
1755}
1756
Jay Agarwal94716cd2013-08-09 16:49:24 +02001757static const struct tegra_pcie_soc_data tegra20_pcie_data = {
1758 .num_ports = 2,
1759 .msi_base_shift = 0,
1760 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1761 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1762 .has_pex_clkreq_en = false,
1763 .has_pex_bias_ctrl = false,
1764 .has_intr_prsnt_sense = false,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001765 .has_cml_clk = false,
1766};
1767
1768static const struct tegra_pcie_soc_data tegra30_pcie_data = {
1769 .num_ports = 3,
1770 .msi_base_shift = 8,
1771 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1772 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1773 .has_pex_clkreq_en = true,
1774 .has_pex_bias_ctrl = true,
1775 .has_intr_prsnt_sense = true,
Jay Agarwal94716cd2013-08-09 16:49:24 +02001776 .has_cml_clk = true,
1777};
1778
1779static const struct of_device_id tegra_pcie_of_match[] = {
1780 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
1781 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
1782 { },
1783};
1784MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
1785
Thierry Reding2cb989f2014-07-22 12:30:46 -06001786static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
1787{
1788 struct tegra_pcie *pcie = s->private;
1789
1790 if (list_empty(&pcie->ports))
1791 return NULL;
1792
1793 seq_printf(s, "Index Status\n");
1794
1795 return seq_list_start(&pcie->ports, *pos);
1796}
1797
1798static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
1799{
1800 struct tegra_pcie *pcie = s->private;
1801
1802 return seq_list_next(v, &pcie->ports, pos);
1803}
1804
1805static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
1806{
1807}
1808
1809static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
1810{
1811 bool up = false, active = false;
1812 struct tegra_pcie_port *port;
1813 unsigned int value;
1814
1815 port = list_entry(v, struct tegra_pcie_port, list);
1816
1817 value = readl(port->base + RP_VEND_XP);
1818
1819 if (value & RP_VEND_XP_DL_UP)
1820 up = true;
1821
1822 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1823
1824 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1825 active = true;
1826
1827 seq_printf(s, "%2u ", port->index);
1828
1829 if (up)
1830 seq_printf(s, "up");
1831
1832 if (active) {
1833 if (up)
1834 seq_printf(s, ", ");
1835
1836 seq_printf(s, "active");
1837 }
1838
1839 seq_printf(s, "\n");
1840 return 0;
1841}
1842
1843static const struct seq_operations tegra_pcie_ports_seq_ops = {
1844 .start = tegra_pcie_ports_seq_start,
1845 .next = tegra_pcie_ports_seq_next,
1846 .stop = tegra_pcie_ports_seq_stop,
1847 .show = tegra_pcie_ports_seq_show,
1848};
1849
1850static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
1851{
1852 struct tegra_pcie *pcie = inode->i_private;
1853 struct seq_file *s;
1854 int err;
1855
1856 err = seq_open(file, &tegra_pcie_ports_seq_ops);
1857 if (err)
1858 return err;
1859
1860 s = file->private_data;
1861 s->private = pcie;
1862
1863 return 0;
1864}
1865
1866static const struct file_operations tegra_pcie_ports_ops = {
1867 .owner = THIS_MODULE,
1868 .open = tegra_pcie_ports_open,
1869 .read = seq_read,
1870 .llseek = seq_lseek,
1871 .release = seq_release,
1872};
1873
1874static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
1875{
1876 struct dentry *file;
1877
1878 pcie->debugfs = debugfs_create_dir("pcie", NULL);
1879 if (!pcie->debugfs)
1880 return -ENOMEM;
1881
1882 file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
1883 pcie, &tegra_pcie_ports_ops);
1884 if (!file)
1885 goto remove;
1886
1887 return 0;
1888
1889remove:
1890 debugfs_remove_recursive(pcie->debugfs);
1891 pcie->debugfs = NULL;
1892 return -ENOMEM;
1893}
1894
Thierry Redingd1523b52013-08-09 16:49:19 +02001895static int tegra_pcie_probe(struct platform_device *pdev)
1896{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001897 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02001898 struct tegra_pcie *pcie;
1899 int err;
1900
Jay Agarwal94716cd2013-08-09 16:49:24 +02001901 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
1902 if (!match)
1903 return -ENODEV;
1904
Thierry Redingd1523b52013-08-09 16:49:19 +02001905 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1906 if (!pcie)
1907 return -ENOMEM;
1908
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001909 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02001910 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001911 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001912 pcie->dev = &pdev->dev;
1913
1914 err = tegra_pcie_parse_dt(pcie);
1915 if (err < 0)
1916 return err;
1917
1918 pcibios_min_mem = 0;
1919
1920 err = tegra_pcie_get_resources(pcie);
1921 if (err < 0) {
1922 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
1923 return err;
1924 }
1925
1926 err = tegra_pcie_enable_controller(pcie);
1927 if (err)
1928 goto put_resources;
1929
1930 /* setup the AFI address translations */
1931 tegra_pcie_setup_translations(pcie);
1932
1933 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1934 err = tegra_pcie_enable_msi(pcie);
1935 if (err < 0) {
1936 dev_err(&pdev->dev,
1937 "failed to enable MSI support: %d\n",
1938 err);
1939 goto put_resources;
1940 }
1941 }
1942
1943 err = tegra_pcie_enable(pcie);
1944 if (err < 0) {
1945 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
1946 goto disable_msi;
1947 }
1948
Thierry Reding2cb989f2014-07-22 12:30:46 -06001949 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1950 err = tegra_pcie_debugfs_init(pcie);
1951 if (err < 0)
1952 dev_err(&pdev->dev, "failed to setup debugfs: %d\n",
1953 err);
1954 }
1955
Thierry Redingd1523b52013-08-09 16:49:19 +02001956 platform_set_drvdata(pdev, pcie);
1957 return 0;
1958
1959disable_msi:
1960 if (IS_ENABLED(CONFIG_PCI_MSI))
1961 tegra_pcie_disable_msi(pcie);
1962put_resources:
1963 tegra_pcie_put_resources(pcie);
1964 return err;
1965}
1966
Thierry Redingd1523b52013-08-09 16:49:19 +02001967static struct platform_driver tegra_pcie_driver = {
1968 .driver = {
1969 .name = "tegra-pcie",
1970 .owner = THIS_MODULE,
1971 .of_match_table = tegra_pcie_of_match,
1972 .suppress_bind_attrs = true,
1973 },
1974 .probe = tegra_pcie_probe,
1975};
1976module_platform_driver(tegra_pcie_driver);
1977
1978MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1979MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
Thierry Redingd975cb52014-07-11 08:58:58 +02001980MODULE_LICENSE("GPL v2");