]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/ppc4xx/4xx_pcie.c
ppc4xx: Fix msg "initialization as root-complex failed" upon PCIe scan
[karo-tx-uboot.git] / cpu / ppc4xx / 4xx_pcie.c
1 /*
2  * (C) Copyright 2006 - 2008
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
6  * Roland Dreier <rolandd@cisco.com>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  */
22
23 /* define DEBUG for debugging output (obviously ;-)) */
24 #if 0
25 #define DEBUG
26 #endif
27
28 #include <common.h>
29 #include <pci.h>
30 #include <ppc4xx.h>
31 #include <asm/processor.h>
32 #include <asm-ppc/io.h>
33 #include <asm/errno.h>
34
35 #if (defined(CONFIG_440SPE) || defined(CONFIG_405EX) || \
36     defined(CONFIG_460EX) || defined(CONFIG_460GT)) && \
37     defined(CONFIG_PCI) && !defined(CONFIG_PCI_DISABLE_PCIE)
38
39 #include <asm/4xx_pcie.h>
40
41 enum {
42         PTYPE_ENDPOINT          = 0x0,
43         PTYPE_LEGACY_ENDPOINT   = 0x1,
44         PTYPE_ROOT_PORT         = 0x4,
45
46         LNKW_X1                 = 0x1,
47         LNKW_X4                 = 0x4,
48         LNKW_X8                 = 0x8
49 };
50
51 static int validate_endpoint(struct pci_controller *hose)
52 {
53         if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE0_CFGBASE)
54                 return (is_end_point(0));
55         else if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE1_CFGBASE)
56                 return (is_end_point(1));
57 #if CONFIG_SYS_PCIE_NR_PORTS > 2
58         else if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE2_CFGBASE)
59                 return (is_end_point(2));
60 #endif
61
62         return 0;
63 }
64
65 static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
66 {
67         u8 *base = (u8*)hose->cfg_data;
68
69         /* use local configuration space for the first bus */
70         if (PCI_BUS(devfn) == 0) {
71                 if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE0_CFGBASE)
72                         base = (u8*)CONFIG_SYS_PCIE0_XCFGBASE;
73                 if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE1_CFGBASE)
74                         base = (u8*)CONFIG_SYS_PCIE1_XCFGBASE;
75 #if CONFIG_SYS_PCIE_NR_PORTS > 2
76                 if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE2_CFGBASE)
77                         base = (u8*)CONFIG_SYS_PCIE2_XCFGBASE;
78 #endif
79         }
80
81         return base;
82 }
83
84 static void pcie_dmer_disable(void)
85 {
86         mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE),
87                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
88         mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
89                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
90 #if CONFIG_SYS_PCIE_NR_PORTS > 2
91         mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
92                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
93 #endif
94 }
95
96 static void pcie_dmer_enable(void)
97 {
98         mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE0_BASE),
99                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
100         mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
101                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
102 #if CONFIG_SYS_PCIE_NR_PORTS > 2
103         mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
104                 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
105 #endif
106 }
107
108 static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
109         int offset, int len, u32 *val) {
110
111         u8 *address;
112         *val = 0;
113
114         if (validate_endpoint(hose))
115                 return 0;               /* No upstream config access */
116
117         /*
118          * Bus numbers are relative to hose->first_busno
119          */
120         devfn -= PCI_BDF(hose->first_busno, 0, 0);
121
122         /*
123          * NOTICE: configuration space ranges are currenlty mapped only for
124          * the first 16 buses, so such limit must be imposed. In case more
125          * buses are required the TLB settings in board/amcc/<board>/init.S
126          * need to be altered accordingly (one bus takes 1 MB of memory space).
127          */
128         if (PCI_BUS(devfn) >= 16)
129                 return 0;
130
131         /*
132          * Only single device/single function is supported for the primary and
133          * secondary buses of the 440SPe host bridge.
134          */
135         if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
136                 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
137                 return 0;
138
139         address = pcie_get_base(hose, devfn);
140         offset += devfn << 4;
141
142         /*
143          * Reading from configuration space of non-existing device can
144          * generate transaction errors. For the read duration we suppress
145          * assertion of machine check exceptions to avoid those.
146          */
147         pcie_dmer_disable ();
148
149         debug("%s: cfg_data=%08x offset=%08x\n", __func__, hose->cfg_data, offset);
150         switch (len) {
151         case 1:
152                 *val = in_8(hose->cfg_data + offset);
153                 break;
154         case 2:
155                 *val = in_le16((u16 *)(hose->cfg_data + offset));
156                 break;
157         default:
158                 *val = in_le32((u32*)(hose->cfg_data + offset));
159                 break;
160         }
161
162         pcie_dmer_enable ();
163
164         return 0;
165 }
166
167 static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
168         int offset, int len, u32 val) {
169
170         u8 *address;
171
172         if (validate_endpoint(hose))
173                 return 0;               /* No upstream config access */
174
175         /*
176          * Bus numbers are relative to hose->first_busno
177          */
178         devfn -= PCI_BDF(hose->first_busno, 0, 0);
179
180         /*
181          * Same constraints as in pcie_read_config().
182          */
183         if (PCI_BUS(devfn) >= 16)
184                 return 0;
185
186         if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
187                 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
188                 return 0;
189
190         address = pcie_get_base(hose, devfn);
191         offset += devfn << 4;
192
193         /*
194          * Suppress MCK exceptions, similar to pcie_read_config()
195          */
196         pcie_dmer_disable ();
197
198         switch (len) {
199         case 1:
200                 out_8(hose->cfg_data + offset, val);
201                 break;
202         case 2:
203                 out_le16((u16 *)(hose->cfg_data + offset), val);
204                 break;
205         default:
206                 out_le32((u32 *)(hose->cfg_data + offset), val);
207                 break;
208         }
209
210         pcie_dmer_enable ();
211
212         return 0;
213 }
214
215 int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
216 {
217         u32 v;
218         int rv;
219
220         rv = pcie_read_config(hose, dev, offset, 1, &v);
221         *val = (u8)v;
222         return rv;
223 }
224
225 int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
226 {
227         u32 v;
228         int rv;
229
230         rv = pcie_read_config(hose, dev, offset, 2, &v);
231         *val = (u16)v;
232         return rv;
233 }
234
235 int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
236 {
237         u32 v;
238         int rv;
239
240         rv = pcie_read_config(hose, dev, offset, 3, &v);
241         *val = (u32)v;
242         return rv;
243 }
244
245 int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
246 {
247         return pcie_write_config(hose,(u32)dev,offset,1,val);
248 }
249
250 int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
251 {
252         return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
253 }
254
255 int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
256 {
257         return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
258 }
259
260 #if defined(CONFIG_440SPE)
261 static void ppc4xx_setup_utl(u32 port) {
262
263         volatile void *utl_base = NULL;
264
265         /*
266          * Map UTL registers
267          */
268         switch (port) {
269         case 0:
270                 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
271                 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
272                 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
273                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
274                 break;
275
276         case 1:
277                 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
278                 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
279                 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
280                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
281                 break;
282
283         case 2:
284                 mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
285                 mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
286                 mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
287                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
288                 break;
289         }
290         utl_base = (unsigned int *)(CONFIG_SYS_PCIE_BASE + 0x1000 * port);
291
292         /*
293          * Set buffer allocations and then assert VRB and TXE.
294          */
295         out_be32(utl_base + PEUTL_OUTTR,   0x08000000);
296         out_be32(utl_base + PEUTL_INTR,    0x02000000);
297         out_be32(utl_base + PEUTL_OPDBSZ,  0x10000000);
298         out_be32(utl_base + PEUTL_PBBSZ,   0x53000000);
299         out_be32(utl_base + PEUTL_IPHBSZ,  0x08000000);
300         out_be32(utl_base + PEUTL_IPDBSZ,  0x10000000);
301         out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
302         out_be32(utl_base + PEUTL_PCTL,    0x80800066);
303 }
304
305 static int check_error(void)
306 {
307         u32 valPE0, valPE1, valPE2;
308         int err = 0;
309
310         /* SDR0_PEGPLLLCT1 reset */
311         if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000))
312                 printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
313
314         valPE0 = SDR_READ(PESDR0_RCSSET);
315         valPE1 = SDR_READ(PESDR1_RCSSET);
316         valPE2 = SDR_READ(PESDR2_RCSSET);
317
318         /* SDR0_PExRCSSET rstgu */
319         if (!(valPE0 & 0x01000000) ||
320             !(valPE1 & 0x01000000) ||
321             !(valPE2 & 0x01000000)) {
322                 printf("PCIE:  SDR0_PExRCSSET rstgu error\n");
323                 err = -1;
324         }
325
326         /* SDR0_PExRCSSET rstdl */
327         if (!(valPE0 & 0x00010000) ||
328             !(valPE1 & 0x00010000) ||
329             !(valPE2 & 0x00010000)) {
330                 printf("PCIE:  SDR0_PExRCSSET rstdl error\n");
331                 err = -1;
332         }
333
334         /* SDR0_PExRCSSET rstpyn */
335         if ((valPE0 & 0x00001000) ||
336             (valPE1 & 0x00001000) ||
337             (valPE2 & 0x00001000)) {
338                 printf("PCIE:  SDR0_PExRCSSET rstpyn error\n");
339                 err = -1;
340         }
341
342         /* SDR0_PExRCSSET hldplb */
343         if ((valPE0 & 0x10000000) ||
344             (valPE1 & 0x10000000) ||
345             (valPE2 & 0x10000000)) {
346                 printf("PCIE:  SDR0_PExRCSSET hldplb error\n");
347                 err = -1;
348         }
349
350         /* SDR0_PExRCSSET rdy */
351         if ((valPE0 & 0x00100000) ||
352             (valPE1 & 0x00100000) ||
353             (valPE2 & 0x00100000)) {
354                 printf("PCIE:  SDR0_PExRCSSET rdy error\n");
355                 err = -1;
356         }
357
358         /* SDR0_PExRCSSET shutdown */
359         if ((valPE0 & 0x00000100) ||
360             (valPE1 & 0x00000100) ||
361             (valPE2 & 0x00000100)) {
362                 printf("PCIE:  SDR0_PExRCSSET shutdown error\n");
363                 err = -1;
364         }
365         return err;
366 }
367
368 /*
369  * Initialize PCI Express core
370  */
371 int ppc4xx_init_pcie(void)
372 {
373         int time_out = 20;
374
375         /* Set PLL clock receiver to LVPECL */
376         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
377
378         if (check_error()) {
379                 printf("ERROR: failed to set PCIe reference clock receiver --"
380                         "PESDR0_PLLLCT1 = 0x%08x\n", SDR_READ(PESDR0_PLLLCT1));
381
382                 return -1;
383         }
384
385         /* Did resistance calibration work? */
386         if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000)) {
387                 printf("ERROR: PCIe resistance calibration failed --"
388                         "PESDR0_PLLLCT2 = 0x%08x\n", SDR_READ(PESDR0_PLLLCT2));
389
390                 return -1;
391         }
392         /* De-assert reset of PCIe PLL, wait for lock */
393         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
394         udelay(300);    /* 300 uS is maximum time lock should take */
395
396         while (time_out) {
397                 if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
398                         time_out--;
399                         udelay(20);     /* Wait 20 uS more if needed */
400                 } else
401                         break;
402         }
403         if (!time_out) {
404                 printf("ERROR: PCIe PLL VCO output not locked to ref clock --"
405                         "PESDR0_PLLLCTS=0x%08x\n", SDR_READ(PESDR0_PLLLCT3));
406
407                 return -1;
408         }
409         return 0;
410 }
411 #endif
412
413 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
414 static void ppc4xx_setup_utl(u32 port)
415 {
416         volatile void *utl_base = NULL;
417
418         /*
419          * Map UTL registers at 0x0801_n000 (4K 0xfff mask) PEGPLn_REGMSK
420          */
421         switch (port) {
422         case 0:
423                 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), U64_TO_U32_HIGH(CONFIG_SYS_PCIE0_UTLBASE));
424                 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), U64_TO_U32_LOW(CONFIG_SYS_PCIE0_UTLBASE));
425                 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);    /* BAM 11100000=4KB */
426                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
427                 break;
428
429         case 1:
430                 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), U64_TO_U32_HIGH(CONFIG_SYS_PCIE0_UTLBASE));
431                 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), U64_TO_U32_LOW(CONFIG_SYS_PCIE0_UTLBASE)
432                         + 0x1000);
433                 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);    /* BAM 11100000=4KB */
434                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
435                 break;
436         }
437         utl_base = (unsigned int *)(CONFIG_SYS_PCIE_BASE + 0x1000 * port);
438
439         /*
440          * Set buffer allocations and then assert VRB and TXE.
441          */
442         out_be32(utl_base + PEUTL_PBCTL, 0x0800000c);   /* PLBME, CRRE */
443         out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
444         out_be32(utl_base + PEUTL_INTR, 0x02000000);
445         out_be32(utl_base + PEUTL_OPDBSZ, 0x04000000);  /* OPD = 512 Bytes */
446         out_be32(utl_base + PEUTL_PBBSZ, 0x00000000);   /* Max 512 Bytes */
447         out_be32(utl_base + PEUTL_IPHBSZ, 0x02000000);
448         out_be32(utl_base + PEUTL_IPDBSZ, 0x04000000);  /* IPD = 512 Bytes */
449         out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
450         out_be32(utl_base + PEUTL_PCTL, 0x80800066);    /* VRB,TXE,timeout=default */
451 }
452
453 /*
454  * TODO: double check PCI express SDR based on the latest user manual
455  *               Some registers specified here no longer exist.. has to be
456  *               updated based on the final EAS spec.
457  */
458 static int check_error(void)
459 {
460         u32 valPE0, valPE1;
461         int err = 0;
462
463         valPE0 = SDR_READ(SDRN_PESDR_RCSSET(0));
464         valPE1 = SDR_READ(SDRN_PESDR_RCSSET(1));
465
466         /* SDR0_PExRCSSET rstgu */
467         if (!(valPE0 & PESDRx_RCSSET_RSTGU) || !(valPE1 & PESDRx_RCSSET_RSTGU)) {
468                 printf("PCIE:  SDR0_PExRCSSET rstgu error\n");
469                 err = -1;
470         }
471
472         /* SDR0_PExRCSSET rstdl */
473         if (!(valPE0 & PESDRx_RCSSET_RSTDL) || !(valPE1 & PESDRx_RCSSET_RSTDL)) {
474                 printf("PCIE:  SDR0_PExRCSSET rstdl error\n");
475                 err = -1;
476         }
477
478         /* SDR0_PExRCSSET rstpyn */
479         if ((valPE0 & PESDRx_RCSSET_RSTPYN) || (valPE1 & PESDRx_RCSSET_RSTPYN)) {
480                 printf("PCIE:  SDR0_PExRCSSET rstpyn error\n");
481                 err = -1;
482         }
483
484         /* SDR0_PExRCSSET hldplb */
485         if ((valPE0 & PESDRx_RCSSET_HLDPLB) || (valPE1 & PESDRx_RCSSET_HLDPLB)) {
486                 printf("PCIE:  SDR0_PExRCSSET hldplb error\n");
487                 err = -1;
488         }
489
490         /* SDR0_PExRCSSET rdy */
491         if ((valPE0 & PESDRx_RCSSET_RDY) || (valPE1 & PESDRx_RCSSET_RDY)) {
492                 printf("PCIE:  SDR0_PExRCSSET rdy error\n");
493                 err = -1;
494         }
495
496         return err;
497 }
498
499 /*
500  * Initialize PCI Express core as described in User Manual
501  * TODO: double check PE SDR PLL Register with the updated user manual.
502  */
503 int ppc4xx_init_pcie(void)
504 {
505         if (check_error())
506                 return -1;
507
508         return 0;
509 }
510 #endif /* CONFIG_460EX */
511
512 #if defined(CONFIG_405EX)
513 static void ppc4xx_setup_utl(u32 port)
514 {
515         u32 utl_base;
516
517         /*
518          * Map UTL registers at 0xef4f_n000 (4K 0xfff mask) PEGPLn_REGMSK
519          */
520         switch (port) {
521         case 0:
522                 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x00000000);
523                 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), CONFIG_SYS_PCIE0_UTLBASE);
524                 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001); /* 4k region, valid */
525                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
526                 break;
527
528         case 1:
529                 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x00000000);
530                 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), CONFIG_SYS_PCIE1_UTLBASE);
531                 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001); /* 4k region, valid */
532                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
533
534                 break;
535         }
536         utl_base = (port==0) ? CONFIG_SYS_PCIE0_UTLBASE : CONFIG_SYS_PCIE1_UTLBASE;
537
538         /*
539          * Set buffer allocations and then assert VRB and TXE.
540          */
541         out_be32((u32 *)(utl_base + PEUTL_OUTTR),   0x02000000);
542         out_be32((u32 *)(utl_base + PEUTL_INTR),    0x02000000);
543         out_be32((u32 *)(utl_base + PEUTL_OPDBSZ),  0x04000000);
544         out_be32((u32 *)(utl_base + PEUTL_PBBSZ),   0x21000000);
545         out_be32((u32 *)(utl_base + PEUTL_IPHBSZ),  0x02000000);
546         out_be32((u32 *)(utl_base + PEUTL_IPDBSZ),  0x04000000);
547         out_be32((u32 *)(utl_base + PEUTL_RCIRQEN), 0x00f00000);
548         out_be32((u32 *)(utl_base + PEUTL_PCTL),    0x80800066);
549
550         out_be32((u32 *)(utl_base + PEUTL_PBCTL),   0x0800000c);
551         out_be32((u32 *)(utl_base + PEUTL_RCSTA),
552                  in_be32((u32 *)(utl_base + PEUTL_RCSTA)) | 0x000040000);
553 }
554
555 int ppc4xx_init_pcie(void)
556 {
557         /*
558          * Nothing to do on 405EX
559          */
560         return 0;
561 }
562 #endif /* CONFIG_405EX */
563
564 /*
565  * Board-specific pcie initialization
566  * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
567  */
568
569 /*
570  * Initialize various parts of the PCI Express core for our port:
571  *
572  * - Set as a root port and enable max width
573  *   (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
574  * - Set up UTL configuration.
575  * - Increase SERDES drive strength to levels suggested by AMCC.
576  * - De-assert RSTPYN, RSTDL and RSTGU.
577  *
578  * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
579  * with default setting 0x11310000. The register has new fields,
580  * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
581  * hang.
582  */
583 #if defined(CONFIG_440SPE)
584 int __ppc4xx_init_pcie_port_hw(int port, int rootport)
585 {
586         u32 val = 1 << 24;
587         u32 utlset1;
588
589         if (rootport) {
590                 val = PTYPE_ROOT_PORT << 20;
591                 utlset1 = 0x21222222;
592         } else {
593                 val = PTYPE_LEGACY_ENDPOINT << 20;
594                 utlset1 = 0x20222222;
595         }
596
597         if (port == 0)
598                 val |= LNKW_X8 << 12;
599         else
600                 val |= LNKW_X4 << 12;
601
602         SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
603         SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
604         if (!ppc440spe_revB())
605                 SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
606         SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
607         SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
608         SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
609         SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
610         if (port == 0) {
611                 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
612                 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
613                 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
614                 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
615         }
616         SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
617                                             ~(1 << 24 | 1 << 16)) | 1 << 12);
618
619         return 0;
620 }
621 #endif /* CONFIG_440SPE */
622
623 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
624 int __ppc4xx_init_pcie_port_hw(int port, int rootport)
625 {
626         u32 val;
627         u32 utlset1;
628
629         if (rootport)
630                 val = PTYPE_ROOT_PORT << 20;
631         else
632                 val = PTYPE_LEGACY_ENDPOINT << 20;
633
634         if (port == 0) {
635                 val |= LNKW_X1 << 12;
636                 utlset1 = 0x20000000;
637         } else {
638                 val |= LNKW_X4 << 12;
639                 utlset1 = 0x20101101;
640         }
641
642         SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
643         SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
644         SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01210000);
645
646         switch (port) {
647         case 0:
648                 SDR_WRITE(PESDR0_L0CDRCTL, 0x00003230);
649                 SDR_WRITE(PESDR0_L0DRV, 0x00000130);
650                 SDR_WRITE(PESDR0_L0CLK, 0x00000006);
651
652                 SDR_WRITE(PESDR0_PHY_CTL_RST,0x10000000);
653                 break;
654
655         case 1:
656                 SDR_WRITE(PESDR1_L0CDRCTL, 0x00003230);
657                 SDR_WRITE(PESDR1_L1CDRCTL, 0x00003230);
658                 SDR_WRITE(PESDR1_L2CDRCTL, 0x00003230);
659                 SDR_WRITE(PESDR1_L3CDRCTL, 0x00003230);
660                 SDR_WRITE(PESDR1_L0DRV, 0x00000130);
661                 SDR_WRITE(PESDR1_L1DRV, 0x00000130);
662                 SDR_WRITE(PESDR1_L2DRV, 0x00000130);
663                 SDR_WRITE(PESDR1_L3DRV, 0x00000130);
664                 SDR_WRITE(PESDR1_L0CLK, 0x00000006);
665                 SDR_WRITE(PESDR1_L1CLK, 0x00000006);
666                 SDR_WRITE(PESDR1_L2CLK, 0x00000006);
667                 SDR_WRITE(PESDR1_L3CLK, 0x00000006);
668
669                 SDR_WRITE(PESDR1_PHY_CTL_RST,0x10000000);
670                 break;
671         }
672
673         SDR_WRITE(SDRN_PESDR_RCSSET(port), SDR_READ(SDRN_PESDR_RCSSET(port)) |
674                   (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
675
676         /* Poll for PHY reset */
677         switch (port) {
678         case 0:
679                 while (!(SDR_READ(PESDR0_RSTSTA) & 0x1))
680                         udelay(10);
681                 break;
682         case 1:
683                 while (!(SDR_READ(PESDR1_RSTSTA) & 0x1))
684                         udelay(10);
685                 break;
686         }
687
688         SDR_WRITE(SDRN_PESDR_RCSSET(port),
689                   (SDR_READ(SDRN_PESDR_RCSSET(port)) &
690                    ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
691                   PESDRx_RCSSET_RSTPYN);
692
693         return 0;
694 }
695 #endif /* CONFIG_440SPE */
696
697 #if defined(CONFIG_405EX)
698 int __ppc4xx_init_pcie_port_hw(int port, int rootport)
699 {
700         u32 val;
701
702         if (rootport)
703                 val = 0x00401000;
704         else
705                 val = 0x00101000;
706
707         SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
708         SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x00000000);
709         SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01010000);
710         SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
711         SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
712
713         /* Assert the PE0_PHY reset */
714         SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
715         udelay(1000);
716
717         /* deassert the PE0_hotreset */
718         if (is_end_point(port))
719                 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01111000);
720         else
721                 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
722
723         /* poll for phy !reset */
724         while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
725                 ;
726
727         /* deassert the PE0_gpl_utl_reset */
728         SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
729
730         if (port == 0)
731                 mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000);  /* guarded on */
732         else
733                 mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000);  /* guarded on */
734
735         return 0;
736 }
737 #endif /* CONFIG_405EX */
738
739 int ppc4xx_init_pcie_port_hw(int port, int rootport)
740 __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
741
742 /*
743  * We map PCI Express configuration access into the 512MB regions
744  *
745  * NOTICE: revB is very strict about PLB real addressess and ranges to
746  * be mapped for config space; it seems to only work with d_nnnn_nnnn
747  * range (hangs the core upon config transaction attempts when set
748  * otherwise) while revA uses c_nnnn_nnnn.
749  *
750  * For 440SPe revA:
751  *     PCIE0: 0xc_4000_0000
752  *     PCIE1: 0xc_8000_0000
753  *     PCIE2: 0xc_c000_0000
754  *
755  * For 440SPe revB:
756  *     PCIE0: 0xd_0000_0000
757  *     PCIE1: 0xd_2000_0000
758  *     PCIE2: 0xd_4000_0000
759  *
760  * For 405EX:
761  *     PCIE0: 0xa000_0000
762  *     PCIE1: 0xc000_0000
763  *
764  * For 460EX/GT:
765  *     PCIE0: 0xd_0000_0000
766  *     PCIE1: 0xd_2000_0000
767  */
768 static inline u64 ppc4xx_get_cfgaddr(int port)
769 {
770 #if defined(CONFIG_405EX)
771         if (port == 0)
772                 return (u64)CONFIG_SYS_PCIE0_CFGBASE;
773         else
774                 return (u64)CONFIG_SYS_PCIE1_CFGBASE;
775 #endif
776 #if defined(CONFIG_440SPE)
777         if (ppc440spe_revB()) {
778                 switch (port) {
779                 default:        /* to satisfy compiler */
780                 case 0:
781                         return 0x0000000d00000000ULL;
782                 case 1:
783                         return 0x0000000d20000000ULL;
784                 case 2:
785                         return 0x0000000d40000000ULL;
786                 }
787         } else {
788                 switch (port) {
789                 default:        /* to satisfy compiler */
790                 case 0:
791                         return 0x0000000c40000000ULL;
792                 case 1:
793                         return 0x0000000c80000000ULL;
794                 case 2:
795                         return 0x0000000cc0000000ULL;
796                 }
797         }
798 #endif
799 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
800         if (port == 0)
801                 return 0x0000000d00000000ULL;
802         else
803                 return 0x0000000d20000000ULL;
804 #endif
805 }
806
807 /*
808  *  4xx boards as end point and root point setup
809  *                    and
810  *    testing inbound and out bound windows
811  *
812  *  4xx boards can be plugged into another 4xx boards or you can get PCI-E
813  *  cable which can be used to setup loop back from one port to another port.
814  *  Please rememeber that unless there is a endpoint plugged in to root port it
815  *  will not initialize. It is the same in case of endpoint , unless there is
816  *  root port attached it will not initialize.
817  *
818  *  In this release of software all the PCI-E ports are configured as either
819  *  endpoint or rootpoint.In future we will have support for selective ports
820  *  setup as endpoint and root point in single board.
821  *
822  *  Once your board came up as root point , you can verify by reading
823  *  /proc/bus/pci/devices. Where you can see the configuration registers
824  *  of end point device attached to the port.
825  *
826  *  Enpoint cofiguration can be verified by connecting 4xx board to any
827  *  host or another 4xx board. Then try to scan the device. In case of
828  *  linux use "lspci" or appripriate os command.
829  *
830  *  How do I verify the inbound and out bound windows ? (4xx to 4xx)
831  *  in this configuration inbound and outbound windows are setup to access
832  *  sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
833  *  is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
834  *  This is waere your POM(PLB out bound memory window) mapped. then
835  *  read the data from other 4xx board's u-boot prompt at address
836  *  0x9000 0000(SRAM). Data should match.
837  *  In case of inbound , write data to u-boot command prompt at 0xb000 0000
838  *  which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
839  *  data at 0x9000 0000(SRAM).Data should match.
840  */
841 int ppc4xx_init_pcie_port(int port, int rootport)
842 {
843         static int core_init;
844         volatile u32 val = 0;
845         int attempts;
846         u64 addr;
847         u32 low, high;
848
849         if (!core_init) {
850                 if (ppc4xx_init_pcie())
851                         return -1;
852                 ++core_init;
853         }
854
855         /*
856          * Initialize various parts of the PCI Express core for our port
857          */
858         ppc4xx_init_pcie_port_hw(port, rootport);
859
860         /*
861          * Notice: the following delay has critical impact on device
862          * initialization - if too short (<50ms) the link doesn't get up.
863          */
864         mdelay(100);
865
866         val = SDR_READ(SDRN_PESDR_RCSSTS(port));
867         if (val & (1 << 20)) {
868                 printf("PCIE%d: PGRST failed %08x\n", port, val);
869                 return -1;
870         }
871
872         /*
873          * Verify link is up
874          */
875         val = SDR_READ(SDRN_PESDR_LOOP(port));
876         if (!(val & 0x00001000)) {
877                 printf("PCIE%d: link is not up.\n", port);
878                 return -ENODEV;
879         }
880
881         /*
882          * Setup UTL registers - but only on revA!
883          * We use default settings for revB chip.
884          */
885         if (!ppc440spe_revB())
886                 ppc4xx_setup_utl(port);
887
888         /*
889          * We map PCI Express configuration access into the 512MB regions
890          */
891         addr = ppc4xx_get_cfgaddr(port);
892         low = U64_TO_U32_LOW(addr);
893         high = U64_TO_U32_HIGH(addr);
894
895         switch (port) {
896         case 0:
897                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
898                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
899                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
900                 break;
901         case 1:
902                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
903                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
904                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
905                 break;
906 #if CONFIG_SYS_PCIE_NR_PORTS > 2
907         case 2:
908                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
909                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
910                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
911                 break;
912 #endif
913         }
914
915         /*
916          * Check for VC0 active and assert RDY.
917          */
918         attempts = 10;
919         while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 16))) {
920                 if (!(attempts--)) {
921                         printf("PCIE%d: VC0 not active\n", port);
922                         return -1;
923                 }
924                 mdelay(1000);
925         }
926         SDR_WRITE(SDRN_PESDR_RCSSET(port),
927                   SDR_READ(SDRN_PESDR_RCSSET(port)) | 1 << 20);
928         mdelay(100);
929
930         return 0;
931 }
932
933 int ppc4xx_init_pcie_rootport(int port)
934 {
935         return ppc4xx_init_pcie_port(port, 1);
936 }
937
938 int ppc4xx_init_pcie_endport(int port)
939 {
940         return ppc4xx_init_pcie_port(port, 0);
941 }
942
943 void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
944 {
945         volatile void *mbase = NULL;
946         volatile void *rmbase = NULL;
947
948         pci_set_ops(hose,
949                     pcie_read_config_byte,
950                     pcie_read_config_word,
951                     pcie_read_config_dword,
952                     pcie_write_config_byte,
953                     pcie_write_config_word,
954                     pcie_write_config_dword);
955
956         switch (port) {
957         case 0:
958                 mbase = (u32 *)CONFIG_SYS_PCIE0_XCFGBASE;
959                 rmbase = (u32 *)CONFIG_SYS_PCIE0_CFGBASE;
960                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE0_CFGBASE;
961                 break;
962         case 1:
963                 mbase = (u32 *)CONFIG_SYS_PCIE1_XCFGBASE;
964                 rmbase = (u32 *)CONFIG_SYS_PCIE1_CFGBASE;
965                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE1_CFGBASE;
966                 break;
967 #if CONFIG_SYS_PCIE_NR_PORTS > 2
968         case 2:
969                 mbase = (u32 *)CONFIG_SYS_PCIE2_XCFGBASE;
970                 rmbase = (u32 *)CONFIG_SYS_PCIE2_CFGBASE;
971                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE2_CFGBASE;
972                 break;
973 #endif
974         }
975
976         /*
977          * Set bus numbers on our root port
978          */
979         out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
980         out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
981         out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
982
983         /*
984          * Set up outbound translation to hose->mem_space from PLB
985          * addresses at an offset of 0xd_0000_0000.  We set the low
986          * bits of the mask to 11 to turn off splitting into 8
987          * subregions and to enable the outbound translation.
988          */
989         out_le32(mbase + PECFG_POM0LAH, 0x00000000);
990         out_le32(mbase + PECFG_POM0LAL, CONFIG_SYS_PCIE_MEMBASE +
991                  port * CONFIG_SYS_PCIE_MEMSIZE);
992         debug("PECFG_POM0LA=%08x.%08x\n", in_le32(mbase + PECFG_POM0LAH),
993               in_le32(mbase + PECFG_POM0LAL));
994
995         switch (port) {
996         case 0:
997                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CONFIG_SYS_PCIE_ADDR_HIGH);
998                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CONFIG_SYS_PCIE_MEMBASE +
999                       port * CONFIG_SYS_PCIE_MEMSIZE);
1000                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
1001                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
1002                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1003                 debug("0:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
1004                       mfdcr(DCRN_PEGPL_OMR1BAH(PCIE0)),
1005                       mfdcr(DCRN_PEGPL_OMR1BAL(PCIE0)),
1006                       mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE0)),
1007                       mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
1008                 break;
1009         case 1:
1010                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CONFIG_SYS_PCIE_ADDR_HIGH);
1011                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CONFIG_SYS_PCIE_MEMBASE +
1012                       port * CONFIG_SYS_PCIE_MEMSIZE);
1013                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
1014                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
1015                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1016                 debug("1:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
1017                       mfdcr(DCRN_PEGPL_OMR1BAH(PCIE1)),
1018                       mfdcr(DCRN_PEGPL_OMR1BAL(PCIE1)),
1019                       mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
1020                       mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
1021                 break;
1022 #if CONFIG_SYS_PCIE_NR_PORTS > 2
1023         case 2:
1024                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CONFIG_SYS_PCIE_ADDR_HIGH);
1025                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CONFIG_SYS_PCIE_MEMBASE +
1026                       port * CONFIG_SYS_PCIE_MEMSIZE);
1027                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
1028                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
1029                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1030                 debug("2:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
1031                       mfdcr(DCRN_PEGPL_OMR1BAH(PCIE2)),
1032                       mfdcr(DCRN_PEGPL_OMR1BAL(PCIE2)),
1033                       mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
1034                       mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
1035                 break;
1036 #endif
1037         }
1038
1039         /* Set up 4GB inbound memory window at 0 */
1040         out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
1041         out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
1042         out_le32(mbase + PECFG_BAR0HMPA, 0x7ffffff);
1043         out_le32(mbase + PECFG_BAR0LMPA, 0);
1044
1045         out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1046         out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1047         out_le32(mbase + PECFG_PIM0LAL, 0);
1048         out_le32(mbase + PECFG_PIM0LAH, 0);
1049         out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1050         out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
1051         out_le32(mbase + PECFG_PIMEN, 0x1);
1052
1053         /* Enable I/O, Mem, and Busmaster cycles */
1054         out_le16((u16 *)(mbase + PCI_COMMAND),
1055                  in_le16((u16 *)(mbase + PCI_COMMAND)) |
1056                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1057
1058         /* Set Device and Vendor Id */
1059         out_le16(mbase + 0x200, 0xaaa0 + port);
1060         out_le16(mbase + 0x202, 0xbed0 + port);
1061
1062         /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1063         out_le32(mbase + 0x208, 0x06040001);
1064
1065         printf("PCIE%d: successfully set as root-complex\n", port);
1066 }
1067
1068 int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
1069 {
1070         volatile void *mbase = NULL;
1071         int attempts = 0;
1072
1073         pci_set_ops(hose,
1074                     pcie_read_config_byte,
1075                     pcie_read_config_word,
1076                     pcie_read_config_dword,
1077                     pcie_write_config_byte,
1078                     pcie_write_config_word,
1079                     pcie_write_config_dword);
1080
1081         switch (port) {
1082         case 0:
1083                 mbase = (u32 *)CONFIG_SYS_PCIE0_XCFGBASE;
1084                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE0_CFGBASE;
1085                 break;
1086         case 1:
1087                 mbase = (u32 *)CONFIG_SYS_PCIE1_XCFGBASE;
1088                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE1_CFGBASE;
1089                 break;
1090 #if defined(CONFIG_SYS_PCIE2_CFGBASE)
1091         case 2:
1092                 mbase = (u32 *)CONFIG_SYS_PCIE2_XCFGBASE;
1093                 hose->cfg_data = (u8 *)CONFIG_SYS_PCIE2_CFGBASE;
1094                 break;
1095 #endif
1096         }
1097
1098         /*
1099          * Set up outbound translation to hose->mem_space from PLB
1100          * addresses at an offset of 0xd_0000_0000.  We set the low
1101          * bits of the mask to 11 to turn off splitting into 8
1102          * subregions and to enable the outbound translation.
1103          */
1104         out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
1105         out_le32(mbase + PECFG_POM0LAL, 0x00001000);
1106
1107         switch (port) {
1108         case 0:
1109                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CONFIG_SYS_PCIE_ADDR_HIGH);
1110                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CONFIG_SYS_PCIE_MEMBASE +
1111                       port * CONFIG_SYS_PCIE_MEMSIZE);
1112                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
1113                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
1114                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1115                 break;
1116         case 1:
1117                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CONFIG_SYS_PCIE_ADDR_HIGH);
1118                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CONFIG_SYS_PCIE_MEMBASE +
1119                       port * CONFIG_SYS_PCIE_MEMSIZE);
1120                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
1121                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
1122                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1123                 break;
1124 #if CONFIG_SYS_PCIE_NR_PORTS > 2
1125         case 2:
1126                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CONFIG_SYS_PCIE_ADDR_HIGH);
1127                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CONFIG_SYS_PCIE_MEMBASE +
1128                       port * CONFIG_SYS_PCIE_MEMSIZE);
1129                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
1130                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
1131                       ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
1132                 break;
1133 #endif
1134         }
1135
1136         /* Set up 64MB inbound memory window at 0 */
1137         out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
1138         out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
1139
1140         out_le32(mbase + PECFG_PIM01SAH, 0xffffffff);
1141         out_le32(mbase + PECFG_PIM01SAL, 0xfc000000);
1142
1143         /* Setup BAR0 */
1144         out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffff);
1145         out_le32(mbase + PECFG_BAR0LMPA, 0xfc000000 | PCI_BASE_ADDRESS_MEM_TYPE_64);
1146
1147         /* Disable BAR1 & BAR2 */
1148         out_le32(mbase + PECFG_BAR1MPA, 0);
1149         out_le32(mbase + PECFG_BAR2HMPA, 0);
1150         out_le32(mbase + PECFG_BAR2LMPA, 0);
1151
1152         out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CONFIG_SYS_PCIE_INBOUND_BASE));
1153         out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CONFIG_SYS_PCIE_INBOUND_BASE));
1154         out_le32(mbase + PECFG_PIMEN, 0x1);
1155
1156         /* Enable I/O, Mem, and Busmaster cycles */
1157         out_le16((u16 *)(mbase + PCI_COMMAND),
1158                  in_le16((u16 *)(mbase + PCI_COMMAND)) |
1159                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1160         out_le16(mbase + 0x200, 0xcaad);                /* Setting vendor ID */
1161         out_le16(mbase + 0x202, 0xfeed);                /* Setting device ID */
1162
1163         /* Set Class Code to Processor/PPC */
1164         out_le32(mbase + 0x208, 0x0b200001);
1165
1166         attempts = 10;
1167         while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
1168                 if (!(attempts--)) {
1169                         printf("PCIE%d: BME not active\n", port);
1170                         return -1;
1171                 }
1172                 mdelay(1000);
1173         }
1174
1175         printf("PCIE%d: successfully set as endpoint\n", port);
1176
1177         return 0;
1178 }
1179 #endif /* CONFIG_440SPE && CONFIG_PCI */