blob: ac0783b428e8bdb2b1637dddaf0b3b6f36ca86df [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Gala124b0822008-08-26 15:01:29 -05002/*
York Sun2896cb72014-03-27 17:54:47 -07003 * Copyright 2008-2014 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05004 */
5
6/*
7 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
8 * Based on code from spd_sdram.c
9 * Author: James Yang [at freescale.com]
10 */
11
12#include <common.h>
Kumar Galac68e86c2011-01-31 22:18:47 -060013#include <i2c.h>
York Sunf0626592013-09-30 09:22:09 -070014#include <fsl_ddr_sdram.h>
York Sunf0626592013-09-30 09:22:09 -070015#include <fsl_ddr.h>
Kumar Gala124b0822008-08-26 15:01:29 -050016
York Sun3a0916d2014-02-10 13:59:43 -080017/*
18 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
19 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
20 * all Power SoCs. But it could be different for ARM SoCs. For example,
21 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
22 * 0x00_8000_0000 ~ 0x00_ffff_ffff
23 * 0x80_8000_0000 ~ 0xff_ffff_ffff
24 */
25#ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
Mario Six805cac12019-01-21 09:18:16 +010026#ifdef CONFIG_MPC83xx
27#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
28#else
York Sun3a0916d2014-02-10 13:59:43 -080029#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
30#endif
Mario Six805cac12019-01-21 09:18:16 +010031#endif
York Sun3a0916d2014-02-10 13:59:43 -080032
York Sun461c9392013-09-30 14:20:51 -070033#ifdef CONFIG_PPC
34#include <asm/fsl_law.h>
35
York Sune8dc17b2012-08-17 08:22:39 +000036void fsl_ddr_set_lawbar(
Kumar Gala124b0822008-08-26 15:01:29 -050037 const common_timing_params_t *memctl_common_params,
38 unsigned int memctl_interleaved,
39 unsigned int ctrl_num);
York Sun461c9392013-09-30 14:20:51 -070040#endif
Kumar Gala124b0822008-08-26 15:01:29 -050041
York Sun461c9392013-09-30 14:20:51 -070042void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac68e86c2011-01-31 22:18:47 -060043#if defined(SPD_EEPROM_ADDRESS) || \
44 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
45 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
York Sunfe845072016-12-28 08:43:45 -080046#if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
47u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060048 [0][0] = SPD_EEPROM_ADDRESS,
49};
York Sunfe845072016-12-28 08:43:45 -080050#elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
51u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060052 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
York Sun92b46ac2011-08-26 11:32:41 -070053 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
54};
York Sunfe845072016-12-28 08:43:45 -080055#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
56u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sun92b46ac2011-08-26 11:32:41 -070057 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
Kumar Galac68e86c2011-01-31 22:18:47 -060058 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
59};
York Sunfe845072016-12-28 08:43:45 -080060#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
61u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060062 [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};
York Sunfe845072016-12-28 08:43:45 -080067#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
68u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000069 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
70 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
71 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
72};
York Sunfe845072016-12-28 08:43:45 -080073#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
74u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000075 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
76 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
77 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
78 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
79 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
80 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
81};
82
Kumar Galac68e86c2011-01-31 22:18:47 -060083#endif
84
York Sun2896cb72014-03-27 17:54:47 -070085#define SPD_SPA0_ADDRESS 0x36
86#define SPD_SPA1_ADDRESS 0x37
87
Kumar Galac68e86c2011-01-31 22:18:47 -060088static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
89{
Valentin Longchamp993967a2013-10-18 11:47:19 +020090 int ret;
York Sun2896cb72014-03-27 17:54:47 -070091#ifdef CONFIG_SYS_FSL_DDR4
92 uint8_t dummy = 0;
93#endif
Valentin Longchamp993967a2013-10-18 11:47:19 +020094
Chuanhua Han37c2c5e2019-07-10 21:00:20 +080095#ifndef CONFIG_DM_I2C
Valentin Longchamp993967a2013-10-18 11:47:19 +020096 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +080097#endif
98
Valentin Longchamp993967a2013-10-18 11:47:19 +020099
York Sun2896cb72014-03-27 17:54:47 -0700100#ifdef CONFIG_SYS_FSL_DDR4
101 /*
102 * DDR4 SPD has 384 to 512 bytes
103 * To access the lower 256 bytes, we need to set EE page address to 0
104 * To access the upper 256 bytes, we need to set EE page address to 1
105 * See Jedec standar No. 21-C for detail
106 */
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800107#ifndef CONFIG_DM_I2C
York Sun2896cb72014-03-27 17:54:47 -0700108 i2c_write(SPD_SPA0_ADDRESS, 0, 1, &dummy, 1);
109 ret = i2c_read(i2c_address, 0, 1, (uchar *)spd, 256);
110 if (!ret) {
111 i2c_write(SPD_SPA1_ADDRESS, 0, 1, &dummy, 1);
112 ret = i2c_read(i2c_address, 0, 1,
113 (uchar *)((ulong)spd + 256),
Masahiro Yamadadb204642014-11-07 03:03:31 +0900114 min(256,
115 (int)sizeof(generic_spd_eeprom_t) - 256));
York Sun2896cb72014-03-27 17:54:47 -0700116 }
117#else
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800118 struct udevice *dev;
119 int read_len = min(256, (int)sizeof(generic_spd_eeprom_t) - 256);
120
121 ret = i2c_get_chip_for_busnum(0, SPD_SPA0_ADDRESS, 1, &dev);
122 if (!ret)
123 dm_i2c_write(dev, 0, &dummy, 1);
124 ret = i2c_get_chip_for_busnum(0, i2c_address, 1, &dev);
125 if (!ret) {
126 if (!dm_i2c_read(dev, 0, (uchar *)spd, 256)) {
127 if (!i2c_get_chip_for_busnum(0, SPD_SPA1_ADDRESS,
128 1, &dev))
129 dm_i2c_write(dev, 0, &dummy, 1);
130 if (!i2c_get_chip_for_busnum(0, i2c_address, 1, &dev))
131 ret = dm_i2c_read(dev, 0,
132 (uchar *)((ulong)spd + 256),
133 read_len);
134 }
135 }
136#endif
137
138#else
139
140#ifndef CONFIG_DM_I2C
Valentin Longchamp993967a2013-10-18 11:47:19 +0200141 ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800142 sizeof(generic_spd_eeprom_t));
143#else
144 ret = i2c_get_chip_for_busnum(0, i2c_address, 1, &dev);
145 if (!ret)
146 ret = dm_i2c_read(dev, 0, (uchar *)spd,
147 sizeof(generic_spd_eeprom_t));
148#endif
149
York Sun2896cb72014-03-27 17:54:47 -0700150#endif
Kumar Galac68e86c2011-01-31 22:18:47 -0600151
152 if (ret) {
York Sun9b9372f2012-10-08 07:44:28 +0000153 if (i2c_address ==
154#ifdef SPD_EEPROM_ADDRESS
155 SPD_EEPROM_ADDRESS
156#elif defined(SPD_EEPROM_ADDRESS1)
157 SPD_EEPROM_ADDRESS1
158#endif
159 ) {
160 printf("DDR: failed to read SPD from address %u\n",
161 i2c_address);
162 } else {
163 debug("DDR: failed to read SPD from address %u\n",
164 i2c_address);
165 }
Kumar Galac68e86c2011-01-31 22:18:47 -0600166 memset(spd, 0, sizeof(generic_spd_eeprom_t));
167 }
168}
169
170__attribute__((weak, alias("__get_spd")))
171void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
172
York Sunb10d1b72015-05-28 14:54:08 +0530173/* This function allows boards to update SPD address */
174__weak void update_spd_address(unsigned int ctrl_num,
175 unsigned int slot,
176 unsigned int *addr)
177{
178}
179
Kumar Galac68e86c2011-01-31 22:18:47 -0600180void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700181 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600182{
183 unsigned int i;
184 unsigned int i2c_address = 0;
185
York Sunfe845072016-12-28 08:43:45 -0800186 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600187 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
188 return;
189 }
190
York Sun79a779b2014-08-01 15:51:00 -0700191 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600192 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb10d1b72015-05-28 14:54:08 +0530193 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac68e86c2011-01-31 22:18:47 -0600194 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
195 }
196}
197#else
198void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700199 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600200{
201}
202#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala124b0822008-08-26 15:01:29 -0500203
204/*
205 * ASSUMPTIONS:
206 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
207 * - Same memory data bus width on all controllers
208 *
209 * NOTES:
210 *
211 * The memory controller and associated documentation use confusing
212 * terminology when referring to the orgranization of DRAM.
213 *
214 * Here is a terminology translation table:
215 *
216 * memory controller/documention |industry |this code |signals
217 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200218 * physical bank/bank |rank |rank |chip select (CS)
219 * logical bank/sub-bank |bank |bank |bank address (BA)
220 * page/row |row |page |row address
221 * ??? |column |column |column address
Kumar Gala124b0822008-08-26 15:01:29 -0500222 *
223 * The naming confusion is further exacerbated by the descriptions of the
224 * memory controller interleaving feature, where accesses are interleaved
225 * _BETWEEN_ two seperate memory controllers. This is configured only in
226 * CS0_CONFIG[INTLV_CTL] of each memory controller.
227 *
228 * memory controller documentation | number of chip selects
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200229 * | per memory controller supported
Kumar Gala124b0822008-08-26 15:01:29 -0500230 * --------------------------------|-----------------------------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200231 * cache line interleaving | 1 (CS0 only)
232 * page interleaving | 1 (CS0 only)
233 * bank interleaving | 1 (CS0 only)
234 * superbank interleraving | depends on bank (chip select)
235 * | interleraving [rank interleaving]
236 * | mode used on every memory controller
Kumar Gala124b0822008-08-26 15:01:29 -0500237 *
238 * Even further confusing is the existence of the interleaving feature
239 * _WITHIN_ each memory controller. The feature is referred to in
240 * documentation as chip select interleaving or bank interleaving,
241 * although it is configured in the DDR_SDRAM_CFG field.
242 *
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200243 * Name of field | documentation name | this code
Kumar Gala124b0822008-08-26 15:01:29 -0500244 * -----------------------------|-----------------------|------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200245 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
246 * | interleaving
Kumar Gala124b0822008-08-26 15:01:29 -0500247 */
248
Kumar Gala124b0822008-08-26 15:01:29 -0500249const char *step_string_tbl[] = {
250 "STEP_GET_SPD",
251 "STEP_COMPUTE_DIMM_PARMS",
252 "STEP_COMPUTE_COMMON_PARMS",
253 "STEP_GATHER_OPTS",
254 "STEP_ASSIGN_ADDRESSES",
255 "STEP_COMPUTE_REGS",
256 "STEP_PROGRAM_REGS",
257 "STEP_ALL"
258};
259
260const char * step_to_string(unsigned int step) {
261
262 unsigned int s = __ilog2(step);
263
264 if ((1 << s) != step)
265 return step_string_tbl[7];
266
York Sunedbeee12014-04-01 14:20:49 -0700267 if (s >= ARRAY_SIZE(step_string_tbl)) {
268 printf("Error for the step in %s\n", __func__);
269 s = 0;
270 }
271
Kumar Gala124b0822008-08-26 15:01:29 -0500272 return step_string_tbl[s];
273}
Kumar Gala124b0822008-08-26 15:01:29 -0500274
York Sun01a82582013-03-25 07:39:35 +0000275static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Sune8dc17b2012-08-17 08:22:39 +0000276 unsigned int dbw_cap_adj[])
Kumar Gala124b0822008-08-26 15:01:29 -0500277{
York Sun79a779b2014-08-01 15:51:00 -0700278 unsigned int i, j;
York Sune8dc17b2012-08-17 08:22:39 +0000279 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
280 unsigned long long rank_density, ctlr_density = 0;
York Sun79a779b2014-08-01 15:51:00 -0700281 unsigned int first_ctrl = pinfo->first_ctrl;
282 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500283
284 /*
285 * If a reduced data width is requested, but the SPD
286 * specifies a physically wider device, adjust the
287 * computed dimm capacities accordingly before
288 * assigning addresses.
289 */
York Sun79a779b2014-08-01 15:51:00 -0700290 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500291 unsigned int found = 0;
292
293 switch (pinfo->memctl_opts[i].data_bus_width) {
294 case 2:
295 /* 16-bit */
York Sundd803dd2011-05-27 07:25:51 +0800296 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
297 unsigned int dw;
298 if (!pinfo->dimm_params[i][j].n_ranks)
299 continue;
300 dw = pinfo->dimm_params[i][j].primary_sdram_width;
301 if ((dw == 72 || dw == 64)) {
302 dbw_cap_adj[i] = 2;
303 break;
304 } else if ((dw == 40 || dw == 32)) {
305 dbw_cap_adj[i] = 1;
306 break;
307 }
308 }
Kumar Gala124b0822008-08-26 15:01:29 -0500309 break;
310
311 case 1:
312 /* 32-bit */
313 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
314 unsigned int dw;
315 dw = pinfo->dimm_params[i][j].data_width;
316 if (pinfo->dimm_params[i][j].n_ranks
317 && (dw == 72 || dw == 64)) {
318 /*
319 * FIXME: can't really do it
320 * like this because this just
321 * further reduces the memory
322 */
323 found = 1;
324 break;
325 }
326 }
327 if (found) {
328 dbw_cap_adj[i] = 1;
329 }
330 break;
331
332 case 0:
333 /* 64-bit */
334 break;
335
336 default:
337 printf("unexpected data bus width "
338 "specified controller %u\n", i);
339 return 1;
340 }
York Sune8dc17b2012-08-17 08:22:39 +0000341 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala124b0822008-08-26 15:01:29 -0500342 }
343
York Sun79a779b2014-08-01 15:51:00 -0700344 current_mem_base = pinfo->mem_base;
York Sune8dc17b2012-08-17 08:22:39 +0000345 total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700346 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
347 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
348 dbw_cap_adj[first_ctrl];
349 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
York Sune8dc17b2012-08-17 08:22:39 +0000350 FSL_DDR_CS0_CS1_CS2_CS3) {
351 case FSL_DDR_CS0_CS1_CS2_CS3:
352 ctlr_density = 4 * rank_density;
353 break;
354 case FSL_DDR_CS0_CS1:
355 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
356 ctlr_density = 2 * rank_density;
357 break;
358 case FSL_DDR_CS2_CS3:
359 default:
360 ctlr_density = rank_density;
361 break;
362 }
363 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
364 rank_density, ctlr_density);
York Sun79a779b2014-08-01 15:51:00 -0700365 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000366 if (pinfo->memctl_opts[i].memctl_interleaving) {
367 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sunc459ae62014-02-10 13:59:44 -0800368 case FSL_DDR_256B_INTERLEAVING:
York Sune8dc17b2012-08-17 08:22:39 +0000369 case FSL_DDR_CACHE_LINE_INTERLEAVING:
370 case FSL_DDR_PAGE_INTERLEAVING:
371 case FSL_DDR_BANK_INTERLEAVING:
372 case FSL_DDR_SUPERBANK_INTERLEAVING:
373 total_ctlr_mem = 2 * ctlr_density;
374 break;
375 case FSL_DDR_3WAY_1KB_INTERLEAVING:
376 case FSL_DDR_3WAY_4KB_INTERLEAVING:
377 case FSL_DDR_3WAY_8KB_INTERLEAVING:
378 total_ctlr_mem = 3 * ctlr_density;
379 break;
380 case FSL_DDR_4WAY_1KB_INTERLEAVING:
381 case FSL_DDR_4WAY_4KB_INTERLEAVING:
382 case FSL_DDR_4WAY_8KB_INTERLEAVING:
383 total_ctlr_mem = 4 * ctlr_density;
384 break;
385 default:
386 panic("Unknown interleaving mode");
387 }
388 pinfo->common_timing_params[i].base_address =
389 current_mem_base;
390 pinfo->common_timing_params[i].total_mem =
391 total_ctlr_mem;
392 total_mem = current_mem_base + total_ctlr_mem;
393 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
394 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
395 } else {
396 /* when 3rd controller not interleaved */
397 current_mem_base = total_mem;
398 total_ctlr_mem = 0;
399 pinfo->common_timing_params[i].base_address =
400 current_mem_base;
401 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
402 unsigned long long cap =
403 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
404 pinfo->dimm_params[i][j].base_address =
405 current_mem_base;
406 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
407 current_mem_base += cap;
408 total_ctlr_mem += cap;
409 }
410 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
411 pinfo->common_timing_params[i].total_mem =
412 total_ctlr_mem;
413 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500414 }
415 }
416 } else {
417 /*
418 * Simple linear assignment if memory
419 * controllers are not interleaved.
420 */
York Sun79a779b2014-08-01 15:51:00 -0700421 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000422 total_ctlr_mem = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500423 pinfo->common_timing_params[i].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000424 current_mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500425 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
426 /* Compute DIMM base addresses. */
427 unsigned long long cap =
York Sune8dc17b2012-08-17 08:22:39 +0000428 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala124b0822008-08-26 15:01:29 -0500429 pinfo->dimm_params[i][j].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000430 current_mem_base;
431 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
432 current_mem_base += cap;
433 total_ctlr_mem += cap;
Kumar Gala124b0822008-08-26 15:01:29 -0500434 }
York Sune8dc17b2012-08-17 08:22:39 +0000435 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500436 pinfo->common_timing_params[i].total_mem =
York Sune8dc17b2012-08-17 08:22:39 +0000437 total_ctlr_mem;
438 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500439 }
440 }
York Sune8dc17b2012-08-17 08:22:39 +0000441 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500442
York Sune8dc17b2012-08-17 08:22:39 +0000443 return total_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500444}
445
York Sun01a82582013-03-25 07:39:35 +0000446/* Use weak function to allow board file to override the address assignment */
447__attribute__((weak, alias("__step_assign_addresses")))
448unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
449 unsigned int dbw_cap_adj[]);
450
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500451unsigned long long
Haiying Wang80ad4012010-12-01 10:35:31 -0500452fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
453 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -0500454{
455 unsigned int i, j;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500456 unsigned long long total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700457 int assert_reset = 0;
458 unsigned int first_ctrl = pinfo->first_ctrl;
459 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
460 __maybe_unused int retval;
461 __maybe_unused bool goodspd = false;
462 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
Kumar Gala124b0822008-08-26 15:01:29 -0500463
464 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
465 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun79a779b2014-08-01 15:51:00 -0700466 if (pinfo->board_need_mem_reset)
467 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala124b0822008-08-26 15:01:29 -0500468
469 /* data bus width capacity adjust shift amount */
York Sunfe845072016-12-28 08:43:45 -0800470 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala124b0822008-08-26 15:01:29 -0500471
York Sun79a779b2014-08-01 15:51:00 -0700472 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala124b0822008-08-26 15:01:29 -0500473 dbw_capacity_adjust[i] = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500474
475 debug("starting at step %u (%s)\n",
476 start_step, step_to_string(start_step));
477
478 switch (start_step) {
479 case STEP_GET_SPD:
York Sune73cc042011-06-07 09:42:16 +0800480#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala124b0822008-08-26 15:01:29 -0500481 /* STEP 1: Gather all DIMM SPD data */
York Sun79a779b2014-08-01 15:51:00 -0700482 for (i = first_ctrl; i <= last_ctrl; i++) {
483 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
484 dimm_slots_per_ctrl);
Kumar Gala124b0822008-08-26 15:01:29 -0500485 }
486
487 case STEP_COMPUTE_DIMM_PARMS:
488 /* STEP 2: Compute DIMM parameters from SPD data */
489
York Sun79a779b2014-08-01 15:51:00 -0700490 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500491 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500492 generic_spd_eeprom_t *spd =
493 &(pinfo->spd_installed_dimms[i][j]);
494 dimm_params_t *pdimm =
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200495 &(pinfo->dimm_params[i][j]);
York Sun2c0b62d2015-01-06 13:18:50 -0800496 retval = compute_dimm_parameters(
497 i, spd, pdimm, j);
York Sun09d8aa82011-06-07 09:42:17 +0800498#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun55eb5fa2015-03-19 09:30:26 -0700499 if (!j && retval) {
York Sune8dc17b2012-08-17 08:22:39 +0000500 printf("SPD error on controller %d! "
501 "Trying fallback to raw timing "
502 "calculation\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700503 retval = fsl_ddr_get_dimm_params(pdimm,
504 i, j);
York Sun09d8aa82011-06-07 09:42:17 +0800505 }
506#else
Kumar Gala124b0822008-08-26 15:01:29 -0500507 if (retval == 2) {
508 printf("Error: compute_dimm_parameters"
509 " non-zero returned FATAL value "
510 "for memctl=%u dimm=%u\n", i, j);
511 return 0;
512 }
York Sun09d8aa82011-06-07 09:42:17 +0800513#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500514 if (retval) {
515 debug("Warning: compute_dimm_parameters"
516 " non-zero return value for memctl=%u "
517 "dimm=%u\n", i, j);
York Sun79a779b2014-08-01 15:51:00 -0700518 } else {
519 goodspd = true;
Kumar Gala124b0822008-08-26 15:01:29 -0500520 }
521 }
522 }
York Sun79a779b2014-08-01 15:51:00 -0700523 if (!goodspd) {
524 /*
525 * No valid SPD found
526 * Throw an error if this is for main memory, i.e.
527 * first_ctrl == 0. Otherwise, siliently return 0
528 * as the memory size.
529 */
530 if (first_ctrl == 0)
531 printf("Error: No valid SPD detected.\n");
Kumar Gala124b0822008-08-26 15:01:29 -0500532
York Sun79a779b2014-08-01 15:51:00 -0700533 return 0;
534 }
Shaohui Xie24009042012-06-28 23:36:38 +0000535#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sune73cc042011-06-07 09:42:16 +0800536 case STEP_COMPUTE_DIMM_PARMS:
York Sun79a779b2014-08-01 15:51:00 -0700537 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune73cc042011-06-07 09:42:16 +0800538 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
539 dimm_params_t *pdimm =
540 &(pinfo->dimm_params[i][j]);
541 fsl_ddr_get_dimm_params(pdimm, i, j);
542 }
543 }
544 debug("Filling dimm parameters from board specific file\n");
545#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500546 case STEP_COMPUTE_COMMON_PARMS:
547 /*
548 * STEP 3: Compute a common set of timing parameters
549 * suitable for all of the DIMMs on each memory controller
550 */
York Sun79a779b2014-08-01 15:51:00 -0700551 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500552 debug("Computing lowest common DIMM"
553 " parameters for memctl=%u\n", i);
York Sun2c0b62d2015-01-06 13:18:50 -0800554 compute_lowest_common_dimm_parameters
555 (i,
556 pinfo->dimm_params[i],
557 &timing_params[i],
558 CONFIG_DIMM_SLOTS_PER_CTLR);
Kumar Gala124b0822008-08-26 15:01:29 -0500559 }
560
561 case STEP_GATHER_OPTS:
562 /* STEP 4: Gather configuration requirements from user */
York Sun79a779b2014-08-01 15:51:00 -0700563 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500564 debug("Reloading memory controller "
565 "configuration options for memctl=%u\n", i);
566 /*
567 * This "reloads" the memory controller options
568 * to defaults. If the user "edits" an option,
569 * next_step points to the step after this,
570 * which is currently STEP_ASSIGN_ADDRESSES.
571 */
572 populate_memctl_options(
York Sun999273f2015-07-23 14:04:48 -0700573 &timing_params[i],
Haiying Wangfa440362008-10-03 12:36:55 -0400574 &pinfo->memctl_opts[i],
575 pinfo->dimm_params[i], i);
York Sun5e155552013-06-25 11:37:48 -0700576 /*
577 * For RDIMMs, JEDEC spec requires clocks to be stable
578 * before reset signal is deasserted. For the boards
579 * using fixed parameters, this function should be
580 * be called from board init file.
581 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530582 if (timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700583 assert_reset = 1;
584 }
York Sun79a779b2014-08-01 15:51:00 -0700585 if (assert_reset && !size_only) {
586 if (pinfo->board_mem_reset) {
587 debug("Asserting mem reset\n");
588 pinfo->board_mem_reset();
589 } else {
590 debug("Asserting mem reset missing\n");
591 }
Kumar Gala124b0822008-08-26 15:01:29 -0500592 }
York Sun5e155552013-06-25 11:37:48 -0700593
Kumar Gala124b0822008-08-26 15:01:29 -0500594 case STEP_ASSIGN_ADDRESSES:
595 /* STEP 5: Assign addresses to chip selects */
York Sune8dc17b2012-08-17 08:22:39 +0000596 check_interleaving_options(pinfo);
597 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sunedbeee12014-04-01 14:20:49 -0700598 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500599
600 case STEP_COMPUTE_REGS:
601 /* STEP 6: compute controller register values */
York Sune8dc17b2012-08-17 08:22:39 +0000602 debug("FSL Memory ctrl register computation\n");
York Sun79a779b2014-08-01 15:51:00 -0700603 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500604 if (timing_params[i].ndimms_present == 0) {
605 memset(&ddr_reg[i], 0,
606 sizeof(fsl_ddr_cfg_regs_t));
607 continue;
608 }
609
York Sun2c0b62d2015-01-06 13:18:50 -0800610 compute_fsl_memctl_config_regs
611 (i,
612 &pinfo->memctl_opts[i],
613 &ddr_reg[i], &timing_params[i],
614 pinfo->dimm_params[i],
615 dbw_capacity_adjust[i],
616 size_only);
Kumar Gala124b0822008-08-26 15:01:29 -0500617 }
618
619 default:
620 break;
621 }
622
York Sune8dc17b2012-08-17 08:22:39 +0000623 {
Kumar Gala124b0822008-08-26 15:01:29 -0500624 /*
625 * Compute the amount of memory available just by
626 * looking for the highest valid CSn_BNDS value.
627 * This allows us to also experiment with using
628 * only CS0 when using dual-rank DIMMs.
629 */
630 unsigned int max_end = 0;
631
York Sun79a779b2014-08-01 15:51:00 -0700632 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500633 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
634 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
635 if (reg->cs[j].config & 0x80000000) {
636 unsigned int end;
York Sunc21a7392013-06-25 11:37:45 -0700637 /*
638 * 0xfffffff is a special value we put
639 * for unused bnds
640 */
641 if (reg->cs[j].bnds == 0xffffffff)
642 continue;
643 end = reg->cs[j].bnds & 0xffff;
Kumar Gala124b0822008-08-26 15:01:29 -0500644 if (end > max_end) {
645 max_end = end;
646 }
647 }
648 }
649 }
650
York Sun6446f1e2013-10-28 16:36:02 -0700651 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun79a779b2014-08-01 15:51:00 -0700652 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500653 }
654
655 return total_mem;
656}
657
York Sun79a779b2014-08-01 15:51:00 -0700658phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala124b0822008-08-26 15:01:29 -0500659{
York Sun79a779b2014-08-01 15:51:00 -0700660 unsigned int i, first_ctrl, last_ctrl;
York Sun461c9392013-09-30 14:20:51 -0700661#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000662 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun461c9392013-09-30 14:20:51 -0700663#endif
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500664 unsigned long long total_memory;
York Sun79a779b2014-08-01 15:51:00 -0700665 int deassert_reset = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500666
York Sun79a779b2014-08-01 15:51:00 -0700667 first_ctrl = pinfo->first_ctrl;
668 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500669
670 /* Compute it once normally. */
York Sunbd495cf2011-09-16 13:21:35 -0700671#ifdef CONFIG_FSL_DDR_INTERACTIVE
James Yang43794382013-01-07 14:01:03 +0000672 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
York Sun79a779b2014-08-01 15:51:00 -0700673 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yang43794382013-01-07 14:01:03 +0000674 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun79a779b2014-08-01 15:51:00 -0700675 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sun5025a8d2013-01-04 08:13:59 +0000676 } else
York Sunbd495cf2011-09-16 13:21:35 -0700677#endif
York Sun79a779b2014-08-01 15:51:00 -0700678 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500679
York Sun016095d2012-10-08 07:44:24 +0000680 /* setup 3-way interleaving before enabling DDRC */
York Sun79a779b2014-08-01 15:51:00 -0700681 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
682 case FSL_DDR_3WAY_1KB_INTERLEAVING:
683 case FSL_DDR_3WAY_4KB_INTERLEAVING:
684 case FSL_DDR_3WAY_8KB_INTERLEAVING:
685 fsl_ddr_set_intl3r(
686 pinfo->memctl_opts[first_ctrl].
687 memctl_interleaving_mode);
688 break;
689 default:
690 break;
York Sun016095d2012-10-08 07:44:24 +0000691 }
692
York Sun5e155552013-06-25 11:37:48 -0700693 /*
694 * Program configuration registers.
695 * JEDEC specs requires clocks to be stable before deasserting reset
696 * for RDIMMs. Clocks start after chip select is enabled and clock
697 * control register is set. During step 1, all controllers have their
698 * registers set but not enabled. Step 2 proceeds after deasserting
699 * reset through board FPGA or GPIO.
700 * For non-registered DIMMs, initialization can go through but it is
701 * also OK to follow the same flow.
702 */
York Sun79a779b2014-08-01 15:51:00 -0700703 if (pinfo->board_need_mem_reset)
704 deassert_reset = pinfo->board_need_mem_reset();
705 for (i = first_ctrl; i <= last_ctrl; i++) {
706 if (pinfo->common_timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700707 deassert_reset = 1;
708 }
York Sun79a779b2014-08-01 15:51:00 -0700709 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500710 debug("Programming controller %u\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700711 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -0500712 debug("No dimms present on controller %u; "
713 "skipping programming\n", i);
714 continue;
715 }
York Sun5e155552013-06-25 11:37:48 -0700716 /*
717 * The following call with step = 1 returns before enabling
718 * the controller. It has to finish with step = 2 later.
719 */
York Sun79a779b2014-08-01 15:51:00 -0700720 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sun5e155552013-06-25 11:37:48 -0700721 deassert_reset ? 1 : 0);
722 }
723 if (deassert_reset) {
724 /* Use board FPGA or GPIO to deassert reset signal */
York Sun79a779b2014-08-01 15:51:00 -0700725 if (pinfo->board_mem_de_reset) {
726 debug("Deasserting mem reset\n");
727 pinfo->board_mem_de_reset();
728 } else {
729 debug("Deasserting mem reset missing\n");
730 }
731 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun5e155552013-06-25 11:37:48 -0700732 /* Call with step = 2 to continue initialization */
York Sun79a779b2014-08-01 15:51:00 -0700733 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sun5e155552013-06-25 11:37:48 -0700734 i, 2);
735 }
Kumar Gala124b0822008-08-26 15:01:29 -0500736 }
737
York Sun8ced0502015-01-06 13:18:55 -0800738#ifdef CONFIG_FSL_DDR_SYNC_REFRESH
739 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
740#endif
741
York Sun461c9392013-09-30 14:20:51 -0700742#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000743 /* program LAWs */
York Sun79a779b2014-08-01 15:51:00 -0700744 for (i = first_ctrl; i <= last_ctrl; i++) {
745 if (pinfo->memctl_opts[i].memctl_interleaving) {
746 switch (pinfo->memctl_opts[i].
747 memctl_interleaving_mode) {
York Sune8dc17b2012-08-17 08:22:39 +0000748 case FSL_DDR_CACHE_LINE_INTERLEAVING:
749 case FSL_DDR_PAGE_INTERLEAVING:
750 case FSL_DDR_BANK_INTERLEAVING:
751 case FSL_DDR_SUPERBANK_INTERLEAVING:
York Sun79a779b2014-08-01 15:51:00 -0700752 if (i % 2)
753 break;
York Sune8dc17b2012-08-17 08:22:39 +0000754 if (i == 0) {
755 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun79a779b2014-08-01 15:51:00 -0700756 fsl_ddr_set_lawbar(
757 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000758 law_memctl, i);
York Sun79a779b2014-08-01 15:51:00 -0700759 }
York Sunfe845072016-12-28 08:43:45 -0800760#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun79a779b2014-08-01 15:51:00 -0700761 else if (i == 2) {
York Sune8dc17b2012-08-17 08:22:39 +0000762 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
York Sun79a779b2014-08-01 15:51:00 -0700763 fsl_ddr_set_lawbar(
764 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000765 law_memctl, i);
766 }
York Sun79a779b2014-08-01 15:51:00 -0700767#endif
York Sune8dc17b2012-08-17 08:22:39 +0000768 break;
769 case FSL_DDR_3WAY_1KB_INTERLEAVING:
770 case FSL_DDR_3WAY_4KB_INTERLEAVING:
771 case FSL_DDR_3WAY_8KB_INTERLEAVING:
772 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
773 if (i == 0) {
York Sun79a779b2014-08-01 15:51:00 -0700774 fsl_ddr_set_lawbar(
775 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000776 law_memctl, i);
777 }
778 break;
779 case FSL_DDR_4WAY_1KB_INTERLEAVING:
780 case FSL_DDR_4WAY_4KB_INTERLEAVING:
781 case FSL_DDR_4WAY_8KB_INTERLEAVING:
782 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
783 if (i == 0)
York Sun79a779b2014-08-01 15:51:00 -0700784 fsl_ddr_set_lawbar(
785 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000786 law_memctl, i);
787 /* place holder for future 4-way interleaving */
788 break;
789 default:
790 break;
791 }
792 } else {
793 switch (i) {
794 case 0:
795 law_memctl = LAW_TRGT_IF_DDR_1;
796 break;
797 case 1:
798 law_memctl = LAW_TRGT_IF_DDR_2;
799 break;
800 case 2:
801 law_memctl = LAW_TRGT_IF_DDR_3;
802 break;
803 case 3:
804 law_memctl = LAW_TRGT_IF_DDR_4;
805 break;
806 default:
807 break;
808 }
York Sun79a779b2014-08-01 15:51:00 -0700809 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
810 law_memctl, i);
Kumar Gala124b0822008-08-26 15:01:29 -0500811 }
812 }
York Sun461c9392013-09-30 14:20:51 -0700813#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500814
York Sune8dc17b2012-08-17 08:22:39 +0000815 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500816
817#if !defined(CONFIG_PHYS_64BIT)
818 /* Check for 4G or more. Bad. */
York Sun79a779b2014-08-01 15:51:00 -0700819 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
Shruti Kanetkar3adfb912013-08-15 11:25:37 -0500820 puts("Detected ");
821 print_size(total_memory, " of memory\n");
Becky Bruce2d8ecac2010-12-17 17:17:59 -0600822 printf(" This U-Boot only supports < 4G of DDR\n");
823 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
Simon Glassd35f3382017-04-06 12:47:05 -0600824 printf(" "); /* re-align to match init_dram print */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500825 total_memory = CONFIG_MAX_MEM_MAPPED;
826 }
827#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500828
829 return total_memory;
830}
Haiying Wang80ad4012010-12-01 10:35:31 -0500831
832/*
York Sun79a779b2014-08-01 15:51:00 -0700833 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassd35f3382017-04-06 12:47:05 -0600834 * called by dram_init() in the board file.
York Sun79a779b2014-08-01 15:51:00 -0700835 *
836 * It returns amount of memory configured in bytes.
837 */
838phys_size_t fsl_ddr_sdram(void)
839{
840 fsl_ddr_info_t info;
841
842 /* Reset info structure. */
843 memset(&info, 0, sizeof(fsl_ddr_info_t));
844 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
845 info.first_ctrl = 0;
846 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
847 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
848 info.board_need_mem_reset = board_need_mem_reset;
849 info.board_mem_reset = board_assert_mem_reset;
850 info.board_mem_de_reset = board_deassert_mem_reset;
York Sund957a672015-11-04 09:53:10 -0800851 remove_unused_controllers(&info);
York Sun79a779b2014-08-01 15:51:00 -0700852
853 return __fsl_ddr_sdram(&info);
854}
855
856#ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
857phys_size_t fsl_other_ddr_sdram(unsigned long long base,
858 unsigned int first_ctrl,
859 unsigned int num_ctrls,
860 unsigned int dimm_slots_per_ctrl,
861 int (*board_need_reset)(void),
862 void (*board_reset)(void),
863 void (*board_de_reset)(void))
864{
865 fsl_ddr_info_t info;
866
867 /* Reset info structure. */
868 memset(&info, 0, sizeof(fsl_ddr_info_t));
869 info.mem_base = base;
870 info.first_ctrl = first_ctrl;
871 info.num_ctrls = num_ctrls;
872 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
873 info.board_need_mem_reset = board_need_reset;
874 info.board_mem_reset = board_reset;
875 info.board_mem_de_reset = board_de_reset;
876
877 return __fsl_ddr_sdram(&info);
878}
879#endif
880
881/*
882 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
883 * size of the total memory without setting ddr control registers.
Haiying Wang80ad4012010-12-01 10:35:31 -0500884 */
885phys_size_t
886fsl_ddr_sdram_size(void)
887{
888 fsl_ddr_info_t info;
889 unsigned long long total_memory = 0;
890
891 memset(&info, 0 , sizeof(fsl_ddr_info_t));
York Sun79a779b2014-08-01 15:51:00 -0700892 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
893 info.first_ctrl = 0;
894 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
895 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
896 info.board_need_mem_reset = NULL;
Ed Swarthout7ae7a0e2016-01-14 12:28:04 -0600897 remove_unused_controllers(&info);
Haiying Wang80ad4012010-12-01 10:35:31 -0500898
899 /* Compute it once normally. */
900 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
901
902 return total_memory;
903}