2 * Copyright 2004 Freescale Semiconductor.
3 * (C) Copyright 2003 Motorola Inc.
4 * Xianghua Xiao (X.Xiao@motorola.com)
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
44 static unsigned int setup_laws_and_tlbs(unsigned int memsize);
48 * Convert picoseconds into clock cycles (rounding up if needed).
52 picos_to_clk(int picos)
56 clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
57 if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
66 * Calculate the Density of each Physical Rank.
67 * Returned size is in bytes.
69 * Study these table from Byte 31 of JEDEC SPD Spec.
83 * Reorder Table to be linear by stripping the bottom
84 * 2 or 5 bits off and shifting them up to the top.
88 compute_banksize(unsigned int mem_type, unsigned char row_dens)
92 if (mem_type == SPD_MEMTYPE_DDR) {
93 /* Bottom 2 bits up to the top. */
94 bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
95 debug("DDR: DDR I rank density = 0x%08x\n", bsize);
97 /* Bottom 5 bits up to the top. */
98 bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
99 debug("DDR: DDR II rank density = 0x%08x\n", bsize);
106 * Convert a two-nibble BCD value into a cycle time.
107 * While the spec calls for nano-seconds, picos are returned.
109 * This implements the tables for bytes 9, 23 and 25 for both
110 * DDR I and II. No allowance for distinguishing the invalid
111 * fields absent for DDR I yet present in DDR II is made.
112 * (That is, cycle times of .25, .33, .66 and .75 ns are
113 * allowed for both DDR II and I.)
117 convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
120 * Table look up the lower nibble, allow DDR I & II.
122 unsigned int tenths_ps[16] = {
141 unsigned int whole_ns = (spd_val & 0xF0) >> 4;
142 unsigned int tenth_ns = spd_val & 0x0F;
143 unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
150 * Determine Refresh Rate. Ignore self refresh bit on DDR I.
151 * Table from SPD Spec, Byte 12, converted to picoseconds and
152 * filled in with "default" normal values.
154 unsigned int determine_refresh_rate(unsigned int spd_refresh)
156 unsigned int refresh_time_ns[8] = {
157 15625000, /* 0 Normal 1.00x */
158 3900000, /* 1 Reduced .25x */
159 7800000, /* 2 Extended .50x */
160 31300000, /* 3 Extended 2.00x */
161 62500000, /* 4 Extended 4.00x */
162 125000000, /* 5 Extended 8.00x */
163 15625000, /* 6 Normal 1.00x filler */
164 15625000, /* 7 Normal 1.00x filler */
167 return picos_to_clk(refresh_time_ns[spd_refresh & 0x7]);
174 volatile immap_t *immap = (immap_t *)CFG_IMMR;
175 volatile ccsr_ddr_t *ddr = &immap->im_ddr;
176 volatile ccsr_gur_t *gur = &immap->im_gur;
178 unsigned int n_ranks;
179 unsigned int rank_density;
180 unsigned int odt_rd_cfg, odt_wr_cfg;
181 unsigned int odt_cfg, mode_odt_enable;
182 unsigned int refresh_clk;
183 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
184 unsigned char clk_adjust;
186 unsigned int dqs_cfg;
187 unsigned char twr_clk, twtr_clk, twr_auto_clk;
188 unsigned int tCKmin_ps, tCKmax_ps;
189 unsigned int max_data_rate, effective_data_rate;
190 unsigned int busfreq;
192 unsigned int memsize;
193 unsigned char caslat, caslat_ctrl;
194 unsigned int trfc, trfc_clk, trfc_low, trfc_high;
195 unsigned int trcd_clk;
196 unsigned int trtp_clk;
197 unsigned char cke_min_clk;
198 unsigned char add_lat;
199 unsigned char wr_lat;
200 unsigned char wr_data_delay;
201 unsigned char four_act;
203 unsigned char burst_len;
204 unsigned int mode_caslat;
205 unsigned char sdram_type;
206 unsigned char d_init;
209 * Read SPD information.
211 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
214 * Check for supported memory module types.
216 if (spd.mem_type != SPD_MEMTYPE_DDR &&
217 spd.mem_type != SPD_MEMTYPE_DDR2) {
218 printf("Unable to locate DDR I or DDR II module.\n"
219 " Fundamental memory type is 0x%0x\n",
225 * These test gloss over DDR I and II differences in interpretation
226 * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
227 * are not supported on DDR I; and not encoded on DDR II.
229 * Also note that the 8548 controller can support:
232 * 8 <= ncol <= 11 (still, for DDR)
233 * 6 <= ncol <= 9 (for FCRAM)
235 if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
236 printf("DDR: Unsupported number of Row Addr lines: %d.\n",
240 if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
241 printf("DDR: Unsupported number of Column Addr lines: %d.\n",
247 * Determine the number of physical banks controlled by
248 * different Chip Select signals. This is not quite the
249 * same as the number of DIMM modules on the board. Feh.
251 if (spd.mem_type == SPD_MEMTYPE_DDR) {
254 n_ranks = (spd.nrows & 0x7) + 1;
257 debug("DDR: number of ranks = %d\n", n_ranks);
260 printf("DDR: Only 2 chip selects are supported: %d\n",
266 * Adjust DDR II IO voltage biasing.
267 * Only 8548 rev 1 needs the fix
269 if ((SVR_VER(get_svr()) == SVR_8548_E) &&
270 (SVR_MJREV(get_svr()) == 1) &&
271 (spd.mem_type == SPD_MEMTYPE_DDR2)) {
272 gur->ddrioovcr = (0x80000000 /* Enable */
273 | 0x10000000);/* VSEL to 1.8V */
277 * Determine the size of each Rank in bytes.
279 rank_density = compute_banksize(spd.mem_type, spd.row_dens);
283 * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
285 ddr->cs0_bnds = (rank_density >> 24) - 1;
288 * ODT configuration recommendation from DDR Controller Chapter.
290 odt_rd_cfg = 0; /* Never assert ODT */
291 odt_wr_cfg = 0; /* Never assert ODT */
292 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
293 odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
295 /* FIXME: How to determine the number of dimm modules? */
296 if (n_dimm_modules == 2) {
297 odt_rd_cfg = 1; /* Assert ODT on reads to CS0 */
302 ddr->cs0_config = ( 1 << 31
305 | (spd.nrow_addr - 12) << 8
306 | (spd.ncol_addr - 8) );
308 debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
309 debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
313 * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
315 ddr->cs1_bnds = ( (rank_density >> 8)
316 | ((rank_density >> (24 - 1)) - 1) );
317 ddr->cs1_config = ( 1<<31
320 | (spd.nrow_addr - 12) << 8
321 | (spd.ncol_addr - 8) );
322 debug("DDR: cs1_bnds = 0x%08x\n", ddr->cs1_bnds);
323 debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
328 * Find the largest CAS by locating the highest 1 bit
329 * in the spd.cas_lat field. Translate it to a DDR
330 * controller field value:
332 * CAS Lat DDR I DDR II Ctrl
333 * Clocks SPD Bit SPD Bit Value
334 * ------- ------- ------- -----
345 caslat = __ilog2(spd.cas_lat);
346 if ((spd.mem_type == SPD_MEMTYPE_DDR)
348 printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
351 } else if (spd.mem_type == SPD_MEMTYPE_DDR2
352 && (caslat < 2 || caslat > 5)) {
353 printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
357 debug("DDR: caslat SPD bit is %d\n", caslat);
360 * Calculate the Maximum Data Rate based on the Minimum Cycle time.
361 * The SPD clk_cycle field (tCKmin) is measured in tenths of
362 * nanoseconds and represented as BCD.
364 tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
365 debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
368 * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
370 max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
371 debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
375 * Adjust the CAS Latency to allow for bus speeds that
376 * are slower than the DDR module.
378 busfreq = get_bus_freq(0) / 1000000; /* MHz */
380 effective_data_rate = max_data_rate;
382 /* DDR rate out-of-range */
383 puts("DDR: platform frequency is not fit for DDR rate\n");
386 } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
388 * busfreq 90~230 range, treated as DDR 200.
390 effective_data_rate = 200;
391 if (spd.clk_cycle3 == 0xa0) /* 10 ns */
393 else if (spd.clk_cycle2 == 0xa0)
396 } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
398 * busfreq 230~280 range, treated as DDR 266.
400 effective_data_rate = 266;
401 if (spd.clk_cycle3 == 0x75) /* 7.5 ns */
403 else if (spd.clk_cycle2 == 0x75)
406 } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
408 * busfreq 280~350 range, treated as DDR 333.
410 effective_data_rate = 333;
411 if (spd.clk_cycle3 == 0x60) /* 6.0 ns */
413 else if (spd.clk_cycle2 == 0x60)
416 } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
418 * busfreq 350~460 range, treated as DDR 400.
420 effective_data_rate = 400;
421 if (spd.clk_cycle3 == 0x50) /* 5.0 ns */
423 else if (spd.clk_cycle2 == 0x50)
426 } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
428 * busfreq 460~560 range, treated as DDR 533.
430 effective_data_rate = 533;
431 if (spd.clk_cycle3 == 0x3D) /* 3.75 ns */
433 else if (spd.clk_cycle2 == 0x3D)
436 } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
438 * busfreq 560~700 range, treated as DDR 667.
440 effective_data_rate = 667;
441 if (spd.clk_cycle3 == 0x30) /* 3.0 ns */
443 else if (spd.clk_cycle2 == 0x30)
446 } else if (700 <= busfreq) {
448 * DDR rate out-of-range
450 printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
451 busfreq, max_data_rate);
457 * Convert caslat clocks to DDR controller value.
458 * Force caslat_ctrl to be DDR Controller field-sized.
460 if (spd.mem_type == SPD_MEMTYPE_DDR) {
461 caslat_ctrl = (caslat + 1) & 0x07;
463 caslat_ctrl = (2 * caslat - 1) & 0x0f;
466 debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
467 debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
468 caslat, caslat_ctrl);
472 * Avoid writing for DDR I. The new PQ38 DDR controller
473 * dreams up non-zero default values to be backwards compatible.
475 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
476 unsigned char taxpd_clk = 8; /* By the book. */
477 unsigned char tmrd_clk = 2; /* By the book. */
478 unsigned char act_pd_exit = 2; /* Empirical? */
479 unsigned char pre_pd_exit = 6; /* Empirical? */
481 ddr->timing_cfg_0 = (0
482 | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
483 | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
484 | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
485 | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
488 ddr->timing_cfg_0 |= 0xaa000000; /* extra cycles */
490 debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
495 * Force extra cycles with 0xaa bits.
496 * Incidentally supply the dreamt-up backwards compat value!
498 ddr->timing_cfg_0 = 0x00110105; /* backwards compat value */
499 ddr->timing_cfg_0 |= 0xaa000000; /* extra cycles */
500 debug("DDR: HACK timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
506 * Some Timing Config 1 values now.
507 * Sneak Extended Refresh Recovery in here too.
511 * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
512 * use conservative value.
513 * For DDR II, they are bytes 36 and 37, in quarter nanos.
516 if (spd.mem_type == SPD_MEMTYPE_DDR) {
517 twr_clk = 3; /* Clocks */
518 twtr_clk = 1; /* Clocks */
520 twr_clk = picos_to_clk(spd.twr * 250);
521 twtr_clk = picos_to_clk(spd.twtr * 250);
525 * Calculate Trfc, in picos.
526 * DDR I: Byte 42 straight up in ns.
527 * DDR II: Byte 40 and 42 swizzled some, in ns.
529 if (spd.mem_type == SPD_MEMTYPE_DDR) {
530 trfc = spd.trfc * 1000; /* up to ps */
532 unsigned int byte40_table_ps[8] = {
543 trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
544 + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
546 trfc_clk = picos_to_clk(trfc);
549 * Trcd, Byte 29, from quarter nanos to ps and clocks.
551 trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
554 * Convert trfc_clk to DDR controller fields. DDR I should
555 * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
556 * 8548 controller has an extended REFREC field of three bits.
557 * The controller automatically adds 8 clocks to this value,
558 * so preadjust it down 8 first before splitting it up.
560 trfc_low = (trfc_clk - 8) & 0xf;
561 trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
564 * Sneak in some Extended Refresh Recovery.
566 ddr->ext_refrec = (trfc_high << 16);
567 debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
571 | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
572 | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
573 | (trcd_clk << 20) /* ACTTORW */
574 | (caslat_ctrl << 16) /* CASLAT */
575 | (trfc_low << 12) /* REFEC */
576 | ((twr_clk & 0x07) << 8) /* WRRREC */
577 | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
578 | ((twtr_clk & 0x07) << 0) /* WRTORD */
581 debug("DDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
592 * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
593 * which comes from Trcd, and also note that:
594 * add_lat + caslat must be >= 4
597 if (spd.mem_type == SPD_MEMTYPE_DDR2
598 && (odt_wr_cfg || odt_rd_cfg)
600 add_lat = 4 - caslat;
601 if (add_lat > trcd_clk) {
602 add_lat = trcd_clk - 1;
608 * Historically 0x2 == 4/8 clock delay.
609 * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
616 * Minimum CKE Pulse Width.
617 * Four Activate Window
619 if (spd.mem_type == SPD_MEMTYPE_DDR) {
621 * This is a lie. It should really be 1, but if it is
622 * set to 1, bits overlap into the old controller's
623 * otherwise unused ACSM field. If we leave it 0, then
624 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
628 trtp_clk = 2; /* By the book. */
629 cke_min_clk = 1; /* By the book. */
630 four_act = 1; /* By the book. */
635 /* Convert SPD value from quarter nanos to picos. */
636 trtp_clk = picos_to_clk(spd.trtp * 250);
638 cke_min_clk = 3; /* By the book. */
639 four_act = picos_to_clk(37500); /* By the book. 1k pages? */
643 * Empirically set ~MCAS-to-preamble override for DDR 2.
644 * Your milage will vary.
647 if (spd.mem_type == SPD_MEMTYPE_DDR2) {
648 if (effective_data_rate == 266 || effective_data_rate == 333) {
649 cpo = 0x7; /* READ_LAT + 5/4 */
650 } else if (effective_data_rate == 400) {
651 cpo = 0x9; /* READ_LAT + 7/4 */
653 /* Pure speculation */
658 ddr->timing_cfg_2 = (0
659 | ((add_lat & 0x7) << 28) /* ADD_LAT */
660 | ((cpo & 0x1f) << 23) /* CPO */
661 | ((wr_lat & 0x7) << 19) /* WR_LAT */
662 | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
663 | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
664 | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
665 | ((four_act & 0x1f) << 0) /* FOUR_ACT */
668 debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
672 * Determine the Mode Register Set.
674 * This is nominally part specific, but it appears to be
675 * consistent for all DDR I devices, and for all DDR II devices.
677 * caslat must be programmed
678 * burst length is always 4
679 * burst type is sequential
682 * operating mode is "normal"
691 * Table lookup from DDR I or II Device Operation Specs.
693 if (spd.mem_type == SPD_MEMTYPE_DDR) {
694 if (1 <= caslat && caslat <= 4) {
695 unsigned char mode_caslat_table[4] = {
696 0x5, /* 1.5 clocks */
697 0x2, /* 2.0 clocks */
698 0x6, /* 2.5 clocks */
701 mode_caslat = mode_caslat_table[caslat - 1];
703 puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
704 "2.5 and 3.0 clocks are supported.\n");
709 if (2 <= caslat && caslat <= 5) {
710 mode_caslat = caslat;
712 puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
713 "4.0 and 5.0 clocks are supported.\n");
719 * Encoded Burst Lenght of 4.
721 burst_len = 2; /* Fiat. */
723 if (spd.mem_type == SPD_MEMTYPE_DDR) {
724 twr_auto_clk = 0; /* Historical */
727 * Determine tCK max in picos. Grab tWR and convert to picos.
728 * Auto-precharge write recovery is:
729 * WR = roundup(tWR_ns/tCKmax_ns).
731 * Ponder: Is twr_auto_clk different than twr_clk?
733 tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
734 twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
739 * Mode Reg in bits 16 ~ 31,
740 * Extended Mode Reg 1 in bits 0 ~ 15.
742 mode_odt_enable = 0x0; /* Default disabled */
743 if (odt_wr_cfg || odt_rd_cfg) {
745 * Bits 6 and 2 in Extended MRS(1)
746 * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
747 * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
749 mode_odt_enable = 0x40; /* 150 Ohm */
754 | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
755 | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
756 | (twr_auto_clk << 9) /* Write Recovery Autopre */
757 | (mode_caslat << 4) /* caslat */
758 | (burst_len << 0) /* Burst length */
761 debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode);
765 * Clear EMRS2 and EMRS3.
767 ddr->sdram_mode_2 = 0;
768 debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
771 * Determine Refresh Rate.
773 refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
776 * Set BSTOPRE to 0x100 for page mode
777 * If auto-charge is used, set BSTOPRE = 0
779 ddr->sdram_interval =
781 | (refresh_clk & 0x3fff) << 16
784 debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
787 * Is this an ECC DDR chip?
788 * But don't mess with it if the DDR controller will init mem.
790 #ifdef CONFIG_DDR_ECC
791 if (spd.config == 0x02) {
792 #ifndef CONFIG_ECC_INIT_VIA_DDRCONTROLLER
793 ddr->err_disable = 0x0000000d;
795 ddr->err_sbe = 0x00ff0000;
798 debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
799 debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
800 #endif /* CONFIG_DDR_ECC */
802 asm("sync;isync;msync");
810 * When ODT is enabled, Chap 9 suggests asserting ODT to
811 * internal IOs only during reads.
814 if (odt_rd_cfg | odt_wr_cfg) {
815 odt_cfg = 0x2; /* ODT to IOs during reads */
819 * Try to use differential DQS with DDR II.
821 if (spd.mem_type == SPD_MEMTYPE_DDR) {
822 dqs_cfg = 0; /* No Differential DQS for DDR I */
824 dqs_cfg = 0x1; /* Differential DQS for DDR II */
827 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
829 * Use the DDR controller to auto initialize memory.
832 ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
833 debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
836 * Memory will be initialized via DMA, or not at all.
841 ddr->sdram_cfg_2 = (0
842 | (dqs_cfg << 26) /* Differential DQS */
843 | (odt_cfg << 21) /* ODT */
844 | (d_init << 4) /* D_INIT auto init DDR */
847 debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
850 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
852 * Setup the clock control.
853 * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
854 * SDRAM_CLK_CNTL[5-7] = Clock Adjust
855 * 0110 3/4 cycle late
856 * 0111 7/8 cycle late
858 if (spd.mem_type == SPD_MEMTYPE_DDR)
863 ddr->sdram_clk_cntl = (0
867 debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
871 * Figure out the settings for the sdram_cfg register.
872 * Build up the entire register in 'sdram_cfg' before writing
873 * since the write into the register will actually enable the
874 * memory controller; all settings must be done before enabling.
876 * sdram_cfg[0] = 1 (ddr sdram logic enable)
877 * sdram_cfg[1] = 1 (self-refresh-enable)
878 * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
882 sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
884 | (1 << 31) /* Enable */
885 | (1 << 30) /* Self refresh */
886 | (sdram_type << 24) /* SDRAM type */
890 * sdram_cfg[3] = RD_EN - registered DIMM enable
891 * A value of 0x26 indicates micron registered DIMMS (micron.com)
893 if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
894 sdram_cfg |= 0x10000000; /* RD_EN */
897 #if defined(CONFIG_DDR_ECC)
899 * If the user wanted ECC (enabled via sdram_cfg[2])
901 if (spd.config == 0x02) {
902 sdram_cfg |= 0x20000000; /* ECC_EN */
907 * REV1 uses 1T timing.
908 * REV2 may use 1T or 2T as configured by the user.
911 uint pvr = get_pvr();
913 if (pvr != PVR_85xx_REV1) {
914 #if defined(CONFIG_DDR_2T_TIMING)
916 * Enable 2T timing by setting sdram_cfg[16].
918 sdram_cfg |= 0x8000; /* 2T_EN */
924 * 200 painful micro-seconds must elapse between
925 * the DDR clock setup and the DDR config enable.
932 ddr->sdram_cfg = sdram_cfg;
934 asm("sync;isync;msync");
937 debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg);
940 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
942 * Poll until memory is initialized.
943 * 512 Meg at 400 might hit this 200 times or so.
945 while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
952 * Figure out memory size in Megabytes.
954 memsize = n_ranks * rank_density / 0x100000;
957 * Establish Local Access Window and TLB mappings for DDR memory.
959 memsize = setup_laws_and_tlbs(memsize);
964 return memsize * 1024 * 1024;
969 * Setup Local Access Window and TLB1 mappings for the requested
970 * amount of memory. Returns the amount of memory actually mapped
971 * (usually the original request size), or 0 on error.
975 setup_laws_and_tlbs(unsigned int memsize)
977 volatile immap_t *immap = (immap_t *)CFG_IMMR;
978 volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
979 unsigned int tlb_size;
980 unsigned int law_size;
981 unsigned int ram_tlb_index;
982 unsigned int ram_tlb_address;
985 * Determine size of each TLB1 entry.
990 tlb_size = BOOKE_PAGESZ_16M;
994 tlb_size = BOOKE_PAGESZ_64M;
998 tlb_size = BOOKE_PAGESZ_256M;
1002 if (PVR_VER(get_pvr()) > PVR_VER(PVR_85xx))
1003 tlb_size = BOOKE_PAGESZ_1G;
1005 tlb_size = BOOKE_PAGESZ_256M;
1008 puts("DDR: only 16M,32M,64M,128M,256M,512M,1G and 2G are supported.\n");
1011 * The memory was not able to be mapped.
1012 * Default to a small size.
1014 tlb_size = BOOKE_PAGESZ_64M;
1020 * Configure DDR TLB1 entries.
1021 * Starting at TLB1 8, use no more than 8 TLB1 entries.
1024 ram_tlb_address = (unsigned int)CFG_DDR_SDRAM_BASE;
1025 while (ram_tlb_address < (memsize * 1024 * 1024)
1026 && ram_tlb_index < 16) {
1027 mtspr(MAS0, TLB1_MAS0(1, ram_tlb_index, 0));
1028 mtspr(MAS1, TLB1_MAS1(1, 1, 0, 0, tlb_size));
1029 mtspr(MAS2, TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
1030 0, 0, 0, 0, 0, 0, 0, 0));
1031 mtspr(MAS3, TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
1032 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
1033 asm volatile("isync;msync;tlbwe;isync");
1035 debug("DDR: MAS0=0x%08x\n", TLB1_MAS0(1, ram_tlb_index, 0));
1036 debug("DDR: MAS1=0x%08x\n", TLB1_MAS1(1, 1, 0, 0, tlb_size));
1037 debug("DDR: MAS2=0x%08x\n",
1038 TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
1039 0, 0, 0, 0, 0, 0, 0, 0));
1040 debug("DDR: MAS3=0x%08x\n",
1041 TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
1042 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
1044 ram_tlb_address += (0x1000 << ((tlb_size - 1) * 2));
1050 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
1052 law_size = 19 + __ilog2(memsize);
1055 * Set up LAWBAR for all of DDR.
1057 ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
1058 ecm->lawar1 = (LAWAR_EN
1060 | (LAWAR_SIZE & law_size));
1061 debug("DDR: LAWBAR1=0x%08x\n", ecm->lawbar1);
1062 debug("DDR: LARAR1=0x%08x\n", ecm->lawar1);
1065 * Confirm that the requested amount of memory was mapped.
1070 #endif /* CONFIG_SPD_EEPROM */
1073 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
1076 * Initialize all of memory for ECC, then enable errors.
1080 ddr_enable_ecc(unsigned int dram_size)
1084 volatile immap_t *immap = (immap_t *)CFG_IMMR;
1085 volatile ccsr_ddr_t *ddr= &immap->im_ddr;
1089 for (*p = 0; p < (uint *)(8 * 1024); p++) {
1090 if (((unsigned int)p & 0x1f) == 0) {
1091 ppcDcbz((unsigned long) p);
1093 *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
1094 if (((unsigned int)p & 0x1c) == 0x1c) {
1095 ppcDcbf((unsigned long) p);
1099 dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
1100 dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
1101 dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
1102 dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
1103 dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
1104 dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
1105 dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
1106 dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
1107 dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
1108 dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
1110 for (i = 1; i < dram_size / 0x800000; i++) {
1111 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
1115 * Enable errors for ECC.
1117 debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
1118 ddr->err_disable = 0x00000000;
1119 asm("sync;isync;msync");
1120 debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
1123 #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */