3463fb5b79c7644ad84ba419fb78e95eb0ecf252
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / mach-tegra / pcie.c
1 /*
2  * arch/arm/mach-tegra/pci.c
3  *
4  * PCIe host controller driver for TEGRA(2) SOCs
5  *
6  * Copyright (c) 2010, CompuLab, Ltd.
7  * Author: Mike Rapoport <mike@compulab.co.il>
8  *
9  * Based on NVIDIA PCIe driver
10  * Copyright (c) 2008-2009, NVIDIA Corporation.
11  *
12  * Bits taken from arch/arm/mach-dove/pcie.c
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22  * more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/irq.h>
33 #include <linux/clk.h>
34 #include <linux/delay.h>
35 #include <linux/export.h>
36
37 #include <asm/sizes.h>
38 #include <asm/mach/pci.h>
39
40 #include <mach/iomap.h>
41 #include <mach/clk.h>
42 #include <mach/powergate.h>
43
44 #include "board.h"
45
46 /* register definitions */
47 #define AFI_OFFSET      0x3800
48 #define PADS_OFFSET     0x3000
49 #define RP0_OFFSET      0x0000
50 #define RP1_OFFSET      0x1000
51
52 #define AFI_AXI_BAR0_SZ 0x00
53 #define AFI_AXI_BAR1_SZ 0x04
54 #define AFI_AXI_BAR2_SZ 0x08
55 #define AFI_AXI_BAR3_SZ 0x0c
56 #define AFI_AXI_BAR4_SZ 0x10
57 #define AFI_AXI_BAR5_SZ 0x14
58
59 #define AFI_AXI_BAR0_START      0x18
60 #define AFI_AXI_BAR1_START      0x1c
61 #define AFI_AXI_BAR2_START      0x20
62 #define AFI_AXI_BAR3_START      0x24
63 #define AFI_AXI_BAR4_START      0x28
64 #define AFI_AXI_BAR5_START      0x2c
65
66 #define AFI_FPCI_BAR0   0x30
67 #define AFI_FPCI_BAR1   0x34
68 #define AFI_FPCI_BAR2   0x38
69 #define AFI_FPCI_BAR3   0x3c
70 #define AFI_FPCI_BAR4   0x40
71 #define AFI_FPCI_BAR5   0x44
72
73 #define AFI_CACHE_BAR0_SZ       0x48
74 #define AFI_CACHE_BAR0_ST       0x4c
75 #define AFI_CACHE_BAR1_SZ       0x50
76 #define AFI_CACHE_BAR1_ST       0x54
77
78 #define AFI_MSI_BAR_SZ          0x60
79 #define AFI_MSI_FPCI_BAR_ST     0x64
80 #define AFI_MSI_AXI_BAR_ST      0x68
81
82 #define AFI_CONFIGURATION               0xac
83 #define  AFI_CONFIGURATION_EN_FPCI      (1 << 0)
84
85 #define AFI_FPCI_ERROR_MASKS    0xb0
86
87 #define AFI_INTR_MASK           0xb4
88 #define  AFI_INTR_MASK_INT_MASK (1 << 0)
89 #define  AFI_INTR_MASK_MSI_MASK (1 << 8)
90
91 #define AFI_INTR_CODE           0xb8
92 #define  AFI_INTR_CODE_MASK     0xf
93 #define  AFI_INTR_MASTER_ABORT  4
94 #define  AFI_INTR_LEGACY        6
95
96 #define AFI_INTR_SIGNATURE      0xbc
97 #define AFI_SM_INTR_ENABLE      0xc4
98
99 #define AFI_AFI_INTR_ENABLE             0xc8
100 #define  AFI_INTR_EN_INI_SLVERR         (1 << 0)
101 #define  AFI_INTR_EN_INI_DECERR         (1 << 1)
102 #define  AFI_INTR_EN_TGT_SLVERR         (1 << 2)
103 #define  AFI_INTR_EN_TGT_DECERR         (1 << 3)
104 #define  AFI_INTR_EN_TGT_WRERR          (1 << 4)
105 #define  AFI_INTR_EN_DFPCI_DECERR       (1 << 5)
106 #define  AFI_INTR_EN_AXI_DECERR         (1 << 6)
107 #define  AFI_INTR_EN_FPCI_TIMEOUT       (1 << 7)
108
109 #define AFI_PCIE_CONFIG                                 0x0f8
110 #define  AFI_PCIE_CONFIG_PCIEC0_DISABLE_DEVICE          (1 << 1)
111 #define  AFI_PCIE_CONFIG_PCIEC1_DISABLE_DEVICE          (1 << 2)
112 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK       (0xf << 20)
113 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE     (0x0 << 20)
114 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL       (0x1 << 20)
115
116 #define AFI_FUSE                        0x104
117 #define  AFI_FUSE_PCIE_T0_GEN2_DIS      (1 << 2)
118
119 #define AFI_PEX0_CTRL                   0x110
120 #define AFI_PEX1_CTRL                   0x118
121 #define  AFI_PEX_CTRL_RST               (1 << 0)
122 #define  AFI_PEX_CTRL_REFCLK_EN         (1 << 3)
123
124 #define RP_VEND_XP      0x00000F00
125 #define  RP_VEND_XP_DL_UP       (1 << 30)
126
127 #define RP_LINK_CONTROL_STATUS                  0x00000090
128 #define  RP_LINK_CONTROL_STATUS_LINKSTAT_MASK   0x3fff0000
129
130 #define PADS_CTL_SEL            0x0000009C
131
132 #define PADS_CTL                0x000000A0
133 #define  PADS_CTL_IDDQ_1L       (1 << 0)
134 #define  PADS_CTL_TX_DATA_EN_1L (1 << 6)
135 #define  PADS_CTL_RX_DATA_EN_1L (1 << 10)
136
137 #define PADS_PLL_CTL                            0x000000B8
138 #define  PADS_PLL_CTL_RST_B4SM                  (1 << 1)
139 #define  PADS_PLL_CTL_LOCKDET                   (1 << 8)
140 #define  PADS_PLL_CTL_REFCLK_MASK               (0x3 << 16)
141 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CML       (0 << 16)
142 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CMOS      (1 << 16)
143 #define  PADS_PLL_CTL_REFCLK_EXTERNAL           (2 << 16)
144 #define  PADS_PLL_CTL_TXCLKREF_MASK             (0x1 << 20)
145 #define  PADS_PLL_CTL_TXCLKREF_DIV10            (0 << 20)
146 #define  PADS_PLL_CTL_TXCLKREF_DIV5             (1 << 20)
147
148 /* PMC access is required for PCIE xclk (un)clamping */
149 #define PMC_SCRATCH42           0x144
150 #define PMC_SCRATCH42_PCX_CLAMP (1 << 0)
151
152 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
153
154 #define pmc_writel(value, reg) \
155         __raw_writel(value, reg_pmc_base + (reg))
156 #define pmc_readl(reg) \
157         __raw_readl(reg_pmc_base + (reg))
158
159 /*
160  * Tegra2 defines 1GB in the AXI address map for PCIe.
161  *
162  * That address space is split into different regions, with sizes and
163  * offsets as follows:
164  *
165  * 0x80000000 - 0x80003fff - PCI controller registers
166  * 0x80004000 - 0x80103fff - PCI configuration space
167  * 0x80104000 - 0x80203fff - PCI extended configuration space
168  * 0x80203fff - 0x803fffff - unused
169  * 0x80400000 - 0x8040ffff - downstream IO
170  * 0x80410000 - 0x8fffffff - unused
171  * 0x90000000 - 0x9fffffff - non-prefetchable memory
172  * 0xa0000000 - 0xbfffffff - prefetchable memory
173  */
174 #define PCIE_REGS_SZ            SZ_16K
175 #define PCIE_CFG_OFF            PCIE_REGS_SZ
176 #define PCIE_CFG_SZ             SZ_1M
177 #define PCIE_EXT_CFG_OFF        (PCIE_CFG_SZ + PCIE_CFG_OFF)
178 #define PCIE_EXT_CFG_SZ         SZ_1M
179 #define PCIE_IOMAP_SZ           (PCIE_REGS_SZ + PCIE_CFG_SZ + PCIE_EXT_CFG_SZ)
180
181 #define MEM_BASE_0              (TEGRA_PCIE_BASE + SZ_256M)
182 #define MEM_SIZE_0              SZ_128M
183 #define MEM_BASE_1              (MEM_BASE_0 + MEM_SIZE_0)
184 #define MEM_SIZE_1              SZ_128M
185 #define PREFETCH_MEM_BASE_0     (MEM_BASE_1 + MEM_SIZE_1)
186 #define PREFETCH_MEM_SIZE_0     SZ_128M
187 #define PREFETCH_MEM_BASE_1     (PREFETCH_MEM_BASE_0 + PREFETCH_MEM_SIZE_0)
188 #define PREFETCH_MEM_SIZE_1     SZ_128M
189
190 #define  PCIE_CONF_BUS(b)       ((b) << 16)
191 #define  PCIE_CONF_DEV(d)       ((d) << 11)
192 #define  PCIE_CONF_FUNC(f)      ((f) << 8)
193 #define  PCIE_CONF_REG(r)       \
194         (((r) & ~0x3) | (((r) < 256) ? PCIE_CFG_OFF : PCIE_EXT_CFG_OFF))
195
196 struct tegra_pcie_port {
197         int                     index;
198         u8                      root_bus_nr;
199         void __iomem            *base;
200
201         bool                    link_up;
202
203         char                    mem_space_name[16];
204         char                    prefetch_space_name[20];
205         struct resource         res[2];
206 };
207
208 struct tegra_pcie_info {
209         struct tegra_pcie_port  port[2];
210         int                     num_ports;
211
212         void __iomem            *regs;
213         struct resource         res_mmio;
214
215         struct clk              *pex_clk;
216         struct clk              *afi_clk;
217         struct clk              *pcie_xclk;
218         struct clk              *pll_e;
219 };
220
221 static struct tegra_pcie_info tegra_pcie;
222
223 static inline void afi_writel(u32 value, unsigned long offset)
224 {
225         writel(value, offset + AFI_OFFSET + tegra_pcie.regs);
226 }
227
228 static inline u32 afi_readl(unsigned long offset)
229 {
230         return readl(offset + AFI_OFFSET + tegra_pcie.regs);
231 }
232
233 static inline void pads_writel(u32 value, unsigned long offset)
234 {
235         writel(value, offset + PADS_OFFSET + tegra_pcie.regs);
236 }
237
238 static inline u32 pads_readl(unsigned long offset)
239 {
240         return readl(offset + PADS_OFFSET + tegra_pcie.regs);
241 }
242
243 static struct tegra_pcie_port *bus_to_port(int bus)
244 {
245         int i;
246
247         for (i = tegra_pcie.num_ports - 1; i >= 0; i--) {
248                 int rbus = tegra_pcie.port[i].root_bus_nr;
249                 if (rbus != -1 && rbus == bus)
250                         break;
251         }
252
253         return i >= 0 ? tegra_pcie.port + i : NULL;
254 }
255
256 static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
257                                 int where, int size, u32 *val)
258 {
259         struct tegra_pcie_port *pp = bus_to_port(bus->number);
260         void __iomem *addr;
261
262         if (pp) {
263                 if (devfn != 0) {
264                         *val = 0xffffffff;
265                         return PCIBIOS_DEVICE_NOT_FOUND;
266                 }
267
268                 addr = pp->base + (where & ~0x3);
269         } else {
270                 addr = tegra_pcie.regs + (PCIE_CONF_BUS(bus->number) +
271                                           PCIE_CONF_DEV(PCI_SLOT(devfn)) +
272                                           PCIE_CONF_FUNC(PCI_FUNC(devfn)) +
273                                           PCIE_CONF_REG(where));
274         }
275
276         *val = readl(addr);
277
278         if (size == 1)
279                 *val = (*val >> (8 * (where & 3))) & 0xff;
280         else if (size == 2)
281                 *val = (*val >> (8 * (where & 3))) & 0xffff;
282
283         return PCIBIOS_SUCCESSFUL;
284 }
285
286 static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
287                                  int where, int size, u32 val)
288 {
289         struct tegra_pcie_port *pp = bus_to_port(bus->number);
290         void __iomem *addr;
291
292         u32 mask;
293         u32 tmp;
294
295         if (pp) {
296                 if (devfn != 0)
297                         return PCIBIOS_DEVICE_NOT_FOUND;
298
299                 addr = pp->base + (where & ~0x3);
300         } else {
301                 addr = tegra_pcie.regs + (PCIE_CONF_BUS(bus->number) +
302                                           PCIE_CONF_DEV(PCI_SLOT(devfn)) +
303                                           PCIE_CONF_FUNC(PCI_FUNC(devfn)) +
304                                           PCIE_CONF_REG(where));
305         }
306
307         if (size == 4) {
308                 writel(val, addr);
309                 return PCIBIOS_SUCCESSFUL;
310         }
311
312         if (size == 2)
313                 mask = ~(0xffff << ((where & 0x3) * 8));
314         else if (size == 1)
315                 mask = ~(0xff << ((where & 0x3) * 8));
316         else
317                 return PCIBIOS_BAD_REGISTER_NUMBER;
318
319         tmp = readl(addr) & mask;
320         tmp |= val << ((where & 0x3) * 8);
321         writel(tmp, addr);
322
323         return PCIBIOS_SUCCESSFUL;
324 }
325
326 static struct pci_ops tegra_pcie_ops = {
327         .read   = tegra_pcie_read_conf,
328         .write  = tegra_pcie_write_conf,
329 };
330
331 static void __devinit tegra_pcie_fixup_bridge(struct pci_dev *dev)
332 {
333         u16 reg;
334
335         if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
336                 pci_read_config_word(dev, PCI_COMMAND, &reg);
337                 reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
338                         PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
339                 pci_write_config_word(dev, PCI_COMMAND, reg);
340         }
341 }
342 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
343
344 /* Tegra PCIE root complex wrongly reports device class */
345 static void __devinit tegra_pcie_fixup_class(struct pci_dev *dev)
346 {
347         dev->class = PCI_CLASS_BRIDGE_PCI << 8;
348 }
349 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
350 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
351
352 /* Tegra PCIE requires relaxed ordering */
353 static void __devinit tegra_pcie_relax_enable(struct pci_dev *dev)
354 {
355         u16 val16;
356         int pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
357
358         if (pos <= 0) {
359                 dev_err(&dev->dev, "skipping relaxed ordering fixup\n");
360                 return;
361         }
362
363         pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &val16);
364         val16 |= PCI_EXP_DEVCTL_RELAX_EN;
365         pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, val16);
366 }
367 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
368
369 static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
370 {
371         struct tegra_pcie_port *pp;
372
373         if (nr >= tegra_pcie.num_ports)
374                 return 0;
375
376         pp = tegra_pcie.port + nr;
377         pp->root_bus_nr = sys->busnr;
378
379         pci_ioremap_io(nr * SZ_64K, TEGRA_PCIE_IO_BASE);
380
381         /*
382          * IORESOURCE_MEM
383          */
384         snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
385                  "PCIe %d MEM", pp->index);
386         pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
387         pp->res[0].name = pp->mem_space_name;
388         if (pp->index == 0) {
389                 pp->res[0].start = MEM_BASE_0;
390                 pp->res[0].end = pp->res[0].start + MEM_SIZE_0 - 1;
391         } else {
392                 pp->res[0].start = MEM_BASE_1;
393                 pp->res[0].end = pp->res[0].start + MEM_SIZE_1 - 1;
394         }
395         pp->res[0].flags = IORESOURCE_MEM;
396         if (request_resource(&iomem_resource, &pp->res[0]))
397                 panic("Request PCIe Memory resource failed\n");
398         pci_add_resource_offset(&sys->resources, &pp->res[0], sys->mem_offset);
399
400         /*
401          * IORESOURCE_MEM | IORESOURCE_PREFETCH
402          */
403         snprintf(pp->prefetch_space_name, sizeof(pp->prefetch_space_name),
404                  "PCIe %d PREFETCH MEM", pp->index);
405         pp->prefetch_space_name[sizeof(pp->prefetch_space_name) - 1] = 0;
406         pp->res[1].name = pp->prefetch_space_name;
407         if (pp->index == 0) {
408                 pp->res[1].start = PREFETCH_MEM_BASE_0;
409                 pp->res[1].end = pp->res[1].start + PREFETCH_MEM_SIZE_0 - 1;
410         } else {
411                 pp->res[1].start = PREFETCH_MEM_BASE_1;
412                 pp->res[1].end = pp->res[1].start + PREFETCH_MEM_SIZE_1 - 1;
413         }
414         pp->res[1].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
415         if (request_resource(&iomem_resource, &pp->res[1]))
416                 panic("Request PCIe Prefetch Memory resource failed\n");
417         pci_add_resource_offset(&sys->resources, &pp->res[1], sys->mem_offset);
418
419         return 1;
420 }
421
422 static int tegra_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
423 {
424         return INT_PCIE_INTR;
425 }
426
427 static struct pci_bus __init *tegra_pcie_scan_bus(int nr,
428                                                   struct pci_sys_data *sys)
429 {
430         struct tegra_pcie_port *pp;
431
432         if (nr >= tegra_pcie.num_ports)
433                 return NULL;
434
435         pp = tegra_pcie.port + nr;
436         pp->root_bus_nr = sys->busnr;
437
438         return pci_scan_root_bus(NULL, sys->busnr, &tegra_pcie_ops, sys,
439                                  &sys->resources);
440 }
441
442 static struct hw_pci tegra_pcie_hw __initdata = {
443         .nr_controllers = 2,
444         .setup          = tegra_pcie_setup,
445         .scan           = tegra_pcie_scan_bus,
446         .map_irq        = tegra_pcie_map_irq,
447 };
448
449
450 static irqreturn_t tegra_pcie_isr(int irq, void *arg)
451 {
452         const char *err_msg[] = {
453                 "Unknown",
454                 "AXI slave error",
455                 "AXI decode error",
456                 "Target abort",
457                 "Master abort",
458                 "Invalid write",
459                 "Response decoding error",
460                 "AXI response decoding error",
461                 "Transcation timeout",
462         };
463
464         u32 code, signature;
465
466         code = afi_readl(AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
467         signature = afi_readl(AFI_INTR_SIGNATURE);
468         afi_writel(0, AFI_INTR_CODE);
469
470         if (code == AFI_INTR_LEGACY)
471                 return IRQ_NONE;
472
473         if (code >= ARRAY_SIZE(err_msg))
474                 code = 0;
475
476         /*
477          * do not pollute kernel log with master abort reports since they
478          * happen a lot during enumeration
479          */
480         if (code == AFI_INTR_MASTER_ABORT)
481                 pr_debug("PCIE: %s, signature: %08x\n", err_msg[code], signature);
482         else
483                 pr_err("PCIE: %s, signature: %08x\n", err_msg[code], signature);
484
485         return IRQ_HANDLED;
486 }
487
488 static void tegra_pcie_setup_translations(void)
489 {
490         u32 fpci_bar;
491         u32 size;
492         u32 axi_address;
493
494         /* Bar 0: config Bar */
495         fpci_bar = ((u32)0xfdff << 16);
496         size = PCIE_CFG_SZ;
497         axi_address = TEGRA_PCIE_BASE + PCIE_CFG_OFF;
498         afi_writel(axi_address, AFI_AXI_BAR0_START);
499         afi_writel(size >> 12, AFI_AXI_BAR0_SZ);
500         afi_writel(fpci_bar, AFI_FPCI_BAR0);
501
502         /* Bar 1: extended config Bar */
503         fpci_bar = ((u32)0xfe1 << 20);
504         size = PCIE_EXT_CFG_SZ;
505         axi_address = TEGRA_PCIE_BASE + PCIE_EXT_CFG_OFF;
506         afi_writel(axi_address, AFI_AXI_BAR1_START);
507         afi_writel(size >> 12, AFI_AXI_BAR1_SZ);
508         afi_writel(fpci_bar, AFI_FPCI_BAR1);
509
510         /* Bar 2: downstream IO bar */
511         fpci_bar = ((__u32)0xfdfc << 16);
512         size = SZ_128K;
513         axi_address = TEGRA_PCIE_IO_BASE;
514         afi_writel(axi_address, AFI_AXI_BAR2_START);
515         afi_writel(size >> 12, AFI_AXI_BAR2_SZ);
516         afi_writel(fpci_bar, AFI_FPCI_BAR2);
517
518         /* Bar 3: prefetchable memory BAR */
519         fpci_bar = (((PREFETCH_MEM_BASE_0 >> 12) & 0x0fffffff) << 4) | 0x1;
520         size =  PREFETCH_MEM_SIZE_0 +  PREFETCH_MEM_SIZE_1;
521         axi_address = PREFETCH_MEM_BASE_0;
522         afi_writel(axi_address, AFI_AXI_BAR3_START);
523         afi_writel(size >> 12, AFI_AXI_BAR3_SZ);
524         afi_writel(fpci_bar, AFI_FPCI_BAR3);
525
526         /* Bar 4: non prefetchable memory BAR */
527         fpci_bar = (((MEM_BASE_0 >> 12) & 0x0FFFFFFF) << 4) | 0x1;
528         size = MEM_SIZE_0 + MEM_SIZE_1;
529         axi_address = MEM_BASE_0;
530         afi_writel(axi_address, AFI_AXI_BAR4_START);
531         afi_writel(size >> 12, AFI_AXI_BAR4_SZ);
532         afi_writel(fpci_bar, AFI_FPCI_BAR4);
533
534         /* Bar 5: NULL out the remaining BAR as it is not used */
535         fpci_bar = 0;
536         size = 0;
537         axi_address = 0;
538         afi_writel(axi_address, AFI_AXI_BAR5_START);
539         afi_writel(size >> 12, AFI_AXI_BAR5_SZ);
540         afi_writel(fpci_bar, AFI_FPCI_BAR5);
541
542         /* map all upstream transactions as uncached */
543         afi_writel(PHYS_OFFSET, AFI_CACHE_BAR0_ST);
544         afi_writel(0, AFI_CACHE_BAR0_SZ);
545         afi_writel(0, AFI_CACHE_BAR1_ST);
546         afi_writel(0, AFI_CACHE_BAR1_SZ);
547
548         /* No MSI */
549         afi_writel(0, AFI_MSI_FPCI_BAR_ST);
550         afi_writel(0, AFI_MSI_BAR_SZ);
551         afi_writel(0, AFI_MSI_AXI_BAR_ST);
552         afi_writel(0, AFI_MSI_BAR_SZ);
553 }
554
555 static int tegra_pcie_enable_controller(void)
556 {
557         u32 val, reg;
558         int i, timeout;
559
560         /* Enable slot clock and pulse the reset signals */
561         for (i = 0, reg = AFI_PEX0_CTRL; i < 2; i++, reg += 0x8) {
562                 val = afi_readl(reg) |  AFI_PEX_CTRL_REFCLK_EN;
563                 afi_writel(val, reg);
564                 val &= ~AFI_PEX_CTRL_RST;
565                 afi_writel(val, reg);
566
567                 val = afi_readl(reg) | AFI_PEX_CTRL_RST;
568                 afi_writel(val, reg);
569         }
570
571         /* Enable dual controller and both ports */
572         val = afi_readl(AFI_PCIE_CONFIG);
573         val &= ~(AFI_PCIE_CONFIG_PCIEC0_DISABLE_DEVICE |
574                  AFI_PCIE_CONFIG_PCIEC1_DISABLE_DEVICE |
575                  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK);
576         val |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
577         afi_writel(val, AFI_PCIE_CONFIG);
578
579         val = afi_readl(AFI_FUSE) & ~AFI_FUSE_PCIE_T0_GEN2_DIS;
580         afi_writel(val, AFI_FUSE);
581
582         /* Initialze internal PHY, enable up to 16 PCIE lanes */
583         pads_writel(0x0, PADS_CTL_SEL);
584
585         /* override IDDQ to 1 on all 4 lanes */
586         val = pads_readl(PADS_CTL) | PADS_CTL_IDDQ_1L;
587         pads_writel(val, PADS_CTL);
588
589         /*
590          * set up PHY PLL inputs select PLLE output as refclock,
591          * set TX ref sel to div10 (not div5)
592          */
593         val = pads_readl(PADS_PLL_CTL);
594         val &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
595         val |= (PADS_PLL_CTL_REFCLK_INTERNAL_CML | PADS_PLL_CTL_TXCLKREF_DIV10);
596         pads_writel(val, PADS_PLL_CTL);
597
598         /* take PLL out of reset  */
599         val = pads_readl(PADS_PLL_CTL) | PADS_PLL_CTL_RST_B4SM;
600         pads_writel(val, PADS_PLL_CTL);
601
602         /*
603          * Hack, set the clock voltage to the DEFAULT provided by hw folks.
604          * This doesn't exist in the documentation
605          */
606         pads_writel(0xfa5cfa5c, 0xc8);
607
608         /* Wait for the PLL to lock */
609         timeout = 300;
610         do {
611                 val = pads_readl(PADS_PLL_CTL);
612                 usleep_range(1000, 1000);
613                 if (--timeout == 0) {
614                         pr_err("Tegra PCIe error: timeout waiting for PLL\n");
615                         return -EBUSY;
616                 }
617         } while (!(val & PADS_PLL_CTL_LOCKDET));
618
619         /* turn off IDDQ override */
620         val = pads_readl(PADS_CTL) & ~PADS_CTL_IDDQ_1L;
621         pads_writel(val, PADS_CTL);
622
623         /* enable TX/RX data */
624         val = pads_readl(PADS_CTL);
625         val |= (PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
626         pads_writel(val, PADS_CTL);
627
628         /* Take the PCIe interface module out of reset */
629         tegra_periph_reset_deassert(tegra_pcie.pcie_xclk);
630
631         /* Finally enable PCIe */
632         val = afi_readl(AFI_CONFIGURATION) | AFI_CONFIGURATION_EN_FPCI;
633         afi_writel(val, AFI_CONFIGURATION);
634
635         val = (AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
636                AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
637                AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR);
638         afi_writel(val, AFI_AFI_INTR_ENABLE);
639         afi_writel(0xffffffff, AFI_SM_INTR_ENABLE);
640
641         /* FIXME: No MSI for now, only INT */
642         afi_writel(AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
643
644         /* Disable all execptions */
645         afi_writel(0, AFI_FPCI_ERROR_MASKS);
646
647         return 0;
648 }
649
650 static void tegra_pcie_xclk_clamp(bool clamp)
651 {
652         u32 reg;
653
654         reg = pmc_readl(PMC_SCRATCH42) & ~PMC_SCRATCH42_PCX_CLAMP;
655
656         if (clamp)
657                 reg |= PMC_SCRATCH42_PCX_CLAMP;
658
659         pmc_writel(reg, PMC_SCRATCH42);
660 }
661
662 static void tegra_pcie_power_off(void)
663 {
664         tegra_periph_reset_assert(tegra_pcie.pcie_xclk);
665         tegra_periph_reset_assert(tegra_pcie.afi_clk);
666         tegra_periph_reset_assert(tegra_pcie.pex_clk);
667
668         tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
669         tegra_pcie_xclk_clamp(true);
670 }
671
672 static int tegra_pcie_power_regate(void)
673 {
674         int err;
675
676         tegra_pcie_power_off();
677
678         tegra_pcie_xclk_clamp(true);
679
680         tegra_periph_reset_assert(tegra_pcie.pcie_xclk);
681         tegra_periph_reset_assert(tegra_pcie.afi_clk);
682
683         err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
684                                                 tegra_pcie.pex_clk);
685         if (err) {
686                 pr_err("PCIE: powerup sequence failed: %d\n", err);
687                 return err;
688         }
689
690         tegra_periph_reset_deassert(tegra_pcie.afi_clk);
691
692         tegra_pcie_xclk_clamp(false);
693
694         clk_prepare_enable(tegra_pcie.afi_clk);
695         clk_prepare_enable(tegra_pcie.pex_clk);
696         return clk_prepare_enable(tegra_pcie.pll_e);
697 }
698
699 static int tegra_pcie_clocks_get(void)
700 {
701         int err;
702
703         tegra_pcie.pex_clk = clk_get(NULL, "pex");
704         if (IS_ERR(tegra_pcie.pex_clk))
705                 return PTR_ERR(tegra_pcie.pex_clk);
706
707         tegra_pcie.afi_clk = clk_get(NULL, "afi");
708         if (IS_ERR(tegra_pcie.afi_clk)) {
709                 err = PTR_ERR(tegra_pcie.afi_clk);
710                 goto err_afi_clk;
711         }
712
713         tegra_pcie.pcie_xclk = clk_get(NULL, "pcie_xclk");
714         if (IS_ERR(tegra_pcie.pcie_xclk)) {
715                 err =  PTR_ERR(tegra_pcie.pcie_xclk);
716                 goto err_pcie_xclk;
717         }
718
719         tegra_pcie.pll_e = clk_get_sys(NULL, "pll_e");
720         if (IS_ERR(tegra_pcie.pll_e)) {
721                 err = PTR_ERR(tegra_pcie.pll_e);
722                 goto err_pll_e;
723         }
724
725         return 0;
726
727 err_pll_e:
728         clk_put(tegra_pcie.pcie_xclk);
729 err_pcie_xclk:
730         clk_put(tegra_pcie.afi_clk);
731 err_afi_clk:
732         clk_put(tegra_pcie.pex_clk);
733
734         return err;
735 }
736
737 static void tegra_pcie_clocks_put(void)
738 {
739         clk_put(tegra_pcie.pll_e);
740         clk_put(tegra_pcie.pcie_xclk);
741         clk_put(tegra_pcie.afi_clk);
742         clk_put(tegra_pcie.pex_clk);
743 }
744
745 static int __init tegra_pcie_get_resources(void)
746 {
747         int err;
748
749         err = tegra_pcie_clocks_get();
750         if (err) {
751                 pr_err("PCIE: failed to get clocks: %d\n", err);
752                 return err;
753         }
754
755         err = tegra_pcie_power_regate();
756         if (err) {
757                 pr_err("PCIE: failed to power up: %d\n", err);
758                 goto err_pwr_on;
759         }
760
761         tegra_pcie.regs = ioremap_nocache(TEGRA_PCIE_BASE, PCIE_IOMAP_SZ);
762         if (tegra_pcie.regs == NULL) {
763                 pr_err("PCIE: Failed to map PCI/AFI registers\n");
764                 err = -ENOMEM;
765                 goto err_map_reg;
766         }
767
768         err = request_irq(INT_PCIE_INTR, tegra_pcie_isr,
769                           IRQF_SHARED, "PCIE", &tegra_pcie);
770         if (err) {
771                 pr_err("PCIE: Failed to register IRQ: %d\n", err);
772                 goto err_req_io;
773         }
774         set_irq_flags(INT_PCIE_INTR, IRQF_VALID);
775
776         return 0;
777
778 err_req_io:
779         iounmap(tegra_pcie.regs);
780 err_map_reg:
781         tegra_pcie_power_off();
782 err_pwr_on:
783         tegra_pcie_clocks_put();
784
785         return err;
786 }
787
788 /*
789  * FIXME: If there are no PCIe cards attached, then calling this function
790  * can result in the increase of the bootup time as there are big timeout
791  * loops.
792  */
793 #define TEGRA_PCIE_LINKUP_TIMEOUT       200     /* up to 1.2 seconds */
794 static bool tegra_pcie_check_link(struct tegra_pcie_port *pp, int idx,
795                                   u32 reset_reg)
796 {
797         u32 reg;
798         int retries = 3;
799         int timeout;
800
801         do {
802                 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
803                 while (timeout) {
804                         reg = readl(pp->base + RP_VEND_XP);
805
806                         if (reg & RP_VEND_XP_DL_UP)
807                                 break;
808
809                         mdelay(1);
810                         timeout--;
811                 }
812
813                 if (!timeout)  {
814                         pr_err("PCIE: port %d: link down, retrying\n", idx);
815                         goto retry;
816                 }
817
818                 timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
819                 while (timeout) {
820                         reg = readl(pp->base + RP_LINK_CONTROL_STATUS);
821
822                         if (reg & 0x20000000)
823                                 return true;
824
825                         mdelay(1);
826                         timeout--;
827                 }
828
829 retry:
830                 /* Pulse the PEX reset */
831                 reg = afi_readl(reset_reg) | AFI_PEX_CTRL_RST;
832                 afi_writel(reg, reset_reg);
833                 mdelay(1);
834                 reg = afi_readl(reset_reg) & ~AFI_PEX_CTRL_RST;
835                 afi_writel(reg, reset_reg);
836
837                 retries--;
838         } while (retries);
839
840         return false;
841 }
842
843 static void __init tegra_pcie_add_port(int index, u32 offset, u32 reset_reg)
844 {
845         struct tegra_pcie_port *pp;
846
847         pp = tegra_pcie.port + tegra_pcie.num_ports;
848
849         pp->index = -1;
850         pp->base = tegra_pcie.regs + offset;
851         pp->link_up = tegra_pcie_check_link(pp, index, reset_reg);
852
853         if (!pp->link_up) {
854                 pp->base = NULL;
855                 printk(KERN_INFO "PCIE: port %d: link down, ignoring\n", index);
856                 return;
857         }
858
859         tegra_pcie.num_ports++;
860         pp->index = index;
861         pp->root_bus_nr = -1;
862         memset(pp->res, 0, sizeof(pp->res));
863 }
864
865 int __init tegra_pcie_init(bool init_port0, bool init_port1)
866 {
867         int err;
868
869         if (!(init_port0 || init_port1))
870                 return -ENODEV;
871
872         pcibios_min_mem = 0;
873
874         err = tegra_pcie_get_resources();
875         if (err)
876                 return err;
877
878         err = tegra_pcie_enable_controller();
879         if (err)
880                 return err;
881
882         /* setup the AFI address translations */
883         tegra_pcie_setup_translations();
884
885         if (init_port0)
886                 tegra_pcie_add_port(0, RP0_OFFSET, AFI_PEX0_CTRL);
887
888         if (init_port1)
889                 tegra_pcie_add_port(1, RP1_OFFSET, AFI_PEX1_CTRL);
890
891         pci_common_init(&tegra_pcie_hw);
892
893         return 0;
894 }