xref: /rk3399_rockchip-uboot/drivers/pci/pci_tegra.c (revision a02e2635480e66a2921d62792fbc4a1e83dc4425)
1 /*
2  * Copyright (c) 2010, CompuLab, Ltd.
3  * Author: Mike Rapoport <mike@compulab.co.il>
4  *
5  * Based on NVIDIA PCIe driver
6  * Copyright (c) 2008-2009, NVIDIA Corporation.
7  *
8  * Copyright (c) 2013-2014, NVIDIA Corporation.
9  *
10  * SPDX-License-Identifier:	GPL-2.0
11  */
12 
13 #define DEBUG
14 #define pr_fmt(fmt) "tegra-pcie: " fmt
15 
16 #include <common.h>
17 #include <errno.h>
18 #include <fdtdec.h>
19 #include <malloc.h>
20 #include <pci.h>
21 
22 #include <asm/io.h>
23 #include <asm/gpio.h>
24 
25 #include <asm/arch/clock.h>
26 #include <asm/arch/powergate.h>
27 #include <asm/arch-tegra/xusb-padctl.h>
28 
29 #include <linux/list.h>
30 
31 #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
32 
33 DECLARE_GLOBAL_DATA_PTR;
34 
35 #define AFI_AXI_BAR0_SZ	0x00
36 #define AFI_AXI_BAR1_SZ	0x04
37 #define AFI_AXI_BAR2_SZ	0x08
38 #define AFI_AXI_BAR3_SZ	0x0c
39 #define AFI_AXI_BAR4_SZ	0x10
40 #define AFI_AXI_BAR5_SZ	0x14
41 
42 #define AFI_AXI_BAR0_START	0x18
43 #define AFI_AXI_BAR1_START	0x1c
44 #define AFI_AXI_BAR2_START	0x20
45 #define AFI_AXI_BAR3_START	0x24
46 #define AFI_AXI_BAR4_START	0x28
47 #define AFI_AXI_BAR5_START	0x2c
48 
49 #define AFI_FPCI_BAR0	0x30
50 #define AFI_FPCI_BAR1	0x34
51 #define AFI_FPCI_BAR2	0x38
52 #define AFI_FPCI_BAR3	0x3c
53 #define AFI_FPCI_BAR4	0x40
54 #define AFI_FPCI_BAR5	0x44
55 
56 #define AFI_CACHE_BAR0_SZ	0x48
57 #define AFI_CACHE_BAR0_ST	0x4c
58 #define AFI_CACHE_BAR1_SZ	0x50
59 #define AFI_CACHE_BAR1_ST	0x54
60 
61 #define AFI_MSI_BAR_SZ		0x60
62 #define AFI_MSI_FPCI_BAR_ST	0x64
63 #define AFI_MSI_AXI_BAR_ST	0x68
64 
65 #define AFI_CONFIGURATION		0xac
66 #define  AFI_CONFIGURATION_EN_FPCI	(1 << 0)
67 
68 #define AFI_FPCI_ERROR_MASKS	0xb0
69 
70 #define AFI_INTR_MASK		0xb4
71 #define  AFI_INTR_MASK_INT_MASK	(1 << 0)
72 #define  AFI_INTR_MASK_MSI_MASK	(1 << 8)
73 
74 #define AFI_SM_INTR_ENABLE	0xc4
75 #define  AFI_SM_INTR_INTA_ASSERT	(1 << 0)
76 #define  AFI_SM_INTR_INTB_ASSERT	(1 << 1)
77 #define  AFI_SM_INTR_INTC_ASSERT	(1 << 2)
78 #define  AFI_SM_INTR_INTD_ASSERT	(1 << 3)
79 #define  AFI_SM_INTR_INTA_DEASSERT	(1 << 4)
80 #define  AFI_SM_INTR_INTB_DEASSERT	(1 << 5)
81 #define  AFI_SM_INTR_INTC_DEASSERT	(1 << 6)
82 #define  AFI_SM_INTR_INTD_DEASSERT	(1 << 7)
83 
84 #define AFI_AFI_INTR_ENABLE		0xc8
85 #define  AFI_INTR_EN_INI_SLVERR		(1 << 0)
86 #define  AFI_INTR_EN_INI_DECERR		(1 << 1)
87 #define  AFI_INTR_EN_TGT_SLVERR		(1 << 2)
88 #define  AFI_INTR_EN_TGT_DECERR		(1 << 3)
89 #define  AFI_INTR_EN_TGT_WRERR		(1 << 4)
90 #define  AFI_INTR_EN_DFPCI_DECERR	(1 << 5)
91 #define  AFI_INTR_EN_AXI_DECERR		(1 << 6)
92 #define  AFI_INTR_EN_FPCI_TIMEOUT	(1 << 7)
93 #define  AFI_INTR_EN_PRSNT_SENSE	(1 << 8)
94 
95 #define AFI_PCIE_CONFIG					0x0f8
96 #define  AFI_PCIE_CONFIG_PCIE_DISABLE(x)		(1 << ((x) + 1))
97 #define  AFI_PCIE_CONFIG_PCIE_DISABLE_ALL		0xe
98 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK	(0xf << 20)
99 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE	(0x0 << 20)
100 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420	(0x0 << 20)
101 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1	(0x0 << 20)
102 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL	(0x1 << 20)
103 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222	(0x1 << 20)
104 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1	(0x1 << 20)
105 #define  AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411	(0x2 << 20)
106 
107 #define AFI_FUSE			0x104
108 #define  AFI_FUSE_PCIE_T0_GEN2_DIS	(1 << 2)
109 
110 #define AFI_PEX0_CTRL			0x110
111 #define AFI_PEX1_CTRL			0x118
112 #define AFI_PEX2_CTRL			0x128
113 #define  AFI_PEX_CTRL_RST		(1 << 0)
114 #define  AFI_PEX_CTRL_CLKREQ_EN		(1 << 1)
115 #define  AFI_PEX_CTRL_REFCLK_EN		(1 << 3)
116 #define  AFI_PEX_CTRL_OVERRIDE_EN	(1 << 4)
117 
118 #define AFI_PLLE_CONTROL		0x160
119 #define  AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
120 #define  AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
121 
122 #define AFI_PEXBIAS_CTRL_0		0x168
123 
124 #define PADS_CTL_SEL		0x0000009C
125 
126 #define PADS_CTL		0x000000A0
127 #define  PADS_CTL_IDDQ_1L	(1 <<  0)
128 #define  PADS_CTL_TX_DATA_EN_1L	(1 <<  6)
129 #define  PADS_CTL_RX_DATA_EN_1L	(1 << 10)
130 
131 #define PADS_PLL_CTL_TEGRA20			0x000000B8
132 #define PADS_PLL_CTL_TEGRA30			0x000000B4
133 #define  PADS_PLL_CTL_RST_B4SM			(0x1 <<  1)
134 #define  PADS_PLL_CTL_LOCKDET			(0x1 <<  8)
135 #define  PADS_PLL_CTL_REFCLK_MASK		(0x3 << 16)
136 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CML	(0x0 << 16)
137 #define  PADS_PLL_CTL_REFCLK_INTERNAL_CMOS	(0x1 << 16)
138 #define  PADS_PLL_CTL_REFCLK_EXTERNAL		(0x2 << 16)
139 #define  PADS_PLL_CTL_TXCLKREF_MASK		(0x1 << 20)
140 #define  PADS_PLL_CTL_TXCLKREF_DIV10		(0x0 << 20)
141 #define  PADS_PLL_CTL_TXCLKREF_DIV5		(0x1 << 20)
142 #define  PADS_PLL_CTL_TXCLKREF_BUF_EN		(0x1 << 22)
143 
144 #define PADS_REFCLK_CFG0			0x000000C8
145 #define PADS_REFCLK_CFG1			0x000000CC
146 
147 /*
148  * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
149  * entries, one entry per PCIe port. These field definitions and desired
150  * values aren't in the TRM, but do come from NVIDIA.
151  */
152 #define PADS_REFCLK_CFG_TERM_SHIFT		2  /* 6:2 */
153 #define PADS_REFCLK_CFG_E_TERM_SHIFT		7
154 #define PADS_REFCLK_CFG_PREDI_SHIFT		8  /* 11:8 */
155 #define PADS_REFCLK_CFG_DRVI_SHIFT		12 /* 15:12 */
156 
157 /* Default value provided by HW engineering is 0xfa5c */
158 #define PADS_REFCLK_CFG_VALUE \
159 	( \
160 		(0x17 << PADS_REFCLK_CFG_TERM_SHIFT)   | \
161 		(0    << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
162 		(0xa  << PADS_REFCLK_CFG_PREDI_SHIFT)  | \
163 		(0xf  << PADS_REFCLK_CFG_DRVI_SHIFT)     \
164 	)
165 
166 #define RP_VEND_XP	0x00000F00
167 #define  RP_VEND_XP_DL_UP	(1 << 30)
168 
169 #define RP_VEND_CTL2				0x00000FA8
170 #define  RP_VEND_CTL2_PCA_ENABLE		(1 << 7)
171 
172 #define RP_PRIV_MISC	0x00000FE0
173 #define  RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
174 #define  RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
175 
176 #define RP_LINK_CONTROL_STATUS			0x00000090
177 #define  RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE	0x20000000
178 #define  RP_LINK_CONTROL_STATUS_LINKSTAT_MASK	0x3fff0000
179 
180 struct tegra_pcie;
181 
182 struct tegra_pcie_port {
183 	struct tegra_pcie *pcie;
184 
185 	struct fdt_resource regs;
186 	unsigned int num_lanes;
187 	unsigned int index;
188 
189 	struct list_head list;
190 };
191 
192 struct tegra_pcie_soc {
193 	unsigned int num_ports;
194 	unsigned long pads_pll_ctl;
195 	unsigned long tx_ref_sel;
196 	bool has_pex_clkreq_en;
197 	bool has_pex_bias_ctrl;
198 	bool has_cml_clk;
199 	bool has_gen2;
200 	bool force_pca_enable;
201 };
202 
203 struct tegra_pcie {
204 	struct pci_controller hose;
205 
206 	struct fdt_resource pads;
207 	struct fdt_resource afi;
208 	struct fdt_resource cs;
209 
210 	struct fdt_resource prefetch;
211 	struct fdt_resource mem;
212 	struct fdt_resource io;
213 
214 	struct list_head ports;
215 	unsigned long xbar;
216 
217 	const struct tegra_pcie_soc *soc;
218 	struct tegra_xusb_phy *phy;
219 };
220 
221 static inline struct tegra_pcie *to_tegra_pcie(struct pci_controller *hose)
222 {
223 	return container_of(hose, struct tegra_pcie, hose);
224 }
225 
226 static void afi_writel(struct tegra_pcie *pcie, unsigned long value,
227 		       unsigned long offset)
228 {
229 	writel(value, pcie->afi.start + offset);
230 }
231 
232 static unsigned long afi_readl(struct tegra_pcie *pcie, unsigned long offset)
233 {
234 	return readl(pcie->afi.start + offset);
235 }
236 
237 static void pads_writel(struct tegra_pcie *pcie, unsigned long value,
238 			unsigned long offset)
239 {
240 	writel(value, pcie->pads.start + offset);
241 }
242 
243 static unsigned long pads_readl(struct tegra_pcie *pcie, unsigned long offset)
244 {
245 	return readl(pcie->pads.start + offset);
246 }
247 
248 static unsigned long rp_readl(struct tegra_pcie_port *port,
249 			      unsigned long offset)
250 {
251 	return readl(port->regs.start + offset);
252 }
253 
254 static void rp_writel(struct tegra_pcie_port *port, unsigned long value,
255 		      unsigned long offset)
256 {
257 	writel(value, port->regs.start + offset);
258 }
259 
260 static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where)
261 {
262 	return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) |
263 	       (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) |
264 	       (where & 0xfc);
265 }
266 
267 static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf,
268 				   int where, unsigned long *address)
269 {
270 	unsigned int bus = PCI_BUS(bdf);
271 
272 	if (bus == 0) {
273 		unsigned int dev = PCI_DEV(bdf);
274 		struct tegra_pcie_port *port;
275 
276 		list_for_each_entry(port, &pcie->ports, list) {
277 			if (port->index + 1 == dev) {
278 				*address = port->regs.start + (where & ~3);
279 				return 0;
280 			}
281 		}
282 	} else {
283 		*address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where);
284 		return 0;
285 	}
286 
287 	return -1;
288 }
289 
290 static int tegra_pcie_read_conf(struct pci_controller *hose, pci_dev_t bdf,
291 				int where, u32 *value)
292 {
293 	struct tegra_pcie *pcie = to_tegra_pcie(hose);
294 	unsigned long address;
295 	int err;
296 
297 	err = tegra_pcie_conf_address(pcie, bdf, where, &address);
298 	if (err < 0) {
299 		*value = 0xffffffff;
300 		return 1;
301 	}
302 
303 	*value = readl(address);
304 
305 	/* fixup root port class */
306 	if (PCI_BUS(bdf) == 0) {
307 		if (where == PCI_CLASS_REVISION) {
308 			*value &= ~0x00ff0000;
309 			*value |= PCI_CLASS_BRIDGE_PCI << 16;
310 		}
311 	}
312 
313 	return 0;
314 }
315 
316 static int tegra_pcie_write_conf(struct pci_controller *hose, pci_dev_t bdf,
317 				 int where, u32 value)
318 {
319 	struct tegra_pcie *pcie = to_tegra_pcie(hose);
320 	unsigned long address;
321 	int err;
322 
323 	err = tegra_pcie_conf_address(pcie, bdf, where, &address);
324 	if (err < 0)
325 		return 1;
326 
327 	writel(value, address);
328 
329 	return 0;
330 }
331 
332 static int tegra_pcie_port_parse_dt(const void *fdt, int node,
333 				    struct tegra_pcie_port *port)
334 {
335 	const u32 *addr;
336 	int len;
337 
338 	addr = fdt_getprop(fdt, node, "assigned-addresses", &len);
339 	if (!addr) {
340 		error("property \"assigned-addresses\" not found");
341 		return -FDT_ERR_NOTFOUND;
342 	}
343 
344 	port->regs.start = fdt32_to_cpu(addr[2]);
345 	port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]);
346 
347 	return 0;
348 }
349 
350 static int tegra_pcie_get_xbar_config(const void *fdt, int node, u32 lanes,
351 				      unsigned long *xbar)
352 {
353 	enum fdt_compat_id id = fdtdec_lookup(fdt, node);
354 
355 	switch (id) {
356 	case COMPAT_NVIDIA_TEGRA20_PCIE:
357 		switch (lanes) {
358 		case 0x00000004:
359 			debug("single-mode configuration\n");
360 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
361 			return 0;
362 
363 		case 0x00000202:
364 			debug("dual-mode configuration\n");
365 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
366 			return 0;
367 		}
368 		break;
369 
370 	case COMPAT_NVIDIA_TEGRA30_PCIE:
371 		switch (lanes) {
372 		case 0x00000204:
373 			debug("4x1, 2x1 configuration\n");
374 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
375 			return 0;
376 
377 		case 0x00020202:
378 			debug("2x3 configuration\n");
379 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
380 			return 0;
381 
382 		case 0x00010104:
383 			debug("4x1, 1x2 configuration\n");
384 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
385 			return 0;
386 		}
387 		break;
388 
389 	case COMPAT_NVIDIA_TEGRA124_PCIE:
390 		switch (lanes) {
391 		case 0x0000104:
392 			debug("4x1, 1x1 configuration\n");
393 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
394 			return 0;
395 
396 		case 0x0000102:
397 			debug("2x1, 1x1 configuration\n");
398 			*xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
399 			return 0;
400 		}
401 		break;
402 
403 	default:
404 		break;
405 	}
406 
407 	return -FDT_ERR_NOTFOUND;
408 }
409 
410 static int tegra_pcie_parse_dt_ranges(const void *fdt, int node,
411 				      struct tegra_pcie *pcie)
412 {
413 	int parent, na_parent, na_pcie, ns_pcie;
414 	const u32 *ptr, *end;
415 	int len;
416 
417 	parent = fdt_parent_offset(fdt, node);
418 	if (parent < 0) {
419 		error("Can't find PCI parent node\n");
420 		return -FDT_ERR_NOTFOUND;
421 	}
422 
423 	na_parent = fdt_address_cells(fdt, parent);
424 	if (na_parent < 1) {
425 		error("bad #address-cells for PCIE parent\n");
426 		return -FDT_ERR_NOTFOUND;
427 	}
428 
429 	na_pcie = fdt_address_cells(fdt, node);
430 	if (na_pcie < 1) {
431 		error("bad #address-cells for PCIE\n");
432 		return -FDT_ERR_NOTFOUND;
433 	}
434 
435 	ns_pcie = fdt_size_cells(fdt, node);
436 	if (ns_pcie < 1) {
437 		error("bad #size-cells for PCIE\n");
438 		return -FDT_ERR_NOTFOUND;
439 	}
440 
441 	ptr = fdt_getprop(fdt, node, "ranges", &len);
442 	if (!ptr) {
443 		error("missing \"ranges\" property");
444 		return -FDT_ERR_NOTFOUND;
445 	}
446 
447 	end = ptr + len / 4;
448 
449 	while (ptr < end) {
450 		struct fdt_resource *res = NULL;
451 		u32 space = fdt32_to_cpu(*ptr);
452 
453 		switch ((space >> 24) & 0x3) {
454 		case 0x01:
455 			res = &pcie->io;
456 			break;
457 
458 		case 0x02: /* 32 bit */
459 		case 0x03: /* 64 bit */
460 			if (space & (1 << 30))
461 				res = &pcie->prefetch;
462 			else
463 				res = &pcie->mem;
464 
465 			break;
466 		}
467 
468 		if (res) {
469 			int start_low = na_pcie + (na_parent - 1);
470 			int size_low = na_pcie + na_parent + (ns_pcie - 1);
471 			res->start = fdt32_to_cpu(ptr[start_low]);
472 			res->end = res->start + fdt32_to_cpu(ptr[size_low]);
473 		}
474 
475 		ptr += na_pcie + na_parent + ns_pcie;
476 	}
477 
478 	debug("PCI regions:\n");
479 	debug("  I/O: %pa-%pa\n", &pcie->io.start, &pcie->io.end);
480 	debug("  non-prefetchable memory: %pa-%pa\n", &pcie->mem.start,
481 	      &pcie->mem.end);
482 	debug("  prefetchable memory: %pa-%pa\n", &pcie->prefetch.start,
483 	      &pcie->prefetch.end);
484 
485 	return 0;
486 }
487 
488 static int tegra_pcie_parse_port_info(const void *fdt, int node,
489 				      unsigned int *index,
490 				      unsigned int *lanes)
491 {
492 	struct fdt_pci_addr addr;
493 	int err;
494 
495 	err = fdtdec_get_int(fdt, node, "nvidia,num-lanes", 0);
496 	if (err < 0) {
497 		error("failed to parse \"nvidia,num-lanes\" property");
498 		return err;
499 	}
500 
501 	*lanes = err;
502 
503 	err = fdtdec_get_pci_addr(fdt, node, 0, "reg", &addr);
504 	if (err < 0) {
505 		error("failed to parse \"reg\" property");
506 		return err;
507 	}
508 
509 	*index = PCI_DEV(addr.phys_hi) - 1;
510 
511 	return 0;
512 }
513 
514 static int tegra_pcie_parse_dt(const void *fdt, int node,
515 			       struct tegra_pcie *pcie)
516 {
517 	int err, subnode;
518 	u32 lanes = 0;
519 
520 	err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "pads",
521 				     &pcie->pads);
522 	if (err < 0) {
523 		error("resource \"pads\" not found");
524 		return err;
525 	}
526 
527 	err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "afi",
528 				     &pcie->afi);
529 	if (err < 0) {
530 		error("resource \"afi\" not found");
531 		return err;
532 	}
533 
534 	err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "cs",
535 				     &pcie->cs);
536 	if (err < 0) {
537 		error("resource \"cs\" not found");
538 		return err;
539 	}
540 
541 	pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
542 	if (pcie->phy) {
543 		err = tegra_xusb_phy_prepare(pcie->phy);
544 		if (err < 0) {
545 			error("failed to prepare PHY: %d", err);
546 			return err;
547 		}
548 	}
549 
550 	err = tegra_pcie_parse_dt_ranges(fdt, node, pcie);
551 	if (err < 0) {
552 		error("failed to parse \"ranges\" property");
553 		return err;
554 	}
555 
556 	fdt_for_each_subnode(fdt, subnode, node) {
557 		unsigned int index = 0, num_lanes = 0;
558 		struct tegra_pcie_port *port;
559 
560 		err = tegra_pcie_parse_port_info(fdt, subnode, &index,
561 						 &num_lanes);
562 		if (err < 0) {
563 			error("failed to obtain root port info");
564 			continue;
565 		}
566 
567 		lanes |= num_lanes << (index << 3);
568 
569 		if (!fdtdec_get_is_enabled(fdt, subnode))
570 			continue;
571 
572 		port = malloc(sizeof(*port));
573 		if (!port)
574 			continue;
575 
576 		memset(port, 0, sizeof(*port));
577 		port->num_lanes = num_lanes;
578 		port->index = index;
579 
580 		err = tegra_pcie_port_parse_dt(fdt, subnode, port);
581 		if (err < 0) {
582 			free(port);
583 			continue;
584 		}
585 
586 		list_add_tail(&port->list, &pcie->ports);
587 		port->pcie = pcie;
588 	}
589 
590 	err = tegra_pcie_get_xbar_config(fdt, node, lanes, &pcie->xbar);
591 	if (err < 0) {
592 		error("invalid lane configuration");
593 		return err;
594 	}
595 
596 	return 0;
597 }
598 
599 int __weak tegra_pcie_board_init(void)
600 {
601 	return 0;
602 }
603 
604 static int tegra_pcie_power_on(struct tegra_pcie *pcie)
605 {
606 	const struct tegra_pcie_soc *soc = pcie->soc;
607 	unsigned long value;
608 	int err;
609 
610 	/* reset PCIEXCLK logic, AFI controller and PCIe controller */
611 	reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
612 	reset_set_enable(PERIPH_ID_AFI, 1);
613 	reset_set_enable(PERIPH_ID_PCIE, 1);
614 
615 	err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
616 	if (err < 0) {
617 		error("failed to power off PCIe partition: %d", err);
618 		return err;
619 	}
620 
621 	err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
622 						PERIPH_ID_PCIE);
623 	if (err < 0) {
624 		error("failed to power up PCIe partition: %d", err);
625 		return err;
626 	}
627 
628 	/* take AFI controller out of reset */
629 	reset_set_enable(PERIPH_ID_AFI, 0);
630 
631 	/* enable AFI clock */
632 	clock_enable(PERIPH_ID_AFI);
633 
634 	if (soc->has_cml_clk) {
635 		/* enable CML clock */
636 		value = readl(NV_PA_CLK_RST_BASE + 0x48c);
637 		value |= (1 << 0);
638 		value &= ~(1 << 1);
639 		writel(value, NV_PA_CLK_RST_BASE + 0x48c);
640 	}
641 
642 	err = tegra_plle_enable();
643 	if (err < 0) {
644 		error("failed to enable PLLE: %d\n", err);
645 		return err;
646 	}
647 
648 	return 0;
649 }
650 
651 static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
652 {
653 	const struct tegra_pcie_soc *soc = pcie->soc;
654 	unsigned long start = get_timer(0);
655 	u32 value;
656 
657 	while (get_timer(start) < timeout) {
658 		value = pads_readl(pcie, soc->pads_pll_ctl);
659 		if (value & PADS_PLL_CTL_LOCKDET)
660 			return 0;
661 	}
662 
663 	return -ETIMEDOUT;
664 }
665 
666 static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
667 {
668 	const struct tegra_pcie_soc *soc = pcie->soc;
669 	u32 value;
670 	int err;
671 
672 	/* initialize internal PHY, enable up to 16 PCIe lanes */
673 	pads_writel(pcie, 0, PADS_CTL_SEL);
674 
675 	/* override IDDQ to 1 on all 4 lanes */
676 	value = pads_readl(pcie, PADS_CTL);
677 	value |= PADS_CTL_IDDQ_1L;
678 	pads_writel(pcie, value, PADS_CTL);
679 
680 	/*
681 	 * Set up PHY PLL inputs select PLLE output as refclock, set TX
682 	 * ref sel to div10 (not div5).
683 	 */
684 	value = pads_readl(pcie, soc->pads_pll_ctl);
685 	value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
686 	value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
687 	pads_writel(pcie, value, soc->pads_pll_ctl);
688 
689 	/* reset PLL */
690 	value = pads_readl(pcie, soc->pads_pll_ctl);
691 	value &= ~PADS_PLL_CTL_RST_B4SM;
692 	pads_writel(pcie, value, soc->pads_pll_ctl);
693 
694 	udelay(20);
695 
696 	/* take PLL out of reset */
697 	value = pads_readl(pcie, soc->pads_pll_ctl);
698 	value |= PADS_PLL_CTL_RST_B4SM;
699 	pads_writel(pcie, value, soc->pads_pll_ctl);
700 
701 	/* configure the reference clock driver */
702 	value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
703 	pads_writel(pcie, value, PADS_REFCLK_CFG0);
704 
705 	if (soc->num_ports > 2)
706 		pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
707 
708 	/* wait for the PLL to lock */
709 	err = tegra_pcie_pll_wait(pcie, 500);
710 	if (err < 0) {
711 		error("PLL failed to lock: %d", err);
712 		return err;
713 	}
714 
715 	/* turn off IDDQ override */
716 	value = pads_readl(pcie, PADS_CTL);
717 	value &= ~PADS_CTL_IDDQ_1L;
718 	pads_writel(pcie, value, PADS_CTL);
719 
720 	/* enable TX/RX data */
721 	value = pads_readl(pcie, PADS_CTL);
722 	value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
723 	pads_writel(pcie, value, PADS_CTL);
724 
725 	return 0;
726 }
727 
728 static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
729 {
730 	const struct tegra_pcie_soc *soc = pcie->soc;
731 	struct tegra_pcie_port *port;
732 	u32 value;
733 	int err;
734 
735 	if (pcie->phy) {
736 		value = afi_readl(pcie, AFI_PLLE_CONTROL);
737 		value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
738 		value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
739 		afi_writel(pcie, value, AFI_PLLE_CONTROL);
740 	}
741 
742 	if (soc->has_pex_bias_ctrl)
743 		afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
744 
745 	value = afi_readl(pcie, AFI_PCIE_CONFIG);
746 	value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
747 	value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
748 
749 	list_for_each_entry(port, &pcie->ports, list)
750 		value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
751 
752 	afi_writel(pcie, value, AFI_PCIE_CONFIG);
753 
754 	value = afi_readl(pcie, AFI_FUSE);
755 
756 	if (soc->has_gen2)
757 		value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
758 	else
759 		value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
760 
761 	afi_writel(pcie, value, AFI_FUSE);
762 
763 	if (pcie->phy)
764 		err = tegra_xusb_phy_enable(pcie->phy);
765 	else
766 		err = tegra_pcie_phy_enable(pcie);
767 
768 	if (err < 0) {
769 		error("failed to power on PHY: %d\n", err);
770 		return err;
771 	}
772 
773 	/* take the PCIEXCLK logic out of reset */
774 	reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
775 
776 	/* finally enable PCIe */
777 	value = afi_readl(pcie, AFI_CONFIGURATION);
778 	value |= AFI_CONFIGURATION_EN_FPCI;
779 	afi_writel(pcie, value, AFI_CONFIGURATION);
780 
781 	/* disable all interrupts */
782 	afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
783 	afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
784 	afi_writel(pcie, 0, AFI_INTR_MASK);
785 	afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
786 
787 	return 0;
788 }
789 
790 static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
791 {
792 	unsigned long fpci, axi, size;
793 
794 	/* BAR 0: type 1 extended configuration space */
795 	fpci = 0xfe100000;
796 	size = fdt_resource_size(&pcie->cs);
797 	axi = pcie->cs.start;
798 
799 	afi_writel(pcie, axi, AFI_AXI_BAR0_START);
800 	afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
801 	afi_writel(pcie, fpci, AFI_FPCI_BAR0);
802 
803 	/* BAR 1: downstream I/O */
804 	fpci = 0xfdfc0000;
805 	size = fdt_resource_size(&pcie->io);
806 	axi = pcie->io.start;
807 
808 	afi_writel(pcie, axi, AFI_AXI_BAR1_START);
809 	afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
810 	afi_writel(pcie, fpci, AFI_FPCI_BAR1);
811 
812 	/* BAR 2: prefetchable memory */
813 	fpci = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
814 	size = fdt_resource_size(&pcie->prefetch);
815 	axi = pcie->prefetch.start;
816 
817 	afi_writel(pcie, axi, AFI_AXI_BAR2_START);
818 	afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
819 	afi_writel(pcie, fpci, AFI_FPCI_BAR2);
820 
821 	/* BAR 3: non-prefetchable memory */
822 	fpci = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
823 	size = fdt_resource_size(&pcie->mem);
824 	axi = pcie->mem.start;
825 
826 	afi_writel(pcie, axi, AFI_AXI_BAR3_START);
827 	afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
828 	afi_writel(pcie, fpci, AFI_FPCI_BAR3);
829 
830 	/* NULL out the remaining BARs as they are not used */
831 	afi_writel(pcie, 0, AFI_AXI_BAR4_START);
832 	afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
833 	afi_writel(pcie, 0, AFI_FPCI_BAR4);
834 
835 	afi_writel(pcie, 0, AFI_AXI_BAR5_START);
836 	afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
837 	afi_writel(pcie, 0, AFI_FPCI_BAR5);
838 
839 	/* map all upstream transactions as uncached */
840 	afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
841 	afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
842 	afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
843 	afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
844 
845 	/* MSI translations are setup only when needed */
846 	afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
847 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
848 	afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
849 	afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
850 }
851 
852 static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
853 {
854 	unsigned long ret = 0;
855 
856 	switch (port->index) {
857 	case 0:
858 		ret = AFI_PEX0_CTRL;
859 		break;
860 
861 	case 1:
862 		ret = AFI_PEX1_CTRL;
863 		break;
864 
865 	case 2:
866 		ret = AFI_PEX2_CTRL;
867 		break;
868 	}
869 
870 	return ret;
871 }
872 
873 static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
874 {
875 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
876 	unsigned long value;
877 
878 	/* pulse reset signel */
879 	value = afi_readl(port->pcie, ctrl);
880 	value &= ~AFI_PEX_CTRL_RST;
881 	afi_writel(port->pcie, value, ctrl);
882 
883 	udelay(2000);
884 
885 	value = afi_readl(port->pcie, ctrl);
886 	value |= AFI_PEX_CTRL_RST;
887 	afi_writel(port->pcie, value, ctrl);
888 }
889 
890 static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
891 {
892 	const struct tegra_pcie_soc *soc = port->pcie->soc;
893 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
894 	unsigned long value;
895 
896 	/* enable reference clock */
897 	value = afi_readl(port->pcie, ctrl);
898 	value |= AFI_PEX_CTRL_REFCLK_EN;
899 
900 	if (port->pcie->soc->has_pex_clkreq_en)
901 		value |= AFI_PEX_CTRL_CLKREQ_EN;
902 
903 	value |= AFI_PEX_CTRL_OVERRIDE_EN;
904 
905 	afi_writel(port->pcie, value, ctrl);
906 
907 	tegra_pcie_port_reset(port);
908 
909 	if (soc->force_pca_enable) {
910 		value = rp_readl(port, RP_VEND_CTL2);
911 		value |= RP_VEND_CTL2_PCA_ENABLE;
912 		rp_writel(port, value, RP_VEND_CTL2);
913 	}
914 }
915 
916 static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
917 {
918 	unsigned int retries = 3;
919 	unsigned long value;
920 
921 	value = rp_readl(port, RP_PRIV_MISC);
922 	value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
923 	value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
924 	rp_writel(port, value, RP_PRIV_MISC);
925 
926 	do {
927 		unsigned int timeout = 200;
928 
929 		do {
930 			value = rp_readl(port, RP_VEND_XP);
931 			if (value & RP_VEND_XP_DL_UP)
932 				break;
933 
934 			udelay(2000);
935 		} while (--timeout);
936 
937 		if (!timeout) {
938 			debug("link %u down, retrying\n", port->index);
939 			goto retry;
940 		}
941 
942 		timeout = 200;
943 
944 		do {
945 			value = rp_readl(port, RP_LINK_CONTROL_STATUS);
946 			if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
947 				return true;
948 
949 			udelay(2000);
950 		} while (--timeout);
951 
952 retry:
953 		tegra_pcie_port_reset(port);
954 	} while (--retries);
955 
956 	return false;
957 }
958 
959 static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
960 {
961 	unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
962 	unsigned long value;
963 
964 	/* assert port reset */
965 	value = afi_readl(port->pcie, ctrl);
966 	value &= ~AFI_PEX_CTRL_RST;
967 	afi_writel(port->pcie, value, ctrl);
968 
969 	/* disable reference clock */
970 	value = afi_readl(port->pcie, ctrl);
971 	value &= ~AFI_PEX_CTRL_REFCLK_EN;
972 	afi_writel(port->pcie, value, ctrl);
973 }
974 
975 static void tegra_pcie_port_free(struct tegra_pcie_port *port)
976 {
977 	list_del(&port->list);
978 	free(port);
979 }
980 
981 static int tegra_pcie_enable(struct tegra_pcie *pcie)
982 {
983 	struct tegra_pcie_port *port, *tmp;
984 
985 	list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
986 		debug("probing port %u, using %u lanes\n", port->index,
987 		      port->num_lanes);
988 
989 		tegra_pcie_port_enable(port);
990 
991 		if (tegra_pcie_port_check_link(port))
992 			continue;
993 
994 		debug("link %u down, ignoring\n", port->index);
995 
996 		tegra_pcie_port_disable(port);
997 		tegra_pcie_port_free(port);
998 	}
999 
1000 	return 0;
1001 }
1002 
1003 static const struct tegra_pcie_soc tegra20_pcie_soc = {
1004 	.num_ports = 2,
1005 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
1006 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
1007 	.has_pex_clkreq_en = false,
1008 	.has_pex_bias_ctrl = false,
1009 	.has_cml_clk = false,
1010 	.has_gen2 = false,
1011 	.force_pca_enable = false,
1012 };
1013 
1014 static const struct tegra_pcie_soc tegra30_pcie_soc = {
1015 	.num_ports = 3,
1016 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1017 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1018 	.has_pex_clkreq_en = true,
1019 	.has_pex_bias_ctrl = true,
1020 	.has_cml_clk = true,
1021 	.has_gen2 = false,
1022 	.force_pca_enable = false,
1023 };
1024 
1025 static const struct tegra_pcie_soc tegra124_pcie_soc = {
1026 	.num_ports = 2,
1027 	.pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
1028 	.tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
1029 	.has_pex_clkreq_en = true,
1030 	.has_pex_bias_ctrl = true,
1031 	.has_cml_clk = true,
1032 	.has_gen2 = true,
1033 	.force_pca_enable = false,
1034 };
1035 
1036 static int process_nodes(const void *fdt, int nodes[], unsigned int count)
1037 {
1038 	unsigned int i;
1039 	uint64_t dram_end;
1040 	uint32_t pci_dram_size;
1041 
1042 	/* Clip PCI-accessible DRAM to 32-bits */
1043 	dram_end = ((uint64_t)NV_PA_SDRAM_BASE) + gd->ram_size;
1044 	if (dram_end > 0x100000000)
1045 		dram_end = 0x100000000;
1046 	pci_dram_size = dram_end - NV_PA_SDRAM_BASE;
1047 
1048 	for (i = 0; i < count; i++) {
1049 		const struct tegra_pcie_soc *soc;
1050 		struct tegra_pcie *pcie;
1051 		enum fdt_compat_id id;
1052 		int err;
1053 
1054 		if (!fdtdec_get_is_enabled(fdt, nodes[i]))
1055 			continue;
1056 
1057 		id = fdtdec_lookup(fdt, nodes[i]);
1058 		switch (id) {
1059 		case COMPAT_NVIDIA_TEGRA20_PCIE:
1060 			soc = &tegra20_pcie_soc;
1061 			break;
1062 
1063 		case COMPAT_NVIDIA_TEGRA30_PCIE:
1064 			soc = &tegra30_pcie_soc;
1065 			break;
1066 
1067 		case COMPAT_NVIDIA_TEGRA124_PCIE:
1068 			soc = &tegra124_pcie_soc;
1069 			break;
1070 
1071 		default:
1072 			error("unsupported compatible: %s",
1073 			      fdtdec_get_compatible(id));
1074 			continue;
1075 		}
1076 
1077 		pcie = malloc(sizeof(*pcie));
1078 		if (!pcie) {
1079 			error("failed to allocate controller");
1080 			continue;
1081 		}
1082 
1083 		memset(pcie, 0, sizeof(*pcie));
1084 		pcie->soc = soc;
1085 
1086 		INIT_LIST_HEAD(&pcie->ports);
1087 
1088 		err = tegra_pcie_parse_dt(fdt, nodes[i], pcie);
1089 		if (err < 0) {
1090 			free(pcie);
1091 			continue;
1092 		}
1093 
1094 		err = tegra_pcie_power_on(pcie);
1095 		if (err < 0) {
1096 			error("failed to power on");
1097 			continue;
1098 		}
1099 
1100 		err = tegra_pcie_enable_controller(pcie);
1101 		if (err < 0) {
1102 			error("failed to enable controller");
1103 			continue;
1104 		}
1105 
1106 		tegra_pcie_setup_translations(pcie);
1107 
1108 		err = tegra_pcie_enable(pcie);
1109 		if (err < 0) {
1110 			error("failed to enable PCIe");
1111 			continue;
1112 		}
1113 
1114 		pcie->hose.first_busno = 0;
1115 		pcie->hose.current_busno = 0;
1116 		pcie->hose.last_busno = 0;
1117 
1118 		pci_set_region(&pcie->hose.regions[0], NV_PA_SDRAM_BASE,
1119 			       NV_PA_SDRAM_BASE, pci_dram_size,
1120 			       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1121 
1122 		pci_set_region(&pcie->hose.regions[1], pcie->io.start,
1123 			       pcie->io.start, fdt_resource_size(&pcie->io),
1124 			       PCI_REGION_IO);
1125 
1126 		pci_set_region(&pcie->hose.regions[2], pcie->mem.start,
1127 			       pcie->mem.start, fdt_resource_size(&pcie->mem),
1128 			       PCI_REGION_MEM);
1129 
1130 		pci_set_region(&pcie->hose.regions[3], pcie->prefetch.start,
1131 			       pcie->prefetch.start,
1132 			       fdt_resource_size(&pcie->prefetch),
1133 			       PCI_REGION_MEM | PCI_REGION_PREFETCH);
1134 
1135 		pcie->hose.region_count = 4;
1136 
1137 		pci_set_ops(&pcie->hose,
1138 			    pci_hose_read_config_byte_via_dword,
1139 			    pci_hose_read_config_word_via_dword,
1140 			    tegra_pcie_read_conf,
1141 			    pci_hose_write_config_byte_via_dword,
1142 			    pci_hose_write_config_word_via_dword,
1143 			    tegra_pcie_write_conf);
1144 
1145 		pci_register_hose(&pcie->hose);
1146 
1147 #ifdef CONFIG_PCI_SCAN_SHOW
1148 		printf("PCI: Enumerating devices...\n");
1149 		printf("---------------------------------------\n");
1150 		printf("  Device        ID          Description\n");
1151 		printf("  ------        --          -----------\n");
1152 #endif
1153 
1154 		pcie->hose.last_busno = pci_hose_scan(&pcie->hose);
1155 	}
1156 
1157 	return 0;
1158 }
1159 
1160 void pci_init_board(void)
1161 {
1162 	const void *fdt = gd->fdt_blob;
1163 	int count, nodes[1];
1164 
1165 	tegra_pcie_board_init();
1166 
1167 	count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
1168 					   COMPAT_NVIDIA_TEGRA124_PCIE,
1169 					   nodes, ARRAY_SIZE(nodes));
1170 	if (process_nodes(fdt, nodes, count))
1171 		return;
1172 
1173 	count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
1174 					   COMPAT_NVIDIA_TEGRA30_PCIE,
1175 					   nodes, ARRAY_SIZE(nodes));
1176 	if (process_nodes(fdt, nodes, count))
1177 		return;
1178 
1179 	count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
1180 					   COMPAT_NVIDIA_TEGRA20_PCIE,
1181 					   nodes, ARRAY_SIZE(nodes));
1182 	if (process_nodes(fdt, nodes, count))
1183 		return;
1184 }
1185 
1186 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
1187 {
1188 	if (PCI_BUS(dev) != 0 && PCI_DEV(dev) > 0)
1189 		return 1;
1190 
1191 	return 0;
1192 }
1193