Merge with git://www.denx.de/git/u-boot.git
[platform/kernel/u-boot.git] / cpu / ppc4xx / 440spe_pcie.c
1 /*
2  * (C) Copyright 2006 - 2007
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 #include <asm/processor.h>
24 #include <asm-ppc/io.h>
25 #include <ppc4xx.h>
26 #include <common.h>
27 #include <pci.h>
28
29 #if defined(CONFIG_440SPE) && defined(CONFIG_PCI)
30
31 #include "440spe_pcie.h"
32
33 enum {
34         PTYPE_ENDPOINT          = 0x0,
35         PTYPE_LEGACY_ENDPOINT   = 0x1,
36         PTYPE_ROOT_PORT         = 0x4,
37
38         LNKW_X1                 = 0x1,
39         LNKW_X4                 = 0x4,
40         LNKW_X8                 = 0x8
41 };
42
43 static inline int pcie_in_8(const volatile unsigned char __iomem *addr)
44 {
45         int ret;
46
47         PCIE_IN(lbzx, ret, addr);
48
49         return ret;
50 }
51
52 static inline int pcie_in_le16(const volatile unsigned short __iomem *addr)
53 {
54         int ret;
55
56         PCIE_IN(lhbrx, ret, addr)
57
58         return ret;
59 }
60
61 static inline unsigned pcie_in_le32(const volatile unsigned __iomem *addr)
62 {
63         unsigned ret;
64
65         PCIE_IN(lwbrx, ret, addr);
66
67         return ret;
68 }
69
70
71 static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
72         int offset, int len, u32 *val) {
73
74         *val = 0;
75         /*
76          * 440SPE implements only one function per port
77          */
78         if (!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 1)))
79                 return 0;
80
81         devfn = PCI_BDF(0,0,0);
82         offset += devfn << 4;
83
84         switch (len) {
85         case 1:
86                 *val = pcie_in_8(hose->cfg_data + offset);
87                 break;
88         case 2:
89                 *val = pcie_in_le16((u16 *)(hose->cfg_data + offset));
90                 break;
91         default:
92                 *val = pcie_in_le32((u32*)(hose->cfg_data + offset));
93                 break;
94         }
95         return 0;
96 }
97
98 static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
99         int offset, int len, u32 val) {
100
101         /*
102          * 440SPE implements only one function per port
103          */
104         if (!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 1)))
105                 return 0;
106
107         devfn = PCI_BDF(0,0,0);
108         offset += devfn << 4;
109
110         switch (len) {
111         case 1:
112                 out_8(hose->cfg_data + offset, val);
113                 break;
114         case 2:
115                 out_le16((u16 *)(hose->cfg_data + offset), val);
116                 break;
117         default:
118                 out_le32((u32 *)(hose->cfg_data + offset), val);
119                 break;
120         }
121         return 0;
122 }
123
124 int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
125 {
126         u32 v;
127         int rv;
128
129         rv =  pcie_read_config(hose, dev, offset, 1, &v);
130         *val = (u8)v;
131         return rv;
132 }
133
134 int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
135 {
136         u32 v;
137         int rv;
138
139         rv = pcie_read_config(hose, dev, offset, 2, &v);
140         *val = (u16)v;
141         return rv;
142 }
143
144 int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
145 {
146         u32 v;
147         int rv;
148
149         rv = pcie_read_config(hose, dev, offset, 3, &v);
150         *val = (u32)v;
151         return rv;
152 }
153
154 int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
155 {
156         return pcie_write_config(hose,(u32)dev,offset,1,val);
157 }
158
159 int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
160 {
161         return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
162 }
163
164 int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
165 {
166         return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
167 }
168
169 static void ppc440spe_setup_utl(u32 port) {
170
171         volatile void *utl_base = NULL;
172
173         /*
174          * Map UTL registers
175          */
176         switch (port) {
177         case 0:
178                 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
179                 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
180                 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
181                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
182                 break;
183
184         case 1:
185                 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
186                 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
187                 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
188                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
189                 break;
190
191         case 2:
192                 mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
193                 mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
194                 mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
195                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
196                 break;
197         }
198         utl_base = (unsigned int *)(CFG_PCIE_BASE + 0x1000 * port);
199
200         /*
201          * Set buffer allocations and then assert VRB and TXE.
202          */
203         out_be32(utl_base + PEUTL_OUTTR,   0x08000000);
204         out_be32(utl_base + PEUTL_INTR,    0x02000000);
205         out_be32(utl_base + PEUTL_OPDBSZ,  0x10000000);
206         out_be32(utl_base + PEUTL_PBBSZ,   0x53000000);
207         out_be32(utl_base + PEUTL_IPHBSZ,  0x08000000);
208         out_be32(utl_base + PEUTL_IPDBSZ,  0x10000000);
209         out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
210         out_be32(utl_base + PEUTL_PCTL,    0x80800066);
211 }
212
213 static int check_error(void)
214 {
215         u32 valPE0, valPE1, valPE2;
216         int err = 0;
217
218         /* SDR0_PEGPLLLCT1 reset */
219         if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
220                 printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
221         }
222
223         valPE0 = SDR_READ(PESDR0_RCSSET);
224         valPE1 = SDR_READ(PESDR1_RCSSET);
225         valPE2 = SDR_READ(PESDR2_RCSSET);
226
227         /* SDR0_PExRCSSET rstgu */
228         if (!(valPE0 & 0x01000000) ||
229             !(valPE1 & 0x01000000) ||
230             !(valPE2 & 0x01000000)) {
231                 printf("PCIE:  SDR0_PExRCSSET rstgu error\n");
232                 err = -1;
233         }
234
235         /* SDR0_PExRCSSET rstdl */
236         if (!(valPE0 & 0x00010000) ||
237             !(valPE1 & 0x00010000) ||
238             !(valPE2 & 0x00010000)) {
239                 printf("PCIE:  SDR0_PExRCSSET rstdl error\n");
240                 err = -1;
241         }
242
243         /* SDR0_PExRCSSET rstpyn */
244         if ((valPE0 & 0x00001000) ||
245             (valPE1 & 0x00001000) ||
246             (valPE2 & 0x00001000)) {
247                 printf("PCIE:  SDR0_PExRCSSET rstpyn error\n");
248                 err = -1;
249         }
250
251         /* SDR0_PExRCSSET hldplb */
252         if ((valPE0 & 0x10000000) ||
253             (valPE1 & 0x10000000) ||
254             (valPE2 & 0x10000000)) {
255                 printf("PCIE:  SDR0_PExRCSSET hldplb error\n");
256                 err = -1;
257         }
258
259         /* SDR0_PExRCSSET rdy */
260         if ((valPE0 & 0x00100000) ||
261             (valPE1 & 0x00100000) ||
262             (valPE2 & 0x00100000)) {
263                 printf("PCIE:  SDR0_PExRCSSET rdy error\n");
264                 err = -1;
265         }
266
267         /* SDR0_PExRCSSET shutdown */
268         if ((valPE0 & 0x00000100) ||
269             (valPE1 & 0x00000100) ||
270             (valPE2 & 0x00000100)) {
271                 printf("PCIE:  SDR0_PExRCSSET shutdown error\n");
272                 err = -1;
273         }
274         return err;
275 }
276
277 /*
278  * Initialize PCI Express core
279  */
280 int ppc440spe_init_pcie(void)
281 {
282         int time_out = 20;
283
284         /* Set PLL clock receiver to LVPECL */
285         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
286
287         if (check_error())
288                 return -1;
289
290         if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
291         {
292                 printf("PCIE: PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
293                        SDR_READ(PESDR0_PLLLCT2));
294                 return -1;
295         }
296         /* De-assert reset of PCIe PLL, wait for lock */
297         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
298         udelay(3);
299
300         while (time_out) {
301                 if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
302                         time_out--;
303                         udelay(1);
304                 } else
305                         break;
306         }
307         if (!time_out) {
308                 printf("PCIE: VCO output not locked\n");
309                 return -1;
310         }
311         return 0;
312 }
313
314 /*
315  *  Yucca board as End point and root point setup
316  *                    and
317  *    testing inbound and out bound windows
318  *
319  *  YUCCA board can be plugged into another yucca board or you can get PCI-E
320  *  cable which can be used to setup loop back from one port to another port.
321  *  Please rememeber that unless there is a endpoint plugged in to root port it
322  *  will not initialize. It is the same in case of endpoint , unless there is
323  *  root port attached it will not initialize.
324  *
325  *  In this release of software all the PCI-E ports are configured as either
326  *  endpoint or rootpoint.In future we will have support for selective ports
327  *  setup as endpoint and root point in single board.
328  *
329  *  Once your board came up as root point , you can verify by reading
330  *  /proc/bus/pci/devices. Where you can see the configuration registers
331  *  of end point device attached to the port.
332  *
333  *  Enpoint cofiguration can be verified by connecting Yucca board to any
334  *  host or another yucca board. Then try to scan the device. In case of
335  *  linux use "lspci" or appripriate os command.
336  *
337  *  How do I verify the inbound and out bound windows ?(yucca to yucca)
338  *  in this configuration inbound and outbound windows are setup to access
339  *  sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
340  *  is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
341  *  This is waere your POM(PLB out bound memory window) mapped. then
342  *  read the data from other yucca board's u-boot prompt at address
343  *  0x9000 0000(SRAM). Data should match.
344  *  In case of inbound , write data to u-boot command prompt at 0xb000 0000
345  *  which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
346  *  data at 0x9000 0000(SRAM).Data should match.
347  */
348 int ppc440spe_init_pcie_rootport(int port)
349 {
350         static int core_init;
351         volatile u32 val = 0;
352         int attempts;
353
354         if (!core_init) {
355                 ++core_init;
356                 if (ppc440spe_init_pcie())
357                         return -1;
358         }
359
360         /*
361          * Initialize various parts of the PCI Express core for our port:
362          *
363          * - Set as a root port and enable max width
364          *   (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
365          * - Set up UTL configuration.
366          * - Increase SERDES drive strength to levels suggested by AMCC.
367          * - De-assert RSTPYN, RSTDL and RSTGU.
368          *
369          * NOTICE for revB chip: PESDRn_UTLSET2 is not set - we leave it with
370          * default setting 0x11310000. The register has new fields,
371          * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
372          * hang.
373          */
374         switch (port) {
375         case 0:
376                 SDR_WRITE(PESDR0_DLPSET,  1 << 24 | PTYPE_ROOT_PORT << 20 | LNKW_X8 << 12);
377
378                 SDR_WRITE(PESDR0_UTLSET1, 0x21222222);
379                 if (!ppc440spe_revB())
380                         SDR_WRITE(PESDR0_UTLSET2, 0x11000000);
381                 SDR_WRITE(PESDR0_HSSL0SET1, 0x35000000);
382                 SDR_WRITE(PESDR0_HSSL1SET1, 0x35000000);
383                 SDR_WRITE(PESDR0_HSSL2SET1, 0x35000000);
384                 SDR_WRITE(PESDR0_HSSL3SET1, 0x35000000);
385                 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
386                 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
387                 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
388                 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
389                 SDR_WRITE(PESDR0_RCSSET,
390                           (SDR_READ(PESDR0_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
391                 break;
392
393         case 1:
394                 SDR_WRITE(PESDR1_DLPSET, 1 << 24 | PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
395                 SDR_WRITE(PESDR1_UTLSET1, 0x21222222);
396                 if (!ppc440spe_revB())
397                         SDR_WRITE(PESDR1_UTLSET2, 0x11000000);
398                 SDR_WRITE(PESDR1_HSSL0SET1, 0x35000000);
399                 SDR_WRITE(PESDR1_HSSL1SET1, 0x35000000);
400                 SDR_WRITE(PESDR1_HSSL2SET1, 0x35000000);
401                 SDR_WRITE(PESDR1_HSSL3SET1, 0x35000000);
402                 SDR_WRITE(PESDR1_RCSSET,
403                           (SDR_READ(PESDR1_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
404                 break;
405
406         case 2:
407                 SDR_WRITE(PESDR2_DLPSET, 1 << 24 | PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
408                 SDR_WRITE(PESDR2_UTLSET1, 0x21222222);
409                 if (!ppc440spe_revB())
410                         SDR_WRITE(PESDR2_UTLSET2, 0x11000000);
411                 SDR_WRITE(PESDR2_HSSL0SET1, 0x35000000);
412                 SDR_WRITE(PESDR2_HSSL1SET1, 0x35000000);
413                 SDR_WRITE(PESDR2_HSSL2SET1, 0x35000000);
414                 SDR_WRITE(PESDR2_HSSL3SET1, 0x35000000);
415                 SDR_WRITE(PESDR2_RCSSET,
416                           (SDR_READ(PESDR2_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
417                 break;
418         }
419         /*
420          * Notice: the following delay has critical impact on device
421          * initialization - if too short (<50ms) the link doesn't get up.
422          */
423         mdelay(100);
424
425         switch (port) {
426         case 0:
427                 val = SDR_READ(PESDR0_RCSSTS);
428                 break;
429         case 1:
430                 val = SDR_READ(PESDR1_RCSSTS);
431                 break;
432         case 2:
433                 val = SDR_READ(PESDR2_RCSSTS);
434                 break;
435         }
436
437         if (val & (1 << 20)) {
438                 printf("PCIE%d: PGRST failed %08x\n", port, val);
439                 return -1;
440         }
441
442         /*
443          * Verify link is up
444          */
445         val = 0;
446         switch (port) {
447         case 0:
448                 val = SDR_READ(PESDR0_LOOP);
449                 break;
450         case 1:
451                 val = SDR_READ(PESDR1_LOOP);
452                 break;
453         case 2:
454                 val = SDR_READ(PESDR2_LOOP);
455                 break;
456         }
457         if (!(val & 0x00001000)) {
458                 printf("PCIE%d: link is not up.\n", port);
459                 return -1;
460         }
461
462         /*
463          * Setup UTL registers - but only on revA!
464          * We use default settings for revB chip.
465          */
466         if (!ppc440spe_revB())
467                 ppc440spe_setup_utl(port);
468
469         /*
470          * We map PCI Express configuration access into the 512MB regions
471          *
472          * NOTICE: revB is very strict about PLB real addressess and ranges to
473          * be mapped for config space; it seems to only work with d_nnnn_nnnn
474          * range (hangs the core upon config transaction attempts when set
475          * otherwise) while revA uses c_nnnn_nnnn.
476          *
477          * For revA:
478          *     PCIE0: 0xc_4000_0000
479          *     PCIE1: 0xc_8000_0000
480          *     PCIE2: 0xc_c000_0000
481          *
482          * For revB:
483          *     PCIE0: 0xd_0000_0000
484          *     PCIE1: 0xd_2000_0000
485          *     PCIE2: 0xd_4000_0000
486          */
487
488         switch (port) {
489         case 0:
490                 if (ppc440spe_revB()) {
491                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000d);
492                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x00000000);
493                 } else {
494                         /* revA */
495                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000c);
496                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x40000000);
497                 }
498                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
499                 break;
500
501         case 1:
502                 if (ppc440spe_revB()) {
503                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000d);
504                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x20000000);
505                 } else {
506                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000c);
507                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x80000000);
508                 }
509                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
510                 break;
511
512         case 2:
513                 if (ppc440spe_revB()) {
514                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000d);
515                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0x40000000);
516                 } else {
517                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000c);
518                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0xc0000000);
519                 }
520                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
521                 break;
522         }
523
524         /*
525          * Check for VC0 active and assert RDY.
526          */
527         attempts = 10;
528         switch (port) {
529         case 0:
530                 while(!(SDR_READ(PESDR0_RCSSTS) & (1 << 16))) {
531                         if (!(attempts--)) {
532                                 printf("PCIE0: VC0 not active\n");
533                                 return -1;
534                         }
535                         mdelay(1000);
536                 }
537                 SDR_WRITE(PESDR0_RCSSET, SDR_READ(PESDR0_RCSSET) | 1 << 20);
538                 break;
539         case 1:
540                 while(!(SDR_READ(PESDR1_RCSSTS) & (1 << 16))) {
541                         if (!(attempts--)) {
542                                 printf("PCIE1: VC0 not active\n");
543                                 return -1;
544                         }
545                         mdelay(1000);
546                 }
547
548                 SDR_WRITE(PESDR1_RCSSET, SDR_READ(PESDR1_RCSSET) | 1 << 20);
549                 break;
550         case 2:
551                 while(!(SDR_READ(PESDR2_RCSSTS) & (1 << 16))) {
552                         if (!(attempts--)) {
553                                 printf("PCIE2: VC0 not active\n");
554                                 return -1;
555                         }
556                         mdelay(1000);
557                 }
558
559                 SDR_WRITE(PESDR2_RCSSET, SDR_READ(PESDR2_RCSSET) | 1 << 20);
560                 break;
561         }
562         mdelay(100);
563
564         return 0;
565 }
566
567 int ppc440spe_init_pcie_endport(int port)
568 {
569         static int core_init;
570         volatile u32 val = 0;
571         int attempts;
572
573         if (!core_init) {
574                 ++core_init;
575                 if (ppc440spe_init_pcie())
576                         return -1;
577         }
578
579         /*
580          * Initialize various parts of the PCI Express core for our port:
581          *
582          * - Set as a end port and enable max width
583          *   (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
584          * - Set up UTL configuration.
585          * - Increase SERDES drive strength to levels suggested by AMCC.
586          * - De-assert RSTPYN, RSTDL and RSTGU.
587          *
588          * NOTICE for revB chip: PESDRn_UTLSET2 is not set - we leave it with
589          * default setting 0x11310000. The register has new fields,
590          * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
591          * hang.
592          */
593         switch (port) {
594         case 0:
595                 SDR_WRITE(PESDR0_DLPSET,  1 << 24 | PTYPE_LEGACY_ENDPOINT << 20 | LNKW_X8 << 12);
596
597                 SDR_WRITE(PESDR0_UTLSET1, 0x20222222);
598                 if (!ppc440spe_revB())
599                         SDR_WRITE(PESDR0_UTLSET2, 0x11000000);
600                 SDR_WRITE(PESDR0_HSSL0SET1, 0x35000000);
601                 SDR_WRITE(PESDR0_HSSL1SET1, 0x35000000);
602                 SDR_WRITE(PESDR0_HSSL2SET1, 0x35000000);
603                 SDR_WRITE(PESDR0_HSSL3SET1, 0x35000000);
604                 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
605                 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
606                 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
607                 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
608                 SDR_WRITE(PESDR0_RCSSET,
609                         (SDR_READ(PESDR0_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
610                 break;
611
612         case 1:
613                 SDR_WRITE(PESDR1_DLPSET, 1 << 24 | PTYPE_LEGACY_ENDPOINT << 20 | LNKW_X4 << 12);
614                 SDR_WRITE(PESDR1_UTLSET1, 0x20222222);
615                 if (!ppc440spe_revB())
616                         SDR_WRITE(PESDR1_UTLSET2, 0x11000000);
617                 SDR_WRITE(PESDR1_HSSL0SET1, 0x35000000);
618                 SDR_WRITE(PESDR1_HSSL1SET1, 0x35000000);
619                 SDR_WRITE(PESDR1_HSSL2SET1, 0x35000000);
620                 SDR_WRITE(PESDR1_HSSL3SET1, 0x35000000);
621                 SDR_WRITE(PESDR1_RCSSET,
622                         (SDR_READ(PESDR1_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
623                 break;
624
625         case 2:
626                 SDR_WRITE(PESDR2_DLPSET, 1 << 24 | PTYPE_LEGACY_ENDPOINT << 20 | LNKW_X4 << 12);
627                 SDR_WRITE(PESDR2_UTLSET1, 0x20222222);
628                 if (!ppc440spe_revB())
629                         SDR_WRITE(PESDR2_UTLSET2, 0x11000000);
630                 SDR_WRITE(PESDR2_HSSL0SET1, 0x35000000);
631                 SDR_WRITE(PESDR2_HSSL1SET1, 0x35000000);
632                 SDR_WRITE(PESDR2_HSSL2SET1, 0x35000000);
633                 SDR_WRITE(PESDR2_HSSL3SET1, 0x35000000);
634                 SDR_WRITE(PESDR2_RCSSET,
635                         (SDR_READ(PESDR2_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
636                 break;
637         }
638         /*
639          * Notice: the following delay has critical impact on device
640          * initialization - if too short (<50ms) the link doesn't get up.
641          */
642         mdelay(100);
643
644         switch (port) {
645         case 0: val = SDR_READ(PESDR0_RCSSTS); break;
646         case 1: val = SDR_READ(PESDR1_RCSSTS); break;
647         case 2: val = SDR_READ(PESDR2_RCSSTS); break;
648         }
649
650         if (val & (1 << 20)) {
651                 printf("PCIE%d: PGRST failed %08x\n", port, val);
652                 return -1;
653         }
654
655         /*
656          * Verify link is up
657          */
658         val = 0;
659         switch (port)
660         {
661                 case 0:
662                         val = SDR_READ(PESDR0_LOOP);
663                         break;
664                 case 1:
665                         val = SDR_READ(PESDR1_LOOP);
666                         break;
667                 case 2:
668                         val = SDR_READ(PESDR2_LOOP);
669                         break;
670         }
671         if (!(val & 0x00001000)) {
672                 printf("PCIE%d: link is not up.\n", port);
673                 return -1;
674         }
675
676         /*
677          * Setup UTL registers - but only on revA!
678          * We use default settings for revB chip.
679          */
680         if (!ppc440spe_revB())
681                 ppc440spe_setup_utl(port);
682
683         /*
684          * We map PCI Express configuration access into the 512MB regions
685          *
686          * NOTICE: revB is very strict about PLB real addressess and ranges to
687          * be mapped for config space; it seems to only work with d_nnnn_nnnn
688          * range (hangs the core upon config transaction attempts when set
689          * otherwise) while revA uses c_nnnn_nnnn.
690          *
691          * For revA:
692          *     PCIE0: 0xc_4000_0000
693          *     PCIE1: 0xc_8000_0000
694          *     PCIE2: 0xc_c000_0000
695          *
696          * For revB:
697          *     PCIE0: 0xd_0000_0000
698          *     PCIE1: 0xd_2000_0000
699          *     PCIE2: 0xd_4000_0000
700          */
701         switch (port) {
702         case 0:
703                 if (ppc440spe_revB()) {
704                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000d);
705                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x00000000);
706                 } else {
707                         /* revA */
708                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000c);
709                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x40000000);
710                 }
711                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
712                 break;
713
714         case 1:
715                 if (ppc440spe_revB()) {
716                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000d);
717                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x20000000);
718                 } else {
719                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000c);
720                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x80000000);
721                 }
722                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
723                 break;
724
725         case 2:
726                 if (ppc440spe_revB()) {
727                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000d);
728                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0x40000000);
729                 } else {
730                         mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000c);
731                         mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0xc0000000);
732                 }
733                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
734                 break;
735         }
736
737         /*
738          * Check for VC0 active and assert RDY.
739          */
740         attempts = 10;
741         switch (port) {
742         case 0:
743                 while(!(SDR_READ(PESDR0_RCSSTS) & (1 << 16))) {
744                         if (!(attempts--)) {
745                                 printf("PCIE0: VC0 not active\n");
746                                 return -1;
747                         }
748                         mdelay(1000);
749                 }
750                 SDR_WRITE(PESDR0_RCSSET, SDR_READ(PESDR0_RCSSET) | 1 << 20);
751                 break;
752         case 1:
753                 while(!(SDR_READ(PESDR1_RCSSTS) & (1 << 16))) {
754                         if (!(attempts--)) {
755                                 printf("PCIE1: VC0 not active\n");
756                                 return -1;
757                         }
758                         mdelay(1000);
759                 }
760
761                 SDR_WRITE(PESDR1_RCSSET, SDR_READ(PESDR1_RCSSET) | 1 << 20);
762                 break;
763         case 2:
764                 while(!(SDR_READ(PESDR2_RCSSTS) & (1 << 16))) {
765                         if (!(attempts--)) {
766                                 printf("PCIE2: VC0 not active\n");
767                                 return -1;
768                         }
769                         mdelay(1000);
770                 }
771
772                 SDR_WRITE(PESDR2_RCSSET, SDR_READ(PESDR2_RCSSET) | 1 << 20);
773                 break;
774         }
775         mdelay(100);
776
777         return 0;
778 }
779
780 void ppc440spe_setup_pcie_rootpoint(struct pci_controller *hose, int port)
781 {
782         volatile void *mbase = NULL;
783         volatile void *rmbase = NULL;
784
785         pci_set_ops(hose,
786                     pcie_read_config_byte,
787                     pcie_read_config_word,
788                     pcie_read_config_dword,
789                     pcie_write_config_byte,
790                     pcie_write_config_word,
791                     pcie_write_config_dword);
792
793         switch (port) {
794         case 0:
795                 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
796                 rmbase = (u32 *)CFG_PCIE0_CFGBASE;
797                 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
798                 break;
799         case 1:
800                 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
801                 rmbase = (u32 *)CFG_PCIE1_CFGBASE;
802                 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
803                 break;
804         case 2:
805                 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
806                 rmbase = (u32 *)CFG_PCIE2_CFGBASE;
807                 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
808                 break;
809         }
810
811         /*
812          * Set bus numbers on our root port
813          */
814         if (ppc440spe_revB()) {
815                 out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
816                 out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
817                 out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
818         } else {
819                 out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
820                 out_8((u8 *)mbase + PCI_SECONDARY_BUS, 0);
821         }
822
823         /*
824          * Set up outbound translation to hose->mem_space from PLB
825          * addresses at an offset of 0xd_0000_0000.  We set the low
826          * bits of the mask to 11 to turn off splitting into 8
827          * subregions and to enable the outbound translation.
828          */
829         out_le32(mbase + PECFG_POM0LAH, 0x00000000);
830         out_le32(mbase + PECFG_POM0LAL, 0x00000000);
831
832         switch (port) {
833         case 0:
834                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0),  0x0000000d);
835                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0),  CFG_PCIE_MEMBASE +
836                         port * CFG_PCIE_MEMSIZE);
837                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
838                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
839                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
840                 break;
841         case 1:
842                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1),  0x0000000d);
843                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1),  (CFG_PCIE_MEMBASE +
844                         port * CFG_PCIE_MEMSIZE));
845                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
846                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
847                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
848                 break;
849         case 2:
850                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2),  0x0000000d);
851                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2),  (CFG_PCIE_MEMBASE +
852                         port * CFG_PCIE_MEMSIZE));
853                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
854                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
855                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
856                 break;
857         }
858
859         /* Set up 16GB inbound memory window at 0 */
860         out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
861         out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
862         out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
863         out_le32(mbase + PECFG_BAR0LMPA, 0);
864
865         out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
866         out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
867         out_le32(mbase + PECFG_PIM0LAL, 0);
868         out_le32(mbase + PECFG_PIM0LAH, 0);
869         out_le32(mbase + PECFG_PIM1LAL,  0x00000000);
870         out_le32(mbase + PECFG_PIM1LAH,  0x00000004);
871         out_le32(mbase + PECFG_PIMEN, 0x1);
872
873         /* Enable I/O, Mem, and Busmaster cycles */
874         out_le16((u16 *)(mbase + PCI_COMMAND),
875                  in_le16((u16 *)(mbase + PCI_COMMAND)) |
876                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
877         printf("PCIE:%d successfully set as rootpoint\n",port);
878 }
879
880 int ppc440spe_setup_pcie_endpoint(struct pci_controller *hose, int port)
881 {
882         volatile void *mbase = NULL;
883         int attempts = 0;
884
885         pci_set_ops(hose,
886                     pcie_read_config_byte,
887                     pcie_read_config_word,
888                     pcie_read_config_dword,
889                     pcie_write_config_byte,
890                     pcie_write_config_word,
891                     pcie_write_config_dword);
892
893         switch (port) {
894         case 0:
895                 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
896                 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
897                 break;
898         case 1:
899                 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
900                 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
901                 break;
902         case 2:
903                 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
904                 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
905                 break;
906         }
907
908         /*
909          * Set up outbound translation to hose->mem_space from PLB
910          * addresses at an offset of 0xd_0000_0000.  We set the low
911          * bits of the mask to 11 to turn off splitting into 8
912          * subregions and to enable the outbound translation.
913          */
914         out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
915         out_le32(mbase + PECFG_POM0LAL, 0x00001000);
916
917         switch (port) {
918         case 0:
919                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0),  0x0000000d);
920                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0),  CFG_PCIE_MEMBASE +
921                         port * CFG_PCIE_MEMSIZE);
922                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
923                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
924                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
925                 break;
926         case 1:
927                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1),  0x0000000d);
928                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1),  (CFG_PCIE_MEMBASE +
929                         port * CFG_PCIE_MEMSIZE));
930                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
931                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
932                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
933                 break;
934         case 2:
935                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2),  0x0000000d);
936                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2),  (CFG_PCIE_MEMBASE +
937                         port * CFG_PCIE_MEMSIZE));
938                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
939                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
940                         ~(CFG_PCIE_MEMSIZE - 1) | 3);
941                 break;
942         }
943
944         /* Set up 16GB inbound memory window at 0 */
945         out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
946         out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
947         out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
948         out_le32(mbase + PECFG_BAR0LMPA, 0);
949         out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
950         out_le32(mbase + PECFG_PIM0LAH, 0x00000004);    /* pointing to SRAM */
951         out_le32(mbase + PECFG_PIMEN, 0x1);
952
953         /* Enable I/O, Mem, and Busmaster cycles */
954         out_le16((u16 *)(mbase + PCI_COMMAND),
955                  in_le16((u16 *)(mbase + PCI_COMMAND)) |
956                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
957         out_le16(mbase + 0x200,0xcaad);                 /* Setting vendor ID */
958         out_le16(mbase + 0x202,0xfeed);                 /* Setting device ID */
959         attempts = 10;
960         switch (port) {
961         case 0:
962                 while (!(SDR_READ(PESDR0_RCSSTS) & (1 << 8))) {
963                         if (!(attempts--)) {
964                                 printf("PCIE0: BMEN is  not active\n");
965                                 return -1;
966                         }
967                         mdelay(1000);
968                 }
969                 break;
970         case 1:
971                 while (!(SDR_READ(PESDR1_RCSSTS) & (1 << 8))) {
972                         if (!(attempts--)) {
973                                 printf("PCIE1: BMEN is not active\n");
974                                 return -1;
975                         }
976                         mdelay(1000);
977                 }
978                 break;
979         case 2:
980                 while (!(SDR_READ(PESDR2_RCSSTS) & (1 << 8))) {
981                         if (!(attempts--)) {
982                                 printf("PCIE2: BMEN is  not active\n");
983                                 return -1;
984                         }
985                         mdelay(1000);
986                 }
987                 break;
988         }
989         printf("PCIE:%d successfully set as endpoint\n",port);
990
991         return 0;
992 }
993 #endif /* CONFIG_440SPE && CONFIG_PCI */