blob: 153848d343db088f42b255a636d8a29d4d412494 [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{
115 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf5035922006-10-25 14:41:21 -0500116 volatile ddr83xx_t *ddr = &immap->ddr;
117 volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
Eran Liberty9095d4a2005-07-28 10:08:46 -0500118 spd_eeprom_t spd;
Dave Liuf5035922006-10-25 14:41:21 -0500119 unsigned tmp;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500120 unsigned int memsize;
121 unsigned int law_size;
Dave Liuf5035922006-10-25 14:41:21 -0500122 unsigned char caslat, caslat_ctrl;
123 unsigned char burstlen;
124 unsigned int max_bus_clk;
125 unsigned int max_data_rate, effective_data_rate;
126 unsigned int ddrc_clk;
127 unsigned int refresh_clk;
128 unsigned sdram_cfg;
129 unsigned int ddrc_ecc_enable;
Jon Loeligerebc72242005-08-01 13:20:47 -0500130
Dave Liuf5035922006-10-25 14:41:21 -0500131
132 /* Read SPD parameters with I2C */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500133 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100134#ifdef SPD_DEBUG
135 spd_debug(&spd);
136#endif
Dave Liuf5035922006-10-25 14:41:21 -0500137 /* Check the memory type */
138 if (spd.mem_type != SPD_MEMTYPE_DDR) {
139 printf("DDR: Module mem type is %02X\n", spd.mem_type);
140 return 0;
141 }
142
143 /* Check the number of physical bank */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500144 if (spd.nrows > 2) {
Dave Liuf5035922006-10-25 14:41:21 -0500145 printf("DDR: The number of physical bank is %02X\n", spd.nrows);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500146 return 0;
147 }
148
Dave Liuf5035922006-10-25 14:41:21 -0500149 /* Check if the number of row of the module is in the range of DDRC */
150 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
151 printf("DDR: Row number is out of range of DDRC, row=%02X\n",
152 spd.nrow_addr);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500153 return 0;
154 }
155
Dave Liuf5035922006-10-25 14:41:21 -0500156 /* Check if the number of col of the module is in the range of DDRC */
157 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
158 printf("DDR: Col number is out of range of DDRC, col=%02X\n",
159 spd.ncol_addr);
160 return 0;
161 }
162 /* Setup DDR chip select register */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500163 ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
164 ddr->cs_config[2] = ( 1 << 31
165 | (spd.nrow_addr - 12) << 8
166 | (spd.ncol_addr - 8) );
167 debug("\n");
168 debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
169 debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
Jon Loeligerebc72242005-08-01 13:20:47 -0500170
Eran Liberty9095d4a2005-07-28 10:08:46 -0500171 if (spd.nrows == 2) {
172 ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
173 | ((banksize(spd.row_dens) >> 23) - 1) );
174 ddr->cs_config[3] = ( 1<<31
175 | (spd.nrow_addr-12) << 8
176 | (spd.ncol_addr-8) );
177 debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
178 debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
179 }
180
181 if (spd.mem_type != 0x07) {
182 puts("No DDR module found!\n");
183 return 0;
184 }
185
186 /*
187 * Figure out memory size in Megabytes.
188 */
189 memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
190
191 /*
192 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
193 */
194 law_size = 19 + __ilog2(memsize);
195
196 /*
197 * Set up LAWBAR for all of DDR.
198 */
199 ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
200 ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
201 debug("DDR:bar=0x%08x\n", ecm->bar);
202 debug("DDR:ar=0x%08x\n", ecm->ar);
203
204 /*
Dave Liuf5035922006-10-25 14:41:21 -0500205 * Find the largest CAS by locating the highest 1 bit
206 * in the spd.cas_lat field. Translate it to a DDR
207 * controller field value:
208 *
209 * CAS Lat DDR I Ctrl
210 * Clocks SPD Bit Value
211 * -------+--------+---------
212 * 1.0 0 001
213 * 1.5 1 010
214 * 2.0 2 011
215 * 2.5 3 100
216 * 3.0 4 101
217 * 3.5 5 110
218 * 4.0 6 111
Eran Liberty9095d4a2005-07-28 10:08:46 -0500219 */
Dave Liuf5035922006-10-25 14:41:21 -0500220 caslat = __ilog2(spd.cas_lat);
221
222 if (caslat > 4 ) {
223 printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n", caslat);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500224 return 0;
225 }
Dave Liuf5035922006-10-25 14:41:21 -0500226 max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
227 + (spd.clk_cycle & 0x0f));
228 max_data_rate = max_bus_clk * 2;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500229
Dave Liuf5035922006-10-25 14:41:21 -0500230 debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500231
Dave Liuf5035922006-10-25 14:41:21 -0500232 ddrc_clk = get_ddr_clk(0) / 1000000;
233
234 if (max_data_rate >= 390) { /* it is DDR 400 */
235 printf("DDR: platform not support DDR 400\n");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500236 return 0;
Dave Liuf5035922006-10-25 14:41:21 -0500237 } else if (max_data_rate >= 323) { /* it is DDR 333 */
238 if (ddrc_clk <= 350 && ddrc_clk > 280) {
239 /* DDRC clk at 280~350 */
240 effective_data_rate = 333; /* 6ns */
241 caslat = caslat;
242 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
243 /* DDRC clk at 230~280 */
244 if (spd.clk_cycle2 == 0x75) {
245 effective_data_rate = 266; /* 7.5ns */
246 caslat = caslat - 1;
247 }
248 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
249 /* DDRC clk at 90~230 */
250 if (spd.clk_cycle3 == 0xa0) {
251 effective_data_rate = 200; /* 10ns */
252 caslat = caslat - 2;
253 }
254 }
255 } else if (max_data_rate >= 256) { /* it is DDR 266 */
256 if (ddrc_clk <= 350 && ddrc_clk > 280) {
257 /* DDRC clk at 280~350 */
258 printf("DDR: DDR controller freq is more than "
259 "max data rate of the module\n");
260 return 0;
261 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
262 /* DDRC clk at 230~280 */
263 effective_data_rate = 266; /* 7.5ns */
264 caslat = caslat;
265 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
266 /* DDRC clk at 90~230 */
267 if (spd.clk_cycle2 == 0xa0) {
268 effective_data_rate = 200; /* 10ns */
269 caslat = caslat - 1;
270 }
271 }
272 } else if (max_data_rate >= 190) { /* it is DDR 200 */
273 if (ddrc_clk <= 350 && ddrc_clk > 230) {
274 /* DDRC clk at 230~350 */
275 printf("DDR: DDR controller freq is more than "
276 "max data rate of the module\n");
277 return 0;
278 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
279 /* DDRC clk at 90~230 */
280 effective_data_rate = 200; /* 10ns */
281 caslat = caslat;
282 }
Eran Liberty9095d4a2005-07-28 10:08:46 -0500283 }
284
285 /*
286 * note: caslat must also be programmed into ddr->sdram_mode
287 * register.
288 *
289 * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
290 * use conservative value here.
291 */
Dave Liuf5035922006-10-25 14:41:21 -0500292 caslat_ctrl = (caslat + 1) & 0x07; /* see as above */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500293
294 ddr->timing_cfg_1 =
295 (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
296 ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
297 ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
Dave Liuf5035922006-10-25 14:41:21 -0500298 ((caslat_ctrl & 0x07) << 16 ) |
299 (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) |
Eran Liberty9095d4a2005-07-28 10:08:46 -0500300 ( 0x300 ) |
301 ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
302
303 ddr->timing_cfg_2 = 0x00000800;
304
305 debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
306 debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
Dave Liuf5035922006-10-25 14:41:21 -0500307 /* Setup init value, but not enable */
308 ddr->sdram_cfg = 0x42000000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500309
Dave Liuf5035922006-10-25 14:41:21 -0500310 /* Check DIMM data bus width */
311 if (spd.dataw_lsb == 0x20)
312 {
313 burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
314 printf("\n DDR DIMM: data bus width is 32 bit");
315 }
316 else
317 {
318 burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
319 printf("\n DDR DIMM: data bus width is 64 bit");
320 }
321
322 /* Is this an ECC DDR chip? */
323 if (spd.config == 0x02) {
324 printf(" with ECC\n");
325 }
326 else
327 printf(" without ECC\n");
328
329 /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
330 Burst type is sequential
Eran Liberty9095d4a2005-07-28 10:08:46 -0500331 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500332 switch(caslat) {
Dave Liuf5035922006-10-25 14:41:21 -0500333 case 1:
334 ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
335 break;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500336 case 2:
Dave Liuf5035922006-10-25 14:41:21 -0500337 ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500338 break;
339 case 3:
Dave Liuf5035922006-10-25 14:41:21 -0500340 ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500341 break;
342 case 4:
Dave Liuf5035922006-10-25 14:41:21 -0500343 ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500344 break;
345 default:
Dave Liuf5035922006-10-25 14:41:21 -0500346 printf("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 "
347 "is supported.\n");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500348 return 0;
349 }
350 debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
351
352 switch(spd.refresh) {
353 case 0x00:
354 case 0x80:
355 tmp = picos_to_clk(15625000);
356 break;
357 case 0x01:
358 case 0x81:
359 tmp = picos_to_clk(3900000);
360 break;
361 case 0x02:
362 case 0x82:
363 tmp = picos_to_clk(7800000);
364 break;
365 case 0x03:
366 case 0x83:
367 tmp = picos_to_clk(31300000);
368 break;
369 case 0x04:
370 case 0x84:
371 tmp = picos_to_clk(62500000);
372 break;
373 case 0x05:
374 case 0x85:
375 tmp = picos_to_clk(125000000);
376 break;
377 default:
378 tmp = 0x512;
379 break;
380 }
381
382 /*
383 * Set BSTOPRE to 0x100 for page mode
384 * If auto-charge is used, set BSTOPRE = 0
385 */
386 ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
387 debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
388
Dave Liuf5035922006-10-25 14:41:21 -0500389 /* SS_EN = 0, source synchronous disable
390 * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd
391 */
392 ddr->sdram_clk_cntl = 0x00000000;
393 debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100394
Eran Liberty9095d4a2005-07-28 10:08:46 -0500395 asm("sync;isync");
396
Dave Liuf5035922006-10-25 14:41:21 -0500397 udelay(600);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500398
399 /*
400 * Figure out the settings for the sdram_cfg register. Build up
401 * the entire register in 'tmp' before writing since the write into
402 * the register will actually enable the memory controller, and all
403 * settings must be done before enabling.
404 *
405 * sdram_cfg[0] = 1 (ddr sdram logic enable)
406 * sdram_cfg[1] = 1 (self-refresh-enable)
407 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
Dave Liuf5035922006-10-25 14:41:21 -0500408 * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
409 * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500410 */
Dave Liuf5035922006-10-25 14:41:21 -0500411 sdram_cfg = 0xC2000000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500412
Dave Liuf5035922006-10-25 14:41:21 -0500413 /* sdram_cfg[3] = RD_EN - registered DIMM enable */
414 if (spd.mod_attr & 0x02) {
415 sdram_cfg |= 0x10000000;
416 }
417
418 /* The DIMM is 32bit width */
419 if (spd.dataw_lsb == 0x20) {
420 sdram_cfg |= 0x000C0000;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500421 }
Dave Liuf5035922006-10-25 14:41:21 -0500422 ddrc_ecc_enable = 0;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500423
424#if defined(CONFIG_DDR_ECC)
Dave Liuf5035922006-10-25 14:41:21 -0500425 /* Enable ECC with sdram_cfg[2] */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500426 if (spd.config == 0x02) {
Dave Liuf5035922006-10-25 14:41:21 -0500427 sdram_cfg |= 0x20000000;
428 ddrc_ecc_enable = 1;
429 /* disable error detection */
430 ddr->err_disable = ~ECC_ERROR_ENABLE;
431 /* set single bit error threshold to maximum value,
432 * reset counter to zero */
433 ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
434 (0 << ECC_ERROR_MAN_SBEC_SHIFT);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500435 }
Dave Liuf5035922006-10-25 14:41:21 -0500436
437 debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
438 debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500439#endif
Dave Liuf5035922006-10-25 14:41:21 -0500440 printf(" DDRC ECC mode: %s", ddrc_ecc_enable ? "ON":"OFF");
Eran Liberty9095d4a2005-07-28 10:08:46 -0500441
442#if defined(CONFIG_DDR_2T_TIMING)
443 /*
444 * Enable 2T timing by setting sdram_cfg[16].
445 */
Dave Liuf5035922006-10-25 14:41:21 -0500446 sdram_cfg |= SDRAM_CFG_2T_EN;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500447#endif
Dave Liuf5035922006-10-25 14:41:21 -0500448 /* Enable controller, and GO! */
449 ddr->sdram_cfg = sdram_cfg;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500450 asm("sync;isync");
451 udelay(500);
452
453 debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100454 return memsize; /*in MBytes*/
Eran Liberty9095d4a2005-07-28 10:08:46 -0500455}
Eran Liberty9095d4a2005-07-28 10:08:46 -0500456#endif /* CONFIG_SPD_EEPROM */
457
458
Dave Liuf5035922006-10-25 14:41:21 -0500459#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500460/*
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100461 * Use timebase counter, get_timer() is not availabe
462 * at this point of initialization yet.
Eran Liberty9095d4a2005-07-28 10:08:46 -0500463 */
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100464static __inline__ unsigned long get_tbms (void)
465{
466 unsigned long tbl;
467 unsigned long tbu1, tbu2;
468 unsigned long ms;
469 unsigned long long tmp;
470
471 ulong tbclk = get_tbclk();
472
473 /* get the timebase ticks */
474 do {
475 asm volatile ("mftbu %0":"=r" (tbu1):);
476 asm volatile ("mftb %0":"=r" (tbl):);
477 asm volatile ("mftbu %0":"=r" (tbu2):);
478 } while (tbu1 != tbu2);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500479
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100480 /* convert ticks to ms */
481 tmp = (unsigned long long)(tbu1);
482 tmp = (tmp << 32);
483 tmp += (unsigned long long)(tbl);
484 ms = tmp/(tbclk/1000);
485
486 return ms;
487}
488
489/*
490 * Initialize all of memory for ECC, then enable errors.
491 */
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200492/* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100493void ddr_enable_ecc(unsigned int dram_size)
Eran Liberty9095d4a2005-07-28 10:08:46 -0500494{
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100495 uint *p;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500496 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
Dave Liuf5035922006-10-25 14:41:21 -0500497 volatile ddr83xx_t *ddr= &immap->ddr;
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100498 unsigned long t_start, t_end;
499#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
500 uint i;
501#endif
502
503 debug("Initialize a Cachline in DRAM\n");
504 icache_enable();
Eran Liberty9095d4a2005-07-28 10:08:46 -0500505
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100506#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
507 /* Initialise DMA for direct Transfers */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500508 dma_init();
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100509#endif
Eran Liberty9095d4a2005-07-28 10:08:46 -0500510
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100511 t_start = get_tbms();
512
513#if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
514 debug("DDR init: Cache flush method\n");
515 for (p = 0; p < (uint *)(dram_size); p++) {
Eran Liberty9095d4a2005-07-28 10:08:46 -0500516 if (((unsigned int)p & 0x1f) == 0) {
517 ppcDcbz((unsigned long) p);
518 }
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100519
520 /* write pattern to cache and flush */
Eran Liberty9095d4a2005-07-28 10:08:46 -0500521 *p = (unsigned int)0xdeadbeef;
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100522
Eran Liberty9095d4a2005-07-28 10:08:46 -0500523 if (((unsigned int)p & 0x1c) == 0x1c) {
524 ppcDcbf((unsigned long) p);
525 }
526 }
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100527#else
528 printf("DDR init: DMA method\n");
529 for (p = 0; p < (uint *)(8 * 1024); p++) {
530 /* zero one data cache line */
531 if (((unsigned int)p & 0x1f) == 0) {
532 ppcDcbz((unsigned long)p);
533 }
534
535 /* write pattern to it and flush */
536 *p = (unsigned int)0xdeadbeef;
537
538 if (((unsigned int)p & 0x1c) == 0x1c) {
539 ppcDcbf((unsigned long)p);
540 }
541 }
Eran Liberty9095d4a2005-07-28 10:08:46 -0500542
543 /* 8K */
544 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
545 /* 16K */
546 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
547 /* 32K */
548 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
549 /* 64K */
550 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
551 /* 128k */
552 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
553 /* 256k */
554 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
555 /* 512k */
556 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
557 /* 1M */
558 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
559 /* 2M */
560 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
561 /* 4M */
562 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
563
564 for (i = 1; i < dram_size / 0x800000; i++) {
565 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
566 }
Jon Loeligerebc72242005-08-01 13:20:47 -0500567#endif
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100568
569 t_end = get_tbms();
570 icache_disable();
Eran Liberty9095d4a2005-07-28 10:08:46 -0500571
Marian Balakowicz6f6104d2006-03-14 16:23:35 +0100572 debug("\nREADY!!\n");
573 debug("ddr init duration: %ld ms\n", t_end - t_start);
574
575 /* Clear All ECC Errors */
576 if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
577 ddr->err_detect |= ECC_ERROR_DETECT_MME;
578 if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
579 ddr->err_detect |= ECC_ERROR_DETECT_MBE;
580 if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
581 ddr->err_detect |= ECC_ERROR_DETECT_SBE;
582 if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
583 ddr->err_detect |= ECC_ERROR_DETECT_MSE;
584
585 /* Disable ECC-Interrupts */
586 ddr->err_int_en &= ECC_ERR_INT_DISABLE;
587
588 /* Enable errors for ECC */
589 ddr->err_disable &= ECC_ERROR_ENABLE;
590
591 __asm__ __volatile__ ("sync");
592 __asm__ __volatile__ ("isync");
593}
Eran Liberty9095d4a2005-07-28 10:08:46 -0500594#endif /* CONFIG_DDR_ECC */