blob: cfc42c4a838d71e2b995eb895fe478f99223e60f [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 *
Dave Liu5f820432006-11-03 19:33:44 -06007 * Copyright (C) 2004-2006 Freescale Semiconductor, Inc.
Eran Libertyf046ccd2005-07-28 10:08:46 -05008 * (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
Eran Libertyf046ccd2005-07-28 10:08:46 -050028 */
29
30#include <common.h>
31#include <asm/processor.h>
32#include <i2c.h>
33#include <spd.h>
34#include <asm/mmu.h>
35#include <spd_sdram.h>
36
37#ifdef CONFIG_SPD_EEPROM
38
Timur Tabie857a5b2006-11-28 12:09:35 -060039DECLARE_GLOBAL_DATA_PTR;
40
Dave Liuf6eda7f2006-10-25 14:41:21 -050041#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Libertyf046ccd2005-07-28 10:08:46 -050042extern void dma_init(void);
43extern uint dma_check(void);
44extern int dma_xfer(void *dest, uint count, void *src);
45#endif
46
Eran Libertyf046ccd2005-07-28 10:08:46 -050047#ifndef CFG_READ_SPD
48#define CFG_READ_SPD i2c_read
49#endif
50
Eran Libertyf046ccd2005-07-28 10:08:46 -050051/*
52 * Convert picoseconds into clock cycles (rounding up if needed).
53 */
Eran Libertyf046ccd2005-07-28 10:08:46 -050054int
55picos_to_clk(int picos)
56{
Dave Liuf6eda7f2006-10-25 14:41:21 -050057 unsigned int ddr_bus_clk;
Eran Libertyf046ccd2005-07-28 10:08:46 -050058 int clks;
59
Timur Tabie857a5b2006-11-28 12:09:35 -060060 ddr_bus_clk = gd->ddr_clk >> 1;
Dave Liuf6eda7f2006-10-25 14:41:21 -050061 clks = picos / ((1000000000 / ddr_bus_clk) * 1000);
Timur Tabie857a5b2006-11-28 12:09:35 -060062 if (picos % ((1000000000 / ddr_bus_clk) * 1000) != 0)
Dave Liuf6eda7f2006-10-25 14:41:21 -050063 clks++;
Eran Libertyf046ccd2005-07-28 10:08:46 -050064
65 return clks;
66}
67
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +010068unsigned int banksize(unsigned char row_dens)
Eran Libertyf046ccd2005-07-28 10:08:46 -050069{
70 return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
71}
72
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +010073int read_spd(uint addr)
74{
75 return ((int) addr);
76}
77
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +010078#undef SPD_DEBUG
79#ifdef SPD_DEBUG
80static void spd_debug(spd_eeprom_t *spd)
81{
82 printf ("\nDIMM type: %-18.18s\n", spd->mpart);
83 printf ("SPD size: %d\n", spd->info_size);
84 printf ("EEPROM size: %d\n", 1 << spd->chip_size);
85 printf ("Memory type: %d\n", spd->mem_type);
86 printf ("Row addr: %d\n", spd->nrow_addr);
87 printf ("Column addr: %d\n", spd->ncol_addr);
88 printf ("# of rows: %d\n", spd->nrows);
89 printf ("Row density: %d\n", spd->row_dens);
90 printf ("# of banks: %d\n", spd->nbanks);
91 printf ("Data width: %d\n",
92 256 * spd->dataw_msb + spd->dataw_lsb);
93 printf ("Chip width: %d\n", spd->primw);
94 printf ("Refresh rate: %02X\n", spd->refresh);
95 printf ("CAS latencies: %02X\n", spd->cas_lat);
96 printf ("Write latencies: %02X\n", spd->write_lat);
97 printf ("tRP: %d\n", spd->trp);
98 printf ("tRCD: %d\n", spd->trcd);
99 printf ("\n");
100}
101#endif /* SPD_DEBUG */
102
103long int spd_sdram()
Eran Libertyf046ccd2005-07-28 10:08:46 -0500104{
Timur Tabid239d742006-11-03 12:00:28 -0600105 volatile immap_t *immap = (immap_t *)CFG_IMMR;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500106 volatile ddr83xx_t *ddr = &immap->ddr;
107 volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
Eran Libertyf046ccd2005-07-28 10:08:46 -0500108 spd_eeprom_t spd;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500109 unsigned int memsize;
110 unsigned int law_size;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500111 unsigned char caslat, caslat_ctrl;
112 unsigned char burstlen;
113 unsigned int max_bus_clk;
114 unsigned int max_data_rate, effective_data_rate;
115 unsigned int ddrc_clk;
116 unsigned int refresh_clk;
117 unsigned sdram_cfg;
118 unsigned int ddrc_ecc_enable;
Jon Loeligerde1d0a62005-08-01 13:20:47 -0500119
Dave Liuf6eda7f2006-10-25 14:41:21 -0500120 /* Read SPD parameters with I2C */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500121 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +0100122#ifdef SPD_DEBUG
123 spd_debug(&spd);
124#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500125 /* Check the memory type */
126 if (spd.mem_type != SPD_MEMTYPE_DDR) {
127 printf("DDR: Module mem type is %02X\n", spd.mem_type);
128 return 0;
129 }
130
131 /* Check the number of physical bank */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500132 if (spd.nrows > 2) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500133 printf("DDR: The number of physical bank is %02X\n", spd.nrows);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500134 return 0;
135 }
136
Dave Liuf6eda7f2006-10-25 14:41:21 -0500137 /* Check if the number of row of the module is in the range of DDRC */
138 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
139 printf("DDR: Row number is out of range of DDRC, row=%02X\n",
140 spd.nrow_addr);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500141 return 0;
142 }
143
Dave Liuf6eda7f2006-10-25 14:41:21 -0500144 /* Check if the number of col of the module is in the range of DDRC */
145 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
146 printf("DDR: Col number is out of range of DDRC, col=%02X\n",
147 spd.ncol_addr);
148 return 0;
149 }
150 /* Setup DDR chip select register */
Dave Liu5f820432006-11-03 19:33:44 -0600151#ifdef CFG_83XX_DDR_USES_CS0
152 ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
153 ddr->cs_config[0] = ( 1 << 31
154 | (spd.nrow_addr - 12) << 8
155 | (spd.ncol_addr - 8) );
156 debug("\n");
157 debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
158 debug("cs0_config = 0x%08x\n",ddr->cs_config[0]);
159
160 if (spd.nrows == 2) {
161 ddr->csbnds[1].csbnds = ( (banksize(spd.row_dens) >> 8)
162 | ((banksize(spd.row_dens) >> 23) - 1) );
163 ddr->cs_config[1] = ( 1<<31
164 | (spd.nrow_addr-12) << 8
165 | (spd.ncol_addr-8) );
166 debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
167 debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
168 }
169
170#else
Eran Libertyf046ccd2005-07-28 10:08:46 -0500171 ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
172 ddr->cs_config[2] = ( 1 << 31
173 | (spd.nrow_addr - 12) << 8
174 | (spd.ncol_addr - 8) );
175 debug("\n");
176 debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
177 debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
Jon Loeligerde1d0a62005-08-01 13:20:47 -0500178
Eran Libertyf046ccd2005-07-28 10:08:46 -0500179 if (spd.nrows == 2) {
180 ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
181 | ((banksize(spd.row_dens) >> 23) - 1) );
182 ddr->cs_config[3] = ( 1<<31
183 | (spd.nrow_addr-12) << 8
184 | (spd.ncol_addr-8) );
185 debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
186 debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
187 }
Timur Tabi2ad6b512006-10-31 18:44:42 -0600188#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500189
190 if (spd.mem_type != 0x07) {
191 puts("No DDR module found!\n");
192 return 0;
193 }
194
195 /*
196 * Figure out memory size in Megabytes.
197 */
198 memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
199
200 /*
201 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
202 */
203 law_size = 19 + __ilog2(memsize);
204
205 /*
206 * Set up LAWBAR for all of DDR.
207 */
208 ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
209 ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
210 debug("DDR:bar=0x%08x\n", ecm->bar);
211 debug("DDR:ar=0x%08x\n", ecm->ar);
212
213 /*
Dave Liuf6eda7f2006-10-25 14:41:21 -0500214 * Find the largest CAS by locating the highest 1 bit
215 * in the spd.cas_lat field. Translate it to a DDR
216 * controller field value:
217 *
218 * CAS Lat DDR I Ctrl
219 * Clocks SPD Bit Value
220 * -------+--------+---------
221 * 1.0 0 001
222 * 1.5 1 010
223 * 2.0 2 011
224 * 2.5 3 100
225 * 3.0 4 101
226 * 3.5 5 110
227 * 4.0 6 111
Eran Libertyf046ccd2005-07-28 10:08:46 -0500228 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500229 caslat = __ilog2(spd.cas_lat);
230
Dave Liu5f820432006-11-03 19:33:44 -0600231 if (caslat > 6 ) {
232 printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n",
233 spd.cas_lat);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500234 return 0;
235 }
Dave Liuf6eda7f2006-10-25 14:41:21 -0500236 max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
237 + (spd.clk_cycle & 0x0f));
238 max_data_rate = max_bus_clk * 2;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500239
Dave Liuf6eda7f2006-10-25 14:41:21 -0500240 debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500241
Timur Tabie857a5b2006-11-28 12:09:35 -0600242 ddrc_clk = gd->ddr_clk / 1000000;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500243
244 if (max_data_rate >= 390) { /* it is DDR 400 */
Dave Liu5f820432006-11-03 19:33:44 -0600245 if (ddrc_clk <= 410 && ddrc_clk > 350) {
246 /* DDR controller clk at 350~410 */
247 effective_data_rate = 400; /* 5ns */
248 caslat = caslat;
249 } else if (ddrc_clk <= 350 && ddrc_clk > 280) {
250 /* DDR controller clk at 280~350 */
251 effective_data_rate = 333; /* 6ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600252 if (spd.clk_cycle2 == 0x60)
Dave Liu5f820432006-11-03 19:33:44 -0600253 caslat = caslat - 1;
Timur Tabie857a5b2006-11-28 12:09:35 -0600254 else
Dave Liu5f820432006-11-03 19:33:44 -0600255 caslat = caslat;
Dave Liu5f820432006-11-03 19:33:44 -0600256 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
257 /* DDR controller clk at 230~280 */
258 effective_data_rate = 266; /* 7.5ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600259 if (spd.clk_cycle3 == 0x75)
Dave Liu5f820432006-11-03 19:33:44 -0600260 caslat = caslat - 2;
Timur Tabie857a5b2006-11-28 12:09:35 -0600261 else if (spd.clk_cycle2 == 0x60)
Dave Liu5f820432006-11-03 19:33:44 -0600262 caslat = caslat - 1;
Timur Tabie857a5b2006-11-28 12:09:35 -0600263 else
Dave Liu5f820432006-11-03 19:33:44 -0600264 caslat = caslat;
Dave Liu5f820432006-11-03 19:33:44 -0600265 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
266 /* DDR controller clk at 90~230 */
267 effective_data_rate = 200; /* 10ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600268 if (spd.clk_cycle3 == 0x75)
Dave Liu5f820432006-11-03 19:33:44 -0600269 caslat = caslat - 2;
Timur Tabie857a5b2006-11-28 12:09:35 -0600270 else if (spd.clk_cycle2 == 0x60)
Dave Liu5f820432006-11-03 19:33:44 -0600271 caslat = caslat - 1;
Timur Tabie857a5b2006-11-28 12:09:35 -0600272 else
Dave Liu5f820432006-11-03 19:33:44 -0600273 caslat = caslat;
Dave Liu5f820432006-11-03 19:33:44 -0600274 }
Dave Liuf6eda7f2006-10-25 14:41:21 -0500275 } else if (max_data_rate >= 323) { /* it is DDR 333 */
276 if (ddrc_clk <= 350 && ddrc_clk > 280) {
Dave Liu5f820432006-11-03 19:33:44 -0600277 /* DDR controller clk at 280~350 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500278 effective_data_rate = 333; /* 6ns */
279 caslat = caslat;
280 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
Dave Liu5f820432006-11-03 19:33:44 -0600281 /* DDR controller clk at 230~280 */
282 effective_data_rate = 266; /* 7.5ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600283 if (spd.clk_cycle2 == 0x75)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500284 caslat = caslat - 1;
Timur Tabie857a5b2006-11-28 12:09:35 -0600285 else
Dave Liu5f820432006-11-03 19:33:44 -0600286 caslat = caslat;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500287 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
Dave Liu5f820432006-11-03 19:33:44 -0600288 /* DDR controller clk at 90~230 */
289 effective_data_rate = 200; /* 10ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600290 if (spd.clk_cycle3 == 0xa0)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500291 caslat = caslat - 2;
Timur Tabie857a5b2006-11-28 12:09:35 -0600292 else if (spd.clk_cycle2 == 0x75)
Dave Liu5f820432006-11-03 19:33:44 -0600293 caslat = caslat - 1;
Timur Tabie857a5b2006-11-28 12:09:35 -0600294 else
Dave Liu5f820432006-11-03 19:33:44 -0600295 caslat = caslat;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500296 }
297 } else if (max_data_rate >= 256) { /* it is DDR 266 */
298 if (ddrc_clk <= 350 && ddrc_clk > 280) {
Dave Liu5f820432006-11-03 19:33:44 -0600299 /* DDR controller clk at 280~350 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500300 printf("DDR: DDR controller freq is more than "
301 "max data rate of the module\n");
302 return 0;
303 } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
Dave Liu5f820432006-11-03 19:33:44 -0600304 /* DDR controller clk at 230~280 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500305 effective_data_rate = 266; /* 7.5ns */
306 caslat = caslat;
307 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
Dave Liu5f820432006-11-03 19:33:44 -0600308 /* DDR controller clk at 90~230 */
309 effective_data_rate = 200; /* 10ns */
Timur Tabie857a5b2006-11-28 12:09:35 -0600310 if (spd.clk_cycle2 == 0xa0)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500311 caslat = caslat - 1;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500312 }
313 } else if (max_data_rate >= 190) { /* it is DDR 200 */
314 if (ddrc_clk <= 350 && ddrc_clk > 230) {
Dave Liu5f820432006-11-03 19:33:44 -0600315 /* DDR controller clk at 230~350 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500316 printf("DDR: DDR controller freq is more than "
317 "max data rate of the module\n");
318 return 0;
319 } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
Dave Liu5f820432006-11-03 19:33:44 -0600320 /* DDR controller clk at 90~230 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500321 effective_data_rate = 200; /* 10ns */
322 caslat = caslat;
323 }
Eran Libertyf046ccd2005-07-28 10:08:46 -0500324 }
325
Dave Liu5f820432006-11-03 19:33:44 -0600326 debug("DDR:Effective data rate is: %dMhz\n", effective_data_rate);
327 debug("DDR:The MSB 1 of CAS Latency is: %d\n", caslat);
Timur Tabibed85ca2006-10-31 18:13:36 -0600328
Dave Liu5f820432006-11-03 19:33:44 -0600329 /*
330 * Errata DDR6 work around: input enable 2 cycles earlier.
331 * including MPC834x Rev1.0/1.1 and MPC8360 Rev1.1/1.2.
332 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600333 if (caslat == 2)
Dave Liu5f820432006-11-03 19:33:44 -0600334 ddr->debug_reg = 0x201c0000; /* CL=2 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600335 else if (caslat == 3)
Dave Liu5f820432006-11-03 19:33:44 -0600336 ddr->debug_reg = 0x202c0000; /* CL=2.5 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600337 else if (caslat == 4)
Dave Liu5f820432006-11-03 19:33:44 -0600338 ddr->debug_reg = 0x202c0000; /* CL=3.0 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600339
Dave Liu5f820432006-11-03 19:33:44 -0600340 __asm__ __volatile__ ("sync");
Timur Tabibed85ca2006-10-31 18:13:36 -0600341
Dave Liu5f820432006-11-03 19:33:44 -0600342 debug("Errata DDR6 (debug_reg=0x%08x)\n", ddr->debug_reg);
Timur Tabibed85ca2006-10-31 18:13:36 -0600343
Eran Libertyf046ccd2005-07-28 10:08:46 -0500344 /*
345 * note: caslat must also be programmed into ddr->sdram_mode
346 * register.
347 *
348 * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
349 * use conservative value here.
350 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500351 caslat_ctrl = (caslat + 1) & 0x07; /* see as above */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500352
353 ddr->timing_cfg_1 =
354 (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
355 ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
356 ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
Dave Liuf6eda7f2006-10-25 14:41:21 -0500357 ((caslat_ctrl & 0x07) << 16 ) |
358 (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) |
Eran Libertyf046ccd2005-07-28 10:08:46 -0500359 ( 0x300 ) |
360 ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
361
362 ddr->timing_cfg_2 = 0x00000800;
363
364 debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
365 debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
Dave Liuf6eda7f2006-10-25 14:41:21 -0500366 /* Setup init value, but not enable */
367 ddr->sdram_cfg = 0x42000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500368
Dave Liuf6eda7f2006-10-25 14:41:21 -0500369 /* Check DIMM data bus width */
Dave Liu5f820432006-11-03 19:33:44 -0600370 if (spd.dataw_lsb == 0x20) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500371 burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
372 printf("\n DDR DIMM: data bus width is 32 bit");
Dave Liu5f820432006-11-03 19:33:44 -0600373 } else {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500374 burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
375 printf("\n DDR DIMM: data bus width is 64 bit");
376 }
377
378 /* Is this an ECC DDR chip? */
Timur Tabie857a5b2006-11-28 12:09:35 -0600379 if (spd.config == 0x02)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500380 printf(" with ECC\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600381 else
Dave Liuf6eda7f2006-10-25 14:41:21 -0500382 printf(" without ECC\n");
383
384 /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
385 Burst type is sequential
Eran Libertyf046ccd2005-07-28 10:08:46 -0500386 */
Dave Liu5f820432006-11-03 19:33:44 -0600387 switch (caslat) {
388 case 1:
389 ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
390 break;
391 case 2:
392 ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
393 break;
394 case 3:
395 ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
396 break;
397 case 4:
398 ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
399 break;
400 default:
401 printf("DDR:only CL 1.5, 2.0, 2.5, 3.0 is supported\n");
402 return 0;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500403 }
404 debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
405
Dave Liu5f820432006-11-03 19:33:44 -0600406 switch (spd.refresh) {
407 case 0x00:
408 case 0x80:
409 refresh_clk = picos_to_clk(15625000);
410 break;
411 case 0x01:
412 case 0x81:
413 refresh_clk = picos_to_clk(3900000);
414 break;
415 case 0x02:
416 case 0x82:
417 refresh_clk = picos_to_clk(7800000);
418 break;
419 case 0x03:
420 case 0x83:
421 refresh_clk = picos_to_clk(31300000);
422 break;
423 case 0x04:
424 case 0x84:
425 refresh_clk = picos_to_clk(62500000);
426 break;
427 case 0x05:
428 case 0x85:
429 refresh_clk = picos_to_clk(125000000);
430 break;
431 default:
432 refresh_clk = 0x512;
433 break;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500434 }
435
436 /*
437 * Set BSTOPRE to 0x100 for page mode
438 * If auto-charge is used, set BSTOPRE = 0
439 */
Dave Liu5f820432006-11-03 19:33:44 -0600440 ddr->sdram_interval = ((refresh_clk & 0x3fff) << 16) | 0x100;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500441 debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
442
Dave Liuf6eda7f2006-10-25 14:41:21 -0500443 /* SS_EN = 0, source synchronous disable
444 * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd
445 */
446 ddr->sdram_clk_cntl = 0x00000000;
447 debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100448
Eran Libertyf046ccd2005-07-28 10:08:46 -0500449 asm("sync;isync");
450
Dave Liuf6eda7f2006-10-25 14:41:21 -0500451 udelay(600);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500452
453 /*
Dave Liu5f820432006-11-03 19:33:44 -0600454 * Figure out the settings for the sdram_cfg register. Build up
455 * the value in 'sdram_cfg' before writing since the write into
Eran Libertyf046ccd2005-07-28 10:08:46 -0500456 * the register will actually enable the memory controller, and all
457 * settings must be done before enabling.
458 *
459 * sdram_cfg[0] = 1 (ddr sdram logic enable)
460 * sdram_cfg[1] = 1 (self-refresh-enable)
461 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500462 * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
463 * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500464 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500465 sdram_cfg = 0xC2000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500466
Dave Liuf6eda7f2006-10-25 14:41:21 -0500467 /* sdram_cfg[3] = RD_EN - registered DIMM enable */
Timur Tabie857a5b2006-11-28 12:09:35 -0600468 if (spd.mod_attr & 0x02)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500469 sdram_cfg |= 0x10000000;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500470
Dave Liuf6eda7f2006-10-25 14:41:21 -0500471 /* The DIMM is 32bit width */
Timur Tabie857a5b2006-11-28 12:09:35 -0600472 if (spd.dataw_lsb == 0x20)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500473 sdram_cfg |= 0x000C0000;
Timur Tabie857a5b2006-11-28 12:09:35 -0600474
Dave Liuf6eda7f2006-10-25 14:41:21 -0500475 ddrc_ecc_enable = 0;
476
Eran Libertyf046ccd2005-07-28 10:08:46 -0500477#if defined(CONFIG_DDR_ECC)
Dave Liuf6eda7f2006-10-25 14:41:21 -0500478 /* Enable ECC with sdram_cfg[2] */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500479 if (spd.config == 0x02) {
Dave Liuf6eda7f2006-10-25 14:41:21 -0500480 sdram_cfg |= 0x20000000;
481 ddrc_ecc_enable = 1;
482 /* disable error detection */
483 ddr->err_disable = ~ECC_ERROR_ENABLE;
484 /* set single bit error threshold to maximum value,
485 * reset counter to zero */
486 ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
Dave Liu5f820432006-11-03 19:33:44 -0600487 (0 << ECC_ERROR_MAN_SBEC_SHIFT);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500488 }
Dave Liuf6eda7f2006-10-25 14:41:21 -0500489
490 debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
491 debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500492#endif
Dave Liu5f820432006-11-03 19:33:44 -0600493 printf(" DDRC ECC mode: %s\n", ddrc_ecc_enable ? "ON":"OFF");
Eran Libertyf046ccd2005-07-28 10:08:46 -0500494
495#if defined(CONFIG_DDR_2T_TIMING)
496 /*
497 * Enable 2T timing by setting sdram_cfg[16].
498 */
Dave Liuf6eda7f2006-10-25 14:41:21 -0500499 sdram_cfg |= SDRAM_CFG_2T_EN;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500500#endif
Dave Liuf6eda7f2006-10-25 14:41:21 -0500501 /* Enable controller, and GO! */
502 ddr->sdram_cfg = sdram_cfg;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500503 asm("sync;isync");
504 udelay(500);
505
506 debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
Rafal Jaworowskidc9e4992006-03-16 17:46:46 +0100507 return memsize; /*in MBytes*/
Eran Libertyf046ccd2005-07-28 10:08:46 -0500508}
Eran Libertyf046ccd2005-07-28 10:08:46 -0500509#endif /* CONFIG_SPD_EEPROM */
510
Dave Liuf6eda7f2006-10-25 14:41:21 -0500511#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500512/*
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100513 * Use timebase counter, get_timer() is not availabe
514 * at this point of initialization yet.
515 */
516static __inline__ unsigned long get_tbms (void)
517{
518 unsigned long tbl;
519 unsigned long tbu1, tbu2;
520 unsigned long ms;
521 unsigned long long tmp;
522
523 ulong tbclk = get_tbclk();
524
525 /* get the timebase ticks */
526 do {
527 asm volatile ("mftbu %0":"=r" (tbu1):);
528 asm volatile ("mftb %0":"=r" (tbl):);
529 asm volatile ("mftbu %0":"=r" (tbu2):);
530 } while (tbu1 != tbu2);
531
532 /* convert ticks to ms */
533 tmp = (unsigned long long)(tbu1);
534 tmp = (tmp << 32);
535 tmp += (unsigned long long)(tbl);
536 ms = tmp/(tbclk/1000);
537
538 return ms;
539}
540
541/*
Eran Libertyf046ccd2005-07-28 10:08:46 -0500542 * Initialize all of memory for ECC, then enable errors.
543 */
Wolfgang Denkcf48eb92006-04-16 10:51:58 +0200544/* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100545void ddr_enable_ecc(unsigned int dram_size)
Eran Libertyf046ccd2005-07-28 10:08:46 -0500546{
Timur Tabid239d742006-11-03 12:00:28 -0600547 volatile immap_t *immap = (immap_t *)CFG_IMMR;
Dave Liuf6eda7f2006-10-25 14:41:21 -0500548 volatile ddr83xx_t *ddr= &immap->ddr;
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100549 unsigned long t_start, t_end;
Dave Liu90f30a72006-11-02 18:05:50 -0600550 register u64 *p;
551 register uint size;
552 unsigned int pattern[2];
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100553#if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
554 uint i;
555#endif
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100556 icache_enable();
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100557 t_start = get_tbms();
Dave Liu90f30a72006-11-02 18:05:50 -0600558 pattern[0] = 0xdeadbeef;
559 pattern[1] = 0xdeadbeef;
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100560
561#if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
Dave Liu90f30a72006-11-02 18:05:50 -0600562 debug("ddr init: CPU FP write method\n");
563 size = dram_size;
564 for (p = 0; p < (u64*)(size); p++) {
565 ppcDWstore((u32*)p, pattern);
Eran Libertyf046ccd2005-07-28 10:08:46 -0500566 }
Dave Liu90f30a72006-11-02 18:05:50 -0600567 __asm__ __volatile__ ("sync");
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100568#else
Dave Liu90f30a72006-11-02 18:05:50 -0600569 debug("ddr init: DMA method\n");
570 size = 0x2000;
571 for (p = 0; p < (u64*)(size); p++) {
572 ppcDWstore((u32*)p, pattern);
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100573 }
Dave Liu90f30a72006-11-02 18:05:50 -0600574 __asm__ __volatile__ ("sync");
Eran Libertyf046ccd2005-07-28 10:08:46 -0500575
Dave Liu90f30a72006-11-02 18:05:50 -0600576 /* Initialise DMA for direct transfer */
577 dma_init();
578 /* Start DMA to transfer */
Dave Liu5f820432006-11-03 19:33:44 -0600579 dma_xfer((uint *)0x2000, 0x2000, (uint *)0); /* 8K */
580 dma_xfer((uint *)0x4000, 0x4000, (uint *)0); /* 16K */
581 dma_xfer((uint *)0x8000, 0x8000, (uint *)0); /* 32K */
582 dma_xfer((uint *)0x10000, 0x10000, (uint *)0); /* 64K */
583 dma_xfer((uint *)0x20000, 0x20000, (uint *)0); /* 128K */
584 dma_xfer((uint *)0x40000, 0x40000, (uint *)0); /* 256K */
585 dma_xfer((uint *)0x80000, 0x80000, (uint *)0); /* 512K */
586 dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
587 dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
588 dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
Eran Libertyf046ccd2005-07-28 10:08:46 -0500589
590 for (i = 1; i < dram_size / 0x800000; i++) {
591 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
592 }
Jon Loeligerde1d0a62005-08-01 13:20:47 -0500593#endif
Eran Libertyf046ccd2005-07-28 10:08:46 -0500594
Marian Balakowicz4c8d1ec2006-03-14 16:23:35 +0100595 t_end = get_tbms();
596 icache_disable();
597
598 debug("\nREADY!!\n");
599 debug("ddr init duration: %ld ms\n", t_end - t_start);
600
601 /* Clear All ECC Errors */
602 if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
603 ddr->err_detect |= ECC_ERROR_DETECT_MME;
604 if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
605 ddr->err_detect |= ECC_ERROR_DETECT_MBE;
606 if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
607 ddr->err_detect |= ECC_ERROR_DETECT_SBE;
608 if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
609 ddr->err_detect |= ECC_ERROR_DETECT_MSE;
610
611 /* Disable ECC-Interrupts */
612 ddr->err_int_en &= ECC_ERR_INT_DISABLE;
613
614 /* Enable errors for ECC */
615 ddr->err_disable &= ECC_ERROR_ENABLE;
616
617 __asm__ __volatile__ ("sync");
618 __asm__ __volatile__ ("isync");
619}
Eran Libertyf046ccd2005-07-28 10:08:46 -0500620#endif /* CONFIG_DDR_ECC */