blob: 8533b28c258295a944438c1fe471ca812041b7cd [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>
Heiko Schocher375038f2019-08-26 17:28:34 +020013#include <dm.h>
Kumar Galac68e86c2011-01-31 22:18:47 -060014#include <i2c.h>
York Sunf0626592013-09-30 09:22:09 -070015#include <fsl_ddr_sdram.h>
York Sunf0626592013-09-30 09:22:09 -070016#include <fsl_ddr.h>
Simon Glass97589732020-05-10 11:40:02 -060017#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Kumar Gala124b0822008-08-26 15:01:29 -050019
York Sun3a0916d2014-02-10 13:59:43 -080020/*
21 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
22 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
23 * all Power SoCs. But it could be different for ARM SoCs. For example,
24 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
25 * 0x00_8000_0000 ~ 0x00_ffff_ffff
26 * 0x80_8000_0000 ~ 0xff_ffff_ffff
27 */
28#ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
Mario Six805cac12019-01-21 09:18:16 +010029#ifdef CONFIG_MPC83xx
30#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
31#else
York Sun3a0916d2014-02-10 13:59:43 -080032#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
33#endif
Mario Six805cac12019-01-21 09:18:16 +010034#endif
York Sun3a0916d2014-02-10 13:59:43 -080035
York Sun461c9392013-09-30 14:20:51 -070036#ifdef CONFIG_PPC
37#include <asm/fsl_law.h>
38
York Sune8dc17b2012-08-17 08:22:39 +000039void fsl_ddr_set_lawbar(
Kumar Gala124b0822008-08-26 15:01:29 -050040 const common_timing_params_t *memctl_common_params,
41 unsigned int memctl_interleaved,
42 unsigned int ctrl_num);
York Sun461c9392013-09-30 14:20:51 -070043#endif
Kumar Gala124b0822008-08-26 15:01:29 -050044
York Sun461c9392013-09-30 14:20:51 -070045void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac68e86c2011-01-31 22:18:47 -060046#if defined(SPD_EEPROM_ADDRESS) || \
47 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
48 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
York Sunfe845072016-12-28 08:43:45 -080049#if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
50u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060051 [0][0] = SPD_EEPROM_ADDRESS,
52};
York Sunfe845072016-12-28 08:43:45 -080053#elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
54u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060055 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
York Sun92b46ac2011-08-26 11:32:41 -070056 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
57};
York Sunfe845072016-12-28 08:43:45 -080058#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
59u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sun92b46ac2011-08-26 11:32:41 -070060 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
Kumar Galac68e86c2011-01-31 22:18:47 -060061 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
62};
York Sunfe845072016-12-28 08:43:45 -080063#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
64u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060065 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
66 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
67 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
68 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
69};
York Sunfe845072016-12-28 08:43:45 -080070#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
71u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000072 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
73 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
74 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
75};
York Sunfe845072016-12-28 08:43:45 -080076#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
77u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000078 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
79 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
80 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
81 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
82 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
83 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
84};
85
Kumar Galac68e86c2011-01-31 22:18:47 -060086#endif
87
Heiko Schocher375038f2019-08-26 17:28:34 +020088#if defined(CONFIG_DM_I2C)
89#define DEV_TYPE struct udevice
90#else
91/* Local udevice */
92struct ludevice {
93 u8 chip;
94};
95
96#define DEV_TYPE struct ludevice
97
98#endif
99
York Sun2896cb72014-03-27 17:54:47 -0700100#define SPD_SPA0_ADDRESS 0x36
101#define SPD_SPA1_ADDRESS 0x37
102
Heiko Schocher375038f2019-08-26 17:28:34 +0200103static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
104 int alen, uint8_t *buf, int len)
Kumar Galac68e86c2011-01-31 22:18:47 -0600105{
Valentin Longchamp993967a2013-10-18 11:47:19 +0200106 int ret;
Heiko Schocher375038f2019-08-26 17:28:34 +0200107
108#ifdef CONFIG_DM_I2C
109 ret = dm_i2c_read(dev, 0, buf, len);
110#else
111 ret = i2c_read(dev->chip, addr, alen, buf, len);
112#endif
113
114 return ret;
115}
116
York Sun2896cb72014-03-27 17:54:47 -0700117#ifdef CONFIG_SYS_FSL_DDR4
Heiko Schocher375038f2019-08-26 17:28:34 +0200118static int ddr_i2c_dummy_write(unsigned int chip_addr)
119{
120 uint8_t buf = 0;
121
122#ifdef CONFIG_DM_I2C
123 struct udevice *dev;
124 int ret;
125
126 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
127 1, &dev);
128 if (ret) {
129 printf("%s: Cannot find udev for a bus %d\n", __func__,
130 CONFIG_SYS_SPD_BUS_NUM);
131 return ret;
132 }
133
134 return dm_i2c_write(dev, 0, &buf, 1);
135#else
136 return i2c_write(chip_addr, 0, 1, &buf, 1);
York Sun2896cb72014-03-27 17:54:47 -0700137#endif
Valentin Longchamp993967a2013-10-18 11:47:19 +0200138
Heiko Schocher375038f2019-08-26 17:28:34 +0200139 return 0;
140}
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800141#endif
142
Heiko Schocher375038f2019-08-26 17:28:34 +0200143static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
144{
145 int ret;
146 DEV_TYPE *dev;
147
148#if defined(CONFIG_DM_I2C)
149 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
150 1, &dev);
151 if (ret) {
152 printf("%s: Cannot find udev for a bus %d\n", __func__,
153 CONFIG_SYS_SPD_BUS_NUM);
154 return;
155 }
156#else /* Non DM I2C support - will be removed */
157 struct ludevice ldev = {
158 .chip = i2c_address,
159 };
160 dev = &ldev;
161
162 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
163#endif
Valentin Longchamp993967a2013-10-18 11:47:19 +0200164
York Sun2896cb72014-03-27 17:54:47 -0700165#ifdef CONFIG_SYS_FSL_DDR4
166 /*
167 * DDR4 SPD has 384 to 512 bytes
168 * To access the lower 256 bytes, we need to set EE page address to 0
169 * To access the upper 256 bytes, we need to set EE page address to 1
170 * See Jedec standar No. 21-C for detail
171 */
Heiko Schocher375038f2019-08-26 17:28:34 +0200172 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
173 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
York Sun2896cb72014-03-27 17:54:47 -0700174 if (!ret) {
Heiko Schocher375038f2019-08-26 17:28:34 +0200175 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
176 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
177 min(256,
178 (int)sizeof(generic_spd_eeprom_t)
179 - 256));
York Sun2896cb72014-03-27 17:54:47 -0700180 }
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800181
182#else
Heiko Schocher375038f2019-08-26 17:28:34 +0200183 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
184 sizeof(generic_spd_eeprom_t));
York Sun2896cb72014-03-27 17:54:47 -0700185#endif
Kumar Galac68e86c2011-01-31 22:18:47 -0600186
187 if (ret) {
York Sun9b9372f2012-10-08 07:44:28 +0000188 if (i2c_address ==
189#ifdef SPD_EEPROM_ADDRESS
190 SPD_EEPROM_ADDRESS
191#elif defined(SPD_EEPROM_ADDRESS1)
192 SPD_EEPROM_ADDRESS1
193#endif
194 ) {
195 printf("DDR: failed to read SPD from address %u\n",
196 i2c_address);
197 } else {
198 debug("DDR: failed to read SPD from address %u\n",
199 i2c_address);
200 }
Kumar Galac68e86c2011-01-31 22:18:47 -0600201 memset(spd, 0, sizeof(generic_spd_eeprom_t));
202 }
203}
204
205__attribute__((weak, alias("__get_spd")))
206void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
207
York Sunb10d1b72015-05-28 14:54:08 +0530208/* This function allows boards to update SPD address */
209__weak void update_spd_address(unsigned int ctrl_num,
210 unsigned int slot,
211 unsigned int *addr)
212{
213}
214
Kumar Galac68e86c2011-01-31 22:18:47 -0600215void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700216 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600217{
218 unsigned int i;
219 unsigned int i2c_address = 0;
220
York Sunfe845072016-12-28 08:43:45 -0800221 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600222 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
223 return;
224 }
225
York Sun79a779b2014-08-01 15:51:00 -0700226 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600227 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb10d1b72015-05-28 14:54:08 +0530228 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac68e86c2011-01-31 22:18:47 -0600229 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
230 }
231}
232#else
233void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700234 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600235{
236}
237#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala124b0822008-08-26 15:01:29 -0500238
239/*
240 * ASSUMPTIONS:
241 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
242 * - Same memory data bus width on all controllers
243 *
244 * NOTES:
245 *
246 * The memory controller and associated documentation use confusing
247 * terminology when referring to the orgranization of DRAM.
248 *
249 * Here is a terminology translation table:
250 *
251 * memory controller/documention |industry |this code |signals
252 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200253 * physical bank/bank |rank |rank |chip select (CS)
254 * logical bank/sub-bank |bank |bank |bank address (BA)
255 * page/row |row |page |row address
256 * ??? |column |column |column address
Kumar Gala124b0822008-08-26 15:01:29 -0500257 *
258 * The naming confusion is further exacerbated by the descriptions of the
259 * memory controller interleaving feature, where accesses are interleaved
260 * _BETWEEN_ two seperate memory controllers. This is configured only in
261 * CS0_CONFIG[INTLV_CTL] of each memory controller.
262 *
263 * memory controller documentation | number of chip selects
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200264 * | per memory controller supported
Kumar Gala124b0822008-08-26 15:01:29 -0500265 * --------------------------------|-----------------------------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200266 * cache line interleaving | 1 (CS0 only)
267 * page interleaving | 1 (CS0 only)
268 * bank interleaving | 1 (CS0 only)
269 * superbank interleraving | depends on bank (chip select)
270 * | interleraving [rank interleaving]
271 * | mode used on every memory controller
Kumar Gala124b0822008-08-26 15:01:29 -0500272 *
273 * Even further confusing is the existence of the interleaving feature
274 * _WITHIN_ each memory controller. The feature is referred to in
275 * documentation as chip select interleaving or bank interleaving,
276 * although it is configured in the DDR_SDRAM_CFG field.
277 *
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200278 * Name of field | documentation name | this code
Kumar Gala124b0822008-08-26 15:01:29 -0500279 * -----------------------------|-----------------------|------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200280 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
281 * | interleaving
Kumar Gala124b0822008-08-26 15:01:29 -0500282 */
283
Kumar Gala124b0822008-08-26 15:01:29 -0500284const char *step_string_tbl[] = {
285 "STEP_GET_SPD",
286 "STEP_COMPUTE_DIMM_PARMS",
287 "STEP_COMPUTE_COMMON_PARMS",
288 "STEP_GATHER_OPTS",
289 "STEP_ASSIGN_ADDRESSES",
290 "STEP_COMPUTE_REGS",
291 "STEP_PROGRAM_REGS",
292 "STEP_ALL"
293};
294
295const char * step_to_string(unsigned int step) {
296
297 unsigned int s = __ilog2(step);
298
299 if ((1 << s) != step)
300 return step_string_tbl[7];
301
York Sunedbeee12014-04-01 14:20:49 -0700302 if (s >= ARRAY_SIZE(step_string_tbl)) {
303 printf("Error for the step in %s\n", __func__);
304 s = 0;
305 }
306
Kumar Gala124b0822008-08-26 15:01:29 -0500307 return step_string_tbl[s];
308}
Kumar Gala124b0822008-08-26 15:01:29 -0500309
York Sun01a82582013-03-25 07:39:35 +0000310static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Sune8dc17b2012-08-17 08:22:39 +0000311 unsigned int dbw_cap_adj[])
Kumar Gala124b0822008-08-26 15:01:29 -0500312{
York Sun79a779b2014-08-01 15:51:00 -0700313 unsigned int i, j;
York Sune8dc17b2012-08-17 08:22:39 +0000314 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
315 unsigned long long rank_density, ctlr_density = 0;
York Sun79a779b2014-08-01 15:51:00 -0700316 unsigned int first_ctrl = pinfo->first_ctrl;
317 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500318
319 /*
320 * If a reduced data width is requested, but the SPD
321 * specifies a physically wider device, adjust the
322 * computed dimm capacities accordingly before
323 * assigning addresses.
324 */
York Sun79a779b2014-08-01 15:51:00 -0700325 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500326 unsigned int found = 0;
327
328 switch (pinfo->memctl_opts[i].data_bus_width) {
329 case 2:
330 /* 16-bit */
York Sundd803dd2011-05-27 07:25:51 +0800331 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
332 unsigned int dw;
333 if (!pinfo->dimm_params[i][j].n_ranks)
334 continue;
335 dw = pinfo->dimm_params[i][j].primary_sdram_width;
336 if ((dw == 72 || dw == 64)) {
337 dbw_cap_adj[i] = 2;
338 break;
339 } else if ((dw == 40 || dw == 32)) {
340 dbw_cap_adj[i] = 1;
341 break;
342 }
343 }
Kumar Gala124b0822008-08-26 15:01:29 -0500344 break;
345
346 case 1:
347 /* 32-bit */
348 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
349 unsigned int dw;
350 dw = pinfo->dimm_params[i][j].data_width;
351 if (pinfo->dimm_params[i][j].n_ranks
352 && (dw == 72 || dw == 64)) {
353 /*
354 * FIXME: can't really do it
355 * like this because this just
356 * further reduces the memory
357 */
358 found = 1;
359 break;
360 }
361 }
362 if (found) {
363 dbw_cap_adj[i] = 1;
364 }
365 break;
366
367 case 0:
368 /* 64-bit */
369 break;
370
371 default:
372 printf("unexpected data bus width "
373 "specified controller %u\n", i);
374 return 1;
375 }
York Sune8dc17b2012-08-17 08:22:39 +0000376 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala124b0822008-08-26 15:01:29 -0500377 }
378
York Sun79a779b2014-08-01 15:51:00 -0700379 current_mem_base = pinfo->mem_base;
York Sune8dc17b2012-08-17 08:22:39 +0000380 total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700381 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
382 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
383 dbw_cap_adj[first_ctrl];
384 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
York Sune8dc17b2012-08-17 08:22:39 +0000385 FSL_DDR_CS0_CS1_CS2_CS3) {
386 case FSL_DDR_CS0_CS1_CS2_CS3:
387 ctlr_density = 4 * rank_density;
388 break;
389 case FSL_DDR_CS0_CS1:
390 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
391 ctlr_density = 2 * rank_density;
392 break;
393 case FSL_DDR_CS2_CS3:
394 default:
395 ctlr_density = rank_density;
396 break;
397 }
398 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
399 rank_density, ctlr_density);
York Sun79a779b2014-08-01 15:51:00 -0700400 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000401 if (pinfo->memctl_opts[i].memctl_interleaving) {
402 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sunc459ae62014-02-10 13:59:44 -0800403 case FSL_DDR_256B_INTERLEAVING:
York Sune8dc17b2012-08-17 08:22:39 +0000404 case FSL_DDR_CACHE_LINE_INTERLEAVING:
405 case FSL_DDR_PAGE_INTERLEAVING:
406 case FSL_DDR_BANK_INTERLEAVING:
407 case FSL_DDR_SUPERBANK_INTERLEAVING:
408 total_ctlr_mem = 2 * ctlr_density;
409 break;
410 case FSL_DDR_3WAY_1KB_INTERLEAVING:
411 case FSL_DDR_3WAY_4KB_INTERLEAVING:
412 case FSL_DDR_3WAY_8KB_INTERLEAVING:
413 total_ctlr_mem = 3 * ctlr_density;
414 break;
415 case FSL_DDR_4WAY_1KB_INTERLEAVING:
416 case FSL_DDR_4WAY_4KB_INTERLEAVING:
417 case FSL_DDR_4WAY_8KB_INTERLEAVING:
418 total_ctlr_mem = 4 * ctlr_density;
419 break;
420 default:
421 panic("Unknown interleaving mode");
422 }
423 pinfo->common_timing_params[i].base_address =
424 current_mem_base;
425 pinfo->common_timing_params[i].total_mem =
426 total_ctlr_mem;
427 total_mem = current_mem_base + total_ctlr_mem;
428 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
429 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
430 } else {
431 /* when 3rd controller not interleaved */
432 current_mem_base = total_mem;
433 total_ctlr_mem = 0;
434 pinfo->common_timing_params[i].base_address =
435 current_mem_base;
436 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
437 unsigned long long cap =
438 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
439 pinfo->dimm_params[i][j].base_address =
440 current_mem_base;
441 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
442 current_mem_base += cap;
443 total_ctlr_mem += cap;
444 }
445 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
446 pinfo->common_timing_params[i].total_mem =
447 total_ctlr_mem;
448 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500449 }
450 }
451 } else {
452 /*
453 * Simple linear assignment if memory
454 * controllers are not interleaved.
455 */
York Sun79a779b2014-08-01 15:51:00 -0700456 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000457 total_ctlr_mem = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500458 pinfo->common_timing_params[i].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000459 current_mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500460 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
461 /* Compute DIMM base addresses. */
462 unsigned long long cap =
York Sune8dc17b2012-08-17 08:22:39 +0000463 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala124b0822008-08-26 15:01:29 -0500464 pinfo->dimm_params[i][j].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000465 current_mem_base;
466 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
467 current_mem_base += cap;
468 total_ctlr_mem += cap;
Kumar Gala124b0822008-08-26 15:01:29 -0500469 }
York Sune8dc17b2012-08-17 08:22:39 +0000470 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500471 pinfo->common_timing_params[i].total_mem =
York Sune8dc17b2012-08-17 08:22:39 +0000472 total_ctlr_mem;
473 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500474 }
475 }
York Sune8dc17b2012-08-17 08:22:39 +0000476 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500477
York Sune8dc17b2012-08-17 08:22:39 +0000478 return total_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500479}
480
York Sun01a82582013-03-25 07:39:35 +0000481/* Use weak function to allow board file to override the address assignment */
482__attribute__((weak, alias("__step_assign_addresses")))
483unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
484 unsigned int dbw_cap_adj[]);
485
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500486unsigned long long
Haiying Wang80ad4012010-12-01 10:35:31 -0500487fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
488 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -0500489{
490 unsigned int i, j;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500491 unsigned long long total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700492 int assert_reset = 0;
493 unsigned int first_ctrl = pinfo->first_ctrl;
494 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
495 __maybe_unused int retval;
496 __maybe_unused bool goodspd = false;
497 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
Kumar Gala124b0822008-08-26 15:01:29 -0500498
499 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
500 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun79a779b2014-08-01 15:51:00 -0700501 if (pinfo->board_need_mem_reset)
502 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala124b0822008-08-26 15:01:29 -0500503
504 /* data bus width capacity adjust shift amount */
York Sunfe845072016-12-28 08:43:45 -0800505 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala124b0822008-08-26 15:01:29 -0500506
York Sun79a779b2014-08-01 15:51:00 -0700507 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala124b0822008-08-26 15:01:29 -0500508 dbw_capacity_adjust[i] = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500509
510 debug("starting at step %u (%s)\n",
511 start_step, step_to_string(start_step));
512
513 switch (start_step) {
514 case STEP_GET_SPD:
York Sune73cc042011-06-07 09:42:16 +0800515#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala124b0822008-08-26 15:01:29 -0500516 /* STEP 1: Gather all DIMM SPD data */
York Sun79a779b2014-08-01 15:51:00 -0700517 for (i = first_ctrl; i <= last_ctrl; i++) {
518 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
519 dimm_slots_per_ctrl);
Kumar Gala124b0822008-08-26 15:01:29 -0500520 }
521
522 case STEP_COMPUTE_DIMM_PARMS:
523 /* STEP 2: Compute DIMM parameters from SPD data */
524
York Sun79a779b2014-08-01 15:51:00 -0700525 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500526 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500527 generic_spd_eeprom_t *spd =
528 &(pinfo->spd_installed_dimms[i][j]);
529 dimm_params_t *pdimm =
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200530 &(pinfo->dimm_params[i][j]);
York Sun2c0b62d2015-01-06 13:18:50 -0800531 retval = compute_dimm_parameters(
532 i, spd, pdimm, j);
York Sun09d8aa82011-06-07 09:42:17 +0800533#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun55eb5fa2015-03-19 09:30:26 -0700534 if (!j && retval) {
York Sune8dc17b2012-08-17 08:22:39 +0000535 printf("SPD error on controller %d! "
536 "Trying fallback to raw timing "
537 "calculation\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700538 retval = fsl_ddr_get_dimm_params(pdimm,
539 i, j);
York Sun09d8aa82011-06-07 09:42:17 +0800540 }
541#else
Kumar Gala124b0822008-08-26 15:01:29 -0500542 if (retval == 2) {
543 printf("Error: compute_dimm_parameters"
544 " non-zero returned FATAL value "
545 "for memctl=%u dimm=%u\n", i, j);
546 return 0;
547 }
York Sun09d8aa82011-06-07 09:42:17 +0800548#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500549 if (retval) {
550 debug("Warning: compute_dimm_parameters"
551 " non-zero return value for memctl=%u "
552 "dimm=%u\n", i, j);
York Sun79a779b2014-08-01 15:51:00 -0700553 } else {
554 goodspd = true;
Kumar Gala124b0822008-08-26 15:01:29 -0500555 }
556 }
557 }
York Sun79a779b2014-08-01 15:51:00 -0700558 if (!goodspd) {
559 /*
560 * No valid SPD found
561 * Throw an error if this is for main memory, i.e.
562 * first_ctrl == 0. Otherwise, siliently return 0
563 * as the memory size.
564 */
565 if (first_ctrl == 0)
566 printf("Error: No valid SPD detected.\n");
Kumar Gala124b0822008-08-26 15:01:29 -0500567
York Sun79a779b2014-08-01 15:51:00 -0700568 return 0;
569 }
Shaohui Xie24009042012-06-28 23:36:38 +0000570#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sune73cc042011-06-07 09:42:16 +0800571 case STEP_COMPUTE_DIMM_PARMS:
York Sun79a779b2014-08-01 15:51:00 -0700572 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune73cc042011-06-07 09:42:16 +0800573 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
574 dimm_params_t *pdimm =
575 &(pinfo->dimm_params[i][j]);
576 fsl_ddr_get_dimm_params(pdimm, i, j);
577 }
578 }
579 debug("Filling dimm parameters from board specific file\n");
580#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500581 case STEP_COMPUTE_COMMON_PARMS:
582 /*
583 * STEP 3: Compute a common set of timing parameters
584 * suitable for all of the DIMMs on each memory controller
585 */
York Sun79a779b2014-08-01 15:51:00 -0700586 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500587 debug("Computing lowest common DIMM"
588 " parameters for memctl=%u\n", i);
York Sun2c0b62d2015-01-06 13:18:50 -0800589 compute_lowest_common_dimm_parameters
590 (i,
591 pinfo->dimm_params[i],
592 &timing_params[i],
593 CONFIG_DIMM_SLOTS_PER_CTLR);
Kumar Gala124b0822008-08-26 15:01:29 -0500594 }
595
596 case STEP_GATHER_OPTS:
597 /* STEP 4: Gather configuration requirements from user */
York Sun79a779b2014-08-01 15:51:00 -0700598 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500599 debug("Reloading memory controller "
600 "configuration options for memctl=%u\n", i);
601 /*
602 * This "reloads" the memory controller options
603 * to defaults. If the user "edits" an option,
604 * next_step points to the step after this,
605 * which is currently STEP_ASSIGN_ADDRESSES.
606 */
607 populate_memctl_options(
York Sun999273f2015-07-23 14:04:48 -0700608 &timing_params[i],
Haiying Wangfa440362008-10-03 12:36:55 -0400609 &pinfo->memctl_opts[i],
610 pinfo->dimm_params[i], i);
York Sun5e155552013-06-25 11:37:48 -0700611 /*
612 * For RDIMMs, JEDEC spec requires clocks to be stable
613 * before reset signal is deasserted. For the boards
614 * using fixed parameters, this function should be
615 * be called from board init file.
616 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530617 if (timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700618 assert_reset = 1;
619 }
York Sun79a779b2014-08-01 15:51:00 -0700620 if (assert_reset && !size_only) {
621 if (pinfo->board_mem_reset) {
622 debug("Asserting mem reset\n");
623 pinfo->board_mem_reset();
624 } else {
625 debug("Asserting mem reset missing\n");
626 }
Kumar Gala124b0822008-08-26 15:01:29 -0500627 }
York Sun5e155552013-06-25 11:37:48 -0700628
Kumar Gala124b0822008-08-26 15:01:29 -0500629 case STEP_ASSIGN_ADDRESSES:
630 /* STEP 5: Assign addresses to chip selects */
York Sune8dc17b2012-08-17 08:22:39 +0000631 check_interleaving_options(pinfo);
632 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sunedbeee12014-04-01 14:20:49 -0700633 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500634
635 case STEP_COMPUTE_REGS:
636 /* STEP 6: compute controller register values */
York Sune8dc17b2012-08-17 08:22:39 +0000637 debug("FSL Memory ctrl register computation\n");
York Sun79a779b2014-08-01 15:51:00 -0700638 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500639 if (timing_params[i].ndimms_present == 0) {
640 memset(&ddr_reg[i], 0,
641 sizeof(fsl_ddr_cfg_regs_t));
642 continue;
643 }
644
York Sun2c0b62d2015-01-06 13:18:50 -0800645 compute_fsl_memctl_config_regs
646 (i,
647 &pinfo->memctl_opts[i],
648 &ddr_reg[i], &timing_params[i],
649 pinfo->dimm_params[i],
650 dbw_capacity_adjust[i],
651 size_only);
Kumar Gala124b0822008-08-26 15:01:29 -0500652 }
653
654 default:
655 break;
656 }
657
York Sune8dc17b2012-08-17 08:22:39 +0000658 {
Kumar Gala124b0822008-08-26 15:01:29 -0500659 /*
660 * Compute the amount of memory available just by
661 * looking for the highest valid CSn_BNDS value.
662 * This allows us to also experiment with using
663 * only CS0 when using dual-rank DIMMs.
664 */
665 unsigned int max_end = 0;
666
York Sun79a779b2014-08-01 15:51:00 -0700667 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500668 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
669 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
670 if (reg->cs[j].config & 0x80000000) {
671 unsigned int end;
York Sunc21a7392013-06-25 11:37:45 -0700672 /*
673 * 0xfffffff is a special value we put
674 * for unused bnds
675 */
676 if (reg->cs[j].bnds == 0xffffffff)
677 continue;
678 end = reg->cs[j].bnds & 0xffff;
Kumar Gala124b0822008-08-26 15:01:29 -0500679 if (end > max_end) {
680 max_end = end;
681 }
682 }
683 }
684 }
685
York Sun6446f1e2013-10-28 16:36:02 -0700686 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun79a779b2014-08-01 15:51:00 -0700687 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500688 }
689
690 return total_mem;
691}
692
York Sun79a779b2014-08-01 15:51:00 -0700693phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala124b0822008-08-26 15:01:29 -0500694{
York Sun79a779b2014-08-01 15:51:00 -0700695 unsigned int i, first_ctrl, last_ctrl;
York Sun461c9392013-09-30 14:20:51 -0700696#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000697 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun461c9392013-09-30 14:20:51 -0700698#endif
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500699 unsigned long long total_memory;
York Sun79a779b2014-08-01 15:51:00 -0700700 int deassert_reset = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500701
York Sun79a779b2014-08-01 15:51:00 -0700702 first_ctrl = pinfo->first_ctrl;
703 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500704
705 /* Compute it once normally. */
York Sunbd495cf2011-09-16 13:21:35 -0700706#ifdef CONFIG_FSL_DDR_INTERACTIVE
James Yang43794382013-01-07 14:01:03 +0000707 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
York Sun79a779b2014-08-01 15:51:00 -0700708 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yang43794382013-01-07 14:01:03 +0000709 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun79a779b2014-08-01 15:51:00 -0700710 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sun5025a8d2013-01-04 08:13:59 +0000711 } else
York Sunbd495cf2011-09-16 13:21:35 -0700712#endif
York Sun79a779b2014-08-01 15:51:00 -0700713 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500714
York Sun016095d2012-10-08 07:44:24 +0000715 /* setup 3-way interleaving before enabling DDRC */
York Sun79a779b2014-08-01 15:51:00 -0700716 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
717 case FSL_DDR_3WAY_1KB_INTERLEAVING:
718 case FSL_DDR_3WAY_4KB_INTERLEAVING:
719 case FSL_DDR_3WAY_8KB_INTERLEAVING:
720 fsl_ddr_set_intl3r(
721 pinfo->memctl_opts[first_ctrl].
722 memctl_interleaving_mode);
723 break;
724 default:
725 break;
York Sun016095d2012-10-08 07:44:24 +0000726 }
727
York Sun5e155552013-06-25 11:37:48 -0700728 /*
729 * Program configuration registers.
730 * JEDEC specs requires clocks to be stable before deasserting reset
731 * for RDIMMs. Clocks start after chip select is enabled and clock
732 * control register is set. During step 1, all controllers have their
733 * registers set but not enabled. Step 2 proceeds after deasserting
734 * reset through board FPGA or GPIO.
735 * For non-registered DIMMs, initialization can go through but it is
736 * also OK to follow the same flow.
737 */
York Sun79a779b2014-08-01 15:51:00 -0700738 if (pinfo->board_need_mem_reset)
739 deassert_reset = pinfo->board_need_mem_reset();
740 for (i = first_ctrl; i <= last_ctrl; i++) {
741 if (pinfo->common_timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700742 deassert_reset = 1;
743 }
York Sun79a779b2014-08-01 15:51:00 -0700744 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500745 debug("Programming controller %u\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700746 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -0500747 debug("No dimms present on controller %u; "
748 "skipping programming\n", i);
749 continue;
750 }
York Sun5e155552013-06-25 11:37:48 -0700751 /*
752 * The following call with step = 1 returns before enabling
753 * the controller. It has to finish with step = 2 later.
754 */
York Sun79a779b2014-08-01 15:51:00 -0700755 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sun5e155552013-06-25 11:37:48 -0700756 deassert_reset ? 1 : 0);
757 }
758 if (deassert_reset) {
759 /* Use board FPGA or GPIO to deassert reset signal */
York Sun79a779b2014-08-01 15:51:00 -0700760 if (pinfo->board_mem_de_reset) {
761 debug("Deasserting mem reset\n");
762 pinfo->board_mem_de_reset();
763 } else {
764 debug("Deasserting mem reset missing\n");
765 }
766 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun5e155552013-06-25 11:37:48 -0700767 /* Call with step = 2 to continue initialization */
York Sun79a779b2014-08-01 15:51:00 -0700768 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sun5e155552013-06-25 11:37:48 -0700769 i, 2);
770 }
Kumar Gala124b0822008-08-26 15:01:29 -0500771 }
772
York Sun8ced0502015-01-06 13:18:55 -0800773#ifdef CONFIG_FSL_DDR_SYNC_REFRESH
774 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
775#endif
776
York Sun461c9392013-09-30 14:20:51 -0700777#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000778 /* program LAWs */
York Sun79a779b2014-08-01 15:51:00 -0700779 for (i = first_ctrl; i <= last_ctrl; i++) {
780 if (pinfo->memctl_opts[i].memctl_interleaving) {
781 switch (pinfo->memctl_opts[i].
782 memctl_interleaving_mode) {
York Sune8dc17b2012-08-17 08:22:39 +0000783 case FSL_DDR_CACHE_LINE_INTERLEAVING:
784 case FSL_DDR_PAGE_INTERLEAVING:
785 case FSL_DDR_BANK_INTERLEAVING:
786 case FSL_DDR_SUPERBANK_INTERLEAVING:
York Sun79a779b2014-08-01 15:51:00 -0700787 if (i % 2)
788 break;
York Sune8dc17b2012-08-17 08:22:39 +0000789 if (i == 0) {
790 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun79a779b2014-08-01 15:51:00 -0700791 fsl_ddr_set_lawbar(
792 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000793 law_memctl, i);
York Sun79a779b2014-08-01 15:51:00 -0700794 }
York Sunfe845072016-12-28 08:43:45 -0800795#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun79a779b2014-08-01 15:51:00 -0700796 else if (i == 2) {
York Sune8dc17b2012-08-17 08:22:39 +0000797 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
York Sun79a779b2014-08-01 15:51:00 -0700798 fsl_ddr_set_lawbar(
799 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000800 law_memctl, i);
801 }
York Sun79a779b2014-08-01 15:51:00 -0700802#endif
York Sune8dc17b2012-08-17 08:22:39 +0000803 break;
804 case FSL_DDR_3WAY_1KB_INTERLEAVING:
805 case FSL_DDR_3WAY_4KB_INTERLEAVING:
806 case FSL_DDR_3WAY_8KB_INTERLEAVING:
807 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
808 if (i == 0) {
York Sun79a779b2014-08-01 15:51:00 -0700809 fsl_ddr_set_lawbar(
810 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000811 law_memctl, i);
812 }
813 break;
814 case FSL_DDR_4WAY_1KB_INTERLEAVING:
815 case FSL_DDR_4WAY_4KB_INTERLEAVING:
816 case FSL_DDR_4WAY_8KB_INTERLEAVING:
817 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
818 if (i == 0)
York Sun79a779b2014-08-01 15:51:00 -0700819 fsl_ddr_set_lawbar(
820 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000821 law_memctl, i);
822 /* place holder for future 4-way interleaving */
823 break;
824 default:
825 break;
826 }
827 } else {
828 switch (i) {
829 case 0:
830 law_memctl = LAW_TRGT_IF_DDR_1;
831 break;
832 case 1:
833 law_memctl = LAW_TRGT_IF_DDR_2;
834 break;
835 case 2:
836 law_memctl = LAW_TRGT_IF_DDR_3;
837 break;
838 case 3:
839 law_memctl = LAW_TRGT_IF_DDR_4;
840 break;
841 default:
842 break;
843 }
York Sun79a779b2014-08-01 15:51:00 -0700844 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
845 law_memctl, i);
Kumar Gala124b0822008-08-26 15:01:29 -0500846 }
847 }
York Sun461c9392013-09-30 14:20:51 -0700848#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500849
York Sune8dc17b2012-08-17 08:22:39 +0000850 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500851
852#if !defined(CONFIG_PHYS_64BIT)
853 /* Check for 4G or more. Bad. */
York Sun79a779b2014-08-01 15:51:00 -0700854 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
Shruti Kanetkar3adfb912013-08-15 11:25:37 -0500855 puts("Detected ");
856 print_size(total_memory, " of memory\n");
Becky Bruce2d8ecac2010-12-17 17:17:59 -0600857 printf(" This U-Boot only supports < 4G of DDR\n");
858 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
Simon Glassd35f3382017-04-06 12:47:05 -0600859 printf(" "); /* re-align to match init_dram print */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500860 total_memory = CONFIG_MAX_MEM_MAPPED;
861 }
862#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500863
864 return total_memory;
865}
Haiying Wang80ad4012010-12-01 10:35:31 -0500866
867/*
York Sun79a779b2014-08-01 15:51:00 -0700868 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassd35f3382017-04-06 12:47:05 -0600869 * called by dram_init() in the board file.
York Sun79a779b2014-08-01 15:51:00 -0700870 *
871 * It returns amount of memory configured in bytes.
872 */
873phys_size_t fsl_ddr_sdram(void)
874{
875 fsl_ddr_info_t info;
876
877 /* Reset info structure. */
878 memset(&info, 0, sizeof(fsl_ddr_info_t));
879 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
880 info.first_ctrl = 0;
881 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
882 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
883 info.board_need_mem_reset = board_need_mem_reset;
884 info.board_mem_reset = board_assert_mem_reset;
885 info.board_mem_de_reset = board_deassert_mem_reset;
York Sund957a672015-11-04 09:53:10 -0800886 remove_unused_controllers(&info);
York Sun79a779b2014-08-01 15:51:00 -0700887
888 return __fsl_ddr_sdram(&info);
889}
890
891#ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
892phys_size_t fsl_other_ddr_sdram(unsigned long long base,
893 unsigned int first_ctrl,
894 unsigned int num_ctrls,
895 unsigned int dimm_slots_per_ctrl,
896 int (*board_need_reset)(void),
897 void (*board_reset)(void),
898 void (*board_de_reset)(void))
899{
900 fsl_ddr_info_t info;
901
902 /* Reset info structure. */
903 memset(&info, 0, sizeof(fsl_ddr_info_t));
904 info.mem_base = base;
905 info.first_ctrl = first_ctrl;
906 info.num_ctrls = num_ctrls;
907 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
908 info.board_need_mem_reset = board_need_reset;
909 info.board_mem_reset = board_reset;
910 info.board_mem_de_reset = board_de_reset;
911
912 return __fsl_ddr_sdram(&info);
913}
914#endif
915
916/*
917 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
918 * size of the total memory without setting ddr control registers.
Haiying Wang80ad4012010-12-01 10:35:31 -0500919 */
920phys_size_t
921fsl_ddr_sdram_size(void)
922{
923 fsl_ddr_info_t info;
924 unsigned long long total_memory = 0;
925
926 memset(&info, 0 , sizeof(fsl_ddr_info_t));
York Sun79a779b2014-08-01 15:51:00 -0700927 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
928 info.first_ctrl = 0;
929 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
930 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
931 info.board_need_mem_reset = NULL;
Ed Swarthout7ae7a0e2016-01-14 12:28:04 -0600932 remove_unused_controllers(&info);
Haiying Wang80ad4012010-12-01 10:35:31 -0500933
934 /* Compute it once normally. */
935 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
936
937 return total_memory;
938}