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