2 * Copyright 2008, 2010-2011 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
12 #include <asm/fsl_ddr_sdram.h>
17 * Use our own stack based buffer before relocation to allow accessing longer
18 * hwconfig strings that might be in the environment before we've relocated.
19 * This is pretty fragile on both the use of stack and if the buffer is big
20 * enough. However we will get a warning from getenv_f for the later.
22 #define HWCONFIG_BUFFER_SIZE 128
24 /* Board-specific functions defined in each board's ddr.c */
25 extern void fsl_ddr_board_options(memctl_options_t *popts,
27 unsigned int ctrl_num);
29 unsigned int populate_memctl_options(int all_DIMMs_registered,
30 memctl_options_t *popts,
32 unsigned int ctrl_num)
35 char buffer[HWCONFIG_BUFFER_SIZE];
39 * Extract hwconfig from environment since we have not properly setup
40 * the environment but need it for ddr config params
42 if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
45 /* Chip select options. */
47 /* Pick chip-select local options. */
48 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
49 /* If not DDR2, odt_rd_cfg and odt_wr_cfg need to be 0. */
51 /* only for single CS? */
52 popts->cs_local_opts[i].odt_rd_cfg = 0;
54 popts->cs_local_opts[i].odt_wr_cfg = 1;
55 popts->cs_local_opts[i].auto_precharge = 0;
58 /* Pick interleaving mode. */
62 * 1 = interleaving between 2 controllers
64 popts->memctl_interleaving = 0;
70 * 3 = superbank (only if CS interleaving is enabled)
72 popts->memctl_interleaving_mode = 0;
75 * 0: cacheline: bit 30 of the 36-bit physical addr selects the memctl
76 * 1: page: bit to the left of the column bits selects the memctl
77 * 2: bank: bit to the left of the bank bits selects the memctl
78 * 3: superbank: bit to the left of the chip select selects the memctl
80 * NOTE: ba_intlv (rank interleaving) is independent of memory
81 * controller interleaving; it is only within a memory controller.
82 * Must use superbank interleaving if rank interleaving is used and
83 * memory controller interleaving is enabled.
90 * 0x60 = CS0,CS1 + CS2,CS3
91 * 0x04 = CS0,CS1,CS2,CS3
93 popts->ba_intlv_ctl = 0;
95 /* Memory Organization Parameters */
96 popts->registered_dimm_en = all_DIMMs_registered;
98 /* Operational Mode Paramters */
101 #ifdef CONFIG_DDR_ECC
102 popts->ECC_mode = 1; /* 0 = disabled, 1 = enabled */
104 popts->ECC_mode = 0; /* 0 = disabled, 1 = enabled */
106 popts->ECC_init_using_memctl = 1; /* 0 = use DMA, 1 = use memctl */
113 #if defined(CONFIG_FSL_DDR1)
114 popts->DQS_config = 0;
115 #elif defined(CONFIG_FSL_DDR2) || defined(CONFIG_FSL_DDR3)
116 popts->DQS_config = 1;
119 /* Choose self-refresh during sleep. */
120 popts->self_refresh_in_sleep = 1;
122 /* Choose dynamic power management mode. */
123 popts->dynamic_power = 0;
125 /* 0 = 64-bit, 1 = 32-bit, 2 = 16-bit */
126 popts->data_bus_width = 0;
128 /* Choose burst length. */
129 #if defined(CONFIG_FSL_DDR3)
130 #if defined(CONFIG_E500MC)
131 popts->OTF_burst_chop_en = 0; /* on-the-fly burst chop disable */
132 popts->burst_length = DDR_BL8; /* Fixed 8-beat burst len */
134 popts->OTF_burst_chop_en = 1; /* on-the-fly burst chop */
135 popts->burst_length = DDR_OTF; /* on-the-fly BC4 and BL8 */
138 popts->burst_length = DDR_BL4; /* has to be 4 for DDR2 */
141 /* Choose ddr controller address mirror mode */
142 #if defined(CONFIG_FSL_DDR3)
143 popts->mirrored_dimm = pdimm[0].mirrored_dimm;
146 /* Global Timing Parameters. */
147 debug("mclk_ps = %u ps\n", get_memory_clk_period_ps());
149 /* Pick a caslat override. */
150 popts->cas_latency_override = 0;
151 popts->cas_latency_override_value = 3;
152 if (popts->cas_latency_override) {
153 debug("using caslat override value = %u\n",
154 popts->cas_latency_override_value);
157 /* Decide whether to use the computed derated latency */
158 popts->use_derated_caslat = 0;
160 /* Choose an additive latency. */
161 popts->additive_latency_override = 0;
162 popts->additive_latency_override_value = 3;
163 if (popts->additive_latency_override) {
164 debug("using additive latency override value = %u\n",
165 popts->additive_latency_override_value);
171 * Factors to consider for 2T_EN:
172 * - number of DIMMs installed
173 * - number of components, number of active ranks
174 * - how much time you want to spend playing around
177 popts->threeT_en = 0;
180 * BSTTOPRE precharge interval
182 * Set this to 0 for global auto precharge
184 * FIXME: Should this be configured in picoseconds?
185 * Why it should be in ps: better understanding of this
186 * relative to actual DRAM timing parameters such as tRAS.
187 * e.g. tRAS(min) = 40 ns
189 popts->bstopre = 0x100;
191 /* Minimum CKE pulse width -- tCKE(MIN) */
192 popts->tCKE_clock_pulse_width_ps
193 = mclk_to_picos(FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR);
196 * Window for four activates -- tFAW
198 * FIXME: UM: applies only to DDR2/DDR3 with eight logical banks only
199 * FIXME: varies depending upon number of column addresses or data
200 * FIXME: width, was considering looking at pdimm->primary_sdram_width
202 #if defined(CONFIG_FSL_DDR1)
203 popts->tFAW_window_four_activates_ps = mclk_to_picos(1);
205 #elif defined(CONFIG_FSL_DDR2)
207 * x4/x8; some datasheets have 35000
208 * x16 wide columns only? Use 50000?
210 popts->tFAW_window_four_activates_ps = 37500;
212 #elif defined(CONFIG_FSL_DDR3)
213 popts->tFAW_window_four_activates_ps = pdimm[0].tFAW_ps;
217 #if defined(CONFIG_FSL_DDR3)
219 * due to ddr3 dimm is fly-by topology
220 * we suggest to enable write leveling to
221 * meet the tQDSS under different loading.
225 popts->wrlvl_override = 0;
229 * Check interleaving configuration from environment.
230 * Please refer to doc/README.fsl-ddr for the detail.
232 * If memory controller interleaving is enabled, then the data
233 * bus widths must be programmed identically for all memory controllers.
235 * XXX: Attempt to set all controllers to the same chip select
236 * interleaving mode. It will do a best effort to get the
237 * requested ranks interleaved together such that the result
238 * should be a subset of the requested configuration.
240 #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
241 if (hwconfig_sub_f("fsl_ddr", "ctlr_intlv", buf)) {
242 if (pdimm[0].n_ranks == 0) {
243 printf("There is no rank on CS0 for controller %d. Because only"
244 " rank on CS0 and ranks chip-select interleaved with CS0"
245 " are controller interleaved, force non memory "
246 "controller interleaving\n", ctrl_num);
247 popts->memctl_interleaving = 0;
249 popts->memctl_interleaving = 1;
251 * test null first. if CONFIG_HWCONFIG is not defined
252 * hwconfig_arg_cmp returns non-zero
254 if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
256 popts->memctl_interleaving = 0;
257 debug("memory controller interleaving disabled.\n");
258 } else if (hwconfig_subarg_cmp_f("fsl_ddr",
261 popts->memctl_interleaving_mode =
262 FSL_DDR_CACHE_LINE_INTERLEAVING;
263 else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
265 popts->memctl_interleaving_mode =
266 FSL_DDR_PAGE_INTERLEAVING;
267 else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
269 popts->memctl_interleaving_mode =
270 FSL_DDR_BANK_INTERLEAVING;
271 else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
273 popts->memctl_interleaving_mode =
274 FSL_DDR_SUPERBANK_INTERLEAVING;
276 popts->memctl_interleaving = 0;
277 printf("hwconfig has unrecognized parameter for ctlr_intlv.\n");
282 if ((hwconfig_sub_f("fsl_ddr", "bank_intlv", buf)) &&
283 (CONFIG_CHIP_SELECTS_PER_CTRL > 1)) {
284 /* test null first. if CONFIG_HWCONFIG is not defined,
285 * hwconfig_subarg_cmp_f returns non-zero */
286 if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
288 debug("bank interleaving disabled.\n");
289 else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
291 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1;
292 else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
294 popts->ba_intlv_ctl = FSL_DDR_CS2_CS3;
295 else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
296 "cs0_cs1_and_cs2_cs3", buf))
297 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_AND_CS2_CS3;
298 else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
299 "cs0_cs1_cs2_cs3", buf))
300 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_CS2_CS3;
302 printf("hwconfig has unrecognized parameter for bank_intlv.\n");
303 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
304 case FSL_DDR_CS0_CS1_CS2_CS3:
305 #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
306 if (pdimm[0].n_ranks < 4) {
307 popts->ba_intlv_ctl = 0;
308 printf("Not enough bank(chip-select) for "
309 "CS0+CS1+CS2+CS3 on controller %d, "
310 "force non-interleaving!\n", ctrl_num);
312 #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
313 if ((pdimm[0].n_ranks < 2) && (pdimm[1].n_ranks < 2)) {
314 popts->ba_intlv_ctl = 0;
315 printf("Not enough bank(chip-select) for "
316 "CS0+CS1+CS2+CS3 on controller %d, "
317 "force non-interleaving!\n", ctrl_num);
319 if (pdimm[0].capacity != pdimm[1].capacity) {
320 popts->ba_intlv_ctl = 0;
321 printf("Not identical DIMM size for "
322 "CS0+CS1+CS2+CS3 on controller %d, "
323 "force non-interleaving!\n", ctrl_num);
327 case FSL_DDR_CS0_CS1:
328 if (pdimm[0].n_ranks < 2) {
329 popts->ba_intlv_ctl = 0;
330 printf("Not enough bank(chip-select) for "
331 "CS0+CS1 on controller %d, "
332 "force non-interleaving!\n", ctrl_num);
335 case FSL_DDR_CS2_CS3:
336 #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
337 if (pdimm[0].n_ranks < 4) {
338 popts->ba_intlv_ctl = 0;
339 printf("Not enough bank(chip-select) for CS2+CS3 "
340 "on controller %d, force non-interleaving!\n", ctrl_num);
342 #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
343 if (pdimm[1].n_ranks < 2) {
344 popts->ba_intlv_ctl = 0;
345 printf("Not enough bank(chip-select) for CS2+CS3 "
346 "on controller %d, force non-interleaving!\n", ctrl_num);
350 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
351 #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
352 if (pdimm[0].n_ranks < 4) {
353 popts->ba_intlv_ctl = 0;
354 printf("Not enough bank(CS) for CS0+CS1 and "
355 "CS2+CS3 on controller %d, "
356 "force non-interleaving!\n", ctrl_num);
358 #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
359 if ((pdimm[0].n_ranks < 2) || (pdimm[1].n_ranks < 2)) {
360 popts->ba_intlv_ctl = 0;
361 printf("Not enough bank(CS) for CS0+CS1 and "
362 "CS2+CS3 on controller %d, "
363 "force non-interleaving!\n", ctrl_num);
368 popts->ba_intlv_ctl = 0;
373 if (hwconfig_sub_f("fsl_ddr", "addr_hash", buf)) {
374 if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash", "null", buf))
375 popts->addr_hash = 0;
376 else if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash",
378 popts->addr_hash = 1;
381 if (pdimm[0].n_ranks == 4)
382 popts->quad_rank_present = 1;
384 fsl_ddr_board_options(popts, pdimm, ctrl_num);
389 void check_interleaving_options(fsl_ddr_info_t *pinfo)
391 int i, j, check_n_ranks, intlv_fixed = 0;
392 unsigned long long check_rank_density;
394 * Check if all controllers are configured for memory
395 * controller interleaving. Identical dimms are recommended. At least
396 * the size should be checked.
399 check_n_ranks = pinfo->dimm_params[0][0].n_ranks;
400 check_rank_density = pinfo->dimm_params[0][0].rank_density;
401 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
402 if ((pinfo->memctl_opts[i].memctl_interleaving) && \
403 (check_rank_density == pinfo->dimm_params[i][0].rank_density) && \
404 (check_n_ranks == pinfo->dimm_params[i][0].n_ranks)) {
408 if (j != CONFIG_NUM_DDR_CONTROLLERS) {
409 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
410 if (pinfo->memctl_opts[i].memctl_interleaving) {
411 pinfo->memctl_opts[i].memctl_interleaving = 0;
415 printf("Not all DIMMs are identical in size. "
416 "Memory controller interleaving disabled.\n");
420 int fsl_use_spd(void)
424 #ifdef CONFIG_DDR_SPD
425 char buffer[HWCONFIG_BUFFER_SIZE];
429 * Extract hwconfig from environment since we have not properly setup
430 * the environment but need it for ddr config params
432 if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
435 /* if hwconfig is not enabled, or "sdram" is not defined, use spd */
436 if (hwconfig_sub_f("fsl_ddr", "sdram", buf)) {
437 if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram", "spd", buf))
439 else if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram",