blob: c4eeb9b67cb90a9314f176590cb47696d263a0df [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ed Swarthout91080f72007-08-02 14:09:49 -05002/*
Minghuan Lianeb811d32012-08-21 23:35:42 +00003 * Copyright 2007-2012 Freescale Semiconductor, Inc.
Ed Swarthout91080f72007-08-02 14:09:49 -05004 */
Ed Swarthout15bc3e72007-07-27 01:50:45 -05005
Ed Swarthout91080f72007-08-02 14:09:49 -05006#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06007#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Kumar Gala4d4384e2010-12-15 14:21:41 -06009#include <malloc.h>
10#include <asm/fsl_serdes.h>
Ed Swarthout91080f72007-08-02 14:09:49 -050011
Kumar Gala47bf4782008-10-22 14:06:24 -050012DECLARE_GLOBAL_DATA_PTR;
13
Ed Swarthout91080f72007-08-02 14:09:49 -050014/*
15 * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
16 *
17 * Initialize controller and call the common driver/pci pci_hose_scan to
18 * scan for bridges and devices.
19 *
20 * Hose fields which need to be pre-initialized by board specific code:
21 * regions[]
22 * first_busno
23 *
24 * Fields updated:
25 * last_busno
26 */
27
28#include <pci.h>
Kumar Galaa37b9ce2009-08-05 07:59:35 -050029#include <asm/io.h>
Kumar Gala9bbd6432009-04-02 13:22:48 -050030#include <asm/fsl_pci.h>
Ed Swarthout91080f72007-08-02 14:09:49 -050031
Kumar Gala47bf4782008-10-22 14:06:24 -050032#ifndef CONFIG_SYS_PCI_MEMORY_BUS
33#define CONFIG_SYS_PCI_MEMORY_BUS 0
34#endif
35
36#ifndef CONFIG_SYS_PCI_MEMORY_PHYS
37#define CONFIG_SYS_PCI_MEMORY_PHYS 0
38#endif
39
40#if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
41#define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
42#endif
43
Kumar Galaa37b9ce2009-08-05 07:59:35 -050044/* Setup one inbound ATMU window.
45 *
46 * We let the caller decide what the window size should be
47 */
48static void set_inbound_window(volatile pit_t *pi,
49 struct pci_region *r,
50 u64 size)
Kumar Gala47bf4782008-10-22 14:06:24 -050051{
Kumar Galaa37b9ce2009-08-05 07:59:35 -050052 u32 sz = (__ilog2_u64(size) - 1);
Chunhe Lan7155ad52014-05-07 10:50:20 +080053#ifdef CONFIG_SYS_FSL_ERRATUM_A005434
54 u32 flag = 0;
55#else
56 u32 flag = PIWAR_LOCAL;
57#endif
58
59 flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
Kumar Galaa37b9ce2009-08-05 07:59:35 -050060
61 out_be32(&pi->pitar, r->phys_start >> 12);
62 out_be32(&pi->piwbar, r->bus_start >> 12);
63#ifdef CONFIG_SYS_PCI_64BIT
64 out_be32(&pi->piwbear, r->bus_start >> 44);
65#else
66 out_be32(&pi->piwbear, 0);
67#endif
68 if (r->flags & PCI_REGION_PREFETCH)
69 flag |= PIWAR_PF;
70 out_be32(&pi->piwar, flag | sz);
71}
72
Kumar Galaa6c612c2009-11-04 13:00:55 -060073int fsl_setup_hose(struct pci_controller *hose, unsigned long addr)
74{
75 volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) addr;
76
John Schmoller60e877f2010-10-22 00:20:23 -050077 /* Reset hose to make sure its in a clean state */
78 memset(hose, 0, sizeof(struct pci_controller));
79
Kumar Galaa6c612c2009-11-04 13:00:55 -060080 pci_setup_indirect(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
81
82 return fsl_is_pci_agent(hose);
83}
84
Kumar Galaa37b9ce2009-08-05 07:59:35 -050085static int fsl_pci_setup_inbound_windows(struct pci_controller *hose,
86 u64 out_lo, u8 pcie_cap,
87 volatile pit_t *pi)
88{
89 struct pci_region *r = hose->regions + hose->region_count;
90 u64 sz = min((u64)gd->ram_size, (1ull << 32));
Kumar Gala47bf4782008-10-22 14:06:24 -050091
92 phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
93 pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
Kumar Galaa37b9ce2009-08-05 07:59:35 -050094 pci_size_t pci_sz;
Kumar Gala47bf4782008-10-22 14:06:24 -050095
Kumar Galaa37b9ce2009-08-05 07:59:35 -050096 /* we have no space available for inbound memory mapping */
97 if (bus_start > out_lo) {
98 printf ("no space for inbound mapping of memory\n");
99 return 0;
100 }
Kumar Gala47bf4782008-10-22 14:06:24 -0500101
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500102 /* limit size */
103 if ((bus_start + sz) > out_lo) {
104 sz = out_lo - bus_start;
105 debug ("limiting size to %llx\n", sz);
106 }
Kumar Gala47bf4782008-10-22 14:06:24 -0500107
108 pci_sz = 1ull << __ilog2_u64(sz);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500109 /*
110 * we can overlap inbound/outbound windows on PCI-E since RX & TX
111 * links a separate
112 */
113 if ((pcie_cap == PCI_CAP_ID_EXP) && (pci_sz < sz)) {
114 debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
115 (u64)bus_start, (u64)phys_start, (u64)sz);
116 pci_set_region(r, bus_start, phys_start, sz,
117 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
118 PCI_REGION_PREFETCH);
119
120 /* if we aren't an exact power of two match, pci_sz is smaller
121 * round it up to the next power of two. We report the actual
122 * size to pci region tracking.
123 */
124 if (pci_sz != sz)
125 sz = 2ull << __ilog2_u64(sz);
126
127 set_inbound_window(pi--, r++, sz);
128 sz = 0; /* make sure we dont set the R2 window */
129 } else {
130 debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
Kumar Gala47bf4782008-10-22 14:06:24 -0500131 (u64)bus_start, (u64)phys_start, (u64)pci_sz);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500132 pci_set_region(r, bus_start, phys_start, pci_sz,
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600133 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
Kumar Gala47bf4782008-10-22 14:06:24 -0500134 PCI_REGION_PREFETCH);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500135 set_inbound_window(pi--, r++, pci_sz);
136
Kumar Gala47bf4782008-10-22 14:06:24 -0500137 sz -= pci_sz;
138 bus_start += pci_sz;
139 phys_start += pci_sz;
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500140
141 pci_sz = 1ull << __ilog2_u64(sz);
142 if (sz) {
143 debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
144 (u64)bus_start, (u64)phys_start, (u64)pci_sz);
145 pci_set_region(r, bus_start, phys_start, pci_sz,
146 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
147 PCI_REGION_PREFETCH);
148 set_inbound_window(pi--, r++, pci_sz);
149 sz -= pci_sz;
150 bus_start += pci_sz;
151 phys_start += pci_sz;
152 }
Kumar Gala47bf4782008-10-22 14:06:24 -0500153 }
154
155#if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
Becky Bruce26176472008-10-27 16:09:42 -0500156 /*
157 * On 64-bit capable systems, set up a mapping for all of DRAM
158 * in high pci address space.
159 */
Kumar Gala47bf4782008-10-22 14:06:24 -0500160 pci_sz = 1ull << __ilog2_u64(gd->ram_size);
161 /* round up to the next largest power of two */
162 if (gd->ram_size > pci_sz)
Becky Bruce26176472008-10-27 16:09:42 -0500163 pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
Kumar Gala47bf4782008-10-22 14:06:24 -0500164 debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
Becky Bruce26176472008-10-27 16:09:42 -0500165 (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
Kumar Gala47bf4782008-10-22 14:06:24 -0500166 (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
167 (u64)pci_sz);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500168 pci_set_region(r,
Becky Bruce26176472008-10-27 16:09:42 -0500169 CONFIG_SYS_PCI64_MEMORY_BUS,
Kumar Gala47bf4782008-10-22 14:06:24 -0500170 CONFIG_SYS_PCI_MEMORY_PHYS,
171 pci_sz,
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600172 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
Kumar Gala47bf4782008-10-22 14:06:24 -0500173 PCI_REGION_PREFETCH);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500174 set_inbound_window(pi--, r++, pci_sz);
Kumar Gala47bf4782008-10-22 14:06:24 -0500175#else
176 pci_sz = 1ull << __ilog2_u64(sz);
177 if (sz) {
178 debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
179 (u64)bus_start, (u64)phys_start, (u64)pci_sz);
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500180 pci_set_region(r, bus_start, phys_start, pci_sz,
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600181 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
Kumar Gala47bf4782008-10-22 14:06:24 -0500182 PCI_REGION_PREFETCH);
183 sz -= pci_sz;
184 bus_start += pci_sz;
185 phys_start += pci_sz;
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500186 set_inbound_window(pi--, r++, pci_sz);
Kumar Gala47bf4782008-10-22 14:06:24 -0500187 }
188#endif
189
Kumar Gala4e8001f2008-12-09 10:27:33 -0600190#ifdef CONFIG_PHYS_64BIT
Kumar Gala47bf4782008-10-22 14:06:24 -0500191 if (sz && (((u64)gd->ram_size) < (1ull << 32)))
192 printf("Was not able to map all of memory via "
193 "inbound windows -- %lld remaining\n", sz);
Kumar Gala4e8001f2008-12-09 10:27:33 -0600194#endif
Kumar Gala47bf4782008-10-22 14:06:24 -0500195
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500196 hose->region_count = r - hose->regions;
197
198 return 1;
Kumar Gala47bf4782008-10-22 14:06:24 -0500199}
200
Liu Gang27afb9c2013-05-07 16:30:46 +0800201#ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
Liu Gang99e0c292012-08-09 05:10:02 +0000202static void fsl_pcie_boot_master(pit_t *pi)
203{
204 /* configure inbound window for slave's u-boot image */
205 debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
206 "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
207 (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
208 (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
209 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
210 struct pci_region r_inbound;
211 u32 sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE)
212 - 1;
213 pci_set_region(&r_inbound,
214 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
215 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
216 sz_inbound,
217 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
218
219 set_inbound_window(pi--, &r_inbound,
220 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
221
222 /* configure inbound window for slave's u-boot image */
223 debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
224 "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
225 (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
226 (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
227 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
228 pci_set_region(&r_inbound,
229 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
230 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
231 sz_inbound,
232 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
233
234 set_inbound_window(pi--, &r_inbound,
235 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
236
237 /* configure inbound window for slave's ucode and ENV */
238 debug("PCIEBOOT - MASTER: Inbound window for slave's "
239 "ucode and ENV; "
240 "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
241 (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
242 (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
243 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
244 sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE)
245 - 1;
246 pci_set_region(&r_inbound,
247 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
248 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
249 sz_inbound,
250 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
251
252 set_inbound_window(pi--, &r_inbound,
253 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
254}
255
256static void fsl_pcie_boot_master_release_slave(int port)
257{
258 unsigned long release_addr;
259
260 /* now release slave's core 0 */
261 switch (port) {
262 case 1:
263 release_addr = CONFIG_SYS_PCIE1_MEM_VIRT
264 + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
265 break;
York Sun024498f2012-10-08 07:44:04 +0000266#ifdef CONFIG_SYS_PCIE2_MEM_VIRT
Liu Gang99e0c292012-08-09 05:10:02 +0000267 case 2:
268 release_addr = CONFIG_SYS_PCIE2_MEM_VIRT
269 + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
270 break;
York Sun024498f2012-10-08 07:44:04 +0000271#endif
272#ifdef CONFIG_SYS_PCIE3_MEM_VIRT
Liu Gang99e0c292012-08-09 05:10:02 +0000273 case 3:
274 release_addr = CONFIG_SYS_PCIE3_MEM_VIRT
275 + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
276 break;
York Sun024498f2012-10-08 07:44:04 +0000277#endif
Liu Gang99e0c292012-08-09 05:10:02 +0000278 default:
279 release_addr = 0;
280 break;
281 }
282 if (release_addr != 0) {
283 out_be32((void *)release_addr,
284 CONFIG_SRIO_PCIE_BOOT_RELEASE_MASK);
285 debug("PCIEBOOT - MASTER: "
286 "Release slave successfully! Now the slave should start up!\n");
287 } else {
288 debug("PCIEBOOT - MASTER: "
289 "Release slave failed!\n");
290 }
291}
292#endif
293
Peter Tyser3771ba32010-12-28 17:47:25 -0600294void fsl_pci_init(struct pci_controller *hose, struct fsl_pci_info *pci_info)
Ed Swarthout91080f72007-08-02 14:09:49 -0500295{
Peter Tyser3771ba32010-12-28 17:47:25 -0600296 u32 cfg_addr = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_addr;
297 u32 cfg_data = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_data;
Ed Swarthout91080f72007-08-02 14:09:49 -0500298 u16 temp16;
299 u32 temp32;
Prabhakar Kushwahab582dae2011-02-04 09:00:43 +0530300 u32 block_rev;
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500301 int enabled, r, inbound = 0;
Ed Swarthout91080f72007-08-02 14:09:49 -0500302 u16 ltssm;
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500303 u8 temp8, pcie_cap;
Zhao Qiang5d39f742013-10-12 13:46:33 +0800304 int pcie_cap_pos;
305 int pci_dcr;
306 int pci_dsr;
307 int pci_lsr;
308
309#if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
310 int pci_lcr;
311#endif
312
Kumar Gala65e198d2009-08-03 20:44:55 -0500313 volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)cfg_addr;
Kumar Galae770f352009-08-03 21:02:02 -0500314 struct pci_region *reg = hose->regions + hose->region_count;
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500315 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
Ed Swarthout91080f72007-08-02 14:09:49 -0500316
317 /* Initialize ATMU registers based on hose regions and flags */
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200318 volatile pot_t *po = &pci->pot[1]; /* skip 0 */
Prabhakar Kushwahab582dae2011-02-04 09:00:43 +0530319 volatile pit_t *pi;
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500320
321 u64 out_hi = 0, out_lo = -1ULL;
322 u32 pcicsrbar, pcicsrbar_sz;
Ed Swarthout91080f72007-08-02 14:09:49 -0500323
Kumar Gala65e198d2009-08-03 20:44:55 -0500324 pci_setup_indirect(hose, cfg_addr, cfg_data);
325
Joakim Tjernlundbc42fde2017-09-12 19:56:41 +0200326#ifdef PEX_CCB_DIV
327 /* Configure the PCIE controller core clock ratio */
328 pci_hose_write_config_dword(hose, dev, 0x440,
329 ((gd->bus_clk / 1000000) *
330 (16 / PEX_CCB_DIV)) / 333);
331#endif
Prabhakar Kushwahab582dae2011-02-04 09:00:43 +0530332 block_rev = in_be32(&pci->block_rev1);
333 if (PEX_IP_BLK_REV_2_2 <= block_rev) {
334 pi = &pci->pit[2]; /* 0xDC0 */
335 } else {
336 pi = &pci->pit[3]; /* 0xDE0 */
337 }
338
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500339 /* Handle setup of outbound windows first */
340 for (r = 0; r < hose->region_count; r++) {
341 unsigned long flags = hose->regions[r].flags;
342 u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
Kumar Galae770f352009-08-03 21:02:02 -0500343
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500344 flags &= PCI_REGION_SYS_MEMORY|PCI_REGION_TYPE;
345 if (flags != PCI_REGION_SYS_MEMORY) {
346 u64 start = hose->regions[r].bus_start;
347 u64 end = start + hose->regions[r].size;
Kumar Galae770f352009-08-03 21:02:02 -0500348
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500349 out_be32(&po->powbar, hose->regions[r].phys_start >> 12);
350 out_be32(&po->potar, start >> 12);
Kumar Gala87006ca2008-10-21 10:13:14 -0500351#ifdef CONFIG_SYS_PCI_64BIT
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500352 out_be32(&po->potear, start >> 44);
Kumar Gala87006ca2008-10-21 10:13:14 -0500353#else
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500354 out_be32(&po->potear, 0);
Kumar Gala87006ca2008-10-21 10:13:14 -0500355#endif
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500356 if (hose->regions[r].flags & PCI_REGION_IO) {
357 out_be32(&po->powar, POWAR_EN | sz |
358 POWAR_IO_READ | POWAR_IO_WRITE);
359 } else {
360 out_be32(&po->powar, POWAR_EN | sz |
361 POWAR_MEM_READ | POWAR_MEM_WRITE);
362 out_lo = min(start, out_lo);
363 out_hi = max(end, out_hi);
364 }
Ed Swarthout91080f72007-08-02 14:09:49 -0500365 po++;
366 }
367 }
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500368 debug("Outbound memory range: %llx:%llx\n", out_lo, out_hi);
369
370 /* setup PCSRBAR/PEXCSRBAR */
371 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
372 pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
373 pcicsrbar_sz = ~pcicsrbar_sz + 1;
374
375 if (out_hi < (0x100000000ull - pcicsrbar_sz) ||
376 (out_lo > 0x100000000ull))
377 pcicsrbar = 0x100000000ull - pcicsrbar_sz;
378 else
379 pcicsrbar = (out_lo - pcicsrbar_sz) & -pcicsrbar_sz;
380 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, pcicsrbar);
381
382 out_lo = min(out_lo, (u64)pcicsrbar);
383
384 debug("PCICSRBAR @ 0x%x\n", pcicsrbar);
385
386 pci_set_region(reg++, pcicsrbar, CONFIG_SYS_CCSRBAR_PHYS,
387 pcicsrbar_sz, PCI_REGION_SYS_MEMORY);
388 hose->region_count++;
Ed Swarthout91080f72007-08-02 14:09:49 -0500389
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500390 /* see if we are a PCIe or PCI controller */
Zhao Qiang5d39f742013-10-12 13:46:33 +0800391 pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
392 pci_dcr = pcie_cap_pos + 0x08;
393 pci_dsr = pcie_cap_pos + 0x0a;
394 pci_lsr = pcie_cap_pos + 0x12;
395
396 pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500397
Liu Gang27afb9c2013-05-07 16:30:46 +0800398#ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
Liu Gang99e0c292012-08-09 05:10:02 +0000399 /* boot from PCIE --master */
Simon Glass64b723f2017-08-03 12:22:12 -0600400 char *s = env_get("bootmaster");
Liu Gang99e0c292012-08-09 05:10:02 +0000401 char pcie[6];
402 sprintf(pcie, "PCIE%d", pci_info->pci_num);
403
404 if (s && (strcmp(s, pcie) == 0)) {
405 debug("PCIEBOOT - MASTER: Master port [ %d ] for pcie boot.\n",
406 pci_info->pci_num);
407 fsl_pcie_boot_master((pit_t *)pi);
408 } else {
409 /* inbound */
410 inbound = fsl_pci_setup_inbound_windows(hose,
411 out_lo, pcie_cap, pi);
412 }
413#else
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500414 /* inbound */
415 inbound = fsl_pci_setup_inbound_windows(hose, out_lo, pcie_cap, pi);
Liu Gang99e0c292012-08-09 05:10:02 +0000416#endif
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500417
418 for (r = 0; r < hose->region_count; r++)
Marek Vasut2e662ee2011-10-21 14:17:21 +0000419 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n", r,
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500420 (u64)hose->regions[r].phys_start,
Marek Vasut2e662ee2011-10-21 14:17:21 +0000421 (u64)hose->regions[r].bus_start,
422 (u64)hose->regions[r].size,
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500423 hose->regions[r].flags);
424
Ed Swarthout91080f72007-08-02 14:09:49 -0500425 pci_register_hose(hose);
426 pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
427 hose->current_busno = hose->first_busno;
428
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500429 out_be32(&pci->pedr, 0xffffffff); /* Clear any errors */
Mike Williamsbf895ad2011-07-22 04:01:30 +0000430 out_be32(&pci->peer, ~0x20140); /* Enable All Error Interrupts except
Ed Swarthout15bc3e72007-07-27 01:50:45 -0500431 * - Master abort (pci)
432 * - Master PERR (pci)
433 * - ICCA (PCIe)
434 */
Zhao Qiang5d39f742013-10-12 13:46:33 +0800435 pci_hose_read_config_dword(hose, dev, pci_dcr, &temp32);
Ed Swarthout91080f72007-08-02 14:09:49 -0500436 temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
Zhao Qiang5d39f742013-10-12 13:46:33 +0800437 pci_hose_write_config_dword(hose, dev, pci_dcr, temp32);
Ed Swarthout91080f72007-08-02 14:09:49 -0500438
Prabhakar Kushwaha1c48e772011-02-01 15:55:58 +0000439#if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
Zhao Qiang5d39f742013-10-12 13:46:33 +0800440 pci_lcr = pcie_cap_pos + 0x10;
Prabhakar Kushwaha1c48e772011-02-01 15:55:58 +0000441 temp32 = 0;
Zhao Qiang5d39f742013-10-12 13:46:33 +0800442 pci_hose_read_config_dword(hose, dev, pci_lcr, &temp32);
Prabhakar Kushwaha1c48e772011-02-01 15:55:58 +0000443 temp32 &= ~0x03; /* Disable ASPM */
Zhao Qiang5d39f742013-10-12 13:46:33 +0800444 pci_hose_write_config_dword(hose, dev, pci_lcr, temp32);
Prabhakar Kushwaha1c48e772011-02-01 15:55:58 +0000445 udelay(1);
446#endif
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500447 if (pcie_cap == PCI_CAP_ID_EXP) {
Zang Roy-R6191169982822013-07-04 07:25:03 +0800448 if (block_rev >= PEX_IP_BLK_REV_3_0) {
449#define PEX_CSR0_LTSSM_MASK 0xFC
450#define PEX_CSR0_LTSSM_SHIFT 2
451 ltssm = (in_be32(&pci->pex_csr0)
452 & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
453 enabled = (ltssm == 0x11) ? 1 : 0;
Zhao Qiangf36e0ba2015-03-26 16:13:09 +0800454#ifdef CONFIG_FSL_PCIE_RESET
455 int i;
456 /* assert PCIe reset */
457 setbits_be32(&pci->pdb_stat, 0x08000000);
458 (void) in_be32(&pci->pdb_stat);
459 udelay(1000);
460 /* clear PCIe reset */
461 clrbits_be32(&pci->pdb_stat, 0x08000000);
462 asm("sync;isync");
463 for (i = 0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
464 pci_hose_read_config_word(hose, dev, PCI_LTSSM,
465 &ltssm);
466 udelay(1000);
467 }
468#endif
Zang Roy-R6191169982822013-07-04 07:25:03 +0800469 } else {
470 /* pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm); */
471 /* enabled = ltssm >= PCI_LTSSM_L0; */
Ed Swarthout91080f72007-08-02 14:09:49 -0500472 pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
473 enabled = ltssm >= PCI_LTSSM_L0;
474
Kumar Gala93166d22007-12-07 12:17:34 -0600475#ifdef CONFIG_FSL_PCIE_RESET
476 if (ltssm == 1) {
477 int i;
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500478 debug("....PCIe link error. " "LTSSM=0x%02x.", ltssm);
479 /* assert PCIe reset */
480 setbits_be32(&pci->pdb_stat, 0x08000000);
481 (void) in_be32(&pci->pdb_stat);
Kumar Gala93166d22007-12-07 12:17:34 -0600482 udelay(100);
Marek Vasut2e662ee2011-10-21 14:17:21 +0000483 debug(" Asserting PCIe reset @%p = %x\n",
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500484 &pci->pdb_stat, in_be32(&pci->pdb_stat));
485 /* clear PCIe reset */
486 clrbits_be32(&pci->pdb_stat, 0x08000000);
Kumar Gala93166d22007-12-07 12:17:34 -0600487 asm("sync;isync");
488 for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
489 pci_hose_read_config_word(hose, dev, PCI_LTSSM,
490 &ltssm);
491 udelay(1000);
492 debug("....PCIe link error. "
493 "LTSSM=0x%02x.\n", ltssm);
494 }
495 enabled = ltssm >= PCI_LTSSM_L0;
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500496
497 /* we need to re-write the bar0 since a reset will
498 * clear it
499 */
500 pci_hose_write_config_dword(hose, dev,
501 PCI_BASE_ADDRESS_0, pcicsrbar);
Kumar Gala93166d22007-12-07 12:17:34 -0600502 }
503#endif
Zang Roy-R6191169982822013-07-04 07:25:03 +0800504 }
Kumar Gala93166d22007-12-07 12:17:34 -0600505
Yuanquan Chenc48234e2012-11-26 23:49:45 +0000506#ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
507 if (enabled == 0) {
508 serdes_corenet_t *srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
509 temp32 = in_be32(&srds_regs->srdspccr0);
510
511 if ((temp32 >> 28) == 3) {
512 int i;
513
514 out_be32(&srds_regs->srdspccr0, 2 << 28);
515 setbits_be32(&pci->pdb_stat, 0x08000000);
516 in_be32(&pci->pdb_stat);
517 udelay(100);
518 clrbits_be32(&pci->pdb_stat, 0x08000000);
519 asm("sync;isync");
520 for (i=0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
521 pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
522 udelay(1000);
523 }
524 enabled = ltssm >= PCI_LTSSM_L0;
525 }
526 }
527#endif
Ed Swarthout91080f72007-08-02 14:09:49 -0500528 if (!enabled) {
Zang Roy-R61911cd940612014-06-12 14:49:23 -0500529 /* Let the user know there's no PCIe link for root
530 * complex. for endpoint, the link may not setup, so
531 * print undetermined.
532 */
533 if (fsl_is_pci_agent(hose))
534 printf("undetermined, regs @ 0x%lx\n", pci_info->regs);
535 else
536 printf("no link, regs @ 0x%lx\n", pci_info->regs);
Ed Swarthout91080f72007-08-02 14:09:49 -0500537 hose->last_busno = hose->first_busno;
538 return;
539 }
540
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500541 out_be32(&pci->pme_msg_det, 0xffffffff);
542 out_be32(&pci->pme_msg_int_en, 0xffffffff);
Peter Tyser3771ba32010-12-28 17:47:25 -0600543
544 /* Print the negotiated PCIe link width */
Zhao Qiang5d39f742013-10-12 13:46:33 +0800545 pci_hose_read_config_word(hose, dev, pci_lsr, &temp16);
Prabhakar Kushwaha5e5b6ee2014-01-25 12:53:32 +0530546 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
547 (temp16 & 0xf), pci_info->regs);
Peter Tyser3771ba32010-12-28 17:47:25 -0600548
Ed Swarthout91080f72007-08-02 14:09:49 -0500549 hose->current_busno++; /* Start scan with secondary */
550 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
Ed Swarthout91080f72007-08-02 14:09:49 -0500551 }
552
Tony O'Brien8acb1272016-12-02 09:22:34 +1300553#ifdef CONFIG_SYS_FSL_ERRATUM_A007815
554 /* The Read-Only Write Enable bit defaults to 1 instead of 0.
555 * Set to 0 to protect the read-only registers.
556 */
557 clrbits_be32(&pci->dbi_ro_wr_en, 0x01);
558#endif
559
Ed Swarthout1ab6def2007-08-20 23:55:33 -0500560 /* Use generic setup_device to initialize standard pci regs,
561 * but do not allocate any windows since any BAR found (such
562 * as PCSRBAR) is not in this cpu's memory space.
563 */
Ed Swarthout1ab6def2007-08-20 23:55:33 -0500564 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
Ed Swarthout91080f72007-08-02 14:09:49 -0500565 hose->pci_prefetch, hose->pci_io);
Ed Swarthout1ab6def2007-08-20 23:55:33 -0500566
Ed Swarthoutd6e526c2007-10-19 17:51:40 -0500567 if (inbound) {
568 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
569 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
570 temp16 | PCI_COMMAND_MEMORY);
571 }
572
Ed Swarthout15bc3e72007-07-27 01:50:45 -0500573#ifndef CONFIG_PCI_NOSCAN
Minghuan Lianeb811d32012-08-21 23:35:42 +0000574 if (!fsl_is_pci_agent(hose)) {
Peter Tyser826fd9d2010-10-29 17:59:26 -0500575 debug(" Scanning PCI bus %02x\n",
Ed Swarthout3c13d702008-10-08 23:38:00 -0500576 hose->current_busno);
577 hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
578 } else {
Peter Tyser2b91f712010-10-29 17:59:24 -0500579 debug(" Not scanning PCI bus %02x. PI=%x\n",
Ed Swarthout3c13d702008-10-08 23:38:00 -0500580 hose->current_busno, temp8);
581 hose->last_busno = hose->current_busno;
582 }
Ed Swarthout91080f72007-08-02 14:09:49 -0500583
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500584 /* if we are PCIe - update limit regs and subordinate busno
585 * for the virtual P2P bridge
586 */
587 if (pcie_cap == PCI_CAP_ID_EXP) {
Ed Swarthout91080f72007-08-02 14:09:49 -0500588 pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
589 }
Ed Swarthout15bc3e72007-07-27 01:50:45 -0500590#else
591 hose->last_busno = hose->current_busno;
592#endif
Ed Swarthout91080f72007-08-02 14:09:49 -0500593
594 /* Clear all error indications */
Kumar Galaa0a5dbd2009-08-05 07:49:27 -0500595 if (pcie_cap == PCI_CAP_ID_EXP)
Kumar Galaa37b9ce2009-08-05 07:59:35 -0500596 out_be32(&pci->pme_msg_det, 0xffffffff);
597 out_be32(&pci->pedr, 0xffffffff);
Ed Swarthout91080f72007-08-02 14:09:49 -0500598
Zhao Qiang5d39f742013-10-12 13:46:33 +0800599 pci_hose_read_config_word(hose, dev, pci_dsr, &temp16);
Ed Swarthout91080f72007-08-02 14:09:49 -0500600 if (temp16) {
Zhao Qiang5d39f742013-10-12 13:46:33 +0800601 pci_hose_write_config_word(hose, dev, pci_dsr, 0xffff);
Ed Swarthout91080f72007-08-02 14:09:49 -0500602 }
603
604 pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
605 if (temp16) {
Ed Swarthout91080f72007-08-02 14:09:49 -0500606 pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
607 }
608}
Kumar Galafe29f1f2008-10-23 00:01:06 -0500609
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600610int fsl_is_pci_agent(struct pci_controller *hose)
611{
Zhao Qiang5d39f742013-10-12 13:46:33 +0800612 int pcie_cap_pos;
Minghuan Lianeb811d32012-08-21 23:35:42 +0000613 u8 pcie_cap;
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600614 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
615
Zhao Qiang5d39f742013-10-12 13:46:33 +0800616 pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
617 pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
Minghuan Lianeb811d32012-08-21 23:35:42 +0000618 if (pcie_cap == PCI_CAP_ID_EXP) {
619 u8 header_type;
620
621 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE,
622 &header_type);
623 return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
624 } else {
625 u8 prog_if;
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600626
Minghuan Lianeb811d32012-08-21 23:35:42 +0000627 pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prog_if);
Zang Roy-R6191169982822013-07-04 07:25:03 +0800628 /* Programming Interface (PCI_CLASS_PROG)
629 * 0 == pci host or pcie root-complex,
630 * 1 == pci agent or pcie end-point
631 */
Minghuan Lianeb811d32012-08-21 23:35:42 +0000632 return (prog_if == FSL_PROG_IF_AGENT);
633 }
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600634}
635
Poonam Aggrwal1c796172009-08-21 07:29:42 +0530636int fsl_pci_init_port(struct fsl_pci_info *pci_info,
Kumar Galab83ff072009-11-04 01:29:04 -0600637 struct pci_controller *hose, int busno)
Poonam Aggrwal1c796172009-08-21 07:29:42 +0530638{
639 volatile ccsr_fsl_pci_t *pci;
640 struct pci_region *r;
Peter Tyser149dcbc2010-10-28 15:24:59 -0500641 pci_dev_t dev = PCI_BDF(busno,0,0);
Zhao Qiang5d39f742013-10-12 13:46:33 +0800642 int pcie_cap_pos;
Peter Tyser149dcbc2010-10-28 15:24:59 -0500643 u8 pcie_cap;
Poonam Aggrwal1c796172009-08-21 07:29:42 +0530644
645 pci = (ccsr_fsl_pci_t *) pci_info->regs;
646
647 /* on non-PCIe controllers we don't have pme_msg_det so this code
648 * should do nothing since the read will return 0
649 */
650 if (in_be32(&pci->pme_msg_det)) {
651 out_be32(&pci->pme_msg_det, 0xffffffff);
652 debug (" with errors. Clearing. Now 0x%08x",
653 pci->pme_msg_det);
654 }
655
656 r = hose->regions + hose->region_count;
657
658 /* outbound memory */
659 pci_set_region(r++,
660 pci_info->mem_bus,
661 pci_info->mem_phys,
662 pci_info->mem_size,
663 PCI_REGION_MEM);
664
665 /* outbound io */
666 pci_set_region(r++,
667 pci_info->io_bus,
668 pci_info->io_phys,
669 pci_info->io_size,
670 PCI_REGION_IO);
671
672 hose->region_count = r - hose->regions;
673 hose->first_busno = busno;
674
Peter Tyser3771ba32010-12-28 17:47:25 -0600675 fsl_pci_init(hose, pci_info);
Poonam Aggrwal1c796172009-08-21 07:29:42 +0530676
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600677 if (fsl_is_pci_agent(hose)) {
678 fsl_pci_config_unlock(hose);
679 hose->last_busno = hose->first_busno;
Liu Gang27afb9c2013-05-07 16:30:46 +0800680#ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
Liu Gang99e0c292012-08-09 05:10:02 +0000681 } else {
682 /* boot from PCIE --master releases slave's core 0 */
Simon Glass64b723f2017-08-03 12:22:12 -0600683 char *s = env_get("bootmaster");
Liu Gang99e0c292012-08-09 05:10:02 +0000684 char pcie[6];
685 sprintf(pcie, "PCIE%d", pci_info->pci_num);
686
687 if (s && (strcmp(s, pcie) == 0))
688 fsl_pcie_boot_master_release_slave(pci_info->pci_num);
689#endif
Ed Swarthout4451a6d2009-11-02 09:05:49 -0600690 }
691
Zhao Qiang5d39f742013-10-12 13:46:33 +0800692 pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
693 pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
Peter Tyser2b91f712010-10-29 17:59:24 -0500694 printf("PCI%s%x: Bus %02x - %02x\n", pcie_cap == PCI_CAP_ID_EXP ?
Peter Tyser3771ba32010-12-28 17:47:25 -0600695 "e" : "", pci_info->pci_num,
Peter Tyser2b91f712010-10-29 17:59:24 -0500696 hose->first_busno, hose->last_busno);
Poonam Aggrwal1c796172009-08-21 07:29:42 +0530697 return(hose->last_busno + 1);
698}
699
Peter Tyserbc98e542008-10-29 12:39:26 -0500700/* Enable inbound PCI config cycles for agent/endpoint interface */
701void fsl_pci_config_unlock(struct pci_controller *hose)
702{
703 pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
Zhao Qiang5d39f742013-10-12 13:46:33 +0800704 int pcie_cap_pos;
Peter Tyserbc98e542008-10-29 12:39:26 -0500705 u8 pcie_cap;
706 u16 pbfr;
707
Minghuan Lianeb811d32012-08-21 23:35:42 +0000708 if (!fsl_is_pci_agent(hose))
Peter Tyserbc98e542008-10-29 12:39:26 -0500709 return;
710
Zhao Qiang5d39f742013-10-12 13:46:33 +0800711 pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
712 pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
Peter Tyserbc98e542008-10-29 12:39:26 -0500713 if (pcie_cap != 0x0) {
Minghuan Lian143adc92015-03-27 13:24:39 +0800714 ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)hose->cfg_addr;
715 u32 block_rev = in_be32(&pci->block_rev1);
Peter Tyserbc98e542008-10-29 12:39:26 -0500716 /* PCIe - set CFG_READY bit of Configuration Ready Register */
Minghuan Lian143adc92015-03-27 13:24:39 +0800717 if (block_rev >= PEX_IP_BLK_REV_3_0)
718 setbits_be32(&pci->config, FSL_PCIE_V3_CFG_RDY);
719 else
720 pci_hose_write_config_byte(hose, dev,
721 FSL_PCIE_CFG_RDY, 0x1);
Peter Tyserbc98e542008-10-29 12:39:26 -0500722 } else {
723 /* PCI - clear ACL bit of PBFR */
724 pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
725 pbfr &= ~0x20;
726 pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
727 }
728}
729
Kumar Gala4d4384e2010-12-15 14:21:41 -0600730#if defined(CONFIG_PCIE1) || defined(CONFIG_PCIE2) || \
Wolfgang Denka4de8352011-02-02 22:36:10 +0100731 defined(CONFIG_PCIE3) || defined(CONFIG_PCIE4)
Kumar Gala4d4384e2010-12-15 14:21:41 -0600732int fsl_configure_pcie(struct fsl_pci_info *info,
733 struct pci_controller *hose,
734 const char *connected, int busno)
735{
736 int is_endpoint;
737
738 set_next_law(info->mem_phys, law_size_bits(info->mem_size), info->law);
739 set_next_law(info->io_phys, law_size_bits(info->io_size), info->law);
Peter Tyser3771ba32010-12-28 17:47:25 -0600740
Kumar Gala4d4384e2010-12-15 14:21:41 -0600741 is_endpoint = fsl_setup_hose(hose, info->regs);
Peter Tyser3771ba32010-12-28 17:47:25 -0600742 printf("PCIe%u: %s", info->pci_num,
743 is_endpoint ? "Endpoint" : "Root Complex");
744 if (connected)
745 printf(" of %s", connected);
746 puts(", ");
747
Kumar Gala4d4384e2010-12-15 14:21:41 -0600748 return fsl_pci_init_port(info, hose, busno);
749}
750
751#if defined(CONFIG_FSL_CORENET)
York Sun9941a222012-10-08 07:44:19 +0000752#ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
753 #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR3_PCIE1
754 #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR3_PCIE2
755 #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR3_PCIE3
756 #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR3_PCIE4
757#else
Kumar Gala4d4384e2010-12-15 14:21:41 -0600758 #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR_PCIE1
759 #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR_PCIE2
760 #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR_PCIE3
761 #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR_PCIE4
York Sun9941a222012-10-08 07:44:19 +0000762#endif
Kumar Gala4d4384e2010-12-15 14:21:41 -0600763 #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
764#elif defined(CONFIG_MPC85xx)
765 #define _DEVDISR_PCIE1 MPC85xx_DEVDISR_PCIE
766 #define _DEVDISR_PCIE2 MPC85xx_DEVDISR_PCIE2
767 #define _DEVDISR_PCIE3 MPC85xx_DEVDISR_PCIE3
768 #define _DEVDISR_PCIE4 0
769 #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
770#elif defined(CONFIG_MPC86xx)
771 #define _DEVDISR_PCIE1 MPC86xx_DEVDISR_PCIE1
772 #define _DEVDISR_PCIE2 MPC86xx_DEVDISR_PCIE2
773 #define _DEVDISR_PCIE3 0
774 #define _DEVDISR_PCIE4 0
775 #define CONFIG_SYS_MPC8xxx_GUTS_ADDR \
776 (&((immap_t *)CONFIG_SYS_IMMR)->im_gur)
777#else
778#error "No defines for DEVDISR_PCIE"
779#endif
780
781/* Implement a dummy function for those platforms w/o SERDES */
782static const char *__board_serdes_name(enum srds_prtcl device)
783{
784 switch (device) {
785#ifdef CONFIG_SYS_PCIE1_NAME
786 case PCIE1:
787 return CONFIG_SYS_PCIE1_NAME;
788#endif
789#ifdef CONFIG_SYS_PCIE2_NAME
790 case PCIE2:
791 return CONFIG_SYS_PCIE2_NAME;
792#endif
793#ifdef CONFIG_SYS_PCIE3_NAME
794 case PCIE3:
795 return CONFIG_SYS_PCIE3_NAME;
796#endif
797#ifdef CONFIG_SYS_PCIE4_NAME
798 case PCIE4:
799 return CONFIG_SYS_PCIE4_NAME;
800#endif
801 default:
802 return NULL;
803 }
804
805 return NULL;
806}
807
808__attribute__((weak, alias("__board_serdes_name"))) const char *
809board_serdes_name(enum srds_prtcl device);
810
811static u32 devdisr_mask[] = {
812 _DEVDISR_PCIE1,
813 _DEVDISR_PCIE2,
814 _DEVDISR_PCIE3,
815 _DEVDISR_PCIE4,
816};
817
818int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
819 struct fsl_pci_info *pci_info)
820{
821 struct pci_controller *hose;
822 int num = dev - PCIE1;
823
824 hose = calloc(1, sizeof(struct pci_controller));
825 if (!hose)
826 return busno;
827
828 if (is_serdes_configured(dev) && !(devdisr & devdisr_mask[num])) {
829 busno = fsl_configure_pcie(pci_info, hose,
830 board_serdes_name(dev), busno);
831 } else {
Peter Tyser3771ba32010-12-28 17:47:25 -0600832 printf("PCIe%d: disabled\n", num + 1);
Kumar Gala4d4384e2010-12-15 14:21:41 -0600833 }
834
835 return busno;
836}
837
838int fsl_pcie_init_board(int busno)
839{
840 struct fsl_pci_info pci_info;
841 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC8xxx_GUTS_ADDR;
York Sun9941a222012-10-08 07:44:19 +0000842 u32 devdisr;
843 u32 *addr;
844
845#ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
846 addr = &gur->devdisr3;
847#else
848 addr = &gur->devdisr;
849#endif
850 devdisr = in_be32(addr);
Kumar Gala4d4384e2010-12-15 14:21:41 -0600851
852#ifdef CONFIG_PCIE1
853 SET_STD_PCIE_INFO(pci_info, 1);
854 busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE1, &pci_info);
855#else
York Sun9941a222012-10-08 07:44:19 +0000856 setbits_be32(addr, _DEVDISR_PCIE1); /* disable */
Kumar Gala4d4384e2010-12-15 14:21:41 -0600857#endif
858
859#ifdef CONFIG_PCIE2
860 SET_STD_PCIE_INFO(pci_info, 2);
861 busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE2, &pci_info);
862#else
York Sun9941a222012-10-08 07:44:19 +0000863 setbits_be32(addr, _DEVDISR_PCIE2); /* disable */
Kumar Gala4d4384e2010-12-15 14:21:41 -0600864#endif
865
866#ifdef CONFIG_PCIE3
867 SET_STD_PCIE_INFO(pci_info, 3);
868 busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE3, &pci_info);
869#else
York Sun9941a222012-10-08 07:44:19 +0000870 setbits_be32(addr, _DEVDISR_PCIE3); /* disable */
Kumar Gala4d4384e2010-12-15 14:21:41 -0600871#endif
872
873#ifdef CONFIG_PCIE4
874 SET_STD_PCIE_INFO(pci_info, 4);
875 busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE4, &pci_info);
876#else
York Sun9941a222012-10-08 07:44:19 +0000877 setbits_be32(addr, _DEVDISR_PCIE4); /* disable */
Kumar Gala4d4384e2010-12-15 14:21:41 -0600878#endif
879
880 return busno;
881}
882#else
883int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
884 struct fsl_pci_info *pci_info)
885{
886 return busno;
887}
888
889int fsl_pcie_init_board(int busno)
890{
891 return busno;
892}
893#endif
894
Kumar Galafe29f1f2008-10-23 00:01:06 -0500895#ifdef CONFIG_OF_BOARD_SETUP
Masahiro Yamada75f82d02018-03-05 01:20:11 +0900896#include <linux/libfdt.h>
Kumar Galafe29f1f2008-10-23 00:01:06 -0500897#include <fdt_support.h>
898
Kumar Galad0f27d32010-07-08 22:37:44 -0500899void ft_fsl_pci_setup(void *blob, const char *pci_compat,
Kumar Galadb943ed2010-12-17 05:57:25 -0600900 unsigned long ctrl_addr)
Kumar Galafe29f1f2008-10-23 00:01:06 -0500901{
Kumar Galad0f27d32010-07-08 22:37:44 -0500902 int off;
Kumar Gala326ed2f2010-03-30 10:07:12 -0500903 u32 bus_range[2];
Kumar Galad0f27d32010-07-08 22:37:44 -0500904 phys_addr_t p_ctrl_addr = (phys_addr_t)ctrl_addr;
Kumar Galadb943ed2010-12-17 05:57:25 -0600905 struct pci_controller *hose;
906
907 hose = find_hose_by_cfg_addr((void *)(ctrl_addr));
Kumar Galad0f27d32010-07-08 22:37:44 -0500908
909 /* convert ctrl_addr to true physical address */
910 p_ctrl_addr = (phys_addr_t)ctrl_addr - CONFIG_SYS_CCSRBAR;
911 p_ctrl_addr += CONFIG_SYS_CCSRBAR_PHYS;
912
913 off = fdt_node_offset_by_compat_reg(blob, pci_compat, p_ctrl_addr);
Kumar Galafe29f1f2008-10-23 00:01:06 -0500914
Kumar Gala326ed2f2010-03-30 10:07:12 -0500915 if (off < 0)
916 return;
Kumar Galafe29f1f2008-10-23 00:01:06 -0500917
Kumar Gala326ed2f2010-03-30 10:07:12 -0500918 /* We assume a cfg_addr not being set means we didn't setup the controller */
919 if ((hose == NULL) || (hose->cfg_addr == NULL)) {
Kumar Galad0f27d32010-07-08 22:37:44 -0500920 fdt_del_node(blob, off);
Kumar Gala326ed2f2010-03-30 10:07:12 -0500921 } else {
Kumar Galafe29f1f2008-10-23 00:01:06 -0500922 bus_range[0] = 0;
923 bus_range[1] = hose->last_busno - hose->first_busno;
924 fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
925 fdt_pci_dma_ranges(blob, off, hose);
926 }
927}
928#endif