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