blob: b9ae9509be3dee082850e38c3101c4219567b063 [file] [log] [blame]
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001/*
York Suna4c66502012-08-17 08:22:39 +00002 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Gala58e5e9a2008-08-26 15:01:29 -05003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
7 */
8
9/*
10 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
11 * Based on code from spd_sdram.c
12 * Author: James Yang [at freescale.com]
13 */
14
15#include <common.h>
Kumar Galac39f44d2011-01-31 22:18:47 -060016#include <i2c.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050017#include <asm/fsl_ddr_sdram.h>
York Suna4c66502012-08-17 08:22:39 +000018#include <asm/fsl_law.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050019
20#include "ddr.h"
21
York Suna4c66502012-08-17 08:22:39 +000022void fsl_ddr_set_lawbar(
Kumar Gala58e5e9a2008-08-26 15:01:29 -050023 const common_timing_params_t *memctl_common_params,
24 unsigned int memctl_interleaved,
25 unsigned int ctrl_num);
York Suna4c66502012-08-17 08:22:39 +000026void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Gala58e5e9a2008-08-26 15:01:29 -050027
Kumar Galac39f44d2011-01-31 22:18:47 -060028#if defined(SPD_EEPROM_ADDRESS) || \
29 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
30 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
31#if (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
32u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
33 [0][0] = SPD_EEPROM_ADDRESS,
34};
York Sun639f3302011-08-26 11:32:41 -070035#elif (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
36u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
37 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
38 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
39};
40#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
Kumar Galac39f44d2011-01-31 22:18:47 -060041u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
42 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
43 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
44};
York Sun639f3302011-08-26 11:32:41 -070045#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
Kumar Galac39f44d2011-01-31 22:18:47 -060046u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
47 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
48 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
49 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
50 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
51};
York Suna4c66502012-08-17 08:22:39 +000052#elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
53u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
54 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
55 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
56 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
57};
58#elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
59u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
60 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
61 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
62 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
63 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
64 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
65 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
66};
67
Kumar Galac39f44d2011-01-31 22:18:47 -060068#endif
69
70static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
71{
72 int ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
73 sizeof(generic_spd_eeprom_t));
74
75 if (ret) {
York Sun82968a72012-10-08 07:44:28 +000076 if (i2c_address ==
77#ifdef SPD_EEPROM_ADDRESS
78 SPD_EEPROM_ADDRESS
79#elif defined(SPD_EEPROM_ADDRESS1)
80 SPD_EEPROM_ADDRESS1
81#endif
82 ) {
83 printf("DDR: failed to read SPD from address %u\n",
84 i2c_address);
85 } else {
86 debug("DDR: failed to read SPD from address %u\n",
87 i2c_address);
88 }
Kumar Galac39f44d2011-01-31 22:18:47 -060089 memset(spd, 0, sizeof(generic_spd_eeprom_t));
90 }
91}
92
93__attribute__((weak, alias("__get_spd")))
94void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
95
96void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
97 unsigned int ctrl_num)
98{
99 unsigned int i;
100 unsigned int i2c_address = 0;
101
102 if (ctrl_num >= CONFIG_NUM_DDR_CONTROLLERS) {
103 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
104 return;
105 }
106
107 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
108 i2c_address = spd_i2c_addr[ctrl_num][i];
109 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
110 }
111}
112#else
113void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
114 unsigned int ctrl_num)
115{
116}
117#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500118
119/*
120 * ASSUMPTIONS:
121 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
122 * - Same memory data bus width on all controllers
123 *
124 * NOTES:
125 *
126 * The memory controller and associated documentation use confusing
127 * terminology when referring to the orgranization of DRAM.
128 *
129 * Here is a terminology translation table:
130 *
131 * memory controller/documention |industry |this code |signals
132 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200133 * physical bank/bank |rank |rank |chip select (CS)
134 * logical bank/sub-bank |bank |bank |bank address (BA)
135 * page/row |row |page |row address
136 * ??? |column |column |column address
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500137 *
138 * The naming confusion is further exacerbated by the descriptions of the
139 * memory controller interleaving feature, where accesses are interleaved
140 * _BETWEEN_ two seperate memory controllers. This is configured only in
141 * CS0_CONFIG[INTLV_CTL] of each memory controller.
142 *
143 * memory controller documentation | number of chip selects
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200144 * | per memory controller supported
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500145 * --------------------------------|-----------------------------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200146 * cache line interleaving | 1 (CS0 only)
147 * page interleaving | 1 (CS0 only)
148 * bank interleaving | 1 (CS0 only)
149 * superbank interleraving | depends on bank (chip select)
150 * | interleraving [rank interleaving]
151 * | mode used on every memory controller
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500152 *
153 * Even further confusing is the existence of the interleaving feature
154 * _WITHIN_ each memory controller. The feature is referred to in
155 * documentation as chip select interleaving or bank interleaving,
156 * although it is configured in the DDR_SDRAM_CFG field.
157 *
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200158 * Name of field | documentation name | this code
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500159 * -----------------------------|-----------------------|------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200160 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
161 * | interleaving
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500162 */
163
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500164const char *step_string_tbl[] = {
165 "STEP_GET_SPD",
166 "STEP_COMPUTE_DIMM_PARMS",
167 "STEP_COMPUTE_COMMON_PARMS",
168 "STEP_GATHER_OPTS",
169 "STEP_ASSIGN_ADDRESSES",
170 "STEP_COMPUTE_REGS",
171 "STEP_PROGRAM_REGS",
172 "STEP_ALL"
173};
174
175const char * step_to_string(unsigned int step) {
176
177 unsigned int s = __ilog2(step);
178
179 if ((1 << s) != step)
180 return step_string_tbl[7];
181
182 return step_string_tbl[s];
183}
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500184
York Sunef002272013-03-25 07:39:35 +0000185static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Suna4c66502012-08-17 08:22:39 +0000186 unsigned int dbw_cap_adj[])
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500187{
188 int i, j;
York Suna4c66502012-08-17 08:22:39 +0000189 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
190 unsigned long long rank_density, ctlr_density = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500191
192 /*
193 * If a reduced data width is requested, but the SPD
194 * specifies a physically wider device, adjust the
195 * computed dimm capacities accordingly before
196 * assigning addresses.
197 */
198 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
199 unsigned int found = 0;
200
201 switch (pinfo->memctl_opts[i].data_bus_width) {
202 case 2:
203 /* 16-bit */
York Sun51d498f2011-05-27 07:25:51 +0800204 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
205 unsigned int dw;
206 if (!pinfo->dimm_params[i][j].n_ranks)
207 continue;
208 dw = pinfo->dimm_params[i][j].primary_sdram_width;
209 if ((dw == 72 || dw == 64)) {
210 dbw_cap_adj[i] = 2;
211 break;
212 } else if ((dw == 40 || dw == 32)) {
213 dbw_cap_adj[i] = 1;
214 break;
215 }
216 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500217 break;
218
219 case 1:
220 /* 32-bit */
221 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
222 unsigned int dw;
223 dw = pinfo->dimm_params[i][j].data_width;
224 if (pinfo->dimm_params[i][j].n_ranks
225 && (dw == 72 || dw == 64)) {
226 /*
227 * FIXME: can't really do it
228 * like this because this just
229 * further reduces the memory
230 */
231 found = 1;
232 break;
233 }
234 }
235 if (found) {
236 dbw_cap_adj[i] = 1;
237 }
238 break;
239
240 case 0:
241 /* 64-bit */
242 break;
243
244 default:
245 printf("unexpected data bus width "
246 "specified controller %u\n", i);
247 return 1;
248 }
York Suna4c66502012-08-17 08:22:39 +0000249 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500250 }
251
York Suna4c66502012-08-17 08:22:39 +0000252 current_mem_base = 0ull;
253 total_mem = 0;
254 if (pinfo->memctl_opts[0].memctl_interleaving) {
255 rank_density = pinfo->dimm_params[0][0].rank_density >>
256 dbw_cap_adj[0];
257 switch (pinfo->memctl_opts[0].ba_intlv_ctl &
258 FSL_DDR_CS0_CS1_CS2_CS3) {
259 case FSL_DDR_CS0_CS1_CS2_CS3:
260 ctlr_density = 4 * rank_density;
261 break;
262 case FSL_DDR_CS0_CS1:
263 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
264 ctlr_density = 2 * rank_density;
265 break;
266 case FSL_DDR_CS2_CS3:
267 default:
268 ctlr_density = rank_density;
269 break;
270 }
271 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
272 rank_density, ctlr_density);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500273 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
York Suna4c66502012-08-17 08:22:39 +0000274 if (pinfo->memctl_opts[i].memctl_interleaving) {
275 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
276 case FSL_DDR_CACHE_LINE_INTERLEAVING:
277 case FSL_DDR_PAGE_INTERLEAVING:
278 case FSL_DDR_BANK_INTERLEAVING:
279 case FSL_DDR_SUPERBANK_INTERLEAVING:
280 total_ctlr_mem = 2 * ctlr_density;
281 break;
282 case FSL_DDR_3WAY_1KB_INTERLEAVING:
283 case FSL_DDR_3WAY_4KB_INTERLEAVING:
284 case FSL_DDR_3WAY_8KB_INTERLEAVING:
285 total_ctlr_mem = 3 * ctlr_density;
286 break;
287 case FSL_DDR_4WAY_1KB_INTERLEAVING:
288 case FSL_DDR_4WAY_4KB_INTERLEAVING:
289 case FSL_DDR_4WAY_8KB_INTERLEAVING:
290 total_ctlr_mem = 4 * ctlr_density;
291 break;
292 default:
293 panic("Unknown interleaving mode");
294 }
295 pinfo->common_timing_params[i].base_address =
296 current_mem_base;
297 pinfo->common_timing_params[i].total_mem =
298 total_ctlr_mem;
299 total_mem = current_mem_base + total_ctlr_mem;
300 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
301 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
302 } else {
303 /* when 3rd controller not interleaved */
304 current_mem_base = total_mem;
305 total_ctlr_mem = 0;
306 pinfo->common_timing_params[i].base_address =
307 current_mem_base;
308 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
309 unsigned long long cap =
310 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
311 pinfo->dimm_params[i][j].base_address =
312 current_mem_base;
313 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
314 current_mem_base += cap;
315 total_ctlr_mem += cap;
316 }
317 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
318 pinfo->common_timing_params[i].total_mem =
319 total_ctlr_mem;
320 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500321 }
322 }
323 } else {
324 /*
325 * Simple linear assignment if memory
326 * controllers are not interleaved.
327 */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500328 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
York Suna4c66502012-08-17 08:22:39 +0000329 total_ctlr_mem = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500330 pinfo->common_timing_params[i].base_address =
York Suna4c66502012-08-17 08:22:39 +0000331 current_mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500332 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
333 /* Compute DIMM base addresses. */
334 unsigned long long cap =
York Suna4c66502012-08-17 08:22:39 +0000335 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500336 pinfo->dimm_params[i][j].base_address =
York Suna4c66502012-08-17 08:22:39 +0000337 current_mem_base;
338 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
339 current_mem_base += cap;
340 total_ctlr_mem += cap;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500341 }
York Suna4c66502012-08-17 08:22:39 +0000342 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500343 pinfo->common_timing_params[i].total_mem =
York Suna4c66502012-08-17 08:22:39 +0000344 total_ctlr_mem;
345 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500346 }
347 }
York Suna4c66502012-08-17 08:22:39 +0000348 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500349
York Suna4c66502012-08-17 08:22:39 +0000350 return total_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500351}
352
York Sunef002272013-03-25 07:39:35 +0000353/* Use weak function to allow board file to override the address assignment */
354__attribute__((weak, alias("__step_assign_addresses")))
355unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
356 unsigned int dbw_cap_adj[]);
357
Kumar Galae7563af2009-06-11 23:42:35 -0500358unsigned long long
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500359fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
360 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500361{
362 unsigned int i, j;
Kumar Galae7563af2009-06-11 23:42:35 -0500363 unsigned long long total_mem = 0;
York Sunc63e1372013-06-25 11:37:48 -0700364 int assert_reset;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500365
366 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
367 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sunc63e1372013-06-25 11:37:48 -0700368 assert_reset = board_need_mem_reset();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500369
370 /* data bus width capacity adjust shift amount */
371 unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
372
373 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
374 dbw_capacity_adjust[i] = 0;
375 }
376
377 debug("starting at step %u (%s)\n",
378 start_step, step_to_string(start_step));
379
380 switch (start_step) {
381 case STEP_GET_SPD:
York Sun1b3e3c42011-06-07 09:42:16 +0800382#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500383 /* STEP 1: Gather all DIMM SPD data */
384 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
385 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
386 }
387
388 case STEP_COMPUTE_DIMM_PARMS:
389 /* STEP 2: Compute DIMM parameters from SPD data */
390
391 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
392 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
393 unsigned int retval;
394 generic_spd_eeprom_t *spd =
395 &(pinfo->spd_installed_dimms[i][j]);
396 dimm_params_t *pdimm =
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200397 &(pinfo->dimm_params[i][j]);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500398
399 retval = compute_dimm_parameters(spd, pdimm, i);
York Sunf2d264b2011-06-07 09:42:17 +0800400#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun62f739f2012-08-17 08:22:42 +0000401 if (!i && !j && retval) {
York Suna4c66502012-08-17 08:22:39 +0000402 printf("SPD error on controller %d! "
403 "Trying fallback to raw timing "
404 "calculation\n", i);
York Sunf2d264b2011-06-07 09:42:17 +0800405 fsl_ddr_get_dimm_params(pdimm, i, j);
406 }
407#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500408 if (retval == 2) {
409 printf("Error: compute_dimm_parameters"
410 " non-zero returned FATAL value "
411 "for memctl=%u dimm=%u\n", i, j);
412 return 0;
413 }
York Sunf2d264b2011-06-07 09:42:17 +0800414#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500415 if (retval) {
416 debug("Warning: compute_dimm_parameters"
417 " non-zero return value for memctl=%u "
418 "dimm=%u\n", i, j);
419 }
420 }
421 }
422
Shaohui Xie98de3692012-06-28 23:36:38 +0000423#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sun1b3e3c42011-06-07 09:42:16 +0800424 case STEP_COMPUTE_DIMM_PARMS:
425 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
426 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
427 dimm_params_t *pdimm =
428 &(pinfo->dimm_params[i][j]);
429 fsl_ddr_get_dimm_params(pdimm, i, j);
430 }
431 }
432 debug("Filling dimm parameters from board specific file\n");
433#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500434 case STEP_COMPUTE_COMMON_PARMS:
435 /*
436 * STEP 3: Compute a common set of timing parameters
437 * suitable for all of the DIMMs on each memory controller
438 */
439 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
440 debug("Computing lowest common DIMM"
441 " parameters for memctl=%u\n", i);
442 compute_lowest_common_dimm_parameters(
443 pinfo->dimm_params[i],
444 &timing_params[i],
445 CONFIG_DIMM_SLOTS_PER_CTLR);
446 }
447
448 case STEP_GATHER_OPTS:
449 /* STEP 4: Gather configuration requirements from user */
450 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
451 debug("Reloading memory controller "
452 "configuration options for memctl=%u\n", i);
453 /*
454 * This "reloads" the memory controller options
455 * to defaults. If the user "edits" an option,
456 * next_step points to the step after this,
457 * which is currently STEP_ASSIGN_ADDRESSES.
458 */
459 populate_memctl_options(
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530460 timing_params[i].all_dimms_registered,
Haiying Wangdfb49102008-10-03 12:36:55 -0400461 &pinfo->memctl_opts[i],
462 pinfo->dimm_params[i], i);
York Sunc63e1372013-06-25 11:37:48 -0700463 /*
464 * For RDIMMs, JEDEC spec requires clocks to be stable
465 * before reset signal is deasserted. For the boards
466 * using fixed parameters, this function should be
467 * be called from board init file.
468 */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530469 if (timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700470 assert_reset = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500471 }
York Sunc63e1372013-06-25 11:37:48 -0700472 if (assert_reset) {
473 debug("Asserting mem reset\n");
474 board_assert_mem_reset();
475 }
476
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500477 case STEP_ASSIGN_ADDRESSES:
478 /* STEP 5: Assign addresses to chip selects */
York Suna4c66502012-08-17 08:22:39 +0000479 check_interleaving_options(pinfo);
480 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500481
482 case STEP_COMPUTE_REGS:
483 /* STEP 6: compute controller register values */
York Suna4c66502012-08-17 08:22:39 +0000484 debug("FSL Memory ctrl register computation\n");
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500485 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
486 if (timing_params[i].ndimms_present == 0) {
487 memset(&ddr_reg[i], 0,
488 sizeof(fsl_ddr_cfg_regs_t));
489 continue;
490 }
491
492 compute_fsl_memctl_config_regs(
493 &pinfo->memctl_opts[i],
494 &ddr_reg[i], &timing_params[i],
495 pinfo->dimm_params[i],
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500496 dbw_capacity_adjust[i],
497 size_only);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500498 }
499
500 default:
501 break;
502 }
503
York Suna4c66502012-08-17 08:22:39 +0000504 {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500505 /*
506 * Compute the amount of memory available just by
507 * looking for the highest valid CSn_BNDS value.
508 * This allows us to also experiment with using
509 * only CS0 when using dual-rank DIMMs.
510 */
511 unsigned int max_end = 0;
512
513 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
514 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
515 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
516 if (reg->cs[j].config & 0x80000000) {
517 unsigned int end;
York Sund8556db2013-06-25 11:37:45 -0700518 /*
519 * 0xfffffff is a special value we put
520 * for unused bnds
521 */
522 if (reg->cs[j].bnds == 0xffffffff)
523 continue;
524 end = reg->cs[j].bnds & 0xffff;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500525 if (end > max_end) {
526 max_end = end;
527 }
528 }
529 }
530 }
531
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500532 total_mem = 1 + (((unsigned long long)max_end << 24ULL)
533 | 0xFFFFFFULL);
534 }
535
536 return total_mem;
537}
538
539/*
540 * fsl_ddr_sdram() -- this is the main function to be called by
541 * initdram() in the board file.
542 *
543 * It returns amount of memory configured in bytes.
544 */
545phys_size_t fsl_ddr_sdram(void)
546{
547 unsigned int i;
York Suna4c66502012-08-17 08:22:39 +0000548 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
Kumar Galae7563af2009-06-11 23:42:35 -0500549 unsigned long long total_memory;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500550 fsl_ddr_info_t info;
York Sunc63e1372013-06-25 11:37:48 -0700551 int deassert_reset;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500552
553 /* Reset info structure. */
554 memset(&info, 0, sizeof(fsl_ddr_info_t));
555
556 /* Compute it once normally. */
York Sun6f5e1dc2011-09-16 13:21:35 -0700557#ifdef CONFIG_FSL_DDR_INTERACTIVE
James Yange8ba6c52013-01-07 14:01:03 +0000558 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
559 total_memory = fsl_ddr_interactive(&info, 0);
560 } else if (fsl_ddr_interactive_env_var_exists()) {
561 total_memory = fsl_ddr_interactive(&info, 1);
York Sune750cfa2013-01-04 08:13:59 +0000562 } else
York Sun6f5e1dc2011-09-16 13:21:35 -0700563#endif
564 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500565
York Sunf31cfd12012-10-08 07:44:24 +0000566 /* setup 3-way interleaving before enabling DDRC */
York Sun0a7c5352013-03-25 07:33:20 +0000567 if (info.memctl_opts[0].memctl_interleaving) {
568 switch (info.memctl_opts[0].memctl_interleaving_mode) {
569 case FSL_DDR_3WAY_1KB_INTERLEAVING:
570 case FSL_DDR_3WAY_4KB_INTERLEAVING:
571 case FSL_DDR_3WAY_8KB_INTERLEAVING:
572 fsl_ddr_set_intl3r(
573 info.memctl_opts[0].memctl_interleaving_mode);
574 break;
575 default:
576 break;
577 }
York Sunf31cfd12012-10-08 07:44:24 +0000578 }
579
York Sunc63e1372013-06-25 11:37:48 -0700580 /*
581 * Program configuration registers.
582 * JEDEC specs requires clocks to be stable before deasserting reset
583 * for RDIMMs. Clocks start after chip select is enabled and clock
584 * control register is set. During step 1, all controllers have their
585 * registers set but not enabled. Step 2 proceeds after deasserting
586 * reset through board FPGA or GPIO.
587 * For non-registered DIMMs, initialization can go through but it is
588 * also OK to follow the same flow.
589 */
590 deassert_reset = board_need_mem_reset();
591 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530592 if (info.common_timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700593 deassert_reset = 1;
594 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500595 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
596 debug("Programming controller %u\n", i);
597 if (info.common_timing_params[i].ndimms_present == 0) {
598 debug("No dimms present on controller %u; "
599 "skipping programming\n", i);
600 continue;
601 }
York Sunc63e1372013-06-25 11:37:48 -0700602 /*
603 * The following call with step = 1 returns before enabling
604 * the controller. It has to finish with step = 2 later.
605 */
606 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i,
607 deassert_reset ? 1 : 0);
608 }
609 if (deassert_reset) {
610 /* Use board FPGA or GPIO to deassert reset signal */
611 debug("Deasserting mem reset\n");
612 board_deassert_mem_reset();
613 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
614 /* Call with step = 2 to continue initialization */
615 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]),
616 i, 2);
617 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500618 }
619
York Suna4c66502012-08-17 08:22:39 +0000620 /* program LAWs */
621 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
622 if (info.memctl_opts[i].memctl_interleaving) {
623 switch (info.memctl_opts[i].memctl_interleaving_mode) {
624 case FSL_DDR_CACHE_LINE_INTERLEAVING:
625 case FSL_DDR_PAGE_INTERLEAVING:
626 case FSL_DDR_BANK_INTERLEAVING:
627 case FSL_DDR_SUPERBANK_INTERLEAVING:
628 if (i == 0) {
629 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
630 fsl_ddr_set_lawbar(&info.common_timing_params[i],
631 law_memctl, i);
632 } else if (i == 2) {
633 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
634 fsl_ddr_set_lawbar(&info.common_timing_params[i],
635 law_memctl, i);
636 }
637 break;
638 case FSL_DDR_3WAY_1KB_INTERLEAVING:
639 case FSL_DDR_3WAY_4KB_INTERLEAVING:
640 case FSL_DDR_3WAY_8KB_INTERLEAVING:
641 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
642 if (i == 0) {
York Suna4c66502012-08-17 08:22:39 +0000643 fsl_ddr_set_lawbar(&info.common_timing_params[i],
644 law_memctl, i);
645 }
646 break;
647 case FSL_DDR_4WAY_1KB_INTERLEAVING:
648 case FSL_DDR_4WAY_4KB_INTERLEAVING:
649 case FSL_DDR_4WAY_8KB_INTERLEAVING:
650 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
651 if (i == 0)
652 fsl_ddr_set_lawbar(&info.common_timing_params[i],
653 law_memctl, i);
654 /* place holder for future 4-way interleaving */
655 break;
656 default:
657 break;
658 }
659 } else {
660 switch (i) {
661 case 0:
662 law_memctl = LAW_TRGT_IF_DDR_1;
663 break;
664 case 1:
665 law_memctl = LAW_TRGT_IF_DDR_2;
666 break;
667 case 2:
668 law_memctl = LAW_TRGT_IF_DDR_3;
669 break;
670 case 3:
671 law_memctl = LAW_TRGT_IF_DDR_4;
672 break;
673 default:
674 break;
675 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500676 fsl_ddr_set_lawbar(&info.common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000677 law_memctl, i);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500678 }
679 }
680
York Suna4c66502012-08-17 08:22:39 +0000681 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Galae7563af2009-06-11 23:42:35 -0500682
683#if !defined(CONFIG_PHYS_64BIT)
684 /* Check for 4G or more. Bad. */
685 if (total_memory >= (1ull << 32)) {
Shruti Kanetkar2f848f92013-08-15 11:25:37 -0500686 puts("Detected ");
687 print_size(total_memory, " of memory\n");
Becky Bruce7ea38712010-12-17 17:17:59 -0600688 printf(" This U-Boot only supports < 4G of DDR\n");
689 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
690 printf(" "); /* re-align to match init_func_ram print */
Kumar Galae7563af2009-06-11 23:42:35 -0500691 total_memory = CONFIG_MAX_MEM_MAPPED;
692 }
693#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500694
695 return total_memory;
696}
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500697
698/*
699 * fsl_ddr_sdram_size() - This function only returns the size of the total
700 * memory without setting ddr control registers.
701 */
702phys_size_t
703fsl_ddr_sdram_size(void)
704{
705 fsl_ddr_info_t info;
706 unsigned long long total_memory = 0;
707
708 memset(&info, 0 , sizeof(fsl_ddr_info_t));
709
710 /* Compute it once normally. */
711 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
712
713 return total_memory;
714}