c3c67fc97bd7e3de78f61bea071da974d4a9f353
[oweals/u-boot.git] / drivers / pci / fsl_pci_init.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2007-2012 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <init.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <asm/fsl_serdes.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /*
16  * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
17  *
18  * Initialize controller and call the common driver/pci pci_hose_scan to
19  * scan for bridges and devices.
20  *
21  * Hose fields which need to be pre-initialized by board specific code:
22  *   regions[]
23  *   first_busno
24  *
25  * Fields updated:
26  *   last_busno
27  */
28
29 #include <pci.h>
30 #include <asm/io.h>
31 #include <asm/fsl_pci.h>
32
33 #ifndef CONFIG_SYS_PCI_MEMORY_BUS
34 #define CONFIG_SYS_PCI_MEMORY_BUS 0
35 #endif
36
37 #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
38 #define CONFIG_SYS_PCI_MEMORY_PHYS 0
39 #endif
40
41 #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
42 #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
43 #endif
44
45 /* Setup one inbound ATMU window.
46  *
47  * We let the caller decide what the window size should be
48  */
49 static void set_inbound_window(volatile pit_t *pi,
50                                 struct pci_region *r,
51                                 u64 size)
52 {
53         u32 sz = (__ilog2_u64(size) - 1);
54 #ifdef CONFIG_SYS_FSL_ERRATUM_A005434
55         u32 flag = 0;
56 #else
57         u32 flag = PIWAR_LOCAL;
58 #endif
59
60         flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
61
62         out_be32(&pi->pitar, r->phys_start >> 12);
63         out_be32(&pi->piwbar, r->bus_start >> 12);
64 #ifdef CONFIG_SYS_PCI_64BIT
65         out_be32(&pi->piwbear, r->bus_start >> 44);
66 #else
67         out_be32(&pi->piwbear, 0);
68 #endif
69         if (r->flags & PCI_REGION_PREFETCH)
70                 flag |= PIWAR_PF;
71         out_be32(&pi->piwar, flag | sz);
72 }
73
74 int fsl_setup_hose(struct pci_controller *hose, unsigned long addr)
75 {
76         volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) addr;
77
78         /* Reset hose to make sure its in a clean state */
79         memset(hose, 0, sizeof(struct pci_controller));
80
81         pci_setup_indirect(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
82
83         return fsl_is_pci_agent(hose);
84 }
85
86 static int fsl_pci_setup_inbound_windows(struct pci_controller *hose,
87                                          u64 out_lo, u8 pcie_cap,
88                                          volatile pit_t *pi)
89 {
90         struct pci_region *r = hose->regions + hose->region_count;
91         u64 sz = min((u64)gd->ram_size, (1ull << 32));
92
93         phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
94         pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
95         pci_size_t pci_sz;
96
97         /* we have no space available for inbound memory mapping */
98         if (bus_start > out_lo) {
99                 printf ("no space for inbound mapping of memory\n");
100                 return 0;
101         }
102
103         /* limit size */
104         if ((bus_start + sz) > out_lo) {
105                 sz = out_lo - bus_start;
106                 debug ("limiting size to %llx\n", sz);
107         }
108
109         pci_sz = 1ull << __ilog2_u64(sz);
110         /*
111          * we can overlap inbound/outbound windows on PCI-E since RX & TX
112          * links a separate
113          */
114         if ((pcie_cap == PCI_CAP_ID_EXP) && (pci_sz < sz)) {
115                 debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
116                         (u64)bus_start, (u64)phys_start, (u64)sz);
117                 pci_set_region(r, bus_start, phys_start, sz,
118                                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
119                                 PCI_REGION_PREFETCH);
120
121                 /* if we aren't an exact power of two match, pci_sz is smaller
122                  * round it up to the next power of two.  We report the actual
123                  * size to pci region tracking.
124                  */
125                 if (pci_sz != sz)
126                         sz = 2ull << __ilog2_u64(sz);
127
128                 set_inbound_window(pi--, r++, sz);
129                 sz = 0; /* make sure we dont set the R2 window */
130         } else {
131                 debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
132                         (u64)bus_start, (u64)phys_start, (u64)pci_sz);
133                 pci_set_region(r, bus_start, phys_start, pci_sz,
134                                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
135                                 PCI_REGION_PREFETCH);
136                 set_inbound_window(pi--, r++, pci_sz);
137
138                 sz -= pci_sz;
139                 bus_start += pci_sz;
140                 phys_start += pci_sz;
141
142                 pci_sz = 1ull << __ilog2_u64(sz);
143                 if (sz) {
144                         debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
145                                 (u64)bus_start, (u64)phys_start, (u64)pci_sz);
146                         pci_set_region(r, bus_start, phys_start, pci_sz,
147                                         PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
148                                         PCI_REGION_PREFETCH);
149                         set_inbound_window(pi--, r++, pci_sz);
150                         sz -= pci_sz;
151                         bus_start += pci_sz;
152                         phys_start += pci_sz;
153                 }
154         }
155
156 #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
157         /*
158          * On 64-bit capable systems, set up a mapping for all of DRAM
159          * in high pci address space.
160          */
161         pci_sz = 1ull << __ilog2_u64(gd->ram_size);
162         /* round up to the next largest power of two */
163         if (gd->ram_size > pci_sz)
164                 pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
165         debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
166                 (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
167                 (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
168                 (u64)pci_sz);
169         pci_set_region(r,
170                         CONFIG_SYS_PCI64_MEMORY_BUS,
171                         CONFIG_SYS_PCI_MEMORY_PHYS,
172                         pci_sz,
173                         PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
174                         PCI_REGION_PREFETCH);
175         set_inbound_window(pi--, r++, pci_sz);
176 #else
177         pci_sz = 1ull << __ilog2_u64(sz);
178         if (sz) {
179                 debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
180                         (u64)bus_start, (u64)phys_start, (u64)pci_sz);
181                 pci_set_region(r, bus_start, phys_start, pci_sz,
182                                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
183                                 PCI_REGION_PREFETCH);
184                 sz -= pci_sz;
185                 bus_start += pci_sz;
186                 phys_start += pci_sz;
187                 set_inbound_window(pi--, r++, pci_sz);
188         }
189 #endif
190
191 #ifdef CONFIG_PHYS_64BIT
192         if (sz && (((u64)gd->ram_size) < (1ull << 32)))
193                 printf("Was not able to map all of memory via "
194                         "inbound windows -- %lld remaining\n", sz);
195 #endif
196
197         hose->region_count = r - hose->regions;
198
199         return 1;
200 }
201
202 #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
203 static void fsl_pcie_boot_master(pit_t *pi)
204 {
205         /* configure inbound window for slave's u-boot image */
206         debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
207                         "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
208                         (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
209                         (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
210                         CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
211         struct pci_region r_inbound;
212         u32 sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE)
213                                         - 1;
214         pci_set_region(&r_inbound,
215                 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
216                 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
217                 sz_inbound,
218                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
219
220         set_inbound_window(pi--, &r_inbound,
221                 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
222
223         /* configure inbound window for slave's u-boot image */
224         debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
225                         "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
226                         (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
227                         (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
228                         CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
229         pci_set_region(&r_inbound,
230                 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
231                 CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
232                 sz_inbound,
233                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
234
235         set_inbound_window(pi--, &r_inbound,
236                 CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
237
238         /* configure inbound window for slave's ucode and ENV */
239         debug("PCIEBOOT - MASTER: Inbound window for slave's "
240                         "ucode and ENV; "
241                         "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
242                         (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
243                         (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
244                         CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
245         sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE)
246                                 - 1;
247         pci_set_region(&r_inbound,
248                 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
249                 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
250                 sz_inbound,
251                 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
252
253         set_inbound_window(pi--, &r_inbound,
254                 CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
255 }
256
257 static void fsl_pcie_boot_master_release_slave(int port)
258 {
259         unsigned long release_addr;
260
261         /* now release slave's core 0 */
262         switch (port) {
263         case 1:
264                 release_addr = CONFIG_SYS_PCIE1_MEM_VIRT
265                         + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
266                 break;
267 #ifdef CONFIG_SYS_PCIE2_MEM_VIRT
268         case 2:
269                 release_addr = CONFIG_SYS_PCIE2_MEM_VIRT
270                         + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
271                 break;
272 #endif
273 #ifdef CONFIG_SYS_PCIE3_MEM_VIRT
274         case 3:
275                 release_addr = CONFIG_SYS_PCIE3_MEM_VIRT
276                         + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
277                 break;
278 #endif
279         default:
280                 release_addr = 0;
281                 break;
282         }
283         if (release_addr != 0) {
284                 out_be32((void *)release_addr,
285                         CONFIG_SRIO_PCIE_BOOT_RELEASE_MASK);
286                 debug("PCIEBOOT - MASTER: "
287                         "Release slave successfully! Now the slave should start up!\n");
288         } else {
289                 debug("PCIEBOOT - MASTER: "
290                         "Release slave failed!\n");
291         }
292 }
293 #endif
294
295 void fsl_pci_init(struct pci_controller *hose, struct fsl_pci_info *pci_info)
296 {
297         u32 cfg_addr = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_addr;
298         u32 cfg_data = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_data;
299         u16 temp16;
300         u32 temp32;
301         u32 block_rev;
302         int enabled, r, inbound = 0;
303         u16 ltssm;
304         u8 temp8, pcie_cap;
305         int pcie_cap_pos;
306         int pci_dcr;
307         int pci_dsr;
308         int pci_lsr;
309
310 #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
311         int pci_lcr;
312 #endif
313
314         volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)cfg_addr;
315         struct pci_region *reg = hose->regions + hose->region_count;
316         pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
317
318         /* Initialize ATMU registers based on hose regions and flags */
319         volatile pot_t *po = &pci->pot[1];      /* skip 0 */
320         volatile pit_t *pi;
321
322         u64 out_hi = 0, out_lo = -1ULL;
323         u32 pcicsrbar, pcicsrbar_sz;
324
325         pci_setup_indirect(hose, cfg_addr, cfg_data);
326
327 #ifdef PEX_CCB_DIV
328         /* Configure the PCIE controller core clock ratio */
329         pci_hose_write_config_dword(hose, dev, 0x440,
330                                     ((gd->bus_clk / 1000000) *
331                                      (16 / PEX_CCB_DIV)) / 333);
332 #endif
333         block_rev = in_be32(&pci->block_rev1);
334         if (PEX_IP_BLK_REV_2_2 <= block_rev) {
335                 pi = &pci->pit[2];      /* 0xDC0 */
336         } else {
337                 pi = &pci->pit[3];      /* 0xDE0 */
338         }
339
340         /* Handle setup of outbound windows first */
341         for (r = 0; r < hose->region_count; r++) {
342                 unsigned long flags = hose->regions[r].flags;
343                 u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
344
345                 flags &= PCI_REGION_SYS_MEMORY|PCI_REGION_TYPE;
346                 if (flags != PCI_REGION_SYS_MEMORY) {
347                         u64 start = hose->regions[r].bus_start;
348                         u64 end = start + hose->regions[r].size;
349
350                         out_be32(&po->powbar, hose->regions[r].phys_start >> 12);
351                         out_be32(&po->potar, start >> 12);
352 #ifdef CONFIG_SYS_PCI_64BIT
353                         out_be32(&po->potear, start >> 44);
354 #else
355                         out_be32(&po->potear, 0);
356 #endif
357                         if (hose->regions[r].flags & PCI_REGION_IO) {
358                                 out_be32(&po->powar, POWAR_EN | sz |
359                                         POWAR_IO_READ | POWAR_IO_WRITE);
360                         } else {
361                                 out_be32(&po->powar, POWAR_EN | sz |
362                                         POWAR_MEM_READ | POWAR_MEM_WRITE);
363                                 out_lo = min(start, out_lo);
364                                 out_hi = max(end, out_hi);
365                         }
366                         po++;
367                 }
368         }
369         debug("Outbound memory range: %llx:%llx\n", out_lo, out_hi);
370
371         /* setup PCSRBAR/PEXCSRBAR */
372         pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
373         pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
374         pcicsrbar_sz = ~pcicsrbar_sz + 1;
375
376         if (out_hi < (0x100000000ull - pcicsrbar_sz) ||
377                 (out_lo > 0x100000000ull))
378                 pcicsrbar = 0x100000000ull - pcicsrbar_sz;
379         else
380                 pcicsrbar = (out_lo - pcicsrbar_sz) & -pcicsrbar_sz;
381         pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, pcicsrbar);
382
383         out_lo = min(out_lo, (u64)pcicsrbar);
384
385         debug("PCICSRBAR @ 0x%x\n", pcicsrbar);
386
387         pci_set_region(reg++, pcicsrbar, CONFIG_SYS_CCSRBAR_PHYS,
388                         pcicsrbar_sz, PCI_REGION_SYS_MEMORY);
389         hose->region_count++;
390
391         /* see if we are a PCIe or PCI controller */
392         pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
393         pci_dcr = pcie_cap_pos + 0x08;
394         pci_dsr = pcie_cap_pos + 0x0a;
395         pci_lsr = pcie_cap_pos + 0x12;
396
397         pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
398
399 #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
400         /* boot from PCIE --master */
401         char *s = env_get("bootmaster");
402         char pcie[6];
403         sprintf(pcie, "PCIE%d", pci_info->pci_num);
404
405         if (s && (strcmp(s, pcie) == 0)) {
406                 debug("PCIEBOOT - MASTER: Master port [ %d ] for pcie boot.\n",
407                                 pci_info->pci_num);
408                 fsl_pcie_boot_master((pit_t *)pi);
409         } else {
410                 /* inbound */
411                 inbound = fsl_pci_setup_inbound_windows(hose,
412                                         out_lo, pcie_cap, pi);
413         }
414 #else
415         /* inbound */
416         inbound = fsl_pci_setup_inbound_windows(hose, out_lo, pcie_cap, pi);
417 #endif
418
419         for (r = 0; r < hose->region_count; r++)
420                 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n", r,
421                         (u64)hose->regions[r].phys_start,
422                         (u64)hose->regions[r].bus_start,
423                         (u64)hose->regions[r].size,
424                         hose->regions[r].flags);
425
426         pci_register_hose(hose);
427         pciauto_config_init(hose);      /* grab pci_{mem,prefetch,io} */
428         hose->current_busno = hose->first_busno;
429
430         out_be32(&pci->pedr, 0xffffffff);       /* Clear any errors */
431         out_be32(&pci->peer, ~0x20140); /* Enable All Error Interrupts except
432                                          * - Master abort (pci)
433                                          * - Master PERR (pci)
434                                          * - ICCA (PCIe)
435                                          */
436         pci_hose_read_config_dword(hose, dev, pci_dcr, &temp32);
437         temp32 |= 0xf000e;              /* set URR, FER, NFER (but not CER) */
438         pci_hose_write_config_dword(hose, dev, pci_dcr, temp32);
439
440 #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
441         pci_lcr = pcie_cap_pos + 0x10;
442         temp32 = 0;
443         pci_hose_read_config_dword(hose, dev, pci_lcr, &temp32);
444         temp32 &= ~0x03;                /* Disable ASPM  */
445         pci_hose_write_config_dword(hose, dev, pci_lcr, temp32);
446         udelay(1);
447 #endif
448         if (pcie_cap == PCI_CAP_ID_EXP) {
449                 if (block_rev >= PEX_IP_BLK_REV_3_0) {
450 #define PEX_CSR0_LTSSM_MASK     0xFC
451 #define PEX_CSR0_LTSSM_SHIFT    2
452                         ltssm = (in_be32(&pci->pex_csr0)
453                                 & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
454                         enabled = (ltssm == 0x11) ? 1 : 0;
455 #ifdef CONFIG_FSL_PCIE_RESET
456                         int i;
457                         /* assert PCIe reset */
458                         setbits_be32(&pci->pdb_stat, 0x08000000);
459                         (void) in_be32(&pci->pdb_stat);
460                         udelay(1000);
461                         /* clear PCIe reset */
462                         clrbits_be32(&pci->pdb_stat, 0x08000000);
463                         asm("sync;isync");
464                         for (i = 0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
465                                 pci_hose_read_config_word(hose, dev, PCI_LTSSM,
466                                                           &ltssm);
467                                 udelay(1000);
468                         }
469 #endif
470                 } else {
471                 /* pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm); */
472                 /* enabled = ltssm >= PCI_LTSSM_L0; */
473                 pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
474                 enabled = ltssm >= PCI_LTSSM_L0;
475
476 #ifdef CONFIG_FSL_PCIE_RESET
477                 if (ltssm == 1) {
478                         int i;
479                         debug("....PCIe link error. " "LTSSM=0x%02x.", ltssm);
480                         /* assert PCIe reset */
481                         setbits_be32(&pci->pdb_stat, 0x08000000);
482                         (void) in_be32(&pci->pdb_stat);
483                         udelay(100);
484                         debug("  Asserting PCIe reset @%p = %x\n",
485                               &pci->pdb_stat, in_be32(&pci->pdb_stat));
486                         /* clear PCIe reset */
487                         clrbits_be32(&pci->pdb_stat, 0x08000000);
488                         asm("sync;isync");
489                         for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
490                                 pci_hose_read_config_word(hose, dev, PCI_LTSSM,
491                                                         &ltssm);
492                                 udelay(1000);
493                                 debug("....PCIe link error. "
494                                       "LTSSM=0x%02x.\n", ltssm);
495                         }
496                         enabled = ltssm >= PCI_LTSSM_L0;
497
498                         /* we need to re-write the bar0 since a reset will
499                          * clear it
500                          */
501                         pci_hose_write_config_dword(hose, dev,
502                                         PCI_BASE_ADDRESS_0, pcicsrbar);
503                 }
504 #endif
505         }
506
507 #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
508                 if (enabled == 0) {
509                         serdes_corenet_t *srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
510                         temp32 = in_be32(&srds_regs->srdspccr0);
511
512                         if ((temp32 >> 28) == 3) {
513                                 int i;
514
515                                 out_be32(&srds_regs->srdspccr0, 2 << 28);
516                                 setbits_be32(&pci->pdb_stat, 0x08000000);
517                                 in_be32(&pci->pdb_stat);
518                                 udelay(100);
519                                 clrbits_be32(&pci->pdb_stat, 0x08000000);
520                                 asm("sync;isync");
521                                 for (i=0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
522                                         pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
523                                         udelay(1000);
524                                 }
525                                 enabled = ltssm >= PCI_LTSSM_L0;
526                         }
527                 }
528 #endif
529                 if (!enabled) {
530                         /* Let the user know there's no PCIe link for root
531                          * complex. for endpoint, the link may not setup, so
532                          * print undetermined.
533                          */
534                         if (fsl_is_pci_agent(hose))
535                                 printf("undetermined, regs @ 0x%lx\n", pci_info->regs);
536                         else
537                                 printf("no link, regs @ 0x%lx\n", pci_info->regs);
538                         hose->last_busno = hose->first_busno;
539                         return;
540                 }
541
542                 out_be32(&pci->pme_msg_det, 0xffffffff);
543                 out_be32(&pci->pme_msg_int_en, 0xffffffff);
544
545                 /* Print the negotiated PCIe link width */
546                 pci_hose_read_config_word(hose, dev, pci_lsr, &temp16);
547                 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
548                        (temp16 & 0xf), pci_info->regs);
549
550                 hose->current_busno++; /* Start scan with secondary */
551                 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
552         }
553
554 #ifdef CONFIG_SYS_FSL_ERRATUM_A007815
555         /* The Read-Only Write Enable bit defaults to 1 instead of 0.
556          * Set to 0 to protect the read-only registers.
557          */
558         clrbits_be32(&pci->dbi_ro_wr_en, 0x01);
559 #endif
560
561         /* Use generic setup_device to initialize standard pci regs,
562          * but do not allocate any windows since any BAR found (such
563          * as PCSRBAR) is not in this cpu's memory space.
564          */
565         pciauto_setup_device(hose, dev, 0, hose->pci_mem,
566                              hose->pci_prefetch, hose->pci_io);
567
568         if (inbound) {
569                 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
570                 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
571                                            temp16 | PCI_COMMAND_MEMORY);
572         }
573
574 #ifndef CONFIG_PCI_NOSCAN
575         if (!fsl_is_pci_agent(hose)) {
576                 debug("           Scanning PCI bus %02x\n",
577                         hose->current_busno);
578                 hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
579         } else {
580                 debug("           Not scanning PCI bus %02x. PI=%x\n",
581                         hose->current_busno, temp8);
582                 hose->last_busno = hose->current_busno;
583         }
584
585         /* if we are PCIe - update limit regs and subordinate busno
586          * for the virtual P2P bridge
587          */
588         if (pcie_cap == PCI_CAP_ID_EXP) {
589                 pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
590         }
591 #else
592         hose->last_busno = hose->current_busno;
593 #endif
594
595         /* Clear all error indications */
596         if (pcie_cap == PCI_CAP_ID_EXP)
597                 out_be32(&pci->pme_msg_det, 0xffffffff);
598         out_be32(&pci->pedr, 0xffffffff);
599
600         pci_hose_read_config_word(hose, dev, pci_dsr, &temp16);
601         if (temp16) {
602                 pci_hose_write_config_word(hose, dev, pci_dsr, 0xffff);
603         }
604
605         pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
606         if (temp16) {
607                 pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
608         }
609 }
610
611 int fsl_is_pci_agent(struct pci_controller *hose)
612 {
613         int pcie_cap_pos;
614         u8 pcie_cap;
615         pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
616
617         pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
618         pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
619         if (pcie_cap == PCI_CAP_ID_EXP) {
620                 u8 header_type;
621
622                 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE,
623                                           &header_type);
624                 return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
625         } else {
626                 u8 prog_if;
627
628                 pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prog_if);
629                 /* Programming Interface (PCI_CLASS_PROG)
630                  * 0 == pci host or pcie root-complex,
631                  * 1 == pci agent or pcie end-point
632                  */
633                 return (prog_if == FSL_PROG_IF_AGENT);
634         }
635 }
636
637 int fsl_pci_init_port(struct fsl_pci_info *pci_info,
638                         struct pci_controller *hose, int busno)
639 {
640         volatile ccsr_fsl_pci_t *pci;
641         struct pci_region *r;
642         pci_dev_t dev = PCI_BDF(busno,0,0);
643         int pcie_cap_pos;
644         u8 pcie_cap;
645
646         pci = (ccsr_fsl_pci_t *) pci_info->regs;
647
648         /* on non-PCIe controllers we don't have pme_msg_det so this code
649          * should do nothing since the read will return 0
650          */
651         if (in_be32(&pci->pme_msg_det)) {
652                 out_be32(&pci->pme_msg_det, 0xffffffff);
653                 debug (" with errors.  Clearing.  Now 0x%08x",
654                         pci->pme_msg_det);
655         }
656
657         r = hose->regions + hose->region_count;
658
659         /* outbound memory */
660         pci_set_region(r++,
661                         pci_info->mem_bus,
662                         pci_info->mem_phys,
663                         pci_info->mem_size,
664                         PCI_REGION_MEM);
665
666         /* outbound io */
667         pci_set_region(r++,
668                         pci_info->io_bus,
669                         pci_info->io_phys,
670                         pci_info->io_size,
671                         PCI_REGION_IO);
672
673         hose->region_count = r - hose->regions;
674         hose->first_busno = busno;
675
676         fsl_pci_init(hose, pci_info);
677
678         if (fsl_is_pci_agent(hose)) {
679                 fsl_pci_config_unlock(hose);
680                 hose->last_busno = hose->first_busno;
681 #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
682         } else {
683                 /* boot from PCIE --master releases slave's core 0 */
684                 char *s = env_get("bootmaster");
685                 char pcie[6];
686                 sprintf(pcie, "PCIE%d", pci_info->pci_num);
687
688                 if (s && (strcmp(s, pcie) == 0))
689                         fsl_pcie_boot_master_release_slave(pci_info->pci_num);
690 #endif
691         }
692
693         pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
694         pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
695         printf("PCI%s%x: Bus %02x - %02x\n", pcie_cap == PCI_CAP_ID_EXP ?
696                 "e" : "", pci_info->pci_num,
697                 hose->first_busno, hose->last_busno);
698         return(hose->last_busno + 1);
699 }
700
701 /* Enable inbound PCI config cycles for agent/endpoint interface */
702 void fsl_pci_config_unlock(struct pci_controller *hose)
703 {
704         pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
705         int pcie_cap_pos;
706         u8 pcie_cap;
707         u16 pbfr;
708
709         if (!fsl_is_pci_agent(hose))
710                 return;
711
712         pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
713         pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
714         if (pcie_cap != 0x0) {
715                 ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)hose->cfg_addr;
716                 u32 block_rev = in_be32(&pci->block_rev1);
717                 /* PCIe - set CFG_READY bit of Configuration Ready Register */
718                 if (block_rev >= PEX_IP_BLK_REV_3_0)
719                         setbits_be32(&pci->config, FSL_PCIE_V3_CFG_RDY);
720                 else
721                         pci_hose_write_config_byte(hose, dev,
722                                                    FSL_PCIE_CFG_RDY, 0x1);
723         } else {
724                 /* PCI - clear ACL bit of PBFR */
725                 pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
726                 pbfr &= ~0x20;
727                 pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
728         }
729 }
730
731 #if defined(CONFIG_PCIE1) || defined(CONFIG_PCIE2) || \
732     defined(CONFIG_PCIE3) || defined(CONFIG_PCIE4)
733 int fsl_configure_pcie(struct fsl_pci_info *info,
734                         struct pci_controller *hose,
735                         const char *connected, int busno)
736 {
737         int is_endpoint;
738
739         set_next_law(info->mem_phys, law_size_bits(info->mem_size), info->law);
740         set_next_law(info->io_phys, law_size_bits(info->io_size), info->law);
741
742         is_endpoint = fsl_setup_hose(hose, info->regs);
743         printf("PCIe%u: %s", info->pci_num,
744                 is_endpoint ? "Endpoint" : "Root Complex");
745         if (connected)
746                 printf(" of %s", connected);
747         puts(", ");
748
749         return fsl_pci_init_port(info, hose, busno);
750 }
751
752 #if defined(CONFIG_FSL_CORENET)
753 #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
754         #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR3_PCIE1
755         #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR3_PCIE2
756         #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR3_PCIE3
757         #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR3_PCIE4
758 #else
759         #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR_PCIE1
760         #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR_PCIE2
761         #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR_PCIE3
762         #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR_PCIE4
763 #endif
764         #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
765 #elif defined(CONFIG_MPC85xx)
766         #define _DEVDISR_PCIE1 MPC85xx_DEVDISR_PCIE
767         #define _DEVDISR_PCIE2 MPC85xx_DEVDISR_PCIE2
768         #define _DEVDISR_PCIE3 MPC85xx_DEVDISR_PCIE3
769         #define _DEVDISR_PCIE4 0
770         #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
771 #elif defined(CONFIG_MPC86xx)
772         #define _DEVDISR_PCIE1 MPC86xx_DEVDISR_PCIE1
773         #define _DEVDISR_PCIE2 MPC86xx_DEVDISR_PCIE2
774         #define _DEVDISR_PCIE3 0
775         #define _DEVDISR_PCIE4 0
776         #define CONFIG_SYS_MPC8xxx_GUTS_ADDR \
777                 (&((immap_t *)CONFIG_SYS_IMMR)->im_gur)
778 #else
779 #error "No defines for DEVDISR_PCIE"
780 #endif
781
782 /* Implement a dummy function for those platforms w/o SERDES */
783 static const char *__board_serdes_name(enum srds_prtcl device)
784 {
785         switch (device) {
786 #ifdef CONFIG_SYS_PCIE1_NAME
787         case PCIE1:
788                 return CONFIG_SYS_PCIE1_NAME;
789 #endif
790 #ifdef CONFIG_SYS_PCIE2_NAME
791         case PCIE2:
792                 return CONFIG_SYS_PCIE2_NAME;
793 #endif
794 #ifdef CONFIG_SYS_PCIE3_NAME
795         case PCIE3:
796                 return CONFIG_SYS_PCIE3_NAME;
797 #endif
798 #ifdef CONFIG_SYS_PCIE4_NAME
799         case PCIE4:
800                 return CONFIG_SYS_PCIE4_NAME;
801 #endif
802         default:
803                 return NULL;
804         }
805
806         return NULL;
807 }
808
809 __attribute__((weak, alias("__board_serdes_name"))) const char *
810 board_serdes_name(enum srds_prtcl device);
811
812 static u32 devdisr_mask[] = {
813         _DEVDISR_PCIE1,
814         _DEVDISR_PCIE2,
815         _DEVDISR_PCIE3,
816         _DEVDISR_PCIE4,
817 };
818
819 int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
820                         struct fsl_pci_info *pci_info)
821 {
822         struct pci_controller *hose;
823         int num = dev - PCIE1;
824
825         hose = calloc(1, sizeof(struct pci_controller));
826         if (!hose)
827                 return busno;
828
829         if (is_serdes_configured(dev) && !(devdisr & devdisr_mask[num])) {
830                 busno = fsl_configure_pcie(pci_info, hose,
831                                 board_serdes_name(dev), busno);
832         } else {
833                 printf("PCIe%d: disabled\n", num + 1);
834         }
835
836         return busno;
837 }
838
839 int fsl_pcie_init_board(int busno)
840 {
841         struct fsl_pci_info pci_info;
842         ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC8xxx_GUTS_ADDR;
843         u32 devdisr;
844         u32 *addr;
845
846 #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
847         addr = &gur->devdisr3;
848 #else
849         addr = &gur->devdisr;
850 #endif
851         devdisr = in_be32(addr);
852
853 #ifdef CONFIG_PCIE1
854         SET_STD_PCIE_INFO(pci_info, 1);
855         busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE1, &pci_info);
856 #else
857         setbits_be32(addr, _DEVDISR_PCIE1); /* disable */
858 #endif
859
860 #ifdef CONFIG_PCIE2
861         SET_STD_PCIE_INFO(pci_info, 2);
862         busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE2, &pci_info);
863 #else
864         setbits_be32(addr, _DEVDISR_PCIE2); /* disable */
865 #endif
866
867 #ifdef CONFIG_PCIE3
868         SET_STD_PCIE_INFO(pci_info, 3);
869         busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE3, &pci_info);
870 #else
871         setbits_be32(addr, _DEVDISR_PCIE3); /* disable */
872 #endif
873
874 #ifdef CONFIG_PCIE4
875         SET_STD_PCIE_INFO(pci_info, 4);
876         busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE4, &pci_info);
877 #else
878         setbits_be32(addr, _DEVDISR_PCIE4); /* disable */
879 #endif
880
881         return busno;
882 }
883 #else
884 int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
885                         struct fsl_pci_info *pci_info)
886 {
887         return busno;
888 }
889
890 int fsl_pcie_init_board(int busno)
891 {
892         return busno;
893 }
894 #endif
895
896 #ifdef CONFIG_OF_BOARD_SETUP
897 #include <linux/libfdt.h>
898 #include <fdt_support.h>
899
900 void ft_fsl_pci_setup(void *blob, const char *pci_compat,
901                         unsigned long ctrl_addr)
902 {
903         int off;
904         u32 bus_range[2];
905         phys_addr_t p_ctrl_addr = (phys_addr_t)ctrl_addr;
906         struct pci_controller *hose;
907
908         hose = find_hose_by_cfg_addr((void *)(ctrl_addr));
909
910         /* convert ctrl_addr to true physical address */
911         p_ctrl_addr = (phys_addr_t)ctrl_addr - CONFIG_SYS_CCSRBAR;
912         p_ctrl_addr += CONFIG_SYS_CCSRBAR_PHYS;
913
914         off = fdt_node_offset_by_compat_reg(blob, pci_compat, p_ctrl_addr);
915
916         if (off < 0)
917                 return;
918
919         /* We assume a cfg_addr not being set means we didn't setup the controller */
920         if ((hose == NULL) || (hose->cfg_addr == NULL)) {
921                 fdt_del_node(blob, off);
922         } else {
923                 bus_range[0] = 0;
924                 bus_range[1] = hose->last_busno - hose->first_busno;
925                 fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
926                 fdt_pci_dma_ranges(blob, off, hose);
927         }
928 }
929 #endif