2 * (C) Copyright 2005-2007
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
6 * DAVE Srl <www.dave-tech.it>
8 * (C) Copyright 2002-2004
9 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
11 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/ppc4xx.h>
16 #include <asm/processor.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #ifdef CONFIG_SDRAM_BANK0
26 #ifndef CONFIG_SYS_SDRAM_TABLE
27 sdram_conf_t mb0cf[] = {
28 {(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) */
35 sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
38 #define N_MB0CF (ARRAY_SIZE(mb0cf))
40 #ifdef CONFIG_SYS_SDRAM_CASL
41 static ulong ns2clks(ulong ns)
43 ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10);
45 return ((ns * 10) + bus_period_x_10) / bus_period_x_10;
47 #endif /* CONFIG_SYS_SDRAM_CASL */
49 static ulong compute_sdtr1(ulong speed)
51 #ifdef CONFIG_SYS_SDRAM_CASL
56 if (CONFIG_SYS_SDRAM_CASL < 2)
57 sdtr1 |= (1 << SDRAM0_TR_CASL);
59 if (CONFIG_SYS_SDRAM_CASL > 4)
60 sdtr1 |= (3 << SDRAM0_TR_CASL);
62 sdtr1 |= ((CONFIG_SYS_SDRAM_CASL-1) << SDRAM0_TR_CASL);
65 tmp = ns2clks(CONFIG_SYS_SDRAM_PTA);
66 if ((tmp >= 2) && (tmp <= 4))
67 sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA);
69 sdtr1 |= ((4-1) << SDRAM0_TR_PTA);
72 tmp = ns2clks(CONFIG_SYS_SDRAM_CTP);
73 if ((tmp >= 2) && (tmp <= 4))
74 sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP);
76 sdtr1 |= ((4-1) << SDRAM0_TR_CTP);
79 tmp = ns2clks(CONFIG_SYS_SDRAM_LDF);
80 if ((tmp >= 2) && (tmp <= 4))
81 sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF);
83 sdtr1 |= ((2-1) << SDRAM0_TR_LDF);
86 tmp = ns2clks(CONFIG_SYS_SDRAM_RFTA);
87 if ((tmp >= 4) && (tmp <= 10))
88 sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA);
90 sdtr1 |= ((10-4) << SDRAM0_TR_RFTA);
93 tmp = ns2clks(CONFIG_SYS_SDRAM_RCD);
94 if ((tmp >= 2) && (tmp <= 4))
95 sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD);
97 sdtr1 |= ((4-1) << SDRAM0_TR_RCD);
100 #else /* CONFIG_SYS_SDRAM_CASL */
102 * If no values are configured in the board config file
103 * use the default values, which seem to be ok for most
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)
111 if (speed > 100000000) {
118 * default: 100 MHz SDRAM
122 #endif /* CONFIG_SYS_SDRAM_CASL */
125 /* refresh is expressed in ms */
126 static ulong compute_rtr(ulong speed, ulong rows, ulong refresh)
128 #ifdef CONFIG_SYS_SDRAM_CASL
131 tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000);
134 return ((tmp & 0x00003FF8) << 16);
135 #else /* CONFIG_SYS_SDRAM_CASL */
136 if (speed > 100000000) {
143 * default: 100 MHz SDRAM
147 #endif /* CONFIG_SYS_SDRAM_CASL */
151 * Autodetect onboard SDRAM on 405 platforms
160 * Determine SDRAM speed
162 speed = get_bus_freq(0); /* parameter not used on ppc4xx */
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.
171 sdtr1 = compute_sdtr1(speed);
173 for (i=0; i<N_MB0CF; i++) {
175 * Disable memory controller.
177 mtsdram(SDRAM0_CFG, 0x00000000);
180 * Set MB0CF for bank 0.
182 mtsdram(SDRAM0_B0CR, mb0cf[i].reg);
183 mtsdram(SDRAM0_TR, sdtr1);
184 mtsdram(SDRAM0_RTR, compute_rtr(speed, mb0cf[i].rows, 64));
189 * Set memory controller options reg, MCOPT1.
190 * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
193 mtsdram(SDRAM0_CFG, 0x80800000);
197 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
198 phys_size_t size = mb0cf[i].size;
201 * OK, size detected. Enable second bank if
202 * defined (assumes same type as bank 0)
204 #ifdef CONFIG_SDRAM_BANK1
205 mtsdram(SDRAM0_CFG, 0x00000000);
206 mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg);
207 mtsdram(SDRAM0_CFG, 0x80800000);
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.
215 if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) !=
217 mtsdram(SDRAM0_B1CR, 0);
218 mtsdram(SDRAM0_CFG, 0);
221 * We have two identical banks, so the size
222 * is twice the bank size
229 * OK, size detected -> all done
240 #else /* CONFIG_440 */
243 * Define some default values. Those can be overwritten in the
247 #ifndef CONFIG_SYS_SDRAM_TABLE
248 sdram_conf_t mb0cf[] = {
249 {(256 << 20), 13, 0x000C4001}, /* 256MB mode 3, 13x10(4) */
250 {(128 << 20), 13, 0x000A4001}, /* 128MB mode 3, 13x10(4) */
251 {(64 << 20), 12, 0x00082001} /* 64MB mode 2, 12x9(4) */
254 sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
257 #ifndef CONFIG_SYS_SDRAM0_TR0
258 #define CONFIG_SYS_SDRAM0_TR0 0x41094012
261 #ifndef CONFIG_SYS_SDRAM0_WDDCTR
262 #define CONFIG_SYS_SDRAM0_WDDCTR 0x00000000 /* wrcp=0 dcd=0 */
265 #ifndef CONFIG_SYS_SDRAM0_RTR
266 #define CONFIG_SYS_SDRAM0_RTR 0x04100000 /* 7.8us @ 133MHz PLB */
269 #ifndef CONFIG_SYS_SDRAM0_CFG0
270 #define CONFIG_SYS_SDRAM0_CFG0 0x82000000 /* DCEN=1, PMUD=0, 64-bit */
273 #define N_MB0CF (ARRAY_SIZE(mb0cf))
278 static void sdram_tr1_set(int ram_address, int* tr1_value)
282 volatile unsigned int* ram_pointer = (unsigned int *)ram_address;
283 int first_good = -1, last_bad = 0x1ff;
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 };
303 /* go through all possible SDRAM0_TR1[RDCT] values */
304 for (i=0; i<=0x1ff; i++) {
305 /* set the current value for TR1 */
306 mtsdram(SDRAM0_TR1, (0x80800800 | i));
309 for (j=0; j<NUM_TRIES; j++) {
310 ram_pointer[j] = test[j];
312 /* clear any cache at ram location */
313 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
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]));
322 if (ram_pointer[j] != test[j])
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 */
345 /* return the current value for TR1 */
346 *tr1_value = (first_good + last_bad) / 2;
350 * Autodetect onboard DDR SDRAM on 440 platforms
352 * NOTE: Some of the hardcoded values are hardware dependant,
353 * so this should be extended for other future boards
354 * using this routine!
361 #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
362 defined(CONFIG_440GR) || defined(CONFIG_440SP)
364 * Soft-reset SDRAM controller.
366 mtsdr(SDR0_SRST, SDR0_SRST_DMC);
367 mtsdr(SDR0_SRST, 0x00000000);
370 for (i=0; i<N_MB0CF; i++) {
372 * Disable memory controller.
374 mtsdram(SDRAM0_CFG0, 0x00000000);
379 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 */
386 * Following for CAS Latency = 2.5 @ 133 MHz PLB
388 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*/
393 udelay(400); /* Delay 200 usecs (min) */
396 * Enable the controller, then wait for DCEN to complete
398 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
401 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
402 phys_size_t size = mb0cf[i].size;
404 * Optimize TR1 to current hardware environment
406 sdram_tr1_set(0x00000000, &tr1_bank1);
407 mtsdram(SDRAM0_TR1, (tr1_bank1 | 0x80800800));
411 * OK, size detected. Enable second bank if
412 * defined (assumes same type as bank 0)
414 #ifdef CONFIG_SDRAM_BANK1
415 mtsdram(SDRAM0_CFG0, 0);
416 mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg);
417 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
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.
425 if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size)
427 mtsdram(SDRAM0_CFG0, 0);
428 mtsdram(SDRAM0_B1CR, 0);
429 mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0);
433 * We have two identical banks, so the size
434 * is twice the bank size
440 #ifdef CONFIG_SDRAM_ECC
445 * OK, size detected -> all done
453 return -ENXIO; /* nothing found ! */
456 #endif /* CONFIG_440 */
458 #endif /* CONFIG_SDRAM_BANK0 */