xref: /rk3399_rockchip-uboot/drivers/pci/fsl_pci_init.c (revision aceea941b65130459eae1e6faec14768d1ce9d24)
193a686eeSJean-Christophe PLAGNIOL-VILLARD /*
2505f3e6fSMinghuan Lian  * Copyright 2007-2012 Freescale Semiconductor, Inc.
393a686eeSJean-Christophe PLAGNIOL-VILLARD  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
593a686eeSJean-Christophe PLAGNIOL-VILLARD  */
693a686eeSJean-Christophe PLAGNIOL-VILLARD 
793a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
8a4aafcc9SKumar Gala #include <malloc.h>
9a4aafcc9SKumar Gala #include <asm/fsl_serdes.h>
1093a686eeSJean-Christophe PLAGNIOL-VILLARD 
11b9a1fa97SKumar Gala DECLARE_GLOBAL_DATA_PTR;
12b9a1fa97SKumar Gala 
1393a686eeSJean-Christophe PLAGNIOL-VILLARD /*
1493a686eeSJean-Christophe PLAGNIOL-VILLARD  * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
1593a686eeSJean-Christophe PLAGNIOL-VILLARD  *
1693a686eeSJean-Christophe PLAGNIOL-VILLARD  * Initialize controller and call the common driver/pci pci_hose_scan to
1793a686eeSJean-Christophe PLAGNIOL-VILLARD  * scan for bridges and devices.
1893a686eeSJean-Christophe PLAGNIOL-VILLARD  *
1993a686eeSJean-Christophe PLAGNIOL-VILLARD  * Hose fields which need to be pre-initialized by board specific code:
2093a686eeSJean-Christophe PLAGNIOL-VILLARD  *   regions[]
2193a686eeSJean-Christophe PLAGNIOL-VILLARD  *   first_busno
2293a686eeSJean-Christophe PLAGNIOL-VILLARD  *
2393a686eeSJean-Christophe PLAGNIOL-VILLARD  * Fields updated:
2493a686eeSJean-Christophe PLAGNIOL-VILLARD  *   last_busno
2593a686eeSJean-Christophe PLAGNIOL-VILLARD  */
2693a686eeSJean-Christophe PLAGNIOL-VILLARD 
2793a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
28ad19e7a5SKumar Gala #include <asm/io.h>
29c8514622SKumar Gala #include <asm/fsl_pci.h>
3093a686eeSJean-Christophe PLAGNIOL-VILLARD 
31b9a1fa97SKumar Gala #ifndef CONFIG_SYS_PCI_MEMORY_BUS
32b9a1fa97SKumar Gala #define CONFIG_SYS_PCI_MEMORY_BUS 0
33b9a1fa97SKumar Gala #endif
34b9a1fa97SKumar Gala 
35b9a1fa97SKumar Gala #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
36b9a1fa97SKumar Gala #define CONFIG_SYS_PCI_MEMORY_PHYS 0
37b9a1fa97SKumar Gala #endif
38b9a1fa97SKumar Gala 
39b9a1fa97SKumar Gala #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
40b9a1fa97SKumar Gala #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
41b9a1fa97SKumar Gala #endif
42b9a1fa97SKumar Gala 
43ad19e7a5SKumar Gala /* Setup one inbound ATMU window.
44ad19e7a5SKumar Gala  *
45ad19e7a5SKumar Gala  * We let the caller decide what the window size should be
46ad19e7a5SKumar Gala  */
47ad19e7a5SKumar Gala static void set_inbound_window(volatile pit_t *pi,
48ad19e7a5SKumar Gala 				struct pci_region *r,
49ad19e7a5SKumar Gala 				u64 size)
50b9a1fa97SKumar Gala {
51ad19e7a5SKumar Gala 	u32 sz = (__ilog2_u64(size) - 1);
52ad19e7a5SKumar Gala 	u32 flag = PIWAR_EN | PIWAR_LOCAL |
53ad19e7a5SKumar Gala 			PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
54ad19e7a5SKumar Gala 
55ad19e7a5SKumar Gala 	out_be32(&pi->pitar, r->phys_start >> 12);
56ad19e7a5SKumar Gala 	out_be32(&pi->piwbar, r->bus_start >> 12);
57ad19e7a5SKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
58ad19e7a5SKumar Gala 	out_be32(&pi->piwbear, r->bus_start >> 44);
59ad19e7a5SKumar Gala #else
60ad19e7a5SKumar Gala 	out_be32(&pi->piwbear, 0);
61ad19e7a5SKumar Gala #endif
62ad19e7a5SKumar Gala 	if (r->flags & PCI_REGION_PREFETCH)
63ad19e7a5SKumar Gala 		flag |= PIWAR_PF;
64ad19e7a5SKumar Gala 	out_be32(&pi->piwar, flag | sz);
65ad19e7a5SKumar Gala }
66ad19e7a5SKumar Gala 
67ee53650dSKumar Gala int fsl_setup_hose(struct pci_controller *hose, unsigned long addr)
68ee53650dSKumar Gala {
69ee53650dSKumar Gala 	volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) addr;
70ee53650dSKumar Gala 
7196d61603SJohn Schmoller 	/* Reset hose to make sure its in a clean state */
7296d61603SJohn Schmoller 	memset(hose, 0, sizeof(struct pci_controller));
7396d61603SJohn Schmoller 
74ee53650dSKumar Gala 	pci_setup_indirect(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
75ee53650dSKumar Gala 
76ee53650dSKumar Gala 	return fsl_is_pci_agent(hose);
77ee53650dSKumar Gala }
78ee53650dSKumar Gala 
79ad19e7a5SKumar Gala static int fsl_pci_setup_inbound_windows(struct pci_controller *hose,
80ad19e7a5SKumar Gala 					 u64 out_lo, u8 pcie_cap,
81ad19e7a5SKumar Gala 					 volatile pit_t *pi)
82ad19e7a5SKumar Gala {
83ad19e7a5SKumar Gala 	struct pci_region *r = hose->regions + hose->region_count;
84ad19e7a5SKumar Gala 	u64 sz = min((u64)gd->ram_size, (1ull << 32));
85b9a1fa97SKumar Gala 
86b9a1fa97SKumar Gala 	phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
87b9a1fa97SKumar Gala 	pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
88ad19e7a5SKumar Gala 	pci_size_t pci_sz;
89b9a1fa97SKumar Gala 
90ad19e7a5SKumar Gala 	/* we have no space available for inbound memory mapping */
91ad19e7a5SKumar Gala 	if (bus_start > out_lo) {
92ad19e7a5SKumar Gala 		printf ("no space for inbound mapping of memory\n");
93ad19e7a5SKumar Gala 		return 0;
94ad19e7a5SKumar Gala 	}
95ad19e7a5SKumar Gala 
96ad19e7a5SKumar Gala 	/* limit size */
97ad19e7a5SKumar Gala 	if ((bus_start + sz) > out_lo) {
98ad19e7a5SKumar Gala 		sz = out_lo - bus_start;
99ad19e7a5SKumar Gala 		debug ("limiting size to %llx\n", sz);
100ad19e7a5SKumar Gala 	}
101ad19e7a5SKumar Gala 
102ad19e7a5SKumar Gala 	pci_sz = 1ull << __ilog2_u64(sz);
103ad19e7a5SKumar Gala 	/*
104ad19e7a5SKumar Gala 	 * we can overlap inbound/outbound windows on PCI-E since RX & TX
105ad19e7a5SKumar Gala 	 * links a separate
106ad19e7a5SKumar Gala 	 */
107ad19e7a5SKumar Gala 	if ((pcie_cap == PCI_CAP_ID_EXP) && (pci_sz < sz)) {
108b9a1fa97SKumar Gala 		debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
109ad19e7a5SKumar Gala 			(u64)bus_start, (u64)phys_start, (u64)sz);
110ad19e7a5SKumar Gala 		pci_set_region(r, bus_start, phys_start, sz,
111ff4e66e9SKumar Gala 				PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
112b9a1fa97SKumar Gala 				PCI_REGION_PREFETCH);
113b9a1fa97SKumar Gala 
114ad19e7a5SKumar Gala 		/* if we aren't an exact power of two match, pci_sz is smaller
115ad19e7a5SKumar Gala 		 * round it up to the next power of two.  We report the actual
116ad19e7a5SKumar Gala 		 * size to pci region tracking.
117ad19e7a5SKumar Gala 		 */
118ad19e7a5SKumar Gala 		if (pci_sz != sz)
119ad19e7a5SKumar Gala 			sz = 2ull << __ilog2_u64(sz);
120ad19e7a5SKumar Gala 
121ad19e7a5SKumar Gala 		set_inbound_window(pi--, r++, sz);
122ad19e7a5SKumar Gala 		sz = 0; /* make sure we dont set the R2 window */
123ad19e7a5SKumar Gala 	} else {
124ad19e7a5SKumar Gala 		debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
125ad19e7a5SKumar Gala 			(u64)bus_start, (u64)phys_start, (u64)pci_sz);
126ad19e7a5SKumar Gala 		pci_set_region(r, bus_start, phys_start, pci_sz,
127ad19e7a5SKumar Gala 				PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
128ad19e7a5SKumar Gala 				PCI_REGION_PREFETCH);
129ad19e7a5SKumar Gala 		set_inbound_window(pi--, r++, pci_sz);
130ad19e7a5SKumar Gala 
131b9a1fa97SKumar Gala 		sz -= pci_sz;
132b9a1fa97SKumar Gala 		bus_start += pci_sz;
133b9a1fa97SKumar Gala 		phys_start += pci_sz;
134b9a1fa97SKumar Gala 
135b9a1fa97SKumar Gala 		pci_sz = 1ull << __ilog2_u64(sz);
136b9a1fa97SKumar Gala 		if (sz) {
137b9a1fa97SKumar Gala 			debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
138b9a1fa97SKumar Gala 				(u64)bus_start, (u64)phys_start, (u64)pci_sz);
139ad19e7a5SKumar Gala 			pci_set_region(r, bus_start, phys_start, pci_sz,
140ff4e66e9SKumar Gala 					PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
141b9a1fa97SKumar Gala 					PCI_REGION_PREFETCH);
142ad19e7a5SKumar Gala 			set_inbound_window(pi--, r++, pci_sz);
143b9a1fa97SKumar Gala 			sz -= pci_sz;
144b9a1fa97SKumar Gala 			bus_start += pci_sz;
145b9a1fa97SKumar Gala 			phys_start += pci_sz;
146b9a1fa97SKumar Gala 		}
147ad19e7a5SKumar Gala 	}
148b9a1fa97SKumar Gala 
149b9a1fa97SKumar Gala #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
150cd425162SBecky Bruce 	/*
151cd425162SBecky Bruce 	 * On 64-bit capable systems, set up a mapping for all of DRAM
152cd425162SBecky Bruce 	 * in high pci address space.
153cd425162SBecky Bruce 	 */
154b9a1fa97SKumar Gala 	pci_sz = 1ull << __ilog2_u64(gd->ram_size);
155b9a1fa97SKumar Gala 	/* round up to the next largest power of two */
156b9a1fa97SKumar Gala 	if (gd->ram_size > pci_sz)
157cd425162SBecky Bruce 		pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
158b9a1fa97SKumar Gala 	debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
159cd425162SBecky Bruce 		(u64)CONFIG_SYS_PCI64_MEMORY_BUS,
160b9a1fa97SKumar Gala 		(u64)CONFIG_SYS_PCI_MEMORY_PHYS,
161b9a1fa97SKumar Gala 		(u64)pci_sz);
162ad19e7a5SKumar Gala 	pci_set_region(r,
163cd425162SBecky Bruce 			CONFIG_SYS_PCI64_MEMORY_BUS,
164b9a1fa97SKumar Gala 			CONFIG_SYS_PCI_MEMORY_PHYS,
165b9a1fa97SKumar Gala 			pci_sz,
166ff4e66e9SKumar Gala 			PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
167b9a1fa97SKumar Gala 			PCI_REGION_PREFETCH);
168ad19e7a5SKumar Gala 	set_inbound_window(pi--, r++, pci_sz);
169b9a1fa97SKumar Gala #else
170b9a1fa97SKumar Gala 	pci_sz = 1ull << __ilog2_u64(sz);
171b9a1fa97SKumar Gala 	if (sz) {
172b9a1fa97SKumar Gala 		debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
173b9a1fa97SKumar Gala 			(u64)bus_start, (u64)phys_start, (u64)pci_sz);
174ad19e7a5SKumar Gala 		pci_set_region(r, bus_start, phys_start, pci_sz,
175ff4e66e9SKumar Gala 				PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
176b9a1fa97SKumar Gala 				PCI_REGION_PREFETCH);
177b9a1fa97SKumar Gala 		sz -= pci_sz;
178b9a1fa97SKumar Gala 		bus_start += pci_sz;
179b9a1fa97SKumar Gala 		phys_start += pci_sz;
180ad19e7a5SKumar Gala 		set_inbound_window(pi--, r++, pci_sz);
181b9a1fa97SKumar Gala 	}
182b9a1fa97SKumar Gala #endif
183b9a1fa97SKumar Gala 
1844c253fdbSKumar Gala #ifdef CONFIG_PHYS_64BIT
185b9a1fa97SKumar Gala 	if (sz && (((u64)gd->ram_size) < (1ull << 32)))
186b9a1fa97SKumar Gala 		printf("Was not able to map all of memory via "
187b9a1fa97SKumar Gala 			"inbound windows -- %lld remaining\n", sz);
1884c253fdbSKumar Gala #endif
189b9a1fa97SKumar Gala 
190ad19e7a5SKumar Gala 	hose->region_count = r - hose->regions;
191ad19e7a5SKumar Gala 
192ad19e7a5SKumar Gala 	return 1;
193b9a1fa97SKumar Gala }
194b9a1fa97SKumar Gala 
195c8b28152SLiu Gang #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
196b5f7c873SLiu Gang static void fsl_pcie_boot_master(pit_t *pi)
197b5f7c873SLiu Gang {
198b5f7c873SLiu Gang 	/* configure inbound window for slave's u-boot image */
199b5f7c873SLiu Gang 	debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
200b5f7c873SLiu Gang 			"Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
201b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
202b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
203b5f7c873SLiu Gang 			CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
204b5f7c873SLiu Gang 	struct pci_region r_inbound;
205b5f7c873SLiu Gang 	u32 sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE)
206b5f7c873SLiu Gang 					- 1;
207b5f7c873SLiu Gang 	pci_set_region(&r_inbound,
208b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
209b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
210b5f7c873SLiu Gang 		sz_inbound,
211b5f7c873SLiu Gang 		PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
212b5f7c873SLiu Gang 
213b5f7c873SLiu Gang 	set_inbound_window(pi--, &r_inbound,
214b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
215b5f7c873SLiu Gang 
216b5f7c873SLiu Gang 	/* configure inbound window for slave's u-boot image */
217b5f7c873SLiu Gang 	debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
218b5f7c873SLiu Gang 			"Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
219b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
220b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
221b5f7c873SLiu Gang 			CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
222b5f7c873SLiu Gang 	pci_set_region(&r_inbound,
223b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
224b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
225b5f7c873SLiu Gang 		sz_inbound,
226b5f7c873SLiu Gang 		PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
227b5f7c873SLiu Gang 
228b5f7c873SLiu Gang 	set_inbound_window(pi--, &r_inbound,
229b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
230b5f7c873SLiu Gang 
231b5f7c873SLiu Gang 	/* configure inbound window for slave's ucode and ENV */
232b5f7c873SLiu Gang 	debug("PCIEBOOT - MASTER: Inbound window for slave's "
233b5f7c873SLiu Gang 			"ucode and ENV; "
234b5f7c873SLiu Gang 			"Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
235b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
236b5f7c873SLiu Gang 			(u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
237b5f7c873SLiu Gang 			CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
238b5f7c873SLiu Gang 	sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE)
239b5f7c873SLiu Gang 				- 1;
240b5f7c873SLiu Gang 	pci_set_region(&r_inbound,
241b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
242b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
243b5f7c873SLiu Gang 		sz_inbound,
244b5f7c873SLiu Gang 		PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
245b5f7c873SLiu Gang 
246b5f7c873SLiu Gang 	set_inbound_window(pi--, &r_inbound,
247b5f7c873SLiu Gang 		CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
248b5f7c873SLiu Gang }
249b5f7c873SLiu Gang 
250b5f7c873SLiu Gang static void fsl_pcie_boot_master_release_slave(int port)
251b5f7c873SLiu Gang {
252b5f7c873SLiu Gang 	unsigned long release_addr;
253b5f7c873SLiu Gang 
254b5f7c873SLiu Gang 	/* now release slave's core 0 */
255b5f7c873SLiu Gang 	switch (port) {
256b5f7c873SLiu Gang 	case 1:
257b5f7c873SLiu Gang 		release_addr = CONFIG_SYS_PCIE1_MEM_VIRT
258b5f7c873SLiu Gang 			+ CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
259b5f7c873SLiu Gang 		break;
260a1e4318cSYork Sun #ifdef CONFIG_SYS_PCIE2_MEM_VIRT
261b5f7c873SLiu Gang 	case 2:
262b5f7c873SLiu Gang 		release_addr = CONFIG_SYS_PCIE2_MEM_VIRT
263b5f7c873SLiu Gang 			+ CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
264b5f7c873SLiu Gang 		break;
265a1e4318cSYork Sun #endif
266a1e4318cSYork Sun #ifdef CONFIG_SYS_PCIE3_MEM_VIRT
267b5f7c873SLiu Gang 	case 3:
268b5f7c873SLiu Gang 		release_addr = CONFIG_SYS_PCIE3_MEM_VIRT
269b5f7c873SLiu Gang 			+ CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
270b5f7c873SLiu Gang 		break;
271a1e4318cSYork Sun #endif
272b5f7c873SLiu Gang 	default:
273b5f7c873SLiu Gang 		release_addr = 0;
274b5f7c873SLiu Gang 		break;
275b5f7c873SLiu Gang 	}
276b5f7c873SLiu Gang 	if (release_addr != 0) {
277b5f7c873SLiu Gang 		out_be32((void *)release_addr,
278b5f7c873SLiu Gang 			CONFIG_SRIO_PCIE_BOOT_RELEASE_MASK);
279b5f7c873SLiu Gang 		debug("PCIEBOOT - MASTER: "
280b5f7c873SLiu Gang 			"Release slave successfully! Now the slave should start up!\n");
281b5f7c873SLiu Gang 	} else {
282b5f7c873SLiu Gang 		debug("PCIEBOOT - MASTER: "
283b5f7c873SLiu Gang 			"Release slave failed!\n");
284b5f7c873SLiu Gang 	}
285b5f7c873SLiu Gang }
286b5f7c873SLiu Gang #endif
287b5f7c873SLiu Gang 
288213ac73eSPeter Tyser void fsl_pci_init(struct pci_controller *hose, struct fsl_pci_info *pci_info)
28993a686eeSJean-Christophe PLAGNIOL-VILLARD {
290213ac73eSPeter Tyser 	u32 cfg_addr = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_addr;
291213ac73eSPeter Tyser 	u32 cfg_data = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_data;
29293a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 temp16;
29393a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 temp32;
294b6ccd2c9SPrabhakar Kushwaha 	u32 block_rev;
2958295b944SKumar Gala 	int enabled, r, inbound = 0;
29693a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 ltssm;
2978295b944SKumar Gala 	u8 temp8, pcie_cap;
298287df01eSZhao Qiang 	int pcie_cap_pos;
299287df01eSZhao Qiang 	int pci_dcr;
300287df01eSZhao Qiang 	int pci_dsr;
301287df01eSZhao Qiang 	int pci_lsr;
302287df01eSZhao Qiang 
303287df01eSZhao Qiang #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
304287df01eSZhao Qiang 	int pci_lcr;
305287df01eSZhao Qiang #endif
306287df01eSZhao Qiang 
307fb3143b3SKumar Gala 	volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)cfg_addr;
308cb151aa2SKumar Gala 	struct pci_region *reg = hose->regions + hose->region_count;
3098295b944SKumar Gala 	pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
31093a686eeSJean-Christophe PLAGNIOL-VILLARD 
31193a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Initialize ATMU registers based on hose regions and flags */
31293a686eeSJean-Christophe PLAGNIOL-VILLARD 	volatile pot_t *po = &pci->pot[1];	/* skip 0 */
313b6ccd2c9SPrabhakar Kushwaha 	volatile pit_t *pi;
314ad19e7a5SKumar Gala 
315ad19e7a5SKumar Gala 	u64 out_hi = 0, out_lo = -1ULL;
316ad19e7a5SKumar Gala 	u32 pcicsrbar, pcicsrbar_sz;
31793a686eeSJean-Christophe PLAGNIOL-VILLARD 
318fb3143b3SKumar Gala 	pci_setup_indirect(hose, cfg_addr, cfg_data);
319fb3143b3SKumar Gala 
320b6ccd2c9SPrabhakar Kushwaha 	block_rev = in_be32(&pci->block_rev1);
321b6ccd2c9SPrabhakar Kushwaha 	if (PEX_IP_BLK_REV_2_2 <= block_rev) {
322b6ccd2c9SPrabhakar Kushwaha 		pi = &pci->pit[2];	/* 0xDC0 */
323b6ccd2c9SPrabhakar Kushwaha 	} else {
324b6ccd2c9SPrabhakar Kushwaha 		pi = &pci->pit[3];	/* 0xDE0 */
325b6ccd2c9SPrabhakar Kushwaha 	}
326b6ccd2c9SPrabhakar Kushwaha 
327ad19e7a5SKumar Gala 	/* Handle setup of outbound windows first */
32893a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (r = 0; r < hose->region_count; r++) {
329ad19e7a5SKumar Gala 		unsigned long flags = hose->regions[r].flags;
330612ea010SKumar Gala 		u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
331ad19e7a5SKumar Gala 
332ad19e7a5SKumar Gala 		flags &= PCI_REGION_SYS_MEMORY|PCI_REGION_TYPE;
333ad19e7a5SKumar Gala 		if (flags != PCI_REGION_SYS_MEMORY) {
334ad19e7a5SKumar Gala 			u64 start = hose->regions[r].bus_start;
335ad19e7a5SKumar Gala 			u64 end = start + hose->regions[r].size;
336ad19e7a5SKumar Gala 
337ad19e7a5SKumar Gala 			out_be32(&po->powbar, hose->regions[r].phys_start >> 12);
338ad19e7a5SKumar Gala 			out_be32(&po->potar, start >> 12);
339612ea010SKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
340ad19e7a5SKumar Gala 			out_be32(&po->potear, start >> 44);
341612ea010SKumar Gala #else
342ad19e7a5SKumar Gala 			out_be32(&po->potear, 0);
343612ea010SKumar Gala #endif
344ad19e7a5SKumar Gala 			if (hose->regions[r].flags & PCI_REGION_IO) {
345ad19e7a5SKumar Gala 				out_be32(&po->powar, POWAR_EN | sz |
346ad19e7a5SKumar Gala 					POWAR_IO_READ | POWAR_IO_WRITE);
347ad19e7a5SKumar Gala 			} else {
348ad19e7a5SKumar Gala 				out_be32(&po->powar, POWAR_EN | sz |
349ad19e7a5SKumar Gala 					POWAR_MEM_READ | POWAR_MEM_WRITE);
350ad19e7a5SKumar Gala 				out_lo = min(start, out_lo);
351ad19e7a5SKumar Gala 				out_hi = max(end, out_hi);
352ad19e7a5SKumar Gala 			}
35393a686eeSJean-Christophe PLAGNIOL-VILLARD 			po++;
35493a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
35593a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
356ad19e7a5SKumar Gala 	debug("Outbound memory range: %llx:%llx\n", out_lo, out_hi);
357ad19e7a5SKumar Gala 
358ad19e7a5SKumar Gala 	/* setup PCSRBAR/PEXCSRBAR */
359ad19e7a5SKumar Gala 	pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
360ad19e7a5SKumar Gala 	pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
361ad19e7a5SKumar Gala 	pcicsrbar_sz = ~pcicsrbar_sz + 1;
362ad19e7a5SKumar Gala 
363ad19e7a5SKumar Gala 	if (out_hi < (0x100000000ull - pcicsrbar_sz) ||
364ad19e7a5SKumar Gala 		(out_lo > 0x100000000ull))
365ad19e7a5SKumar Gala 		pcicsrbar = 0x100000000ull - pcicsrbar_sz;
366ad19e7a5SKumar Gala 	else
367ad19e7a5SKumar Gala 		pcicsrbar = (out_lo - pcicsrbar_sz) & -pcicsrbar_sz;
368ad19e7a5SKumar Gala 	pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, pcicsrbar);
369ad19e7a5SKumar Gala 
370ad19e7a5SKumar Gala 	out_lo = min(out_lo, (u64)pcicsrbar);
371ad19e7a5SKumar Gala 
372ad19e7a5SKumar Gala 	debug("PCICSRBAR @ 0x%x\n", pcicsrbar);
373ad19e7a5SKumar Gala 
374ad19e7a5SKumar Gala 	pci_set_region(reg++, pcicsrbar, CONFIG_SYS_CCSRBAR_PHYS,
375ad19e7a5SKumar Gala 			pcicsrbar_sz, PCI_REGION_SYS_MEMORY);
376ad19e7a5SKumar Gala 	hose->region_count++;
37793a686eeSJean-Christophe PLAGNIOL-VILLARD 
3788295b944SKumar Gala 	/* see if we are a PCIe or PCI controller */
379287df01eSZhao Qiang 	pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
380287df01eSZhao Qiang 	pci_dcr = pcie_cap_pos + 0x08;
381287df01eSZhao Qiang 	pci_dsr = pcie_cap_pos + 0x0a;
382287df01eSZhao Qiang 	pci_lsr = pcie_cap_pos + 0x12;
383287df01eSZhao Qiang 
384287df01eSZhao Qiang 	pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
3858295b944SKumar Gala 
386c8b28152SLiu Gang #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
387b5f7c873SLiu Gang 	/* boot from PCIE --master */
388b5f7c873SLiu Gang 	char *s = getenv("bootmaster");
389b5f7c873SLiu Gang 	char pcie[6];
390b5f7c873SLiu Gang 	sprintf(pcie, "PCIE%d", pci_info->pci_num);
391b5f7c873SLiu Gang 
392b5f7c873SLiu Gang 	if (s && (strcmp(s, pcie) == 0)) {
393b5f7c873SLiu Gang 		debug("PCIEBOOT - MASTER: Master port [ %d ] for pcie boot.\n",
394b5f7c873SLiu Gang 				pci_info->pci_num);
395b5f7c873SLiu Gang 		fsl_pcie_boot_master((pit_t *)pi);
396b5f7c873SLiu Gang 	} else {
397b5f7c873SLiu Gang 		/* inbound */
398b5f7c873SLiu Gang 		inbound = fsl_pci_setup_inbound_windows(hose,
399b5f7c873SLiu Gang 					out_lo, pcie_cap, pi);
400b5f7c873SLiu Gang 	}
401b5f7c873SLiu Gang #else
402ad19e7a5SKumar Gala 	/* inbound */
403ad19e7a5SKumar Gala 	inbound = fsl_pci_setup_inbound_windows(hose, out_lo, pcie_cap, pi);
404b5f7c873SLiu Gang #endif
405ad19e7a5SKumar Gala 
406ad19e7a5SKumar Gala 	for (r = 0; r < hose->region_count; r++)
407d015df8fSMarek Vasut 		debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n", r,
408ad19e7a5SKumar Gala 			(u64)hose->regions[r].phys_start,
409d015df8fSMarek Vasut 			(u64)hose->regions[r].bus_start,
410d015df8fSMarek Vasut 			(u64)hose->regions[r].size,
411ad19e7a5SKumar Gala 			hose->regions[r].flags);
412ad19e7a5SKumar Gala 
41393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_register_hose(hose);
41493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);	/* grab pci_{mem,prefetch,io} */
41593a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->current_busno = hose->first_busno;
41693a686eeSJean-Christophe PLAGNIOL-VILLARD 
417ad19e7a5SKumar Gala 	out_be32(&pci->pedr, 0xffffffff);	/* Clear any errors */
41816263087SMike Williams 	out_be32(&pci->peer, ~0x20140);	/* Enable All Error Interrupts except
41993a686eeSJean-Christophe PLAGNIOL-VILLARD 					 * - Master abort (pci)
42093a686eeSJean-Christophe PLAGNIOL-VILLARD 					 * - Master PERR (pci)
42193a686eeSJean-Christophe PLAGNIOL-VILLARD 					 * - ICCA (PCIe)
42293a686eeSJean-Christophe PLAGNIOL-VILLARD 					 */
423287df01eSZhao Qiang 	pci_hose_read_config_dword(hose, dev, pci_dcr, &temp32);
42493a686eeSJean-Christophe PLAGNIOL-VILLARD 	temp32 |= 0xf000e;		/* set URR, FER, NFER (but not CER) */
425287df01eSZhao Qiang 	pci_hose_write_config_dword(hose, dev, pci_dcr, temp32);
42693a686eeSJean-Christophe PLAGNIOL-VILLARD 
427b03a466dSPrabhakar Kushwaha #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
428287df01eSZhao Qiang 	pci_lcr = pcie_cap_pos + 0x10;
429b03a466dSPrabhakar Kushwaha 	temp32 = 0;
430287df01eSZhao Qiang 	pci_hose_read_config_dword(hose, dev, pci_lcr, &temp32);
431b03a466dSPrabhakar Kushwaha 	temp32 &= ~0x03;		/* Disable ASPM  */
432287df01eSZhao Qiang 	pci_hose_write_config_dword(hose, dev, pci_lcr, temp32);
433b03a466dSPrabhakar Kushwaha 	udelay(1);
434b03a466dSPrabhakar Kushwaha #endif
4358295b944SKumar Gala 	if (pcie_cap == PCI_CAP_ID_EXP) {
4367b4e5844SZang Roy-R61911 		if (block_rev >= PEX_IP_BLK_REV_3_0) {
4377b4e5844SZang Roy-R61911 #define PEX_CSR0_LTSSM_MASK	0xFC
4387b4e5844SZang Roy-R61911 #define PEX_CSR0_LTSSM_SHIFT	2
4397b4e5844SZang Roy-R61911 			ltssm = (in_be32(&pci->pex_csr0)
4407b4e5844SZang Roy-R61911 				& PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
4417b4e5844SZang Roy-R61911 			enabled = (ltssm == 0x11) ? 1 : 0;
4427b4e5844SZang Roy-R61911 		} else {
4437b4e5844SZang Roy-R61911 		/* pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm); */
4447b4e5844SZang Roy-R61911 		/* enabled = ltssm >= PCI_LTSSM_L0; */
44593a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
44693a686eeSJean-Christophe PLAGNIOL-VILLARD 		enabled = ltssm >= PCI_LTSSM_L0;
44793a686eeSJean-Christophe PLAGNIOL-VILLARD 
4488ff3de61SKumar Gala #ifdef CONFIG_FSL_PCIE_RESET
4498ff3de61SKumar Gala 		if (ltssm == 1) {
4508ff3de61SKumar Gala 			int i;
451ad19e7a5SKumar Gala 			debug("....PCIe link error. " "LTSSM=0x%02x.", ltssm);
452ad19e7a5SKumar Gala 			/* assert PCIe reset */
453ad19e7a5SKumar Gala 			setbits_be32(&pci->pdb_stat, 0x08000000);
454ad19e7a5SKumar Gala 			(void) in_be32(&pci->pdb_stat);
4558ff3de61SKumar Gala 			udelay(100);
456d015df8fSMarek Vasut 			debug("  Asserting PCIe reset @%p = %x\n",
457ad19e7a5SKumar Gala 			      &pci->pdb_stat, in_be32(&pci->pdb_stat));
458ad19e7a5SKumar Gala 			/* clear PCIe reset */
459ad19e7a5SKumar Gala 			clrbits_be32(&pci->pdb_stat, 0x08000000);
4608ff3de61SKumar Gala 			asm("sync;isync");
4618ff3de61SKumar Gala 			for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
4628ff3de61SKumar Gala 				pci_hose_read_config_word(hose, dev, PCI_LTSSM,
4638ff3de61SKumar Gala 							&ltssm);
4648ff3de61SKumar Gala 				udelay(1000);
4658ff3de61SKumar Gala 				debug("....PCIe link error. "
4668ff3de61SKumar Gala 				      "LTSSM=0x%02x.\n", ltssm);
4678ff3de61SKumar Gala 			}
4688ff3de61SKumar Gala 			enabled = ltssm >= PCI_LTSSM_L0;
469ad19e7a5SKumar Gala 
470ad19e7a5SKumar Gala 			/* we need to re-write the bar0 since a reset will
471ad19e7a5SKumar Gala 			 * clear it
472ad19e7a5SKumar Gala 			 */
473ad19e7a5SKumar Gala 			pci_hose_write_config_dword(hose, dev,
474ad19e7a5SKumar Gala 					PCI_BASE_ADDRESS_0, pcicsrbar);
4758ff3de61SKumar Gala 		}
4768ff3de61SKumar Gala #endif
4777b4e5844SZang Roy-R61911 	}
4788ff3de61SKumar Gala 
479c0a4e6b8SYuanquan Chen #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
480c0a4e6b8SYuanquan Chen 		if (enabled == 0) {
481c0a4e6b8SYuanquan Chen 			serdes_corenet_t *srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
482c0a4e6b8SYuanquan Chen 			temp32 = in_be32(&srds_regs->srdspccr0);
483c0a4e6b8SYuanquan Chen 
484c0a4e6b8SYuanquan Chen 			if ((temp32 >> 28) == 3) {
485c0a4e6b8SYuanquan Chen 				int i;
486c0a4e6b8SYuanquan Chen 
487c0a4e6b8SYuanquan Chen 				out_be32(&srds_regs->srdspccr0, 2 << 28);
488c0a4e6b8SYuanquan Chen 				setbits_be32(&pci->pdb_stat, 0x08000000);
489c0a4e6b8SYuanquan Chen 				in_be32(&pci->pdb_stat);
490c0a4e6b8SYuanquan Chen 				udelay(100);
491c0a4e6b8SYuanquan Chen 				clrbits_be32(&pci->pdb_stat, 0x08000000);
492c0a4e6b8SYuanquan Chen 				asm("sync;isync");
493c0a4e6b8SYuanquan Chen 				for (i=0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
494c0a4e6b8SYuanquan Chen 					pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
495c0a4e6b8SYuanquan Chen 					udelay(1000);
496c0a4e6b8SYuanquan Chen 				}
497c0a4e6b8SYuanquan Chen 				enabled = ltssm >= PCI_LTSSM_L0;
498c0a4e6b8SYuanquan Chen 			}
499c0a4e6b8SYuanquan Chen 		}
500c0a4e6b8SYuanquan Chen #endif
50193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!enabled) {
502213ac73eSPeter Tyser 			/* Let the user know there's no PCIe link */
503213ac73eSPeter Tyser 			printf("no link, regs @ 0x%lx\n", pci_info->regs);
50493a686eeSJean-Christophe PLAGNIOL-VILLARD 			hose->last_busno = hose->first_busno;
50593a686eeSJean-Christophe PLAGNIOL-VILLARD 			return;
50693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
50793a686eeSJean-Christophe PLAGNIOL-VILLARD 
508ad19e7a5SKumar Gala 		out_be32(&pci->pme_msg_det, 0xffffffff);
509ad19e7a5SKumar Gala 		out_be32(&pci->pme_msg_int_en, 0xffffffff);
510213ac73eSPeter Tyser 
511213ac73eSPeter Tyser 		/* Print the negotiated PCIe link width */
512287df01eSZhao Qiang 		pci_hose_read_config_word(hose, dev, pci_lsr, &temp16);
513*aceea941SPrabhakar Kushwaha 		printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
514*aceea941SPrabhakar Kushwaha 		       (temp16 & 0xf), pci_info->regs);
515213ac73eSPeter Tyser 
51693a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->current_busno++; /* Start scan with secondary */
51793a686eeSJean-Christophe PLAGNIOL-VILLARD 		pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
51893a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
51993a686eeSJean-Christophe PLAGNIOL-VILLARD 
52093a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Use generic setup_device to initialize standard pci regs,
52193a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * but do not allocate any windows since any BAR found (such
52293a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * as PCSRBAR) is not in this cpu's memory space.
52393a686eeSJean-Christophe PLAGNIOL-VILLARD 	 */
52493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_setup_device(hose, dev, 0, hose->pci_mem,
52593a686eeSJean-Christophe PLAGNIOL-VILLARD 			     hose->pci_prefetch, hose->pci_io);
52693a686eeSJean-Christophe PLAGNIOL-VILLARD 
52793a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (inbound) {
52893a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
52993a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_word(hose, dev, PCI_COMMAND,
53093a686eeSJean-Christophe PLAGNIOL-VILLARD 					   temp16 | PCI_COMMAND_MEMORY);
53193a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
53293a686eeSJean-Christophe PLAGNIOL-VILLARD 
53393a686eeSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_PCI_NOSCAN
534505f3e6fSMinghuan Lian 	if (!fsl_is_pci_agent(hose)) {
53537d03fceSPeter Tyser 		debug("           Scanning PCI bus %02x\n",
5366df0efd5SEd Swarthout 			hose->current_busno);
53793a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
5386df0efd5SEd Swarthout 	} else {
5396df0efd5SEd Swarthout 		debug("           Not scanning PCI bus %02x. PI=%x\n",
5406df0efd5SEd Swarthout 			hose->current_busno, temp8);
5416df0efd5SEd Swarthout 		hose->last_busno = hose->current_busno;
5426df0efd5SEd Swarthout 	}
54393a686eeSJean-Christophe PLAGNIOL-VILLARD 
5448295b944SKumar Gala 	/* if we are PCIe - update limit regs and subordinate busno
5458295b944SKumar Gala 	 * for the virtual P2P bridge
5468295b944SKumar Gala 	 */
5478295b944SKumar Gala 	if (pcie_cap == PCI_CAP_ID_EXP) {
54893a686eeSJean-Christophe PLAGNIOL-VILLARD 		pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
54993a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
55093a686eeSJean-Christophe PLAGNIOL-VILLARD #else
55193a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->last_busno = hose->current_busno;
55293a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
55393a686eeSJean-Christophe PLAGNIOL-VILLARD 
55493a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Clear all error indications */
5558295b944SKumar Gala 	if (pcie_cap == PCI_CAP_ID_EXP)
556ad19e7a5SKumar Gala 		out_be32(&pci->pme_msg_det, 0xffffffff);
557ad19e7a5SKumar Gala 	out_be32(&pci->pedr, 0xffffffff);
55893a686eeSJean-Christophe PLAGNIOL-VILLARD 
559287df01eSZhao Qiang 	pci_hose_read_config_word(hose, dev, pci_dsr, &temp16);
56093a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (temp16) {
561287df01eSZhao Qiang 		pci_hose_write_config_word(hose, dev, pci_dsr, 0xffff);
56293a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
56393a686eeSJean-Christophe PLAGNIOL-VILLARD 
56493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
56593a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (temp16) {
56693a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
56793a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
56893a686eeSJean-Christophe PLAGNIOL-VILLARD }
569a2aab460SKumar Gala 
570715d8f76SEd Swarthout int fsl_is_pci_agent(struct pci_controller *hose)
571715d8f76SEd Swarthout {
572287df01eSZhao Qiang 	int pcie_cap_pos;
573505f3e6fSMinghuan Lian 	u8 pcie_cap;
574715d8f76SEd Swarthout 	pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
575715d8f76SEd Swarthout 
576287df01eSZhao Qiang 	pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
577287df01eSZhao Qiang 	pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
578505f3e6fSMinghuan Lian 	if (pcie_cap == PCI_CAP_ID_EXP) {
579505f3e6fSMinghuan Lian 		u8 header_type;
580715d8f76SEd Swarthout 
581505f3e6fSMinghuan Lian 		pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE,
582505f3e6fSMinghuan Lian 					  &header_type);
583505f3e6fSMinghuan Lian 		return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
584505f3e6fSMinghuan Lian 	} else {
585505f3e6fSMinghuan Lian 		u8 prog_if;
586505f3e6fSMinghuan Lian 
587505f3e6fSMinghuan Lian 		pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prog_if);
5887b4e5844SZang Roy-R61911 		/* Programming Interface (PCI_CLASS_PROG)
5897b4e5844SZang Roy-R61911 		 * 0 == pci host or pcie root-complex,
5907b4e5844SZang Roy-R61911 		 * 1 == pci agent or pcie end-point
5917b4e5844SZang Roy-R61911 		 */
592715d8f76SEd Swarthout 		return (prog_if == FSL_PROG_IF_AGENT);
593715d8f76SEd Swarthout 	}
594505f3e6fSMinghuan Lian }
595715d8f76SEd Swarthout 
5960d3d68b2SPoonam Aggrwal int fsl_pci_init_port(struct fsl_pci_info *pci_info,
59701471d53SKumar Gala 			struct pci_controller *hose, int busno)
5980d3d68b2SPoonam Aggrwal {
5990d3d68b2SPoonam Aggrwal 	volatile ccsr_fsl_pci_t *pci;
6000d3d68b2SPoonam Aggrwal 	struct pci_region *r;
601a72dbae2SPeter Tyser 	pci_dev_t dev = PCI_BDF(busno,0,0);
602287df01eSZhao Qiang 	int pcie_cap_pos;
603a72dbae2SPeter Tyser 	u8 pcie_cap;
6040d3d68b2SPoonam Aggrwal 
6050d3d68b2SPoonam Aggrwal 	pci = (ccsr_fsl_pci_t *) pci_info->regs;
6060d3d68b2SPoonam Aggrwal 
6070d3d68b2SPoonam Aggrwal 	/* on non-PCIe controllers we don't have pme_msg_det so this code
6080d3d68b2SPoonam Aggrwal 	 * should do nothing since the read will return 0
6090d3d68b2SPoonam Aggrwal 	 */
6100d3d68b2SPoonam Aggrwal 	if (in_be32(&pci->pme_msg_det)) {
6110d3d68b2SPoonam Aggrwal 		out_be32(&pci->pme_msg_det, 0xffffffff);
6120d3d68b2SPoonam Aggrwal 		debug (" with errors.  Clearing.  Now 0x%08x",
6130d3d68b2SPoonam Aggrwal 			pci->pme_msg_det);
6140d3d68b2SPoonam Aggrwal 	}
6150d3d68b2SPoonam Aggrwal 
6160d3d68b2SPoonam Aggrwal 	r = hose->regions + hose->region_count;
6170d3d68b2SPoonam Aggrwal 
6180d3d68b2SPoonam Aggrwal 	/* outbound memory */
6190d3d68b2SPoonam Aggrwal 	pci_set_region(r++,
6200d3d68b2SPoonam Aggrwal 			pci_info->mem_bus,
6210d3d68b2SPoonam Aggrwal 			pci_info->mem_phys,
6220d3d68b2SPoonam Aggrwal 			pci_info->mem_size,
6230d3d68b2SPoonam Aggrwal 			PCI_REGION_MEM);
6240d3d68b2SPoonam Aggrwal 
6250d3d68b2SPoonam Aggrwal 	/* outbound io */
6260d3d68b2SPoonam Aggrwal 	pci_set_region(r++,
6270d3d68b2SPoonam Aggrwal 			pci_info->io_bus,
6280d3d68b2SPoonam Aggrwal 			pci_info->io_phys,
6290d3d68b2SPoonam Aggrwal 			pci_info->io_size,
6300d3d68b2SPoonam Aggrwal 			PCI_REGION_IO);
6310d3d68b2SPoonam Aggrwal 
6320d3d68b2SPoonam Aggrwal 	hose->region_count = r - hose->regions;
6330d3d68b2SPoonam Aggrwal 	hose->first_busno = busno;
6340d3d68b2SPoonam Aggrwal 
635213ac73eSPeter Tyser 	fsl_pci_init(hose, pci_info);
6360d3d68b2SPoonam Aggrwal 
637715d8f76SEd Swarthout 	if (fsl_is_pci_agent(hose)) {
638715d8f76SEd Swarthout 		fsl_pci_config_unlock(hose);
639715d8f76SEd Swarthout 		hose->last_busno = hose->first_busno;
640c8b28152SLiu Gang #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
641b5f7c873SLiu Gang 	} else {
642b5f7c873SLiu Gang 		/* boot from PCIE --master releases slave's core 0 */
643b5f7c873SLiu Gang 		char *s = getenv("bootmaster");
644b5f7c873SLiu Gang 		char pcie[6];
645b5f7c873SLiu Gang 		sprintf(pcie, "PCIE%d", pci_info->pci_num);
646b5f7c873SLiu Gang 
647b5f7c873SLiu Gang 		if (s && (strcmp(s, pcie) == 0))
648b5f7c873SLiu Gang 			fsl_pcie_boot_master_release_slave(pci_info->pci_num);
649b5f7c873SLiu Gang #endif
650715d8f76SEd Swarthout 	}
651715d8f76SEd Swarthout 
652287df01eSZhao Qiang 	pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
653287df01eSZhao Qiang 	pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
6548ca78f2cSPeter Tyser 	printf("PCI%s%x: Bus %02x - %02x\n", pcie_cap == PCI_CAP_ID_EXP ?
655213ac73eSPeter Tyser 		"e" : "", pci_info->pci_num,
6560d3d68b2SPoonam Aggrwal 		hose->first_busno, hose->last_busno);
6570d3d68b2SPoonam Aggrwal 	return(hose->last_busno + 1);
6580d3d68b2SPoonam Aggrwal }
6590d3d68b2SPoonam Aggrwal 
6607a897959SPeter Tyser /* Enable inbound PCI config cycles for agent/endpoint interface */
6617a897959SPeter Tyser void fsl_pci_config_unlock(struct pci_controller *hose)
6627a897959SPeter Tyser {
6637a897959SPeter Tyser 	pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
664287df01eSZhao Qiang 	int pcie_cap_pos;
6657a897959SPeter Tyser 	u8 pcie_cap;
6667a897959SPeter Tyser 	u16 pbfr;
6677a897959SPeter Tyser 
668505f3e6fSMinghuan Lian 	if (!fsl_is_pci_agent(hose))
6697a897959SPeter Tyser 		return;
6707a897959SPeter Tyser 
671287df01eSZhao Qiang 	pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
672287df01eSZhao Qiang 	pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
6737a897959SPeter Tyser 	if (pcie_cap != 0x0) {
6747a897959SPeter Tyser 		/* PCIe - set CFG_READY bit of Configuration Ready Register */
6757a897959SPeter Tyser 		pci_hose_write_config_byte(hose, dev, FSL_PCIE_CFG_RDY, 0x1);
6767a897959SPeter Tyser 	} else {
6777a897959SPeter Tyser 		/* PCI - clear ACL bit of PBFR */
6787a897959SPeter Tyser 		pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
6797a897959SPeter Tyser 		pbfr &= ~0x20;
6807a897959SPeter Tyser 		pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
6817a897959SPeter Tyser 	}
6827a897959SPeter Tyser }
6837a897959SPeter Tyser 
684a4aafcc9SKumar Gala #if defined(CONFIG_PCIE1) || defined(CONFIG_PCIE2) || \
685a4aafcc9SKumar Gala     defined(CONFIG_PCIE3) || defined(CONFIG_PCIE4)
686a4aafcc9SKumar Gala int fsl_configure_pcie(struct fsl_pci_info *info,
687a4aafcc9SKumar Gala 			struct pci_controller *hose,
688a4aafcc9SKumar Gala 			const char *connected, int busno)
689a4aafcc9SKumar Gala {
690a4aafcc9SKumar Gala 	int is_endpoint;
691a4aafcc9SKumar Gala 
692a4aafcc9SKumar Gala 	set_next_law(info->mem_phys, law_size_bits(info->mem_size), info->law);
693a4aafcc9SKumar Gala 	set_next_law(info->io_phys, law_size_bits(info->io_size), info->law);
694213ac73eSPeter Tyser 
695a4aafcc9SKumar Gala 	is_endpoint = fsl_setup_hose(hose, info->regs);
696213ac73eSPeter Tyser 	printf("PCIe%u: %s", info->pci_num,
697213ac73eSPeter Tyser 		is_endpoint ? "Endpoint" : "Root Complex");
698213ac73eSPeter Tyser 	if (connected)
699213ac73eSPeter Tyser 		printf(" of %s", connected);
700213ac73eSPeter Tyser 	puts(", ");
701213ac73eSPeter Tyser 
702a4aafcc9SKumar Gala 	return fsl_pci_init_port(info, hose, busno);
703a4aafcc9SKumar Gala }
704a4aafcc9SKumar Gala 
705a4aafcc9SKumar Gala #if defined(CONFIG_FSL_CORENET)
7069e758758SYork Sun #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
7079e758758SYork Sun 	#define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR3_PCIE1
7089e758758SYork Sun 	#define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR3_PCIE2
7099e758758SYork Sun 	#define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR3_PCIE3
7109e758758SYork Sun 	#define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR3_PCIE4
7119e758758SYork Sun #else
712a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR_PCIE1
713a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR_PCIE2
714a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR_PCIE3
715a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR_PCIE4
7169e758758SYork Sun #endif
717a4aafcc9SKumar Gala 	#define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
718a4aafcc9SKumar Gala #elif defined(CONFIG_MPC85xx)
719a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE1 MPC85xx_DEVDISR_PCIE
720a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE2 MPC85xx_DEVDISR_PCIE2
721a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE3 MPC85xx_DEVDISR_PCIE3
722a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE4 0
723a4aafcc9SKumar Gala 	#define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
724a4aafcc9SKumar Gala #elif defined(CONFIG_MPC86xx)
725a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE1 MPC86xx_DEVDISR_PCIE1
726a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE2 MPC86xx_DEVDISR_PCIE2
727a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE3 0
728a4aafcc9SKumar Gala 	#define _DEVDISR_PCIE4 0
729a4aafcc9SKumar Gala 	#define CONFIG_SYS_MPC8xxx_GUTS_ADDR \
730a4aafcc9SKumar Gala 		(&((immap_t *)CONFIG_SYS_IMMR)->im_gur)
731a4aafcc9SKumar Gala #else
732a4aafcc9SKumar Gala #error "No defines for DEVDISR_PCIE"
733a4aafcc9SKumar Gala #endif
734a4aafcc9SKumar Gala 
735a4aafcc9SKumar Gala /* Implement a dummy function for those platforms w/o SERDES */
736a4aafcc9SKumar Gala static const char *__board_serdes_name(enum srds_prtcl device)
737a4aafcc9SKumar Gala {
738a4aafcc9SKumar Gala 	switch (device) {
739a4aafcc9SKumar Gala #ifdef CONFIG_SYS_PCIE1_NAME
740a4aafcc9SKumar Gala 	case PCIE1:
741a4aafcc9SKumar Gala 		return CONFIG_SYS_PCIE1_NAME;
742a4aafcc9SKumar Gala #endif
743a4aafcc9SKumar Gala #ifdef CONFIG_SYS_PCIE2_NAME
744a4aafcc9SKumar Gala 	case PCIE2:
745a4aafcc9SKumar Gala 		return CONFIG_SYS_PCIE2_NAME;
746a4aafcc9SKumar Gala #endif
747a4aafcc9SKumar Gala #ifdef CONFIG_SYS_PCIE3_NAME
748a4aafcc9SKumar Gala 	case PCIE3:
749a4aafcc9SKumar Gala 		return CONFIG_SYS_PCIE3_NAME;
750a4aafcc9SKumar Gala #endif
751a4aafcc9SKumar Gala #ifdef CONFIG_SYS_PCIE4_NAME
752a4aafcc9SKumar Gala 	case PCIE4:
753a4aafcc9SKumar Gala 		return CONFIG_SYS_PCIE4_NAME;
754a4aafcc9SKumar Gala #endif
755a4aafcc9SKumar Gala 	default:
756a4aafcc9SKumar Gala 		return NULL;
757a4aafcc9SKumar Gala 	}
758a4aafcc9SKumar Gala 
759a4aafcc9SKumar Gala 	return NULL;
760a4aafcc9SKumar Gala }
761a4aafcc9SKumar Gala 
762a4aafcc9SKumar Gala __attribute__((weak, alias("__board_serdes_name"))) const char *
763a4aafcc9SKumar Gala board_serdes_name(enum srds_prtcl device);
764a4aafcc9SKumar Gala 
765a4aafcc9SKumar Gala static u32 devdisr_mask[] = {
766a4aafcc9SKumar Gala 	_DEVDISR_PCIE1,
767a4aafcc9SKumar Gala 	_DEVDISR_PCIE2,
768a4aafcc9SKumar Gala 	_DEVDISR_PCIE3,
769a4aafcc9SKumar Gala 	_DEVDISR_PCIE4,
770a4aafcc9SKumar Gala };
771a4aafcc9SKumar Gala 
772a4aafcc9SKumar Gala int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
773a4aafcc9SKumar Gala 			struct fsl_pci_info *pci_info)
774a4aafcc9SKumar Gala {
775a4aafcc9SKumar Gala 	struct pci_controller *hose;
776a4aafcc9SKumar Gala 	int num = dev - PCIE1;
777a4aafcc9SKumar Gala 
778a4aafcc9SKumar Gala 	hose = calloc(1, sizeof(struct pci_controller));
779a4aafcc9SKumar Gala 	if (!hose)
780a4aafcc9SKumar Gala 		return busno;
781a4aafcc9SKumar Gala 
782a4aafcc9SKumar Gala 	if (is_serdes_configured(dev) && !(devdisr & devdisr_mask[num])) {
783a4aafcc9SKumar Gala 		busno = fsl_configure_pcie(pci_info, hose,
784a4aafcc9SKumar Gala 				board_serdes_name(dev), busno);
785a4aafcc9SKumar Gala 	} else {
786213ac73eSPeter Tyser 		printf("PCIe%d: disabled\n", num + 1);
787a4aafcc9SKumar Gala 	}
788a4aafcc9SKumar Gala 
789a4aafcc9SKumar Gala 	return busno;
790a4aafcc9SKumar Gala }
791a4aafcc9SKumar Gala 
792a4aafcc9SKumar Gala int fsl_pcie_init_board(int busno)
793a4aafcc9SKumar Gala {
794a4aafcc9SKumar Gala 	struct fsl_pci_info pci_info;
795a4aafcc9SKumar Gala 	ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC8xxx_GUTS_ADDR;
7969e758758SYork Sun 	u32 devdisr;
7979e758758SYork Sun 	u32 *addr;
7989e758758SYork Sun 
7999e758758SYork Sun #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
8009e758758SYork Sun 	addr = &gur->devdisr3;
8019e758758SYork Sun #else
8029e758758SYork Sun 	addr = &gur->devdisr;
8039e758758SYork Sun #endif
8049e758758SYork Sun 	devdisr = in_be32(addr);
805a4aafcc9SKumar Gala 
806a4aafcc9SKumar Gala #ifdef CONFIG_PCIE1
807a4aafcc9SKumar Gala 	SET_STD_PCIE_INFO(pci_info, 1);
808a4aafcc9SKumar Gala 	busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE1, &pci_info);
809a4aafcc9SKumar Gala #else
8109e758758SYork Sun 	setbits_be32(addr, _DEVDISR_PCIE1); /* disable */
811a4aafcc9SKumar Gala #endif
812a4aafcc9SKumar Gala 
813a4aafcc9SKumar Gala #ifdef CONFIG_PCIE2
814a4aafcc9SKumar Gala 	SET_STD_PCIE_INFO(pci_info, 2);
815a4aafcc9SKumar Gala 	busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE2, &pci_info);
816a4aafcc9SKumar Gala #else
8179e758758SYork Sun 	setbits_be32(addr, _DEVDISR_PCIE2); /* disable */
818a4aafcc9SKumar Gala #endif
819a4aafcc9SKumar Gala 
820a4aafcc9SKumar Gala #ifdef CONFIG_PCIE3
821a4aafcc9SKumar Gala 	SET_STD_PCIE_INFO(pci_info, 3);
822a4aafcc9SKumar Gala 	busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE3, &pci_info);
823a4aafcc9SKumar Gala #else
8249e758758SYork Sun 	setbits_be32(addr, _DEVDISR_PCIE3); /* disable */
825a4aafcc9SKumar Gala #endif
826a4aafcc9SKumar Gala 
827a4aafcc9SKumar Gala #ifdef CONFIG_PCIE4
828a4aafcc9SKumar Gala 	SET_STD_PCIE_INFO(pci_info, 4);
829a4aafcc9SKumar Gala 	busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE4, &pci_info);
830a4aafcc9SKumar Gala #else
8319e758758SYork Sun 	setbits_be32(addr, _DEVDISR_PCIE4); /* disable */
832a4aafcc9SKumar Gala #endif
833a4aafcc9SKumar Gala 
834a4aafcc9SKumar Gala  	return busno;
835a4aafcc9SKumar Gala }
836a4aafcc9SKumar Gala #else
837a4aafcc9SKumar Gala int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
838a4aafcc9SKumar Gala 			struct fsl_pci_info *pci_info)
839a4aafcc9SKumar Gala {
840a4aafcc9SKumar Gala 	return busno;
841a4aafcc9SKumar Gala }
842a4aafcc9SKumar Gala 
843a4aafcc9SKumar Gala int fsl_pcie_init_board(int busno)
844a4aafcc9SKumar Gala {
845a4aafcc9SKumar Gala 	return busno;
846a4aafcc9SKumar Gala }
847a4aafcc9SKumar Gala #endif
848a4aafcc9SKumar Gala 
849a2aab460SKumar Gala #ifdef CONFIG_OF_BOARD_SETUP
850a2aab460SKumar Gala #include <libfdt.h>
851a2aab460SKumar Gala #include <fdt_support.h>
852a2aab460SKumar Gala 
8536525d51fSKumar Gala void ft_fsl_pci_setup(void *blob, const char *pci_compat,
8543a0e3c27SKumar Gala 			unsigned long ctrl_addr)
855a2aab460SKumar Gala {
8566525d51fSKumar Gala 	int off;
857a2aab460SKumar Gala 	u32 bus_range[2];
8586525d51fSKumar Gala 	phys_addr_t p_ctrl_addr = (phys_addr_t)ctrl_addr;
8593a0e3c27SKumar Gala 	struct pci_controller *hose;
8603a0e3c27SKumar Gala 
8613a0e3c27SKumar Gala 	hose = find_hose_by_cfg_addr((void *)(ctrl_addr));
8626525d51fSKumar Gala 
8636525d51fSKumar Gala 	/* convert ctrl_addr to true physical address */
8646525d51fSKumar Gala 	p_ctrl_addr = (phys_addr_t)ctrl_addr - CONFIG_SYS_CCSRBAR;
8656525d51fSKumar Gala 	p_ctrl_addr += CONFIG_SYS_CCSRBAR_PHYS;
8666525d51fSKumar Gala 
8676525d51fSKumar Gala 	off = fdt_node_offset_by_compat_reg(blob, pci_compat, p_ctrl_addr);
868a2aab460SKumar Gala 
8695a85a309SKumar Gala 	if (off < 0)
8705a85a309SKumar Gala 		return;
8715a85a309SKumar Gala 
8725a85a309SKumar Gala 	/* We assume a cfg_addr not being set means we didn't setup the controller */
8735a85a309SKumar Gala 	if ((hose == NULL) || (hose->cfg_addr == NULL)) {
8746525d51fSKumar Gala 		fdt_del_node(blob, off);
8755a85a309SKumar Gala 	} else {
876a2aab460SKumar Gala 		bus_range[0] = 0;
877a2aab460SKumar Gala 		bus_range[1] = hose->last_busno - hose->first_busno;
878a2aab460SKumar Gala 		fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
879a2aab460SKumar Gala 		fdt_pci_dma_ranges(blob, off, hose);
880a2aab460SKumar Gala 	}
881a2aab460SKumar Gala }
882a2aab460SKumar Gala #endif
883