blob: 3dfde8ac0479c283fb4ed7abb744c7324a8081f2 [file] [log] [blame]
Eran Liberty9095d4a2005-07-28 10:08:46 -05001/*
Dave Liuf5035922006-10-25 14:41:21 -05002 * (C) Copyright 2006 Freescale Semiconductor, Inc.
3 *
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +01004 * (C) Copyright 2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denkebd3deb2006-04-16 10:51:58 +02006 *
Eran Liberty9095d4a2005-07-28 10:08:46 -05007 * Copyright 2004 Freescale Semiconductor.
8 * (C) Copyright 2003 Motorola Inc.
9 * Xianghua Xiao (X.Xiao@motorola.com)
10 *
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 *
29 * Change log:
30 *
31 * 20050101: Eran Liberty (liberty@freescale.com)
32 * Initial file creating (porting from 85XX & 8260)
Dave Liuf5035922006-10-25 14:41:21 -050033 * 20060601: Dave Liu (daveliu@freescale.com)
34 * DDR ECC support
35 * unify variable names for 83xx
36 * code cleanup
Eran Liberty9095d4a2005-07-28 10:08:46 -050037 */
38
39#include <common.h>
40#include <asm/processor.h>
41#include <i2c.h>
42#include <spd.h>
43#include <asm/mmu.h>
44#include <spd_sdram.h>
45
46#ifdef CONFIG_SPD_EEPROM
47
Dave Liuf5035922006-10-25 14:41:21 -050048#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Liberty9095d4a2005-07-28 10:08:46 -050049extern void dma_init(void);
50extern uint dma_check(void);
51extern int dma_xfer(void *dest, uint count, void *src);
52#endif
53
Eran Liberty9095d4a2005-07-28 10:08:46 -050054#ifndef CFG_READ_SPD
55#define CFG_READ_SPD i2c_read
56#endif
57
Eran Liberty9095d4a2005-07-28 10:08:46 -050058/*
59 * Convert picoseconds into clock cycles (rounding up if needed).
60 */
Dave Liuf5035922006-10-25 14:41:21 -050061extern ulong get_ddr_clk(ulong dummy);
Eran Liberty9095d4a2005-07-28 10:08:46 -050062
63int
64picos_to_clk(int picos)
65{
Dave Liuf5035922006-10-25 14:41:21 -050066 unsigned int ddr_bus_clk;
Eran Liberty9095d4a2005-07-28 10:08:46 -050067 int clks;
68
Dave Liuf5035922006-10-25 14:41:21 -050069 ddr_bus_clk = get_ddr_clk(0) >> 1;
70 clks = picos / ((1000000000 / ddr_bus_clk) * 1000);
71 if (picos % ((1000000000 / ddr_bus_clk) * 1000) !=0) {
72 clks++;
Eran Liberty9095d4a2005-07-28 10:08:46 -050073 }
74
75 return clks;
76}
77
Marian Balakowicz6f6104d2006-03-14 16:23:35 +010078unsigned int banksize(unsigned char row_dens)
Eran Liberty9095d4a2005-07-28 10:08:46 -050079{
80 return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
81}
82
Marian Balakowicz6f6104d2006-03-14 16:23:35 +010083int read_spd(uint addr)
84{
85 return ((int) addr);
86}
87
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +010088#undef SPD_DEBUG
89#ifdef SPD_DEBUG
90static void spd_debug(spd_eeprom_t *spd)
91{
92 printf ("\nDIMM type: %-18.18s\n", spd->mpart);
93 printf ("SPD size: %d\n", spd->info_size);
94 printf ("EEPROM size: %d\n", 1 << spd->chip_size);
95 printf ("Memory type: %d\n", spd->mem_type);
96 printf ("Row addr: %d\n", spd->nrow_addr);
97 printf ("Column addr: %d\n", spd->ncol_addr);
98 printf ("# of rows: %d\n", spd->nrows);
99 printf ("Row density: %d\n", spd->row_dens);
100 printf ("# of banks: %d\n", spd->nbanks);
101 printf ("Data width: %d\n",
102 256 * spd->dataw_msb + spd->dataw_lsb);
103 printf ("Chip width: %d\n", spd->primw);
104 printf ("Refresh rate: %02X\n", spd->refresh);
105 printf ("CAS latencies: %02X\n", spd->cas_lat);
106 printf ("Write latencies: %02X\n", spd->write_lat);
107 printf ("tRP: %d\n", spd->trp);
108 printf ("tRCD: %d\n", spd->trcd);
109 printf ("\n");
110}
111#endif /* SPD_DEBUG */
112
113long int spd_sdram()
Eran Liberty9095d4a2005-07-28 10:08:46 -0500114{
Timur Tabiefec6302006-10-31 18:13:36 -0600115#ifdef CONFIG_MPC834X
116 int caslat_83xx; /* For Errata DDR6 */
117#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500118 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf5035922006-10-25 14:41:21 -0500119 volatile ddr83xx_t *ddr = &immap->ddr;
120 volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
Eran Liberty9095d4a2005-07-28 10:08:46 -0500121 spd_eeprom_t spd;
Timur Tabiefec6302006-10-31 18:13:36 -0600122 unsigned int tmp, tmp1;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500123 unsigned int memsize;
124 unsigned int law_size;
Dave Liuf5035922006-10-25 14:41:21 -0500125 unsigned char caslat, caslat_ctrl;
126 unsigned char burstlen;
127 unsigned int max_bus_clk;
128 unsigned int max_data_rate, effective_data_rate;
129 unsigned int ddrc_clk;
130 unsigned int refresh_clk;
131 unsigned sdram_cfg;
132 unsigned int ddrc_ecc_enable;
Jon Loeligerebc72242005-08-01 13:20:47 -0500133
Dave Liuf5035922006-10-25 14:41:21 -0500134
135 /* Read SPD parameters with I2C */
Timur Tabi054838e2006-10-31 18:44:42 -0600136#ifdef CFG_83XX_DDR_USES_CS0
137 ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
138 ddr->cs_config[0] = ( 1 << 31
139 | (spd.nrow_addr - 12) << 8
140 | (spd.ncol_addr - 8) );
141 debug("\n");
142 debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
143 debug("cs0_config = 0x%08x\n",ddr->cs_config[0]);
144
145 if (spd.nrows == 2) {
146 ddr->csbnds[1].csbnds = ( (banksize(spd.row_dens) >> 8)
147 | ((banksize(spd.row_dens) >> 23) - 1) );
148 ddr->cs_config[1] = ( 1<<31
149 | (spd.nrow_addr-12) << 8
150 | (spd.ncol_addr-8) );
151 debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
152 debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
153 }
154
155#else
Eran Liberty9095d4a2005-07-28 10:08:46 -0500156 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100157#ifdef SPD_DEBUG
158 spd_debug(&spd);
159#endif
Dave Liuf5035922006-10-25 14:41:21 -0500160 /* Check the memory type */
161 if (spd.mem_type != SPD_MEMTYPE_DDR) {
162 printf("DDR: Module mem type is %02X\n", spd.mem_type);
163 return 0;
164 }
165
166 /* Check the number of physical bank */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500167 if (spd.nrows > 2) {
Dave Liuf5035922006-10-25 14:41:21 -0500168 printf("DDR: The number of physical bank is %02X\n", spd.nrows);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500169 return 0;
170 }
171
Dave Liuf5035922006-10-25 14:41:21 -0500172 /* Check if the number of row of the module is in the range of DDRC */
173 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
174 printf("DDR: Row number is out of range of DDRC, row=%02X\n",
175 spd.nrow_addr);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500176 return 0;
177 }
178
Dave Liuf5035922006-10-25 14:41:21 -0500179 /* Check if the number of col of the module is in the range of DDRC */
180 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
181 printf("DDR: Col number is out of range of DDRC, col=%02X\n",
182 spd.ncol_addr);
183 return 0;
184 }
185 /* Setup DDR chip select register */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500186 ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
187 ddr->cs_config[2] = ( 1 << 31
188 | (spd.nrow_addr - 12) << 8
189 | (spd.ncol_addr - 8) );
190 debug("\n");
191 debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
192 debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
Jon Loeligerebc72242005-08-01 13:20:47 -0500193
Eran Liberty9095d4a2005-07-28 10:08:46 -0500194 if (spd.nrows == 2) {
195 ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
196 | ((banksize(spd.row_dens) >> 23) - 1) );
197 ddr->cs_config[3] = ( 1<<31
198 | (spd.nrow_addr-12) << 8
199 | (spd.ncol_addr-8) );
200 debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
201 debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
202 }
Timur Tabi054838e2006-10-31 18:44:42 -0600203#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500204
205 if (spd.mem_type != 0x07) {
206 puts("No DDR module found!\n");
207 return 0;
208 }
209
210 /*
211 * Figure out memory size in Megabytes.
212 */
213 memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
214
215 /*
216 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
217 */
218 law_size = 19 + __ilog2(memsize);
219
220 /*
221 * Set up LAWBAR for all of DDR.
222 */
223 ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
224 ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
225 debug("DDR:bar=0x%08x\n", ecm->bar);
226 debug("DDR:ar=0x%08x\n", ecm->ar);
227
228 /*
Dave Liuf5035922006-10-25 14:41:21 -0500229 * Find the largest CAS by locating the highest 1 bit
230 * in the spd.cas_lat field. Translate it to a DDR
231 * controller field value:
232 *
233 * CAS Lat DDR I Ctrl
234 * Clocks SPD Bit Value
235 * -------+--------+---------
236 * 1.0 0 001
237 * 1.5 1 010
238 * 2.0 2 011
239 * 2.5 3 100
240 * 3.0 4 101
241 * 3.5 5 110
242 * 4.0 6 111
Eran Liberty9095d4a2005-07-28 10:08:46 -0500243 */
Dave Liuf5035922006-10-25 14:41:21 -0500244 caslat = __ilog2(spd.cas_lat);
245
246 if (caslat > 4 ) {
247 printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n", caslat);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500248 return 0;
249 }
Dave Liuf5035922006-10-25 14:41:21 -0500250 max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
251 + (spd.clk_cycle & 0x0f));
252 max_data_rate = max_bus_clk * 2;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500253
Dave Liuf5035922006-10-25 14:41:21 -0500254 debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500255
Dave Liuf5035922006-10-25 14:41:21 -0500256 ddrc_clk = get_ddr_clk(0) / 1000000;
257
258 if (max_data_rate >= 390) { /* it is DDR 400 */
259 printf("DDR: platform not support DDR 400\n");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500260 return 0;
Dave Liuf5035922006-10-25 14:41:21 -0500261 } else if (max_data_rate >= 323) { /* it is DDR 333 */
262 if (ddrc_clk <= 350 && ddrc_clk > 280) {
263 /* DDRC clk at 280~350 */
264 effective_data_rate = 333; /* 6ns */
265 caslat = caslat;
266 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
267 /* DDRC clk at 230~280 */
268 if (spd.clk_cycle2 == 0x75) {
269 effective_data_rate = 266; /* 7.5ns */
270 caslat = caslat - 1;
271 }
272 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
273 /* DDRC clk at 90~230 */
274 if (spd.clk_cycle3 == 0xa0) {
275 effective_data_rate = 200; /* 10ns */
276 caslat = caslat - 2;
277 }
278 }
279 } else if (max_data_rate >= 256) { /* it is DDR 266 */
280 if (ddrc_clk <= 350 && ddrc_clk > 280) {
281 /* DDRC clk at 280~350 */
282 printf("DDR: DDR controller freq is more than "
283 "max data rate of the module\n");
284 return 0;
285 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
286 /* DDRC clk at 230~280 */
287 effective_data_rate = 266; /* 7.5ns */
288 caslat = caslat;
289 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
290 /* DDRC clk at 90~230 */
291 if (spd.clk_cycle2 == 0xa0) {
292 effective_data_rate = 200; /* 10ns */
293 caslat = caslat - 1;
294 }
295 }
296 } else if (max_data_rate >= 190) { /* it is DDR 200 */
297 if (ddrc_clk <= 350 && ddrc_clk > 230) {
298 /* DDRC clk at 230~350 */
299 printf("DDR: DDR controller freq is more than "
300 "max data rate of the module\n");
301 return 0;
302 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
303 /* DDRC clk at 90~230 */
304 effective_data_rate = 200; /* 10ns */
305 caslat = caslat;
306 }
Timur Tabiefec6302006-10-31 18:13:36 -0600307 }
308
309#ifdef CONFIG_MPC834X
310/* Errata DDR6
311 This errata affects all MPC8349E, MPC8343E and MPC8347E processors.
312*/
313 if ((tmp1 >= 280) && (tmp1 < 350)) /* CSB=333 */
314 {
315 if (spd.mid[0] == 0x2c) {
316 /* Micron memory running at 333 MHz */
317 /* Chances are, U-Boot will crash before we get here,
318 but just in case, display a message and return error. */
319 printf("Micron DDR not supported at 333MHz CSB\n");
320 return 0;
321 } else if (spd.mid[0] == 0xad) {
322 printf("Hynix DDR does not require Errata DDR6\n");
323 } else {
324 /* enable 2 cycle Earlier for CL=2.5 or 3 */
325 ddr->debug_reg = 0x202c0000;
326 printf("Errata DDR6 (debug_reg=0x%x)\n", ddr->debug_reg);
327 }
328 caslat_83xx = caslat;
329 }
330
331 if ((tmp1 >= 230) && (tmp1 < 280)) { /* CSB=266 */
332 if (spd.mid[0] != 0x2c) /* non-Micron */
333 caslat_83xx = caslat - 1;
334
335 }
336
337 if ((tmp1 >= 90) && (tmp1 < 230)) { /* CSB=200 */
338 caslat = 3;
339 caslat_83xx = 2;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500340 }
Timur Tabiefec6302006-10-31 18:13:36 -0600341#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500342
343 /*
344 * note: caslat must also be programmed into ddr->sdram_mode
345 * register.
346 *
347 * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
348 * use conservative value here.
349 */
Dave Liuf5035922006-10-25 14:41:21 -0500350 caslat_ctrl = (caslat + 1) & 0x07; /* see as above */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500351
352 ddr->timing_cfg_1 =
353 (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
354 ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
355 ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
Timur Tabiefec6302006-10-31 18:13:36 -0600356#ifdef CONFIG_MPC834x
357 ((caslat_83xx & 0x07) << 16 ) |
358#else
Dave Liuf5035922006-10-25 14:41:21 -0500359 ((caslat_ctrl & 0x07) << 16 ) |
Timur Tabiefec6302006-10-31 18:13:36 -0600360#endif
Dave Liuf5035922006-10-25 14:41:21 -0500361 (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) |
Eran Liberty9095d4a2005-07-28 10:08:46 -0500362 ( 0x300 ) |
363 ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
364
365 ddr->timing_cfg_2 = 0x00000800;
366
367 debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
368 debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
Dave Liuf5035922006-10-25 14:41:21 -0500369 /* Setup init value, but not enable */
370 ddr->sdram_cfg = 0x42000000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500371
Dave Liuf5035922006-10-25 14:41:21 -0500372 /* Check DIMM data bus width */
373 if (spd.dataw_lsb == 0x20)
374 {
375 burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
376 printf("\n DDR DIMM: data bus width is 32 bit");
377 }
378 else
379 {
380 burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
381 printf("\n DDR DIMM: data bus width is 64 bit");
382 }
383
384 /* Is this an ECC DDR chip? */
385 if (spd.config == 0x02) {
386 printf(" with ECC\n");
387 }
388 else
389 printf(" without ECC\n");
390
391 /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
392 Burst type is sequential
Eran Liberty9095d4a2005-07-28 10:08:46 -0500393 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500394 switch(caslat) {
Dave Liuf5035922006-10-25 14:41:21 -0500395 case 1:
396 ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
397 break;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500398 case 2:
Dave Liuf5035922006-10-25 14:41:21 -0500399 ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500400 break;
401 case 3:
Dave Liuf5035922006-10-25 14:41:21 -0500402 ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500403 break;
404 case 4:
Dave Liuf5035922006-10-25 14:41:21 -0500405 ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500406 break;
407 default:
Dave Liuf5035922006-10-25 14:41:21 -0500408 printf("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 "
409 "is supported.\n");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500410 return 0;
411 }
412 debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
413
414 switch(spd.refresh) {
415 case 0x00:
416 case 0x80:
417 tmp = picos_to_clk(15625000);
418 break;
419 case 0x01:
420 case 0x81:
421 tmp = picos_to_clk(3900000);
422 break;
423 case 0x02:
424 case 0x82:
425 tmp = picos_to_clk(7800000);
426 break;
427 case 0x03:
428 case 0x83:
429 tmp = picos_to_clk(31300000);
430 break;
431 case 0x04:
432 case 0x84:
433 tmp = picos_to_clk(62500000);
434 break;
435 case 0x05:
436 case 0x85:
437 tmp = picos_to_clk(125000000);
438 break;
439 default:
440 tmp = 0x512;
441 break;
442 }
443
444 /*
445 * Set BSTOPRE to 0x100 for page mode
446 * If auto-charge is used, set BSTOPRE = 0
447 */
448 ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
449 debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
450
Dave Liuf5035922006-10-25 14:41:21 -0500451 /* SS_EN = 0, source synchronous disable
452 * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd
453 */
454 ddr->sdram_clk_cntl = 0x00000000;
455 debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100456
Eran Liberty9095d4a2005-07-28 10:08:46 -0500457 asm("sync;isync");
458
Dave Liuf5035922006-10-25 14:41:21 -0500459 udelay(600);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500460
461 /*
462 * Figure out the settings for the sdram_cfg register. Build up
463 * the entire register in 'tmp' before writing since the write into
464 * the register will actually enable the memory controller, and all
465 * settings must be done before enabling.
466 *
467 * sdram_cfg[0] = 1 (ddr sdram logic enable)
468 * sdram_cfg[1] = 1 (self-refresh-enable)
469 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
Dave Liuf5035922006-10-25 14:41:21 -0500470 * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
471 * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500472 */
Dave Liuf5035922006-10-25 14:41:21 -0500473 sdram_cfg = 0xC2000000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500474
Dave Liuf5035922006-10-25 14:41:21 -0500475 /* sdram_cfg[3] = RD_EN - registered DIMM enable */
476 if (spd.mod_attr & 0x02) {
477 sdram_cfg |= 0x10000000;
478 }
479
480 /* The DIMM is 32bit width */
481 if (spd.dataw_lsb == 0x20) {
482 sdram_cfg |= 0x000C0000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500483 }
Dave Liuf5035922006-10-25 14:41:21 -0500484 ddrc_ecc_enable = 0;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500485
486#if defined(CONFIG_DDR_ECC)
Dave Liuf5035922006-10-25 14:41:21 -0500487 /* Enable ECC with sdram_cfg[2] */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500488 if (spd.config == 0x02) {
Dave Liuf5035922006-10-25 14:41:21 -0500489 sdram_cfg |= 0x20000000;
490 ddrc_ecc_enable = 1;
491 /* disable error detection */
492 ddr->err_disable = ~ECC_ERROR_ENABLE;
493 /* set single bit error threshold to maximum value,
494 * reset counter to zero */
495 ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
496 (0 << ECC_ERROR_MAN_SBEC_SHIFT);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500497 }
Dave Liuf5035922006-10-25 14:41:21 -0500498
499 debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
500 debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500501#endif
Dave Liuf5035922006-10-25 14:41:21 -0500502 printf(" DDRC ECC mode: %s", ddrc_ecc_enable ? "ON":"OFF");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500503
504#if defined(CONFIG_DDR_2T_TIMING)
505 /*
506 * Enable 2T timing by setting sdram_cfg[16].
507 */
Dave Liuf5035922006-10-25 14:41:21 -0500508 sdram_cfg |= SDRAM_CFG_2T_EN;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500509#endif
Dave Liuf5035922006-10-25 14:41:21 -0500510 /* Enable controller, and GO! */
511 ddr->sdram_cfg = sdram_cfg;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500512 asm("sync;isync");
513 udelay(500);
514
515 debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100516 return memsize; /*in MBytes*/
Eran Liberty9095d4a2005-07-28 10:08:46 -0500517}
Eran Liberty9095d4a2005-07-28 10:08:46 -0500518#endif /* CONFIG_SPD_EEPROM */
519
520
Dave Liuf5035922006-10-25 14:41:21 -0500521#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500522/*
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100523 * Use timebase counter, get_timer() is not availabe
524 * at this point of initialization yet.
Eran Liberty9095d4a2005-07-28 10:08:46 -0500525 */
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100526static __inline__ unsigned long get_tbms (void)
527{
528 unsigned long tbl;
529 unsigned long tbu1, tbu2;
530 unsigned long ms;
531 unsigned long long tmp;
532
533 ulong tbclk = get_tbclk();
534
535 /* get the timebase ticks */
536 do {
537 asm volatile ("mftbu %0":"=r" (tbu1):);
538 asm volatile ("mftb %0":"=r" (tbl):);
539 asm volatile ("mftbu %0":"=r" (tbu2):);
540 } while (tbu1 != tbu2);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500541
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100542 /* convert ticks to ms */
543 tmp = (unsigned long long)(tbu1);
544 tmp = (tmp << 32);
545 tmp += (unsigned long long)(tbl);
546 ms = tmp/(tbclk/1000);
547
548 return ms;
549}
550
551/*
552 * Initialize all of memory for ECC, then enable errors.
553 */
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200554/* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100555void ddr_enable_ecc(unsigned int dram_size)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500556{
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100557 uint *p;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500558 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf5035922006-10-25 14:41:21 -0500559 volatile ddr83xx_t *ddr= &immap->ddr;
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100560 unsigned long t_start, t_end;
561#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
562 uint i;
563#endif
564
565 debug("Initialize a Cachline in DRAM\n");
566 icache_enable();
Eran Liberty9095d4a2005-07-28 10:08:46 -0500567
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100568#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
569 /* Initialise DMA for direct Transfers */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500570 dma_init();
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100571#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500572
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100573 t_start = get_tbms();
574
575#if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
576 debug("DDR init: Cache flush method\n");
577 for (p = 0; p < (uint *)(dram_size); p++) {
Eran Liberty9095d4a2005-07-28 10:08:46 -0500578 if (((unsigned int)p & 0x1f) == 0) {
579 ppcDcbz((unsigned long) p);
580 }
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100581
582 /* write pattern to cache and flush */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500583 *p = (unsigned int)0xdeadbeef;
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100584
Eran Liberty9095d4a2005-07-28 10:08:46 -0500585 if (((unsigned int)p & 0x1c) == 0x1c) {
586 ppcDcbf((unsigned long) p);
587 }
588 }
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100589#else
590 printf("DDR init: DMA method\n");
591 for (p = 0; p < (uint *)(8 * 1024); p++) {
592 /* zero one data cache line */
593 if (((unsigned int)p & 0x1f) == 0) {
594 ppcDcbz((unsigned long)p);
595 }
596
597 /* write pattern to it and flush */
598 *p = (unsigned int)0xdeadbeef;
599
600 if (((unsigned int)p & 0x1c) == 0x1c) {
601 ppcDcbf((unsigned long)p);
602 }
603 }
Eran Liberty9095d4a2005-07-28 10:08:46 -0500604
605 /* 8K */
606 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
607 /* 16K */
608 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
609 /* 32K */
610 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
611 /* 64K */
612 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
613 /* 128k */
614 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
615 /* 256k */
616 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
617 /* 512k */
618 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
619 /* 1M */
620 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
621 /* 2M */
622 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
623 /* 4M */
624 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
625
626 for (i = 1; i < dram_size / 0x800000; i++) {
627 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
628 }
Jon Loeligerebc72242005-08-01 13:20:47 -0500629#endif
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100630
631 t_end = get_tbms();
632 icache_disable();
Eran Liberty9095d4a2005-07-28 10:08:46 -0500633
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100634 debug("\nREADY!!\n");
635 debug("ddr init duration: %ld ms\n", t_end - t_start);
636
637 /* Clear All ECC Errors */
638 if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
639 ddr->err_detect |= ECC_ERROR_DETECT_MME;
640 if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
641 ddr->err_detect |= ECC_ERROR_DETECT_MBE;
642 if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
643 ddr->err_detect |= ECC_ERROR_DETECT_SBE;
644 if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
645 ddr->err_detect |= ECC_ERROR_DETECT_MSE;
646
647 /* Disable ECC-Interrupts */
648 ddr->err_int_en &= ECC_ERR_INT_DISABLE;
649
650 /* Enable errors for ECC */
651 ddr->err_disable &= ECC_ERROR_ENABLE;
652
653 __asm__ __volatile__ ("sync");
654 __asm__ __volatile__ ("isync");
655}
Eran Liberty9095d4a2005-07-28 10:08:46 -0500656#endif /* CONFIG_DDR_ECC */