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