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