2 * Copyright 2004 Freescale Semiconductor.
3 * (C) Copyright 2003 Motorola Inc.
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <asm/processor.h>
32 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
33 extern void dma_init(void);
34 extern uint dma_check(void);
35 extern int dma_xfer(void *dest, uint count, void *src);
38 #ifdef CONFIG_SPD_EEPROM
41 #define CFG_READ_SPD i2c_read
45 * Convert picoseconds into clock cycles (rounding up if needed).
49 picos_to_clk(int picos)
53 clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
54 if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
63 * Calculate the Density of each Physical Rank.
64 * Returned size is in bytes.
66 * Study these table from Byte 31 of JEDEC SPD Spec.
80 * Reorder Table to be linear by stripping the bottom
81 * 2 or 5 bits off and shifting them up to the top.
85 compute_banksize(unsigned int mem_type, unsigned char row_dens)
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);
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);
103 * Convert a two-nibble BCD value into a cycle time.
104 * While the spec calls for nano-seconds, picos are returned.
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.)
114 convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
117 * Table look up the lower nibble, allow DDR I & II.
119 unsigned int tenths_ps[16] = {
131 330, /* FIXME: Is 333 better/valid? */
132 660, /* FIXME: Is 667 better/valid? */
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];
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;
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;
174 unsigned char burst_len;
175 unsigned int mode_caslat;
176 unsigned char sdram_type;
177 unsigned char d_init;
180 unsigned int law_size;
181 volatile ccsr_local_mcm_t *mcm = &immap->im_local_mcm;
184 * Read SPD information.
187 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
190 * Check for supported memory module types.
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",
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.
205 * Also note that the 8548 controller can support:
208 * 8 <= ncol <= 11 (still, for DDR)
209 * 6 <= ncol <= 9 (for FCRAM)
211 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
212 printf("DDR: Unsupported number of Row Addr lines: %d.\n",
216 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
217 printf("DDR: Unsupported number of Column Addr lines: %d.\n",
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.
227 if (spd.mem_type == SPD_MEMTYPE_DDR) {
230 n_ranks = (spd.nrows & 0x7) + 1;
233 debug("DDR: number of ranks = %d\n", n_ranks);
236 printf("DDR: Only 2 chip selects are supported: %d\n",
242 * Adjust DDR II IO voltage biasing. It just makes it work.
244 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
246 | 0x80000000 /* Enable */
247 | 0x10000000 /* VSEL to 1.8V */
252 * Determine the size of each Rank in bytes.
254 rank_density = compute_banksize(spd.mem_type, spd.row_dens);
258 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
260 ddr1->cs0_bnds = (rank_density >> 24) - 1;
263 * ODT configuration recommendation from DDR Controller Chapter.
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 */
271 ddr1->cs0_config = ( 1 << 31
274 | (spd.nrow_addr - 12) << 8
275 | (spd.ncol_addr - 8) );
277 debug("DDR: cs0_bnds = 0x%08x\n", ddr1->cs0_bnds);
278 debug("DDR: cs0_config = 0x%08x\n", ddr1->cs0_config);
282 * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
284 ddr1->cs1_bnds = ( (rank_density >> 8)
285 | ((rank_density >> (24 - 1)) - 1) );
286 ddr1->cs1_config = ( 1<<31
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);
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:
301 * CAS Lat DDR I DDR II Ctrl
302 * Clocks SPD Bit SPD Bit Value
303 * ------- ------- ------- -----
314 caslat = __ilog2(spd.cas_lat);
315 if ((spd.mem_type == SPD_MEMTYPE_DDR)
317 printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
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",
326 debug("DDR: caslat SPD bit is %d\n", caslat);
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.
333 tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
334 debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
337 * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
339 max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
340 debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
344 * Adjust the CAS Latency to allow for bus speeds that
345 * are slower than the DDR module.
347 busfreq = get_bus_freq(0) / 1000000; /* MHz */
349 effective_data_rate = max_data_rate;
351 /* DDR rate out-of-range */
352 puts("DDR: platform frequency is not fit for DDR rate\n");
355 } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
357 * busfreq 90~230 range, treated as DDR 200.
359 effective_data_rate = 200;
360 if (spd.clk_cycle3 == 0xa0) /* 10 ns */
362 else if (spd.clk_cycle2 == 0xa0)
365 } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
367 * busfreq 230~280 range, treated as DDR 266.
369 effective_data_rate = 266;
370 if (spd.clk_cycle3 == 0x75) /* 7.5 ns */
372 else if (spd.clk_cycle2 == 0x75)
375 } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
377 * busfreq 280~350 range, treated as DDR 333.
379 effective_data_rate = 333;
380 if (spd.clk_cycle3 == 0x60) /* 6.0 ns */
382 else if (spd.clk_cycle2 == 0x60)
385 } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
387 * busfreq 350~460 range, treated as DDR 400.
389 effective_data_rate = 400;
390 if (spd.clk_cycle3 == 0x50) /* 5.0 ns */
392 else if (spd.clk_cycle2 == 0x50)
395 } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
397 * busfreq 460~560 range, treated as DDR 533.
399 effective_data_rate = 533;
400 if (spd.clk_cycle3 == 0x3D) /* 3.75 ns */
402 else if (spd.clk_cycle2 == 0x3D)
405 } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
407 * busfreq 560~700 range, treated as DDR 667.
409 effective_data_rate = 667;
410 if (spd.clk_cycle3 == 0x30) /* 3.0 ns */
412 else if (spd.clk_cycle2 == 0x30)
415 } else if (700 <= busfreq) {
417 * DDR rate out-of-range
419 printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
420 busfreq, max_data_rate);
426 * Convert caslat clocks to DDR controller value.
427 * Force caslat_ctrl to be DDR Controller field-sized.
429 if (spd.mem_type == SPD_MEMTYPE_DDR) {
430 caslat_ctrl = (caslat + 1) & 0x07;
432 caslat_ctrl = (2 * caslat - 1) & 0x0f;
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);
441 * Avoid writing for DDR I. The new PQ38 DDR controller
442 * dreams up non-zero default values to be backwards compatible.
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? */
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 */
456 debug("DDR: timing_cfg_0 = 0x%08x\n", ddr1->timing_cfg_0);
463 * Some Timing Config 1 values now.
464 * Sneak Extended Refresh Recovery in here too.
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.
473 if (spd.mem_type == SPD_MEMTYPE_DDR) {
474 twr_clk = 3; /* Clocks */
475 twtr_clk = 1; /* Clocks */
477 twr_clk = picos_to_clk(spd.twr * 250);
478 twtr_clk = picos_to_clk(spd.twtr * 250);
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.
486 if (spd.mem_type == SPD_MEMTYPE_DDR) {
487 trfc = spd.trfc * 1000; /* up to ps */
489 unsigned int byte40_table_ps[8] = {
500 trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
501 + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
503 trfc_clk = picos_to_clk(trfc);
506 * Trcd, Byte 29, from quarter nanos to ps and clocks.
508 trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
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.
517 trfc_low = (trfc_clk - 8) & 0xf;
518 trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
521 * Sneak in some Extended Refresh Recovery.
523 ddr1->ext_refrec = (trfc_high << 16);
524 debug("DDR: ext_refrec = 0x%08x\n", ddr1->ext_refrec);
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 */
538 debug("DDR: timing_cfg_1 = 0x%08x\n", ddr1->timing_cfg_1);
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
554 if (spd.mem_type == SPD_MEMTYPE_DDR2
555 && (odt_wr_cfg || odt_rd_cfg)
557 add_lat = 4 - caslat;
558 if (add_lat > trcd_clk) {
559 add_lat = trcd_clk - 1;
565 * Historically 0x2 == 4/8 clock delay.
566 * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
573 * Minimum CKE Pulse Width.
574 * Four Activate Window
576 if (spd.mem_type == SPD_MEMTYPE_DDR) {
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.
585 trtp_clk = 2; /* By the book. */
586 cke_min_clk = 1; /* By the book. */
587 four_act = 1; /* By the book. */
592 /* Convert SPD value from quarter nanos to picos. */
593 trtp_clk = picos_to_clk(spd.trtp * 250);
595 cke_min_clk = 3; /* By the book. */
596 four_act = picos_to_clk(37500); /* By the book. 1k pages? */
600 * Empirically set ~MCAS-to-preamble override for DDR 2.
601 * Your milage will vary.
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 */
610 /* Pure speculation */
615 ddr1->timing_cfg_2 = (0
616 | ((add_lat & 0x7) << 28) /* ADD_LAT */
617 | ((cpo & 0x1f) << 23) /* CPO */
618 | ((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 */
625 debug("DDR: timing_cfg_2 = 0x%08x\n", ddr1->timing_cfg_2);
629 * Determine the Mode Register Set.
631 * This is nominally part specific, but it appears to be
632 * consistent for all DDR I devices, and for all DDR II devices.
634 * caslat must be programmed
635 * burst length is always 4
636 * burst type is sequential
639 * operating mode is "normal"
648 * Table lookup from DDR I or II Device Operation Specs.
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 */
658 mode_caslat = mode_caslat_table[caslat - 1];
660 puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
661 "2.5 and 3.0 clocks are supported.\n");
666 if (2 <= caslat && caslat <= 5) {
667 mode_caslat = caslat;
669 puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
670 "4.0 and 5.0 clocks are supported.\n");
676 * Encoded Burst Lenght of 4.
678 burst_len = 2; /* Fiat. */
680 if (spd.mem_type == SPD_MEMTYPE_DDR) {
681 twr_auto_clk = 0; /* Historical */
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).
688 * Ponder: Is twr_auto_clk different than twr_clk?
690 tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
691 twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
696 * Mode Reg in bits 16 ~ 31,
697 * Extended Mode Reg 1 in bits 0 ~ 15.
699 mode_odt_enable = 0x0; /* Default disabled */
700 if (odt_wr_cfg || odt_rd_cfg) {
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.
706 mode_odt_enable = 0x40; /* 150 Ohm */
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 */
718 debug("DDR: sdram_mode = 0x%08x\n", ddr1->sdram_mode_1);
722 * Clear EMRS2 and EMRS3.
724 ddr1->sdram_mode_2 = 0;
725 debug("DDR: sdram_mode_2 = 0x%08x\n", ddr1->sdram_mode_2);
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.
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 */
746 refresh_clk = picos_to_clk(refresh_time_ns[spd.refresh & 0x7]);
749 * Set BSTOPRE to 0x100 for page mode
750 * If auto-charge is used, set BSTOPRE = 0
752 ddr1->sdram_interval =
754 | (refresh_clk & 0x3fff) << 16
757 debug("DDR: sdram_interval = 0x%08x\n", ddr1->sdram_interval);
761 * Is this an ECC DDR chip?
762 * But don't mess with it if the DDR controller will init mem.
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;
769 debug("DDR: err_disable = 0x%08x\n", ddr1->err_disable);
770 debug("DDR: err_sbe = 0x%08x\n", ddr1->err_sbe);
781 * When ODT is enabled, Chap 9 suggests asserting ODT to
782 * internal IOs only during reads.
785 if (odt_rd_cfg | odt_wr_cfg) {
786 odt_cfg = 0x2; /* ODT to IOs during reads */
790 * Try to use differential DQS with DDR II.
792 if (spd.mem_type == SPD_MEMTYPE_DDR) {
793 dqs_cfg = 0; /* No Differential DQS for DDR I */
795 dqs_cfg = 0x1; /* Differential DQS for DDR II */
798 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
800 * Use the DDR controller to auto initialize memory.
803 ddr1->sdram_data_init = CONFIG_MEM_INIT_VALUE;
804 debug("DDR: ddr_data_init = 0x%08x\n", ddr1->sdram_data_init);
807 * Memory will be initialized via DMA, or not at all.
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 */
818 debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr1->sdram_cfg_2);
821 #ifdef MPC86xx_DDR_SDRAM_CLK_CNTL
823 unsigned char clk_adjust;
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
832 if (spd.mem_type == SPD_MEMTYPE_DDR) {
838 ddr1->sdram_clk_cntl = (0
842 debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr1->sdram_clk_cntl);
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.
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)
858 sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
860 | (1 << 31) /* Enable */
861 | (1 << 30) /* Self refresh */
862 | (sdram_type << 24) /* SDRAM type */
866 * sdram_cfg[3] = RD_EN - registered DIMM enable
867 * A value of 0x26 indicates micron registered DIMMS (micron.com)
869 if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
870 sdram_cfg_1 |= 0x10000000; /* RD_EN */
873 #if defined(CONFIG_DDR_ECC)
875 * If the user wanted ECC (enabled via sdram_cfg[2])
877 if (spd.config == 0x02) {
878 sdram_cfg_1 |= 0x20000000; /* ECC_EN */
883 * REV1 uses 1T timing.
884 * REV2 may use 1T or 2T as configured by the user.
887 uint pvr = get_pvr();
889 if (pvr != PVR_85xx_REV1) {
890 #if defined(CONFIG_DDR_2T_TIMING)
892 * Enable 2T timing by setting sdram_cfg[16].
894 sdram_cfg_1 |= 0x8000; /* 2T_EN */
900 * 200 painful micro-seconds must elapse between
901 * the DDR clock setup and the DDR config enable.
908 ddr1->sdram_cfg_1 = sdram_cfg_1;
913 debug("DDR: sdram_cfg = 0x%08x\n", ddr1->sdram_cfg_1);
916 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
917 debug("DDR: memory initializing\n");
919 * Poll until memory is initialized.
920 * 512 Meg at 400 might hit this 200 times or so.
922 while ((ddr1->sdram_cfg_2 & (d_init << 4)) != 0) {
925 debug("DDR: memory initialized\n");
930 * Figure out memory size in Megabytes.
932 memsize = n_ranks * rank_density / 0x100000;
936 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
938 law_size = 19 + __ilog2(memsize);
941 * Set up LAWBAR for all of DDR.
943 mcm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
944 mcm->lawar1 = (LAWAR_EN
946 | (LAWAR_SIZE & law_size));
947 debug("DDR: LAWBAR1=0x%08x\n", mcm->lawbar1);
948 debug("DDR: LARAR1=0x%08x\n", mcm->lawar1);
951 return memsize * 1024 * 1024;
954 #endif /* CONFIG_SPD_EEPROM */
957 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
960 * Initialize all of memory for ECC, then enable errors.
964 ddr_enable_ecc(unsigned int dram_size)
968 volatile immap_t *immap = (immap_t *)CFG_IMMR;
969 volatile ccsr_ddr_t *ddr1= &immap->im_ddr1;
973 for (*p = 0; p < (uint *)(8 * 1024); p++) {
974 if (((unsigned int)p & 0x1f) == 0) {
975 ppcDcbz((unsigned long) p);
977 *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
978 if (((unsigned int)p & 0x1c) == 0x1c) {
979 ppcDcbf((unsigned long) p);
984 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
986 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
988 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
990 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
992 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
994 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
996 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
998 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
1000 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
1002 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
1004 for (i = 1; i < dram_size / 0x800000; i++) {
1005 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
1009 * Enable errors for ECC.
1011 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1012 ddr1->err_disable = 0x00000000;
1013 asm("sync;isync;msync");
1014 debug("DMA DDR: err_disable = 0x%08x\n", ddr1->err_disable);
1017 #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */