blob: c416bcebf6aa91d32685051bcc2531f3bdd11be5 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Stefan Roese5fb692c2007-01-18 10:25:34 +01002 * (C) Copyright 2005-2007
Stefan Roese5568e612005-11-22 13:20:42 +01003 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Stefan Roese62534be2006-03-17 10:28:24 +01005 * (C) Copyright 2006
6 * DAVE Srl <www.dave-tech.it>
7 *
stroesede8d5a32004-07-15 14:41:13 +00008 * (C) Copyright 2002-2004
wdenkc6097192002-11-03 00:24:07 +00009 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
10 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +020011 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000012 */
13
14#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020015#include <asm/ppc4xx.h>
wdenkc6097192002-11-03 00:24:07 +000016#include <asm/processor.h>
Stefan Roese62534be2006-03-17 10:28:24 +010017#include "sdram.h"
Grant Ericksonc821b5f2008-05-22 14:44:14 -070018#include "ecc.h"
wdenkc6097192002-11-03 00:24:07 +000019
Simon Glass088454c2017-03-31 08:40:25 -060020DECLARE_GLOBAL_DATA_PTR;
21
wdenkc6097192002-11-03 00:24:07 +000022#ifdef CONFIG_SDRAM_BANK0
23
Stefan Roese5fb692c2007-01-18 10:25:34 +010024#ifndef CONFIG_440
wdenkc6097192002-11-03 00:24:07 +000025
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026#ifndef CONFIG_SYS_SDRAM_TABLE
stroesede8d5a32004-07-15 14:41:13 +000027sdram_conf_t mb0cf[] = {
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020028 {(128 << 20), 13, 0x000A4001}, /* (0-128MB) Address Mode 3, 13x10(4) */
29 {(64 << 20), 13, 0x00084001}, /* (0-64MB) Address Mode 3, 13x9(4) */
30 {(32 << 20), 12, 0x00062001}, /* (0-32MB) Address Mode 2, 12x9(4) */
31 {(16 << 20), 12, 0x00046001}, /* (0-16MB) Address Mode 4, 12x8(4) */
32 {(4 << 20), 11, 0x00008001}, /* (0-4MB) Address Mode 5, 11x8(2) */
stroesede8d5a32004-07-15 14:41:13 +000033};
Stefan Roese5568e612005-11-22 13:20:42 +010034#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020035sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
Stefan Roese5568e612005-11-22 13:20:42 +010036#endif
37
Robert P. J. Dayb7707b02016-05-23 06:49:21 -040038#define N_MB0CF (ARRAY_SIZE(mb0cf))
stroesede8d5a32004-07-15 14:41:13 +000039
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040#ifdef CONFIG_SYS_SDRAM_CASL
Stefan Roese62534be2006-03-17 10:28:24 +010041static ulong ns2clks(ulong ns)
42{
43 ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10);
44
45 return ((ns * 10) + bus_period_x_10) / bus_period_x_10;
46}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047#endif /* CONFIG_SYS_SDRAM_CASL */
Stefan Roese62534be2006-03-17 10:28:24 +010048
49static ulong compute_sdtr1(ulong speed)
50{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifdef CONFIG_SYS_SDRAM_CASL
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020052 ulong tmp;
53 ulong sdtr1 = 0;
Stefan Roese62534be2006-03-17 10:28:24 +010054
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020055 /* CASL */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056 if (CONFIG_SYS_SDRAM_CASL < 2)
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020057 sdtr1 |= (1 << SDRAM0_TR_CASL);
58 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020059 if (CONFIG_SYS_SDRAM_CASL > 4)
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020060 sdtr1 |= (3 << SDRAM0_TR_CASL);
61 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062 sdtr1 |= ((CONFIG_SYS_SDRAM_CASL-1) << SDRAM0_TR_CASL);
Stefan Roese62534be2006-03-17 10:28:24 +010063
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020064 /* PTA */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065 tmp = ns2clks(CONFIG_SYS_SDRAM_PTA);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020066 if ((tmp >= 2) && (tmp <= 4))
67 sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA);
68 else
69 sdtr1 |= ((4-1) << SDRAM0_TR_PTA);
Stefan Roese62534be2006-03-17 10:28:24 +010070
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020071 /* CTP */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020072 tmp = ns2clks(CONFIG_SYS_SDRAM_CTP);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020073 if ((tmp >= 2) && (tmp <= 4))
74 sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP);
75 else
76 sdtr1 |= ((4-1) << SDRAM0_TR_CTP);
Stefan Roese62534be2006-03-17 10:28:24 +010077
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020078 /* LDF */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079 tmp = ns2clks(CONFIG_SYS_SDRAM_LDF);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020080 if ((tmp >= 2) && (tmp <= 4))
81 sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF);
82 else
83 sdtr1 |= ((2-1) << SDRAM0_TR_LDF);
Stefan Roese62534be2006-03-17 10:28:24 +010084
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020085 /* RFTA */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086 tmp = ns2clks(CONFIG_SYS_SDRAM_RFTA);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020087 if ((tmp >= 4) && (tmp <= 10))
88 sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA);
89 else
90 sdtr1 |= ((10-4) << SDRAM0_TR_RFTA);
Stefan Roese62534be2006-03-17 10:28:24 +010091
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020092 /* RCD */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020093 tmp = ns2clks(CONFIG_SYS_SDRAM_RCD);
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020094 if ((tmp >= 2) && (tmp <= 4))
95 sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD);
96 else
97 sdtr1 |= ((4-1) << SDRAM0_TR_RCD);
Stefan Roese62534be2006-03-17 10:28:24 +010098
Wolfgang Denkcf48eb92006-04-16 10:51:58 +020099 return sdtr1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100#else /* CONFIG_SYS_SDRAM_CASL */
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200101 /*
102 * If no values are configured in the board config file
103 * use the default values, which seem to be ok for most
104 * boards.
105 *
106 * REMARK:
107 * For new board ports we strongly recommend to define the
108 * correct values for the used SDRAM chips in your board
109 * config file (see PPChameleonEVB.h)
110 */
111 if (speed > 100000000) {
112 /*
113 * 133 MHz SDRAM
114 */
115 return 0x01074015;
116 } else {
117 /*
118 * default: 100 MHz SDRAM
119 */
120 return 0x0086400d;
121 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122#endif /* CONFIG_SYS_SDRAM_CASL */
Stefan Roese62534be2006-03-17 10:28:24 +0100123}
124
125/* refresh is expressed in ms */
126static ulong compute_rtr(ulong speed, ulong rows, ulong refresh)
127{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200128#ifdef CONFIG_SYS_SDRAM_CASL
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200129 ulong tmp;
Stefan Roese62534be2006-03-17 10:28:24 +0100130
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200131 tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000);
132 tmp /= 1000000;
Stefan Roese62534be2006-03-17 10:28:24 +0100133
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200134 return ((tmp & 0x00003FF8) << 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200135#else /* CONFIG_SYS_SDRAM_CASL */
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200136 if (speed > 100000000) {
137 /*
138 * 133 MHz SDRAM
139 */
Stefan Roese62534be2006-03-17 10:28:24 +0100140 return 0x07f00000;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200141 } else {
142 /*
143 * default: 100 MHz SDRAM
144 */
Stefan Roese62534be2006-03-17 10:28:24 +0100145 return 0x05f00000;
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200146 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200147#endif /* CONFIG_SYS_SDRAM_CASL */
Stefan Roese62534be2006-03-17 10:28:24 +0100148}
149
Stefan Roese5568e612005-11-22 13:20:42 +0100150/*
151 * Autodetect onboard SDRAM on 405 platforms
152 */
Simon Glassf1683aa2017-04-06 12:47:05 -0600153int dram_init(void)
wdenkc6097192002-11-03 00:24:07 +0000154{
Stefan Roese62534be2006-03-17 10:28:24 +0100155 ulong speed;
wdenkc6097192002-11-03 00:24:07 +0000156 ulong sdtr1;
stroesede8d5a32004-07-15 14:41:13 +0000157 int i;
wdenkc6097192002-11-03 00:24:07 +0000158
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200159 /*
160 * Determine SDRAM speed
161 */
162 speed = get_bus_freq(0); /* parameter not used on ppc4xx */
Stefan Roese62534be2006-03-17 10:28:24 +0100163
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200164 /*
165 * sdtr1 (register SDRAM0_TR) must take into account timings listed
166 * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into
167 * account actual SDRAM size. So we can set up sdtr1 according to what
168 * is specified in board configuration file while rtr dependds on SDRAM
169 * size we are assuming before detection.
170 */
171 sdtr1 = compute_sdtr1(speed);
wdenkc6097192002-11-03 00:24:07 +0000172
stroesede8d5a32004-07-15 14:41:13 +0000173 for (i=0; i<N_MB0CF; i++) {
stroese61774452003-02-10 16:26:37 +0000174 /*
stroesede8d5a32004-07-15 14:41:13 +0000175 * Disable memory controller.
stroese61774452003-02-10 16:26:37 +0000176 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200177 mtsdram(SDRAM0_CFG, 0x00000000);
wdenke5ad56b2003-02-11 01:49:43 +0000178
wdenkc6097192002-11-03 00:24:07 +0000179 /*
stroesede8d5a32004-07-15 14:41:13 +0000180 * Set MB0CF for bank 0.
wdenkc6097192002-11-03 00:24:07 +0000181 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200182 mtsdram(SDRAM0_B0CR, mb0cf[i].reg);
183 mtsdram(SDRAM0_TR, sdtr1);
184 mtsdram(SDRAM0_RTR, compute_rtr(speed, mb0cf[i].rows, 64));
wdenke5ad56b2003-02-11 01:49:43 +0000185
stroesede8d5a32004-07-15 14:41:13 +0000186 udelay(200);
wdenke5ad56b2003-02-11 01:49:43 +0000187
wdenkc6097192002-11-03 00:24:07 +0000188 /*
stroesede8d5a32004-07-15 14:41:13 +0000189 * Set memory controller options reg, MCOPT1.
190 * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
191 * read/prefetch.
wdenkc6097192002-11-03 00:24:07 +0000192 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200193 mtsdram(SDRAM0_CFG, 0x80800000);
stroesede8d5a32004-07-15 14:41:13 +0000194
195 udelay(10000);
196
197 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
Stefan Roese7bf5ecf2008-09-10 16:53:47 +0200198 phys_size_t size = mb0cf[i].size;
199
stroesede8d5a32004-07-15 14:41:13 +0000200 /*
John Otkend4024bb2007-07-26 17:49:11 +0200201 * OK, size detected. Enable second bank if
202 * defined (assumes same type as bank 0)
stroesede8d5a32004-07-15 14:41:13 +0000203 */
John Otkend4024bb2007-07-26 17:49:11 +0200204#ifdef CONFIG_SDRAM_BANK1
Stefan Roese95b602b2009-09-24 13:59:57 +0200205 mtsdram(SDRAM0_CFG, 0x00000000);
206 mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg);
207 mtsdram(SDRAM0_CFG, 0x80800000);
John Otkend4024bb2007-07-26 17:49:11 +0200208 udelay(10000);
Stefan Roese779e9752007-08-14 14:44:41 +0200209
210 /*
211 * Check if 2nd bank is really available.
212 * If the size not equal to the size of the first
213 * bank, then disable the 2nd bank completely.
214 */
215 if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) !=
216 mb0cf[i].size) {
Stefan Roese95b602b2009-09-24 13:59:57 +0200217 mtsdram(SDRAM0_B1CR, 0);
218 mtsdram(SDRAM0_CFG, 0);
Stefan Roese7bf5ecf2008-09-10 16:53:47 +0200219 } else {
220 /*
221 * We have two identical banks, so the size
222 * is twice the bank size
223 */
224 size = 2 * size;
Stefan Roese779e9752007-08-14 14:44:41 +0200225 }
John Otkend4024bb2007-07-26 17:49:11 +0200226#endif
Stefan Roesebbeff302008-06-02 17:37:28 +0200227
228 /*
229 * OK, size detected -> all done
230 */
Simon Glass088454c2017-03-31 08:40:25 -0600231 gd->ram_size = size;
232
233 return 0;
stroesede8d5a32004-07-15 14:41:13 +0000234 }
wdenkc6097192002-11-03 00:24:07 +0000235 }
Stefan Roesebbeff302008-06-02 17:37:28 +0200236
Simon Glass088454c2017-03-31 08:40:25 -0600237 return -ENXIO;
wdenkc6097192002-11-03 00:24:07 +0000238}
239
Stefan Roese5568e612005-11-22 13:20:42 +0100240#else /* CONFIG_440 */
241
Stefan Roese5fb692c2007-01-18 10:25:34 +0100242/*
243 * Define some default values. Those can be overwritten in the
244 * board config file.
245 */
246
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200247#ifndef CONFIG_SYS_SDRAM_TABLE
Stefan Roese5fb692c2007-01-18 10:25:34 +0100248sdram_conf_t mb0cf[] = {
249 {(256 << 20), 13, 0x000C4001}, /* 256MB mode 3, 13x10(4) */
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100250 {(128 << 20), 13, 0x000A4001}, /* 128MB mode 3, 13x10(4) */
Stefan Roese5fb692c2007-01-18 10:25:34 +0100251 {(64 << 20), 12, 0x00082001} /* 64MB mode 2, 12x9(4) */
252};
253#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200254sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
Stefan Roese5fb692c2007-01-18 10:25:34 +0100255#endif
256
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257#ifndef CONFIG_SYS_SDRAM0_TR0
258#define CONFIG_SYS_SDRAM0_TR0 0x41094012
Stefan Roese5fb692c2007-01-18 10:25:34 +0100259#endif
260
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100261#ifndef CONFIG_SYS_SDRAM0_WDDCTR
262#define CONFIG_SYS_SDRAM0_WDDCTR 0x00000000 /* wrcp=0 dcd=0 */
263#endif
264
265#ifndef CONFIG_SYS_SDRAM0_RTR
266#define CONFIG_SYS_SDRAM0_RTR 0x04100000 /* 7.8us @ 133MHz PLB */
267#endif
268
269#ifndef CONFIG_SYS_SDRAM0_CFG0
270#define CONFIG_SYS_SDRAM0_CFG0 0x82000000 /* DCEN=1, PMUD=0, 64-bit */
271#endif
272
Robert P. J. Dayb7707b02016-05-23 06:49:21 -0400273#define N_MB0CF (ARRAY_SIZE(mb0cf))
Stefan Roese5fb692c2007-01-18 10:25:34 +0100274
Stefan Roese62534be2006-03-17 10:28:24 +0100275#define NUM_TRIES 64
276#define NUM_READS 10
277
278static void sdram_tr1_set(int ram_address, int* tr1_value)
279{
280 int i;
281 int j, k;
282 volatile unsigned int* ram_pointer = (unsigned int *)ram_address;
283 int first_good = -1, last_bad = 0x1ff;
284
285 unsigned long test[NUM_TRIES] = {
286 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
287 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
288 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
289 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
290 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
291 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
292 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
293 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
294 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
295 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
296 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
297 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
298 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
299 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
300 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
301 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
302
303 /* go through all possible SDRAM0_TR1[RDCT] values */
304 for (i=0; i<=0x1ff; i++) {
305 /* set the current value for TR1 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200306 mtsdram(SDRAM0_TR1, (0x80800800 | i));
Stefan Roese62534be2006-03-17 10:28:24 +0100307
308 /* write values */
309 for (j=0; j<NUM_TRIES; j++) {
310 ram_pointer[j] = test[j];
311
312 /* clear any cache at ram location */
313 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
314 }
315
316 /* read values back */
317 for (j=0; j<NUM_TRIES; j++) {
318 for (k=0; k<NUM_READS; k++) {
319 /* clear any cache at ram location */
320 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
321
322 if (ram_pointer[j] != test[j])
323 break;
324 }
325
326 /* read error */
327 if (k != NUM_READS)
328 break;
329 }
330
331 /* we have a SDRAM0_TR1[RDCT] that is part of the window */
332 if (j == NUM_TRIES) {
333 if (first_good == -1)
334 first_good = i; /* found beginning of window */
335 } else { /* bad read */
336 /* if we have not had a good read then don't care */
337 if (first_good != -1) {
338 /* first failure after a good read */
339 last_bad = i-1;
340 break;
341 }
342 }
343 }
344
345 /* return the current value for TR1 */
346 *tr1_value = (first_good + last_bad) / 2;
347}
348
Stefan Roese5568e612005-11-22 13:20:42 +0100349/*
350 * Autodetect onboard DDR SDRAM on 440 platforms
351 *
352 * NOTE: Some of the hardcoded values are hardware dependant,
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200353 * so this should be extended for other future boards
354 * using this routine!
Stefan Roese5568e612005-11-22 13:20:42 +0100355 */
Simon Glassf1683aa2017-04-06 12:47:05 -0600356int dram_init(void)
Stefan Roese5568e612005-11-22 13:20:42 +0100357{
358 int i;
Stefan Roese62534be2006-03-17 10:28:24 +0100359 int tr1_bank1;
Stefan Roese5568e612005-11-22 13:20:42 +0100360
Stefan Roese5fb692c2007-01-18 10:25:34 +0100361#if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
362 defined(CONFIG_440GR) || defined(CONFIG_440SP)
Stefan Roese899620c2006-08-15 14:22:35 +0200363 /*
364 * Soft-reset SDRAM controller.
365 */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200366 mtsdr(SDR0_SRST, SDR0_SRST_DMC);
367 mtsdr(SDR0_SRST, 0x00000000);
Stefan Roese899620c2006-08-15 14:22:35 +0200368#endif
369
Stefan Roese5568e612005-11-22 13:20:42 +0100370 for (i=0; i<N_MB0CF; i++) {
371 /*
372 * Disable memory controller.
373 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200374 mtsdram(SDRAM0_CFG0, 0x00000000);
Stefan Roese5568e612005-11-22 13:20:42 +0100375
376 /*
377 * Setup some default
378 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200379 mtsdram(SDRAM0_UABBA, 0x00000000); /* ubba=0 (default) */
380 mtsdram(SDRAM0_SLIO, 0x00000000); /* rdre=0 wrre=0 rarw=0 */
381 mtsdram(SDRAM0_DEVOPT, 0x00000000); /* dll=0 ds=0 (normal) */
382 mtsdram(SDRAM0_WDDCTR, CONFIG_SYS_SDRAM0_WDDCTR);
383 mtsdram(SDRAM0_CLKTR, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0 */
Stefan Roese5568e612005-11-22 13:20:42 +0100384
385 /*
386 * Following for CAS Latency = 2.5 @ 133 MHz PLB
387 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200388 mtsdram(SDRAM0_B0CR, mb0cf[i].reg);
389 mtsdram(SDRAM0_TR0, CONFIG_SYS_SDRAM0_TR0);
390 mtsdram(SDRAM0_TR1, 0x80800800); /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/
391 mtsdram(SDRAM0_RTR, CONFIG_SYS_SDRAM0_RTR);
392 mtsdram(SDRAM0_CFG1, 0x00000000); /* Self-refresh exit, disable PM*/
Stefan Roese5568e612005-11-22 13:20:42 +0100393 udelay(400); /* Delay 200 usecs (min) */
394
395 /*
396 * Enable the controller, then wait for DCEN to complete
397 */
Stefan Roese95b602b2009-09-24 13:59:57 +0200398 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
Stefan Roese5568e612005-11-22 13:20:42 +0100399 udelay(10000);
400
401 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100402 phys_size_t size = mb0cf[i].size;
Stefan Roese5568e612005-11-22 13:20:42 +0100403 /*
Stefan Roese62534be2006-03-17 10:28:24 +0100404 * Optimize TR1 to current hardware environment
405 */
406 sdram_tr1_set(0x00000000, &tr1_bank1);
Stefan Roese95b602b2009-09-24 13:59:57 +0200407 mtsdram(SDRAM0_TR1, (tr1_bank1 | 0x80800800));
Stefan Roese62534be2006-03-17 10:28:24 +0100408
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100409
410 /*
411 * OK, size detected. Enable second bank if
412 * defined (assumes same type as bank 0)
413 */
414#ifdef CONFIG_SDRAM_BANK1
Stefan Roese95b602b2009-09-24 13:59:57 +0200415 mtsdram(SDRAM0_CFG0, 0);
416 mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg);
417 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100418 udelay(10000);
419
420 /*
421 * Check if 2nd bank is really available.
422 * If the size not equal to the size of the first
423 * bank, then disable the 2nd bank completely.
424 */
425 if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size)
426 != mb0cf[i].size) {
Stefan Roese95b602b2009-09-24 13:59:57 +0200427 mtsdram(SDRAM0_CFG0, 0);
428 mtsdram(SDRAM0_B1CR, 0);
429 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100430 udelay(10000);
431 } else {
432 /*
433 * We have two identical banks, so the size
434 * is twice the bank size
435 */
436 size = 2 * size;
437 }
438#endif
439
Stefan Roese62534be2006-03-17 10:28:24 +0100440#ifdef CONFIG_SDRAM_ECC
Dirk Eibach3943d2f2008-12-09 11:00:07 +0100441 ecc_init(0, size);
Stefan Roese62534be2006-03-17 10:28:24 +0100442#endif
443
444 /*
Stefan Roese5568e612005-11-22 13:20:42 +0100445 * OK, size detected -> all done
446 */
Simon Glass088454c2017-03-31 08:40:25 -0600447 gd->ram_size = size;
448
449 return 0;
Stefan Roese5568e612005-11-22 13:20:42 +0100450 }
451 }
452
Simon Glass088454c2017-03-31 08:40:25 -0600453 return -ENXIO; /* nothing found ! */
Stefan Roese5568e612005-11-22 13:20:42 +0100454}
455
456#endif /* CONFIG_440 */
457
wdenkc6097192002-11-03 00:24:07 +0000458#endif /* CONFIG_SDRAM_BANK0 */