blob: 2bc9638f414826161c669c04356822893b00c48e [file] [log] [blame]
Rafal Jaworowski692519b2006-08-10 12:43:17 +02001/*
Grzegorz Bernackic9240982007-07-31 18:51:48 +02002 * (C) Copyright 2006 - 2007
Rafal Jaworowski692519b2006-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
23#include <asm/processor.h>
24#include <asm-ppc/io.h>
25#include <ppc4xx.h>
26#include <common.h>
27#include <pci.h>
28
Stefan Roese5fb692c2007-01-18 10:25:34 +010029#if defined(CONFIG_440SPE) && defined(CONFIG_PCI)
Rafal Jaworowski692519b2006-08-10 12:43:17 +020030
Stefan Roesec7c6da22007-10-03 07:34:10 +020031#include <asm/4xx_pcie.h>
Rafal Jaworowski692519b2006-08-10 12:43:17 +020032
33enum {
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
Grzegorz Bernacki7f191392007-09-07 18:20:23 +020043static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
44{
45 u8 *base = (u8*)hose->cfg_data;
46
47 /* use local configuration space for the first bus */
48 if (PCI_BUS(devfn) == 0) {
49 if (hose->cfg_data == (u8*)CFG_PCIE0_CFGBASE)
50 base = (u8*)CFG_PCIE0_XCFGBASE;
51 if (hose->cfg_data == (u8*)CFG_PCIE1_CFGBASE)
52 base = (u8*)CFG_PCIE1_XCFGBASE;
53 if (hose->cfg_data == (u8*)CFG_PCIE2_CFGBASE)
54 base = (u8*)CFG_PCIE2_XCFGBASE;
55 }
56
57 return base;
58}
59
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +020060static void pcie_dmer_disable(void)
Grzegorz Bernackic9240982007-07-31 18:51:48 +020061{
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +020062 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE),
63 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
64 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
65 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
66 mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
67 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
Grzegorz Bernackic9240982007-07-31 18:51:48 +020068}
69
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +020070static void pcie_dmer_enable(void)
Grzegorz Bernackic9240982007-07-31 18:51:48 +020071{
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +020072 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE0_BASE),
73 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
74 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
75 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
76 mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
77 mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
Grzegorz Bernackic9240982007-07-31 18:51:48 +020078}
79
Rafal Jaworowski692519b2006-08-10 12:43:17 +020080static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
81 int offset, int len, u32 *val) {
82
Grzegorz Bernacki7f191392007-09-07 18:20:23 +020083 u8 *address;
Rafal Jaworowski692519b2006-08-10 12:43:17 +020084 *val = 0;
Grzegorz Bernacki7f191392007-09-07 18:20:23 +020085
Rafal Jaworowski692519b2006-08-10 12:43:17 +020086 /*
Grzegorz Bernacki7f191392007-09-07 18:20:23 +020087 * Bus numbers are relative to hose->first_busno
Rafal Jaworowski692519b2006-08-10 12:43:17 +020088 */
Grzegorz Bernacki7f191392007-09-07 18:20:23 +020089 devfn -= PCI_BDF(hose->first_busno, 0, 0);
90
91 /*
92 * NOTICE: configuration space ranges are currenlty mapped only for
93 * the first 16 buses, so such limit must be imposed. In case more
94 * buses are required the TLB settings in board/amcc/<board>/init.S
95 * need to be altered accordingly (one bus takes 1 MB of memory space).
96 */
97 if (PCI_BUS(devfn) >= 16)
Rafal Jaworowski692519b2006-08-10 12:43:17 +020098 return 0;
99
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200100 /*
101 * Only single device/single function is supported for the primary and
102 * secondary buses of the 440SPe host bridge.
103 */
104 if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
105 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
106 return 0;
Stefan Roese738815c2007-10-02 11:44:46 +0200107
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200108 address = pcie_get_base(hose, devfn);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200109 offset += devfn << 4;
110
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200111 /*
112 * Reading from configuration space of non-existing device can
113 * generate transaction errors. For the read duration we suppress
114 * assertion of machine check exceptions to avoid those.
115 */
116 pcie_dmer_disable ();
117
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200118 switch (len) {
119 case 1:
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200120 *val = in_8(hose->cfg_data + offset);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200121 break;
122 case 2:
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200123 *val = in_le16((u16 *)(hose->cfg_data + offset));
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200124 break;
125 default:
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200126 *val = in_le32((u32*)(hose->cfg_data + offset));
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200127 break;
128 }
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200129
130 pcie_dmer_enable ();
131
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200132 return 0;
133}
134
135static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
136 int offset, int len, u32 val) {
137
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200138 u8 *address;
Stefan Roese738815c2007-10-02 11:44:46 +0200139
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200140 /*
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200141 * Bus numbers are relative to hose->first_busno
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200142 */
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200143 devfn -= PCI_BDF(hose->first_busno, 0, 0);
Stefan Roese738815c2007-10-02 11:44:46 +0200144
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200145 /*
146 * Same constraints as in pcie_read_config().
147 */
148 if (PCI_BUS(devfn) >= 16)
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200149 return 0;
150
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200151 if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
152 ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
153 return 0;
Stefan Roese738815c2007-10-02 11:44:46 +0200154
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200155 address = pcie_get_base(hose, devfn);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200156 offset += devfn << 4;
157
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200158 /*
159 * Suppress MCK exceptions, similar to pcie_read_config()
160 */
161 pcie_dmer_disable ();
162
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200163 switch (len) {
164 case 1:
165 out_8(hose->cfg_data + offset, val);
166 break;
167 case 2:
168 out_le16((u16 *)(hose->cfg_data + offset), val);
169 break;
170 default:
171 out_le32((u32 *)(hose->cfg_data + offset), val);
172 break;
173 }
Grzegorz Bernacki15ee4732007-09-07 17:46:18 +0200174
175 pcie_dmer_enable ();
176
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200177 return 0;
178}
179
180int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
181{
182 u32 v;
183 int rv;
184
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200185 rv = pcie_read_config(hose, dev, offset, 1, &v);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200186 *val = (u8)v;
187 return rv;
188}
189
190int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
191{
192 u32 v;
193 int rv;
194
195 rv = pcie_read_config(hose, dev, offset, 2, &v);
196 *val = (u16)v;
197 return rv;
198}
199
200int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
201{
202 u32 v;
203 int rv;
204
205 rv = pcie_read_config(hose, dev, offset, 3, &v);
206 *val = (u32)v;
207 return rv;
208}
209
210int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
211{
212 return pcie_write_config(hose,(u32)dev,offset,1,val);
213}
214
215int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
216{
217 return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
218}
219
220int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
221{
222 return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
223}
224
Stefan Roese026f7112007-10-03 07:48:09 +0200225static void ppc4xx_setup_utl(u32 port) {
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200226
227 volatile void *utl_base = NULL;
228
229 /*
230 * Map UTL registers
231 */
232 switch (port) {
233 case 0:
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200234 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
235 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
236 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200237 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200238 break;
239
240 case 1:
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200241 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
242 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
243 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200244 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200245 break;
246
247 case 2:
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200248 mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
249 mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
250 mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200251 mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200252 break;
253 }
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200254 utl_base = (unsigned int *)(CFG_PCIE_BASE + 0x1000 * port);
Wolfgang Denk16850912006-08-27 18:10:01 +0200255
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200256 /*
257 * Set buffer allocations and then assert VRB and TXE.
258 */
259 out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
260 out_be32(utl_base + PEUTL_INTR, 0x02000000);
261 out_be32(utl_base + PEUTL_OPDBSZ, 0x10000000);
262 out_be32(utl_base + PEUTL_PBBSZ, 0x53000000);
263 out_be32(utl_base + PEUTL_IPHBSZ, 0x08000000);
264 out_be32(utl_base + PEUTL_IPDBSZ, 0x10000000);
265 out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200266 out_be32(utl_base + PEUTL_PCTL, 0x80800066);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200267}
268
269static int check_error(void)
270{
271 u32 valPE0, valPE1, valPE2;
272 int err = 0;
273
274 /* SDR0_PEGPLLLCT1 reset */
275 if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
276 printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
277 }
278
279 valPE0 = SDR_READ(PESDR0_RCSSET);
280 valPE1 = SDR_READ(PESDR1_RCSSET);
281 valPE2 = SDR_READ(PESDR2_RCSSET);
282
283 /* SDR0_PExRCSSET rstgu */
284 if (!(valPE0 & 0x01000000) ||
285 !(valPE1 & 0x01000000) ||
286 !(valPE2 & 0x01000000)) {
287 printf("PCIE: SDR0_PExRCSSET rstgu error\n");
288 err = -1;
289 }
290
291 /* SDR0_PExRCSSET rstdl */
292 if (!(valPE0 & 0x00010000) ||
293 !(valPE1 & 0x00010000) ||
294 !(valPE2 & 0x00010000)) {
295 printf("PCIE: SDR0_PExRCSSET rstdl error\n");
296 err = -1;
297 }
298
299 /* SDR0_PExRCSSET rstpyn */
300 if ((valPE0 & 0x00001000) ||
301 (valPE1 & 0x00001000) ||
302 (valPE2 & 0x00001000)) {
303 printf("PCIE: SDR0_PExRCSSET rstpyn error\n");
304 err = -1;
305 }
306
307 /* SDR0_PExRCSSET hldplb */
308 if ((valPE0 & 0x10000000) ||
309 (valPE1 & 0x10000000) ||
310 (valPE2 & 0x10000000)) {
311 printf("PCIE: SDR0_PExRCSSET hldplb error\n");
312 err = -1;
313 }
314
315 /* SDR0_PExRCSSET rdy */
316 if ((valPE0 & 0x00100000) ||
317 (valPE1 & 0x00100000) ||
318 (valPE2 & 0x00100000)) {
319 printf("PCIE: SDR0_PExRCSSET rdy error\n");
320 err = -1;
321 }
322
323 /* SDR0_PExRCSSET shutdown */
324 if ((valPE0 & 0x00000100) ||
325 (valPE1 & 0x00000100) ||
326 (valPE2 & 0x00000100)) {
327 printf("PCIE: SDR0_PExRCSSET shutdown error\n");
328 err = -1;
329 }
330 return err;
331}
332
333/*
334 * Initialize PCI Express core
335 */
Stefan Roese026f7112007-10-03 07:48:09 +0200336int ppc4xx_init_pcie(void)
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200337{
338 int time_out = 20;
339
340 /* Set PLL clock receiver to LVPECL */
341 SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
342
343 if (check_error())
344 return -1;
345
346 if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
347 {
348 printf("PCIE: PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
349 SDR_READ(PESDR0_PLLLCT2));
350 return -1;
351 }
352 /* De-assert reset of PCIe PLL, wait for lock */
353 SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
354 udelay(3);
355
Stefan Roese2b393b02006-08-29 08:05:15 +0200356 while (time_out) {
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200357 if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
358 time_out--;
359 udelay(1);
360 } else
361 break;
362 }
363 if (!time_out) {
364 printf("PCIE: VCO output not locked\n");
365 return -1;
366 }
367 return 0;
368}
369
Stefan Roese2b393b02006-08-29 08:05:15 +0200370/*
Stefan Roese03d344b2007-10-03 10:38:09 +0200371 * Board-specific pcie initialization
372 * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
373 */
374
375/*
376 * Initialize various parts of the PCI Express core for our port:
377 *
378 * - Set as a root port and enable max width
379 * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
380 * - Set up UTL configuration.
381 * - Increase SERDES drive strength to levels suggested by AMCC.
382 * - De-assert RSTPYN, RSTDL and RSTGU.
383 *
384 * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
385 * with default setting 0x11310000. The register has new fields,
386 * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
387 * hang.
388 */
389#if defined(CONFIG_440SPE)
390int __ppc4xx_init_pcie_port_hw(int port, int rootport)
391{
392 u32 val = 1 << 24;
393 u32 utlset1;
394
395 if (rootport) {
396 val = PTYPE_ROOT_PORT << 20;
397 utlset1 = 0x21222222;
398 } else {
399 val = PTYPE_LEGACY_ENDPOINT << 20;
400 utlset1 = 0x20222222;
401 }
402
403 if (port == 0)
404 val |= LNKW_X8 << 12;
405 else
406 val |= LNKW_X4 << 12;
407
408 SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
409 SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
410 if (!ppc440spe_revB())
411 SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
412 SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
413 SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
414 SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
415 SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
416 if (port == 0) {
417 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
418 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
419 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
420 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
421 }
422 SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
423 ~(1 << 24 | 1 << 16)) | 1 << 12);
424
425 return 0;
426}
427#endif /* CONFIG_440SPE */
428
429#if defined(CONFIG_405EX)
430int __ppc4xx_init_pcie_port_hw(int port, int rootport)
431{
432 u32 val;
433
434 if (rootport)
435 val = 0x00401000;
436 else
437 val = 0x00101000;
438
439 SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
440 SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x20222222);
441 SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01110000);
442 SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
443 SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
444
445 /* Assert the PE0_PHY reset */
446 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
447 udelay(1000);
448
449 /* deassert the PE0_hotreset */
450 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
451
452 /* poll for phy !reset */
453 while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
454 ;
455
456 /* deassert the PE0_gpl_utl_reset */
457 SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
458
459 if (port == 0)
460 mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000); /* guarded on */
461 else
462 mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000); /* guarded on */
463
464 return 0;
465}
466#endif /* CONFIG_405EX */
467
468int ppc4xx_init_pcie_port_hw(int port, int rootport)
469 __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
470
471/*
472 * We map PCI Express configuration access into the 512MB regions
473 *
474 * NOTICE: revB is very strict about PLB real addressess and ranges to
475 * be mapped for config space; it seems to only work with d_nnnn_nnnn
476 * range (hangs the core upon config transaction attempts when set
477 * otherwise) while revA uses c_nnnn_nnnn.
478 *
479 * For revA:
480 * PCIE0: 0xc_4000_0000
481 * PCIE1: 0xc_8000_0000
482 * PCIE2: 0xc_c000_0000
483 *
484 * For revB:
485 * PCIE0: 0xd_0000_0000
486 * PCIE1: 0xd_2000_0000
487 * PCIE2: 0xd_4000_0000
488 *
489 * For 405EX:
490 * PCIE0: 0xa000_0000
491 * PCIE1: 0xc000_0000
492 */
493static inline u64 ppc4xx_get_cfgaddr(int port)
494{
495#if defined(CONFIG_405EX)
496 if (port == 0)
497 return (u64)CFG_PCIE0_CFGBASE;
498 else
499 return (u64)CFG_PCIE1_CFGBASE;
500#endif
501#if defined(CONFIG_440SPE)
502 if (ppc440spe_revB()) {
503 switch (port) {
504 default: /* to satisfy compiler */
505 case 0:
506 return 0x0000000d00000000ULL;
507 case 1:
508 return 0x0000000d20000000ULL;
509 case 2:
510 return 0x0000000d40000000ULL;
511 }
512 } else {
513 switch (port) {
514 default: /* to satisfy compiler */
515 case 0:
516 return 0x0000000c40000000ULL;
517 case 1:
518 return 0x0000000c80000000ULL;
519 case 2:
520 return 0x0000000cc0000000ULL;
521 }
522 }
523#endif
524}
525
526/*
527 * 4xx boards as end point and root point setup
Stefan Roese2b393b02006-08-29 08:05:15 +0200528 * and
529 * testing inbound and out bound windows
530 *
Stefan Roese03d344b2007-10-03 10:38:09 +0200531 * 4xx boards can be plugged into another 4xx boards or you can get PCI-E
Stefan Roese2b393b02006-08-29 08:05:15 +0200532 * cable which can be used to setup loop back from one port to another port.
533 * Please rememeber that unless there is a endpoint plugged in to root port it
534 * will not initialize. It is the same in case of endpoint , unless there is
535 * root port attached it will not initialize.
536 *
537 * In this release of software all the PCI-E ports are configured as either
538 * endpoint or rootpoint.In future we will have support for selective ports
539 * setup as endpoint and root point in single board.
540 *
541 * Once your board came up as root point , you can verify by reading
542 * /proc/bus/pci/devices. Where you can see the configuration registers
543 * of end point device attached to the port.
544 *
Stefan Roese03d344b2007-10-03 10:38:09 +0200545 * Enpoint cofiguration can be verified by connecting 4xx board to any
546 * host or another 4xx board. Then try to scan the device. In case of
Stefan Roese2b393b02006-08-29 08:05:15 +0200547 * linux use "lspci" or appripriate os command.
548 *
Stefan Roese03d344b2007-10-03 10:38:09 +0200549 * How do I verify the inbound and out bound windows ? (4xx to 4xx)
Stefan Roese2b393b02006-08-29 08:05:15 +0200550 * in this configuration inbound and outbound windows are setup to access
551 * sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
552 * is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
553 * This is waere your POM(PLB out bound memory window) mapped. then
Stefan Roese03d344b2007-10-03 10:38:09 +0200554 * read the data from other 4xx board's u-boot prompt at address
Stefan Roese2b393b02006-08-29 08:05:15 +0200555 * 0x9000 0000(SRAM). Data should match.
556 * In case of inbound , write data to u-boot command prompt at 0xb000 0000
557 * which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
558 * data at 0x9000 0000(SRAM).Data should match.
559 */
Stefan Roese03d344b2007-10-03 10:38:09 +0200560int ppc4xx_init_pcie_port(int port, int rootport)
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200561{
562 static int core_init;
563 volatile u32 val = 0;
564 int attempts;
Stefan Roese03d344b2007-10-03 10:38:09 +0200565 u64 addr;
566 u32 low, high;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200567
568 if (!core_init) {
569 ++core_init;
Stefan Roese026f7112007-10-03 07:48:09 +0200570 if (ppc4xx_init_pcie())
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200571 return -1;
572 }
573
574 /*
Stefan Roese03d344b2007-10-03 10:38:09 +0200575 * Initialize various parts of the PCI Express core for our port
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200576 */
Stefan Roese03d344b2007-10-03 10:38:09 +0200577 ppc4xx_init_pcie_port_hw(port, rootport);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200578
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200579 /*
580 * Notice: the following delay has critical impact on device
581 * initialization - if too short (<50ms) the link doesn't get up.
582 */
583 mdelay(100);
584
Stefan Roese03d344b2007-10-03 10:38:09 +0200585 val = SDR_READ(SDRN_PESDR_RCSSTS(sdr_base(port)));
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200586 if (val & (1 << 20)) {
587 printf("PCIE%d: PGRST failed %08x\n", port, val);
588 return -1;
589 }
590
591 /*
592 * Verify link is up
593 */
Stefan Roese03d344b2007-10-03 10:38:09 +0200594 val = SDR_READ(SDRN_PESDR_LOOP(sdr_base(port)));
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200595 if (!(val & 0x00001000)) {
596 printf("PCIE%d: link is not up.\n", port);
597 return -1;
598 }
599
600 /*
601 * Setup UTL registers - but only on revA!
602 * We use default settings for revB chip.
603 */
604 if (!ppc440spe_revB())
Stefan Roese026f7112007-10-03 07:48:09 +0200605 ppc4xx_setup_utl(port);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200606
607 /*
608 * We map PCI Express configuration access into the 512MB regions
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200609 */
Stefan Roese03d344b2007-10-03 10:38:09 +0200610 addr = ppc4xx_get_cfgaddr(port);
611 low = (u32)(addr & 0x00000000ffffffff);
612 high = (u32)(addr >> 32);
Rafal Jaworowski36b904a2006-08-11 12:35:52 +0200613
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200614 switch (port) {
615 case 0:
Stefan Roese03d344b2007-10-03 10:38:09 +0200616 mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
617 mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200618 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
619 break;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200620 case 1:
Stefan Roese03d344b2007-10-03 10:38:09 +0200621 mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
622 mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200623 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
624 break;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200625 case 2:
Stefan Roese03d344b2007-10-03 10:38:09 +0200626 mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
627 mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200628 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
629 break;
630 }
631
632 /*
633 * Check for VC0 active and assert RDY.
634 */
635 attempts = 10;
Stefan Roese03d344b2007-10-03 10:38:09 +0200636 while(!(SDR_READ(SDRN_PESDR_RCSSTS(sdr_base(port))) & (1 << 16))) {
637 if (!(attempts--)) {
638 printf("PCIE%d: VC0 not active\n", port);
639 return -1;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200640 }
Stefan Roese03d344b2007-10-03 10:38:09 +0200641 mdelay(1000);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200642 }
Stefan Roese03d344b2007-10-03 10:38:09 +0200643 SDR_WRITE(SDRN_PESDR_RCSSET(sdr_base(port)),
644 SDR_READ(SDRN_PESDR_RCSSET(sdr_base(port))) | 1 << 20);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200645 mdelay(100);
646
647 return 0;
648}
649
Stefan Roese03d344b2007-10-03 10:38:09 +0200650int ppc4xx_init_pcie_rootport(int port)
651{
652 return ppc4xx_init_pcie_port(port, 1);
653}
654
Stefan Roese026f7112007-10-03 07:48:09 +0200655int ppc4xx_init_pcie_endport(int port)
Stefan Roese2b393b02006-08-29 08:05:15 +0200656{
Stefan Roese03d344b2007-10-03 10:38:09 +0200657 return ppc4xx_init_pcie_port(port, 0);
Stefan Roese2b393b02006-08-29 08:05:15 +0200658}
659
Stefan Roese026f7112007-10-03 07:48:09 +0200660void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200661{
662 volatile void *mbase = NULL;
Stefan Roese2b393b02006-08-29 08:05:15 +0200663 volatile void *rmbase = NULL;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200664
665 pci_set_ops(hose,
Stefan Roese03d344b2007-10-03 10:38:09 +0200666 pcie_read_config_byte,
667 pcie_read_config_word,
668 pcie_read_config_dword,
669 pcie_write_config_byte,
670 pcie_write_config_word,
671 pcie_write_config_dword);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200672
Stefan Roese2b393b02006-08-29 08:05:15 +0200673 switch (port) {
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200674 case 0:
675 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
Stefan Roese2b393b02006-08-29 08:05:15 +0200676 rmbase = (u32 *)CFG_PCIE0_CFGBASE;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200677 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
678 break;
679 case 1:
680 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
Stefan Roese2b393b02006-08-29 08:05:15 +0200681 rmbase = (u32 *)CFG_PCIE1_CFGBASE;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200682 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
683 break;
684 case 2:
685 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
Stefan Roese2b393b02006-08-29 08:05:15 +0200686 rmbase = (u32 *)CFG_PCIE2_CFGBASE;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200687 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
688 break;
689 }
690
691 /*
692 * Set bus numbers on our root port
693 */
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200694 out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
695 out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
696 out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200697
698 /*
699 * Set up outbound translation to hose->mem_space from PLB
700 * addresses at an offset of 0xd_0000_0000. We set the low
701 * bits of the mask to 11 to turn off splitting into 8
702 * subregions and to enable the outbound translation.
703 */
704 out_le32(mbase + PECFG_POM0LAH, 0x00000000);
Stefan Roese2b393b02006-08-29 08:05:15 +0200705 out_le32(mbase + PECFG_POM0LAL, 0x00000000);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200706
707 switch (port) {
708 case 0:
709 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), 0x0000000d);
710 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
Stefan Roese03d344b2007-10-03 10:38:09 +0200711 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200712 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
713 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
Stefan Roese03d344b2007-10-03 10:38:09 +0200714 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200715 break;
716 case 1:
717 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), 0x0000000d);
Stefan Roese03d344b2007-10-03 10:38:09 +0200718 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
719 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200720 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
721 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
Stefan Roese03d344b2007-10-03 10:38:09 +0200722 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200723 break;
724 case 2:
725 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), 0x0000000d);
Stefan Roese03d344b2007-10-03 10:38:09 +0200726 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
727 port * CFG_PCIE_MEMSIZE);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200728 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
729 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
Stefan Roese03d344b2007-10-03 10:38:09 +0200730 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200731 break;
732 }
733
734 /* Set up 16GB inbound memory window at 0 */
735 out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
736 out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
737 out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
738 out_le32(mbase + PECFG_BAR0LMPA, 0);
Stefan Roese2b393b02006-08-29 08:05:15 +0200739
740 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
741 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200742 out_le32(mbase + PECFG_PIM0LAL, 0);
743 out_le32(mbase + PECFG_PIM0LAH, 0);
Stefan Roese2b393b02006-08-29 08:05:15 +0200744 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
745 out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200746 out_le32(mbase + PECFG_PIMEN, 0x1);
747
748 /* Enable I/O, Mem, and Busmaster cycles */
749 out_le16((u16 *)(mbase + PCI_COMMAND),
750 in_le16((u16 *)(mbase + PCI_COMMAND)) |
751 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Stefan Roese738815c2007-10-02 11:44:46 +0200752
Grzegorz Bernacki7f191392007-09-07 18:20:23 +0200753 /* Set Device and Vendor Id */
754 switch (port) {
755 case 0:
756 out_le16(mbase + 0x200, 0xaaa0);
757 out_le16(mbase + 0x202, 0xbed0);
758 break;
759 case 1:
760 out_le16(mbase + 0x200, 0xaaa1);
761 out_le16(mbase + 0x202, 0xbed1);
762 break;
763 case 2:
764 out_le16(mbase + 0x200, 0xaaa2);
765 out_le16(mbase + 0x202, 0xbed2);
766 break;
767 default:
768 out_le16(mbase + 0x200, 0xaaa3);
769 out_le16(mbase + 0x202, 0xbed3);
770 }
771
772 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
773 out_le32(mbase + 0x208, 0x06040001);
774
Stefan Roese03d344b2007-10-03 10:38:09 +0200775 printf("PCIE:%d successfully set as rootpoint\n", port);
Stefan Roese2b393b02006-08-29 08:05:15 +0200776}
777
Stefan Roese026f7112007-10-03 07:48:09 +0200778int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
Stefan Roese2b393b02006-08-29 08:05:15 +0200779{
780 volatile void *mbase = NULL;
781 int attempts = 0;
782
783 pci_set_ops(hose,
784 pcie_read_config_byte,
785 pcie_read_config_word,
786 pcie_read_config_dword,
787 pcie_write_config_byte,
788 pcie_write_config_word,
789 pcie_write_config_dword);
790
791 switch (port) {
792 case 0:
793 mbase = (u32 *)CFG_PCIE0_XCFGBASE;
794 hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
795 break;
796 case 1:
797 mbase = (u32 *)CFG_PCIE1_XCFGBASE;
798 hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
799 break;
800 case 2:
801 mbase = (u32 *)CFG_PCIE2_XCFGBASE;
802 hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
803 break;
804 }
805
806 /*
807 * Set up outbound translation to hose->mem_space from PLB
808 * addresses at an offset of 0xd_0000_0000. We set the low
809 * bits of the mask to 11 to turn off splitting into 8
810 * subregions and to enable the outbound translation.
811 */
812 out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
813 out_le32(mbase + PECFG_POM0LAL, 0x00001000);
814
815 switch (port) {
816 case 0:
817 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), 0x0000000d);
818 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
Stefan Roese03d344b2007-10-03 10:38:09 +0200819 port * CFG_PCIE_MEMSIZE);
Stefan Roese2b393b02006-08-29 08:05:15 +0200820 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
821 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
Stefan Roese03d344b2007-10-03 10:38:09 +0200822 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese2b393b02006-08-29 08:05:15 +0200823 break;
824 case 1:
825 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), 0x0000000d);
Stefan Roese03d344b2007-10-03 10:38:09 +0200826 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
827 port * CFG_PCIE_MEMSIZE);
Stefan Roese2b393b02006-08-29 08:05:15 +0200828 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
829 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
Stefan Roese03d344b2007-10-03 10:38:09 +0200830 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese2b393b02006-08-29 08:05:15 +0200831 break;
832 case 2:
833 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), 0x0000000d);
Stefan Roese03d344b2007-10-03 10:38:09 +0200834 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
835 port * CFG_PCIE_MEMSIZE);
Stefan Roese2b393b02006-08-29 08:05:15 +0200836 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
837 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
Stefan Roese03d344b2007-10-03 10:38:09 +0200838 ~(CFG_PCIE_MEMSIZE - 1) | 3);
Stefan Roese2b393b02006-08-29 08:05:15 +0200839 break;
840 }
841
842 /* Set up 16GB inbound memory window at 0 */
843 out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
844 out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
845 out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
846 out_le32(mbase + PECFG_BAR0LMPA, 0);
847 out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
848 out_le32(mbase + PECFG_PIM0LAH, 0x00000004); /* pointing to SRAM */
849 out_le32(mbase + PECFG_PIMEN, 0x1);
850
851 /* Enable I/O, Mem, and Busmaster cycles */
852 out_le16((u16 *)(mbase + PCI_COMMAND),
Stefan Roese03d344b2007-10-03 10:38:09 +0200853 in_le16((u16 *)(mbase + PCI_COMMAND)) |
854 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Stefan Roese2b393b02006-08-29 08:05:15 +0200855 out_le16(mbase + 0x200,0xcaad); /* Setting vendor ID */
856 out_le16(mbase + 0x202,0xfeed); /* Setting device ID */
Stefan Roese03d344b2007-10-03 10:38:09 +0200857
Stefan Roese2b393b02006-08-29 08:05:15 +0200858 attempts = 10;
Stefan Roese03d344b2007-10-03 10:38:09 +0200859 while(!(SDR_READ(SDRN_PESDR_RCSSTS(sdr_base(port))) & (1 << 8))) {
860 if (!(attempts--)) {
861 printf("PCIE%d: BME not active\n", port);
862 return -1;
Stefan Roese2b393b02006-08-29 08:05:15 +0200863 }
Stefan Roese03d344b2007-10-03 10:38:09 +0200864 mdelay(1000);
Stefan Roese2b393b02006-08-29 08:05:15 +0200865 }
Stefan Roese03d344b2007-10-03 10:38:09 +0200866
Stefan Roese2b393b02006-08-29 08:05:15 +0200867 printf("PCIE:%d successfully set as endpoint\n",port);
868
869 return 0;
Rafal Jaworowski692519b2006-08-10 12:43:17 +0200870}
Stefan Roese5fb692c2007-01-18 10:25:34 +0100871#endif /* CONFIG_440SPE && CONFIG_PCI */