blob: 9ab358857ad590a761f8965e48eb9549528b84ac [file] [log] [blame]
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +02001/*
Grzegorz Bernackid84fe302007-07-31 18:51:48 +02002 * (C) Copyright 2006 - 2007
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +02003 * 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
Stefan Roese3dced492007-10-05 07:57:20 +020023/* define DEBUG for debugging output (obviously ;-)) */
Stefan Roesedb4f4542007-10-05 09:22:33 +020024#if 0
Stefan Roese3dced492007-10-05 07:57:20 +020025#define DEBUG
26#endif
27
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +020028#include <asm/processor.h>
29#include <asm-ppc/io.h>
30#include <ppc4xx.h>
31#include <common.h>
32#include <pci.h>
33
Stefan Roese7a41bde2007-10-05 09:18:23 +020034#if (defined(CONFIG_440SPE) || defined(CONFIG_405EX)) && \
35 defined(CONFIG_PCI)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +020036
Stefan Roese216f0632007-10-03 07:34:10 +020037#include <asm/4xx_pcie.h>
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +020038
39enum {
40 PTYPE_ENDPOINT = 0x0,
41 PTYPE_LEGACY_ENDPOINT = 0x1,
42 PTYPE_ROOT_PORT = 0x4,
43
44 LNKW_X1 = 0x1,
45 LNKW_X4 = 0x4,
46 LNKW_X8 = 0x8
47};
48
Stefan Roese89bac402007-10-13 16:43:23 +020049static int validate_endpoint(struct pci_controller *hose)
50{
51 if (hose->cfg_data == (u8 *)CFG_PCIE0_CFGBASE)
52 return (is_end_point(0));
53 else if (hose->cfg_data == (u8 *)CFG_PCIE1_CFGBASE)
54 return (is_end_point(1));
55#if CFG_PCIE_NR_PORTS > 2
56 else if (hose->cfg_data == (u8 *)CFG_PCIE2_CFGBASE)
57 return (is_end_point(2));
58#endif
59
60 return 0;
61}
62
Grzegorz Bernackid2f21332007-09-07 18:20:23 +020063static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
64{
65 u8 *base = (u8*)hose->cfg_data;
66
67 /* use local configuration space for the first bus */
68 if (PCI_BUS(devfn) == 0) {
69 if (hose->cfg_data == (u8*)CFG_PCIE0_CFGBASE)
70 base = (u8*)CFG_PCIE0_XCFGBASE;
71 if (hose->cfg_data == (u8*)CFG_PCIE1_CFGBASE)
72 base = (u8*)CFG_PCIE1_XCFGBASE;
Stefan Roese7a41bde2007-10-05 09:18:23 +020073#if CFG_PCIE_NR_PORTS > 2
Grzegorz Bernackid2f21332007-09-07 18:20:23 +020074 if (hose->cfg_data == (u8*)CFG_PCIE2_CFGBASE)
75 base = (u8*)CFG_PCIE2_XCFGBASE;
Stefan Roese7a41bde2007-10-05 09:18:23 +020076#endif
Grzegorz Bernackid2f21332007-09-07 18:20:23 +020077 }
78
79 return base;
80}
81
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +020082static void pcie_dmer_disable(void)
Grzegorz Bernackid84fe302007-07-31 18:51:48 +020083{
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +020084 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE),
85 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
86 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
87 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
Stefan Roese7a41bde2007-10-05 09:18:23 +020088#if CFG_PCIE_NR_PORTS > 2
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +020089 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
90 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
Stefan Roese7a41bde2007-10-05 09:18:23 +020091#endif
Grzegorz Bernackid84fe302007-07-31 18:51:48 +020092}
93
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +020094static void pcie_dmer_enable(void)
Grzegorz Bernackid84fe302007-07-31 18:51:48 +020095{
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +020096 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE0_BASE),
97 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
98 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
99 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200100#if CFG_PCIE_NR_PORTS > 2
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200101 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
102 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200103#endif
Grzegorz Bernackid84fe302007-07-31 18:51:48 +0200104}
105
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200106static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
107 int offset, int len, u32 *val) {
108
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200109 u8 *address;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200110 *val = 0;
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200111
Stefan Roese89bac402007-10-13 16:43:23 +0200112 if (validate_endpoint(hose))
113 return 0; /* No upstream config access */
114
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200115 /*
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200116 * Bus numbers are relative to hose->first_busno
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200117 */
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200118 devfn -= PCI_BDF(hose->first_busno, 0, 0);
119
120 /*
121 * NOTICE: configuration space ranges are currenlty mapped only for
122 * the first 16 buses, so such limit must be imposed. In case more
123 * buses are required the TLB settings in board/amcc/<board>/init.S
124 * need to be altered accordingly (one bus takes 1 MB of memory space).
125 */
126 if (PCI_BUS(devfn) >= 16)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200127 return 0;
128
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200129 /*
130 * Only single device/single function is supported for the primary and
131 * secondary buses of the 440SPe host bridge.
132 */
133 if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
134 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
135 return 0;
Stefan Roese43867c82007-10-02 11:44:46 +0200136
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200137 address = pcie_get_base(hose, devfn);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200138 offset += devfn << 4;
139
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200140 /*
141 * Reading from configuration space of non-existing device can
142 * generate transaction errors. For the read duration we suppress
143 * assertion of machine check exceptions to avoid those.
144 */
145 pcie_dmer_disable ();
146
Stefan Roese7a41bde2007-10-05 09:18:23 +0200147 debug("%s: cfg_data=%08x offset=%08x\n", __func__, hose->cfg_data, offset);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200148 switch (len) {
149 case 1:
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200150 *val = in_8(hose->cfg_data + offset);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200151 break;
152 case 2:
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200153 *val = in_le16((u16 *)(hose->cfg_data + offset));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200154 break;
155 default:
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200156 *val = in_le32((u32*)(hose->cfg_data + offset));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200157 break;
158 }
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200159
160 pcie_dmer_enable ();
161
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200162 return 0;
163}
164
165static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
166 int offset, int len, u32 val) {
167
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200168 u8 *address;
Stefan Roese43867c82007-10-02 11:44:46 +0200169
Stefan Roese89bac402007-10-13 16:43:23 +0200170 if (validate_endpoint(hose))
171 return 0; /* No upstream config access */
172
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200173 /*
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200174 * Bus numbers are relative to hose->first_busno
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200175 */
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200176 devfn -= PCI_BDF(hose->first_busno, 0, 0);
Stefan Roese43867c82007-10-02 11:44:46 +0200177
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200178 /*
179 * Same constraints as in pcie_read_config().
180 */
181 if (PCI_BUS(devfn) >= 16)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200182 return 0;
183
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200184 if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
185 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
186 return 0;
Stefan Roese43867c82007-10-02 11:44:46 +0200187
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200188 address = pcie_get_base(hose, devfn);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200189 offset += devfn << 4;
190
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200191 /*
192 * Suppress MCK exceptions, similar to pcie_read_config()
193 */
194 pcie_dmer_disable ();
195
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200196 switch (len) {
197 case 1:
198 out_8(hose->cfg_data + offset, val);
199 break;
200 case 2:
201 out_le16((u16 *)(hose->cfg_data + offset), val);
202 break;
203 default:
204 out_le32((u32 *)(hose->cfg_data + offset), val);
205 break;
206 }
Grzegorz Bernackieff9bc12007-09-07 17:46:18 +0200207
208 pcie_dmer_enable ();
209
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200210 return 0;
211}
212
213int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
214{
215 u32 v;
216 int rv;
217
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200218 rv = pcie_read_config(hose, dev, offset, 1, &v);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200219 *val = (u8)v;
220 return rv;
221}
222
223int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
224{
225 u32 v;
226 int rv;
227
228 rv = pcie_read_config(hose, dev, offset, 2, &v);
229 *val = (u16)v;
230 return rv;
231}
232
233int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
234{
235 u32 v;
236 int rv;
237
238 rv = pcie_read_config(hose, dev, offset, 3, &v);
239 *val = (u32)v;
240 return rv;
241}
242
243int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
244{
245 return pcie_write_config(hose,(u32)dev,offset,1,val);
246}
247
248int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
249{
250 return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
251}
252
253int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
254{
255 return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
256}
257
Stefan Roese7a41bde2007-10-05 09:18:23 +0200258#if defined(CONFIG_440SPE)
Stefan Roese9c00e512007-10-03 07:48:09 +0200259static void ppc4xx_setup_utl(u32 port) {
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200260
261 volatile void *utl_base = NULL;
262
263 /*
264 * Map UTL registers
265 */
266 switch (port) {
267 case 0:
Rafal Jaworowskie9799092006-08-11 12:35:52 +0200268 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
269 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
270 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200271 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200272 break;
273
274 case 1:
Rafal Jaworowskie9799092006-08-11 12:35:52 +0200275 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
276 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
277 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200278 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200279 break;
280
281 case 2:
Rafal Jaworowskie9799092006-08-11 12:35:52 +0200282 mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
283 mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
284 mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200285 mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200286 break;
287 }
Rafal Jaworowskie9799092006-08-11 12:35:52 +0200288 utl_base = (unsigned int *)(CFG_PCIE_BASE + 0x1000 * port);
Wolfgang Denkdd314d12006-08-27 18:10:01 +0200289
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200290 /*
291 * Set buffer allocations and then assert VRB and TXE.
292 */
293 out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
294 out_be32(utl_base + PEUTL_INTR, 0x02000000);
295 out_be32(utl_base + PEUTL_OPDBSZ, 0x10000000);
296 out_be32(utl_base + PEUTL_PBBSZ, 0x53000000);
297 out_be32(utl_base + PEUTL_IPHBSZ, 0x08000000);
298 out_be32(utl_base + PEUTL_IPDBSZ, 0x10000000);
299 out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
Rafal Jaworowskie9799092006-08-11 12:35:52 +0200300 out_be32(utl_base + PEUTL_PCTL, 0x80800066);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200301}
302
303static int check_error(void)
304{
305 u32 valPE0, valPE1, valPE2;
306 int err = 0;
307
308 /* SDR0_PEGPLLLCT1 reset */
309 if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
310 printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
311 }
312
313 valPE0 = SDR_READ(PESDR0_RCSSET);
314 valPE1 = SDR_READ(PESDR1_RCSSET);
315 valPE2 = SDR_READ(PESDR2_RCSSET);
316
317 /* SDR0_PExRCSSET rstgu */
318 if (!(valPE0 & 0x01000000) ||
319 !(valPE1 & 0x01000000) ||
320 !(valPE2 & 0x01000000)) {
321 printf("PCIE: SDR0_PExRCSSET rstgu error\n");
322 err = -1;
323 }
324
325 /* SDR0_PExRCSSET rstdl */
326 if (!(valPE0 & 0x00010000) ||
327 !(valPE1 & 0x00010000) ||
328 !(valPE2 & 0x00010000)) {
329 printf("PCIE: SDR0_PExRCSSET rstdl error\n");
330 err = -1;
331 }
332
333 /* SDR0_PExRCSSET rstpyn */
334 if ((valPE0 & 0x00001000) ||
335 (valPE1 & 0x00001000) ||
336 (valPE2 & 0x00001000)) {
337 printf("PCIE: SDR0_PExRCSSET rstpyn error\n");
338 err = -1;
339 }
340
341 /* SDR0_PExRCSSET hldplb */
342 if ((valPE0 & 0x10000000) ||
343 (valPE1 & 0x10000000) ||
344 (valPE2 & 0x10000000)) {
345 printf("PCIE: SDR0_PExRCSSET hldplb error\n");
346 err = -1;
347 }
348
349 /* SDR0_PExRCSSET rdy */
350 if ((valPE0 & 0x00100000) ||
351 (valPE1 & 0x00100000) ||
352 (valPE2 & 0x00100000)) {
353 printf("PCIE: SDR0_PExRCSSET rdy error\n");
354 err = -1;
355 }
356
357 /* SDR0_PExRCSSET shutdown */
358 if ((valPE0 & 0x00000100) ||
359 (valPE1 & 0x00000100) ||
360 (valPE2 & 0x00000100)) {
361 printf("PCIE: SDR0_PExRCSSET shutdown error\n");
362 err = -1;
363 }
364 return err;
365}
366
367/*
368 * Initialize PCI Express core
369 */
Stefan Roese9c00e512007-10-03 07:48:09 +0200370int ppc4xx_init_pcie(void)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200371{
372 int time_out = 20;
373
374 /* Set PLL clock receiver to LVPECL */
375 SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
376
377 if (check_error())
378 return -1;
379
380 if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
381 {
382 printf("PCIE: PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
383 SDR_READ(PESDR0_PLLLCT2));
384 return -1;
385 }
386 /* De-assert reset of PCIe PLL, wait for lock */
387 SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
388 udelay(3);
389
Stefan Roese074e9752006-08-29 08:05:15 +0200390 while (time_out) {
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200391 if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
392 time_out--;
393 udelay(1);
394 } else
395 break;
396 }
397 if (!time_out) {
398 printf("PCIE: VCO output not locked\n");
399 return -1;
400 }
401 return 0;
402}
Stefan Roese7a41bde2007-10-05 09:18:23 +0200403#else
404int ppc4xx_init_pcie(void)
405{
406 /*
407 * Nothing to do on 405EX
408 */
409 return 0;
410}
411#endif
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200412
Stefan Roese074e9752006-08-29 08:05:15 +0200413/*
Stefan Roesea0d96342007-10-03 10:38:09 +0200414 * Board-specific pcie initialization
415 * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
416 */
417
418/*
419 * Initialize various parts of the PCI Express core for our port:
420 *
421 * - Set as a root port and enable max width
422 * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
423 * - Set up UTL configuration.
424 * - Increase SERDES drive strength to levels suggested by AMCC.
425 * - De-assert RSTPYN, RSTDL and RSTGU.
426 *
427 * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
428 * with default setting 0x11310000. The register has new fields,
429 * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
430 * hang.
431 */
432#if defined(CONFIG_440SPE)
433int __ppc4xx_init_pcie_port_hw(int port, int rootport)
434{
435 u32 val = 1 << 24;
436 u32 utlset1;
437
438 if (rootport) {
439 val = PTYPE_ROOT_PORT << 20;
440 utlset1 = 0x21222222;
441 } else {
442 val = PTYPE_LEGACY_ENDPOINT << 20;
443 utlset1 = 0x20222222;
444 }
445
446 if (port == 0)
447 val |= LNKW_X8 << 12;
448 else
449 val |= LNKW_X4 << 12;
450
451 SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
452 SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
453 if (!ppc440spe_revB())
454 SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
455 SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
456 SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
457 SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
458 SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
459 if (port == 0) {
460 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
461 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
462 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
463 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
464 }
465 SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
466 ~(1 << 24 | 1 << 16)) | 1 << 12);
467
468 return 0;
469}
470#endif /* CONFIG_440SPE */
471
472#if defined(CONFIG_405EX)
473int __ppc4xx_init_pcie_port_hw(int port, int rootport)
474{
475 u32 val;
476
Stefan Roesec72bfb62007-10-03 14:14:58 +0200477 /*
478 * test-only:
479 * This needs some testing and perhaps changes for
480 * endpoint configuration. Probably no PHY reset at all, etc.
481 * sr, 2007-10-03
482 */
Stefan Roesea0d96342007-10-03 10:38:09 +0200483 if (rootport)
484 val = 0x00401000;
485 else
486 val = 0x00101000;
487
488 SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
489 SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x20222222);
490 SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01110000);
491 SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
492 SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
493
494 /* Assert the PE0_PHY reset */
495 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
496 udelay(1000);
497
498 /* deassert the PE0_hotreset */
499 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
500
501 /* poll for phy !reset */
502 while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
503 ;
504
505 /* deassert the PE0_gpl_utl_reset */
506 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
507
508 if (port == 0)
509 mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000); /* guarded on */
510 else
511 mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000); /* guarded on */
512
513 return 0;
514}
515#endif /* CONFIG_405EX */
516
517int ppc4xx_init_pcie_port_hw(int port, int rootport)
518 __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
519
520/*
521 * We map PCI Express configuration access into the 512MB regions
522 *
523 * NOTICE: revB is very strict about PLB real addressess and ranges to
524 * be mapped for config space; it seems to only work with d_nnnn_nnnn
525 * range (hangs the core upon config transaction attempts when set
526 * otherwise) while revA uses c_nnnn_nnnn.
527 *
528 * For revA:
529 * PCIE0: 0xc_4000_0000
530 * PCIE1: 0xc_8000_0000
531 * PCIE2: 0xc_c000_0000
532 *
533 * For revB:
534 * PCIE0: 0xd_0000_0000
535 * PCIE1: 0xd_2000_0000
536 * PCIE2: 0xd_4000_0000
537 *
538 * For 405EX:
539 * PCIE0: 0xa000_0000
540 * PCIE1: 0xc000_0000
541 */
542static inline u64 ppc4xx_get_cfgaddr(int port)
543{
544#if defined(CONFIG_405EX)
545 if (port == 0)
546 return (u64)CFG_PCIE0_CFGBASE;
547 else
548 return (u64)CFG_PCIE1_CFGBASE;
549#endif
550#if defined(CONFIG_440SPE)
551 if (ppc440spe_revB()) {
552 switch (port) {
553 default: /* to satisfy compiler */
554 case 0:
555 return 0x0000000d00000000ULL;
556 case 1:
557 return 0x0000000d20000000ULL;
558 case 2:
559 return 0x0000000d40000000ULL;
560 }
561 } else {
562 switch (port) {
563 default: /* to satisfy compiler */
564 case 0:
565 return 0x0000000c40000000ULL;
566 case 1:
567 return 0x0000000c80000000ULL;
568 case 2:
569 return 0x0000000cc0000000ULL;
570 }
571 }
572#endif
573}
574
575/*
576 * 4xx boards as end point and root point setup
Stefan Roese074e9752006-08-29 08:05:15 +0200577 * and
578 * testing inbound and out bound windows
579 *
Stefan Roesea0d96342007-10-03 10:38:09 +0200580 * 4xx boards can be plugged into another 4xx boards or you can get PCI-E
Stefan Roese074e9752006-08-29 08:05:15 +0200581 * cable which can be used to setup loop back from one port to another port.
582 * Please rememeber that unless there is a endpoint plugged in to root port it
583 * will not initialize. It is the same in case of endpoint , unless there is
584 * root port attached it will not initialize.
585 *
586 * In this release of software all the PCI-E ports are configured as either
587 * endpoint or rootpoint.In future we will have support for selective ports
588 * setup as endpoint and root point in single board.
589 *
590 * Once your board came up as root point , you can verify by reading
591 * /proc/bus/pci/devices. Where you can see the configuration registers
592 * of end point device attached to the port.
593 *
Stefan Roesea0d96342007-10-03 10:38:09 +0200594 * Enpoint cofiguration can be verified by connecting 4xx board to any
595 * host or another 4xx board. Then try to scan the device. In case of
Stefan Roese074e9752006-08-29 08:05:15 +0200596 * linux use "lspci" or appripriate os command.
597 *
Stefan Roesea0d96342007-10-03 10:38:09 +0200598 * How do I verify the inbound and out bound windows ? (4xx to 4xx)
Stefan Roese074e9752006-08-29 08:05:15 +0200599 * in this configuration inbound and outbound windows are setup to access
600 * sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
601 * is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
602 * This is waere your POM(PLB out bound memory window) mapped. then
Stefan Roesea0d96342007-10-03 10:38:09 +0200603 * read the data from other 4xx board's u-boot prompt at address
Stefan Roese074e9752006-08-29 08:05:15 +0200604 * 0x9000 0000(SRAM). Data should match.
605 * In case of inbound , write data to u-boot command prompt at 0xb000 0000
606 * which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
607 * data at 0x9000 0000(SRAM).Data should match.
608 */
Stefan Roesea0d96342007-10-03 10:38:09 +0200609int ppc4xx_init_pcie_port(int port, int rootport)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200610{
611 static int core_init;
612 volatile u32 val = 0;
613 int attempts;
Stefan Roesea0d96342007-10-03 10:38:09 +0200614 u64 addr;
615 u32 low, high;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200616
617 if (!core_init) {
Stefan Roese9c00e512007-10-03 07:48:09 +0200618 if (ppc4xx_init_pcie())
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200619 return -1;
Stefan Roese89bac402007-10-13 16:43:23 +0200620 ++core_init;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200621 }
622
623 /*
Stefan Roesea0d96342007-10-03 10:38:09 +0200624 * Initialize various parts of the PCI Express core for our port
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200625 */
Stefan Roesea0d96342007-10-03 10:38:09 +0200626 ppc4xx_init_pcie_port_hw(port, rootport);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200627
Stefan Roese074e9752006-08-29 08:05:15 +0200628 /*
629 * Notice: the following delay has critical impact on device
630 * initialization - if too short (<50ms) the link doesn't get up.
631 */
632 mdelay(100);
633
Stefan Roesebe6fea92007-10-03 21:16:32 +0200634 val = SDR_READ(SDRN_PESDR_RCSSTS(port));
Stefan Roese074e9752006-08-29 08:05:15 +0200635 if (val & (1 << 20)) {
636 printf("PCIE%d: PGRST failed %08x\n", port, val);
637 return -1;
638 }
639
640 /*
641 * Verify link is up
642 */
Stefan Roesebe6fea92007-10-03 21:16:32 +0200643 val = SDR_READ(SDRN_PESDR_LOOP(port));
Stefan Roese074e9752006-08-29 08:05:15 +0200644 if (!(val & 0x00001000)) {
645 printf("PCIE%d: link is not up.\n", port);
646 return -1;
647 }
648
Stefan Roese7a41bde2007-10-05 09:18:23 +0200649#if defined(CONFIG_440SPE)
Stefan Roese074e9752006-08-29 08:05:15 +0200650 /*
651 * Setup UTL registers - but only on revA!
652 * We use default settings for revB chip.
653 */
654 if (!ppc440spe_revB())
Stefan Roese9c00e512007-10-03 07:48:09 +0200655 ppc4xx_setup_utl(port);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200656#endif
Stefan Roese074e9752006-08-29 08:05:15 +0200657
658 /*
659 * We map PCI Express configuration access into the 512MB regions
Stefan Roese074e9752006-08-29 08:05:15 +0200660 */
Stefan Roesea0d96342007-10-03 10:38:09 +0200661 addr = ppc4xx_get_cfgaddr(port);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200662 low = U64_TO_U32_LOW(addr);
663 high = U64_TO_U32_HIGH(addr);
Stefan Roese074e9752006-08-29 08:05:15 +0200664
665 switch (port) {
666 case 0:
Stefan Roesea0d96342007-10-03 10:38:09 +0200667 mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
668 mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
Stefan Roese074e9752006-08-29 08:05:15 +0200669 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
670 break;
Stefan Roese074e9752006-08-29 08:05:15 +0200671 case 1:
Stefan Roesea0d96342007-10-03 10:38:09 +0200672 mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
673 mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
Stefan Roese074e9752006-08-29 08:05:15 +0200674 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
675 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200676#if CFG_PCIE_NR_PORTS > 2
Stefan Roese074e9752006-08-29 08:05:15 +0200677 case 2:
Stefan Roesea0d96342007-10-03 10:38:09 +0200678 mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
679 mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
Stefan Roese074e9752006-08-29 08:05:15 +0200680 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
681 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200682#endif
Stefan Roese074e9752006-08-29 08:05:15 +0200683 }
684
685 /*
686 * Check for VC0 active and assert RDY.
687 */
688 attempts = 10;
Stefan Roesebe6fea92007-10-03 21:16:32 +0200689 while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 16))) {
Stefan Roesea0d96342007-10-03 10:38:09 +0200690 if (!(attempts--)) {
691 printf("PCIE%d: VC0 not active\n", port);
692 return -1;
Stefan Roese074e9752006-08-29 08:05:15 +0200693 }
Stefan Roesea0d96342007-10-03 10:38:09 +0200694 mdelay(1000);
Stefan Roese074e9752006-08-29 08:05:15 +0200695 }
Stefan Roesebe6fea92007-10-03 21:16:32 +0200696 SDR_WRITE(SDRN_PESDR_RCSSET(port),
697 SDR_READ(SDRN_PESDR_RCSSET(port)) | 1 << 20);
Stefan Roese074e9752006-08-29 08:05:15 +0200698 mdelay(100);
699
700 return 0;
701}
702
Stefan Roesea0d96342007-10-03 10:38:09 +0200703int ppc4xx_init_pcie_rootport(int port)
Stefan Roese074e9752006-08-29 08:05:15 +0200704{
Stefan Roesea0d96342007-10-03 10:38:09 +0200705 return ppc4xx_init_pcie_port(port, 1);
706}
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200707
Stefan Roesea0d96342007-10-03 10:38:09 +0200708int ppc4xx_init_pcie_endport(int port)
709{
710 return ppc4xx_init_pcie_port(port, 0);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200711}
712
Stefan Roese9c00e512007-10-03 07:48:09 +0200713void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200714{
715 volatile void *mbase = NULL;
Stefan Roese074e9752006-08-29 08:05:15 +0200716 volatile void *rmbase = NULL;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200717
718 pci_set_ops(hose,
Stefan Roesea0d96342007-10-03 10:38:09 +0200719 pcie_read_config_byte,
720 pcie_read_config_word,
721 pcie_read_config_dword,
722 pcie_write_config_byte,
723 pcie_write_config_word,
724 pcie_write_config_dword);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200725
Stefan Roese074e9752006-08-29 08:05:15 +0200726 switch (port) {
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200727 case 0:
728 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
Stefan Roese074e9752006-08-29 08:05:15 +0200729 rmbase = (u32 *)CFG_PCIE0_CFGBASE;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200730 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
731 break;
732 case 1:
733 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
Stefan Roese074e9752006-08-29 08:05:15 +0200734 rmbase = (u32 *)CFG_PCIE1_CFGBASE;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200735 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
736 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200737#if CFG_PCIE_NR_PORTS > 2
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200738 case 2:
739 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
Stefan Roese074e9752006-08-29 08:05:15 +0200740 rmbase = (u32 *)CFG_PCIE2_CFGBASE;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200741 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
742 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200743#endif
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200744 }
745
746 /*
747 * Set bus numbers on our root port
748 */
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200749 out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
750 out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
751 out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200752
753 /*
754 * Set up outbound translation to hose->mem_space from PLB
755 * addresses at an offset of 0xd_0000_0000. We set the low
756 * bits of the mask to 11 to turn off splitting into 8
757 * subregions and to enable the outbound translation.
758 */
759 out_le32(mbase + PECFG_POM0LAH, 0x00000000);
Stefan Roese3dced492007-10-05 07:57:20 +0200760 out_le32(mbase + PECFG_POM0LAL, CFG_PCIE_MEMBASE +
761 port * CFG_PCIE_MEMSIZE);
762 debug("PECFG_POM0LA=%08x.%08x\n", in_le32(mbase + PECFG_POM0LAH),
763 in_le32(mbase + PECFG_POM0LAL));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200764
765 switch (port) {
766 case 0:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200767 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
768 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200769 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200770 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
771 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
Stefan Roesea0d96342007-10-03 10:38:09 +0200772 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese3dced492007-10-05 07:57:20 +0200773 debug("0:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
774 mfdcr(DCRN_PEGPL_OMR1BAH(PCIE0)),
775 mfdcr(DCRN_PEGPL_OMR1BAL(PCIE0)),
776 mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE0)),
777 mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200778 break;
779 case 1:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200780 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
781 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200782 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200783 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
784 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
Stefan Roesea0d96342007-10-03 10:38:09 +0200785 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese3dced492007-10-05 07:57:20 +0200786 debug("1:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
787 mfdcr(DCRN_PEGPL_OMR1BAH(PCIE1)),
788 mfdcr(DCRN_PEGPL_OMR1BAL(PCIE1)),
789 mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
790 mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200791 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200792#if CFG_PCIE_NR_PORTS > 2
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200793 case 2:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200794 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
795 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200796 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200797 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
798 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
Stefan Roesea0d96342007-10-03 10:38:09 +0200799 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese3dced492007-10-05 07:57:20 +0200800 debug("2:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
801 mfdcr(DCRN_PEGPL_OMR1BAH(PCIE2)),
802 mfdcr(DCRN_PEGPL_OMR1BAL(PCIE2)),
803 mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
804 mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200805 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200806#endif
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200807 }
808
809 /* Set up 16GB inbound memory window at 0 */
810 out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
811 out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
812 out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
813 out_le32(mbase + PECFG_BAR0LMPA, 0);
Stefan Roese074e9752006-08-29 08:05:15 +0200814
815 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
816 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200817 out_le32(mbase + PECFG_PIM0LAL, 0);
818 out_le32(mbase + PECFG_PIM0LAH, 0);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200819 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
820 out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
Stefan Roese074e9752006-08-29 08:05:15 +0200821 out_le32(mbase + PECFG_PIMEN, 0x1);
822
823 /* Enable I/O, Mem, and Busmaster cycles */
824 out_le16((u16 *)(mbase + PCI_COMMAND),
825 in_le16((u16 *)(mbase + PCI_COMMAND)) |
826 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Stefan Roese43867c82007-10-02 11:44:46 +0200827
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200828 /* Set Device and Vendor Id */
Stefan Roese7a41bde2007-10-05 09:18:23 +0200829 out_le16(mbase + 0x200, 0xaaa0 + port);
830 out_le16(mbase + 0x202, 0xbed0 + port);
Grzegorz Bernackid2f21332007-09-07 18:20:23 +0200831
832 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
833 out_le32(mbase + 0x208, 0x06040001);
834
Stefan Roese4e456622007-10-05 14:23:43 +0200835 printf("PCIE%d: successfully set as root-complex\n", port);
Stefan Roese074e9752006-08-29 08:05:15 +0200836}
837
Stefan Roese9c00e512007-10-03 07:48:09 +0200838int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
Stefan Roese074e9752006-08-29 08:05:15 +0200839{
840 volatile void *mbase = NULL;
841 int attempts = 0;
842
843 pci_set_ops(hose,
844 pcie_read_config_byte,
845 pcie_read_config_word,
846 pcie_read_config_dword,
847 pcie_write_config_byte,
848 pcie_write_config_word,
849 pcie_write_config_dword);
850
851 switch (port) {
852 case 0:
853 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
854 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
855 break;
856 case 1:
857 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
858 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
859 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200860#if defined(CFG_PCIE2_CFGBASE)
Stefan Roese074e9752006-08-29 08:05:15 +0200861 case 2:
862 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
863 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
864 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200865#endif
Stefan Roese074e9752006-08-29 08:05:15 +0200866 }
867
868 /*
869 * Set up outbound translation to hose->mem_space from PLB
870 * addresses at an offset of 0xd_0000_0000. We set the low
871 * bits of the mask to 11 to turn off splitting into 8
872 * subregions and to enable the outbound translation.
873 */
874 out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
875 out_le32(mbase + PECFG_POM0LAL, 0x00001000);
876
877 switch (port) {
878 case 0:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200879 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
880 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200881 port * CFG_PCIE_MEMSIZE);
Stefan Roese074e9752006-08-29 08:05:15 +0200882 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
883 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
Stefan Roesea0d96342007-10-03 10:38:09 +0200884 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese074e9752006-08-29 08:05:15 +0200885 break;
886 case 1:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200887 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
888 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200889 port * CFG_PCIE_MEMSIZE);
Stefan Roese074e9752006-08-29 08:05:15 +0200890 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
891 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
Stefan Roesea0d96342007-10-03 10:38:09 +0200892 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese074e9752006-08-29 08:05:15 +0200893 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200894#if CFG_PCIE_NR_PORTS > 2
Stefan Roese074e9752006-08-29 08:05:15 +0200895 case 2:
Stefan Roese7a41bde2007-10-05 09:18:23 +0200896 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
897 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
Stefan Roesea0d96342007-10-03 10:38:09 +0200898 port * CFG_PCIE_MEMSIZE);
Stefan Roese074e9752006-08-29 08:05:15 +0200899 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
900 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
Stefan Roesea0d96342007-10-03 10:38:09 +0200901 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese074e9752006-08-29 08:05:15 +0200902 break;
Stefan Roese7a41bde2007-10-05 09:18:23 +0200903#endif
Stefan Roese074e9752006-08-29 08:05:15 +0200904 }
905
906 /* Set up 16GB inbound memory window at 0 */
907 out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
908 out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
909 out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
910 out_le32(mbase + PECFG_BAR0LMPA, 0);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200911 out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CFG_PCIE_INBOUND_BASE));
912 out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CFG_PCIE_INBOUND_BASE));
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200913 out_le32(mbase + PECFG_PIMEN, 0x1);
914
915 /* Enable I/O, Mem, and Busmaster cycles */
916 out_le16((u16 *)(mbase + PCI_COMMAND),
Stefan Roesea0d96342007-10-03 10:38:09 +0200917 in_le16((u16 *)(mbase + PCI_COMMAND)) |
918 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Stefan Roese7a41bde2007-10-05 09:18:23 +0200919 out_le16(mbase + 0x200, 0xcaad); /* Setting vendor ID */
920 out_le16(mbase + 0x202, 0xfeed); /* Setting device ID */
Stefan Roesea0d96342007-10-03 10:38:09 +0200921
Stefan Roese074e9752006-08-29 08:05:15 +0200922 attempts = 10;
Stefan Roesebe6fea92007-10-03 21:16:32 +0200923 while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
Stefan Roesea0d96342007-10-03 10:38:09 +0200924 if (!(attempts--)) {
925 printf("PCIE%d: BME not active\n", port);
926 return -1;
Stefan Roese074e9752006-08-29 08:05:15 +0200927 }
Stefan Roesea0d96342007-10-03 10:38:09 +0200928 mdelay(1000);
Stefan Roese074e9752006-08-29 08:05:15 +0200929 }
Stefan Roesea0d96342007-10-03 10:38:09 +0200930
Stefan Roese4e456622007-10-05 14:23:43 +0200931 printf("PCIE%d: successfully set as endpoint\n", port);
Stefan Roese074e9752006-08-29 08:05:15 +0200932
933 return 0;
Rafal Jaworowskia2e7ef02006-08-10 12:43:17 +0200934}
Stefan Roese8d982302007-01-18 10:25:34 +0100935#endif /* CONFIG_440SPE && CONFIG_PCI */