blob: 6681c3182c85935daeaf4ab668008e023e183c3f [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 Redingd1523b52013-08-09 16:49:19 +020028#include <linux/delay.h>
29#include <linux/export.h>
30#include <linux/interrupt.h>
31#include <linux/irq.h>
32#include <linux/irqdomain.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/msi.h>
36#include <linux/of_address.h>
37#include <linux/of_pci.h>
38#include <linux/of_platform.h>
39#include <linux/pci.h>
40#include <linux/platform_device.h>
Stephen Warren3127a6b2013-11-06 15:56:58 -070041#include <linux/reset.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020042#include <linux/sizes.h>
43#include <linux/slab.h>
Stephen Warrenb4f17372013-05-06 14:19:19 -060044#include <linux/tegra-cpuidle.h>
Thierry Redingd1523b52013-08-09 16:49:19 +020045#include <linux/tegra-powergate.h>
46#include <linux/vmalloc.h>
47#include <linux/regulator/consumer.h>
48
49#include <asm/mach/irq.h>
50#include <asm/mach/map.h>
51#include <asm/mach/pci.h>
52
53#define INT_PCI_MSI_NR (8 * 32)
Thierry Redingd1523b52013-08-09 16:49:19 +020054
55/* register definitions */
56
57#define AFI_AXI_BAR0_SZ 0x00
58#define AFI_AXI_BAR1_SZ 0x04
59#define AFI_AXI_BAR2_SZ 0x08
60#define AFI_AXI_BAR3_SZ 0x0c
61#define AFI_AXI_BAR4_SZ 0x10
62#define AFI_AXI_BAR5_SZ 0x14
63
64#define AFI_AXI_BAR0_START 0x18
65#define AFI_AXI_BAR1_START 0x1c
66#define AFI_AXI_BAR2_START 0x20
67#define AFI_AXI_BAR3_START 0x24
68#define AFI_AXI_BAR4_START 0x28
69#define AFI_AXI_BAR5_START 0x2c
70
71#define AFI_FPCI_BAR0 0x30
72#define AFI_FPCI_BAR1 0x34
73#define AFI_FPCI_BAR2 0x38
74#define AFI_FPCI_BAR3 0x3c
75#define AFI_FPCI_BAR4 0x40
76#define AFI_FPCI_BAR5 0x44
77
78#define AFI_CACHE_BAR0_SZ 0x48
79#define AFI_CACHE_BAR0_ST 0x4c
80#define AFI_CACHE_BAR1_SZ 0x50
81#define AFI_CACHE_BAR1_ST 0x54
82
83#define AFI_MSI_BAR_SZ 0x60
84#define AFI_MSI_FPCI_BAR_ST 0x64
85#define AFI_MSI_AXI_BAR_ST 0x68
86
87#define AFI_MSI_VEC0 0x6c
88#define AFI_MSI_VEC1 0x70
89#define AFI_MSI_VEC2 0x74
90#define AFI_MSI_VEC3 0x78
91#define AFI_MSI_VEC4 0x7c
92#define AFI_MSI_VEC5 0x80
93#define AFI_MSI_VEC6 0x84
94#define AFI_MSI_VEC7 0x88
95
96#define AFI_MSI_EN_VEC0 0x8c
97#define AFI_MSI_EN_VEC1 0x90
98#define AFI_MSI_EN_VEC2 0x94
99#define AFI_MSI_EN_VEC3 0x98
100#define AFI_MSI_EN_VEC4 0x9c
101#define AFI_MSI_EN_VEC5 0xa0
102#define AFI_MSI_EN_VEC6 0xa4
103#define AFI_MSI_EN_VEC7 0xa8
104
105#define AFI_CONFIGURATION 0xac
106#define AFI_CONFIGURATION_EN_FPCI (1 << 0)
107
108#define AFI_FPCI_ERROR_MASKS 0xb0
109
110#define AFI_INTR_MASK 0xb4
111#define AFI_INTR_MASK_INT_MASK (1 << 0)
112#define AFI_INTR_MASK_MSI_MASK (1 << 8)
113
114#define AFI_INTR_CODE 0xb8
115#define AFI_INTR_CODE_MASK 0xf
116#define AFI_INTR_AXI_SLAVE_ERROR 1
117#define AFI_INTR_AXI_DECODE_ERROR 2
118#define AFI_INTR_TARGET_ABORT 3
119#define AFI_INTR_MASTER_ABORT 4
120#define AFI_INTR_INVALID_WRITE 5
121#define AFI_INTR_LEGACY 6
122#define AFI_INTR_FPCI_DECODE_ERROR 7
123
124#define AFI_INTR_SIGNATURE 0xbc
125#define AFI_UPPER_FPCI_ADDRESS 0xc0
126#define AFI_SM_INTR_ENABLE 0xc4
127#define AFI_SM_INTR_INTA_ASSERT (1 << 0)
128#define AFI_SM_INTR_INTB_ASSERT (1 << 1)
129#define AFI_SM_INTR_INTC_ASSERT (1 << 2)
130#define AFI_SM_INTR_INTD_ASSERT (1 << 3)
131#define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
132#define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
133#define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
134#define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
135
136#define AFI_AFI_INTR_ENABLE 0xc8
137#define AFI_INTR_EN_INI_SLVERR (1 << 0)
138#define AFI_INTR_EN_INI_DECERR (1 << 1)
139#define AFI_INTR_EN_TGT_SLVERR (1 << 2)
140#define AFI_INTR_EN_TGT_DECERR (1 << 3)
141#define AFI_INTR_EN_TGT_WRERR (1 << 4)
142#define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
143#define AFI_INTR_EN_AXI_DECERR (1 << 6)
144#define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200145#define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
Thierry Redingd1523b52013-08-09 16:49:19 +0200146
147#define AFI_PCIE_CONFIG 0x0f8
148#define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
149#define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
150#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
151#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200152#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200153#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200154#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
155#define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
Thierry Redingd1523b52013-08-09 16:49:19 +0200156
157#define AFI_FUSE 0x104
158#define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
159
160#define AFI_PEX0_CTRL 0x110
161#define AFI_PEX1_CTRL 0x118
Jay Agarwal94716cd2013-08-09 16:49:24 +0200162#define AFI_PEX2_CTRL 0x128
Thierry Redingd1523b52013-08-09 16:49:19 +0200163#define AFI_PEX_CTRL_RST (1 << 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200164#define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
Thierry Redingd1523b52013-08-09 16:49:19 +0200165#define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
166
Jay Agarwal94716cd2013-08-09 16:49:24 +0200167#define AFI_PEXBIAS_CTRL_0 0x168
168
Thierry Redingd1523b52013-08-09 16:49:19 +0200169#define RP_VEND_XP 0x00000F00
170#define RP_VEND_XP_DL_UP (1 << 30)
171
172#define RP_LINK_CONTROL_STATUS 0x00000090
173#define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
174#define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
175
176#define PADS_CTL_SEL 0x0000009C
177
178#define PADS_CTL 0x000000A0
179#define PADS_CTL_IDDQ_1L (1 << 0)
180#define PADS_CTL_TX_DATA_EN_1L (1 << 6)
181#define PADS_CTL_RX_DATA_EN_1L (1 << 10)
182
Jay Agarwal94716cd2013-08-09 16:49:24 +0200183#define PADS_PLL_CTL_TEGRA20 0x000000B8
184#define PADS_PLL_CTL_TEGRA30 0x000000B4
Thierry Redingd1523b52013-08-09 16:49:19 +0200185#define PADS_PLL_CTL_RST_B4SM (1 << 1)
186#define PADS_PLL_CTL_LOCKDET (1 << 8)
187#define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
188#define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
189#define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
190#define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
191#define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
192#define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
193#define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200194#define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
195
196#define PADS_REFCLK_CFG0 0x000000C8
197#define PADS_REFCLK_CFG1 0x000000CC
Thierry Redingd1523b52013-08-09 16:49:19 +0200198
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200199/*
200 * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
201 * entries, one entry per PCIe port. These field definitions and desired
202 * values aren't in the TRM, but do come from NVIDIA.
203 */
204#define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
205#define PADS_REFCLK_CFG_E_TERM_SHIFT 7
206#define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
207#define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
208
209/* Default value provided by HW engineering is 0xfa5c */
210#define PADS_REFCLK_CFG_VALUE \
211 ( \
212 (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
213 (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
214 (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
215 (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
216 )
217
Thierry Redingd1523b52013-08-09 16:49:19 +0200218struct tegra_msi {
219 struct msi_chip chip;
220 DECLARE_BITMAP(used, INT_PCI_MSI_NR);
221 struct irq_domain *domain;
222 unsigned long pages;
223 struct mutex lock;
224 int irq;
225};
226
Jay Agarwal94716cd2013-08-09 16:49:24 +0200227/* used to differentiate between Tegra SoC generations */
228struct tegra_pcie_soc_data {
229 unsigned int num_ports;
230 unsigned int msi_base_shift;
231 u32 pads_pll_ctl;
232 u32 tx_ref_sel;
233 bool has_pex_clkreq_en;
234 bool has_pex_bias_ctrl;
235 bool has_intr_prsnt_sense;
236 bool has_avdd_supply;
237 bool has_cml_clk;
238};
239
Thierry Redingd1523b52013-08-09 16:49:19 +0200240static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
241{
242 return container_of(chip, struct tegra_msi, chip);
243}
244
245struct tegra_pcie {
246 struct device *dev;
247
248 void __iomem *pads;
249 void __iomem *afi;
250 int irq;
251
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700252 struct list_head buses;
Thierry Redingd1523b52013-08-09 16:49:19 +0200253 struct resource *cs;
254
255 struct resource io;
256 struct resource mem;
257 struct resource prefetch;
258 struct resource busn;
259
260 struct clk *pex_clk;
261 struct clk *afi_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200262 struct clk *pll_e;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200263 struct clk *cml_clk;
Thierry Redingd1523b52013-08-09 16:49:19 +0200264
Stephen Warren3127a6b2013-11-06 15:56:58 -0700265 struct reset_control *pex_rst;
266 struct reset_control *afi_rst;
267 struct reset_control *pcie_xrst;
268
Thierry Redingd1523b52013-08-09 16:49:19 +0200269 struct tegra_msi msi;
270
271 struct list_head ports;
272 unsigned int num_ports;
273 u32 xbar_config;
274
275 struct regulator *pex_clk_supply;
276 struct regulator *vdd_supply;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200277 struct regulator *avdd_supply;
278
279 const struct tegra_pcie_soc_data *soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200280};
281
282struct tegra_pcie_port {
283 struct tegra_pcie *pcie;
284 struct list_head list;
285 struct resource regs;
286 void __iomem *base;
287 unsigned int index;
288 unsigned int lanes;
289};
290
291struct tegra_pcie_bus {
292 struct vm_struct *area;
293 struct list_head list;
294 unsigned int nr;
295};
296
297static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
298{
299 return sys->private_data;
300}
301
302static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
303 unsigned long offset)
304{
305 writel(value, pcie->afi + offset);
306}
307
308static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
309{
310 return readl(pcie->afi + offset);
311}
312
313static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
314 unsigned long offset)
315{
316 writel(value, pcie->pads + offset);
317}
318
319static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
320{
321 return readl(pcie->pads + offset);
322}
323
324/*
325 * The configuration space mapping on Tegra is somewhat similar to the ECAM
326 * defined by PCIe. However it deviates a bit in how the 4 bits for extended
327 * register accesses are mapped:
328 *
329 * [27:24] extended register number
330 * [23:16] bus number
331 * [15:11] device number
332 * [10: 8] function number
333 * [ 7: 0] register number
334 *
335 * Mapping the whole extended configuration space would require 256 MiB of
336 * virtual address space, only a small part of which will actually be used.
337 * To work around this, a 1 MiB of virtual addresses are allocated per bus
338 * when the bus is first accessed. When the physical range is mapped, the
339 * the bus number bits are hidden so that the extended register number bits
340 * appear as bits [19:16]. Therefore the virtual mapping looks like this:
341 *
342 * [19:16] extended register number
343 * [15:11] device number
344 * [10: 8] function number
345 * [ 7: 0] register number
346 *
347 * This is achieved by stitching together 16 chunks of 64 KiB of physical
348 * address space via the MMU.
349 */
350static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
351{
352 return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
353 (PCI_FUNC(devfn) << 8) | (where & 0xfc);
354}
355
356static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
357 unsigned int busnr)
358{
359 pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
360 L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
361 phys_addr_t cs = pcie->cs->start;
362 struct tegra_pcie_bus *bus;
363 unsigned int i;
364 int err;
365
366 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
367 if (!bus)
368 return ERR_PTR(-ENOMEM);
369
370 INIT_LIST_HEAD(&bus->list);
371 bus->nr = busnr;
372
373 /* allocate 1 MiB of virtual addresses */
374 bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
375 if (!bus->area) {
376 err = -ENOMEM;
377 goto free;
378 }
379
380 /* map each of the 16 chunks of 64 KiB each */
381 for (i = 0; i < 16; i++) {
382 unsigned long virt = (unsigned long)bus->area->addr +
383 i * SZ_64K;
384 phys_addr_t phys = cs + i * SZ_1M + busnr * SZ_64K;
385
386 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
387 if (err < 0) {
388 dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
389 err);
390 goto unmap;
391 }
392 }
393
394 return bus;
395
396unmap:
397 vunmap(bus->area->addr);
398free:
399 kfree(bus);
400 return ERR_PTR(err);
401}
402
403/*
404 * Look up a virtual address mapping for the specified bus number. If no such
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700405 * mapping exists, try to create one.
Thierry Redingd1523b52013-08-09 16:49:19 +0200406 */
407static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
408 unsigned int busnr)
409{
410 struct tegra_pcie_bus *bus;
411
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700412 list_for_each_entry(bus, &pcie->buses, list)
Thierry Redingd1523b52013-08-09 16:49:19 +0200413 if (bus->nr == busnr)
Jingoo Han1e652492013-09-25 16:40:54 -0600414 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200415
416 bus = tegra_pcie_bus_alloc(pcie, busnr);
417 if (IS_ERR(bus))
418 return NULL;
419
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700420 list_add_tail(&bus->list, &pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +0200421
Jingoo Han1e652492013-09-25 16:40:54 -0600422 return (void __iomem *)bus->area->addr;
Thierry Redingd1523b52013-08-09 16:49:19 +0200423}
424
425static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
426 unsigned int devfn,
427 int where)
428{
429 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
430 void __iomem *addr = NULL;
431
432 if (bus->number == 0) {
433 unsigned int slot = PCI_SLOT(devfn);
434 struct tegra_pcie_port *port;
435
436 list_for_each_entry(port, &pcie->ports, list) {
437 if (port->index + 1 == slot) {
438 addr = port->base + (where & ~3);
439 break;
440 }
441 }
442 } else {
443 addr = tegra_pcie_bus_map(pcie, bus->number);
444 if (!addr) {
445 dev_err(pcie->dev,
446 "failed to map cfg. space for bus %u\n",
447 bus->number);
448 return NULL;
449 }
450
451 addr += tegra_pcie_conf_offset(devfn, where);
452 }
453
454 return addr;
455}
456
457static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
458 int where, int size, u32 *value)
459{
460 void __iomem *addr;
461
462 addr = tegra_pcie_conf_address(bus, devfn, where);
463 if (!addr) {
464 *value = 0xffffffff;
465 return PCIBIOS_DEVICE_NOT_FOUND;
466 }
467
468 *value = readl(addr);
469
470 if (size == 1)
471 *value = (*value >> (8 * (where & 3))) & 0xff;
472 else if (size == 2)
473 *value = (*value >> (8 * (where & 3))) & 0xffff;
474
475 return PCIBIOS_SUCCESSFUL;
476}
477
478static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
479 int where, int size, u32 value)
480{
481 void __iomem *addr;
482 u32 mask, tmp;
483
484 addr = tegra_pcie_conf_address(bus, devfn, where);
485 if (!addr)
486 return PCIBIOS_DEVICE_NOT_FOUND;
487
488 if (size == 4) {
489 writel(value, addr);
490 return PCIBIOS_SUCCESSFUL;
491 }
492
493 if (size == 2)
494 mask = ~(0xffff << ((where & 0x3) * 8));
495 else if (size == 1)
496 mask = ~(0xff << ((where & 0x3) * 8));
497 else
498 return PCIBIOS_BAD_REGISTER_NUMBER;
499
500 tmp = readl(addr) & mask;
501 tmp |= value << ((where & 0x3) * 8);
502 writel(tmp, addr);
503
504 return PCIBIOS_SUCCESSFUL;
505}
506
507static struct pci_ops tegra_pcie_ops = {
508 .read = tegra_pcie_read_conf,
509 .write = tegra_pcie_write_conf,
510};
511
512static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
513{
514 unsigned long ret = 0;
515
516 switch (port->index) {
517 case 0:
518 ret = AFI_PEX0_CTRL;
519 break;
520
521 case 1:
522 ret = AFI_PEX1_CTRL;
523 break;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200524
525 case 2:
526 ret = AFI_PEX2_CTRL;
527 break;
Thierry Redingd1523b52013-08-09 16:49:19 +0200528 }
529
530 return ret;
531}
532
533static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
534{
535 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
536 unsigned long value;
537
538 /* pulse reset signal */
539 value = afi_readl(port->pcie, ctrl);
540 value &= ~AFI_PEX_CTRL_RST;
541 afi_writel(port->pcie, value, ctrl);
542
543 usleep_range(1000, 2000);
544
545 value = afi_readl(port->pcie, ctrl);
546 value |= AFI_PEX_CTRL_RST;
547 afi_writel(port->pcie, value, ctrl);
548}
549
550static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
551{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200552 const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200553 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
554 unsigned long value;
555
556 /* enable reference clock */
557 value = afi_readl(port->pcie, ctrl);
558 value |= AFI_PEX_CTRL_REFCLK_EN;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200559
560 if (soc->has_pex_clkreq_en)
561 value |= AFI_PEX_CTRL_CLKREQ_EN;
562
Thierry Redingd1523b52013-08-09 16:49:19 +0200563 afi_writel(port->pcie, value, ctrl);
564
565 tegra_pcie_port_reset(port);
566}
567
568static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
569{
570 unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
571 unsigned long value;
572
573 /* assert port reset */
574 value = afi_readl(port->pcie, ctrl);
575 value &= ~AFI_PEX_CTRL_RST;
576 afi_writel(port->pcie, value, ctrl);
577
578 /* disable reference clock */
579 value = afi_readl(port->pcie, ctrl);
580 value &= ~AFI_PEX_CTRL_REFCLK_EN;
581 afi_writel(port->pcie, value, ctrl);
582}
583
584static void tegra_pcie_port_free(struct tegra_pcie_port *port)
585{
586 struct tegra_pcie *pcie = port->pcie;
587
588 devm_iounmap(pcie->dev, port->base);
589 devm_release_mem_region(pcie->dev, port->regs.start,
590 resource_size(&port->regs));
591 list_del(&port->list);
592 devm_kfree(pcie->dev, port);
593}
594
595static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
596{
597 u16 reg;
598
599 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
600 pci_read_config_word(dev, PCI_COMMAND, &reg);
601 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
602 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
603 pci_write_config_word(dev, PCI_COMMAND, reg);
604 }
605}
606DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
607
608/* Tegra PCIE root complex wrongly reports device class */
609static void tegra_pcie_fixup_class(struct pci_dev *dev)
610{
611 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
612}
613DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
614DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200615DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
616DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
Thierry Redingd1523b52013-08-09 16:49:19 +0200617
618/* Tegra PCIE requires relaxed ordering */
619static void tegra_pcie_relax_enable(struct pci_dev *dev)
620{
621 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
622}
623DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
624
625static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
626{
627 struct tegra_pcie *pcie = sys_to_pcie(sys);
628
629 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
630 pci_add_resource_offset(&sys->resources, &pcie->prefetch,
631 sys->mem_offset);
632 pci_add_resource(&sys->resources, &pcie->busn);
633
634 pci_ioremap_io(nr * SZ_64K, pcie->io.start);
635
636 return 1;
637}
638
639static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
640{
641 struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
642
Stephen Warrenb4f17372013-05-06 14:19:19 -0600643 tegra_cpuidle_pcie_irqs_in_use();
644
Thierry Redingd1523b52013-08-09 16:49:19 +0200645 return pcie->irq;
646}
647
648static void tegra_pcie_add_bus(struct pci_bus *bus)
649{
650 if (IS_ENABLED(CONFIG_PCI_MSI)) {
651 struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
652
653 bus->msi = &pcie->msi.chip;
654 }
655}
656
657static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
658{
659 struct tegra_pcie *pcie = sys_to_pcie(sys);
660 struct pci_bus *bus;
661
662 bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
663 &sys->resources);
664 if (!bus)
665 return NULL;
666
667 pci_scan_child_bus(bus);
668
669 return bus;
670}
671
672static irqreturn_t tegra_pcie_isr(int irq, void *arg)
673{
674 const char *err_msg[] = {
675 "Unknown",
676 "AXI slave error",
677 "AXI decode error",
678 "Target abort",
679 "Master abort",
680 "Invalid write",
681 "Response decoding error",
682 "AXI response decoding error",
683 "Transaction timeout",
684 };
685 struct tegra_pcie *pcie = arg;
686 u32 code, signature;
687
688 code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
689 signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
690 afi_writel(pcie, 0, AFI_INTR_CODE);
691
692 if (code == AFI_INTR_LEGACY)
693 return IRQ_NONE;
694
695 if (code >= ARRAY_SIZE(err_msg))
696 code = 0;
697
698 /*
699 * do not pollute kernel log with master abort reports since they
700 * happen a lot during enumeration
701 */
702 if (code == AFI_INTR_MASTER_ABORT)
703 dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
704 signature);
705 else
706 dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
707 signature);
708
709 if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
710 code == AFI_INTR_FPCI_DECODE_ERROR) {
711 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
712 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
713
714 if (code == AFI_INTR_MASTER_ABORT)
715 dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
716 else
717 dev_err(pcie->dev, " FPCI address: %10llx\n", address);
718 }
719
720 return IRQ_HANDLED;
721}
722
723/*
724 * FPCI map is as follows:
725 * - 0xfdfc000000: I/O space
726 * - 0xfdfe000000: type 0 configuration space
727 * - 0xfdff000000: type 1 configuration space
728 * - 0xfe00000000: type 0 extended configuration space
729 * - 0xfe10000000: type 1 extended configuration space
730 */
731static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
732{
733 u32 fpci_bar, size, axi_address;
734
735 /* Bar 0: type 1 extended configuration space */
736 fpci_bar = 0xfe100000;
737 size = resource_size(pcie->cs);
738 axi_address = pcie->cs->start;
739 afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
740 afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
741 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
742
743 /* Bar 1: downstream IO bar */
744 fpci_bar = 0xfdfc0000;
745 size = resource_size(&pcie->io);
746 axi_address = pcie->io.start;
747 afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
748 afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
749 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
750
751 /* Bar 2: prefetchable memory BAR */
752 fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
753 size = resource_size(&pcie->prefetch);
754 axi_address = pcie->prefetch.start;
755 afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
756 afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
757 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
758
759 /* Bar 3: non prefetchable memory BAR */
760 fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
761 size = resource_size(&pcie->mem);
762 axi_address = pcie->mem.start;
763 afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
764 afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
765 afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
766
767 /* NULL out the remaining BARs as they are not used */
768 afi_writel(pcie, 0, AFI_AXI_BAR4_START);
769 afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
770 afi_writel(pcie, 0, AFI_FPCI_BAR4);
771
772 afi_writel(pcie, 0, AFI_AXI_BAR5_START);
773 afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
774 afi_writel(pcie, 0, AFI_FPCI_BAR5);
775
776 /* map all upstream transactions as uncached */
777 afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
778 afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
779 afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
780 afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
781
782 /* MSI translations are setup only when needed */
783 afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
784 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
785 afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
786 afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
787}
788
789static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
790{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200791 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200792 struct tegra_pcie_port *port;
793 unsigned int timeout;
794 unsigned long value;
795
Jay Agarwal94716cd2013-08-09 16:49:24 +0200796 /* power down PCIe slot clock bias pad */
797 if (soc->has_pex_bias_ctrl)
798 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
799
Thierry Redingd1523b52013-08-09 16:49:19 +0200800 /* configure mode and disable all ports */
801 value = afi_readl(pcie, AFI_PCIE_CONFIG);
802 value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
803 value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
804
805 list_for_each_entry(port, &pcie->ports, list)
806 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
807
808 afi_writel(pcie, value, AFI_PCIE_CONFIG);
809
810 value = afi_readl(pcie, AFI_FUSE);
811 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
812 afi_writel(pcie, value, AFI_FUSE);
813
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700814 /* initialize internal PHY, enable up to 16 PCIE lanes */
Thierry Redingd1523b52013-08-09 16:49:19 +0200815 pads_writel(pcie, 0x0, PADS_CTL_SEL);
816
817 /* override IDDQ to 1 on all 4 lanes */
818 value = pads_readl(pcie, PADS_CTL);
819 value |= PADS_CTL_IDDQ_1L;
820 pads_writel(pcie, value, PADS_CTL);
821
822 /*
823 * Set up PHY PLL inputs select PLLE output as refclock,
824 * set TX ref sel to div10 (not div5).
825 */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200826 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200827 value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
Jay Agarwal94716cd2013-08-09 16:49:24 +0200828 value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
829 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200830
831 /* take PLL out of reset */
Jay Agarwal94716cd2013-08-09 16:49:24 +0200832 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200833 value |= PADS_PLL_CTL_RST_B4SM;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200834 pads_writel(pcie, value, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200835
Stephen Warrenb02b07a2013-08-09 16:49:25 +0200836 /* Configure the reference clock driver */
837 value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
838 pads_writel(pcie, value, PADS_REFCLK_CFG0);
839 if (soc->num_ports > 2)
840 pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
Thierry Redingd1523b52013-08-09 16:49:19 +0200841
842 /* wait for the PLL to lock */
843 timeout = 300;
844 do {
Jay Agarwal94716cd2013-08-09 16:49:24 +0200845 value = pads_readl(pcie, soc->pads_pll_ctl);
Thierry Redingd1523b52013-08-09 16:49:19 +0200846 usleep_range(1000, 2000);
847 if (--timeout == 0) {
848 pr_err("Tegra PCIe error: timeout waiting for PLL\n");
849 return -EBUSY;
850 }
851 } while (!(value & PADS_PLL_CTL_LOCKDET));
852
853 /* turn off IDDQ override */
854 value = pads_readl(pcie, PADS_CTL);
855 value &= ~PADS_CTL_IDDQ_1L;
856 pads_writel(pcie, value, PADS_CTL);
857
858 /* enable TX/RX data */
859 value = pads_readl(pcie, PADS_CTL);
860 value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
861 pads_writel(pcie, value, PADS_CTL);
862
863 /* take the PCIe interface module out of reset */
Stephen Warren3127a6b2013-11-06 15:56:58 -0700864 reset_control_deassert(pcie->pcie_xrst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200865
866 /* finally enable PCIe */
867 value = afi_readl(pcie, AFI_CONFIGURATION);
868 value |= AFI_CONFIGURATION_EN_FPCI;
869 afi_writel(pcie, value, AFI_CONFIGURATION);
870
871 value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
872 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
873 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
Jay Agarwal94716cd2013-08-09 16:49:24 +0200874
875 if (soc->has_intr_prsnt_sense)
876 value |= AFI_INTR_EN_PRSNT_SENSE;
877
Thierry Redingd1523b52013-08-09 16:49:19 +0200878 afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
879 afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
880
881 /* don't enable MSI for now, only when needed */
882 afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
883
884 /* disable all exceptions */
885 afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
886
887 return 0;
888}
889
890static void tegra_pcie_power_off(struct tegra_pcie *pcie)
891{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200892 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200893 int err;
894
895 /* TODO: disable and unprepare clocks? */
896
Stephen Warren3127a6b2013-11-06 15:56:58 -0700897 reset_control_assert(pcie->pcie_xrst);
898 reset_control_assert(pcie->afi_rst);
899 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200900
901 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
902
Jay Agarwal94716cd2013-08-09 16:49:24 +0200903 if (soc->has_avdd_supply) {
904 err = regulator_disable(pcie->avdd_supply);
905 if (err < 0)
906 dev_warn(pcie->dev,
907 "failed to disable AVDD regulator: %d\n",
908 err);
909 }
910
Thierry Redingd1523b52013-08-09 16:49:19 +0200911 err = regulator_disable(pcie->pex_clk_supply);
912 if (err < 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200913 dev_warn(pcie->dev, "failed to disable pex-clk regulator: %d\n",
914 err);
Thierry Redingd1523b52013-08-09 16:49:19 +0200915
916 err = regulator_disable(pcie->vdd_supply);
917 if (err < 0)
Jay Agarwal94716cd2013-08-09 16:49:24 +0200918 dev_warn(pcie->dev, "failed to disable VDD regulator: %d\n",
919 err);
Thierry Redingd1523b52013-08-09 16:49:19 +0200920}
921
922static int tegra_pcie_power_on(struct tegra_pcie *pcie)
923{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200924 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +0200925 int err;
926
Stephen Warren3127a6b2013-11-06 15:56:58 -0700927 reset_control_assert(pcie->pcie_xrst);
928 reset_control_assert(pcie->afi_rst);
929 reset_control_assert(pcie->pex_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200930
931 tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
932
933 /* enable regulators */
934 err = regulator_enable(pcie->vdd_supply);
935 if (err < 0) {
936 dev_err(pcie->dev, "failed to enable VDD regulator: %d\n", err);
937 return err;
938 }
939
940 err = regulator_enable(pcie->pex_clk_supply);
941 if (err < 0) {
942 dev_err(pcie->dev, "failed to enable pex-clk regulator: %d\n",
943 err);
944 return err;
945 }
946
Jay Agarwal94716cd2013-08-09 16:49:24 +0200947 if (soc->has_avdd_supply) {
948 err = regulator_enable(pcie->avdd_supply);
949 if (err < 0) {
950 dev_err(pcie->dev,
951 "failed to enable AVDD regulator: %d\n",
952 err);
953 return err;
954 }
955 }
956
Thierry Redingd1523b52013-08-09 16:49:19 +0200957 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
958 pcie->pex_clk);
959 if (err) {
960 dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
961 return err;
962 }
963
Stephen Warren3127a6b2013-11-06 15:56:58 -0700964 reset_control_deassert(pcie->afi_rst);
Thierry Redingd1523b52013-08-09 16:49:19 +0200965
966 err = clk_prepare_enable(pcie->afi_clk);
967 if (err < 0) {
968 dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
969 return err;
970 }
971
Jay Agarwal94716cd2013-08-09 16:49:24 +0200972 if (soc->has_cml_clk) {
973 err = clk_prepare_enable(pcie->cml_clk);
974 if (err < 0) {
975 dev_err(pcie->dev, "failed to enable CML clock: %d\n",
976 err);
977 return err;
978 }
979 }
980
Thierry Redingd1523b52013-08-09 16:49:19 +0200981 err = clk_prepare_enable(pcie->pll_e);
982 if (err < 0) {
983 dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
984 return err;
985 }
986
987 return 0;
988}
989
990static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
991{
Jay Agarwal94716cd2013-08-09 16:49:24 +0200992 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
993
Thierry Redingd1523b52013-08-09 16:49:19 +0200994 pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
995 if (IS_ERR(pcie->pex_clk))
996 return PTR_ERR(pcie->pex_clk);
997
998 pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
999 if (IS_ERR(pcie->afi_clk))
1000 return PTR_ERR(pcie->afi_clk);
1001
Thierry Redingd1523b52013-08-09 16:49:19 +02001002 pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
1003 if (IS_ERR(pcie->pll_e))
1004 return PTR_ERR(pcie->pll_e);
1005
Jay Agarwal94716cd2013-08-09 16:49:24 +02001006 if (soc->has_cml_clk) {
1007 pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
1008 if (IS_ERR(pcie->cml_clk))
1009 return PTR_ERR(pcie->cml_clk);
1010 }
1011
Thierry Redingd1523b52013-08-09 16:49:19 +02001012 return 0;
1013}
1014
Stephen Warren3127a6b2013-11-06 15:56:58 -07001015static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1016{
1017 pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
1018 if (IS_ERR(pcie->pex_rst))
1019 return PTR_ERR(pcie->pex_rst);
1020
1021 pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
1022 if (IS_ERR(pcie->afi_rst))
1023 return PTR_ERR(pcie->afi_rst);
1024
1025 pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
1026 if (IS_ERR(pcie->pcie_xrst))
1027 return PTR_ERR(pcie->pcie_xrst);
1028
1029 return 0;
1030}
1031
Thierry Redingd1523b52013-08-09 16:49:19 +02001032static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1033{
1034 struct platform_device *pdev = to_platform_device(pcie->dev);
1035 struct resource *pads, *afi, *res;
1036 int err;
1037
1038 err = tegra_pcie_clocks_get(pcie);
1039 if (err) {
1040 dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
1041 return err;
1042 }
1043
Stephen Warren3127a6b2013-11-06 15:56:58 -07001044 err = tegra_pcie_resets_get(pcie);
1045 if (err) {
1046 dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1047 return err;
1048 }
1049
Thierry Redingd1523b52013-08-09 16:49:19 +02001050 err = tegra_pcie_power_on(pcie);
1051 if (err) {
1052 dev_err(&pdev->dev, "failed to power up: %d\n", err);
1053 return err;
1054 }
1055
Thierry Redingd1523b52013-08-09 16:49:19 +02001056 pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001057 pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
1058 if (IS_ERR(pcie->pads)) {
1059 err = PTR_ERR(pcie->pads);
Thierry Redingd1523b52013-08-09 16:49:19 +02001060 goto poweroff;
1061 }
1062
1063 afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
Julia Lawalldc05ee32013-08-26 11:11:09 +02001064 pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
1065 if (IS_ERR(pcie->afi)) {
1066 err = PTR_ERR(pcie->afi);
Thierry Redingd1523b52013-08-09 16:49:19 +02001067 goto poweroff;
1068 }
1069
Julia Lawalldc05ee32013-08-26 11:11:09 +02001070 /* request configuration space, but remap later, on demand */
Thierry Redingd1523b52013-08-09 16:49:19 +02001071 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1072 if (!res) {
1073 err = -EADDRNOTAVAIL;
1074 goto poweroff;
1075 }
1076
1077 pcie->cs = devm_request_mem_region(pcie->dev, res->start,
1078 resource_size(res), res->name);
1079 if (!pcie->cs) {
1080 err = -EADDRNOTAVAIL;
1081 goto poweroff;
1082 }
1083
1084 /* request interrupt */
1085 err = platform_get_irq_byname(pdev, "intr");
1086 if (err < 0) {
1087 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1088 goto poweroff;
1089 }
1090
1091 pcie->irq = err;
1092
1093 err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1094 if (err) {
1095 dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
1096 goto poweroff;
1097 }
1098
1099 return 0;
1100
1101poweroff:
1102 tegra_pcie_power_off(pcie);
1103 return err;
1104}
1105
1106static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1107{
1108 if (pcie->irq > 0)
1109 free_irq(pcie->irq, pcie);
1110
1111 tegra_pcie_power_off(pcie);
1112 return 0;
1113}
1114
1115static int tegra_msi_alloc(struct tegra_msi *chip)
1116{
1117 int msi;
1118
1119 mutex_lock(&chip->lock);
1120
1121 msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1122 if (msi < INT_PCI_MSI_NR)
1123 set_bit(msi, chip->used);
1124 else
1125 msi = -ENOSPC;
1126
1127 mutex_unlock(&chip->lock);
1128
1129 return msi;
1130}
1131
1132static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1133{
1134 struct device *dev = chip->chip.dev;
1135
1136 mutex_lock(&chip->lock);
1137
1138 if (!test_bit(irq, chip->used))
1139 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1140 else
1141 clear_bit(irq, chip->used);
1142
1143 mutex_unlock(&chip->lock);
1144}
1145
1146static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1147{
1148 struct tegra_pcie *pcie = data;
1149 struct tegra_msi *msi = &pcie->msi;
1150 unsigned int i, processed = 0;
1151
1152 for (i = 0; i < 8; i++) {
1153 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1154
1155 while (reg) {
1156 unsigned int offset = find_first_bit(&reg, 32);
1157 unsigned int index = i * 32 + offset;
1158 unsigned int irq;
1159
1160 /* clear the interrupt */
1161 afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1162
1163 irq = irq_find_mapping(msi->domain, index);
1164 if (irq) {
1165 if (test_bit(index, msi->used))
1166 generic_handle_irq(irq);
1167 else
1168 dev_info(pcie->dev, "unhandled MSI\n");
1169 } else {
1170 /*
1171 * that's weird who triggered this?
1172 * just clear it
1173 */
1174 dev_info(pcie->dev, "unexpected MSI\n");
1175 }
1176
1177 /* see if there's any more pending in this vector */
1178 reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1179
1180 processed++;
1181 }
1182 }
1183
1184 return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1185}
1186
1187static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
1188 struct msi_desc *desc)
1189{
1190 struct tegra_msi *msi = to_tegra_msi(chip);
1191 struct msi_msg msg;
1192 unsigned int irq;
1193 int hwirq;
1194
1195 hwirq = tegra_msi_alloc(msi);
1196 if (hwirq < 0)
1197 return hwirq;
1198
1199 irq = irq_create_mapping(msi->domain, hwirq);
1200 if (!irq)
1201 return -EINVAL;
1202
1203 irq_set_msi_desc(irq, desc);
1204
1205 msg.address_lo = virt_to_phys((void *)msi->pages);
1206 /* 32 bit address only */
1207 msg.address_hi = 0;
1208 msg.data = hwirq;
1209
1210 write_msi_msg(irq, &msg);
1211
1212 return 0;
1213}
1214
1215static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
1216{
1217 struct tegra_msi *msi = to_tegra_msi(chip);
1218 struct irq_data *d = irq_get_irq_data(irq);
1219
1220 tegra_msi_free(msi, d->hwirq);
1221}
1222
1223static struct irq_chip tegra_msi_irq_chip = {
1224 .name = "Tegra PCIe MSI",
1225 .irq_enable = unmask_msi_irq,
1226 .irq_disable = mask_msi_irq,
1227 .irq_mask = mask_msi_irq,
1228 .irq_unmask = unmask_msi_irq,
1229};
1230
1231static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1232 irq_hw_number_t hwirq)
1233{
1234 irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1235 irq_set_chip_data(irq, domain->host_data);
1236 set_irq_flags(irq, IRQF_VALID);
1237
Stephen Warrenb4f17372013-05-06 14:19:19 -06001238 tegra_cpuidle_pcie_irqs_in_use();
1239
Thierry Redingd1523b52013-08-09 16:49:19 +02001240 return 0;
1241}
1242
1243static const struct irq_domain_ops msi_domain_ops = {
1244 .map = tegra_msi_map,
1245};
1246
1247static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1248{
1249 struct platform_device *pdev = to_platform_device(pcie->dev);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001250 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001251 struct tegra_msi *msi = &pcie->msi;
1252 unsigned long base;
1253 int err;
1254 u32 reg;
1255
1256 mutex_init(&msi->lock);
1257
1258 msi->chip.dev = pcie->dev;
1259 msi->chip.setup_irq = tegra_msi_setup_irq;
1260 msi->chip.teardown_irq = tegra_msi_teardown_irq;
1261
1262 msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
1263 &msi_domain_ops, &msi->chip);
1264 if (!msi->domain) {
1265 dev_err(&pdev->dev, "failed to create IRQ domain\n");
1266 return -ENOMEM;
1267 }
1268
1269 err = platform_get_irq_byname(pdev, "msi");
1270 if (err < 0) {
1271 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
1272 goto err;
1273 }
1274
1275 msi->irq = err;
1276
1277 err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
1278 tegra_msi_irq_chip.name, pcie);
1279 if (err < 0) {
1280 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1281 goto err;
1282 }
1283
1284 /* setup AFI/FPCI range */
1285 msi->pages = __get_free_pages(GFP_KERNEL, 0);
1286 base = virt_to_phys((void *)msi->pages);
1287
Jay Agarwal94716cd2013-08-09 16:49:24 +02001288 afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
Thierry Redingd1523b52013-08-09 16:49:19 +02001289 afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
1290 /* this register is in 4K increments */
1291 afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1292
1293 /* enable all MSI vectors */
1294 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1295 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1296 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1297 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1298 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1299 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1300 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1301 afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1302
1303 /* and unmask the MSI interrupt */
1304 reg = afi_readl(pcie, AFI_INTR_MASK);
1305 reg |= AFI_INTR_MASK_MSI_MASK;
1306 afi_writel(pcie, reg, AFI_INTR_MASK);
1307
1308 return 0;
1309
1310err:
1311 irq_domain_remove(msi->domain);
1312 return err;
1313}
1314
1315static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1316{
1317 struct tegra_msi *msi = &pcie->msi;
1318 unsigned int i, irq;
1319 u32 value;
1320
1321 /* mask the MSI interrupt */
1322 value = afi_readl(pcie, AFI_INTR_MASK);
1323 value &= ~AFI_INTR_MASK_MSI_MASK;
1324 afi_writel(pcie, value, AFI_INTR_MASK);
1325
1326 /* disable all MSI vectors */
1327 afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1328 afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1329 afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1330 afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1331 afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1332 afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1333 afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1334 afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1335
1336 free_pages(msi->pages, 0);
1337
1338 if (msi->irq > 0)
1339 free_irq(msi->irq, pcie);
1340
1341 for (i = 0; i < INT_PCI_MSI_NR; i++) {
1342 irq = irq_find_mapping(msi->domain, i);
1343 if (irq > 0)
1344 irq_dispose_mapping(irq);
1345 }
1346
1347 irq_domain_remove(msi->domain);
1348
1349 return 0;
1350}
1351
1352static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1353 u32 *xbar)
1354{
1355 struct device_node *np = pcie->dev->of_node;
1356
Jay Agarwal94716cd2013-08-09 16:49:24 +02001357 if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1358 switch (lanes) {
1359 case 0x00000204:
1360 dev_info(pcie->dev, "4x1, 2x1 configuration\n");
1361 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1362 return 0;
Thierry Redingd1523b52013-08-09 16:49:19 +02001363
Jay Agarwal94716cd2013-08-09 16:49:24 +02001364 case 0x00020202:
1365 dev_info(pcie->dev, "2x3 configuration\n");
1366 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1367 return 0;
1368
1369 case 0x00010104:
1370 dev_info(pcie->dev, "4x1, 1x2 configuration\n");
1371 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1372 return 0;
1373 }
1374 } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1375 switch (lanes) {
1376 case 0x00000004:
1377 dev_info(pcie->dev, "single-mode configuration\n");
1378 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1379 return 0;
1380
1381 case 0x00000202:
1382 dev_info(pcie->dev, "dual-mode configuration\n");
1383 *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1384 return 0;
1385 }
Thierry Redingd1523b52013-08-09 16:49:19 +02001386 }
1387
1388 return -EINVAL;
1389}
1390
1391static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1392{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001393 const struct tegra_pcie_soc_data *soc = pcie->soc_data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001394 struct device_node *np = pcie->dev->of_node, *port;
1395 struct of_pci_range_parser parser;
1396 struct of_pci_range range;
1397 struct resource res;
1398 u32 lanes = 0;
1399 int err;
1400
1401 if (of_pci_range_parser_init(&parser, np)) {
1402 dev_err(pcie->dev, "missing \"ranges\" property\n");
1403 return -EINVAL;
1404 }
1405
1406 pcie->vdd_supply = devm_regulator_get(pcie->dev, "vdd");
1407 if (IS_ERR(pcie->vdd_supply))
1408 return PTR_ERR(pcie->vdd_supply);
1409
1410 pcie->pex_clk_supply = devm_regulator_get(pcie->dev, "pex-clk");
1411 if (IS_ERR(pcie->pex_clk_supply))
1412 return PTR_ERR(pcie->pex_clk_supply);
1413
Jay Agarwal94716cd2013-08-09 16:49:24 +02001414 if (soc->has_avdd_supply) {
1415 pcie->avdd_supply = devm_regulator_get(pcie->dev, "avdd");
1416 if (IS_ERR(pcie->avdd_supply))
1417 return PTR_ERR(pcie->avdd_supply);
1418 }
1419
Thierry Redingd1523b52013-08-09 16:49:19 +02001420 for_each_of_pci_range(&parser, &range) {
1421 of_pci_range_to_resource(&range, np, &res);
1422
1423 switch (res.flags & IORESOURCE_TYPE_BITS) {
1424 case IORESOURCE_IO:
1425 memcpy(&pcie->io, &res, sizeof(res));
1426 pcie->io.name = "I/O";
1427 break;
1428
1429 case IORESOURCE_MEM:
1430 if (res.flags & IORESOURCE_PREFETCH) {
1431 memcpy(&pcie->prefetch, &res, sizeof(res));
1432 pcie->prefetch.name = "PREFETCH";
1433 } else {
1434 memcpy(&pcie->mem, &res, sizeof(res));
1435 pcie->mem.name = "MEM";
1436 }
1437 break;
1438 }
1439 }
1440
1441 err = of_pci_parse_bus_range(np, &pcie->busn);
1442 if (err < 0) {
1443 dev_err(pcie->dev, "failed to parse ranges property: %d\n",
1444 err);
1445 pcie->busn.name = np->name;
1446 pcie->busn.start = 0;
1447 pcie->busn.end = 0xff;
1448 pcie->busn.flags = IORESOURCE_BUS;
1449 }
1450
1451 /* parse root ports */
1452 for_each_child_of_node(np, port) {
1453 struct tegra_pcie_port *rp;
1454 unsigned int index;
1455 u32 value;
1456
1457 err = of_pci_get_devfn(port);
1458 if (err < 0) {
1459 dev_err(pcie->dev, "failed to parse address: %d\n",
1460 err);
1461 return err;
1462 }
1463
1464 index = PCI_SLOT(err);
1465
Jay Agarwal94716cd2013-08-09 16:49:24 +02001466 if (index < 1 || index > soc->num_ports) {
Thierry Redingd1523b52013-08-09 16:49:19 +02001467 dev_err(pcie->dev, "invalid port number: %d\n", index);
1468 return -EINVAL;
1469 }
1470
1471 index--;
1472
1473 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1474 if (err < 0) {
1475 dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
1476 err);
1477 return err;
1478 }
1479
1480 if (value > 16) {
1481 dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
1482 return -EINVAL;
1483 }
1484
1485 lanes |= value << (index << 3);
1486
1487 if (!of_device_is_available(port))
1488 continue;
1489
1490 rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
1491 if (!rp)
1492 return -ENOMEM;
1493
1494 err = of_address_to_resource(port, 0, &rp->regs);
1495 if (err < 0) {
1496 dev_err(pcie->dev, "failed to parse address: %d\n",
1497 err);
1498 return err;
1499 }
1500
1501 INIT_LIST_HEAD(&rp->list);
1502 rp->index = index;
1503 rp->lanes = value;
1504 rp->pcie = pcie;
1505
Julia Lawalldc05ee32013-08-26 11:11:09 +02001506 rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
1507 if (IS_ERR(rp->base))
1508 return PTR_ERR(rp->base);
Thierry Redingd1523b52013-08-09 16:49:19 +02001509
1510 list_add_tail(&rp->list, &pcie->ports);
1511 }
1512
1513 err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1514 if (err < 0) {
1515 dev_err(pcie->dev, "invalid lane configuration\n");
1516 return err;
1517 }
1518
1519 return 0;
1520}
1521
1522/*
1523 * FIXME: If there are no PCIe cards attached, then calling this function
1524 * can result in the increase of the bootup time as there are big timeout
1525 * loops.
1526 */
1527#define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
1528static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
1529{
1530 unsigned int retries = 3;
1531 unsigned long value;
1532
1533 do {
1534 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1535
1536 do {
1537 value = readl(port->base + RP_VEND_XP);
1538
1539 if (value & RP_VEND_XP_DL_UP)
1540 break;
1541
1542 usleep_range(1000, 2000);
1543 } while (--timeout);
1544
1545 if (!timeout) {
1546 dev_err(port->pcie->dev, "link %u down, retrying\n",
1547 port->index);
1548 goto retry;
1549 }
1550
1551 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
1552
1553 do {
1554 value = readl(port->base + RP_LINK_CONTROL_STATUS);
1555
1556 if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
1557 return true;
1558
1559 usleep_range(1000, 2000);
1560 } while (--timeout);
1561
1562retry:
1563 tegra_pcie_port_reset(port);
1564 } while (--retries);
1565
1566 return false;
1567}
1568
1569static int tegra_pcie_enable(struct tegra_pcie *pcie)
1570{
1571 struct tegra_pcie_port *port, *tmp;
1572 struct hw_pci hw;
1573
1574 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1575 dev_info(pcie->dev, "probing port %u, using %u lanes\n",
1576 port->index, port->lanes);
1577
1578 tegra_pcie_port_enable(port);
1579
1580 if (tegra_pcie_port_check_link(port))
1581 continue;
1582
1583 dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
1584
1585 tegra_pcie_port_disable(port);
1586 tegra_pcie_port_free(port);
1587 }
1588
1589 memset(&hw, 0, sizeof(hw));
1590
1591 hw.nr_controllers = 1;
1592 hw.private_data = (void **)&pcie;
1593 hw.setup = tegra_pcie_setup;
1594 hw.map_irq = tegra_pcie_map_irq;
1595 hw.add_bus = tegra_pcie_add_bus;
1596 hw.scan = tegra_pcie_scan_bus;
1597 hw.ops = &tegra_pcie_ops;
1598
1599 pci_common_init_dev(pcie->dev, &hw);
1600
1601 return 0;
1602}
1603
Jay Agarwal94716cd2013-08-09 16:49:24 +02001604static const struct tegra_pcie_soc_data tegra20_pcie_data = {
1605 .num_ports = 2,
1606 .msi_base_shift = 0,
1607 .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1608 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1609 .has_pex_clkreq_en = false,
1610 .has_pex_bias_ctrl = false,
1611 .has_intr_prsnt_sense = false,
1612 .has_avdd_supply = false,
1613 .has_cml_clk = false,
1614};
1615
1616static const struct tegra_pcie_soc_data tegra30_pcie_data = {
1617 .num_ports = 3,
1618 .msi_base_shift = 8,
1619 .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1620 .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1621 .has_pex_clkreq_en = true,
1622 .has_pex_bias_ctrl = true,
1623 .has_intr_prsnt_sense = true,
1624 .has_avdd_supply = true,
1625 .has_cml_clk = true,
1626};
1627
1628static const struct of_device_id tegra_pcie_of_match[] = {
1629 { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
1630 { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
1631 { },
1632};
1633MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
1634
Thierry Redingd1523b52013-08-09 16:49:19 +02001635static int tegra_pcie_probe(struct platform_device *pdev)
1636{
Jay Agarwal94716cd2013-08-09 16:49:24 +02001637 const struct of_device_id *match;
Thierry Redingd1523b52013-08-09 16:49:19 +02001638 struct tegra_pcie *pcie;
1639 int err;
1640
Jay Agarwal94716cd2013-08-09 16:49:24 +02001641 match = of_match_device(tegra_pcie_of_match, &pdev->dev);
1642 if (!match)
1643 return -ENODEV;
1644
Thierry Redingd1523b52013-08-09 16:49:19 +02001645 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1646 if (!pcie)
1647 return -ENOMEM;
1648
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001649 INIT_LIST_HEAD(&pcie->buses);
Thierry Redingd1523b52013-08-09 16:49:19 +02001650 INIT_LIST_HEAD(&pcie->ports);
Jay Agarwal94716cd2013-08-09 16:49:24 +02001651 pcie->soc_data = match->data;
Thierry Redingd1523b52013-08-09 16:49:19 +02001652 pcie->dev = &pdev->dev;
1653
1654 err = tegra_pcie_parse_dt(pcie);
1655 if (err < 0)
1656 return err;
1657
1658 pcibios_min_mem = 0;
1659
1660 err = tegra_pcie_get_resources(pcie);
1661 if (err < 0) {
1662 dev_err(&pdev->dev, "failed to request resources: %d\n", err);
1663 return err;
1664 }
1665
1666 err = tegra_pcie_enable_controller(pcie);
1667 if (err)
1668 goto put_resources;
1669
1670 /* setup the AFI address translations */
1671 tegra_pcie_setup_translations(pcie);
1672
1673 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1674 err = tegra_pcie_enable_msi(pcie);
1675 if (err < 0) {
1676 dev_err(&pdev->dev,
1677 "failed to enable MSI support: %d\n",
1678 err);
1679 goto put_resources;
1680 }
1681 }
1682
1683 err = tegra_pcie_enable(pcie);
1684 if (err < 0) {
1685 dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
1686 goto disable_msi;
1687 }
1688
1689 platform_set_drvdata(pdev, pcie);
1690 return 0;
1691
1692disable_msi:
1693 if (IS_ENABLED(CONFIG_PCI_MSI))
1694 tegra_pcie_disable_msi(pcie);
1695put_resources:
1696 tegra_pcie_put_resources(pcie);
1697 return err;
1698}
1699
Thierry Redingd1523b52013-08-09 16:49:19 +02001700static struct platform_driver tegra_pcie_driver = {
1701 .driver = {
1702 .name = "tegra-pcie",
1703 .owner = THIS_MODULE,
1704 .of_match_table = tegra_pcie_of_match,
1705 .suppress_bind_attrs = true,
1706 },
1707 .probe = tegra_pcie_probe,
1708};
1709module_platform_driver(tegra_pcie_driver);
1710
1711MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1712MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
1713MODULE_LICENSE("GPLv2");