blob: b4988e194cfad3f20fcddabbc2dfb875181441b9 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
York Sune8dc17b2012-08-17 08:22:39 +00002 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-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 Galac68e86c2011-01-31 22:18:47 -060016#include <i2c.h>
York Sunf0626592013-09-30 09:22:09 -070017#include <fsl_ddr_sdram.h>
York Sunf0626592013-09-30 09:22:09 -070018#include <fsl_ddr.h>
Kumar Gala124b0822008-08-26 15:01:29 -050019
York Sun461c9392013-09-30 14:20:51 -070020#ifdef CONFIG_PPC
21#include <asm/fsl_law.h>
22
York Sune8dc17b2012-08-17 08:22:39 +000023void fsl_ddr_set_lawbar(
Kumar Gala124b0822008-08-26 15:01:29 -050024 const common_timing_params_t *memctl_common_params,
25 unsigned int memctl_interleaved,
26 unsigned int ctrl_num);
York Sun461c9392013-09-30 14:20:51 -070027#endif
Kumar Gala124b0822008-08-26 15:01:29 -050028
York Sun461c9392013-09-30 14:20:51 -070029void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac68e86c2011-01-31 22:18:47 -060030#if defined(SPD_EEPROM_ADDRESS) || \
31 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
32 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
33#if (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
34u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
35 [0][0] = SPD_EEPROM_ADDRESS,
36};
York Sun92b46ac2011-08-26 11:32:41 -070037#elif (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
Kumar Galac68e86c2011-01-31 22:18:47 -060038u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
39 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
York Sun92b46ac2011-08-26 11:32:41 -070040 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
41};
42#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
43u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
44 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
Kumar Galac68e86c2011-01-31 22:18:47 -060045 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
46};
York Sun92b46ac2011-08-26 11:32:41 -070047#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
Kumar Galac68e86c2011-01-31 22:18:47 -060048u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
49 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
50 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
51 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
52 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
53};
York Sune8dc17b2012-08-17 08:22:39 +000054#elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
55u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
56 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
57 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
58 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
59};
60#elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
61u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
62 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
63 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
64 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
65 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
66 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
67 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
68};
69
Kumar Galac68e86c2011-01-31 22:18:47 -060070#endif
71
72static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
73{
Valentin Longchamp993967a2013-10-18 11:47:19 +020074 int ret;
75
76 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
77
78 ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
Kumar Galac68e86c2011-01-31 22:18:47 -060079 sizeof(generic_spd_eeprom_t));
80
81 if (ret) {
York Sun9b9372f2012-10-08 07:44:28 +000082 if (i2c_address ==
83#ifdef SPD_EEPROM_ADDRESS
84 SPD_EEPROM_ADDRESS
85#elif defined(SPD_EEPROM_ADDRESS1)
86 SPD_EEPROM_ADDRESS1
87#endif
88 ) {
89 printf("DDR: failed to read SPD from address %u\n",
90 i2c_address);
91 } else {
92 debug("DDR: failed to read SPD from address %u\n",
93 i2c_address);
94 }
Kumar Galac68e86c2011-01-31 22:18:47 -060095 memset(spd, 0, sizeof(generic_spd_eeprom_t));
96 }
97}
98
99__attribute__((weak, alias("__get_spd")))
100void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
101
102void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
103 unsigned int ctrl_num)
104{
105 unsigned int i;
106 unsigned int i2c_address = 0;
107
108 if (ctrl_num >= CONFIG_NUM_DDR_CONTROLLERS) {
109 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
110 return;
111 }
112
113 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
114 i2c_address = spd_i2c_addr[ctrl_num][i];
115 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
116 }
117}
118#else
119void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
120 unsigned int ctrl_num)
121{
122}
123#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala124b0822008-08-26 15:01:29 -0500124
125/*
126 * ASSUMPTIONS:
127 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
128 * - Same memory data bus width on all controllers
129 *
130 * NOTES:
131 *
132 * The memory controller and associated documentation use confusing
133 * terminology when referring to the orgranization of DRAM.
134 *
135 * Here is a terminology translation table:
136 *
137 * memory controller/documention |industry |this code |signals
138 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200139 * physical bank/bank |rank |rank |chip select (CS)
140 * logical bank/sub-bank |bank |bank |bank address (BA)
141 * page/row |row |page |row address
142 * ??? |column |column |column address
Kumar Gala124b0822008-08-26 15:01:29 -0500143 *
144 * The naming confusion is further exacerbated by the descriptions of the
145 * memory controller interleaving feature, where accesses are interleaved
146 * _BETWEEN_ two seperate memory controllers. This is configured only in
147 * CS0_CONFIG[INTLV_CTL] of each memory controller.
148 *
149 * memory controller documentation | number of chip selects
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200150 * | per memory controller supported
Kumar Gala124b0822008-08-26 15:01:29 -0500151 * --------------------------------|-----------------------------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200152 * cache line interleaving | 1 (CS0 only)
153 * page interleaving | 1 (CS0 only)
154 * bank interleaving | 1 (CS0 only)
155 * superbank interleraving | depends on bank (chip select)
156 * | interleraving [rank interleaving]
157 * | mode used on every memory controller
Kumar Gala124b0822008-08-26 15:01:29 -0500158 *
159 * Even further confusing is the existence of the interleaving feature
160 * _WITHIN_ each memory controller. The feature is referred to in
161 * documentation as chip select interleaving or bank interleaving,
162 * although it is configured in the DDR_SDRAM_CFG field.
163 *
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200164 * Name of field | documentation name | this code
Kumar Gala124b0822008-08-26 15:01:29 -0500165 * -----------------------------|-----------------------|------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200166 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
167 * | interleaving
Kumar Gala124b0822008-08-26 15:01:29 -0500168 */
169
Kumar Gala124b0822008-08-26 15:01:29 -0500170const char *step_string_tbl[] = {
171 "STEP_GET_SPD",
172 "STEP_COMPUTE_DIMM_PARMS",
173 "STEP_COMPUTE_COMMON_PARMS",
174 "STEP_GATHER_OPTS",
175 "STEP_ASSIGN_ADDRESSES",
176 "STEP_COMPUTE_REGS",
177 "STEP_PROGRAM_REGS",
178 "STEP_ALL"
179};
180
181const char * step_to_string(unsigned int step) {
182
183 unsigned int s = __ilog2(step);
184
185 if ((1 << s) != step)
186 return step_string_tbl[7];
187
188 return step_string_tbl[s];
189}
Kumar Gala124b0822008-08-26 15:01:29 -0500190
York Sun01a82582013-03-25 07:39:35 +0000191static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Sune8dc17b2012-08-17 08:22:39 +0000192 unsigned int dbw_cap_adj[])
Kumar Gala124b0822008-08-26 15:01:29 -0500193{
194 int i, j;
York Sune8dc17b2012-08-17 08:22:39 +0000195 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
196 unsigned long long rank_density, ctlr_density = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500197
198 /*
199 * If a reduced data width is requested, but the SPD
200 * specifies a physically wider device, adjust the
201 * computed dimm capacities accordingly before
202 * assigning addresses.
203 */
204 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
205 unsigned int found = 0;
206
207 switch (pinfo->memctl_opts[i].data_bus_width) {
208 case 2:
209 /* 16-bit */
York Sundd803dd2011-05-27 07:25:51 +0800210 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
211 unsigned int dw;
212 if (!pinfo->dimm_params[i][j].n_ranks)
213 continue;
214 dw = pinfo->dimm_params[i][j].primary_sdram_width;
215 if ((dw == 72 || dw == 64)) {
216 dbw_cap_adj[i] = 2;
217 break;
218 } else if ((dw == 40 || dw == 32)) {
219 dbw_cap_adj[i] = 1;
220 break;
221 }
222 }
Kumar Gala124b0822008-08-26 15:01:29 -0500223 break;
224
225 case 1:
226 /* 32-bit */
227 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
228 unsigned int dw;
229 dw = pinfo->dimm_params[i][j].data_width;
230 if (pinfo->dimm_params[i][j].n_ranks
231 && (dw == 72 || dw == 64)) {
232 /*
233 * FIXME: can't really do it
234 * like this because this just
235 * further reduces the memory
236 */
237 found = 1;
238 break;
239 }
240 }
241 if (found) {
242 dbw_cap_adj[i] = 1;
243 }
244 break;
245
246 case 0:
247 /* 64-bit */
248 break;
249
250 default:
251 printf("unexpected data bus width "
252 "specified controller %u\n", i);
253 return 1;
254 }
York Sune8dc17b2012-08-17 08:22:39 +0000255 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala124b0822008-08-26 15:01:29 -0500256 }
257
York Sune8dc17b2012-08-17 08:22:39 +0000258 current_mem_base = 0ull;
259 total_mem = 0;
260 if (pinfo->memctl_opts[0].memctl_interleaving) {
261 rank_density = pinfo->dimm_params[0][0].rank_density >>
262 dbw_cap_adj[0];
263 switch (pinfo->memctl_opts[0].ba_intlv_ctl &
264 FSL_DDR_CS0_CS1_CS2_CS3) {
265 case FSL_DDR_CS0_CS1_CS2_CS3:
266 ctlr_density = 4 * rank_density;
267 break;
268 case FSL_DDR_CS0_CS1:
269 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
270 ctlr_density = 2 * rank_density;
271 break;
272 case FSL_DDR_CS2_CS3:
273 default:
274 ctlr_density = rank_density;
275 break;
276 }
277 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
278 rank_density, ctlr_density);
Kumar Gala124b0822008-08-26 15:01:29 -0500279 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000280 if (pinfo->memctl_opts[i].memctl_interleaving) {
281 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
282 case FSL_DDR_CACHE_LINE_INTERLEAVING:
283 case FSL_DDR_PAGE_INTERLEAVING:
284 case FSL_DDR_BANK_INTERLEAVING:
285 case FSL_DDR_SUPERBANK_INTERLEAVING:
286 total_ctlr_mem = 2 * ctlr_density;
287 break;
288 case FSL_DDR_3WAY_1KB_INTERLEAVING:
289 case FSL_DDR_3WAY_4KB_INTERLEAVING:
290 case FSL_DDR_3WAY_8KB_INTERLEAVING:
291 total_ctlr_mem = 3 * ctlr_density;
292 break;
293 case FSL_DDR_4WAY_1KB_INTERLEAVING:
294 case FSL_DDR_4WAY_4KB_INTERLEAVING:
295 case FSL_DDR_4WAY_8KB_INTERLEAVING:
296 total_ctlr_mem = 4 * ctlr_density;
297 break;
298 default:
299 panic("Unknown interleaving mode");
300 }
301 pinfo->common_timing_params[i].base_address =
302 current_mem_base;
303 pinfo->common_timing_params[i].total_mem =
304 total_ctlr_mem;
305 total_mem = current_mem_base + total_ctlr_mem;
306 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
307 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
308 } else {
309 /* when 3rd controller not interleaved */
310 current_mem_base = total_mem;
311 total_ctlr_mem = 0;
312 pinfo->common_timing_params[i].base_address =
313 current_mem_base;
314 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
315 unsigned long long cap =
316 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
317 pinfo->dimm_params[i][j].base_address =
318 current_mem_base;
319 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
320 current_mem_base += cap;
321 total_ctlr_mem += cap;
322 }
323 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
324 pinfo->common_timing_params[i].total_mem =
325 total_ctlr_mem;
326 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500327 }
328 }
329 } else {
330 /*
331 * Simple linear assignment if memory
332 * controllers are not interleaved.
333 */
Kumar Gala124b0822008-08-26 15:01:29 -0500334 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000335 total_ctlr_mem = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500336 pinfo->common_timing_params[i].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000337 current_mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500338 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
339 /* Compute DIMM base addresses. */
340 unsigned long long cap =
York Sune8dc17b2012-08-17 08:22:39 +0000341 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala124b0822008-08-26 15:01:29 -0500342 pinfo->dimm_params[i][j].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000343 current_mem_base;
344 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
345 current_mem_base += cap;
346 total_ctlr_mem += cap;
Kumar Gala124b0822008-08-26 15:01:29 -0500347 }
York Sune8dc17b2012-08-17 08:22:39 +0000348 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500349 pinfo->common_timing_params[i].total_mem =
York Sune8dc17b2012-08-17 08:22:39 +0000350 total_ctlr_mem;
351 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500352 }
353 }
York Sune8dc17b2012-08-17 08:22:39 +0000354 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500355
York Sune8dc17b2012-08-17 08:22:39 +0000356 return total_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500357}
358
York Sun01a82582013-03-25 07:39:35 +0000359/* Use weak function to allow board file to override the address assignment */
360__attribute__((weak, alias("__step_assign_addresses")))
361unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
362 unsigned int dbw_cap_adj[]);
363
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500364unsigned long long
Haiying Wang80ad4012010-12-01 10:35:31 -0500365fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
366 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -0500367{
368 unsigned int i, j;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500369 unsigned long long total_mem = 0;
York Sun5e155552013-06-25 11:37:48 -0700370 int assert_reset;
Kumar Gala124b0822008-08-26 15:01:29 -0500371
372 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
373 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun5e155552013-06-25 11:37:48 -0700374 assert_reset = board_need_mem_reset();
Kumar Gala124b0822008-08-26 15:01:29 -0500375
376 /* data bus width capacity adjust shift amount */
377 unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
378
379 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
380 dbw_capacity_adjust[i] = 0;
381 }
382
383 debug("starting at step %u (%s)\n",
384 start_step, step_to_string(start_step));
385
386 switch (start_step) {
387 case STEP_GET_SPD:
York Sune73cc042011-06-07 09:42:16 +0800388#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala124b0822008-08-26 15:01:29 -0500389 /* STEP 1: Gather all DIMM SPD data */
390 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
391 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
392 }
393
394 case STEP_COMPUTE_DIMM_PARMS:
395 /* STEP 2: Compute DIMM parameters from SPD data */
396
397 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
398 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
399 unsigned int retval;
400 generic_spd_eeprom_t *spd =
401 &(pinfo->spd_installed_dimms[i][j]);
402 dimm_params_t *pdimm =
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200403 &(pinfo->dimm_params[i][j]);
Kumar Gala124b0822008-08-26 15:01:29 -0500404
405 retval = compute_dimm_parameters(spd, pdimm, i);
York Sun09d8aa82011-06-07 09:42:17 +0800406#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun11dd8842012-08-17 08:22:42 +0000407 if (!i && !j && retval) {
York Sune8dc17b2012-08-17 08:22:39 +0000408 printf("SPD error on controller %d! "
409 "Trying fallback to raw timing "
410 "calculation\n", i);
York Sun09d8aa82011-06-07 09:42:17 +0800411 fsl_ddr_get_dimm_params(pdimm, i, j);
412 }
413#else
Kumar Gala124b0822008-08-26 15:01:29 -0500414 if (retval == 2) {
415 printf("Error: compute_dimm_parameters"
416 " non-zero returned FATAL value "
417 "for memctl=%u dimm=%u\n", i, j);
418 return 0;
419 }
York Sun09d8aa82011-06-07 09:42:17 +0800420#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500421 if (retval) {
422 debug("Warning: compute_dimm_parameters"
423 " non-zero return value for memctl=%u "
424 "dimm=%u\n", i, j);
425 }
426 }
427 }
428
Shaohui Xie24009042012-06-28 23:36:38 +0000429#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sune73cc042011-06-07 09:42:16 +0800430 case STEP_COMPUTE_DIMM_PARMS:
431 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
432 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
433 dimm_params_t *pdimm =
434 &(pinfo->dimm_params[i][j]);
435 fsl_ddr_get_dimm_params(pdimm, i, j);
436 }
437 }
438 debug("Filling dimm parameters from board specific file\n");
439#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500440 case STEP_COMPUTE_COMMON_PARMS:
441 /*
442 * STEP 3: Compute a common set of timing parameters
443 * suitable for all of the DIMMs on each memory controller
444 */
445 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
446 debug("Computing lowest common DIMM"
447 " parameters for memctl=%u\n", i);
448 compute_lowest_common_dimm_parameters(
449 pinfo->dimm_params[i],
450 &timing_params[i],
451 CONFIG_DIMM_SLOTS_PER_CTLR);
452 }
453
454 case STEP_GATHER_OPTS:
455 /* STEP 4: Gather configuration requirements from user */
456 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
457 debug("Reloading memory controller "
458 "configuration options for memctl=%u\n", i);
459 /*
460 * This "reloads" the memory controller options
461 * to defaults. If the user "edits" an option,
462 * next_step points to the step after this,
463 * which is currently STEP_ASSIGN_ADDRESSES.
464 */
465 populate_memctl_options(
Priyanka Jain4a717412013-09-25 10:41:19 +0530466 timing_params[i].all_dimms_registered,
Haiying Wangfa440362008-10-03 12:36:55 -0400467 &pinfo->memctl_opts[i],
468 pinfo->dimm_params[i], i);
York Sun5e155552013-06-25 11:37:48 -0700469 /*
470 * For RDIMMs, JEDEC spec requires clocks to be stable
471 * before reset signal is deasserted. For the boards
472 * using fixed parameters, this function should be
473 * be called from board init file.
474 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530475 if (timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700476 assert_reset = 1;
477 }
478 if (assert_reset) {
479 debug("Asserting mem reset\n");
480 board_assert_mem_reset();
Kumar Gala124b0822008-08-26 15:01:29 -0500481 }
York Sun5e155552013-06-25 11:37:48 -0700482
Kumar Gala124b0822008-08-26 15:01:29 -0500483 case STEP_ASSIGN_ADDRESSES:
484 /* STEP 5: Assign addresses to chip selects */
York Sune8dc17b2012-08-17 08:22:39 +0000485 check_interleaving_options(pinfo);
486 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
Kumar Gala124b0822008-08-26 15:01:29 -0500487
488 case STEP_COMPUTE_REGS:
489 /* STEP 6: compute controller register values */
York Sune8dc17b2012-08-17 08:22:39 +0000490 debug("FSL Memory ctrl register computation\n");
Kumar Gala124b0822008-08-26 15:01:29 -0500491 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
492 if (timing_params[i].ndimms_present == 0) {
493 memset(&ddr_reg[i], 0,
494 sizeof(fsl_ddr_cfg_regs_t));
495 continue;
496 }
497
498 compute_fsl_memctl_config_regs(
499 &pinfo->memctl_opts[i],
500 &ddr_reg[i], &timing_params[i],
501 pinfo->dimm_params[i],
Haiying Wang80ad4012010-12-01 10:35:31 -0500502 dbw_capacity_adjust[i],
503 size_only);
Kumar Gala124b0822008-08-26 15:01:29 -0500504 }
505
506 default:
507 break;
508 }
509
York Sune8dc17b2012-08-17 08:22:39 +0000510 {
Kumar Gala124b0822008-08-26 15:01:29 -0500511 /*
512 * Compute the amount of memory available just by
513 * looking for the highest valid CSn_BNDS value.
514 * This allows us to also experiment with using
515 * only CS0 when using dual-rank DIMMs.
516 */
517 unsigned int max_end = 0;
518
519 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
520 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
521 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
522 if (reg->cs[j].config & 0x80000000) {
523 unsigned int end;
York Sunc21a7392013-06-25 11:37:45 -0700524 /*
525 * 0xfffffff is a special value we put
526 * for unused bnds
527 */
528 if (reg->cs[j].bnds == 0xffffffff)
529 continue;
530 end = reg->cs[j].bnds & 0xffff;
Kumar Gala124b0822008-08-26 15:01:29 -0500531 if (end > max_end) {
532 max_end = end;
533 }
534 }
535 }
536 }
537
Kumar Gala124b0822008-08-26 15:01:29 -0500538 total_mem = 1 + (((unsigned long long)max_end << 24ULL)
539 | 0xFFFFFFULL);
540 }
541
542 return total_mem;
543}
544
545/*
546 * fsl_ddr_sdram() -- this is the main function to be called by
547 * initdram() in the board file.
548 *
549 * It returns amount of memory configured in bytes.
550 */
551phys_size_t fsl_ddr_sdram(void)
552{
553 unsigned int i;
York Sun461c9392013-09-30 14:20:51 -0700554#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000555 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun461c9392013-09-30 14:20:51 -0700556#endif
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500557 unsigned long long total_memory;
Kumar Gala124b0822008-08-26 15:01:29 -0500558 fsl_ddr_info_t info;
York Sun5e155552013-06-25 11:37:48 -0700559 int deassert_reset;
Kumar Gala124b0822008-08-26 15:01:29 -0500560
561 /* Reset info structure. */
562 memset(&info, 0, sizeof(fsl_ddr_info_t));
563
564 /* Compute it once normally. */
York Sunbd495cf2011-09-16 13:21:35 -0700565#ifdef CONFIG_FSL_DDR_INTERACTIVE
James Yang43794382013-01-07 14:01:03 +0000566 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
567 total_memory = fsl_ddr_interactive(&info, 0);
568 } else if (fsl_ddr_interactive_env_var_exists()) {
569 total_memory = fsl_ddr_interactive(&info, 1);
York Sun5025a8d2013-01-04 08:13:59 +0000570 } else
York Sunbd495cf2011-09-16 13:21:35 -0700571#endif
572 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500573
York Sun016095d2012-10-08 07:44:24 +0000574 /* setup 3-way interleaving before enabling DDRC */
York Sunfcb60e82013-03-25 07:33:20 +0000575 if (info.memctl_opts[0].memctl_interleaving) {
576 switch (info.memctl_opts[0].memctl_interleaving_mode) {
577 case FSL_DDR_3WAY_1KB_INTERLEAVING:
578 case FSL_DDR_3WAY_4KB_INTERLEAVING:
579 case FSL_DDR_3WAY_8KB_INTERLEAVING:
580 fsl_ddr_set_intl3r(
581 info.memctl_opts[0].memctl_interleaving_mode);
582 break;
583 default:
584 break;
585 }
York Sun016095d2012-10-08 07:44:24 +0000586 }
587
York Sun5e155552013-06-25 11:37:48 -0700588 /*
589 * Program configuration registers.
590 * JEDEC specs requires clocks to be stable before deasserting reset
591 * for RDIMMs. Clocks start after chip select is enabled and clock
592 * control register is set. During step 1, all controllers have their
593 * registers set but not enabled. Step 2 proceeds after deasserting
594 * reset through board FPGA or GPIO.
595 * For non-registered DIMMs, initialization can go through but it is
596 * also OK to follow the same flow.
597 */
598 deassert_reset = board_need_mem_reset();
599 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
Priyanka Jain4a717412013-09-25 10:41:19 +0530600 if (info.common_timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700601 deassert_reset = 1;
602 }
Kumar Gala124b0822008-08-26 15:01:29 -0500603 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
604 debug("Programming controller %u\n", i);
605 if (info.common_timing_params[i].ndimms_present == 0) {
606 debug("No dimms present on controller %u; "
607 "skipping programming\n", i);
608 continue;
609 }
York Sun5e155552013-06-25 11:37:48 -0700610 /*
611 * The following call with step = 1 returns before enabling
612 * the controller. It has to finish with step = 2 later.
613 */
614 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i,
615 deassert_reset ? 1 : 0);
616 }
617 if (deassert_reset) {
618 /* Use board FPGA or GPIO to deassert reset signal */
619 debug("Deasserting mem reset\n");
620 board_deassert_mem_reset();
621 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
622 /* Call with step = 2 to continue initialization */
623 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]),
624 i, 2);
625 }
Kumar Gala124b0822008-08-26 15:01:29 -0500626 }
627
York Sun461c9392013-09-30 14:20:51 -0700628#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000629 /* program LAWs */
630 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
631 if (info.memctl_opts[i].memctl_interleaving) {
632 switch (info.memctl_opts[i].memctl_interleaving_mode) {
633 case FSL_DDR_CACHE_LINE_INTERLEAVING:
634 case FSL_DDR_PAGE_INTERLEAVING:
635 case FSL_DDR_BANK_INTERLEAVING:
636 case FSL_DDR_SUPERBANK_INTERLEAVING:
637 if (i == 0) {
638 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
639 fsl_ddr_set_lawbar(&info.common_timing_params[i],
640 law_memctl, i);
641 } else if (i == 2) {
642 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
643 fsl_ddr_set_lawbar(&info.common_timing_params[i],
644 law_memctl, i);
645 }
646 break;
647 case FSL_DDR_3WAY_1KB_INTERLEAVING:
648 case FSL_DDR_3WAY_4KB_INTERLEAVING:
649 case FSL_DDR_3WAY_8KB_INTERLEAVING:
650 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
651 if (i == 0) {
York Sune8dc17b2012-08-17 08:22:39 +0000652 fsl_ddr_set_lawbar(&info.common_timing_params[i],
653 law_memctl, i);
654 }
655 break;
656 case FSL_DDR_4WAY_1KB_INTERLEAVING:
657 case FSL_DDR_4WAY_4KB_INTERLEAVING:
658 case FSL_DDR_4WAY_8KB_INTERLEAVING:
659 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
660 if (i == 0)
661 fsl_ddr_set_lawbar(&info.common_timing_params[i],
662 law_memctl, i);
663 /* place holder for future 4-way interleaving */
664 break;
665 default:
666 break;
667 }
668 } else {
669 switch (i) {
670 case 0:
671 law_memctl = LAW_TRGT_IF_DDR_1;
672 break;
673 case 1:
674 law_memctl = LAW_TRGT_IF_DDR_2;
675 break;
676 case 2:
677 law_memctl = LAW_TRGT_IF_DDR_3;
678 break;
679 case 3:
680 law_memctl = LAW_TRGT_IF_DDR_4;
681 break;
682 default:
683 break;
684 }
Kumar Gala124b0822008-08-26 15:01:29 -0500685 fsl_ddr_set_lawbar(&info.common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000686 law_memctl, i);
Kumar Gala124b0822008-08-26 15:01:29 -0500687 }
688 }
York Sun461c9392013-09-30 14:20:51 -0700689#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500690
York Sune8dc17b2012-08-17 08:22:39 +0000691 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500692
693#if !defined(CONFIG_PHYS_64BIT)
694 /* Check for 4G or more. Bad. */
695 if (total_memory >= (1ull << 32)) {
Shruti Kanetkar3adfb912013-08-15 11:25:37 -0500696 puts("Detected ");
697 print_size(total_memory, " of memory\n");
Becky Bruce2d8ecac2010-12-17 17:17:59 -0600698 printf(" This U-Boot only supports < 4G of DDR\n");
699 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
700 printf(" "); /* re-align to match init_func_ram print */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500701 total_memory = CONFIG_MAX_MEM_MAPPED;
702 }
703#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500704
705 return total_memory;
706}
Haiying Wang80ad4012010-12-01 10:35:31 -0500707
708/*
709 * fsl_ddr_sdram_size() - This function only returns the size of the total
710 * memory without setting ddr control registers.
711 */
712phys_size_t
713fsl_ddr_sdram_size(void)
714{
715 fsl_ddr_info_t info;
716 unsigned long long total_memory = 0;
717
718 memset(&info, 0 , sizeof(fsl_ddr_info_t));
719
720 /* Compute it once normally. */
721 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
722
723 return total_memory;
724}