blob: 97bbf4386855997f8e0041b752f3c71c7a58f8a0 [file] [log] [blame]
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001/*
2 * Copyright 2004 Freescale Semiconductor.
3 * (C) Copyright 2003 Motorola Inc.
4 * Xianghua Xiao (X.Xiao@motorola.com)
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <asm/processor.h>
27#include <i2c.h>
28#include <spd.h>
29#include <asm/mmu.h>
30
31
32#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
33extern void dma_init(void);
34extern uint dma_check(void);
35extern int dma_xfer(void *dest, uint count, void *src);
36#endif
37
38#ifdef CONFIG_SPD_EEPROM
39
40#ifndef CFG_READ_SPD
41#define CFG_READ_SPD i2c_read
42#endif
43
44/*
Jon Loeliger598e1722006-05-19 13:26:34 -050045 * Only one of the following three should be 1; others should be 0
46 * By default the cache line interleaving is selected if
John Traillfcd18ba2006-08-08 11:32:43 +010047 * the CONFIG_DDR_INTERLEAVE flag is defined
Jon Loeliger598e1722006-05-19 13:26:34 -050048 */
49#define CFG_PAGE_INTERLEAVING 0
50#define CFG_BANK_INTERLEAVING 0
51#define CFG_SUPER_BANK_INTERLEAVING 0
52
53/*
Jon Loeliger5c8aa972006-04-26 17:58:56 -050054 * Convert picoseconds into clock cycles (rounding up if needed).
55 */
56
57int
58picos_to_clk(int picos)
59{
60 int clks;
61
62 clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
63 if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
64 clks++;
65 }
66
67 return clks;
68}
69
70
71/*
72 * Calculate the Density of each Physical Rank.
73 * Returned size is in bytes.
74 *
75 * Study these table from Byte 31 of JEDEC SPD Spec.
76 *
77 * DDR I DDR II
78 * Bit Size Size
79 * --- ----- ------
80 * 7 high 512MB 512MB
81 * 6 256MB 256MB
82 * 5 128MB 128MB
83 * 4 64MB 16GB
84 * 3 32MB 8GB
85 * 2 16MB 4GB
86 * 1 2GB 2GB
87 * 0 low 1GB 1GB
88 *
89 * Reorder Table to be linear by stripping the bottom
90 * 2 or 5 bits off and shifting them up to the top.
91 */
92
93unsigned int
94compute_banksize(unsigned int mem_type, unsigned char row_dens)
95{
96 unsigned int bsize;
97
98 if (mem_type == SPD_MEMTYPE_DDR) {
99 /* Bottom 2 bits up to the top. */
100 bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
101 debug("DDR: DDR I rank density = 0x%08x\n", bsize);
102 } else {
103 /* Bottom 5 bits up to the top. */
104 bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
105 debug("DDR: DDR II rank density = 0x%08x\n", bsize);
106 }
107 return bsize;
108}
109
110
111/*
112 * Convert a two-nibble BCD value into a cycle time.
113 * While the spec calls for nano-seconds, picos are returned.
114 *
115 * This implements the tables for bytes 9, 23 and 25 for both
116 * DDR I and II. No allowance for distinguishing the invalid
117 * fields absent for DDR I yet present in DDR II is made.
118 * (That is, cycle times of .25, .33, .66 and .75 ns are
119 * allowed for both DDR II and I.)
120 */
121
122unsigned int
123convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
124{
125 /*
126 * Table look up the lower nibble, allow DDR I & II.
127 */
128 unsigned int tenths_ps[16] = {
129 0,
130 100,
131 200,
132 300,
133 400,
134 500,
135 600,
136 700,
137 800,
138 900,
139 250,
John Traillfcd18ba2006-08-08 11:32:43 +0100140 330,
141 660,
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500142 750,
143 0, /* undefined */
144 0 /* undefined */
145 };
146
147 unsigned int whole_ns = (spd_val & 0xF0) >> 4;
148 unsigned int tenth_ns = spd_val & 0x0F;
149 unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
150
151 return ps;
152}
153
154
Jon Loeligerae13a642006-10-10 17:19:03 -0500155/*
156 * Determine Refresh Rate. Ignore self refresh bit on DDR I.
157 * Table from SPD Spec, Byte 12, converted to picoseconds and
158 * filled in with "default" normal values.
159 */
160unsigned int determine_refresh_rate(unsigned int spd_refresh)
161{
162 unsigned int refresh_time_ns[8] = {
163 15625000, /* 0 Normal 1.00x */
164 3900000, /* 1 Reduced .25x */
165 7800000, /* 2 Extended .50x */
166 31300000, /* 3 Extended 2.00x */
167 62500000, /* 4 Extended 4.00x */
168 125000000, /* 5 Extended 8.00x */
169 15625000, /* 6 Normal 1.00x filler */
170 15625000, /* 7 Normal 1.00x filler */
171 };
172
173 return picos_to_clk(refresh_time_ns[spd_refresh & 0x7]);
174}
175
176
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500177long int
Jon Loeliger598e1722006-05-19 13:26:34 -0500178spd_init(unsigned char i2c_address, unsigned int ddr_num,
179 unsigned int dimm_num, unsigned int start_addr)
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500180{
181 volatile immap_t *immap = (immap_t *)CFG_IMMR;
Jon Loeliger598e1722006-05-19 13:26:34 -0500182 volatile ccsr_ddr_t *ddr;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500183 volatile ccsr_gur_t *gur = &immap->im_gur;
184 spd_eeprom_t spd;
185 unsigned int n_ranks;
186 unsigned int rank_density;
187 unsigned int odt_rd_cfg, odt_wr_cfg;
188 unsigned int odt_cfg, mode_odt_enable;
Jon Loeligerae13a642006-10-10 17:19:03 -0500189 unsigned int refresh_clk;
190#ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
191 unsigned char clk_adjust;
192#endif
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500193 unsigned int dqs_cfg;
194 unsigned char twr_clk, twtr_clk, twr_auto_clk;
195 unsigned int tCKmin_ps, tCKmax_ps;
John Traillfcd18ba2006-08-08 11:32:43 +0100196 unsigned int max_data_rate;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500197 unsigned int busfreq;
198 unsigned sdram_cfg_1;
199 unsigned int memsize;
200 unsigned char caslat, caslat_ctrl;
201 unsigned int trfc, trfc_clk, trfc_low, trfc_high;
202 unsigned int trcd_clk;
203 unsigned int trtp_clk;
204 unsigned char cke_min_clk;
205 unsigned char add_lat;
206 unsigned char wr_lat;
207 unsigned char wr_data_delay;
208 unsigned char four_act;
209 unsigned char cpo;
210 unsigned char burst_len;
211 unsigned int mode_caslat;
212 unsigned char sdram_type;
213 unsigned char d_init;
Jon Loeliger598e1722006-05-19 13:26:34 -0500214 unsigned int law_size;
215 volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
John Traillfcd18ba2006-08-08 11:32:43 +0100216 unsigned int tCycle_ps, modfreq;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500217
Jon Loeliger598e1722006-05-19 13:26:34 -0500218 if (ddr_num == 1)
219 ddr = &immap->im_ddr1;
220 else
221 ddr = &immap->im_ddr2;
Jon Loeliger465b9d82006-04-27 10:15:16 -0500222
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500223 /*
224 * Read SPD information.
225 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500226 debug("Performing SPD read at I2C address 0x%02lx\n",i2c_address);
227 memset((void *)&spd, 0, sizeof(spd));
228 CFG_READ_SPD(i2c_address, 0, 1, (uchar *) &spd, sizeof(spd));
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500229
230 /*
231 * Check for supported memory module types.
232 */
233 if (spd.mem_type != SPD_MEMTYPE_DDR &&
234 spd.mem_type != SPD_MEMTYPE_DDR2) {
Jon Loeliger598e1722006-05-19 13:26:34 -0500235 debug("Warning: Unable to locate DDR I or DDR II module for DIMM %d of DDR controller %d.\n"
236 " Fundamental memory type is 0x%0x\n",
237 dimm_num,
238 ddr_num,
239 spd.mem_type);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500240 return 0;
241 }
242
Jon Loeliger598e1722006-05-19 13:26:34 -0500243 debug("\nFound memory of type 0x%02lx ", spd.mem_type);
244 if (spd.mem_type == SPD_MEMTYPE_DDR)
245 debug("DDR I\n");
246 else
247 debug("DDR II\n");
248
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500249 /*
250 * These test gloss over DDR I and II differences in interpretation
251 * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
252 * are not supported on DDR I; and not encoded on DDR II.
253 *
254 * Also note that the 8548 controller can support:
255 * 12 <= nrow <= 16
256 * and
257 * 8 <= ncol <= 11 (still, for DDR)
258 * 6 <= ncol <= 9 (for FCRAM)
259 */
260 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
261 printf("DDR: Unsupported number of Row Addr lines: %d.\n",
262 spd.nrow_addr);
263 return 0;
264 }
265 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
266 printf("DDR: Unsupported number of Column Addr lines: %d.\n",
267 spd.ncol_addr);
268 return 0;
269 }
270
271 /*
272 * Determine the number of physical banks controlled by
273 * different Chip Select signals. This is not quite the
274 * same as the number of DIMM modules on the board. Feh.
275 */
276 if (spd.mem_type == SPD_MEMTYPE_DDR) {
277 n_ranks = spd.nrows;
278 } else {
279 n_ranks = (spd.nrows & 0x7) + 1;
280 }
281
282 debug("DDR: number of ranks = %d\n", n_ranks);
283
284 if (n_ranks > 2) {
285 printf("DDR: Only 2 chip selects are supported: %d\n",
286 n_ranks);
287 return 0;
288 }
289
290 /*
291 * Adjust DDR II IO voltage biasing. It just makes it work.
292 */
293 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
294 gur->ddrioovcr = (0
295 | 0x80000000 /* Enable */
296 | 0x10000000 /* VSEL to 1.8V */
297 );
298 }
299
300 /*
301 * Determine the size of each Rank in bytes.
302 */
303 rank_density = compute_banksize(spd.mem_type, spd.row_dens);
304
Jon Loeliger598e1722006-05-19 13:26:34 -0500305 debug("Start address for this controller is 0x%08lx\n", start_addr);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500306
307 /*
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500308 * ODT configuration recommendation from DDR Controller Chapter.
309 */
310 odt_rd_cfg = 0; /* Never assert ODT */
311 odt_wr_cfg = 0; /* Never assert ODT */
312 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
313 odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
314 }
315
Jon Loeliger598e1722006-05-19 13:26:34 -0500316#ifdef CONFIG_DDR_INTERLEAVE
John Traillfcd18ba2006-08-08 11:32:43 +0100317
Jon Loeliger598e1722006-05-19 13:26:34 -0500318 if (dimm_num != 1) {
319 printf("For interleaving memory on HPCN, need to use DIMM 1 for DDR Controller %d !\n", ddr_num);
320 return 0;
321 } else {
322 /*
323 * Since interleaved memory only uses CS0, the
324 * memory sticks have to be identical in size and quantity
325 * of ranks. That essentially gives double the size on
326 * one rank, i.e on CS0 for both controllers put together.
327 * Confirm this???
328 */
329 rank_density *= 2;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500330
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500331 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500332 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500333 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500334 start_addr = 0;
335 ddr->cs0_bnds = (start_addr >> 8)
336 | (((start_addr + rank_density - 1) >> 24));
337 /*
338 * Default interleaving mode to cache-line interleaving.
339 */
340 ddr->cs0_config = ( 1 << 31
341#if (CFG_PAGE_INTERLEAVING == 1)
342 | (PAGE_INTERLEAVING)
343#elif (CFG_BANK_INTERLEAVING == 1)
344 | (BANK_INTERLEAVING)
345#elif (CFG_SUPER_BANK_INTERLEAVING == 1)
346 | (SUPER_BANK_INTERLEAVING)
347#else
348 | (CACHE_LINE_INTERLEAVING)
349#endif
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500350 | (odt_rd_cfg << 20)
351 | (odt_wr_cfg << 16)
352 | (spd.nrow_addr - 12) << 8
353 | (spd.ncol_addr - 8) );
Jon Loeliger598e1722006-05-19 13:26:34 -0500354
355 debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
356 debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
357
358 /*
359 * Adjustment for dual rank memory to get correct memory
360 * size (return value of this function).
361 */
362 if (n_ranks == 2) {
363 n_ranks = 1;
364 rank_density /= 2;
365 } else {
366 rank_density /= 2;
367 }
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500368 }
Jon Loeliger598e1722006-05-19 13:26:34 -0500369#else /* CONFIG_DDR_INTERLEAVE */
370
371 if (dimm_num == 1) {
372 /*
373 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
374 */
375 ddr->cs0_bnds = (start_addr >> 8)
376 | (((start_addr + rank_density - 1) >> 24));
377
378 ddr->cs0_config = ( 1 << 31
379 | (odt_rd_cfg << 20)
380 | (odt_wr_cfg << 16)
381 | (spd.nrow_addr - 12) << 8
382 | (spd.ncol_addr - 8) );
383
384 debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
385 debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
386
387 if (n_ranks == 2) {
388 /*
389 * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
390 * second 256 Meg
391 */
392 ddr->cs1_bnds = (((start_addr + rank_density) >> 8)
393 | (( start_addr + 2*rank_density - 1)
394 >> 24));
395 ddr->cs1_config = ( 1<<31
396 | (odt_rd_cfg << 20)
397 | (odt_wr_cfg << 16)
398 | (spd.nrow_addr - 12) << 8
399 | (spd.ncol_addr - 8) );
400 debug("DDR: cs1_bnds = 0x%08x\n", ddr->cs1_bnds);
401 debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
402 }
403
404 } else {
405 /*
406 * This is the 2nd DIMM slot for this controller
407 */
408 /*
409 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
410 */
411 ddr->cs2_bnds = (start_addr >> 8)
412 | (((start_addr + rank_density - 1) >> 24));
413
414 ddr->cs2_config = ( 1 << 31
415 | (odt_rd_cfg << 20)
416 | (odt_wr_cfg << 16)
417 | (spd.nrow_addr - 12) << 8
418 | (spd.ncol_addr - 8) );
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500419
Jon Loeliger598e1722006-05-19 13:26:34 -0500420 debug("DDR: cs2_bnds = 0x%08x\n", ddr->cs2_bnds);
421 debug("DDR: cs2_config = 0x%08x\n", ddr->cs2_config);
422
423 if (n_ranks == 2) {
424 /*
425 * Eg: Bounds: 0x1000_0000 to 0x1f00_0000,
426 * second 256 Meg
427 */
428 ddr->cs3_bnds = (((start_addr + rank_density) >> 8)
429 | (( start_addr + 2*rank_density - 1)
430 >> 24));
431 ddr->cs3_config = ( 1<<31
432 | (odt_rd_cfg << 20)
433 | (odt_wr_cfg << 16)
434 | (spd.nrow_addr - 12) << 8
435 | (spd.ncol_addr - 8) );
436 debug("DDR: cs3_bnds = 0x%08x\n", ddr->cs3_bnds);
437 debug("DDR: cs3_config = 0x%08x\n", ddr->cs3_config);
438 }
439 }
440#endif /* CONFIG_DDR_INTERLEAVE */
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500441
442 /*
443 * Find the largest CAS by locating the highest 1 bit
444 * in the spd.cas_lat field. Translate it to a DDR
445 * controller field value:
446 *
447 * CAS Lat DDR I DDR II Ctrl
448 * Clocks SPD Bit SPD Bit Value
449 * ------- ------- ------- -----
450 * 1.0 0 0001
451 * 1.5 1 0010
452 * 2.0 2 2 0011
453 * 2.5 3 0100
454 * 3.0 4 3 0101
455 * 3.5 5 0110
456 * 4.0 4 0111
457 * 4.5 1000
458 * 5.0 5 1001
459 */
460 caslat = __ilog2(spd.cas_lat);
461 if ((spd.mem_type == SPD_MEMTYPE_DDR)
462 && (caslat > 5)) {
463 printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
464 return 0;
465
466 } else if (spd.mem_type == SPD_MEMTYPE_DDR2
467 && (caslat < 2 || caslat > 5)) {
468 printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
469 spd.cas_lat);
470 return 0;
471 }
472 debug("DDR: caslat SPD bit is %d\n", caslat);
473
474 /*
475 * Calculate the Maximum Data Rate based on the Minimum Cycle time.
476 * The SPD clk_cycle field (tCKmin) is measured in tenths of
477 * nanoseconds and represented as BCD.
478 */
479 tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
480 debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
481
482 /*
483 * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
484 */
485 max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
486 debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
487
488
489 /*
490 * Adjust the CAS Latency to allow for bus speeds that
491 * are slower than the DDR module.
492 */
493 busfreq = get_bus_freq(0) / 1000000; /* MHz */
John Traill132f1932006-09-29 08:23:12 +0100494 tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle3);
495 modfreq = 2 * 1000 * 1000 / tCycle_ps;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500496
John Traillfcd18ba2006-08-08 11:32:43 +0100497 if ((spd.mem_type == SPD_MEMTYPE_DDR2) && (busfreq < 266)) {
498 printf("DDR: platform frequency too low for correct DDR2 controller operation\n");
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500499 return 0;
John Traillfcd18ba2006-08-08 11:32:43 +0100500 } else if (busfreq < 90) {
501 printf("DDR: platform frequency too low for correct DDR1 operation\n");
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500502 return 0;
503 }
504
John Traillfcd18ba2006-08-08 11:32:43 +0100505 if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 2)))) {
506 caslat -= 2;
507 } else {
508 tCycle_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle2);
509 modfreq = 2 * 1000 * 1000 / tCycle_ps;
510 if ((busfreq <= modfreq) && (spd.cas_lat & (1 << (caslat - 1))))
511 caslat -= 1;
512 else if (busfreq > max_data_rate) {
513 printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
514 busfreq, max_data_rate);
515 return 0;
516 }
517 }
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500518
519 /*
John Traillfcd18ba2006-08-08 11:32:43 +0100520 * Empirically set ~MCAS-to-preamble override for DDR 2.
521 * Your milage will vary.
522 */
523 cpo = 0;
524 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
525 if (busfreq <= 333) {
526 cpo = 0x7;
527 } else if (busfreq <= 400) {
528 cpo = 0x9;
529 } else {
530 cpo = 0xa;
531 }
532 }
533
534 /*
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500535 * Convert caslat clocks to DDR controller value.
536 * Force caslat_ctrl to be DDR Controller field-sized.
537 */
538 if (spd.mem_type == SPD_MEMTYPE_DDR) {
539 caslat_ctrl = (caslat + 1) & 0x07;
540 } else {
541 caslat_ctrl = (2 * caslat - 1) & 0x0f;
542 }
543
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500544 debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
545 caslat, caslat_ctrl);
546
547 /*
548 * Timing Config 0.
549 * Avoid writing for DDR I. The new PQ38 DDR controller
550 * dreams up non-zero default values to be backwards compatible.
551 */
552 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
553 unsigned char taxpd_clk = 8; /* By the book. */
554 unsigned char tmrd_clk = 2; /* By the book. */
555 unsigned char act_pd_exit = 2; /* Empirical? */
556 unsigned char pre_pd_exit = 6; /* Empirical? */
557
Jon Loeliger598e1722006-05-19 13:26:34 -0500558 ddr->timing_cfg_0 = (0
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500559 | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
560 | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
561 | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
562 | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
563 );
Jon Loeliger598e1722006-05-19 13:26:34 -0500564 debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500565
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500566 }
567
568
569 /*
570 * Some Timing Config 1 values now.
571 * Sneak Extended Refresh Recovery in here too.
572 */
573
574 /*
575 * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
576 * use conservative value.
577 * For DDR II, they are bytes 36 and 37, in quarter nanos.
578 */
579
580 if (spd.mem_type == SPD_MEMTYPE_DDR) {
581 twr_clk = 3; /* Clocks */
582 twtr_clk = 1; /* Clocks */
583 } else {
584 twr_clk = picos_to_clk(spd.twr * 250);
585 twtr_clk = picos_to_clk(spd.twtr * 250);
586 }
587
588 /*
589 * Calculate Trfc, in picos.
590 * DDR I: Byte 42 straight up in ns.
591 * DDR II: Byte 40 and 42 swizzled some, in ns.
592 */
593 if (spd.mem_type == SPD_MEMTYPE_DDR) {
594 trfc = spd.trfc * 1000; /* up to ps */
595 } else {
596 unsigned int byte40_table_ps[8] = {
597 0,
598 250,
599 330,
600 500,
601 660,
602 750,
603 0,
604 0
605 };
606
607 trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
608 + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
609 }
610 trfc_clk = picos_to_clk(trfc);
611
612 /*
613 * Trcd, Byte 29, from quarter nanos to ps and clocks.
614 */
615 trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
616
617 /*
618 * Convert trfc_clk to DDR controller fields. DDR I should
619 * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
620 * 8548 controller has an extended REFREC field of three bits.
621 * The controller automatically adds 8 clocks to this value,
622 * so preadjust it down 8 first before splitting it up.
623 */
624 trfc_low = (trfc_clk - 8) & 0xf;
625 trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
626
627 /*
628 * Sneak in some Extended Refresh Recovery.
629 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500630 ddr->ext_refrec = (trfc_high << 16);
631 debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500632
Jon Loeliger598e1722006-05-19 13:26:34 -0500633 ddr->timing_cfg_1 =
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500634 (0
635 | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
636 | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
637 | (trcd_clk << 20) /* ACTTORW */
638 | (caslat_ctrl << 16) /* CASLAT */
639 | (trfc_low << 12) /* REFEC */
640 | ((twr_clk & 0x07) << 8) /* WRRREC */
641 | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
642 | ((twtr_clk & 0x07) << 0) /* WRTORD */
643 );
644
Jon Loeliger598e1722006-05-19 13:26:34 -0500645 debug("DDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500646
647
648 /*
649 * Timing_Config_2
650 * Was: 0x00000800;
651 */
652
653 /*
654 * Additive Latency
655 * For DDR I, 0.
656 * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
657 * which comes from Trcd, and also note that:
658 * add_lat + caslat must be >= 4
659 */
660 add_lat = 0;
661 if (spd.mem_type == SPD_MEMTYPE_DDR2
662 && (odt_wr_cfg || odt_rd_cfg)
663 && (caslat < 4)) {
664 add_lat = 4 - caslat;
John Traillfcd18ba2006-08-08 11:32:43 +0100665 if (add_lat >= trcd_clk) {
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500666 add_lat = trcd_clk - 1;
667 }
668 }
669
670 /*
671 * Write Data Delay
672 * Historically 0x2 == 4/8 clock delay.
673 * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
674 */
675 wr_data_delay = 3;
676
677 /*
678 * Write Latency
679 * Read to Precharge
680 * Minimum CKE Pulse Width.
681 * Four Activate Window
682 */
683 if (spd.mem_type == SPD_MEMTYPE_DDR) {
684 /*
685 * This is a lie. It should really be 1, but if it is
686 * set to 1, bits overlap into the old controller's
687 * otherwise unused ACSM field. If we leave it 0, then
688 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
689 */
690 wr_lat = 0;
691
692 trtp_clk = 2; /* By the book. */
693 cke_min_clk = 1; /* By the book. */
694 four_act = 1; /* By the book. */
695
696 } else {
697 wr_lat = caslat - 1;
698
699 /* Convert SPD value from quarter nanos to picos. */
700 trtp_clk = picos_to_clk(spd.trtp * 250);
701
702 cke_min_clk = 3; /* By the book. */
703 four_act = picos_to_clk(37500); /* By the book. 1k pages? */
704 }
705
Jon Loeliger598e1722006-05-19 13:26:34 -0500706 ddr->timing_cfg_2 = (0
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500707 | ((add_lat & 0x7) << 28) /* ADD_LAT */
Jon Loeliger465b9d82006-04-27 10:15:16 -0500708 | ((cpo & 0x1f) << 23) /* CPO */
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500709 | ((wr_lat & 0x7) << 19) /* WR_LAT */
710 | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
711 | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
712 | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
713 | ((four_act & 0x1f) << 0) /* FOUR_ACT */
714 );
715
Jon Loeliger598e1722006-05-19 13:26:34 -0500716 debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500717
718
719 /*
720 * Determine the Mode Register Set.
721 *
722 * This is nominally part specific, but it appears to be
723 * consistent for all DDR I devices, and for all DDR II devices.
724 *
725 * caslat must be programmed
726 * burst length is always 4
727 * burst type is sequential
728 *
729 * For DDR I:
730 * operating mode is "normal"
731 *
732 * For DDR II:
733 * other stuff
734 */
735
736 mode_caslat = 0;
737
738 /*
739 * Table lookup from DDR I or II Device Operation Specs.
740 */
741 if (spd.mem_type == SPD_MEMTYPE_DDR) {
742 if (1 <= caslat && caslat <= 4) {
743 unsigned char mode_caslat_table[4] = {
744 0x5, /* 1.5 clocks */
745 0x2, /* 2.0 clocks */
746 0x6, /* 2.5 clocks */
747 0x3 /* 3.0 clocks */
748 };
749 mode_caslat = mode_caslat_table[caslat - 1];
750 } else {
751 puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
752 "2.5 and 3.0 clocks are supported.\n");
753 return 0;
754 }
755
756 } else {
757 if (2 <= caslat && caslat <= 5) {
758 mode_caslat = caslat;
759 } else {
760 puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
761 "4.0 and 5.0 clocks are supported.\n");
762 return 0;
763 }
764 }
765
766 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500767 * Encoded Burst Length of 4.
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500768 */
769 burst_len = 2; /* Fiat. */
770
771 if (spd.mem_type == SPD_MEMTYPE_DDR) {
772 twr_auto_clk = 0; /* Historical */
773 } else {
774 /*
775 * Determine tCK max in picos. Grab tWR and convert to picos.
776 * Auto-precharge write recovery is:
777 * WR = roundup(tWR_ns/tCKmax_ns).
778 *
779 * Ponder: Is twr_auto_clk different than twr_clk?
780 */
781 tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
782 twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
783 }
784
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500785 /*
786 * Mode Reg in bits 16 ~ 31,
787 * Extended Mode Reg 1 in bits 0 ~ 15.
788 */
789 mode_odt_enable = 0x0; /* Default disabled */
790 if (odt_wr_cfg || odt_rd_cfg) {
791 /*
792 * Bits 6 and 2 in Extended MRS(1)
793 * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
794 * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
795 */
796 mode_odt_enable = 0x40; /* 150 Ohm */
797 }
798
Jon Loeliger598e1722006-05-19 13:26:34 -0500799 ddr->sdram_mode_1 =
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500800 (0
801 | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
802 | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
803 | (twr_auto_clk << 9) /* Write Recovery Autopre */
804 | (mode_caslat << 4) /* caslat */
805 | (burst_len << 0) /* Burst length */
806 );
807
Jon Loeliger598e1722006-05-19 13:26:34 -0500808 debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode_1);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500809
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500810 /*
811 * Clear EMRS2 and EMRS3.
812 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500813 ddr->sdram_mode_2 = 0;
814 debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500815
Jon Loeligerae13a642006-10-10 17:19:03 -0500816 /*
817 * Determine Refresh Rate.
818 */
819 refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500820
821 /*
Jon Loeligerae13a642006-10-10 17:19:03 -0500822 * Set BSTOPRE to 0x100 for page mode
823 * If auto-charge is used, set BSTOPRE = 0
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500824 */
Jon Loeligerae13a642006-10-10 17:19:03 -0500825 ddr->sdram_interval =
826 (0
827 | (refresh_clk & 0x3fff) << 16
828 | 0x100
829 );
830 debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500831
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500832
833 /*
834 * Is this an ECC DDR chip?
835 * But don't mess with it if the DDR controller will init mem.
836 */
837#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
838 if (spd.config == 0x02) {
Jon Loeliger598e1722006-05-19 13:26:34 -0500839 ddr->err_disable = 0x0000000d;
840 ddr->err_sbe = 0x00ff0000;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500841 }
Jon Loeliger598e1722006-05-19 13:26:34 -0500842 debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
843 debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500844#endif
845
Jon Loeligere9750152006-08-29 09:48:49 -0500846 asm volatile("sync;isync");
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500847 udelay(500);
848
849 /*
850 * SDRAM Cfg 2
851 */
852
853 /*
854 * When ODT is enabled, Chap 9 suggests asserting ODT to
855 * internal IOs only during reads.
856 */
857 odt_cfg = 0;
858 if (odt_rd_cfg | odt_wr_cfg) {
859 odt_cfg = 0x2; /* ODT to IOs during reads */
860 }
861
862 /*
863 * Try to use differential DQS with DDR II.
864 */
865 if (spd.mem_type == SPD_MEMTYPE_DDR) {
866 dqs_cfg = 0; /* No Differential DQS for DDR I */
867 } else {
868 dqs_cfg = 0x1; /* Differential DQS for DDR II */
869 }
870
871#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
872 /*
873 * Use the DDR controller to auto initialize memory.
874 */
875 d_init = 1;
Jon Loeliger598e1722006-05-19 13:26:34 -0500876 ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
877 debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500878#else
879 /*
880 * Memory will be initialized via DMA, or not at all.
881 */
Jon Loeliger465b9d82006-04-27 10:15:16 -0500882 d_init = 0;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500883#endif
884
Jon Loeliger598e1722006-05-19 13:26:34 -0500885 ddr->sdram_cfg_2 = (0
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500886 | (dqs_cfg << 26) /* Differential DQS */
887 | (odt_cfg << 21) /* ODT */
888 | (d_init << 4) /* D_INIT auto init DDR */
889 );
890
Jon Loeliger598e1722006-05-19 13:26:34 -0500891 debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500892
893
894#ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
Jon Loeligerae13a642006-10-10 17:19:03 -0500895 /*
896 * Setup the clock control.
897 * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
898 * SDRAM_CLK_CNTL[5-7] = Clock Adjust
899 * 0110 3/4 cycle late
900 * 0111 7/8 cycle late
901 */
902 if (spd.mem_type == SPD_MEMTYPE_DDR)
903 clk_adjust = 0x6;
904 else
905 clk_adjust = 0x7;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500906
Jon Loeligerae13a642006-10-10 17:19:03 -0500907 ddr->sdram_clk_cntl = (0
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500908 | 0x80000000
909 | (clk_adjust << 23)
910 );
Jon Loeligerae13a642006-10-10 17:19:03 -0500911 debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500912#endif
913
914 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500915 * Figure out memory size in Megabytes.
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500916 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500917 debug("# ranks = %d, rank_density = 0x%08lx\n", n_ranks, rank_density);
918 memsize = n_ranks * rank_density / 0x100000;
919 return memsize;
920}
921
922
923unsigned int enable_ddr(unsigned int ddr_num)
924{
925 volatile immap_t *immap = (immap_t *)CFG_IMMR;
926 spd_eeprom_t spd1,spd2;
927 volatile ccsr_ddr_t *ddr;
928 unsigned sdram_cfg_1;
929 unsigned char sdram_type, mem_type, config, mod_attr;
930 unsigned char d_init;
931 unsigned int no_dimm1=0, no_dimm2=0;
932
933 /* Set up pointer to enable the current ddr controller */
934 if (ddr_num == 1)
935 ddr = &immap->im_ddr1;
936 else
937 ddr = &immap->im_ddr2;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500938
939 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500940 * Read both dimm slots and decide whether
941 * or not to enable this controller.
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500942 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500943 memset((void *)&spd1,0,sizeof(spd1));
944 memset((void *)&spd2,0,sizeof(spd2));
945
946 if (ddr_num == 1) {
947 CFG_READ_SPD(SPD_EEPROM_ADDRESS1,
948 0, 1, (uchar *) &spd1, sizeof(spd1));
949 CFG_READ_SPD(SPD_EEPROM_ADDRESS2,
950 0, 1, (uchar *) &spd2, sizeof(spd2));
951 } else {
952 CFG_READ_SPD(SPD_EEPROM_ADDRESS3,
953 0, 1, (uchar *) &spd1, sizeof(spd1));
954 CFG_READ_SPD(SPD_EEPROM_ADDRESS4,
955 0, 1, (uchar *) &spd2, sizeof(spd2));
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500956 }
957
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500958 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500959 * Check for supported memory module types.
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500960 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500961 if (spd1.mem_type != SPD_MEMTYPE_DDR
962 && spd1.mem_type != SPD_MEMTYPE_DDR2) {
963 no_dimm1 = 1;
964 } else {
965 debug("\nFound memory of type 0x%02lx ",spd1.mem_type );
966 if (spd1.mem_type == SPD_MEMTYPE_DDR)
967 debug("DDR I\n");
968 else
969 debug("DDR II\n");
970 }
971
972 if (spd2.mem_type != SPD_MEMTYPE_DDR &&
973 spd2.mem_type != SPD_MEMTYPE_DDR2) {
974 no_dimm2 = 1;
975 } else {
976 debug("\nFound memory of type 0x%02lx ",spd2.mem_type );
977 if (spd2.mem_type == SPD_MEMTYPE_DDR)
978 debug("DDR I\n");
979 else
980 debug("DDR II\n");
981 }
982
983#ifdef CONFIG_DDR_INTERLEAVE
984 if (no_dimm1) {
985 printf("For interleaved operation memory modules need to be present in CS0 DIMM slots of both DDR controllers!\n");
986 return 0;
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500987 }
988#endif
989
990 /*
Jon Loeliger598e1722006-05-19 13:26:34 -0500991 * Memory is not present in DIMM1 and DIMM2 - so do not enable DDRn
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500992 */
Jon Loeliger598e1722006-05-19 13:26:34 -0500993 if (no_dimm1 && no_dimm2) {
994 printf("No memory modules found for DDR controller %d!!\n", ddr_num);
995 return 0;
996 } else {
997 mem_type = no_dimm2 ? spd1.mem_type : spd2.mem_type;
998
999 /*
1000 * Figure out the settings for the sdram_cfg register.
1001 * Build up the entire register in 'sdram_cfg' before
1002 * writing since the write into the register will
1003 * actually enable the memory controller; all settings
1004 * must be done before enabling.
1005 *
1006 * sdram_cfg[0] = 1 (ddr sdram logic enable)
1007 * sdram_cfg[1] = 1 (self-refresh-enable)
1008 * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
1009 * 010 DDR 1 SDRAM
1010 * 011 DDR 2 SDRAM
1011 */
1012 sdram_type = (mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
1013 sdram_cfg_1 = (0
1014 | (1 << 31) /* Enable */
1015 | (1 << 30) /* Self refresh */
1016 | (sdram_type << 24) /* SDRAM type */
1017 );
1018
1019 /*
1020 * sdram_cfg[3] = RD_EN - registered DIMM enable
1021 * A value of 0x26 indicates micron registered
1022 * DIMMS (micron.com)
1023 */
1024 mod_attr = no_dimm2 ? spd1.mod_attr : spd2.mod_attr;
1025 if (mem_type == SPD_MEMTYPE_DDR && mod_attr == 0x26) {
1026 sdram_cfg_1 |= 0x10000000; /* RD_EN */
1027 }
1028
1029#if defined(CONFIG_DDR_ECC)
1030
1031 config = no_dimm2 ? spd1.config : spd2.config;
1032
1033 /*
1034 * If the user wanted ECC (enabled via sdram_cfg[2])
1035 */
1036 if (config == 0x02) {
Haiying Wang22277752006-05-30 08:51:19 -05001037 ddr->err_disable = 0x00000000;
Jon Loeligere9750152006-08-29 09:48:49 -05001038 asm volatile("sync;isync;");
Haiying Wang22277752006-05-30 08:51:19 -05001039 ddr->err_sbe = 0x00ff0000;
1040 ddr->err_int_en = 0x0000000d;
Jon Loeliger598e1722006-05-19 13:26:34 -05001041 sdram_cfg_1 |= 0x20000000; /* ECC_EN */
1042 }
1043#endif
1044
1045 /*
Haiying Wang22277752006-05-30 08:51:19 -05001046 * Set 1T or 2T timing based on 1 or 2 modules
Jon Loeliger598e1722006-05-19 13:26:34 -05001047 */
1048 {
Haiying Wang22277752006-05-30 08:51:19 -05001049 if (!(no_dimm1 || no_dimm2)) {
Jon Loeliger598e1722006-05-19 13:26:34 -05001050 /*
Haiying Wang22277752006-05-30 08:51:19 -05001051 * 2T timing,because both DIMMS are present.
Jon Loeliger598e1722006-05-19 13:26:34 -05001052 * Enable 2T timing by setting sdram_cfg[16].
1053 */
1054 sdram_cfg_1 |= 0x8000; /* 2T_EN */
Jon Loeliger598e1722006-05-19 13:26:34 -05001055 }
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001056 }
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001057
Jon Loeliger598e1722006-05-19 13:26:34 -05001058 /*
1059 * 200 painful micro-seconds must elapse between
1060 * the DDR clock setup and the DDR config enable.
1061 */
1062 udelay(200);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001063
Jon Loeliger598e1722006-05-19 13:26:34 -05001064 /*
1065 * Go!
1066 */
1067 ddr->sdram_cfg_1 = sdram_cfg_1;
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001068
Jon Loeliger598e1722006-05-19 13:26:34 -05001069 asm volatile("sync;isync");
1070 udelay(500);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001071
Jon Loeliger598e1722006-05-19 13:26:34 -05001072 debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg_1);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001073
1074
1075#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
Jon Loeliger598e1722006-05-19 13:26:34 -05001076 d_init = 1;
1077 debug("DDR: memory initializing\n");
1078
1079 /*
1080 * Poll until memory is initialized.
1081 * 512 Meg at 400 might hit this 200 times or so.
1082 */
1083 while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
1084 udelay(1000);
1085 }
1086 debug("DDR: memory initialized\n\n");
1087#endif
1088
1089 debug("Enabled DDR Controller %d\n", ddr_num);
1090 return 1;
1091 }
1092}
1093
1094
1095long int
1096spd_sdram(void)
1097{
1098 int memsize_ddr1_dimm1 = 0;
1099 int memsize_ddr1_dimm2 = 0;
1100 int memsize_ddr2_dimm1 = 0;
1101 int memsize_ddr2_dimm2 = 0;
1102 int memsize_total = 0;
1103 int memsize_ddr1 = 0;
1104 int memsize_ddr2 = 0;
1105 unsigned int ddr1_enabled = 0;
1106 unsigned int ddr2_enabled = 0;
1107 unsigned int law_size_ddr1;
1108 unsigned int law_size_ddr2;
1109 volatile immap_t *immap = (immap_t *)CFG_IMMR;
1110 volatile ccsr_ddr_t *ddr1 = &immap->im_ddr1;
1111 volatile ccsr_ddr_t *ddr2 = &immap->im_ddr2;
1112 volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
1113
1114#ifdef CONFIG_DDR_INTERLEAVE
1115 unsigned int law_size_interleaved;
1116
1117 memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
1118 1, 1,
1119 (unsigned int)memsize_total * 1024*1024);
1120 memsize_total += memsize_ddr1_dimm1;
1121
1122 memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
1123 2, 1,
1124 (unsigned int)memsize_total * 1024*1024);
1125 memsize_total += memsize_ddr2_dimm1;
1126
1127 if (memsize_ddr1_dimm1 != memsize_ddr2_dimm1) {
1128 if (memsize_ddr1_dimm1 < memsize_ddr2_dimm1)
1129 memsize_total -= memsize_ddr1_dimm1;
1130 else
1131 memsize_total -= memsize_ddr2_dimm1;
1132 debug("Total memory available for interleaving 0x%08lx\n",
1133 memsize_total * 1024 * 1024);
1134 debug("Adjusting CS0_BNDS to account for unequal DIMM sizes in interleaved memory\n");
1135 ddr1->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
1136 ddr2->cs0_bnds = ((memsize_total * 1024 * 1024) - 1) >> 24;
1137 debug("DDR1: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
1138 debug("DDR2: cs0_bnds = 0x%08x\n", ddr2->cs0_bnds);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001139 }
Jon Loeliger598e1722006-05-19 13:26:34 -05001140
1141 ddr1_enabled = enable_ddr(1);
1142 ddr2_enabled = enable_ddr(2);
1143
1144 /*
1145 * Both controllers need to be enabled for interleaving.
1146 */
1147 if (ddr1_enabled && ddr2_enabled) {
1148 law_size_interleaved = 19 + __ilog2(memsize_total);
1149
1150 /*
1151 * Set up LAWBAR for DDR 1 space.
1152 */
1153 mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
1154 mcm->lawar1 = (LAWAR_EN
1155 | LAWAR_TRGT_IF_DDR_INTERLEAVED
1156 | (LAWAR_SIZE & law_size_interleaved));
1157 debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
1158 debug("DDR: LAWAR1=0x%08x\n", mcm->lawar1);
1159 debug("Interleaved memory size is 0x%08lx\n", memsize_total);
1160
1161#ifdef CONFIG_DDR_INTERLEAVE
1162#if (CFG_PAGE_INTERLEAVING == 1)
1163 printf("Page ");
1164#elif (CFG_BANK_INTERLEAVING == 1)
1165 printf("Bank ");
1166#elif (CFG_SUPER_BANK_INTERLEAVING == 1)
1167 printf("Super-bank ");
1168#else
1169 printf("Cache-line ");
1170#endif
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001171#endif
Jon Loeliger598e1722006-05-19 13:26:34 -05001172 printf("Interleaved");
1173 return memsize_total * 1024 * 1024;
1174 } else {
1175 printf("Interleaved memory not enabled - check CS0 DIMM slots for both controllers.\n");
1176 return 0;
1177 }
1178
1179#else
1180 /*
1181 * Call spd_sdram() routine to init ddr1 - pass I2c address,
1182 * controller number, dimm number, and starting address.
1183 */
1184 memsize_ddr1_dimm1 = spd_init(SPD_EEPROM_ADDRESS1,
1185 1, 1,
1186 (unsigned int)memsize_total * 1024*1024);
1187 memsize_total += memsize_ddr1_dimm1;
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001188
Jon Loeliger598e1722006-05-19 13:26:34 -05001189 memsize_ddr1_dimm2 = spd_init(SPD_EEPROM_ADDRESS2,
1190 1, 2,
1191 (unsigned int)memsize_total * 1024*1024);
1192 memsize_total += memsize_ddr1_dimm2;
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001193
1194 /*
Jon Loeliger598e1722006-05-19 13:26:34 -05001195 * Enable the DDR controller - pass ddr controller number.
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001196 */
Jon Loeliger598e1722006-05-19 13:26:34 -05001197 ddr1_enabled = enable_ddr(1);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001198
Jon Loeliger598e1722006-05-19 13:26:34 -05001199 /* Keep track of memory to be addressed by DDR1 */
1200 memsize_ddr1 = memsize_ddr1_dimm1 + memsize_ddr1_dimm2;
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001201
Jon Loeliger598e1722006-05-19 13:26:34 -05001202 /*
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001203 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
1204 */
Jon Loeliger598e1722006-05-19 13:26:34 -05001205 if (ddr1_enabled) {
1206 law_size_ddr1 = 19 + __ilog2(memsize_ddr1);
1207
1208 /*
1209 * Set up LAWBAR for DDR 1 space.
1210 */
1211 mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
1212 mcm->lawar1 = (LAWAR_EN
1213 | LAWAR_TRGT_IF_DDR1
1214 | (LAWAR_SIZE & law_size_ddr1));
1215 debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
1216 debug("DDR: LAWAR1=0x%08x\n", mcm->lawar1);
1217 }
1218
1219#if (CONFIG_NUM_DDR_CONTROLLERS > 1)
1220 memsize_ddr2_dimm1 = spd_init(SPD_EEPROM_ADDRESS3,
1221 2, 1,
1222 (unsigned int)memsize_total * 1024*1024);
1223 memsize_total += memsize_ddr2_dimm1;
1224
1225 memsize_ddr2_dimm2 = spd_init(SPD_EEPROM_ADDRESS4,
1226 2, 2,
1227 (unsigned int)memsize_total * 1024*1024);
1228 memsize_total += memsize_ddr2_dimm2;
1229
1230 ddr2_enabled = enable_ddr(2);
1231
1232 /* Keep track of memory to be addressed by DDR2 */
1233 memsize_ddr2 = memsize_ddr2_dimm1 + memsize_ddr2_dimm2;
1234
1235 if (ddr2_enabled) {
1236 law_size_ddr2 = 19 + __ilog2(memsize_ddr2);
1237
1238 /*
1239 * Set up LAWBAR for DDR 2 space.
1240 */
1241 if (ddr1_enabled)
1242 mcm->lawbar8 = (((memsize_ddr1 * 1024 * 1024) >> 12)
1243 & 0xfffff);
1244 else
1245 mcm->lawbar8 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
1246
1247 mcm->lawar8 = (LAWAR_EN
1248 | LAWAR_TRGT_IF_DDR2
1249 | (LAWAR_SIZE & law_size_ddr2));
1250 debug("\nDDR: LAWBAR8=0x%08x\n", mcm->lawbar8);
1251 debug("DDR: LAWAR8=0x%08x\n", mcm->lawar8);
1252 }
1253#endif /* CONFIG_NUM_DDR_CONTROLLERS > 1 */
1254
1255 debug("\nMemory sizes are DDR1 = 0x%08lx, DDR2 = 0x%08lx\n",
1256 memsize_ddr1, memsize_ddr2);
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001257
1258 /*
Jon Loeliger598e1722006-05-19 13:26:34 -05001259 * If neither DDR controller is enabled return 0.
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001260 */
Jon Loeliger598e1722006-05-19 13:26:34 -05001261 if (!ddr1_enabled && !ddr2_enabled)
1262 return 0;
Jon Loeligerae13a642006-10-10 17:19:03 -05001263
1264 printf("Non-interleaved");
1265 return memsize_total * 1024 * 1024;
Jon Loeliger465b9d82006-04-27 10:15:16 -05001266
Jon Loeliger598e1722006-05-19 13:26:34 -05001267#endif /* CONFIG_DDR_INTERLEAVE */
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001268}
1269
Jon Loeliger598e1722006-05-19 13:26:34 -05001270
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001271#endif /* CONFIG_SPD_EEPROM */
1272
1273
1274#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
1275
1276/*
1277 * Initialize all of memory for ECC, then enable errors.
1278 */
1279
1280void
1281ddr_enable_ecc(unsigned int dram_size)
1282{
1283 uint *p = 0;
1284 uint i = 0;
1285 volatile immap_t *immap = (immap_t *)CFG_IMMR;
1286 volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
1287
1288 dma_init();
1289
1290 for (*p = 0; p < (uint *)(8 * 1024); p++) {
1291 if (((unsigned int)p & 0x1f) == 0) {
1292 ppcDcbz((unsigned long) p);
1293 }
1294 *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
1295 if (((unsigned int)p & 0x1c) == 0x1c) {
1296 ppcDcbf((unsigned long) p);
1297 }
1298 }
1299
Jon Loeligerae13a642006-10-10 17:19:03 -05001300 dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
1301 dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
1302 dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
1303 dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
1304 dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
1305 dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
1306 dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
1307 dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
1308 dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
1309 dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001310
1311 for (i = 1; i < dram_size / 0x800000; i++) {
1312 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
1313 }
1314
1315 /*
1316 * Enable errors for ECC.
1317 */
1318 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1319 ddr1->err_disable = 0x00000000;
Jon Loeligere9750152006-08-29 09:48:49 -05001320 asm volatile("sync;isync");
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001321 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1322}
1323
1324#endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */