blob: 8ee4234631bf901c6676b7c364b780e98c82514a [file] [log] [blame]
Eran Libertyf046ccd2005-07-28 10:08:46 -05001/*
Dave Liuf6eda7f2006-10-25 14:41:21 -05002 * (C) Copyright 2006 Freescale Semiconductor, Inc.
3 *
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +01004 * (C) Copyright 2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denkcf48eb92006-04-16 10:51:58 +02006 *
Eran Libertyf046ccd2005-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 Liuf6eda7f2006-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 Libertyf046ccd2005-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 Liuf6eda7f2006-10-25 14:41:21 -050048#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Libertyf046ccd2005-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 Libertyf046ccd2005-07-28 10:08:46 -050054#ifndef CFG_READ_SPD
55#define CFG_READ_SPD i2c_read
56#endif
57
Eran Libertyf046ccd2005-07-28 10:08:46 -050058/*
59 * Convert picoseconds into clock cycles (rounding up if needed).
60 */
Dave Liuf6eda7f2006-10-25 14:41:21 -050061extern ulong get_ddr_clk(ulong dummy);
Eran Libertyf046ccd2005-07-28 10:08:46 -050062
63int
64picos_to_clk(int picos)
65{
Dave Liuf6eda7f2006-10-25 14:41:21 -050066 unsigned int ddr_bus_clk;
Eran Libertyf046ccd2005-07-28 10:08:46 -050067 int clks;
68
Dave Liuf6eda7f2006-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 Libertyf046ccd2005-07-28 10:08:46 -050073 }
74
75 return clks;
76}
77
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +010078unsigned int banksize(unsigned char row_dens)
Eran Libertyf046ccd2005-07-28 10:08:46 -050079{
80 return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
81}
82
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +010083int read_spd(uint addr)
84{
85 return ((int) addr);
86}
87
Rafal Jaworowskidc9e4992006-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 Libertyf046ccd2005-07-28 10:08:46 -0500114{
Timur Tabibed85ca2006-10-31 18:13:36 -0600115#ifdef CONFIG_MPC834X
116 int caslat_83xx; /* For Errata DDR6 */
117#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500118 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500119 volatile ddr83xx_t *ddr = &immap->ddr;
120 volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
Eran Libertyf046ccd2005-07-28 10:08:46 -0500121 spd_eeprom_t spd;
Timur Tabibed85ca2006-10-31 18:13:36 -0600122 unsigned int tmp, tmp1;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500123 unsigned int memsize;
124 unsigned int law_size;
Dave Liuf6eda7f2006-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 Loeligerde1d0a62005-08-01 13:20:47 -0500133
Dave Liuf6eda7f2006-10-25 14:41:21 -0500134
135 /* Read SPD parameters with I2C */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500136 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +0100137#ifdef SPD_DEBUG
138 spd_debug(&spd);
139#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500140 /* Check the memory type */
141 if (spd.mem_type != SPD_MEMTYPE_DDR) {
142 printf("DDR: Module mem type is %02X\n", spd.mem_type);
143 return 0;
144 }
145
146 /* Check the number of physical bank */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500147 if (spd.nrows > 2) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500148 printf("DDR: The number of physical bank is %02X\n", spd.nrows);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500149 return 0;
150 }
151
Dave Liuf6eda7f2006-10-25 14:41:21 -0500152 /* Check if the number of row of the module is in the range of DDRC */
153 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
154 printf("DDR: Row number is out of range of DDRC, row=%02X\n",
155 spd.nrow_addr);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500156 return 0;
157 }
158
Dave Liuf6eda7f2006-10-25 14:41:21 -0500159 /* Check if the number of col of the module is in the range of DDRC */
160 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
161 printf("DDR: Col number is out of range of DDRC, col=%02X\n",
162 spd.ncol_addr);
163 return 0;
164 }
165 /* Setup DDR chip select register */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500166 ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
167 ddr->cs_config[2] = ( 1 << 31
168 | (spd.nrow_addr - 12) << 8
169 | (spd.ncol_addr - 8) );
170 debug("\n");
171 debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
172 debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
Jon Loeligerde1d0a62005-08-01 13:20:47 -0500173
Eran Libertyf046ccd2005-07-28 10:08:46 -0500174 if (spd.nrows == 2) {
175 ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
176 | ((banksize(spd.row_dens) >> 23) - 1) );
177 ddr->cs_config[3] = ( 1<<31
178 | (spd.nrow_addr-12) << 8
179 | (spd.ncol_addr-8) );
180 debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
181 debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
182 }
183
184 if (spd.mem_type != 0x07) {
185 puts("No DDR module found!\n");
186 return 0;
187 }
188
189 /*
190 * Figure out memory size in Megabytes.
191 */
192 memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
193
194 /*
195 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
196 */
197 law_size = 19 + __ilog2(memsize);
198
199 /*
200 * Set up LAWBAR for all of DDR.
201 */
202 ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
203 ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
204 debug("DDR:bar=0x%08x\n", ecm->bar);
205 debug("DDR:ar=0x%08x\n", ecm->ar);
206
207 /*
Dave Liuf6eda7f2006-10-25 14:41:21 -0500208 * Find the largest CAS by locating the highest 1 bit
209 * in the spd.cas_lat field. Translate it to a DDR
210 * controller field value:
211 *
212 * CAS Lat DDR I Ctrl
213 * Clocks SPD Bit Value
214 * -------+--------+---------
215 * 1.0 0 001
216 * 1.5 1 010
217 * 2.0 2 011
218 * 2.5 3 100
219 * 3.0 4 101
220 * 3.5 5 110
221 * 4.0 6 111
Eran Libertyf046ccd2005-07-28 10:08:46 -0500222 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500223 caslat = __ilog2(spd.cas_lat);
224
225 if (caslat > 4 ) {
226 printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n", caslat);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500227 return 0;
228 }
Dave Liuf6eda7f2006-10-25 14:41:21 -0500229 max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
230 + (spd.clk_cycle & 0x0f));
231 max_data_rate = max_bus_clk * 2;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500232
Dave Liuf6eda7f2006-10-25 14:41:21 -0500233 debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500234
Dave Liuf6eda7f2006-10-25 14:41:21 -0500235 ddrc_clk = get_ddr_clk(0) / 1000000;
236
237 if (max_data_rate >= 390) { /* it is DDR 400 */
238 printf("DDR: platform not support DDR 400\n");
Eran Libertyf046ccd2005-07-28 10:08:46 -0500239 return 0;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500240 } else if (max_data_rate >= 323) { /* it is DDR 333 */
241 if (ddrc_clk <= 350 && ddrc_clk > 280) {
242 /* DDRC clk at 280~350 */
243 effective_data_rate = 333; /* 6ns */
244 caslat = caslat;
245 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
246 /* DDRC clk at 230~280 */
247 if (spd.clk_cycle2 == 0x75) {
248 effective_data_rate = 266; /* 7.5ns */
249 caslat = caslat - 1;
250 }
251 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
252 /* DDRC clk at 90~230 */
253 if (spd.clk_cycle3 == 0xa0) {
254 effective_data_rate = 200; /* 10ns */
255 caslat = caslat - 2;
256 }
257 }
258 } else if (max_data_rate >= 256) { /* it is DDR 266 */
259 if (ddrc_clk <= 350 && ddrc_clk > 280) {
260 /* DDRC clk at 280~350 */
261 printf("DDR: DDR controller freq is more than "
262 "max data rate of the module\n");
263 return 0;
264 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
265 /* DDRC clk at 230~280 */
266 effective_data_rate = 266; /* 7.5ns */
267 caslat = caslat;
268 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
269 /* DDRC clk at 90~230 */
270 if (spd.clk_cycle2 == 0xa0) {
271 effective_data_rate = 200; /* 10ns */
272 caslat = caslat - 1;
273 }
274 }
275 } else if (max_data_rate >= 190) { /* it is DDR 200 */
276 if (ddrc_clk <= 350 && ddrc_clk > 230) {
277 /* DDRC clk at 230~350 */
278 printf("DDR: DDR controller freq is more than "
279 "max data rate of the module\n");
280 return 0;
281 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
282 /* DDRC clk at 90~230 */
283 effective_data_rate = 200; /* 10ns */
284 caslat = caslat;
285 }
Eran Libertyf046ccd2005-07-28 10:08:46 -0500286 }
287
Timur Tabibed85ca2006-10-31 18:13:36 -0600288#ifdef CONFIG_MPC834X
289/* Errata DDR6
290 This errata affects all MPC8349E, MPC8343E and MPC8347E processors.
291*/
292 if ((tmp1 >= 280) && (tmp1 < 350)) /* CSB=333 */
293 {
294 if (spd.mid[0] == 0x2c) {
295 /* Micron memory running at 333 MHz */
296 /* Chances are, U-Boot will crash before we get here,
297 but just in case, display a message and return error. */
298 printf("Micron DDR not supported at 333MHz CSB\n");
299 return 0;
300 } else if (spd.mid[0] == 0xad) {
301 printf("Hynix DDR does not require Errata DDR6\n");
302 } else {
303 /* enable 2 cycle Earlier for CL=2.5 or 3 */
304 ddr->debug_reg = 0x202c0000;
305 printf("Errata DDR6 (debug_reg=0x%x)\n", ddr->debug_reg);
306 }
307 caslat_83xx = caslat;
308 }
309
310 if ((tmp1 >= 230) && (tmp1 < 280)) { /* CSB=266 */
311 if (spd.mid[0] != 0x2c) /* non-Micron */
312 caslat_83xx = caslat - 1;
313
314 }
315
316 if ((tmp1 >= 90) && (tmp1 < 230)) { /* CSB=200 */
317 caslat = 3;
318 caslat_83xx = 2;
319 }
320#endif
321
Eran Libertyf046ccd2005-07-28 10:08:46 -0500322 /*
323 * note: caslat must also be programmed into ddr->sdram_mode
324 * register.
325 *
326 * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
327 * use conservative value here.
328 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500329 caslat_ctrl = (caslat + 1) & 0x07; /* see as above */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500330
331 ddr->timing_cfg_1 =
332 (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
333 ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
334 ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
Timur Tabibed85ca2006-10-31 18:13:36 -0600335#ifdef CONFIG_MPC834x
336 ((caslat_83xx & 0x07) << 16 ) |
337#else
Dave Liuf6eda7f2006-10-25 14:41:21 -0500338 ((caslat_ctrl & 0x07) << 16 ) |
Timur Tabibed85ca2006-10-31 18:13:36 -0600339#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500340 (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) |
Eran Libertyf046ccd2005-07-28 10:08:46 -0500341 ( 0x300 ) |
342 ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
343
344 ddr->timing_cfg_2 = 0x00000800;
345
346 debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
347 debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
Dave Liuf6eda7f2006-10-25 14:41:21 -0500348 /* Setup init value, but not enable */
349 ddr->sdram_cfg = 0x42000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500350
Dave Liuf6eda7f2006-10-25 14:41:21 -0500351 /* Check DIMM data bus width */
352 if (spd.dataw_lsb == 0x20)
353 {
354 burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
355 printf("\n DDR DIMM: data bus width is 32 bit");
356 }
357 else
358 {
359 burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
360 printf("\n DDR DIMM: data bus width is 64 bit");
361 }
362
363 /* Is this an ECC DDR chip? */
364 if (spd.config == 0x02) {
365 printf(" with ECC\n");
366 }
367 else
368 printf(" without ECC\n");
369
370 /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
371 Burst type is sequential
Eran Libertyf046ccd2005-07-28 10:08:46 -0500372 */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500373 switch(caslat) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500374 case 1:
375 ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
376 break;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500377 case 2:
Dave Liuf6eda7f2006-10-25 14:41:21 -0500378 ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500379 break;
380 case 3:
Dave Liuf6eda7f2006-10-25 14:41:21 -0500381 ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500382 break;
383 case 4:
Dave Liuf6eda7f2006-10-25 14:41:21 -0500384 ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500385 break;
386 default:
Dave Liuf6eda7f2006-10-25 14:41:21 -0500387 printf("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 "
388 "is supported.\n");
Eran Libertyf046ccd2005-07-28 10:08:46 -0500389 return 0;
390 }
391 debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
392
393 switch(spd.refresh) {
394 case 0x00:
395 case 0x80:
396 tmp = picos_to_clk(15625000);
397 break;
398 case 0x01:
399 case 0x81:
400 tmp = picos_to_clk(3900000);
401 break;
402 case 0x02:
403 case 0x82:
404 tmp = picos_to_clk(7800000);
405 break;
406 case 0x03:
407 case 0x83:
408 tmp = picos_to_clk(31300000);
409 break;
410 case 0x04:
411 case 0x84:
412 tmp = picos_to_clk(62500000);
413 break;
414 case 0x05:
415 case 0x85:
416 tmp = picos_to_clk(125000000);
417 break;
418 default:
419 tmp = 0x512;
420 break;
421 }
422
423 /*
424 * Set BSTOPRE to 0x100 for page mode
425 * If auto-charge is used, set BSTOPRE = 0
426 */
427 ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
428 debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
429
Dave Liuf6eda7f2006-10-25 14:41:21 -0500430 /* SS_EN = 0, source synchronous disable
431 * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd
432 */
433 ddr->sdram_clk_cntl = 0x00000000;
434 debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100435
Eran Libertyf046ccd2005-07-28 10:08:46 -0500436 asm("sync;isync");
437
Dave Liuf6eda7f2006-10-25 14:41:21 -0500438 udelay(600);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500439
440 /*
441 * Figure out the settings for the sdram_cfg register. Build up
442 * the entire register in 'tmp' before writing since the write into
443 * the register will actually enable the memory controller, and all
444 * settings must be done before enabling.
445 *
446 * sdram_cfg[0] = 1 (ddr sdram logic enable)
447 * sdram_cfg[1] = 1 (self-refresh-enable)
448 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500449 * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
450 * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500451 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500452 sdram_cfg = 0xC2000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500453
Dave Liuf6eda7f2006-10-25 14:41:21 -0500454 /* sdram_cfg[3] = RD_EN - registered DIMM enable */
455 if (spd.mod_attr & 0x02) {
456 sdram_cfg |= 0x10000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500457 }
458
Dave Liuf6eda7f2006-10-25 14:41:21 -0500459 /* The DIMM is 32bit width */
460 if (spd.dataw_lsb == 0x20) {
461 sdram_cfg |= 0x000C0000;
462 }
463 ddrc_ecc_enable = 0;
464
Eran Libertyf046ccd2005-07-28 10:08:46 -0500465#if defined(CONFIG_DDR_ECC)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500466 /* Enable ECC with sdram_cfg[2] */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500467 if (spd.config == 0x02) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500468 sdram_cfg |= 0x20000000;
469 ddrc_ecc_enable = 1;
470 /* disable error detection */
471 ddr->err_disable = ~ECC_ERROR_ENABLE;
472 /* set single bit error threshold to maximum value,
473 * reset counter to zero */
474 ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
475 (0 << ECC_ERROR_MAN_SBEC_SHIFT);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500476 }
Dave Liuf6eda7f2006-10-25 14:41:21 -0500477
478 debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
479 debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500480#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500481 printf(" DDRC ECC mode: %s", ddrc_ecc_enable ? "ON":"OFF");
Eran Libertyf046ccd2005-07-28 10:08:46 -0500482
483#if defined(CONFIG_DDR_2T_TIMING)
484 /*
485 * Enable 2T timing by setting sdram_cfg[16].
486 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500487 sdram_cfg |= SDRAM_CFG_2T_EN;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500488#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500489 /* Enable controller, and GO! */
490 ddr->sdram_cfg = sdram_cfg;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500491 asm("sync;isync");
492 udelay(500);
493
494 debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +0100495 return memsize; /*in MBytes*/
Eran Libertyf046ccd2005-07-28 10:08:46 -0500496}
Eran Libertyf046ccd2005-07-28 10:08:46 -0500497#endif /* CONFIG_SPD_EEPROM */
498
499
Dave Liuf6eda7f2006-10-25 14:41:21 -0500500#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500501/*
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100502 * Use timebase counter, get_timer() is not availabe
503 * at this point of initialization yet.
504 */
505static __inline__ unsigned long get_tbms (void)
506{
507 unsigned long tbl;
508 unsigned long tbu1, tbu2;
509 unsigned long ms;
510 unsigned long long tmp;
511
512 ulong tbclk = get_tbclk();
513
514 /* get the timebase ticks */
515 do {
516 asm volatile ("mftbu %0":"=r" (tbu1):);
517 asm volatile ("mftb %0":"=r" (tbl):);
518 asm volatile ("mftbu %0":"=r" (tbu2):);
519 } while (tbu1 != tbu2);
520
521 /* convert ticks to ms */
522 tmp = (unsigned long long)(tbu1);
523 tmp = (tmp << 32);
524 tmp += (unsigned long long)(tbl);
525 ms = tmp/(tbclk/1000);
526
527 return ms;
528}
529
530/*
Eran Libertyf046ccd2005-07-28 10:08:46 -0500531 * Initialize all of memory for ECC, then enable errors.
532 */
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200533/* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100534void ddr_enable_ecc(unsigned int dram_size)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500535{
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100536 uint *p;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500537 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500538 volatile ddr83xx_t *ddr= &immap->ddr;
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100539 unsigned long t_start, t_end;
540#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
541 uint i;
542#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500543
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100544 debug("Initialize a Cachline in DRAM\n");
545 icache_enable();
546
547#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
548 /* Initialise DMA for direct Transfers */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500549 dma_init();
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100550#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500551
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100552 t_start = get_tbms();
553
554#if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
555 debug("DDR init: Cache flush method\n");
556 for (p = 0; p < (uint *)(dram_size); p++) {
Eran Libertyf046ccd2005-07-28 10:08:46 -0500557 if (((unsigned int)p & 0x1f) == 0) {
558 ppcDcbz((unsigned long) p);
559 }
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100560
561 /* write pattern to cache and flush */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500562 *p = (unsigned int)0xdeadbeef;
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100563
Eran Libertyf046ccd2005-07-28 10:08:46 -0500564 if (((unsigned int)p & 0x1c) == 0x1c) {
565 ppcDcbf((unsigned long) p);
566 }
567 }
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100568#else
569 printf("DDR init: DMA method\n");
570 for (p = 0; p < (uint *)(8 * 1024); p++) {
571 /* zero one data cache line */
572 if (((unsigned int)p & 0x1f) == 0) {
573 ppcDcbz((unsigned long)p);
574 }
575
576 /* write pattern to it and flush */
577 *p = (unsigned int)0xdeadbeef;
578
579 if (((unsigned int)p & 0x1c) == 0x1c) {
580 ppcDcbf((unsigned long)p);
581 }
582 }
Eran Libertyf046ccd2005-07-28 10:08:46 -0500583
584 /* 8K */
585 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
586 /* 16K */
587 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
588 /* 32K */
589 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
590 /* 64K */
591 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
592 /* 128k */
593 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
594 /* 256k */
595 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
596 /* 512k */
597 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
598 /* 1M */
599 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
600 /* 2M */
601 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
602 /* 4M */
603 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
604
605 for (i = 1; i < dram_size / 0x800000; i++) {
606 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
607 }
Jon Loeligerde1d0a62005-08-01 13:20:47 -0500608#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500609
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100610 t_end = get_tbms();
611 icache_disable();
612
613 debug("\nREADY!!\n");
614 debug("ddr init duration: %ld ms\n", t_end - t_start);
615
616 /* Clear All ECC Errors */
617 if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
618 ddr->err_detect |= ECC_ERROR_DETECT_MME;
619 if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
620 ddr->err_detect |= ECC_ERROR_DETECT_MBE;
621 if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
622 ddr->err_detect |= ECC_ERROR_DETECT_SBE;
623 if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
624 ddr->err_detect |= ECC_ERROR_DETECT_MSE;
625
626 /* Disable ECC-Interrupts */
627 ddr->err_int_en &= ECC_ERR_INT_DISABLE;
628
629 /* Enable errors for ECC */
630 ddr->err_disable &= ECC_ERROR_ENABLE;
631
632 __asm__ __volatile__ ("sync");
633 __asm__ __volatile__ ("isync");
634}
Eran Libertyf046ccd2005-07-28 10:08:46 -0500635#endif /* CONFIG_DDR_ECC */