blob: 9ce31d7c6f31767861d0e1ded83247bf807b0599 [file] [log] [blame]
Jon Loeligerdebb7352006-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/*
45 * Convert picoseconds into clock cycles (rounding up if needed).
46 */
47
48int
49picos_to_clk(int picos)
50{
51 int clks;
52
53 clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
54 if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
55 clks++;
56 }
57
58 return clks;
59}
60
61
62/*
63 * Calculate the Density of each Physical Rank.
64 * Returned size is in bytes.
65 *
66 * Study these table from Byte 31 of JEDEC SPD Spec.
67 *
68 * DDR I DDR II
69 * Bit Size Size
70 * --- ----- ------
71 * 7 high 512MB 512MB
72 * 6 256MB 256MB
73 * 5 128MB 128MB
74 * 4 64MB 16GB
75 * 3 32MB 8GB
76 * 2 16MB 4GB
77 * 1 2GB 2GB
78 * 0 low 1GB 1GB
79 *
80 * Reorder Table to be linear by stripping the bottom
81 * 2 or 5 bits off and shifting them up to the top.
82 */
83
84unsigned int
85compute_banksize(unsigned int mem_type, unsigned char row_dens)
86{
87 unsigned int bsize;
88
89 if (mem_type == SPD_MEMTYPE_DDR) {
90 /* Bottom 2 bits up to the top. */
91 bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
92 debug("DDR: DDR I rank density = 0x%08x\n", bsize);
93 } else {
94 /* Bottom 5 bits up to the top. */
95 bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
96 debug("DDR: DDR II rank density = 0x%08x\n", bsize);
97 }
98 return bsize;
99}
100
101
102/*
103 * Convert a two-nibble BCD value into a cycle time.
104 * While the spec calls for nano-seconds, picos are returned.
105 *
106 * This implements the tables for bytes 9, 23 and 25 for both
107 * DDR I and II. No allowance for distinguishing the invalid
108 * fields absent for DDR I yet present in DDR II is made.
109 * (That is, cycle times of .25, .33, .66 and .75 ns are
110 * allowed for both DDR II and I.)
111 */
112
113unsigned int
114convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
115{
116 /*
117 * Table look up the lower nibble, allow DDR I & II.
118 */
119 unsigned int tenths_ps[16] = {
120 0,
121 100,
122 200,
123 300,
124 400,
125 500,
126 600,
127 700,
128 800,
129 900,
130 250,
131 330, /* FIXME: Is 333 better/valid? */
132 660, /* FIXME: Is 667 better/valid? */
133 750,
134 0, /* undefined */
135 0 /* undefined */
136 };
137
138 unsigned int whole_ns = (spd_val & 0xF0) >> 4;
139 unsigned int tenth_ns = spd_val & 0x0F;
140 unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
141
142 return ps;
143}
144
145
146long int
147spd_sdram(void)
148{
149 volatile immap_t *immap = (immap_t *)CFG_IMMR;
150 volatile ccsr_ddr_t *ddr1 = &immap->im_ddr1;
151 volatile ccsr_gur_t *gur = &immap->im_gur;
152 spd_eeprom_t spd;
153 unsigned int n_ranks;
154 unsigned int rank_density;
155 unsigned int odt_rd_cfg, odt_wr_cfg;
156 unsigned int odt_cfg, mode_odt_enable;
157 unsigned int dqs_cfg;
158 unsigned char twr_clk, twtr_clk, twr_auto_clk;
159 unsigned int tCKmin_ps, tCKmax_ps;
160 unsigned int max_data_rate, effective_data_rate;
161 unsigned int busfreq;
162 unsigned sdram_cfg_1;
163 unsigned int memsize;
164 unsigned char caslat, caslat_ctrl;
165 unsigned int trfc, trfc_clk, trfc_low, trfc_high;
166 unsigned int trcd_clk;
167 unsigned int trtp_clk;
168 unsigned char cke_min_clk;
169 unsigned char add_lat;
170 unsigned char wr_lat;
171 unsigned char wr_data_delay;
172 unsigned char four_act;
173 unsigned char cpo;
174 unsigned char burst_len;
175 unsigned int mode_caslat;
176 unsigned char sdram_type;
177 unsigned char d_init;
178
179
180 unsigned int law_size;
181 volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
Jon Loeliger5c9efb32006-04-27 10:15:16 -0500182
Jon Loeligerdebb7352006-04-26 17:58:56 -0500183 /*
184 * Read SPD information.
185 */
186
187 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
188
189 /*
190 * Check for supported memory module types.
191 */
192 if (spd.mem_type != SPD_MEMTYPE_DDR &&
193 spd.mem_type != SPD_MEMTYPE_DDR2) {
194 printf("Unable to locate DDR I or DDR II module.\n"
195 " Fundamental memory type is 0x%0x\n",
196 spd.mem_type);
197 return 0;
198 }
199
200 /*
201 * These test gloss over DDR I and II differences in interpretation
202 * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
203 * are not supported on DDR I; and not encoded on DDR II.
204 *
205 * Also note that the 8548 controller can support:
206 * 12 <= nrow <= 16
207 * and
208 * 8 <= ncol <= 11 (still, for DDR)
209 * 6 <= ncol <= 9 (for FCRAM)
210 */
211 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
212 printf("DDR: Unsupported number of Row Addr lines: %d.\n",
213 spd.nrow_addr);
214 return 0;
215 }
216 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
217 printf("DDR: Unsupported number of Column Addr lines: %d.\n",
218 spd.ncol_addr);
219 return 0;
220 }
221
222 /*
223 * Determine the number of physical banks controlled by
224 * different Chip Select signals. This is not quite the
225 * same as the number of DIMM modules on the board. Feh.
226 */
227 if (spd.mem_type == SPD_MEMTYPE_DDR) {
228 n_ranks = spd.nrows;
229 } else {
230 n_ranks = (spd.nrows & 0x7) + 1;
231 }
232
233 debug("DDR: number of ranks = %d\n", n_ranks);
234
235 if (n_ranks > 2) {
236 printf("DDR: Only 2 chip selects are supported: %d\n",
237 n_ranks);
238 return 0;
239 }
240
241 /*
242 * Adjust DDR II IO voltage biasing. It just makes it work.
243 */
244 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
245 gur->ddrioovcr = (0
246 | 0x80000000 /* Enable */
247 | 0x10000000 /* VSEL to 1.8V */
248 );
249 }
250
251 /*
252 * Determine the size of each Rank in bytes.
253 */
254 rank_density = compute_banksize(spd.mem_type, spd.row_dens);
255
256
257 /*
258 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
259 */
260 ddr1->cs0_bnds = (rank_density >> 24) - 1;
261
262 /*
263 * ODT configuration recommendation from DDR Controller Chapter.
264 */
265 odt_rd_cfg = 0; /* Never assert ODT */
266 odt_wr_cfg = 0; /* Never assert ODT */
267 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
268 odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
269 }
270
271 ddr1->cs0_config = ( 1 << 31
272 | (odt_rd_cfg << 20)
273 | (odt_wr_cfg << 16)
274 | (spd.nrow_addr - 12) << 8
275 | (spd.ncol_addr - 8) );
276 debug("\n");
277 debug("DDR: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
278 debug("DDR: cs0_config = 0x%08x\n", ddr1->cs0_config);
279
280 if (n_ranks == 2) {
281 /*
282 * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
283 */
284 ddr1->cs1_bnds = ( (rank_density >> 8)
285 | ((rank_density >> (24 - 1)) - 1) );
286 ddr1->cs1_config = ( 1<<31
287 | (odt_rd_cfg << 20)
288 | (odt_wr_cfg << 16)
289 | (spd.nrow_addr - 12) << 8
290 | (spd.ncol_addr - 8) );
291 debug("DDR: cs1_bnds = 0x%08x\n", ddr1->cs1_bnds);
292 debug("DDR: cs1_config = 0x%08x\n", ddr1->cs1_config);
293 }
294
295
296 /*
297 * Find the largest CAS by locating the highest 1 bit
298 * in the spd.cas_lat field. Translate it to a DDR
299 * controller field value:
300 *
301 * CAS Lat DDR I DDR II Ctrl
302 * Clocks SPD Bit SPD Bit Value
303 * ------- ------- ------- -----
304 * 1.0 0 0001
305 * 1.5 1 0010
306 * 2.0 2 2 0011
307 * 2.5 3 0100
308 * 3.0 4 3 0101
309 * 3.5 5 0110
310 * 4.0 4 0111
311 * 4.5 1000
312 * 5.0 5 1001
313 */
314 caslat = __ilog2(spd.cas_lat);
315 if ((spd.mem_type == SPD_MEMTYPE_DDR)
316 && (caslat > 5)) {
317 printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
318 return 0;
319
320 } else if (spd.mem_type == SPD_MEMTYPE_DDR2
321 && (caslat < 2 || caslat > 5)) {
322 printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
323 spd.cas_lat);
324 return 0;
325 }
326 debug("DDR: caslat SPD bit is %d\n", caslat);
327
328 /*
329 * Calculate the Maximum Data Rate based on the Minimum Cycle time.
330 * The SPD clk_cycle field (tCKmin) is measured in tenths of
331 * nanoseconds and represented as BCD.
332 */
333 tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
334 debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
335
336 /*
337 * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
338 */
339 max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
340 debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
341
342
343 /*
344 * Adjust the CAS Latency to allow for bus speeds that
345 * are slower than the DDR module.
346 */
347 busfreq = get_bus_freq(0) / 1000000; /* MHz */
348
349 effective_data_rate = max_data_rate;
350 if (busfreq < 90) {
351 /* DDR rate out-of-range */
352 puts("DDR: platform frequency is not fit for DDR rate\n");
353 return 0;
354
355 } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
356 /*
357 * busfreq 90~230 range, treated as DDR 200.
358 */
359 effective_data_rate = 200;
360 if (spd.clk_cycle3 == 0xa0) /* 10 ns */
361 caslat -= 2;
362 else if (spd.clk_cycle2 == 0xa0)
363 caslat--;
364
365 } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
366 /*
367 * busfreq 230~280 range, treated as DDR 266.
368 */
369 effective_data_rate = 266;
370 if (spd.clk_cycle3 == 0x75) /* 7.5 ns */
371 caslat -= 2;
372 else if (spd.clk_cycle2 == 0x75)
373 caslat--;
374
375 } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
376 /*
377 * busfreq 280~350 range, treated as DDR 333.
378 */
379 effective_data_rate = 333;
380 if (spd.clk_cycle3 == 0x60) /* 6.0 ns */
381 caslat -= 2;
382 else if (spd.clk_cycle2 == 0x60)
383 caslat--;
384
385 } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
386 /*
387 * busfreq 350~460 range, treated as DDR 400.
388 */
389 effective_data_rate = 400;
390 if (spd.clk_cycle3 == 0x50) /* 5.0 ns */
391 caslat -= 2;
392 else if (spd.clk_cycle2 == 0x50)
393 caslat--;
394
395 } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
396 /*
397 * busfreq 460~560 range, treated as DDR 533.
398 */
399 effective_data_rate = 533;
400 if (spd.clk_cycle3 == 0x3D) /* 3.75 ns */
401 caslat -= 2;
402 else if (spd.clk_cycle2 == 0x3D)
403 caslat--;
404
405 } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
406 /*
407 * busfreq 560~700 range, treated as DDR 667.
408 */
409 effective_data_rate = 667;
410 if (spd.clk_cycle3 == 0x30) /* 3.0 ns */
411 caslat -= 2;
412 else if (spd.clk_cycle2 == 0x30)
413 caslat--;
414
415 } else if (700 <= busfreq) {
416 /*
417 * DDR rate out-of-range
418 */
419 printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
420 busfreq, max_data_rate);
421 return 0;
422 }
423
424
425 /*
426 * Convert caslat clocks to DDR controller value.
427 * Force caslat_ctrl to be DDR Controller field-sized.
428 */
429 if (spd.mem_type == SPD_MEMTYPE_DDR) {
430 caslat_ctrl = (caslat + 1) & 0x07;
431 } else {
432 caslat_ctrl = (2 * caslat - 1) & 0x0f;
433 }
434
435 debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
436 debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
437 caslat, caslat_ctrl);
438
439 /*
440 * Timing Config 0.
441 * Avoid writing for DDR I. The new PQ38 DDR controller
442 * dreams up non-zero default values to be backwards compatible.
443 */
444 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
445 unsigned char taxpd_clk = 8; /* By the book. */
446 unsigned char tmrd_clk = 2; /* By the book. */
447 unsigned char act_pd_exit = 2; /* Empirical? */
448 unsigned char pre_pd_exit = 6; /* Empirical? */
449
450 ddr1->timing_cfg_0 = (0
451 | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
452 | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
453 | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
454 | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
455 );
456 debug("DDR: timing_cfg_0 = 0x%08x\n", ddr1->timing_cfg_0);
457
458 } else {
459 }
460
461
462 /*
463 * Some Timing Config 1 values now.
464 * Sneak Extended Refresh Recovery in here too.
465 */
466
467 /*
468 * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
469 * use conservative value.
470 * For DDR II, they are bytes 36 and 37, in quarter nanos.
471 */
472
473 if (spd.mem_type == SPD_MEMTYPE_DDR) {
474 twr_clk = 3; /* Clocks */
475 twtr_clk = 1; /* Clocks */
476 } else {
477 twr_clk = picos_to_clk(spd.twr * 250);
478 twtr_clk = picos_to_clk(spd.twtr * 250);
479 }
480
481 /*
482 * Calculate Trfc, in picos.
483 * DDR I: Byte 42 straight up in ns.
484 * DDR II: Byte 40 and 42 swizzled some, in ns.
485 */
486 if (spd.mem_type == SPD_MEMTYPE_DDR) {
487 trfc = spd.trfc * 1000; /* up to ps */
488 } else {
489 unsigned int byte40_table_ps[8] = {
490 0,
491 250,
492 330,
493 500,
494 660,
495 750,
496 0,
497 0
498 };
499
500 trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
501 + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
502 }
503 trfc_clk = picos_to_clk(trfc);
504
505 /*
506 * Trcd, Byte 29, from quarter nanos to ps and clocks.
507 */
508 trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
509
510 /*
511 * Convert trfc_clk to DDR controller fields. DDR I should
512 * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
513 * 8548 controller has an extended REFREC field of three bits.
514 * The controller automatically adds 8 clocks to this value,
515 * so preadjust it down 8 first before splitting it up.
516 */
517 trfc_low = (trfc_clk - 8) & 0xf;
518 trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
519
520 /*
521 * Sneak in some Extended Refresh Recovery.
522 */
523 ddr1->ext_refrec = (trfc_high << 16);
524 debug("DDR: ext_refrec = 0x%08x\n", ddr1->ext_refrec);
525
526 ddr1->timing_cfg_1 =
527 (0
528 | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
529 | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
530 | (trcd_clk << 20) /* ACTTORW */
531 | (caslat_ctrl << 16) /* CASLAT */
532 | (trfc_low << 12) /* REFEC */
533 | ((twr_clk & 0x07) << 8) /* WRRREC */
534 | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
535 | ((twtr_clk & 0x07) << 0) /* WRTORD */
536 );
537
538 debug("DDR: timing_cfg_1 = 0x%08x\n", ddr1->timing_cfg_1);
539
540
541 /*
542 * Timing_Config_2
543 * Was: 0x00000800;
544 */
545
546 /*
547 * Additive Latency
548 * For DDR I, 0.
549 * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
550 * which comes from Trcd, and also note that:
551 * add_lat + caslat must be >= 4
552 */
553 add_lat = 0;
554 if (spd.mem_type == SPD_MEMTYPE_DDR2
555 && (odt_wr_cfg || odt_rd_cfg)
556 && (caslat < 4)) {
557 add_lat = 4 - caslat;
558 if (add_lat > trcd_clk) {
559 add_lat = trcd_clk - 1;
560 }
561 }
562
563 /*
564 * Write Data Delay
565 * Historically 0x2 == 4/8 clock delay.
566 * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
567 */
568 wr_data_delay = 3;
569
570 /*
571 * Write Latency
572 * Read to Precharge
573 * Minimum CKE Pulse Width.
574 * Four Activate Window
575 */
576 if (spd.mem_type == SPD_MEMTYPE_DDR) {
577 /*
578 * This is a lie. It should really be 1, but if it is
579 * set to 1, bits overlap into the old controller's
580 * otherwise unused ACSM field. If we leave it 0, then
581 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
582 */
583 wr_lat = 0;
584
585 trtp_clk = 2; /* By the book. */
586 cke_min_clk = 1; /* By the book. */
587 four_act = 1; /* By the book. */
588
589 } else {
590 wr_lat = caslat - 1;
591
592 /* Convert SPD value from quarter nanos to picos. */
593 trtp_clk = picos_to_clk(spd.trtp * 250);
594
595 cke_min_clk = 3; /* By the book. */
596 four_act = picos_to_clk(37500); /* By the book. 1k pages? */
597 }
598
599 /*
600 * Empirically set ~MCAS-to-preamble override for DDR 2.
601 * Your milage will vary.
602 */
603 cpo = 0;
604 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
605 if (effective_data_rate == 266 || effective_data_rate == 333) {
606 cpo = 0x7; /* READ_LAT + 5/4 */
607 } else if (effective_data_rate == 400) {
608 cpo = 0x9; /* READ_LAT + 7/4 */
609 } else {
610 /* Pure speculation */
611 cpo = 0xb;
612 }
613 }
614
615 ddr1->timing_cfg_2 = (0
616 | ((add_lat & 0x7) << 28) /* ADD_LAT */
Jon Loeliger5c9efb32006-04-27 10:15:16 -0500617 | ((cpo & 0x1f) << 23) /* CPO */
Jon Loeligerdebb7352006-04-26 17:58:56 -0500618 | ((wr_lat & 0x7) << 19) /* WR_LAT */
619 | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
620 | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
621 | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
622 | ((four_act & 0x1f) << 0) /* FOUR_ACT */
623 );
624
625 debug("DDR: timing_cfg_2 = 0x%08x\n", ddr1->timing_cfg_2);
626
627
628 /*
629 * Determine the Mode Register Set.
630 *
631 * This is nominally part specific, but it appears to be
632 * consistent for all DDR I devices, and for all DDR II devices.
633 *
634 * caslat must be programmed
635 * burst length is always 4
636 * burst type is sequential
637 *
638 * For DDR I:
639 * operating mode is "normal"
640 *
641 * For DDR II:
642 * other stuff
643 */
644
645 mode_caslat = 0;
646
647 /*
648 * Table lookup from DDR I or II Device Operation Specs.
649 */
650 if (spd.mem_type == SPD_MEMTYPE_DDR) {
651 if (1 <= caslat && caslat <= 4) {
652 unsigned char mode_caslat_table[4] = {
653 0x5, /* 1.5 clocks */
654 0x2, /* 2.0 clocks */
655 0x6, /* 2.5 clocks */
656 0x3 /* 3.0 clocks */
657 };
658 mode_caslat = mode_caslat_table[caslat - 1];
659 } else {
660 puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
661 "2.5 and 3.0 clocks are supported.\n");
662 return 0;
663 }
664
665 } else {
666 if (2 <= caslat && caslat <= 5) {
667 mode_caslat = caslat;
668 } else {
669 puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
670 "4.0 and 5.0 clocks are supported.\n");
671 return 0;
672 }
673 }
674
675 /*
676 * Encoded Burst Lenght of 4.
677 */
678 burst_len = 2; /* Fiat. */
679
680 if (spd.mem_type == SPD_MEMTYPE_DDR) {
681 twr_auto_clk = 0; /* Historical */
682 } else {
683 /*
684 * Determine tCK max in picos. Grab tWR and convert to picos.
685 * Auto-precharge write recovery is:
686 * WR = roundup(tWR_ns/tCKmax_ns).
687 *
688 * Ponder: Is twr_auto_clk different than twr_clk?
689 */
690 tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
691 twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
692 }
693
694
695 /*
696 * Mode Reg in bits 16 ~ 31,
697 * Extended Mode Reg 1 in bits 0 ~ 15.
698 */
699 mode_odt_enable = 0x0; /* Default disabled */
700 if (odt_wr_cfg || odt_rd_cfg) {
701 /*
702 * Bits 6 and 2 in Extended MRS(1)
703 * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
704 * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
705 */
706 mode_odt_enable = 0x40; /* 150 Ohm */
707 }
708
709 ddr1->sdram_mode_1 =
710 (0
711 | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
712 | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
713 | (twr_auto_clk << 9) /* Write Recovery Autopre */
714 | (mode_caslat << 4) /* caslat */
715 | (burst_len << 0) /* Burst length */
716 );
717
718 debug("DDR: sdram_mode = 0x%08x\n", ddr1->sdram_mode_1);
719
720
721 /*
722 * Clear EMRS2 and EMRS3.
723 */
724 ddr1->sdram_mode_2 = 0;
725 debug("DDR: sdram_mode_2 = 0x%08x\n", ddr1->sdram_mode_2);
726
727
728 /*
729 * Determine Refresh Rate. Ignore self refresh bit on DDR I.
730 * Table from SPD Spec, Byte 12, converted to picoseconds and
731 * filled in with "default" normal values.
732 */
733 {
734 unsigned int refresh_clk;
735 unsigned int refresh_time_ns[8] = {
736 15625000, /* 0 Normal 1.00x */
737 3900000, /* 1 Reduced .25x */
738 7800000, /* 2 Extended .50x */
739 31300000, /* 3 Extended 2.00x */
740 62500000, /* 4 Extended 4.00x */
741 125000000, /* 5 Extended 8.00x */
742 15625000, /* 6 Normal 1.00x filler */
743 15625000, /* 7 Normal 1.00x filler */
744 };
745
746 refresh_clk = picos_to_clk(refresh_time_ns[spd.refresh & 0x7]);
747
748 /*
749 * Set BSTOPRE to 0x100 for page mode
750 * If auto-charge is used, set BSTOPRE = 0
751 */
752 ddr1->sdram_interval =
753 (0
754 | (refresh_clk & 0x3fff) << 16
755 | 0x100
756 );
757 debug("DDR: sdram_interval = 0x%08x\n", ddr1->sdram_interval);
758 }
759
760 /*
761 * Is this an ECC DDR chip?
762 * But don't mess with it if the DDR controller will init mem.
763 */
764#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
765 if (spd.config == 0x02) {
766 ddr1->err_disable = 0x0000000d;
767 ddr1->err_sbe = 0x00ff0000;
768 }
769 debug("DDR: err_disable = 0x%08x\n", ddr1->err_disable);
770 debug("DDR: err_sbe = 0x%08x\n", ddr1->err_sbe);
771#endif
772
773 asm("sync;isync");
774 udelay(500);
775
776 /*
777 * SDRAM Cfg 2
778 */
779
780 /*
781 * When ODT is enabled, Chap 9 suggests asserting ODT to
782 * internal IOs only during reads.
783 */
784 odt_cfg = 0;
785 if (odt_rd_cfg | odt_wr_cfg) {
786 odt_cfg = 0x2; /* ODT to IOs during reads */
787 }
788
789 /*
790 * Try to use differential DQS with DDR II.
791 */
792 if (spd.mem_type == SPD_MEMTYPE_DDR) {
793 dqs_cfg = 0; /* No Differential DQS for DDR I */
794 } else {
795 dqs_cfg = 0x1; /* Differential DQS for DDR II */
796 }
797
798#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
799 /*
800 * Use the DDR controller to auto initialize memory.
801 */
802 d_init = 1;
803 ddr1->sdram_data_init = CONFIG_MEM_INIT_VALUE;
804 debug("DDR: ddr_data_init = 0x%08x\n", ddr1->sdram_data_init);
805#else
806 /*
807 * Memory will be initialized via DMA, or not at all.
808 */
Jon Loeliger5c9efb32006-04-27 10:15:16 -0500809 d_init = 0;
Jon Loeligerdebb7352006-04-26 17:58:56 -0500810#endif
811
812 ddr1->sdram_cfg_2 = (0
813 | (dqs_cfg << 26) /* Differential DQS */
814 | (odt_cfg << 21) /* ODT */
815 | (d_init << 4) /* D_INIT auto init DDR */
816 );
817
818 debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr1->sdram_cfg_2);
819
820
821#ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
822 {
823 unsigned char clk_adjust;
824
825 /*
826 * Setup the clock control.
827 * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
828 * SDRAM_CLK_CNTL[5-7] = Clock Adjust
829 * 0110 3/4 cycle late
830 * 0111 7/8 cycle late
831 */
832 if (spd.mem_type == SPD_MEMTYPE_DDR) {
833 clk_adjust = 0x6;
834 } else {
835 clk_adjust = 0x7;
836 }
837
838 ddr1->sdram_clk_cntl = (0
839 | 0x80000000
840 | (clk_adjust << 23)
841 );
842 debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr1->sdram_clk_cntl);
843 }
844#endif
845
846 /*
847 * Figure out the settings for the sdram_cfg register.
848 * Build up the entire register in 'sdram_cfg' before writing
849 * since the write into the register will actually enable the
850 * memory controller; all settings must be done before enabling.
851 *
852 * sdram_cfg[0] = 1 (ddr sdram logic enable)
853 * sdram_cfg[1] = 1 (self-refresh-enable)
854 * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
855 * 010 DDR 1 SDRAM
856 * 011 DDR 2 SDRAM
857 */
858 sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
859 sdram_cfg_1 = (0
860 | (1 << 31) /* Enable */
861 | (1 << 30) /* Self refresh */
862 | (sdram_type << 24) /* SDRAM type */
863 );
864
865 /*
866 * sdram_cfg[3] = RD_EN - registered DIMM enable
867 * A value of 0x26 indicates micron registered DIMMS (micron.com)
868 */
869 if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
870 sdram_cfg_1 |= 0x10000000; /* RD_EN */
871 }
872
873#if defined(CONFIG_DDR_ECC)
874 /*
875 * If the user wanted ECC (enabled via sdram_cfg[2])
876 */
877 if (spd.config == 0x02) {
878 sdram_cfg_1 |= 0x20000000; /* ECC_EN */
879 }
880#endif
881
882 /*
883 * REV1 uses 1T timing.
884 * REV2 may use 1T or 2T as configured by the user.
885 */
886 {
887 uint pvr = get_pvr();
888
889 if (pvr != PVR_85xx_REV1) {
890#if defined(CONFIG_DDR_2T_TIMING)
891 /*
892 * Enable 2T timing by setting sdram_cfg[16].
893 */
894 sdram_cfg_1 |= 0x8000; /* 2T_EN */
895#endif
896 }
897 }
898
899 /*
900 * 200 painful micro-seconds must elapse between
901 * the DDR clock setup and the DDR config enable.
902 */
903 udelay(200);
904
905 /*
906 * Go!
907 */
908 ddr1->sdram_cfg_1 = sdram_cfg_1;
909
910 asm("sync;isync");
911 udelay(500);
912
913 debug("DDR: sdram_cfg = 0x%08x\n", ddr1->sdram_cfg_1);
914
915
916#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
917 debug("DDR: memory initializing\n");
918 /*
919 * Poll until memory is initialized.
920 * 512 Meg at 400 might hit this 200 times or so.
921 */
922 while ((ddr1->sdram_cfg_2 & (d_init << 4)) != 0) {
923 udelay(1000);
924 }
925 debug("DDR: memory initialized\n");
926#endif
927
928
929 /*
930 * Figure out memory size in Megabytes.
931 */
932 memsize = n_ranks * rank_density / 0x100000;
933
934
935 /*
936 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
937 */
938 law_size = 19 + __ilog2(memsize);
939
940 /*
941 * Set up LAWBAR for all of DDR.
942 */
943 mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
944 mcm->lawar1 = (LAWAR_EN
945 | LAWAR_TRGT_IF_DDR
946 | (LAWAR_SIZE & law_size));
947 debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
948 debug("DDR: LARAR1=0x%08x\n", mcm->lawar1);
Jon Loeliger5c9efb32006-04-27 10:15:16 -0500949
Jon Loeligerdebb7352006-04-26 17:58:56 -0500950 return memsize * 1024 * 1024;
951}
952
953#endif /* CONFIG_SPD_EEPROM */
954
955
956#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
957
958/*
959 * Initialize all of memory for ECC, then enable errors.
960 */
961
962void
963ddr_enable_ecc(unsigned int dram_size)
964{
965 uint *p = 0;
966 uint i = 0;
967 volatile immap_t *immap = (immap_t *)CFG_IMMR;
968 volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
969
970 dma_init();
971
972 for (*p = 0; p < (uint *)(8 * 1024); p++) {
973 if (((unsigned int)p & 0x1f) == 0) {
974 ppcDcbz((unsigned long) p);
975 }
976 *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
977 if (((unsigned int)p & 0x1c) == 0x1c) {
978 ppcDcbf((unsigned long) p);
979 }
980 }
981
982 /* 8K */
983 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
984 /* 16K */
985 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
986 /* 32K */
987 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
988 /* 64K */
989 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
990 /* 128k */
991 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
992 /* 256k */
993 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
994 /* 512k */
995 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
996 /* 1M */
997 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
998 /* 2M */
999 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
1000 /* 4M */
1001 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
1002
1003 for (i = 1; i < dram_size / 0x800000; i++) {
1004 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
1005 }
1006
1007 /*
1008 * Enable errors for ECC.
1009 */
1010 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1011 ddr1->err_disable = 0x00000000;
1012 asm("sync;isync;msync");
1013 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1014}
1015
1016#endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */