blob: f389e5ef952417e55011e0edcffbd0f1f5d8265d [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.
Priyanka Singhaa44eb22021-08-19 11:39:03 +05304 * Copyright 2021 NXP
Kumar Gala124b0822008-08-26 15:01:29 -05005 */
6
7/*
8 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
9 * Based on code from spd_sdram.c
10 * Author: James Yang [at freescale.com]
11 */
12
13#include <common.h>
Heiko Schocher375038f2019-08-26 17:28:34 +020014#include <dm.h>
Kumar Galac68e86c2011-01-31 22:18:47 -060015#include <i2c.h>
York Sunf0626592013-09-30 09:22:09 -070016#include <fsl_ddr_sdram.h>
York Sunf0626592013-09-30 09:22:09 -070017#include <fsl_ddr.h>
Simon Glass97589732020-05-10 11:40:02 -060018#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <asm/bitops.h>
Kumar Gala124b0822008-08-26 15:01:29 -050021
York Sun3a0916d2014-02-10 13:59:43 -080022/*
23 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
24 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
25 * all Power SoCs. But it could be different for ARM SoCs. For example,
26 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
27 * 0x00_8000_0000 ~ 0x00_ffff_ffff
28 * 0x80_8000_0000 ~ 0xff_ffff_ffff
29 */
30#ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
Mario Six805cac12019-01-21 09:18:16 +010031#ifdef CONFIG_MPC83xx
32#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
33#else
York Sun3a0916d2014-02-10 13:59:43 -080034#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
35#endif
Mario Six805cac12019-01-21 09:18:16 +010036#endif
York Sun3a0916d2014-02-10 13:59:43 -080037
York Sun461c9392013-09-30 14:20:51 -070038#ifdef CONFIG_PPC
39#include <asm/fsl_law.h>
40
York Sune8dc17b2012-08-17 08:22:39 +000041void fsl_ddr_set_lawbar(
Kumar Gala124b0822008-08-26 15:01:29 -050042 const common_timing_params_t *memctl_common_params,
43 unsigned int memctl_interleaved,
44 unsigned int ctrl_num);
York Sun461c9392013-09-30 14:20:51 -070045#endif
Kumar Gala124b0822008-08-26 15:01:29 -050046
York Sun461c9392013-09-30 14:20:51 -070047void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac68e86c2011-01-31 22:18:47 -060048#if defined(SPD_EEPROM_ADDRESS) || \
49 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
50 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
York Sunfe845072016-12-28 08:43:45 -080051#if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
52u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060053 [0][0] = SPD_EEPROM_ADDRESS,
54};
York Sunfe845072016-12-28 08:43:45 -080055#elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
56u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060057 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
York Sun92b46ac2011-08-26 11:32:41 -070058 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
59};
York Sunfe845072016-12-28 08:43:45 -080060#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
61u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sun92b46ac2011-08-26 11:32:41 -070062 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
Kumar Galac68e86c2011-01-31 22:18:47 -060063 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
64};
York Sunfe845072016-12-28 08:43:45 -080065#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
66u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac68e86c2011-01-31 22:18:47 -060067 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
68 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
69 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
70 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
71};
York Sunfe845072016-12-28 08:43:45 -080072#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
73u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000074 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
75 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
76 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
77};
York Sunfe845072016-12-28 08:43:45 -080078#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
79u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sune8dc17b2012-08-17 08:22:39 +000080 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
81 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
82 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
83 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
84 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
85 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
86};
87
Kumar Galac68e86c2011-01-31 22:18:47 -060088#endif
89
Igor Opaniukf7c91762021-02-09 13:52:45 +020090#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher375038f2019-08-26 17:28:34 +020091#define DEV_TYPE struct udevice
92#else
93/* Local udevice */
94struct ludevice {
95 u8 chip;
96};
97
98#define DEV_TYPE struct ludevice
99
100#endif
101
York Sun2896cb72014-03-27 17:54:47 -0700102#define SPD_SPA0_ADDRESS 0x36
103#define SPD_SPA1_ADDRESS 0x37
104
Heiko Schocher375038f2019-08-26 17:28:34 +0200105static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
106 int alen, uint8_t *buf, int len)
Kumar Galac68e86c2011-01-31 22:18:47 -0600107{
Valentin Longchamp993967a2013-10-18 11:47:19 +0200108 int ret;
Heiko Schocher375038f2019-08-26 17:28:34 +0200109
Igor Opaniukf7c91762021-02-09 13:52:45 +0200110#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher375038f2019-08-26 17:28:34 +0200111 ret = dm_i2c_read(dev, 0, buf, len);
112#else
113 ret = i2c_read(dev->chip, addr, alen, buf, len);
114#endif
115
116 return ret;
117}
118
York Sun2896cb72014-03-27 17:54:47 -0700119#ifdef CONFIG_SYS_FSL_DDR4
Heiko Schocher375038f2019-08-26 17:28:34 +0200120static int ddr_i2c_dummy_write(unsigned int chip_addr)
121{
122 uint8_t buf = 0;
123
Igor Opaniukf7c91762021-02-09 13:52:45 +0200124#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher375038f2019-08-26 17:28:34 +0200125 struct udevice *dev;
126 int ret;
127
128 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
129 1, &dev);
130 if (ret) {
131 printf("%s: Cannot find udev for a bus %d\n", __func__,
132 CONFIG_SYS_SPD_BUS_NUM);
133 return ret;
134 }
135
136 return dm_i2c_write(dev, 0, &buf, 1);
137#else
138 return i2c_write(chip_addr, 0, 1, &buf, 1);
York Sun2896cb72014-03-27 17:54:47 -0700139#endif
Valentin Longchamp993967a2013-10-18 11:47:19 +0200140
Heiko Schocher375038f2019-08-26 17:28:34 +0200141 return 0;
142}
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800143#endif
144
Heiko Schocher375038f2019-08-26 17:28:34 +0200145static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
146{
147 int ret;
148 DEV_TYPE *dev;
149
Igor Opaniukf7c91762021-02-09 13:52:45 +0200150#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher375038f2019-08-26 17:28:34 +0200151 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
152 1, &dev);
153 if (ret) {
154 printf("%s: Cannot find udev for a bus %d\n", __func__,
155 CONFIG_SYS_SPD_BUS_NUM);
156 return;
157 }
158#else /* Non DM I2C support - will be removed */
159 struct ludevice ldev = {
160 .chip = i2c_address,
161 };
162 dev = &ldev;
163
164 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
165#endif
Valentin Longchamp993967a2013-10-18 11:47:19 +0200166
York Sun2896cb72014-03-27 17:54:47 -0700167#ifdef CONFIG_SYS_FSL_DDR4
168 /*
169 * DDR4 SPD has 384 to 512 bytes
170 * To access the lower 256 bytes, we need to set EE page address to 0
171 * To access the upper 256 bytes, we need to set EE page address to 1
172 * See Jedec standar No. 21-C for detail
173 */
Heiko Schocher375038f2019-08-26 17:28:34 +0200174 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
175 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
York Sun2896cb72014-03-27 17:54:47 -0700176 if (!ret) {
Heiko Schocher375038f2019-08-26 17:28:34 +0200177 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
178 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
179 min(256,
180 (int)sizeof(generic_spd_eeprom_t)
181 - 256));
York Sun2896cb72014-03-27 17:54:47 -0700182 }
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800183
184#else
Heiko Schocher375038f2019-08-26 17:28:34 +0200185 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
186 sizeof(generic_spd_eeprom_t));
York Sun2896cb72014-03-27 17:54:47 -0700187#endif
Kumar Galac68e86c2011-01-31 22:18:47 -0600188
189 if (ret) {
York Sun9b9372f2012-10-08 07:44:28 +0000190 if (i2c_address ==
191#ifdef SPD_EEPROM_ADDRESS
192 SPD_EEPROM_ADDRESS
193#elif defined(SPD_EEPROM_ADDRESS1)
194 SPD_EEPROM_ADDRESS1
195#endif
196 ) {
197 printf("DDR: failed to read SPD from address %u\n",
198 i2c_address);
199 } else {
200 debug("DDR: failed to read SPD from address %u\n",
201 i2c_address);
202 }
Kumar Galac68e86c2011-01-31 22:18:47 -0600203 memset(spd, 0, sizeof(generic_spd_eeprom_t));
204 }
205}
206
207__attribute__((weak, alias("__get_spd")))
208void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
209
York Sunb10d1b72015-05-28 14:54:08 +0530210/* This function allows boards to update SPD address */
211__weak void update_spd_address(unsigned int ctrl_num,
212 unsigned int slot,
213 unsigned int *addr)
214{
215}
216
Kumar Galac68e86c2011-01-31 22:18:47 -0600217void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700218 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600219{
220 unsigned int i;
221 unsigned int i2c_address = 0;
222
York Sunfe845072016-12-28 08:43:45 -0800223 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600224 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
225 return;
226 }
227
York Sun79a779b2014-08-01 15:51:00 -0700228 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac68e86c2011-01-31 22:18:47 -0600229 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb10d1b72015-05-28 14:54:08 +0530230 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac68e86c2011-01-31 22:18:47 -0600231 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
232 }
233}
234#else
235void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun79a779b2014-08-01 15:51:00 -0700236 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac68e86c2011-01-31 22:18:47 -0600237{
238}
239#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala124b0822008-08-26 15:01:29 -0500240
241/*
242 * ASSUMPTIONS:
243 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
244 * - Same memory data bus width on all controllers
245 *
246 * NOTES:
247 *
248 * The memory controller and associated documentation use confusing
249 * terminology when referring to the orgranization of DRAM.
250 *
251 * Here is a terminology translation table:
252 *
253 * memory controller/documention |industry |this code |signals
254 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200255 * physical bank/bank |rank |rank |chip select (CS)
256 * logical bank/sub-bank |bank |bank |bank address (BA)
257 * page/row |row |page |row address
258 * ??? |column |column |column address
Kumar Gala124b0822008-08-26 15:01:29 -0500259 *
260 * The naming confusion is further exacerbated by the descriptions of the
261 * memory controller interleaving feature, where accesses are interleaved
262 * _BETWEEN_ two seperate memory controllers. This is configured only in
263 * CS0_CONFIG[INTLV_CTL] of each memory controller.
264 *
265 * memory controller documentation | number of chip selects
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200266 * | per memory controller supported
Kumar Gala124b0822008-08-26 15:01:29 -0500267 * --------------------------------|-----------------------------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200268 * cache line interleaving | 1 (CS0 only)
269 * page interleaving | 1 (CS0 only)
270 * bank interleaving | 1 (CS0 only)
271 * superbank interleraving | depends on bank (chip select)
272 * | interleraving [rank interleaving]
273 * | mode used on every memory controller
Kumar Gala124b0822008-08-26 15:01:29 -0500274 *
275 * Even further confusing is the existence of the interleaving feature
276 * _WITHIN_ each memory controller. The feature is referred to in
277 * documentation as chip select interleaving or bank interleaving,
278 * although it is configured in the DDR_SDRAM_CFG field.
279 *
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200280 * Name of field | documentation name | this code
Kumar Gala124b0822008-08-26 15:01:29 -0500281 * -----------------------------|-----------------------|------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200282 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
283 * | interleaving
Kumar Gala124b0822008-08-26 15:01:29 -0500284 */
285
Kumar Gala124b0822008-08-26 15:01:29 -0500286const char *step_string_tbl[] = {
287 "STEP_GET_SPD",
288 "STEP_COMPUTE_DIMM_PARMS",
289 "STEP_COMPUTE_COMMON_PARMS",
290 "STEP_GATHER_OPTS",
291 "STEP_ASSIGN_ADDRESSES",
292 "STEP_COMPUTE_REGS",
293 "STEP_PROGRAM_REGS",
294 "STEP_ALL"
295};
296
297const char * step_to_string(unsigned int step) {
298
299 unsigned int s = __ilog2(step);
300
Priyanka Singhaa44eb22021-08-19 11:39:03 +0530301 if (s <= 31) {
302 if ((1 << s) != step)
303 return step_string_tbl[7];
304 } else {
305 if ((1 << (s - 32)) != step)
306 return step_string_tbl[7];
307 }
York Sunedbeee12014-04-01 14:20:49 -0700308 if (s >= ARRAY_SIZE(step_string_tbl)) {
309 printf("Error for the step in %s\n", __func__);
310 s = 0;
311 }
312
Kumar Gala124b0822008-08-26 15:01:29 -0500313 return step_string_tbl[s];
314}
Kumar Gala124b0822008-08-26 15:01:29 -0500315
York Sun01a82582013-03-25 07:39:35 +0000316static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Sune8dc17b2012-08-17 08:22:39 +0000317 unsigned int dbw_cap_adj[])
Kumar Gala124b0822008-08-26 15:01:29 -0500318{
York Sun79a779b2014-08-01 15:51:00 -0700319 unsigned int i, j;
York Sune8dc17b2012-08-17 08:22:39 +0000320 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
321 unsigned long long rank_density, ctlr_density = 0;
York Sun79a779b2014-08-01 15:51:00 -0700322 unsigned int first_ctrl = pinfo->first_ctrl;
323 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500324
325 /*
326 * If a reduced data width is requested, but the SPD
327 * specifies a physically wider device, adjust the
328 * computed dimm capacities accordingly before
329 * assigning addresses.
330 */
York Sun79a779b2014-08-01 15:51:00 -0700331 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500332 unsigned int found = 0;
333
334 switch (pinfo->memctl_opts[i].data_bus_width) {
335 case 2:
336 /* 16-bit */
York Sundd803dd2011-05-27 07:25:51 +0800337 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
338 unsigned int dw;
339 if (!pinfo->dimm_params[i][j].n_ranks)
340 continue;
341 dw = pinfo->dimm_params[i][j].primary_sdram_width;
342 if ((dw == 72 || dw == 64)) {
343 dbw_cap_adj[i] = 2;
344 break;
345 } else if ((dw == 40 || dw == 32)) {
346 dbw_cap_adj[i] = 1;
347 break;
348 }
349 }
Kumar Gala124b0822008-08-26 15:01:29 -0500350 break;
351
352 case 1:
353 /* 32-bit */
354 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
355 unsigned int dw;
356 dw = pinfo->dimm_params[i][j].data_width;
357 if (pinfo->dimm_params[i][j].n_ranks
358 && (dw == 72 || dw == 64)) {
359 /*
360 * FIXME: can't really do it
361 * like this because this just
362 * further reduces the memory
363 */
364 found = 1;
365 break;
366 }
367 }
368 if (found) {
369 dbw_cap_adj[i] = 1;
370 }
371 break;
372
373 case 0:
374 /* 64-bit */
375 break;
376
377 default:
378 printf("unexpected data bus width "
379 "specified controller %u\n", i);
380 return 1;
381 }
York Sune8dc17b2012-08-17 08:22:39 +0000382 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala124b0822008-08-26 15:01:29 -0500383 }
384
York Sun79a779b2014-08-01 15:51:00 -0700385 current_mem_base = pinfo->mem_base;
York Sune8dc17b2012-08-17 08:22:39 +0000386 total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700387 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
388 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
389 dbw_cap_adj[first_ctrl];
390 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
York Sune8dc17b2012-08-17 08:22:39 +0000391 FSL_DDR_CS0_CS1_CS2_CS3) {
392 case FSL_DDR_CS0_CS1_CS2_CS3:
393 ctlr_density = 4 * rank_density;
394 break;
395 case FSL_DDR_CS0_CS1:
396 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
397 ctlr_density = 2 * rank_density;
398 break;
399 case FSL_DDR_CS2_CS3:
400 default:
401 ctlr_density = rank_density;
402 break;
403 }
404 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
405 rank_density, ctlr_density);
York Sun79a779b2014-08-01 15:51:00 -0700406 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000407 if (pinfo->memctl_opts[i].memctl_interleaving) {
408 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sunc459ae62014-02-10 13:59:44 -0800409 case FSL_DDR_256B_INTERLEAVING:
York Sune8dc17b2012-08-17 08:22:39 +0000410 case FSL_DDR_CACHE_LINE_INTERLEAVING:
411 case FSL_DDR_PAGE_INTERLEAVING:
412 case FSL_DDR_BANK_INTERLEAVING:
413 case FSL_DDR_SUPERBANK_INTERLEAVING:
414 total_ctlr_mem = 2 * ctlr_density;
415 break;
416 case FSL_DDR_3WAY_1KB_INTERLEAVING:
417 case FSL_DDR_3WAY_4KB_INTERLEAVING:
418 case FSL_DDR_3WAY_8KB_INTERLEAVING:
419 total_ctlr_mem = 3 * ctlr_density;
420 break;
421 case FSL_DDR_4WAY_1KB_INTERLEAVING:
422 case FSL_DDR_4WAY_4KB_INTERLEAVING:
423 case FSL_DDR_4WAY_8KB_INTERLEAVING:
424 total_ctlr_mem = 4 * ctlr_density;
425 break;
426 default:
427 panic("Unknown interleaving mode");
428 }
429 pinfo->common_timing_params[i].base_address =
430 current_mem_base;
431 pinfo->common_timing_params[i].total_mem =
432 total_ctlr_mem;
433 total_mem = current_mem_base + total_ctlr_mem;
434 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
435 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
436 } else {
437 /* when 3rd controller not interleaved */
438 current_mem_base = total_mem;
439 total_ctlr_mem = 0;
440 pinfo->common_timing_params[i].base_address =
441 current_mem_base;
442 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
443 unsigned long long cap =
444 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
445 pinfo->dimm_params[i][j].base_address =
446 current_mem_base;
447 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
448 current_mem_base += cap;
449 total_ctlr_mem += cap;
450 }
451 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
452 pinfo->common_timing_params[i].total_mem =
453 total_ctlr_mem;
454 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500455 }
456 }
457 } else {
458 /*
459 * Simple linear assignment if memory
460 * controllers are not interleaved.
461 */
York Sun79a779b2014-08-01 15:51:00 -0700462 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune8dc17b2012-08-17 08:22:39 +0000463 total_ctlr_mem = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500464 pinfo->common_timing_params[i].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000465 current_mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500466 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
467 /* Compute DIMM base addresses. */
468 unsigned long long cap =
York Sune8dc17b2012-08-17 08:22:39 +0000469 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala124b0822008-08-26 15:01:29 -0500470 pinfo->dimm_params[i][j].base_address =
York Sune8dc17b2012-08-17 08:22:39 +0000471 current_mem_base;
472 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
473 current_mem_base += cap;
474 total_ctlr_mem += cap;
Kumar Gala124b0822008-08-26 15:01:29 -0500475 }
York Sune8dc17b2012-08-17 08:22:39 +0000476 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500477 pinfo->common_timing_params[i].total_mem =
York Sune8dc17b2012-08-17 08:22:39 +0000478 total_ctlr_mem;
479 total_mem += total_ctlr_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500480 }
481 }
York Sune8dc17b2012-08-17 08:22:39 +0000482 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500483
York Sune8dc17b2012-08-17 08:22:39 +0000484 return total_mem;
Kumar Gala124b0822008-08-26 15:01:29 -0500485}
486
York Sun01a82582013-03-25 07:39:35 +0000487/* Use weak function to allow board file to override the address assignment */
488__attribute__((weak, alias("__step_assign_addresses")))
489unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
490 unsigned int dbw_cap_adj[]);
491
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500492unsigned long long
Haiying Wang80ad4012010-12-01 10:35:31 -0500493fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
494 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -0500495{
496 unsigned int i, j;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500497 unsigned long long total_mem = 0;
York Sun79a779b2014-08-01 15:51:00 -0700498 int assert_reset = 0;
499 unsigned int first_ctrl = pinfo->first_ctrl;
500 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
501 __maybe_unused int retval;
502 __maybe_unused bool goodspd = false;
503 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
Kumar Gala124b0822008-08-26 15:01:29 -0500504
505 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
506 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun79a779b2014-08-01 15:51:00 -0700507 if (pinfo->board_need_mem_reset)
508 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala124b0822008-08-26 15:01:29 -0500509
510 /* data bus width capacity adjust shift amount */
York Sunfe845072016-12-28 08:43:45 -0800511 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala124b0822008-08-26 15:01:29 -0500512
York Sun79a779b2014-08-01 15:51:00 -0700513 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala124b0822008-08-26 15:01:29 -0500514 dbw_capacity_adjust[i] = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500515
516 debug("starting at step %u (%s)\n",
517 start_step, step_to_string(start_step));
518
519 switch (start_step) {
520 case STEP_GET_SPD:
York Sune73cc042011-06-07 09:42:16 +0800521#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala124b0822008-08-26 15:01:29 -0500522 /* STEP 1: Gather all DIMM SPD data */
York Sun79a779b2014-08-01 15:51:00 -0700523 for (i = first_ctrl; i <= last_ctrl; i++) {
524 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
525 dimm_slots_per_ctrl);
Kumar Gala124b0822008-08-26 15:01:29 -0500526 }
527
528 case STEP_COMPUTE_DIMM_PARMS:
529 /* STEP 2: Compute DIMM parameters from SPD data */
530
York Sun79a779b2014-08-01 15:51:00 -0700531 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500532 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500533 generic_spd_eeprom_t *spd =
534 &(pinfo->spd_installed_dimms[i][j]);
535 dimm_params_t *pdimm =
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200536 &(pinfo->dimm_params[i][j]);
York Sun2c0b62d2015-01-06 13:18:50 -0800537 retval = compute_dimm_parameters(
538 i, spd, pdimm, j);
York Sun09d8aa82011-06-07 09:42:17 +0800539#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun55eb5fa2015-03-19 09:30:26 -0700540 if (!j && retval) {
York Sune8dc17b2012-08-17 08:22:39 +0000541 printf("SPD error on controller %d! "
542 "Trying fallback to raw timing "
543 "calculation\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700544 retval = fsl_ddr_get_dimm_params(pdimm,
545 i, j);
York Sun09d8aa82011-06-07 09:42:17 +0800546 }
547#else
Kumar Gala124b0822008-08-26 15:01:29 -0500548 if (retval == 2) {
549 printf("Error: compute_dimm_parameters"
550 " non-zero returned FATAL value "
551 "for memctl=%u dimm=%u\n", i, j);
552 return 0;
553 }
York Sun09d8aa82011-06-07 09:42:17 +0800554#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500555 if (retval) {
556 debug("Warning: compute_dimm_parameters"
557 " non-zero return value for memctl=%u "
558 "dimm=%u\n", i, j);
York Sun79a779b2014-08-01 15:51:00 -0700559 } else {
560 goodspd = true;
Kumar Gala124b0822008-08-26 15:01:29 -0500561 }
562 }
563 }
York Sun79a779b2014-08-01 15:51:00 -0700564 if (!goodspd) {
565 /*
566 * No valid SPD found
567 * Throw an error if this is for main memory, i.e.
568 * first_ctrl == 0. Otherwise, siliently return 0
569 * as the memory size.
570 */
571 if (first_ctrl == 0)
572 printf("Error: No valid SPD detected.\n");
Kumar Gala124b0822008-08-26 15:01:29 -0500573
York Sun79a779b2014-08-01 15:51:00 -0700574 return 0;
575 }
Shaohui Xie24009042012-06-28 23:36:38 +0000576#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sune73cc042011-06-07 09:42:16 +0800577 case STEP_COMPUTE_DIMM_PARMS:
York Sun79a779b2014-08-01 15:51:00 -0700578 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sune73cc042011-06-07 09:42:16 +0800579 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
580 dimm_params_t *pdimm =
581 &(pinfo->dimm_params[i][j]);
582 fsl_ddr_get_dimm_params(pdimm, i, j);
583 }
584 }
585 debug("Filling dimm parameters from board specific file\n");
586#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500587 case STEP_COMPUTE_COMMON_PARMS:
588 /*
589 * STEP 3: Compute a common set of timing parameters
590 * suitable for all of the DIMMs on each memory controller
591 */
York Sun79a779b2014-08-01 15:51:00 -0700592 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500593 debug("Computing lowest common DIMM"
594 " parameters for memctl=%u\n", i);
York Sun2c0b62d2015-01-06 13:18:50 -0800595 compute_lowest_common_dimm_parameters
596 (i,
597 pinfo->dimm_params[i],
598 &timing_params[i],
599 CONFIG_DIMM_SLOTS_PER_CTLR);
Kumar Gala124b0822008-08-26 15:01:29 -0500600 }
601
602 case STEP_GATHER_OPTS:
603 /* STEP 4: Gather configuration requirements from user */
York Sun79a779b2014-08-01 15:51:00 -0700604 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500605 debug("Reloading memory controller "
606 "configuration options for memctl=%u\n", i);
607 /*
608 * This "reloads" the memory controller options
609 * to defaults. If the user "edits" an option,
610 * next_step points to the step after this,
611 * which is currently STEP_ASSIGN_ADDRESSES.
612 */
613 populate_memctl_options(
York Sun999273f2015-07-23 14:04:48 -0700614 &timing_params[i],
Haiying Wangfa440362008-10-03 12:36:55 -0400615 &pinfo->memctl_opts[i],
616 pinfo->dimm_params[i], i);
York Sun5e155552013-06-25 11:37:48 -0700617 /*
618 * For RDIMMs, JEDEC spec requires clocks to be stable
619 * before reset signal is deasserted. For the boards
620 * using fixed parameters, this function should be
621 * be called from board init file.
622 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530623 if (timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700624 assert_reset = 1;
625 }
York Sun79a779b2014-08-01 15:51:00 -0700626 if (assert_reset && !size_only) {
627 if (pinfo->board_mem_reset) {
628 debug("Asserting mem reset\n");
629 pinfo->board_mem_reset();
630 } else {
631 debug("Asserting mem reset missing\n");
632 }
Kumar Gala124b0822008-08-26 15:01:29 -0500633 }
York Sun5e155552013-06-25 11:37:48 -0700634
Kumar Gala124b0822008-08-26 15:01:29 -0500635 case STEP_ASSIGN_ADDRESSES:
636 /* STEP 5: Assign addresses to chip selects */
York Sune8dc17b2012-08-17 08:22:39 +0000637 check_interleaving_options(pinfo);
638 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sunedbeee12014-04-01 14:20:49 -0700639 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala124b0822008-08-26 15:01:29 -0500640
641 case STEP_COMPUTE_REGS:
642 /* STEP 6: compute controller register values */
York Sune8dc17b2012-08-17 08:22:39 +0000643 debug("FSL Memory ctrl register computation\n");
York Sun79a779b2014-08-01 15:51:00 -0700644 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500645 if (timing_params[i].ndimms_present == 0) {
646 memset(&ddr_reg[i], 0,
647 sizeof(fsl_ddr_cfg_regs_t));
648 continue;
649 }
650
York Sun2c0b62d2015-01-06 13:18:50 -0800651 compute_fsl_memctl_config_regs
652 (i,
653 &pinfo->memctl_opts[i],
654 &ddr_reg[i], &timing_params[i],
655 pinfo->dimm_params[i],
656 dbw_capacity_adjust[i],
657 size_only);
Kumar Gala124b0822008-08-26 15:01:29 -0500658 }
659
660 default:
661 break;
662 }
663
York Sune8dc17b2012-08-17 08:22:39 +0000664 {
Kumar Gala124b0822008-08-26 15:01:29 -0500665 /*
666 * Compute the amount of memory available just by
667 * looking for the highest valid CSn_BNDS value.
668 * This allows us to also experiment with using
669 * only CS0 when using dual-rank DIMMs.
670 */
671 unsigned int max_end = 0;
672
York Sun79a779b2014-08-01 15:51:00 -0700673 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500674 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
675 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
676 if (reg->cs[j].config & 0x80000000) {
677 unsigned int end;
York Sunc21a7392013-06-25 11:37:45 -0700678 /*
679 * 0xfffffff is a special value we put
680 * for unused bnds
681 */
682 if (reg->cs[j].bnds == 0xffffffff)
683 continue;
684 end = reg->cs[j].bnds & 0xffff;
Kumar Gala124b0822008-08-26 15:01:29 -0500685 if (end > max_end) {
686 max_end = end;
687 }
688 }
689 }
690 }
691
York Sun6446f1e2013-10-28 16:36:02 -0700692 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun79a779b2014-08-01 15:51:00 -0700693 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala124b0822008-08-26 15:01:29 -0500694 }
695
696 return total_mem;
697}
698
York Sun79a779b2014-08-01 15:51:00 -0700699phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala124b0822008-08-26 15:01:29 -0500700{
York Sun79a779b2014-08-01 15:51:00 -0700701 unsigned int i, first_ctrl, last_ctrl;
York Sun461c9392013-09-30 14:20:51 -0700702#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000703 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun461c9392013-09-30 14:20:51 -0700704#endif
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500705 unsigned long long total_memory;
York Sun79a779b2014-08-01 15:51:00 -0700706 int deassert_reset = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500707
York Sun79a779b2014-08-01 15:51:00 -0700708 first_ctrl = pinfo->first_ctrl;
709 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500710
711 /* Compute it once normally. */
York Sunbd495cf2011-09-16 13:21:35 -0700712#ifdef CONFIG_FSL_DDR_INTERACTIVE
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200713 if (tstc() && (getchar() == 'd')) { /* we got a key press of 'd' */
York Sun79a779b2014-08-01 15:51:00 -0700714 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yang43794382013-01-07 14:01:03 +0000715 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun79a779b2014-08-01 15:51:00 -0700716 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sun5025a8d2013-01-04 08:13:59 +0000717 } else
York Sunbd495cf2011-09-16 13:21:35 -0700718#endif
York Sun79a779b2014-08-01 15:51:00 -0700719 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500720
York Sun016095d2012-10-08 07:44:24 +0000721 /* setup 3-way interleaving before enabling DDRC */
York Sun79a779b2014-08-01 15:51:00 -0700722 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
723 case FSL_DDR_3WAY_1KB_INTERLEAVING:
724 case FSL_DDR_3WAY_4KB_INTERLEAVING:
725 case FSL_DDR_3WAY_8KB_INTERLEAVING:
726 fsl_ddr_set_intl3r(
727 pinfo->memctl_opts[first_ctrl].
728 memctl_interleaving_mode);
729 break;
730 default:
731 break;
York Sun016095d2012-10-08 07:44:24 +0000732 }
733
York Sun5e155552013-06-25 11:37:48 -0700734 /*
735 * Program configuration registers.
736 * JEDEC specs requires clocks to be stable before deasserting reset
737 * for RDIMMs. Clocks start after chip select is enabled and clock
738 * control register is set. During step 1, all controllers have their
739 * registers set but not enabled. Step 2 proceeds after deasserting
740 * reset through board FPGA or GPIO.
741 * For non-registered DIMMs, initialization can go through but it is
742 * also OK to follow the same flow.
743 */
York Sun79a779b2014-08-01 15:51:00 -0700744 if (pinfo->board_need_mem_reset)
745 deassert_reset = pinfo->board_need_mem_reset();
746 for (i = first_ctrl; i <= last_ctrl; i++) {
747 if (pinfo->common_timing_params[i].all_dimms_registered)
York Sun5e155552013-06-25 11:37:48 -0700748 deassert_reset = 1;
749 }
York Sun79a779b2014-08-01 15:51:00 -0700750 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala124b0822008-08-26 15:01:29 -0500751 debug("Programming controller %u\n", i);
York Sun79a779b2014-08-01 15:51:00 -0700752 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -0500753 debug("No dimms present on controller %u; "
754 "skipping programming\n", i);
755 continue;
756 }
York Sun5e155552013-06-25 11:37:48 -0700757 /*
758 * The following call with step = 1 returns before enabling
759 * the controller. It has to finish with step = 2 later.
760 */
York Sun79a779b2014-08-01 15:51:00 -0700761 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sun5e155552013-06-25 11:37:48 -0700762 deassert_reset ? 1 : 0);
763 }
764 if (deassert_reset) {
765 /* Use board FPGA or GPIO to deassert reset signal */
York Sun79a779b2014-08-01 15:51:00 -0700766 if (pinfo->board_mem_de_reset) {
767 debug("Deasserting mem reset\n");
768 pinfo->board_mem_de_reset();
769 } else {
770 debug("Deasserting mem reset missing\n");
771 }
772 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun5e155552013-06-25 11:37:48 -0700773 /* Call with step = 2 to continue initialization */
York Sun79a779b2014-08-01 15:51:00 -0700774 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sun5e155552013-06-25 11:37:48 -0700775 i, 2);
776 }
Kumar Gala124b0822008-08-26 15:01:29 -0500777 }
778
York Sun8ced0502015-01-06 13:18:55 -0800779#ifdef CONFIG_FSL_DDR_SYNC_REFRESH
780 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
781#endif
782
York Sun461c9392013-09-30 14:20:51 -0700783#ifdef CONFIG_PPC
York Sune8dc17b2012-08-17 08:22:39 +0000784 /* program LAWs */
York Sun79a779b2014-08-01 15:51:00 -0700785 for (i = first_ctrl; i <= last_ctrl; i++) {
786 if (pinfo->memctl_opts[i].memctl_interleaving) {
787 switch (pinfo->memctl_opts[i].
788 memctl_interleaving_mode) {
York Sune8dc17b2012-08-17 08:22:39 +0000789 case FSL_DDR_CACHE_LINE_INTERLEAVING:
790 case FSL_DDR_PAGE_INTERLEAVING:
791 case FSL_DDR_BANK_INTERLEAVING:
792 case FSL_DDR_SUPERBANK_INTERLEAVING:
York Sun79a779b2014-08-01 15:51:00 -0700793 if (i % 2)
794 break;
York Sune8dc17b2012-08-17 08:22:39 +0000795 if (i == 0) {
796 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun79a779b2014-08-01 15:51:00 -0700797 fsl_ddr_set_lawbar(
798 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000799 law_memctl, i);
York Sun79a779b2014-08-01 15:51:00 -0700800 }
York Sunfe845072016-12-28 08:43:45 -0800801#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun79a779b2014-08-01 15:51:00 -0700802 else if (i == 2) {
York Sune8dc17b2012-08-17 08:22:39 +0000803 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
York Sun79a779b2014-08-01 15:51:00 -0700804 fsl_ddr_set_lawbar(
805 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000806 law_memctl, i);
807 }
York Sun79a779b2014-08-01 15:51:00 -0700808#endif
York Sune8dc17b2012-08-17 08:22:39 +0000809 break;
810 case FSL_DDR_3WAY_1KB_INTERLEAVING:
811 case FSL_DDR_3WAY_4KB_INTERLEAVING:
812 case FSL_DDR_3WAY_8KB_INTERLEAVING:
813 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
814 if (i == 0) {
York Sun79a779b2014-08-01 15:51:00 -0700815 fsl_ddr_set_lawbar(
816 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000817 law_memctl, i);
818 }
819 break;
820 case FSL_DDR_4WAY_1KB_INTERLEAVING:
821 case FSL_DDR_4WAY_4KB_INTERLEAVING:
822 case FSL_DDR_4WAY_8KB_INTERLEAVING:
823 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
824 if (i == 0)
York Sun79a779b2014-08-01 15:51:00 -0700825 fsl_ddr_set_lawbar(
826 &pinfo->common_timing_params[i],
York Sune8dc17b2012-08-17 08:22:39 +0000827 law_memctl, i);
828 /* place holder for future 4-way interleaving */
829 break;
830 default:
831 break;
832 }
833 } else {
834 switch (i) {
835 case 0:
836 law_memctl = LAW_TRGT_IF_DDR_1;
837 break;
838 case 1:
839 law_memctl = LAW_TRGT_IF_DDR_2;
840 break;
841 case 2:
842 law_memctl = LAW_TRGT_IF_DDR_3;
843 break;
844 case 3:
845 law_memctl = LAW_TRGT_IF_DDR_4;
846 break;
847 default:
848 break;
849 }
York Sun79a779b2014-08-01 15:51:00 -0700850 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
851 law_memctl, i);
Kumar Gala124b0822008-08-26 15:01:29 -0500852 }
853 }
York Sun461c9392013-09-30 14:20:51 -0700854#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500855
York Sune8dc17b2012-08-17 08:22:39 +0000856 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500857
858#if !defined(CONFIG_PHYS_64BIT)
859 /* Check for 4G or more. Bad. */
York Sun79a779b2014-08-01 15:51:00 -0700860 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
Shruti Kanetkar3adfb912013-08-15 11:25:37 -0500861 puts("Detected ");
862 print_size(total_memory, " of memory\n");
Becky Bruce2d8ecac2010-12-17 17:17:59 -0600863 printf(" This U-Boot only supports < 4G of DDR\n");
864 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
Simon Glassd35f3382017-04-06 12:47:05 -0600865 printf(" "); /* re-align to match init_dram print */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500866 total_memory = CONFIG_MAX_MEM_MAPPED;
867 }
868#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500869
870 return total_memory;
871}
Haiying Wang80ad4012010-12-01 10:35:31 -0500872
873/*
York Sun79a779b2014-08-01 15:51:00 -0700874 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassd35f3382017-04-06 12:47:05 -0600875 * called by dram_init() in the board file.
York Sun79a779b2014-08-01 15:51:00 -0700876 *
877 * It returns amount of memory configured in bytes.
878 */
879phys_size_t fsl_ddr_sdram(void)
880{
881 fsl_ddr_info_t info;
882
883 /* Reset info structure. */
884 memset(&info, 0, sizeof(fsl_ddr_info_t));
885 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
886 info.first_ctrl = 0;
887 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
888 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
889 info.board_need_mem_reset = board_need_mem_reset;
890 info.board_mem_reset = board_assert_mem_reset;
891 info.board_mem_de_reset = board_deassert_mem_reset;
York Sund957a672015-11-04 09:53:10 -0800892 remove_unused_controllers(&info);
York Sun79a779b2014-08-01 15:51:00 -0700893
894 return __fsl_ddr_sdram(&info);
895}
896
897#ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
898phys_size_t fsl_other_ddr_sdram(unsigned long long base,
899 unsigned int first_ctrl,
900 unsigned int num_ctrls,
901 unsigned int dimm_slots_per_ctrl,
902 int (*board_need_reset)(void),
903 void (*board_reset)(void),
904 void (*board_de_reset)(void))
905{
906 fsl_ddr_info_t info;
907
908 /* Reset info structure. */
909 memset(&info, 0, sizeof(fsl_ddr_info_t));
910 info.mem_base = base;
911 info.first_ctrl = first_ctrl;
912 info.num_ctrls = num_ctrls;
913 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
914 info.board_need_mem_reset = board_need_reset;
915 info.board_mem_reset = board_reset;
916 info.board_mem_de_reset = board_de_reset;
917
918 return __fsl_ddr_sdram(&info);
919}
920#endif
921
922/*
923 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
924 * size of the total memory without setting ddr control registers.
Haiying Wang80ad4012010-12-01 10:35:31 -0500925 */
926phys_size_t
927fsl_ddr_sdram_size(void)
928{
929 fsl_ddr_info_t info;
930 unsigned long long total_memory = 0;
931
932 memset(&info, 0 , sizeof(fsl_ddr_info_t));
York Sun79a779b2014-08-01 15:51:00 -0700933 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
934 info.first_ctrl = 0;
935 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
936 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
937 info.board_need_mem_reset = NULL;
Ed Swarthout7ae7a0e2016-01-14 12:28:04 -0600938 remove_unused_controllers(&info);
Haiying Wang80ad4012010-12-01 10:35:31 -0500939
940 /* Compute it once normally. */
941 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
942
943 return total_memory;
944}