blob: 774c0e4b42f249f6aa02ff14002bccd388513eb0 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
Dave Liu707aa5c2010-03-05 12:22:00 +08002 * Copyright 2008, 2010 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05003 *
Dave Liu707aa5c2010-03-05 12:22:00 +08004 * 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)
7 * any later version.
Kumar Gala124b0822008-08-26 15:01:29 -05008 */
9
10#include <common.h>
Kumar Gala64042092010-07-14 10:04:21 -050011#include <hwconfig.h>
Kumar Gala124b0822008-08-26 15:01:29 -050012#include <asm/fsl_ddr_sdram.h>
13
14#include "ddr.h"
15
16/* Board-specific functions defined in each board's ddr.c */
17extern void fsl_ddr_board_options(memctl_options_t *popts,
Haiying Wangfa440362008-10-03 12:36:55 -040018 dimm_params_t *pdimm,
Kumar Gala124b0822008-08-26 15:01:29 -050019 unsigned int ctrl_num);
20
21unsigned int populate_memctl_options(int all_DIMMs_registered,
22 memctl_options_t *popts,
Haiying Wangfa440362008-10-03 12:36:55 -040023 dimm_params_t *pdimm,
Kumar Gala124b0822008-08-26 15:01:29 -050024 unsigned int ctrl_num)
25{
26 unsigned int i;
27
28 /* Chip select options. */
29
30 /* Pick chip-select local options. */
31 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
32 /* If not DDR2, odt_rd_cfg and odt_wr_cfg need to be 0. */
33
34 /* only for single CS? */
35 popts->cs_local_opts[i].odt_rd_cfg = 0;
36
37 popts->cs_local_opts[i].odt_wr_cfg = 1;
38 popts->cs_local_opts[i].auto_precharge = 0;
39 }
40
41 /* Pick interleaving mode. */
42
43 /*
44 * 0 = no interleaving
45 * 1 = interleaving between 2 controllers
46 */
47 popts->memctl_interleaving = 0;
48
49 /*
50 * 0 = cacheline
51 * 1 = page
52 * 2 = (logical) bank
53 * 3 = superbank (only if CS interleaving is enabled)
54 */
55 popts->memctl_interleaving_mode = 0;
56
57 /*
58 * 0: cacheline: bit 30 of the 36-bit physical addr selects the memctl
59 * 1: page: bit to the left of the column bits selects the memctl
60 * 2: bank: bit to the left of the bank bits selects the memctl
61 * 3: superbank: bit to the left of the chip select selects the memctl
62 *
63 * NOTE: ba_intlv (rank interleaving) is independent of memory
64 * controller interleaving; it is only within a memory controller.
65 * Must use superbank interleaving if rank interleaving is used and
66 * memory controller interleaving is enabled.
67 */
68
69 /*
70 * 0 = no
71 * 0x40 = CS0,CS1
72 * 0x20 = CS2,CS3
73 * 0x60 = CS0,CS1 + CS2,CS3
74 * 0x04 = CS0,CS1,CS2,CS3
75 */
76 popts->ba_intlv_ctl = 0;
77
78 /* Memory Organization Parameters */
79 popts->registered_dimm_en = all_DIMMs_registered;
80
81 /* Operational Mode Paramters */
82
83 /* Pick ECC modes */
84#ifdef CONFIG_DDR_ECC
85 popts->ECC_mode = 1; /* 0 = disabled, 1 = enabled */
86#else
87 popts->ECC_mode = 0; /* 0 = disabled, 1 = enabled */
88#endif
89 popts->ECC_init_using_memctl = 1; /* 0 = use DMA, 1 = use memctl */
90
91 /*
92 * Choose DQS config
93 * 0 for DDR1
94 * 1 for DDR2
95 */
96#if defined(CONFIG_FSL_DDR1)
97 popts->DQS_config = 0;
Dave Liu4be87b22009-03-14 12:48:30 +080098#elif defined(CONFIG_FSL_DDR2) || defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -050099 popts->DQS_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500100#endif
101
102 /* Choose self-refresh during sleep. */
103 popts->self_refresh_in_sleep = 1;
104
105 /* Choose dynamic power management mode. */
106 popts->dynamic_power = 0;
107
108 /* 0 = 64-bit, 1 = 32-bit, 2 = 16-bit */
109 popts->data_bus_width = 0;
110
111 /* Choose burst length. */
Dave Liu4be87b22009-03-14 12:48:30 +0800112#if defined(CONFIG_FSL_DDR3)
Dave Liu707aa5c2010-03-05 12:22:00 +0800113#if defined(CONFIG_E500MC)
114 popts->OTF_burst_chop_en = 0; /* on-the-fly burst chop disable */
115 popts->burst_length = DDR_BL8; /* Fixed 8-beat burst len */
116#else
Dave Liu4be87b22009-03-14 12:48:30 +0800117 popts->OTF_burst_chop_en = 1; /* on-the-fly burst chop */
118 popts->burst_length = DDR_OTF; /* on-the-fly BC4 and BL8 */
Dave Liu707aa5c2010-03-05 12:22:00 +0800119#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800120#else
121 popts->burst_length = DDR_BL4; /* has to be 4 for DDR2 */
122#endif
123
124 /* Choose ddr controller address mirror mode */
125#if defined(CONFIG_FSL_DDR3)
126 popts->mirrored_dimm = pdimm[0].mirrored_dimm;
127#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500128
129 /* Global Timing Parameters. */
130 debug("mclk_ps = %u ps\n", get_memory_clk_period_ps());
131
132 /* Pick a caslat override. */
133 popts->cas_latency_override = 0;
134 popts->cas_latency_override_value = 3;
135 if (popts->cas_latency_override) {
136 debug("using caslat override value = %u\n",
137 popts->cas_latency_override_value);
138 }
139
140 /* Decide whether to use the computed derated latency */
141 popts->use_derated_caslat = 0;
142
143 /* Choose an additive latency. */
144 popts->additive_latency_override = 0;
145 popts->additive_latency_override_value = 3;
146 if (popts->additive_latency_override) {
147 debug("using additive latency override value = %u\n",
148 popts->additive_latency_override_value);
149 }
150
151 /*
152 * 2T_EN setting
153 *
154 * Factors to consider for 2T_EN:
155 * - number of DIMMs installed
156 * - number of components, number of active ranks
157 * - how much time you want to spend playing around
158 */
Dave Liua06d74c2008-11-21 16:31:43 +0800159 popts->twoT_en = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500160 popts->threeT_en = 0;
161
162 /*
163 * BSTTOPRE precharge interval
164 *
165 * Set this to 0 for global auto precharge
166 *
167 * FIXME: Should this be configured in picoseconds?
168 * Why it should be in ps: better understanding of this
169 * relative to actual DRAM timing parameters such as tRAS.
170 * e.g. tRAS(min) = 40 ns
171 */
172 popts->bstopre = 0x100;
173
174 /* Minimum CKE pulse width -- tCKE(MIN) */
175 popts->tCKE_clock_pulse_width_ps
176 = mclk_to_picos(FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR);
177
178 /*
179 * Window for four activates -- tFAW
180 *
181 * FIXME: UM: applies only to DDR2/DDR3 with eight logical banks only
182 * FIXME: varies depending upon number of column addresses or data
183 * FIXME: width, was considering looking at pdimm->primary_sdram_width
184 */
185#if defined(CONFIG_FSL_DDR1)
186 popts->tFAW_window_four_activates_ps = mclk_to_picos(1);
187
188#elif defined(CONFIG_FSL_DDR2)
189 /*
190 * x4/x8; some datasheets have 35000
191 * x16 wide columns only? Use 50000?
192 */
193 popts->tFAW_window_four_activates_ps = 37500;
194
195#elif defined(CONFIG_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +0800196 popts->tFAW_window_four_activates_ps = pdimm[0].tFAW_ps;
197#endif
198 popts->zq_en = 0;
199 popts->wrlvl_en = 0;
200#if defined(CONFIG_FSL_DDR3)
201 /*
202 * due to ddr3 dimm is fly-by topology
203 * we suggest to enable write leveling to
204 * meet the tQDSS under different loading.
205 */
206 popts->wrlvl_en = 1;
york1714e492010-07-02 22:25:56 +0000207 popts->zq_en = 1;
Dave Liu64ee7df2009-12-16 10:24:37 -0600208 popts->wrlvl_override = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500209#endif
210
Kumar Gala124b0822008-08-26 15:01:29 -0500211 /*
Haiying Wangb834f922008-10-03 12:37:10 -0400212 * Check interleaving configuration from environment.
213 * Please refer to doc/README.fsl-ddr for the detail.
Kumar Gala124b0822008-08-26 15:01:29 -0500214 *
215 * If memory controller interleaving is enabled, then the data
york93799ca2010-07-02 22:25:52 +0000216 * bus widths must be programmed identically for all memory controllers.
Haiying Wangb834f922008-10-03 12:37:10 -0400217 *
york93799ca2010-07-02 22:25:52 +0000218 * XXX: Attempt to set all controllers to the same chip select
Haiying Wangb834f922008-10-03 12:37:10 -0400219 * interleaving mode. It will do a best effort to get the
220 * requested ranks interleaved together such that the result
221 * should be a subset of the requested configuration.
Kumar Gala124b0822008-08-26 15:01:29 -0500222 */
Kumar Galac0f3b3c2009-02-06 09:56:34 -0600223#if (CONFIG_NUM_DDR_CONTROLLERS > 1)
Kumar Gala64042092010-07-14 10:04:21 -0500224 if (hwconfig_sub("fsl_ddr", "ctlr_intlv")) {
Haiying Wangb834f922008-10-03 12:37:10 -0400225 if (pdimm[0].n_ranks == 0) {
york93799ca2010-07-02 22:25:52 +0000226 printf("There is no rank on CS0 for controller %d. Because only"
227 " rank on CS0 and ranks chip-select interleaved with CS0"
Ed Swarthoutb135d932008-10-29 09:21:44 -0500228 " are controller interleaved, force non memory "
york93799ca2010-07-02 22:25:52 +0000229 "controller interleaving\n", ctrl_num);
Haiying Wangb834f922008-10-03 12:37:10 -0400230 popts->memctl_interleaving = 0;
231 } else {
232 popts->memctl_interleaving = 1;
york93799ca2010-07-02 22:25:52 +0000233 /*
234 * test null first. if CONFIG_HWCONFIG is not defined
235 * hwconfig_arg_cmp returns non-zero
236 */
Kumar Gala64042092010-07-14 10:04:21 -0500237 if (hwconfig_subarg_cmp("fsl_ddr", "ctlr_intlv", "null")) {
238 popts->memctl_interleaving = 0;
239 debug("memory controller interleaving disabled.\n");
240 } else if (hwconfig_subarg_cmp("fsl_ddr", "ctlr_intlv", "cacheline"))
Haiying Wangb834f922008-10-03 12:37:10 -0400241 popts->memctl_interleaving_mode =
242 FSL_DDR_CACHE_LINE_INTERLEAVING;
Kumar Gala64042092010-07-14 10:04:21 -0500243 else if (hwconfig_subarg_cmp("fsl_ddr", "ctlr_intlv", "page"))
Haiying Wangb834f922008-10-03 12:37:10 -0400244 popts->memctl_interleaving_mode =
245 FSL_DDR_PAGE_INTERLEAVING;
Kumar Gala64042092010-07-14 10:04:21 -0500246 else if (hwconfig_subarg_cmp("fsl_ddr", "ctlr_intlv", "bank"))
Haiying Wangb834f922008-10-03 12:37:10 -0400247 popts->memctl_interleaving_mode =
248 FSL_DDR_BANK_INTERLEAVING;
Kumar Gala64042092010-07-14 10:04:21 -0500249 else if (hwconfig_subarg_cmp("fsl_ddr", "ctlr_intlv", "superbank"))
Haiying Wangb834f922008-10-03 12:37:10 -0400250 popts->memctl_interleaving_mode =
251 FSL_DDR_SUPERBANK_INTERLEAVING;
Kumar Gala64042092010-07-14 10:04:21 -0500252 else {
253 popts->memctl_interleaving = 0;
254 printf("hwconfig has unrecognized parameter for ctlr_intlv.\n");
255 }
Haiying Wangb834f922008-10-03 12:37:10 -0400256 }
257 }
Dave Liu0f9318f2009-11-12 07:26:37 +0800258#endif
Kumar Gala64042092010-07-14 10:04:21 -0500259 if ((hwconfig_sub("fsl_ddr", "bank_intlv")) &&
Dave Liu0f9318f2009-11-12 07:26:37 +0800260 (CONFIG_CHIP_SELECTS_PER_CTRL > 1)) {
Kumar Gala64042092010-07-14 10:04:21 -0500261 /* test null first. if CONFIG_HWCONFIG is not defined,
262 * hwconfig_arg_cmp returns non-zero */
263 if (hwconfig_subarg_cmp("fsl_ddr", "bank_intlv", "null"))
york93799ca2010-07-02 22:25:52 +0000264 debug("bank interleaving disabled.\n");
Kumar Gala64042092010-07-14 10:04:21 -0500265 else if (hwconfig_subarg_cmp("fsl_ddr", "bank_intlv", "cs0_cs1"))
Haiying Wangb834f922008-10-03 12:37:10 -0400266 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1;
Kumar Gala64042092010-07-14 10:04:21 -0500267 else if (hwconfig_subarg_cmp("fsl_ddr", "bank_intlv", "cs2_cs3"))
Haiying Wangb834f922008-10-03 12:37:10 -0400268 popts->ba_intlv_ctl = FSL_DDR_CS2_CS3;
Kumar Gala64042092010-07-14 10:04:21 -0500269 else if (hwconfig_subarg_cmp("fsl_ddr", "bank_intlv", "cs0_cs1_and_cs2_cs3"))
Haiying Wangb834f922008-10-03 12:37:10 -0400270 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_AND_CS2_CS3;
Kumar Gala64042092010-07-14 10:04:21 -0500271 else if (hwconfig_subarg_cmp("fsl_ddr", "bank_intlv", "cs0_cs1_cs2_cs3"))
Haiying Wangb834f922008-10-03 12:37:10 -0400272 popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_CS2_CS3;
273 else
york93799ca2010-07-02 22:25:52 +0000274 printf("hwconfig has unrecognized parameter for bank_intlv.\n");
Haiying Wangb834f922008-10-03 12:37:10 -0400275 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
276 case FSL_DDR_CS0_CS1_CS2_CS3:
york93799ca2010-07-02 22:25:52 +0000277#if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
yorkf4f93c62010-07-02 22:25:53 +0000278 if (pdimm[0].n_ranks < 4) {
york93799ca2010-07-02 22:25:52 +0000279 popts->ba_intlv_ctl = 0;
280 printf("Not enough bank(chip-select) for "
281 "CS0+CS1+CS2+CS3 on controller %d, "
282 "force non-interleaving!\n", ctrl_num);
283 }
284#elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
yorkf4f93c62010-07-02 22:25:53 +0000285 if ((pdimm[0].n_ranks < 2) && (pdimm[1].n_ranks < 2)) {
york93799ca2010-07-02 22:25:52 +0000286 popts->ba_intlv_ctl = 0;
287 printf("Not enough bank(chip-select) for "
288 "CS0+CS1+CS2+CS3 on controller %d, "
289 "force non-interleaving!\n", ctrl_num);
290 }
291 if (pdimm[0].capacity != pdimm[1].capacity) {
292 popts->ba_intlv_ctl = 0;
293 printf("Not identical DIMM size for "
294 "CS0+CS1+CS2+CS3 on controller %d, "
295 "force non-interleaving!\n", ctrl_num);
296 }
297#endif
298 break;
Haiying Wangb834f922008-10-03 12:37:10 -0400299 case FSL_DDR_CS0_CS1:
yorkf4f93c62010-07-02 22:25:53 +0000300 if (pdimm[0].n_ranks < 2) {
Haiying Wangb834f922008-10-03 12:37:10 -0400301 popts->ba_intlv_ctl = 0;
Ed Swarthoutb135d932008-10-29 09:21:44 -0500302 printf("Not enough bank(chip-select) for "
york93799ca2010-07-02 22:25:52 +0000303 "CS0+CS1 on controller %d, "
304 "force non-interleaving!\n", ctrl_num);
Haiying Wangb834f922008-10-03 12:37:10 -0400305 }
306 break;
307 case FSL_DDR_CS2_CS3:
york93799ca2010-07-02 22:25:52 +0000308#if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
yorkf4f93c62010-07-02 22:25:53 +0000309 if (pdimm[0].n_ranks < 4) {
Haiying Wangb834f922008-10-03 12:37:10 -0400310 popts->ba_intlv_ctl = 0;
york93799ca2010-07-02 22:25:52 +0000311 printf("Not enough bank(chip-select) for CS2+CS3 "
312 "on controller %d, force non-interleaving!\n", ctrl_num);
Haiying Wangb834f922008-10-03 12:37:10 -0400313 }
york93799ca2010-07-02 22:25:52 +0000314#elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
yorkf4f93c62010-07-02 22:25:53 +0000315 if (pdimm[1].n_ranks < 2) {
york93799ca2010-07-02 22:25:52 +0000316 popts->ba_intlv_ctl = 0;
317 printf("Not enough bank(chip-select) for CS2+CS3 "
318 "on controller %d, force non-interleaving!\n", ctrl_num);
319 }
320#endif
Haiying Wangb834f922008-10-03 12:37:10 -0400321 break;
322 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
york93799ca2010-07-02 22:25:52 +0000323#if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
yorkf4f93c62010-07-02 22:25:53 +0000324 if (pdimm[0].n_ranks < 4) {
york93799ca2010-07-02 22:25:52 +0000325 popts->ba_intlv_ctl = 0;
326 printf("Not enough bank(CS) for CS0+CS1 and "
327 "CS2+CS3 on controller %d, "
328 "force non-interleaving!\n", ctrl_num);
329 }
330#elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
yorkf4f93c62010-07-02 22:25:53 +0000331 if ((pdimm[0].n_ranks < 2) || (pdimm[1].n_ranks < 2)) {
Haiying Wangb834f922008-10-03 12:37:10 -0400332 popts->ba_intlv_ctl = 0;
york93799ca2010-07-02 22:25:52 +0000333 printf("Not enough bank(CS) for CS0+CS1 and "
334 "CS2+CS3 on controller %d, "
335 "force non-interleaving!\n", ctrl_num);
Haiying Wangb834f922008-10-03 12:37:10 -0400336 }
york93799ca2010-07-02 22:25:52 +0000337#endif
Haiying Wangb834f922008-10-03 12:37:10 -0400338 break;
339 default:
340 popts->ba_intlv_ctl = 0;
341 break;
342 }
343 }
Kumar Gala124b0822008-08-26 15:01:29 -0500344
york42603722010-07-02 22:25:54 +0000345 if (hwconfig_sub("fsl_ddr", "addr_hash")) {
346 if (hwconfig_subarg_cmp("fsl_ddr", "addr_hash", "null"))
347 popts->addr_hash = 0;
348 else if (hwconfig_subarg_cmp("fsl_ddr", "addr_hash", "true"))
349 popts->addr_hash = 1;
350 }
351
yorkf4f93c62010-07-02 22:25:53 +0000352 if (pdimm[0].n_ranks == 4)
353 popts->quad_rank_present = 1;
354
Haiying Wangfa440362008-10-03 12:36:55 -0400355 fsl_ddr_board_options(popts, pdimm, ctrl_num);
Kumar Gala124b0822008-08-26 15:01:29 -0500356
357 return 0;
358}
york93799ca2010-07-02 22:25:52 +0000359
360void check_interleaving_options(fsl_ddr_info_t *pinfo)
361{
362 int i, j, check_n_ranks, intlv_fixed = 0;
363 unsigned long long check_rank_density;
364 /*
365 * Check if all controllers are configured for memory
366 * controller interleaving. Identical dimms are recommended. At least
367 * the size should be checked.
368 */
369 j = 0;
370 check_n_ranks = pinfo->dimm_params[0][0].n_ranks;
371 check_rank_density = pinfo->dimm_params[0][0].rank_density;
372 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
373 if ((pinfo->memctl_opts[i].memctl_interleaving) && \
374 (check_rank_density == pinfo->dimm_params[i][0].rank_density) && \
375 (check_n_ranks == pinfo->dimm_params[i][0].n_ranks)) {
376 j++;
377 }
378 }
379 if (j != CONFIG_NUM_DDR_CONTROLLERS) {
380 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
381 if (pinfo->memctl_opts[i].memctl_interleaving) {
382 pinfo->memctl_opts[i].memctl_interleaving = 0;
383 intlv_fixed = 1;
384 }
385 if (intlv_fixed)
386 printf("Not all DIMMs are identical in size. "
387 "Memory controller interleaving disabled.\n");
388 }
389}