GNU Linux-libre 4.14.332-gnu1
[releases.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  * Author: Thierry Reding <treding@nvidia.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22  * more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
27  */
28
29 #include <linux/clk.h>
30 #include <linux/debugfs.h>
31 #include <linux/delay.h>
32 #include <linux/export.h>
33 #include <linux/interrupt.h>
34 #include <linux/irq.h>
35 #include <linux/irqdomain.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/msi.h>
39 #include <linux/of_address.h>
40 #include <linux/of_pci.h>
41 #include <linux/of_platform.h>
42 #include <linux/pci.h>
43 #include <linux/phy/phy.h>
44 #include <linux/platform_device.h>
45 #include <linux/reset.h>
46 #include <linux/sizes.h>
47 #include <linux/slab.h>
48 #include <linux/vmalloc.h>
49 #include <linux/regulator/consumer.h>
50
51 #include <soc/tegra/cpuidle.h>
52 #include <soc/tegra/pmc.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_INI_SLAVE_ERROR       1
118 #define  AFI_INTR_INI_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 #define  AFI_INTR_AXI_DECODE_ERROR      8
125 #define  AFI_INTR_FPCI_TIMEOUT          9
126 #define  AFI_INTR_PE_PRSNT_SENSE        10
127 #define  AFI_INTR_PE_CLKREQ_SENSE       11
128 #define  AFI_INTR_CLKCLAMP_SENSE        12
129 #define  AFI_INTR_RDY4PD_SENSE          13
130 #define  AFI_INTR_P2P_ERROR             14
131
132 #define AFI_INTR_SIGNATURE      0xbc
133 #define AFI_UPPER_FPCI_ADDRESS  0xc0
134 #define AFI_SM_INTR_ENABLE      0xc4
135 #define  AFI_SM_INTR_INTA_ASSERT        (1 << 0)
136 #define  AFI_SM_INTR_INTB_ASSERT        (1 << 1)
137 #define  AFI_SM_INTR_INTC_ASSERT        (1 << 2)
138 #define  AFI_SM_INTR_INTD_ASSERT        (1 << 3)
139 #define  AFI_SM_INTR_INTA_DEASSERT      (1 << 4)
140 #define  AFI_SM_INTR_INTB_DEASSERT      (1 << 5)
141 #define  AFI_SM_INTR_INTC_DEASSERT      (1 << 6)
142 #define  AFI_SM_INTR_INTD_DEASSERT      (1 << 7)
143
144 #define AFI_AFI_INTR_ENABLE             0xc8
145 #define  AFI_INTR_EN_INI_SLVERR         (1 << 0)
146 #define  AFI_INTR_EN_INI_DECERR         (1 << 1)
147 #define  AFI_INTR_EN_TGT_SLVERR         (1 << 2)
148 #define  AFI_INTR_EN_TGT_DECERR         (1 << 3)
149 #define  AFI_INTR_EN_TGT_WRERR          (1 << 4)
150 #define  AFI_INTR_EN_DFPCI_DECERR       (1 << 5)
151 #define  AFI_INTR_EN_AXI_DECERR         (1 << 6)
152 #define  AFI_INTR_EN_FPCI_TIMEOUT       (1 << 7)
153 #define  AFI_INTR_EN_PRSNT_SENSE        (1 << 8)
154
155 #define AFI_PCIE_CONFIG                                 0x0f8
156 #define  AFI_PCIE_CONFIG_PCIE_DISABLE(x)                (1 << ((x) + 1))
157 #define  AFI_PCIE_CONFIG_PCIE_DISABLE_ALL               0xe
158 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK       (0xf << 20)
159 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE     (0x0 << 20)
160 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420        (0x0 << 20)
161 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1      (0x0 << 20)
162 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL       (0x1 << 20)
163 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222        (0x1 << 20)
164 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1      (0x1 << 20)
165 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411        (0x2 << 20)
166
167 #define AFI_FUSE                        0x104
168 #define  AFI_FUSE_PCIE_T0_GEN2_DIS      (1 << 2)
169
170 #define AFI_PEX0_CTRL                   0x110
171 #define AFI_PEX1_CTRL                   0x118
172 #define AFI_PEX2_CTRL                   0x128
173 #define  AFI_PEX_CTRL_RST               (1 << 0)
174 #define  AFI_PEX_CTRL_CLKREQ_EN         (1 << 1)
175 #define  AFI_PEX_CTRL_REFCLK_EN         (1 << 3)
176 #define  AFI_PEX_CTRL_OVERRIDE_EN       (1 << 4)
177
178 #define AFI_PLLE_CONTROL                0x160
179 #define  AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
180 #define  AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
181
182 #define AFI_PEXBIAS_CTRL_0              0x168
183
184 #define RP_VEND_XP      0x00000f00
185 #define  RP_VEND_XP_DL_UP       (1 << 30)
186
187 #define RP_VEND_CTL2 0x00000fa8
188 #define  RP_VEND_CTL2_PCA_ENABLE (1 << 7)
189
190 #define RP_PRIV_MISC    0x00000fe0
191 #define  RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
192 #define  RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
193
194 #define RP_LINK_CONTROL_STATUS                  0x00000090
195 #define  RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE  0x20000000
196 #define  RP_LINK_CONTROL_STATUS_LINKSTAT_MASK   0x3fff0000
197
198 #define PADS_CTL_SEL            0x0000009c
199
200 #define PADS_CTL                0x000000a0
201 #define  PADS_CTL_IDDQ_1L       (1 << 0)
202 #define  PADS_CTL_TX_DATA_EN_1L (1 << 6)
203 #define  PADS_CTL_RX_DATA_EN_1L (1 << 10)
204
205 #define PADS_PLL_CTL_TEGRA20                    0x000000b8
206 #define PADS_PLL_CTL_TEGRA30                    0x000000b4
207 #define  PADS_PLL_CTL_RST_B4SM                  (1 << 1)
208 #define  PADS_PLL_CTL_LOCKDET                   (1 << 8)
209 #define  PADS_PLL_CTL_REFCLK_MASK               (0x3 << 16)
210 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CML       (0 << 16)
211 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CMOS      (1 << 16)
212 #define  PADS_PLL_CTL_REFCLK_EXTERNAL           (2 << 16)
213 #define  PADS_PLL_CTL_TXCLKREF_MASK             (0x1 << 20)
214 #define  PADS_PLL_CTL_TXCLKREF_DIV10            (0 << 20)
215 #define  PADS_PLL_CTL_TXCLKREF_DIV5             (1 << 20)
216 #define  PADS_PLL_CTL_TXCLKREF_BUF_EN           (1 << 22)
217
218 #define PADS_REFCLK_CFG0                        0x000000c8
219 #define PADS_REFCLK_CFG1                        0x000000cc
220 #define PADS_REFCLK_BIAS                        0x000000d0
221
222 /*
223  * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
224  * entries, one entry per PCIe port. These field definitions and desired
225  * values aren't in the TRM, but do come from NVIDIA.
226  */
227 #define PADS_REFCLK_CFG_TERM_SHIFT              2  /* 6:2 */
228 #define PADS_REFCLK_CFG_E_TERM_SHIFT            7
229 #define PADS_REFCLK_CFG_PREDI_SHIFT             8  /* 11:8 */
230 #define PADS_REFCLK_CFG_DRVI_SHIFT              12 /* 15:12 */
231
232 struct tegra_msi {
233         struct msi_controller chip;
234         DECLARE_BITMAP(used, INT_PCI_MSI_NR);
235         struct irq_domain *domain;
236         unsigned long pages;
237         struct mutex lock;
238         u64 phys;
239         int irq;
240 };
241
242 /* used to differentiate between Tegra SoC generations */
243 struct tegra_pcie_soc {
244         unsigned int num_ports;
245         unsigned int msi_base_shift;
246         u32 pads_pll_ctl;
247         u32 tx_ref_sel;
248         u32 pads_refclk_cfg0;
249         u32 pads_refclk_cfg1;
250         bool has_pex_clkreq_en;
251         bool has_pex_bias_ctrl;
252         bool has_intr_prsnt_sense;
253         bool has_cml_clk;
254         bool has_gen2;
255         bool force_pca_enable;
256 };
257
258 static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
259 {
260         return container_of(chip, struct tegra_msi, chip);
261 }
262
263 struct tegra_pcie {
264         struct device *dev;
265
266         void __iomem *pads;
267         void __iomem *afi;
268         int irq;
269
270         struct list_head buses;
271         struct resource *cs;
272
273         struct resource io;
274         struct resource pio;
275         struct resource mem;
276         struct resource prefetch;
277         struct resource busn;
278
279         struct {
280                 resource_size_t mem;
281                 resource_size_t io;
282         } offset;
283
284         struct clk *pex_clk;
285         struct clk *afi_clk;
286         struct clk *pll_e;
287         struct clk *cml_clk;
288
289         struct reset_control *pex_rst;
290         struct reset_control *afi_rst;
291         struct reset_control *pcie_xrst;
292
293         bool legacy_phy;
294         struct phy *phy;
295
296         struct tegra_msi msi;
297
298         struct list_head ports;
299         u32 xbar_config;
300
301         struct regulator_bulk_data *supplies;
302         unsigned int num_supplies;
303
304         const struct tegra_pcie_soc *soc;
305         struct dentry *debugfs;
306 };
307
308 struct tegra_pcie_port {
309         struct tegra_pcie *pcie;
310         struct device_node *np;
311         struct list_head list;
312         struct resource regs;
313         void __iomem *base;
314         unsigned int index;
315         unsigned int lanes;
316
317         struct phy **phys;
318 };
319
320 struct tegra_pcie_bus {
321         struct vm_struct *area;
322         struct list_head list;
323         unsigned int nr;
324 };
325
326 static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
327                               unsigned long offset)
328 {
329         writel(value, pcie->afi + offset);
330 }
331
332 static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
333 {
334         return readl(pcie->afi + offset);
335 }
336
337 static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
338                                unsigned long offset)
339 {
340         writel(value, pcie->pads + offset);
341 }
342
343 static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
344 {
345         return readl(pcie->pads + offset);
346 }
347
348 /*
349  * The configuration space mapping on Tegra is somewhat similar to the ECAM
350  * defined by PCIe. However it deviates a bit in how the 4 bits for extended
351  * register accesses are mapped:
352  *
353  *    [27:24] extended register number
354  *    [23:16] bus number
355  *    [15:11] device number
356  *    [10: 8] function number
357  *    [ 7: 0] register number
358  *
359  * Mapping the whole extended configuration space would require 256 MiB of
360  * virtual address space, only a small part of which will actually be used.
361  * To work around this, a 1 MiB of virtual addresses are allocated per bus
362  * when the bus is first accessed. When the physical range is mapped, the
363  * the bus number bits are hidden so that the extended register number bits
364  * appear as bits [19:16]. Therefore the virtual mapping looks like this:
365  *
366  *    [19:16] extended register number
367  *    [15:11] device number
368  *    [10: 8] function number
369  *    [ 7: 0] register number
370  *
371  * This is achieved by stitching together 16 chunks of 64 KiB of physical
372  * address space via the MMU.
373  */
374 static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
375 {
376         return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
377                (PCI_FUNC(devfn) << 8) | (where & 0xfc);
378 }
379
380 static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
381                                                    unsigned int busnr)
382 {
383         struct device *dev = pcie->dev;
384         pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
385         phys_addr_t cs = pcie->cs->start;
386         struct tegra_pcie_bus *bus;
387         unsigned int i;
388         int err;
389
390         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
391         if (!bus)
392                 return ERR_PTR(-ENOMEM);
393
394         INIT_LIST_HEAD(&bus->list);
395         bus->nr = busnr;
396
397         /* allocate 1 MiB of virtual addresses */
398         bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
399         if (!bus->area) {
400                 err = -ENOMEM;
401                 goto free;
402         }
403
404         /* map each of the 16 chunks of 64 KiB each */
405         for (i = 0; i < 16; i++) {
406                 unsigned long virt = (unsigned long)bus->area->addr +
407                                      i * SZ_64K;
408                 phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
409
410                 err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
411                 if (err < 0) {
412                         dev_err(dev, "ioremap_page_range() failed: %d\n", err);
413                         goto unmap;
414                 }
415         }
416
417         return bus;
418
419 unmap:
420         vunmap(bus->area->addr);
421 free:
422         kfree(bus);
423         return ERR_PTR(err);
424 }
425
426 static int tegra_pcie_add_bus(struct pci_bus *bus)
427 {
428         struct pci_host_bridge *host = pci_find_host_bridge(bus);
429         struct tegra_pcie *pcie = pci_host_bridge_priv(host);
430         struct tegra_pcie_bus *b;
431
432         b = tegra_pcie_bus_alloc(pcie, bus->number);
433         if (IS_ERR(b))
434                 return PTR_ERR(b);
435
436         list_add_tail(&b->list, &pcie->buses);
437
438         return 0;
439 }
440
441 static void tegra_pcie_remove_bus(struct pci_bus *child)
442 {
443         struct pci_host_bridge *host = pci_find_host_bridge(child);
444         struct tegra_pcie *pcie = pci_host_bridge_priv(host);
445         struct tegra_pcie_bus *bus, *tmp;
446
447         list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
448                 if (bus->nr == child->number) {
449                         vunmap(bus->area->addr);
450                         list_del(&bus->list);
451                         kfree(bus);
452                         break;
453                 }
454         }
455 }
456
457 static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
458                                         unsigned int devfn,
459                                         int where)
460 {
461         struct pci_host_bridge *host = pci_find_host_bridge(bus);
462         struct tegra_pcie *pcie = pci_host_bridge_priv(host);
463         struct device *dev = pcie->dev;
464         void __iomem *addr = NULL;
465
466         if (bus->number == 0) {
467                 unsigned int slot = PCI_SLOT(devfn);
468                 struct tegra_pcie_port *port;
469
470                 list_for_each_entry(port, &pcie->ports, list) {
471                         if (port->index + 1 == slot) {
472                                 addr = port->base + (where & ~3);
473                                 break;
474                         }
475                 }
476         } else {
477                 struct tegra_pcie_bus *b;
478
479                 list_for_each_entry(b, &pcie->buses, list)
480                         if (b->nr == bus->number)
481                                 addr = (void __iomem *)b->area->addr;
482
483                 if (!addr) {
484                         dev_err(dev, "failed to map cfg. space for bus %u\n",
485                                 bus->number);
486                         return NULL;
487                 }
488
489                 addr += tegra_pcie_conf_offset(devfn, where);
490         }
491
492         return addr;
493 }
494
495 static struct pci_ops tegra_pcie_ops = {
496         .add_bus = tegra_pcie_add_bus,
497         .remove_bus = tegra_pcie_remove_bus,
498         .map_bus = tegra_pcie_map_bus,
499         .read = pci_generic_config_read32,
500         .write = pci_generic_config_write32,
501 };
502
503 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
504 {
505         unsigned long ret = 0;
506
507         switch (port->index) {
508         case 0:
509                 ret = AFI_PEX0_CTRL;
510                 break;
511
512         case 1:
513                 ret = AFI_PEX1_CTRL;
514                 break;
515
516         case 2:
517                 ret = AFI_PEX2_CTRL;
518                 break;
519         }
520
521         return ret;
522 }
523
524 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
525 {
526         unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
527         unsigned long value;
528
529         /* pulse reset signal */
530         value = afi_readl(port->pcie, ctrl);
531         value &= ~AFI_PEX_CTRL_RST;
532         afi_writel(port->pcie, value, ctrl);
533
534         usleep_range(1000, 2000);
535
536         value = afi_readl(port->pcie, ctrl);
537         value |= AFI_PEX_CTRL_RST;
538         afi_writel(port->pcie, value, ctrl);
539 }
540
541 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
542 {
543         unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
544         const struct tegra_pcie_soc *soc = port->pcie->soc;
545         unsigned long value;
546
547         /* enable reference clock */
548         value = afi_readl(port->pcie, ctrl);
549         value |= AFI_PEX_CTRL_REFCLK_EN;
550
551         if (soc->has_pex_clkreq_en)
552                 value |= AFI_PEX_CTRL_CLKREQ_EN;
553
554         value |= AFI_PEX_CTRL_OVERRIDE_EN;
555
556         afi_writel(port->pcie, value, ctrl);
557
558         tegra_pcie_port_reset(port);
559
560         if (soc->force_pca_enable) {
561                 value = readl(port->base + RP_VEND_CTL2);
562                 value |= RP_VEND_CTL2_PCA_ENABLE;
563                 writel(value, port->base + RP_VEND_CTL2);
564         }
565 }
566
567 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
568 {
569         unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
570         const struct tegra_pcie_soc *soc = port->pcie->soc;
571         unsigned long value;
572
573         /* assert port reset */
574         value = afi_readl(port->pcie, ctrl);
575         value &= ~AFI_PEX_CTRL_RST;
576         afi_writel(port->pcie, value, ctrl);
577
578         /* disable reference clock */
579         value = afi_readl(port->pcie, ctrl);
580
581         if (soc->has_pex_clkreq_en)
582                 value &= ~AFI_PEX_CTRL_CLKREQ_EN;
583
584         value &= ~AFI_PEX_CTRL_REFCLK_EN;
585         afi_writel(port->pcie, value, ctrl);
586 }
587
588 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
589 {
590         struct tegra_pcie *pcie = port->pcie;
591         struct device *dev = pcie->dev;
592
593         devm_iounmap(dev, port->base);
594         devm_release_mem_region(dev, port->regs.start,
595                                 resource_size(&port->regs));
596         list_del(&port->list);
597         devm_kfree(dev, port);
598 }
599
600 /* Tegra PCIE root complex wrongly reports device class */
601 static void tegra_pcie_fixup_class(struct pci_dev *dev)
602 {
603         dev->class = PCI_CLASS_BRIDGE_PCI << 8;
604 }
605 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
606 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
607 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
608 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
609
610 /* Tegra20 and Tegra30 PCIE requires relaxed ordering */
611 static void tegra_pcie_relax_enable(struct pci_dev *dev)
612 {
613         pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
614 }
615 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_relax_enable);
616 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_relax_enable);
617 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_relax_enable);
618 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_relax_enable);
619
620 static int tegra_pcie_request_resources(struct tegra_pcie *pcie)
621 {
622         struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
623         struct list_head *windows = &host->windows;
624         struct device *dev = pcie->dev;
625         int err;
626
627         pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
628         pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
629         pci_add_resource_offset(windows, &pcie->prefetch, pcie->offset.mem);
630         pci_add_resource(windows, &pcie->busn);
631
632         err = devm_request_pci_bus_resources(dev, windows);
633         if (err < 0)
634                 return err;
635
636         pci_remap_iospace(&pcie->pio, pcie->io.start);
637
638         return 0;
639 }
640
641 static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
642 {
643         struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
644         struct tegra_pcie *pcie = pci_host_bridge_priv(host);
645         int irq;
646
647         tegra_cpuidle_pcie_irqs_in_use();
648
649         irq = of_irq_parse_and_map_pci(pdev, slot, pin);
650         if (!irq)
651                 irq = pcie->irq;
652
653         return irq;
654 }
655
656 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
657 {
658         const char *err_msg[] = {
659                 "Unknown",
660                 "AXI slave error",
661                 "AXI decode error",
662                 "Target abort",
663                 "Master abort",
664                 "Invalid write",
665                 "Legacy interrupt",
666                 "Response decoding error",
667                 "AXI response decoding error",
668                 "Transaction timeout",
669                 "Slot present pin change",
670                 "Slot clock request change",
671                 "TMS clock ramp change",
672                 "TMS ready for power down",
673                 "Peer2Peer error",
674         };
675         struct tegra_pcie *pcie = arg;
676         struct device *dev = pcie->dev;
677         u32 code, signature;
678
679         code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
680         signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
681         afi_writel(pcie, 0, AFI_INTR_CODE);
682
683         if (code == AFI_INTR_LEGACY)
684                 return IRQ_NONE;
685
686         if (code >= ARRAY_SIZE(err_msg))
687                 code = 0;
688
689         /*
690          * do not pollute kernel log with master abort reports since they
691          * happen a lot during enumeration
692          */
693         if (code == AFI_INTR_MASTER_ABORT)
694                 dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
695         else
696                 dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
697
698         if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
699             code == AFI_INTR_FPCI_DECODE_ERROR) {
700                 u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
701                 u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
702
703                 if (code == AFI_INTR_MASTER_ABORT)
704                         dev_dbg(dev, "  FPCI address: %10llx\n", address);
705                 else
706                         dev_err(dev, "  FPCI address: %10llx\n", address);
707         }
708
709         return IRQ_HANDLED;
710 }
711
712 /*
713  * FPCI map is as follows:
714  * - 0xfdfc000000: I/O space
715  * - 0xfdfe000000: type 0 configuration space
716  * - 0xfdff000000: type 1 configuration space
717  * - 0xfe00000000: type 0 extended configuration space
718  * - 0xfe10000000: type 1 extended configuration space
719  */
720 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
721 {
722         u32 fpci_bar, size, axi_address;
723
724         /* Bar 0: type 1 extended configuration space */
725         fpci_bar = 0xfe100000;
726         size = resource_size(pcie->cs);
727         axi_address = pcie->cs->start;
728         afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
729         afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
730         afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
731
732         /* Bar 1: downstream IO bar */
733         fpci_bar = 0xfdfc0000;
734         size = resource_size(&pcie->io);
735         axi_address = pcie->io.start;
736         afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
737         afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
738         afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
739
740         /* Bar 2: prefetchable memory BAR */
741         fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
742         size = resource_size(&pcie->prefetch);
743         axi_address = pcie->prefetch.start;
744         afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
745         afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
746         afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
747
748         /* Bar 3: non prefetchable memory BAR */
749         fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
750         size = resource_size(&pcie->mem);
751         axi_address = pcie->mem.start;
752         afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
753         afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
754         afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
755
756         /* NULL out the remaining BARs as they are not used */
757         afi_writel(pcie, 0, AFI_AXI_BAR4_START);
758         afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
759         afi_writel(pcie, 0, AFI_FPCI_BAR4);
760
761         afi_writel(pcie, 0, AFI_AXI_BAR5_START);
762         afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
763         afi_writel(pcie, 0, AFI_FPCI_BAR5);
764
765         /* map all upstream transactions as uncached */
766         afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
767         afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
768         afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
769         afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
770
771         /* MSI translations are setup only when needed */
772         afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
773         afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
774         afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
775         afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
776 }
777
778 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
779 {
780         const struct tegra_pcie_soc *soc = pcie->soc;
781         u32 value;
782
783         timeout = jiffies + msecs_to_jiffies(timeout);
784
785         while (time_before(jiffies, timeout)) {
786                 value = pads_readl(pcie, soc->pads_pll_ctl);
787                 if (value & PADS_PLL_CTL_LOCKDET)
788                         return 0;
789         }
790
791         return -ETIMEDOUT;
792 }
793
794 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
795 {
796         struct device *dev = pcie->dev;
797         const struct tegra_pcie_soc *soc = pcie->soc;
798         u32 value;
799         int err;
800
801         /* initialize internal PHY, enable up to 16 PCIE lanes */
802         pads_writel(pcie, 0x0, PADS_CTL_SEL);
803
804         /* override IDDQ to 1 on all 4 lanes */
805         value = pads_readl(pcie, PADS_CTL);
806         value |= PADS_CTL_IDDQ_1L;
807         pads_writel(pcie, value, PADS_CTL);
808
809         /*
810          * Set up PHY PLL inputs select PLLE output as refclock,
811          * set TX ref sel to div10 (not div5).
812          */
813         value = pads_readl(pcie, soc->pads_pll_ctl);
814         value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
815         value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
816         pads_writel(pcie, value, soc->pads_pll_ctl);
817
818         /* reset PLL */
819         value = pads_readl(pcie, soc->pads_pll_ctl);
820         value &= ~PADS_PLL_CTL_RST_B4SM;
821         pads_writel(pcie, value, soc->pads_pll_ctl);
822
823         usleep_range(20, 100);
824
825         /* take PLL out of reset  */
826         value = pads_readl(pcie, soc->pads_pll_ctl);
827         value |= PADS_PLL_CTL_RST_B4SM;
828         pads_writel(pcie, value, soc->pads_pll_ctl);
829
830         /* wait for the PLL to lock */
831         err = tegra_pcie_pll_wait(pcie, 500);
832         if (err < 0) {
833                 dev_err(dev, "PLL failed to lock: %d\n", err);
834                 return err;
835         }
836
837         /* turn off IDDQ override */
838         value = pads_readl(pcie, PADS_CTL);
839         value &= ~PADS_CTL_IDDQ_1L;
840         pads_writel(pcie, value, PADS_CTL);
841
842         /* enable TX/RX data */
843         value = pads_readl(pcie, PADS_CTL);
844         value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
845         pads_writel(pcie, value, PADS_CTL);
846
847         return 0;
848 }
849
850 static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
851 {
852         const struct tegra_pcie_soc *soc = pcie->soc;
853         u32 value;
854
855         /* disable TX/RX data */
856         value = pads_readl(pcie, PADS_CTL);
857         value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
858         pads_writel(pcie, value, PADS_CTL);
859
860         /* override IDDQ */
861         value = pads_readl(pcie, PADS_CTL);
862         value |= PADS_CTL_IDDQ_1L;
863         pads_writel(pcie, value, PADS_CTL);
864
865         /* reset PLL */
866         value = pads_readl(pcie, soc->pads_pll_ctl);
867         value &= ~PADS_PLL_CTL_RST_B4SM;
868         pads_writel(pcie, value, soc->pads_pll_ctl);
869
870         usleep_range(20, 100);
871
872         return 0;
873 }
874
875 static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
876 {
877         struct device *dev = port->pcie->dev;
878         unsigned int i;
879         int err;
880
881         for (i = 0; i < port->lanes; i++) {
882                 err = phy_power_on(port->phys[i]);
883                 if (err < 0) {
884                         dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
885                         return err;
886                 }
887         }
888
889         return 0;
890 }
891
892 static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
893 {
894         struct device *dev = port->pcie->dev;
895         unsigned int i;
896         int err;
897
898         for (i = 0; i < port->lanes; i++) {
899                 err = phy_power_off(port->phys[i]);
900                 if (err < 0) {
901                         dev_err(dev, "failed to power off PHY#%u: %d\n", i,
902                                 err);
903                         return err;
904                 }
905         }
906
907         return 0;
908 }
909
910 static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
911 {
912         struct device *dev = pcie->dev;
913         const struct tegra_pcie_soc *soc = pcie->soc;
914         struct tegra_pcie_port *port;
915         int err;
916
917         if (pcie->legacy_phy) {
918                 if (pcie->phy)
919                         err = phy_power_on(pcie->phy);
920                 else
921                         err = tegra_pcie_phy_enable(pcie);
922
923                 if (err < 0)
924                         dev_err(dev, "failed to power on PHY: %d\n", err);
925
926                 return err;
927         }
928
929         list_for_each_entry(port, &pcie->ports, list) {
930                 err = tegra_pcie_port_phy_power_on(port);
931                 if (err < 0) {
932                         dev_err(dev,
933                                 "failed to power on PCIe port %u PHY: %d\n",
934                                 port->index, err);
935                         return err;
936                 }
937         }
938
939         /* Configure the reference clock driver */
940         pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
941
942         if (soc->num_ports > 2)
943                 pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
944
945         return 0;
946 }
947
948 static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
949 {
950         struct device *dev = pcie->dev;
951         struct tegra_pcie_port *port;
952         int err;
953
954         if (pcie->legacy_phy) {
955                 if (pcie->phy)
956                         err = phy_power_off(pcie->phy);
957                 else
958                         err = tegra_pcie_phy_disable(pcie);
959
960                 if (err < 0)
961                         dev_err(dev, "failed to power off PHY: %d\n", err);
962
963                 return err;
964         }
965
966         list_for_each_entry(port, &pcie->ports, list) {
967                 err = tegra_pcie_port_phy_power_off(port);
968                 if (err < 0) {
969                         dev_err(dev,
970                                 "failed to power off PCIe port %u PHY: %d\n",
971                                 port->index, err);
972                         return err;
973                 }
974         }
975
976         return 0;
977 }
978
979 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
980 {
981         struct device *dev = pcie->dev;
982         const struct tegra_pcie_soc *soc = pcie->soc;
983         struct tegra_pcie_port *port;
984         unsigned long value;
985         int err;
986
987         /* enable PLL power down */
988         if (pcie->phy) {
989                 value = afi_readl(pcie, AFI_PLLE_CONTROL);
990                 value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
991                 value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
992                 afi_writel(pcie, value, AFI_PLLE_CONTROL);
993         }
994
995         /* power down PCIe slot clock bias pad */
996         if (soc->has_pex_bias_ctrl)
997                 afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
998
999         /* configure mode and disable all ports */
1000         value = afi_readl(pcie, AFI_PCIE_CONFIG);
1001         value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1002         value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
1003
1004         list_for_each_entry(port, &pcie->ports, list)
1005                 value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
1006
1007         afi_writel(pcie, value, AFI_PCIE_CONFIG);
1008
1009         if (soc->has_gen2) {
1010                 value = afi_readl(pcie, AFI_FUSE);
1011                 value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1012                 afi_writel(pcie, value, AFI_FUSE);
1013         } else {
1014                 value = afi_readl(pcie, AFI_FUSE);
1015                 value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
1016                 afi_writel(pcie, value, AFI_FUSE);
1017         }
1018
1019         err = tegra_pcie_phy_power_on(pcie);
1020         if (err < 0) {
1021                 dev_err(dev, "failed to power on PHY(s): %d\n", err);
1022                 return err;
1023         }
1024
1025         /* take the PCIe interface module out of reset */
1026         reset_control_deassert(pcie->pcie_xrst);
1027
1028         /* finally enable PCIe */
1029         value = afi_readl(pcie, AFI_CONFIGURATION);
1030         value |= AFI_CONFIGURATION_EN_FPCI;
1031         afi_writel(pcie, value, AFI_CONFIGURATION);
1032
1033         value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
1034                 AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
1035                 AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
1036
1037         if (soc->has_intr_prsnt_sense)
1038                 value |= AFI_INTR_EN_PRSNT_SENSE;
1039
1040         afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
1041         afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
1042
1043         /* don't enable MSI for now, only when needed */
1044         afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
1045
1046         /* disable all exceptions */
1047         afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
1048
1049         return 0;
1050 }
1051
1052 static void tegra_pcie_power_off(struct tegra_pcie *pcie)
1053 {
1054         struct device *dev = pcie->dev;
1055         int err;
1056
1057         /* TODO: disable and unprepare clocks? */
1058
1059         err = tegra_pcie_phy_power_off(pcie);
1060         if (err < 0)
1061                 dev_err(dev, "failed to power off PHY(s): %d\n", err);
1062
1063         reset_control_assert(pcie->pcie_xrst);
1064         reset_control_assert(pcie->afi_rst);
1065         reset_control_assert(pcie->pex_rst);
1066
1067         tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1068
1069         err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
1070         if (err < 0)
1071                 dev_warn(dev, "failed to disable regulators: %d\n", err);
1072 }
1073
1074 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
1075 {
1076         struct device *dev = pcie->dev;
1077         const struct tegra_pcie_soc *soc = pcie->soc;
1078         int err;
1079
1080         reset_control_assert(pcie->pcie_xrst);
1081         reset_control_assert(pcie->afi_rst);
1082         reset_control_assert(pcie->pex_rst);
1083
1084         tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
1085
1086         /* enable regulators */
1087         err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
1088         if (err < 0)
1089                 dev_err(dev, "failed to enable regulators: %d\n", err);
1090
1091         err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
1092                                                 pcie->pex_clk,
1093                                                 pcie->pex_rst);
1094         if (err) {
1095                 dev_err(dev, "powerup sequence failed: %d\n", err);
1096                 return err;
1097         }
1098
1099         reset_control_deassert(pcie->afi_rst);
1100
1101         err = clk_prepare_enable(pcie->afi_clk);
1102         if (err < 0) {
1103                 dev_err(dev, "failed to enable AFI clock: %d\n", err);
1104                 return err;
1105         }
1106
1107         if (soc->has_cml_clk) {
1108                 err = clk_prepare_enable(pcie->cml_clk);
1109                 if (err < 0) {
1110                         dev_err(dev, "failed to enable CML clock: %d\n", err);
1111                         return err;
1112                 }
1113         }
1114
1115         err = clk_prepare_enable(pcie->pll_e);
1116         if (err < 0) {
1117                 dev_err(dev, "failed to enable PLLE clock: %d\n", err);
1118                 return err;
1119         }
1120
1121         return 0;
1122 }
1123
1124 static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
1125 {
1126         struct device *dev = pcie->dev;
1127         const struct tegra_pcie_soc *soc = pcie->soc;
1128
1129         pcie->pex_clk = devm_clk_get(dev, "pex");
1130         if (IS_ERR(pcie->pex_clk))
1131                 return PTR_ERR(pcie->pex_clk);
1132
1133         pcie->afi_clk = devm_clk_get(dev, "afi");
1134         if (IS_ERR(pcie->afi_clk))
1135                 return PTR_ERR(pcie->afi_clk);
1136
1137         pcie->pll_e = devm_clk_get(dev, "pll_e");
1138         if (IS_ERR(pcie->pll_e))
1139                 return PTR_ERR(pcie->pll_e);
1140
1141         if (soc->has_cml_clk) {
1142                 pcie->cml_clk = devm_clk_get(dev, "cml");
1143                 if (IS_ERR(pcie->cml_clk))
1144                         return PTR_ERR(pcie->cml_clk);
1145         }
1146
1147         return 0;
1148 }
1149
1150 static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
1151 {
1152         struct device *dev = pcie->dev;
1153
1154         pcie->pex_rst = devm_reset_control_get_exclusive(dev, "pex");
1155         if (IS_ERR(pcie->pex_rst))
1156                 return PTR_ERR(pcie->pex_rst);
1157
1158         pcie->afi_rst = devm_reset_control_get_exclusive(dev, "afi");
1159         if (IS_ERR(pcie->afi_rst))
1160                 return PTR_ERR(pcie->afi_rst);
1161
1162         pcie->pcie_xrst = devm_reset_control_get_exclusive(dev, "pcie_x");
1163         if (IS_ERR(pcie->pcie_xrst))
1164                 return PTR_ERR(pcie->pcie_xrst);
1165
1166         return 0;
1167 }
1168
1169 static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
1170 {
1171         struct device *dev = pcie->dev;
1172         int err;
1173
1174         pcie->phy = devm_phy_optional_get(dev, "pcie");
1175         if (IS_ERR(pcie->phy)) {
1176                 err = PTR_ERR(pcie->phy);
1177                 dev_err(dev, "failed to get PHY: %d\n", err);
1178                 return err;
1179         }
1180
1181         err = phy_init(pcie->phy);
1182         if (err < 0) {
1183                 dev_err(dev, "failed to initialize PHY: %d\n", err);
1184                 return err;
1185         }
1186
1187         pcie->legacy_phy = true;
1188
1189         return 0;
1190 }
1191
1192 static struct phy *devm_of_phy_optional_get_index(struct device *dev,
1193                                                   struct device_node *np,
1194                                                   const char *consumer,
1195                                                   unsigned int index)
1196 {
1197         struct phy *phy;
1198         char *name;
1199
1200         name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
1201         if (!name)
1202                 return ERR_PTR(-ENOMEM);
1203
1204         phy = devm_of_phy_get(dev, np, name);
1205         kfree(name);
1206
1207         if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
1208                 phy = NULL;
1209
1210         return phy;
1211 }
1212
1213 static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
1214 {
1215         struct device *dev = port->pcie->dev;
1216         struct phy *phy;
1217         unsigned int i;
1218         int err;
1219
1220         port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
1221         if (!port->phys)
1222                 return -ENOMEM;
1223
1224         for (i = 0; i < port->lanes; i++) {
1225                 phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
1226                 if (IS_ERR(phy)) {
1227                         dev_err(dev, "failed to get PHY#%u: %ld\n", i,
1228                                 PTR_ERR(phy));
1229                         return PTR_ERR(phy);
1230                 }
1231
1232                 err = phy_init(phy);
1233                 if (err < 0) {
1234                         dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
1235                                 err);
1236                         return err;
1237                 }
1238
1239                 port->phys[i] = phy;
1240         }
1241
1242         return 0;
1243 }
1244
1245 static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
1246 {
1247         const struct tegra_pcie_soc *soc = pcie->soc;
1248         struct device_node *np = pcie->dev->of_node;
1249         struct tegra_pcie_port *port;
1250         int err;
1251
1252         if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
1253                 return tegra_pcie_phys_get_legacy(pcie);
1254
1255         list_for_each_entry(port, &pcie->ports, list) {
1256                 err = tegra_pcie_port_get_phys(port);
1257                 if (err < 0)
1258                         return err;
1259         }
1260
1261         return 0;
1262 }
1263
1264 static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
1265 {
1266         struct device *dev = pcie->dev;
1267         struct platform_device *pdev = to_platform_device(dev);
1268         struct resource *pads, *afi, *res;
1269         int err;
1270
1271         err = tegra_pcie_clocks_get(pcie);
1272         if (err) {
1273                 dev_err(dev, "failed to get clocks: %d\n", err);
1274                 return err;
1275         }
1276
1277         err = tegra_pcie_resets_get(pcie);
1278         if (err) {
1279                 dev_err(dev, "failed to get resets: %d\n", err);
1280                 return err;
1281         }
1282
1283         err = tegra_pcie_phys_get(pcie);
1284         if (err < 0) {
1285                 dev_err(dev, "failed to get PHYs: %d\n", err);
1286                 return err;
1287         }
1288
1289         err = tegra_pcie_power_on(pcie);
1290         if (err) {
1291                 dev_err(dev, "failed to power up: %d\n", err);
1292                 return err;
1293         }
1294
1295         pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
1296         pcie->pads = devm_ioremap_resource(dev, pads);
1297         if (IS_ERR(pcie->pads)) {
1298                 err = PTR_ERR(pcie->pads);
1299                 goto poweroff;
1300         }
1301
1302         afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
1303         pcie->afi = devm_ioremap_resource(dev, afi);
1304         if (IS_ERR(pcie->afi)) {
1305                 err = PTR_ERR(pcie->afi);
1306                 goto poweroff;
1307         }
1308
1309         /* request configuration space, but remap later, on demand */
1310         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
1311         if (!res) {
1312                 err = -EADDRNOTAVAIL;
1313                 goto poweroff;
1314         }
1315
1316         pcie->cs = devm_request_mem_region(dev, res->start,
1317                                            resource_size(res), res->name);
1318         if (!pcie->cs) {
1319                 err = -EADDRNOTAVAIL;
1320                 goto poweroff;
1321         }
1322
1323         /* request interrupt */
1324         err = platform_get_irq_byname(pdev, "intr");
1325         if (err < 0) {
1326                 dev_err(dev, "failed to get IRQ: %d\n", err);
1327                 goto poweroff;
1328         }
1329
1330         pcie->irq = err;
1331
1332         err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
1333         if (err) {
1334                 dev_err(dev, "failed to register IRQ: %d\n", err);
1335                 goto poweroff;
1336         }
1337
1338         return 0;
1339
1340 poweroff:
1341         tegra_pcie_power_off(pcie);
1342         return err;
1343 }
1344
1345 static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
1346 {
1347         struct device *dev = pcie->dev;
1348         int err;
1349
1350         if (pcie->irq > 0)
1351                 free_irq(pcie->irq, pcie);
1352
1353         tegra_pcie_power_off(pcie);
1354
1355         err = phy_exit(pcie->phy);
1356         if (err < 0)
1357                 dev_err(dev, "failed to teardown PHY: %d\n", err);
1358
1359         return 0;
1360 }
1361
1362 static int tegra_msi_alloc(struct tegra_msi *chip)
1363 {
1364         int msi;
1365
1366         mutex_lock(&chip->lock);
1367
1368         msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
1369         if (msi < INT_PCI_MSI_NR)
1370                 set_bit(msi, chip->used);
1371         else
1372                 msi = -ENOSPC;
1373
1374         mutex_unlock(&chip->lock);
1375
1376         return msi;
1377 }
1378
1379 static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
1380 {
1381         struct device *dev = chip->chip.dev;
1382
1383         mutex_lock(&chip->lock);
1384
1385         if (!test_bit(irq, chip->used))
1386                 dev_err(dev, "trying to free unused MSI#%lu\n", irq);
1387         else
1388                 clear_bit(irq, chip->used);
1389
1390         mutex_unlock(&chip->lock);
1391 }
1392
1393 static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
1394 {
1395         struct tegra_pcie *pcie = data;
1396         struct device *dev = pcie->dev;
1397         struct tegra_msi *msi = &pcie->msi;
1398         unsigned int i, processed = 0;
1399
1400         for (i = 0; i < 8; i++) {
1401                 unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1402
1403                 while (reg) {
1404                         unsigned int offset = find_first_bit(&reg, 32);
1405                         unsigned int index = i * 32 + offset;
1406                         unsigned int irq;
1407
1408                         /* clear the interrupt */
1409                         afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
1410
1411                         irq = irq_find_mapping(msi->domain, index);
1412                         if (irq) {
1413                                 if (test_bit(index, msi->used))
1414                                         generic_handle_irq(irq);
1415                                 else
1416                                         dev_info(dev, "unhandled MSI\n");
1417                         } else {
1418                                 /*
1419                                  * that's weird who triggered this?
1420                                  * just clear it
1421                                  */
1422                                 dev_info(dev, "unexpected MSI\n");
1423                         }
1424
1425                         /* see if there's any more pending in this vector */
1426                         reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
1427
1428                         processed++;
1429                 }
1430         }
1431
1432         return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
1433 }
1434
1435 static int tegra_msi_setup_irq(struct msi_controller *chip,
1436                                struct pci_dev *pdev, struct msi_desc *desc)
1437 {
1438         struct tegra_msi *msi = to_tegra_msi(chip);
1439         struct msi_msg msg;
1440         unsigned int irq;
1441         int hwirq;
1442
1443         hwirq = tegra_msi_alloc(msi);
1444         if (hwirq < 0)
1445                 return hwirq;
1446
1447         irq = irq_create_mapping(msi->domain, hwirq);
1448         if (!irq) {
1449                 tegra_msi_free(msi, hwirq);
1450                 return -EINVAL;
1451         }
1452
1453         irq_set_msi_desc(irq, desc);
1454
1455         msg.address_lo = lower_32_bits(msi->phys);
1456         msg.address_hi = upper_32_bits(msi->phys);
1457         msg.data = hwirq;
1458
1459         pci_write_msi_msg(irq, &msg);
1460
1461         return 0;
1462 }
1463
1464 static void tegra_msi_teardown_irq(struct msi_controller *chip,
1465                                    unsigned int irq)
1466 {
1467         struct tegra_msi *msi = to_tegra_msi(chip);
1468         struct irq_data *d = irq_get_irq_data(irq);
1469         irq_hw_number_t hwirq = irqd_to_hwirq(d);
1470
1471         irq_dispose_mapping(irq);
1472         tegra_msi_free(msi, hwirq);
1473 }
1474
1475 static struct irq_chip tegra_msi_irq_chip = {
1476         .name = "Tegra PCIe MSI",
1477         .irq_enable = pci_msi_unmask_irq,
1478         .irq_disable = pci_msi_mask_irq,
1479         .irq_mask = pci_msi_mask_irq,
1480         .irq_unmask = pci_msi_unmask_irq,
1481 };
1482
1483 static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
1484                          irq_hw_number_t hwirq)
1485 {
1486         irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
1487         irq_set_chip_data(irq, domain->host_data);
1488
1489         tegra_cpuidle_pcie_irqs_in_use();
1490
1491         return 0;
1492 }
1493
1494 static const struct irq_domain_ops msi_domain_ops = {
1495         .map = tegra_msi_map,
1496 };
1497
1498 static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
1499 {
1500         struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1501         struct platform_device *pdev = to_platform_device(pcie->dev);
1502         const struct tegra_pcie_soc *soc = pcie->soc;
1503         struct tegra_msi *msi = &pcie->msi;
1504         struct device *dev = pcie->dev;
1505         int err;
1506         u32 reg;
1507
1508         mutex_init(&msi->lock);
1509
1510         msi->chip.dev = dev;
1511         msi->chip.setup_irq = tegra_msi_setup_irq;
1512         msi->chip.teardown_irq = tegra_msi_teardown_irq;
1513
1514         msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
1515                                             &msi_domain_ops, &msi->chip);
1516         if (!msi->domain) {
1517                 dev_err(dev, "failed to create IRQ domain\n");
1518                 return -ENOMEM;
1519         }
1520
1521         err = platform_get_irq_byname(pdev, "msi");
1522         if (err < 0) {
1523                 dev_err(dev, "failed to get IRQ: %d\n", err);
1524                 goto err;
1525         }
1526
1527         msi->irq = err;
1528
1529         err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
1530                           tegra_msi_irq_chip.name, pcie);
1531         if (err < 0) {
1532                 dev_err(dev, "failed to request IRQ: %d\n", err);
1533                 goto err;
1534         }
1535
1536         /* setup AFI/FPCI range */
1537         msi->pages = __get_free_pages(GFP_KERNEL, 0);
1538         msi->phys = virt_to_phys((void *)msi->pages);
1539
1540         afi_writel(pcie, msi->phys >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
1541         afi_writel(pcie, msi->phys, AFI_MSI_AXI_BAR_ST);
1542         /* this register is in 4K increments */
1543         afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
1544
1545         /* enable all MSI vectors */
1546         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
1547         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
1548         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
1549         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
1550         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
1551         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
1552         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
1553         afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
1554
1555         /* and unmask the MSI interrupt */
1556         reg = afi_readl(pcie, AFI_INTR_MASK);
1557         reg |= AFI_INTR_MASK_MSI_MASK;
1558         afi_writel(pcie, reg, AFI_INTR_MASK);
1559
1560         host->msi = &msi->chip;
1561
1562         return 0;
1563
1564 err:
1565         irq_domain_remove(msi->domain);
1566         return err;
1567 }
1568
1569 static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
1570 {
1571         struct tegra_msi *msi = &pcie->msi;
1572         unsigned int i, irq;
1573         u32 value;
1574
1575         /* mask the MSI interrupt */
1576         value = afi_readl(pcie, AFI_INTR_MASK);
1577         value &= ~AFI_INTR_MASK_MSI_MASK;
1578         afi_writel(pcie, value, AFI_INTR_MASK);
1579
1580         /* disable all MSI vectors */
1581         afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
1582         afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
1583         afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
1584         afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
1585         afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
1586         afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
1587         afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
1588         afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
1589
1590         free_pages(msi->pages, 0);
1591
1592         if (msi->irq > 0)
1593                 free_irq(msi->irq, pcie);
1594
1595         for (i = 0; i < INT_PCI_MSI_NR; i++) {
1596                 irq = irq_find_mapping(msi->domain, i);
1597                 if (irq > 0)
1598                         irq_dispose_mapping(irq);
1599         }
1600
1601         irq_domain_remove(msi->domain);
1602
1603         return 0;
1604 }
1605
1606 static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
1607                                       u32 *xbar)
1608 {
1609         struct device *dev = pcie->dev;
1610         struct device_node *np = dev->of_node;
1611
1612         if (of_device_is_compatible(np, "nvidia,tegra124-pcie") ||
1613             of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1614                 switch (lanes) {
1615                 case 0x0000104:
1616                         dev_info(dev, "4x1, 1x1 configuration\n");
1617                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
1618                         return 0;
1619
1620                 case 0x0000102:
1621                         dev_info(dev, "2x1, 1x1 configuration\n");
1622                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
1623                         return 0;
1624                 }
1625         } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1626                 switch (lanes) {
1627                 case 0x00000204:
1628                         dev_info(dev, "4x1, 2x1 configuration\n");
1629                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
1630                         return 0;
1631
1632                 case 0x00020202:
1633                         dev_info(dev, "2x3 configuration\n");
1634                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
1635                         return 0;
1636
1637                 case 0x00010104:
1638                         dev_info(dev, "4x1, 1x2 configuration\n");
1639                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
1640                         return 0;
1641                 }
1642         } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1643                 switch (lanes) {
1644                 case 0x00000004:
1645                         dev_info(dev, "single-mode configuration\n");
1646                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
1647                         return 0;
1648
1649                 case 0x00000202:
1650                         dev_info(dev, "dual-mode configuration\n");
1651                         *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
1652                         return 0;
1653                 }
1654         }
1655
1656         return -EINVAL;
1657 }
1658
1659 /*
1660  * Check whether a given set of supplies is available in a device tree node.
1661  * This is used to check whether the new or the legacy device tree bindings
1662  * should be used.
1663  */
1664 static bool of_regulator_bulk_available(struct device_node *np,
1665                                         struct regulator_bulk_data *supplies,
1666                                         unsigned int num_supplies)
1667 {
1668         char property[32];
1669         unsigned int i;
1670
1671         for (i = 0; i < num_supplies; i++) {
1672                 snprintf(property, 32, "%s-supply", supplies[i].supply);
1673
1674                 if (of_find_property(np, property, NULL) == NULL)
1675                         return false;
1676         }
1677
1678         return true;
1679 }
1680
1681 /*
1682  * Old versions of the device tree binding for this device used a set of power
1683  * supplies that didn't match the hardware inputs. This happened to work for a
1684  * number of cases but is not future proof. However to preserve backwards-
1685  * compatibility with old device trees, this function will try to use the old
1686  * set of supplies.
1687  */
1688 static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
1689 {
1690         struct device *dev = pcie->dev;
1691         struct device_node *np = dev->of_node;
1692
1693         if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
1694                 pcie->num_supplies = 3;
1695         else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
1696                 pcie->num_supplies = 2;
1697
1698         if (pcie->num_supplies == 0) {
1699                 dev_err(dev, "device %pOF not supported in legacy mode\n", np);
1700                 return -ENODEV;
1701         }
1702
1703         pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1704                                       sizeof(*pcie->supplies),
1705                                       GFP_KERNEL);
1706         if (!pcie->supplies)
1707                 return -ENOMEM;
1708
1709         pcie->supplies[0].supply = "pex-clk";
1710         pcie->supplies[1].supply = "vdd";
1711
1712         if (pcie->num_supplies > 2)
1713                 pcie->supplies[2].supply = "avdd";
1714
1715         return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
1716 }
1717
1718 /*
1719  * Obtains the list of regulators required for a particular generation of the
1720  * IP block.
1721  *
1722  * This would've been nice to do simply by providing static tables for use
1723  * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
1724  * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
1725  * and either seems to be optional depending on which ports are being used.
1726  */
1727 static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
1728 {
1729         struct device *dev = pcie->dev;
1730         struct device_node *np = dev->of_node;
1731         unsigned int i = 0;
1732
1733         if (of_device_is_compatible(np, "nvidia,tegra210-pcie")) {
1734                 pcie->num_supplies = 6;
1735
1736                 pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
1737                                               sizeof(*pcie->supplies),
1738                                               GFP_KERNEL);
1739                 if (!pcie->supplies)
1740                         return -ENOMEM;
1741
1742                 pcie->supplies[i++].supply = "avdd-pll-uerefe";
1743                 pcie->supplies[i++].supply = "hvddio-pex";
1744                 pcie->supplies[i++].supply = "dvddio-pex";
1745                 pcie->supplies[i++].supply = "dvdd-pex-pll";
1746                 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1747                 pcie->supplies[i++].supply = "vddio-pex-ctl";
1748         } else if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
1749                 pcie->num_supplies = 7;
1750
1751                 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1752                                               sizeof(*pcie->supplies),
1753                                               GFP_KERNEL);
1754                 if (!pcie->supplies)
1755                         return -ENOMEM;
1756
1757                 pcie->supplies[i++].supply = "avddio-pex";
1758                 pcie->supplies[i++].supply = "dvddio-pex";
1759                 pcie->supplies[i++].supply = "avdd-pex-pll";
1760                 pcie->supplies[i++].supply = "hvdd-pex";
1761                 pcie->supplies[i++].supply = "hvdd-pex-pll-e";
1762                 pcie->supplies[i++].supply = "vddio-pex-ctl";
1763                 pcie->supplies[i++].supply = "avdd-pll-erefe";
1764         } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
1765                 bool need_pexa = false, need_pexb = false;
1766
1767                 /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
1768                 if (lane_mask & 0x0f)
1769                         need_pexa = true;
1770
1771                 /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
1772                 if (lane_mask & 0x30)
1773                         need_pexb = true;
1774
1775                 pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
1776                                          (need_pexb ? 2 : 0);
1777
1778                 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1779                                               sizeof(*pcie->supplies),
1780                                               GFP_KERNEL);
1781                 if (!pcie->supplies)
1782                         return -ENOMEM;
1783
1784                 pcie->supplies[i++].supply = "avdd-pex-pll";
1785                 pcie->supplies[i++].supply = "hvdd-pex";
1786                 pcie->supplies[i++].supply = "vddio-pex-ctl";
1787                 pcie->supplies[i++].supply = "avdd-plle";
1788
1789                 if (need_pexa) {
1790                         pcie->supplies[i++].supply = "avdd-pexa";
1791                         pcie->supplies[i++].supply = "vdd-pexa";
1792                 }
1793
1794                 if (need_pexb) {
1795                         pcie->supplies[i++].supply = "avdd-pexb";
1796                         pcie->supplies[i++].supply = "vdd-pexb";
1797                 }
1798         } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
1799                 pcie->num_supplies = 5;
1800
1801                 pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
1802                                               sizeof(*pcie->supplies),
1803                                               GFP_KERNEL);
1804                 if (!pcie->supplies)
1805                         return -ENOMEM;
1806
1807                 pcie->supplies[0].supply = "avdd-pex";
1808                 pcie->supplies[1].supply = "vdd-pex";
1809                 pcie->supplies[2].supply = "avdd-pex-pll";
1810                 pcie->supplies[3].supply = "avdd-plle";
1811                 pcie->supplies[4].supply = "vddio-pex-clk";
1812         }
1813
1814         if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
1815                                         pcie->num_supplies))
1816                 return devm_regulator_bulk_get(dev, pcie->num_supplies,
1817                                                pcie->supplies);
1818
1819         /*
1820          * If not all regulators are available for this new scheme, assume
1821          * that the device tree complies with an older version of the device
1822          * tree binding.
1823          */
1824         dev_info(dev, "using legacy DT binding for power supplies\n");
1825
1826         devm_kfree(dev, pcie->supplies);
1827         pcie->num_supplies = 0;
1828
1829         return tegra_pcie_get_legacy_regulators(pcie);
1830 }
1831
1832 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
1833 {
1834         struct device *dev = pcie->dev;
1835         struct device_node *np = dev->of_node, *port;
1836         const struct tegra_pcie_soc *soc = pcie->soc;
1837         struct of_pci_range_parser parser;
1838         struct of_pci_range range;
1839         u32 lanes = 0, mask = 0;
1840         unsigned int lane = 0;
1841         struct resource res;
1842         int err;
1843
1844         if (of_pci_range_parser_init(&parser, np)) {
1845                 dev_err(dev, "missing \"ranges\" property\n");
1846                 return -EINVAL;
1847         }
1848
1849         for_each_of_pci_range(&parser, &range) {
1850                 err = of_pci_range_to_resource(&range, np, &res);
1851                 if (err < 0)
1852                         return err;
1853
1854                 switch (res.flags & IORESOURCE_TYPE_BITS) {
1855                 case IORESOURCE_IO:
1856                         /* Track the bus -> CPU I/O mapping offset. */
1857                         pcie->offset.io = res.start - range.pci_addr;
1858
1859                         memcpy(&pcie->pio, &res, sizeof(res));
1860                         pcie->pio.name = np->full_name;
1861
1862                         /*
1863                          * The Tegra PCIe host bridge uses this to program the
1864                          * mapping of the I/O space to the physical address,
1865                          * so we override the .start and .end fields here that
1866                          * of_pci_range_to_resource() converted to I/O space.
1867                          * We also set the IORESOURCE_MEM type to clarify that
1868                          * the resource is in the physical memory space.
1869                          */
1870                         pcie->io.start = range.cpu_addr;
1871                         pcie->io.end = range.cpu_addr + range.size - 1;
1872                         pcie->io.flags = IORESOURCE_MEM;
1873                         pcie->io.name = "I/O";
1874
1875                         memcpy(&res, &pcie->io, sizeof(res));
1876                         break;
1877
1878                 case IORESOURCE_MEM:
1879                         /*
1880                          * Track the bus -> CPU memory mapping offset. This
1881                          * assumes that the prefetchable and non-prefetchable
1882                          * regions will be the last of type IORESOURCE_MEM in
1883                          * the ranges property.
1884                          * */
1885                         pcie->offset.mem = res.start - range.pci_addr;
1886
1887                         if (res.flags & IORESOURCE_PREFETCH) {
1888                                 memcpy(&pcie->prefetch, &res, sizeof(res));
1889                                 pcie->prefetch.name = "prefetchable";
1890                         } else {
1891                                 memcpy(&pcie->mem, &res, sizeof(res));
1892                                 pcie->mem.name = "non-prefetchable";
1893                         }
1894                         break;
1895                 }
1896         }
1897
1898         err = of_pci_parse_bus_range(np, &pcie->busn);
1899         if (err < 0) {
1900                 dev_err(dev, "failed to parse ranges property: %d\n", err);
1901                 pcie->busn.name = np->name;
1902                 pcie->busn.start = 0;
1903                 pcie->busn.end = 0xff;
1904                 pcie->busn.flags = IORESOURCE_BUS;
1905         }
1906
1907         /* parse root ports */
1908         for_each_child_of_node(np, port) {
1909                 struct tegra_pcie_port *rp;
1910                 unsigned int index;
1911                 u32 value;
1912
1913                 err = of_pci_get_devfn(port);
1914                 if (err < 0) {
1915                         dev_err(dev, "failed to parse address: %d\n", err);
1916                         goto err_node_put;
1917                 }
1918
1919                 index = PCI_SLOT(err);
1920
1921                 if (index < 1 || index > soc->num_ports) {
1922                         dev_err(dev, "invalid port number: %d\n", index);
1923                         err = -EINVAL;
1924                         goto err_node_put;
1925                 }
1926
1927                 index--;
1928
1929                 err = of_property_read_u32(port, "nvidia,num-lanes", &value);
1930                 if (err < 0) {
1931                         dev_err(dev, "failed to parse # of lanes: %d\n",
1932                                 err);
1933                         goto err_node_put;
1934                 }
1935
1936                 if (value > 16) {
1937                         dev_err(dev, "invalid # of lanes: %u\n", value);
1938                         err = -EINVAL;
1939                         goto err_node_put;
1940                 }
1941
1942                 lanes |= value << (index << 3);
1943
1944                 if (!of_device_is_available(port)) {
1945                         lane += value;
1946                         continue;
1947                 }
1948
1949                 mask |= ((1 << value) - 1) << lane;
1950                 lane += value;
1951
1952                 rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
1953                 if (!rp) {
1954                         err = -ENOMEM;
1955                         goto err_node_put;
1956                 }
1957
1958                 err = of_address_to_resource(port, 0, &rp->regs);
1959                 if (err < 0) {
1960                         dev_err(dev, "failed to parse address: %d\n", err);
1961                         goto err_node_put;
1962                 }
1963
1964                 INIT_LIST_HEAD(&rp->list);
1965                 rp->index = index;
1966                 rp->lanes = value;
1967                 rp->pcie = pcie;
1968                 rp->np = port;
1969
1970                 rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
1971                 if (IS_ERR(rp->base))
1972                         return PTR_ERR(rp->base);
1973
1974                 list_add_tail(&rp->list, &pcie->ports);
1975         }
1976
1977         err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
1978         if (err < 0) {
1979                 dev_err(dev, "invalid lane configuration\n");
1980                 return err;
1981         }
1982
1983         err = tegra_pcie_get_regulators(pcie, mask);
1984         if (err < 0)
1985                 return err;
1986
1987         return 0;
1988
1989 err_node_put:
1990         of_node_put(port);
1991         return err;
1992 }
1993
1994 /*
1995  * FIXME: If there are no PCIe cards attached, then calling this function
1996  * can result in the increase of the bootup time as there are big timeout
1997  * loops.
1998  */
1999 #define TEGRA_PCIE_LINKUP_TIMEOUT       200     /* up to 1.2 seconds */
2000 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
2001 {
2002         struct device *dev = port->pcie->dev;
2003         unsigned int retries = 3;
2004         unsigned long value;
2005
2006         /* override presence detection */
2007         value = readl(port->base + RP_PRIV_MISC);
2008         value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
2009         value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
2010         writel(value, port->base + RP_PRIV_MISC);
2011
2012         do {
2013                 unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2014
2015                 do {
2016                         value = readl(port->base + RP_VEND_XP);
2017
2018                         if (value & RP_VEND_XP_DL_UP)
2019                                 break;
2020
2021                         usleep_range(1000, 2000);
2022                 } while (--timeout);
2023
2024                 if (!timeout) {
2025                         dev_err(dev, "link %u down, retrying\n", port->index);
2026                         goto retry;
2027                 }
2028
2029                 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
2030
2031                 do {
2032                         value = readl(port->base + RP_LINK_CONTROL_STATUS);
2033
2034                         if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2035                                 return true;
2036
2037                         usleep_range(1000, 2000);
2038                 } while (--timeout);
2039
2040 retry:
2041                 tegra_pcie_port_reset(port);
2042         } while (--retries);
2043
2044         return false;
2045 }
2046
2047 static void tegra_pcie_enable_ports(struct tegra_pcie *pcie)
2048 {
2049         struct device *dev = pcie->dev;
2050         struct tegra_pcie_port *port, *tmp;
2051
2052         list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
2053                 dev_info(dev, "probing port %u, using %u lanes\n",
2054                          port->index, port->lanes);
2055
2056                 tegra_pcie_port_enable(port);
2057
2058                 if (tegra_pcie_port_check_link(port))
2059                         continue;
2060
2061                 dev_info(dev, "link %u down, ignoring\n", port->index);
2062
2063                 tegra_pcie_port_disable(port);
2064                 tegra_pcie_port_free(port);
2065         }
2066 }
2067
2068 static const struct tegra_pcie_soc tegra20_pcie = {
2069         .num_ports = 2,
2070         .msi_base_shift = 0,
2071         .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
2072         .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
2073         .pads_refclk_cfg0 = 0xfa5cfa5c,
2074         .has_pex_clkreq_en = false,
2075         .has_pex_bias_ctrl = false,
2076         .has_intr_prsnt_sense = false,
2077         .has_cml_clk = false,
2078         .has_gen2 = false,
2079         .force_pca_enable = false,
2080 };
2081
2082 static const struct tegra_pcie_soc tegra30_pcie = {
2083         .num_ports = 3,
2084         .msi_base_shift = 8,
2085         .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2086         .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2087         .pads_refclk_cfg0 = 0xfa5cfa5c,
2088         .pads_refclk_cfg1 = 0xfa5cfa5c,
2089         .has_pex_clkreq_en = true,
2090         .has_pex_bias_ctrl = true,
2091         .has_intr_prsnt_sense = true,
2092         .has_cml_clk = true,
2093         .has_gen2 = false,
2094         .force_pca_enable = false,
2095 };
2096
2097 static const struct tegra_pcie_soc tegra124_pcie = {
2098         .num_ports = 2,
2099         .msi_base_shift = 8,
2100         .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2101         .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2102         .pads_refclk_cfg0 = 0x44ac44ac,
2103         .has_pex_clkreq_en = true,
2104         .has_pex_bias_ctrl = true,
2105         .has_intr_prsnt_sense = true,
2106         .has_cml_clk = true,
2107         .has_gen2 = true,
2108         .force_pca_enable = false,
2109 };
2110
2111 static const struct tegra_pcie_soc tegra210_pcie = {
2112         .num_ports = 2,
2113         .msi_base_shift = 8,
2114         .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
2115         .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
2116         .pads_refclk_cfg0 = 0x90b890b8,
2117         .has_pex_clkreq_en = true,
2118         .has_pex_bias_ctrl = true,
2119         .has_intr_prsnt_sense = true,
2120         .has_cml_clk = true,
2121         .has_gen2 = true,
2122         .force_pca_enable = true,
2123 };
2124
2125 static const struct of_device_id tegra_pcie_of_match[] = {
2126         { .compatible = "nvidia,tegra210-pcie", .data = &tegra210_pcie },
2127         { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
2128         { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
2129         { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
2130         { },
2131 };
2132
2133 static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
2134 {
2135         struct tegra_pcie *pcie = s->private;
2136
2137         if (list_empty(&pcie->ports))
2138                 return NULL;
2139
2140         seq_printf(s, "Index  Status\n");
2141
2142         return seq_list_start(&pcie->ports, *pos);
2143 }
2144
2145 static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
2146 {
2147         struct tegra_pcie *pcie = s->private;
2148
2149         return seq_list_next(v, &pcie->ports, pos);
2150 }
2151
2152 static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
2153 {
2154 }
2155
2156 static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
2157 {
2158         bool up = false, active = false;
2159         struct tegra_pcie_port *port;
2160         unsigned int value;
2161
2162         port = list_entry(v, struct tegra_pcie_port, list);
2163
2164         value = readl(port->base + RP_VEND_XP);
2165
2166         if (value & RP_VEND_XP_DL_UP)
2167                 up = true;
2168
2169         value = readl(port->base + RP_LINK_CONTROL_STATUS);
2170
2171         if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
2172                 active = true;
2173
2174         seq_printf(s, "%2u     ", port->index);
2175
2176         if (up)
2177                 seq_printf(s, "up");
2178
2179         if (active) {
2180                 if (up)
2181                         seq_printf(s, ", ");
2182
2183                 seq_printf(s, "active");
2184         }
2185
2186         seq_printf(s, "\n");
2187         return 0;
2188 }
2189
2190 static const struct seq_operations tegra_pcie_ports_seq_ops = {
2191         .start = tegra_pcie_ports_seq_start,
2192         .next = tegra_pcie_ports_seq_next,
2193         .stop = tegra_pcie_ports_seq_stop,
2194         .show = tegra_pcie_ports_seq_show,
2195 };
2196
2197 static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
2198 {
2199         struct tegra_pcie *pcie = inode->i_private;
2200         struct seq_file *s;
2201         int err;
2202
2203         err = seq_open(file, &tegra_pcie_ports_seq_ops);
2204         if (err)
2205                 return err;
2206
2207         s = file->private_data;
2208         s->private = pcie;
2209
2210         return 0;
2211 }
2212
2213 static const struct file_operations tegra_pcie_ports_ops = {
2214         .owner = THIS_MODULE,
2215         .open = tegra_pcie_ports_open,
2216         .read = seq_read,
2217         .llseek = seq_lseek,
2218         .release = seq_release,
2219 };
2220
2221 static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
2222 {
2223         struct dentry *file;
2224
2225         pcie->debugfs = debugfs_create_dir("pcie", NULL);
2226         if (!pcie->debugfs)
2227                 return -ENOMEM;
2228
2229         file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
2230                                    pcie, &tegra_pcie_ports_ops);
2231         if (!file)
2232                 goto remove;
2233
2234         return 0;
2235
2236 remove:
2237         debugfs_remove_recursive(pcie->debugfs);
2238         pcie->debugfs = NULL;
2239         return -ENOMEM;
2240 }
2241
2242 static int tegra_pcie_probe(struct platform_device *pdev)
2243 {
2244         struct device *dev = &pdev->dev;
2245         struct pci_host_bridge *host;
2246         struct tegra_pcie *pcie;
2247         struct pci_bus *child;
2248         int err;
2249
2250         host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
2251         if (!host)
2252                 return -ENOMEM;
2253
2254         pcie = pci_host_bridge_priv(host);
2255
2256         pcie->soc = of_device_get_match_data(dev);
2257         INIT_LIST_HEAD(&pcie->buses);
2258         INIT_LIST_HEAD(&pcie->ports);
2259         pcie->dev = dev;
2260
2261         err = tegra_pcie_parse_dt(pcie);
2262         if (err < 0)
2263                 return err;
2264
2265         err = tegra_pcie_get_resources(pcie);
2266         if (err < 0) {
2267                 dev_err(dev, "failed to request resources: %d\n", err);
2268                 return err;
2269         }
2270
2271         err = tegra_pcie_enable_controller(pcie);
2272         if (err)
2273                 goto put_resources;
2274
2275         err = tegra_pcie_request_resources(pcie);
2276         if (err)
2277                 goto put_resources;
2278
2279         /* setup the AFI address translations */
2280         tegra_pcie_setup_translations(pcie);
2281
2282         if (IS_ENABLED(CONFIG_PCI_MSI)) {
2283                 err = tegra_pcie_enable_msi(pcie);
2284                 if (err < 0) {
2285                         dev_err(dev, "failed to enable MSI support: %d\n", err);
2286                         goto put_resources;
2287                 }
2288         }
2289
2290         tegra_pcie_enable_ports(pcie);
2291
2292         pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
2293         host->busnr = pcie->busn.start;
2294         host->dev.parent = &pdev->dev;
2295         host->ops = &tegra_pcie_ops;
2296         host->map_irq = tegra_pcie_map_irq;
2297         host->swizzle_irq = pci_common_swizzle;
2298
2299         err = pci_scan_root_bus_bridge(host);
2300         if (err < 0) {
2301                 dev_err(dev, "failed to register host: %d\n", err);
2302                 goto disable_msi;
2303         }
2304
2305         pci_bus_size_bridges(host->bus);
2306         pci_bus_assign_resources(host->bus);
2307
2308         list_for_each_entry(child, &host->bus->children, node)
2309                 pcie_bus_configure_settings(child);
2310
2311         pci_bus_add_devices(host->bus);
2312
2313         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2314                 err = tegra_pcie_debugfs_init(pcie);
2315                 if (err < 0)
2316                         dev_err(dev, "failed to setup debugfs: %d\n", err);
2317         }
2318
2319         return 0;
2320
2321 disable_msi:
2322         if (IS_ENABLED(CONFIG_PCI_MSI))
2323                 tegra_pcie_disable_msi(pcie);
2324 put_resources:
2325         tegra_pcie_put_resources(pcie);
2326         return err;
2327 }
2328
2329 static struct platform_driver tegra_pcie_driver = {
2330         .driver = {
2331                 .name = "tegra-pcie",
2332                 .of_match_table = tegra_pcie_of_match,
2333                 .suppress_bind_attrs = true,
2334         },
2335         .probe = tegra_pcie_probe,
2336 };
2337 builtin_platform_driver(tegra_pcie_driver);