blob: cd332718b640469af9e2494edb02daa97902136a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002/*
York Sun34e026f2014-03-27 17:54:47 -07003 * Copyright 2008-2014 Freescale Semiconductor, Inc.
Priyanka Singh71b255b2021-08-19 11:39:03 +05304 * Copyright 2021 NXP
Kumar Gala58e5e9a2008-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>
Simon Glass4e4bf942022-07-31 12:28:48 -060014#include <display_options.h>
Heiko Schocher09aa70f2019-08-26 17:28:34 +020015#include <dm.h>
Kumar Galac39f44d2011-01-31 22:18:47 -060016#include <i2c.h>
York Sun5614e712013-09-30 09:22:09 -070017#include <fsl_ddr_sdram.h>
York Sun5614e712013-09-30 09:22:09 -070018#include <fsl_ddr.h>
Simon Glass691d7192020-05-10 11:40:02 -060019#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Simon Glasscd93d622020-05-10 11:40:13 -060021#include <asm/bitops.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050022
York Sun6b9e3092014-02-10 13:59:43 -080023/*
Tom Rini6cc04542022-10-28 20:27:13 -040024 * CFG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
Tom Rini65cc0e22022-11-16 13:10:41 -050025 * of DDR controllers. It is the same as CFG_SYS_DDR_SDRAM_BASE for
York Sun6b9e3092014-02-10 13:59:43 -080026 * all Power SoCs. But it could be different for ARM SoCs. For example,
27 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
28 * 0x00_8000_0000 ~ 0x00_ffff_ffff
29 * 0x80_8000_0000 ~ 0xff_ffff_ffff
30 */
Tom Rini6cc04542022-10-28 20:27:13 -040031#ifndef CFG_SYS_FSL_DDR_SDRAM_BASE_PHY
Mario Six133ec602019-01-21 09:18:16 +010032#ifdef CONFIG_MPC83xx
Tom Riniaa6e94d2022-11-16 13:10:37 -050033#define CFG_SYS_FSL_DDR_SDRAM_BASE_PHY CFG_SYS_SDRAM_BASE
Mario Six133ec602019-01-21 09:18:16 +010034#else
Tom Rini65cc0e22022-11-16 13:10:41 -050035#define CFG_SYS_FSL_DDR_SDRAM_BASE_PHY CFG_SYS_DDR_SDRAM_BASE
York Sun6b9e3092014-02-10 13:59:43 -080036#endif
Mario Six133ec602019-01-21 09:18:16 +010037#endif
York Sun6b9e3092014-02-10 13:59:43 -080038
York Sun9ac4ffb2013-09-30 14:20:51 -070039#ifdef CONFIG_PPC
40#include <asm/fsl_law.h>
41
York Suna4c66502012-08-17 08:22:39 +000042void fsl_ddr_set_lawbar(
Kumar Gala58e5e9a2008-08-26 15:01:29 -050043 const common_timing_params_t *memctl_common_params,
44 unsigned int memctl_interleaved,
45 unsigned int ctrl_num);
York Sun9ac4ffb2013-09-30 14:20:51 -070046#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -050047
York Sun9ac4ffb2013-09-30 14:20:51 -070048void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac39f44d2011-01-31 22:18:47 -060049#if defined(SPD_EEPROM_ADDRESS) || \
50 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
51 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
York Sun51370d52016-12-28 08:43:45 -080052#if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
53u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060054 [0][0] = SPD_EEPROM_ADDRESS,
55};
York Sun51370d52016-12-28 08:43:45 -080056#elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
57u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sun639f3302011-08-26 11:32:41 -070058 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
59 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
60};
York Sun51370d52016-12-28 08:43:45 -080061#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
62u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060063 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
64 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
65};
York Sun51370d52016-12-28 08:43:45 -080066#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
67u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060068 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
69 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
70 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
71 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
72};
York Sun51370d52016-12-28 08:43:45 -080073#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
74u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Suna4c66502012-08-17 08:22:39 +000075 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
76 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
77 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
78};
York Sun51370d52016-12-28 08:43:45 -080079#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
80u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Suna4c66502012-08-17 08:22:39 +000081 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
82 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
83 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
84 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
85 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
86 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
87};
88
Kumar Galac39f44d2011-01-31 22:18:47 -060089#endif
90
Igor Opaniuk2147a162021-02-09 13:52:45 +020091#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-08-26 17:28:34 +020092#define DEV_TYPE struct udevice
93#else
94/* Local udevice */
95struct ludevice {
96 u8 chip;
97};
98
99#define DEV_TYPE struct ludevice
100
101#endif
102
York Sun34e026f2014-03-27 17:54:47 -0700103#define SPD_SPA0_ADDRESS 0x36
104#define SPD_SPA1_ADDRESS 0x37
105
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200106static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
107 int alen, uint8_t *buf, int len)
108{
109 int ret;
110
Igor Opaniuk2147a162021-02-09 13:52:45 +0200111#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200112 ret = dm_i2c_read(dev, 0, buf, len);
113#else
114 ret = i2c_read(dev->chip, addr, alen, buf, len);
115#endif
116
117 return ret;
118}
119
120#ifdef CONFIG_SYS_FSL_DDR4
121static int ddr_i2c_dummy_write(unsigned int chip_addr)
122{
123 uint8_t buf = 0;
124
Igor Opaniuk2147a162021-02-09 13:52:45 +0200125#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200126 struct udevice *dev;
127 int ret;
128
129 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
130 1, &dev);
131 if (ret) {
132 printf("%s: Cannot find udev for a bus %d\n", __func__,
133 CONFIG_SYS_SPD_BUS_NUM);
134 return ret;
135 }
136
137 return dm_i2c_write(dev, 0, &buf, 1);
138#else
139 return i2c_write(chip_addr, 0, 1, &buf, 1);
140#endif
141
142 return 0;
143}
144#endif
145
Kumar Galac39f44d2011-01-31 22:18:47 -0600146static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
147{
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200148 int ret;
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200149 DEV_TYPE *dev;
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200150
Igor Opaniuk2147a162021-02-09 13:52:45 +0200151#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200152 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
153 1, &dev);
154 if (ret) {
155 printf("%s: Cannot find udev for a bus %d\n", __func__,
156 CONFIG_SYS_SPD_BUS_NUM);
157 return;
158 }
159#else /* Non DM I2C support - will be removed */
160 struct ludevice ldev = {
161 .chip = i2c_address,
162 };
163 dev = &ldev;
164
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200165 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800166#endif
167
York Sun34e026f2014-03-27 17:54:47 -0700168#ifdef CONFIG_SYS_FSL_DDR4
169 /*
170 * DDR4 SPD has 384 to 512 bytes
171 * To access the lower 256 bytes, we need to set EE page address to 0
172 * To access the upper 256 bytes, we need to set EE page address to 1
173 * See Jedec standar No. 21-C for detail
174 */
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200175 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
176 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
York Sun34e026f2014-03-27 17:54:47 -0700177 if (!ret) {
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200178 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
179 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
180 min(256,
181 (int)sizeof(generic_spd_eeprom_t)
182 - 256));
York Sun34e026f2014-03-27 17:54:47 -0700183 }
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800184
185#else
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200186 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
187 sizeof(generic_spd_eeprom_t));
York Sun34e026f2014-03-27 17:54:47 -0700188#endif
Kumar Galac39f44d2011-01-31 22:18:47 -0600189
190 if (ret) {
York Sun82968a72012-10-08 07:44:28 +0000191 if (i2c_address ==
192#ifdef SPD_EEPROM_ADDRESS
193 SPD_EEPROM_ADDRESS
194#elif defined(SPD_EEPROM_ADDRESS1)
195 SPD_EEPROM_ADDRESS1
196#endif
197 ) {
198 printf("DDR: failed to read SPD from address %u\n",
199 i2c_address);
200 } else {
201 debug("DDR: failed to read SPD from address %u\n",
202 i2c_address);
203 }
Kumar Galac39f44d2011-01-31 22:18:47 -0600204 memset(spd, 0, sizeof(generic_spd_eeprom_t));
205 }
206}
207
208__attribute__((weak, alias("__get_spd")))
209void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
210
York Sunb92557c2015-05-28 14:54:08 +0530211/* This function allows boards to update SPD address */
212__weak void update_spd_address(unsigned int ctrl_num,
213 unsigned int slot,
214 unsigned int *addr)
215{
216}
217
Kumar Galac39f44d2011-01-31 22:18:47 -0600218void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun1d71efb2014-08-01 15:51:00 -0700219 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600220{
221 unsigned int i;
222 unsigned int i2c_address = 0;
223
York Sun51370d52016-12-28 08:43:45 -0800224 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600225 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
226 return;
227 }
228
York Sun1d71efb2014-08-01 15:51:00 -0700229 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600230 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb92557c2015-05-28 14:54:08 +0530231 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac39f44d2011-01-31 22:18:47 -0600232 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
233 }
234}
235#else
236void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun1d71efb2014-08-01 15:51:00 -0700237 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600238{
239}
240#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500241
242/*
243 * ASSUMPTIONS:
244 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
245 * - Same memory data bus width on all controllers
246 *
247 * NOTES:
248 *
249 * The memory controller and associated documentation use confusing
250 * terminology when referring to the orgranization of DRAM.
251 *
252 * Here is a terminology translation table:
253 *
254 * memory controller/documention |industry |this code |signals
255 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200256 * physical bank/bank |rank |rank |chip select (CS)
257 * logical bank/sub-bank |bank |bank |bank address (BA)
258 * page/row |row |page |row address
259 * ??? |column |column |column address
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500260 *
261 * The naming confusion is further exacerbated by the descriptions of the
262 * memory controller interleaving feature, where accesses are interleaved
263 * _BETWEEN_ two seperate memory controllers. This is configured only in
264 * CS0_CONFIG[INTLV_CTL] of each memory controller.
265 *
266 * memory controller documentation | number of chip selects
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200267 * | per memory controller supported
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500268 * --------------------------------|-----------------------------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200269 * cache line interleaving | 1 (CS0 only)
270 * page interleaving | 1 (CS0 only)
271 * bank interleaving | 1 (CS0 only)
272 * superbank interleraving | depends on bank (chip select)
273 * | interleraving [rank interleaving]
274 * | mode used on every memory controller
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500275 *
276 * Even further confusing is the existence of the interleaving feature
277 * _WITHIN_ each memory controller. The feature is referred to in
278 * documentation as chip select interleaving or bank interleaving,
279 * although it is configured in the DDR_SDRAM_CFG field.
280 *
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200281 * Name of field | documentation name | this code
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500282 * -----------------------------|-----------------------|------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200283 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
284 * | interleaving
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500285 */
286
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500287const char *step_string_tbl[] = {
288 "STEP_GET_SPD",
289 "STEP_COMPUTE_DIMM_PARMS",
290 "STEP_COMPUTE_COMMON_PARMS",
291 "STEP_GATHER_OPTS",
292 "STEP_ASSIGN_ADDRESSES",
293 "STEP_COMPUTE_REGS",
294 "STEP_PROGRAM_REGS",
295 "STEP_ALL"
296};
297
298const char * step_to_string(unsigned int step) {
299
300 unsigned int s = __ilog2(step);
301
Priyanka Singh71b255b2021-08-19 11:39:03 +0530302 if (s <= 31) {
303 if ((1 << s) != step)
304 return step_string_tbl[7];
305 } else {
306 if ((1 << (s - 32)) != step)
307 return step_string_tbl[7];
308 }
York Sun349689b2014-04-01 14:20:49 -0700309 if (s >= ARRAY_SIZE(step_string_tbl)) {
310 printf("Error for the step in %s\n", __func__);
311 s = 0;
312 }
313
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500314 return step_string_tbl[s];
315}
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500316
York Sunef002272013-03-25 07:39:35 +0000317static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Suna4c66502012-08-17 08:22:39 +0000318 unsigned int dbw_cap_adj[])
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500319{
York Sun1d71efb2014-08-01 15:51:00 -0700320 unsigned int i, j;
York Suna4c66502012-08-17 08:22:39 +0000321 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
322 unsigned long long rank_density, ctlr_density = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700323 unsigned int first_ctrl = pinfo->first_ctrl;
324 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500325
326 /*
327 * If a reduced data width is requested, but the SPD
328 * specifies a physically wider device, adjust the
329 * computed dimm capacities accordingly before
330 * assigning addresses.
331 */
York Sun1d71efb2014-08-01 15:51:00 -0700332 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500333 unsigned int found = 0;
334
335 switch (pinfo->memctl_opts[i].data_bus_width) {
336 case 2:
337 /* 16-bit */
York Sun51d498f2011-05-27 07:25:51 +0800338 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
339 unsigned int dw;
340 if (!pinfo->dimm_params[i][j].n_ranks)
341 continue;
342 dw = pinfo->dimm_params[i][j].primary_sdram_width;
343 if ((dw == 72 || dw == 64)) {
344 dbw_cap_adj[i] = 2;
345 break;
346 } else if ((dw == 40 || dw == 32)) {
347 dbw_cap_adj[i] = 1;
348 break;
349 }
350 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500351 break;
352
353 case 1:
354 /* 32-bit */
355 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
356 unsigned int dw;
357 dw = pinfo->dimm_params[i][j].data_width;
358 if (pinfo->dimm_params[i][j].n_ranks
359 && (dw == 72 || dw == 64)) {
360 /*
361 * FIXME: can't really do it
362 * like this because this just
363 * further reduces the memory
364 */
365 found = 1;
366 break;
367 }
368 }
369 if (found) {
370 dbw_cap_adj[i] = 1;
371 }
372 break;
373
374 case 0:
375 /* 64-bit */
376 break;
377
378 default:
379 printf("unexpected data bus width "
380 "specified controller %u\n", i);
381 return 1;
382 }
York Suna4c66502012-08-17 08:22:39 +0000383 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500384 }
385
York Sun1d71efb2014-08-01 15:51:00 -0700386 current_mem_base = pinfo->mem_base;
York Suna4c66502012-08-17 08:22:39 +0000387 total_mem = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700388 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
389 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
390 dbw_cap_adj[first_ctrl];
391 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
York Suna4c66502012-08-17 08:22:39 +0000392 FSL_DDR_CS0_CS1_CS2_CS3) {
393 case FSL_DDR_CS0_CS1_CS2_CS3:
394 ctlr_density = 4 * rank_density;
395 break;
396 case FSL_DDR_CS0_CS1:
397 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
398 ctlr_density = 2 * rank_density;
399 break;
400 case FSL_DDR_CS2_CS3:
401 default:
402 ctlr_density = rank_density;
403 break;
404 }
405 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
406 rank_density, ctlr_density);
York Sun1d71efb2014-08-01 15:51:00 -0700407 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000408 if (pinfo->memctl_opts[i].memctl_interleaving) {
409 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sun6b1e1252014-02-10 13:59:44 -0800410 case FSL_DDR_256B_INTERLEAVING:
York Suna4c66502012-08-17 08:22:39 +0000411 case FSL_DDR_CACHE_LINE_INTERLEAVING:
412 case FSL_DDR_PAGE_INTERLEAVING:
413 case FSL_DDR_BANK_INTERLEAVING:
414 case FSL_DDR_SUPERBANK_INTERLEAVING:
415 total_ctlr_mem = 2 * ctlr_density;
416 break;
417 case FSL_DDR_3WAY_1KB_INTERLEAVING:
418 case FSL_DDR_3WAY_4KB_INTERLEAVING:
419 case FSL_DDR_3WAY_8KB_INTERLEAVING:
420 total_ctlr_mem = 3 * ctlr_density;
421 break;
422 case FSL_DDR_4WAY_1KB_INTERLEAVING:
423 case FSL_DDR_4WAY_4KB_INTERLEAVING:
424 case FSL_DDR_4WAY_8KB_INTERLEAVING:
425 total_ctlr_mem = 4 * ctlr_density;
426 break;
427 default:
428 panic("Unknown interleaving mode");
429 }
430 pinfo->common_timing_params[i].base_address =
431 current_mem_base;
432 pinfo->common_timing_params[i].total_mem =
433 total_ctlr_mem;
434 total_mem = current_mem_base + total_ctlr_mem;
435 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
436 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
437 } else {
438 /* when 3rd controller not interleaved */
439 current_mem_base = total_mem;
440 total_ctlr_mem = 0;
441 pinfo->common_timing_params[i].base_address =
442 current_mem_base;
443 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
444 unsigned long long cap =
445 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
446 pinfo->dimm_params[i][j].base_address =
447 current_mem_base;
448 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
449 current_mem_base += cap;
450 total_ctlr_mem += cap;
451 }
452 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
453 pinfo->common_timing_params[i].total_mem =
454 total_ctlr_mem;
455 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500456 }
457 }
458 } else {
459 /*
460 * Simple linear assignment if memory
461 * controllers are not interleaved.
462 */
York Sun1d71efb2014-08-01 15:51:00 -0700463 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000464 total_ctlr_mem = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500465 pinfo->common_timing_params[i].base_address =
York Suna4c66502012-08-17 08:22:39 +0000466 current_mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500467 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
468 /* Compute DIMM base addresses. */
469 unsigned long long cap =
York Suna4c66502012-08-17 08:22:39 +0000470 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500471 pinfo->dimm_params[i][j].base_address =
York Suna4c66502012-08-17 08:22:39 +0000472 current_mem_base;
473 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
474 current_mem_base += cap;
475 total_ctlr_mem += cap;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500476 }
York Suna4c66502012-08-17 08:22:39 +0000477 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500478 pinfo->common_timing_params[i].total_mem =
York Suna4c66502012-08-17 08:22:39 +0000479 total_ctlr_mem;
480 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500481 }
482 }
York Suna4c66502012-08-17 08:22:39 +0000483 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500484
York Suna4c66502012-08-17 08:22:39 +0000485 return total_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500486}
487
York Sunef002272013-03-25 07:39:35 +0000488/* Use weak function to allow board file to override the address assignment */
489__attribute__((weak, alias("__step_assign_addresses")))
490unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
491 unsigned int dbw_cap_adj[]);
492
Kumar Galae7563af2009-06-11 23:42:35 -0500493unsigned long long
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500494fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
495 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500496{
497 unsigned int i, j;
Kumar Galae7563af2009-06-11 23:42:35 -0500498 unsigned long long total_mem = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700499 int assert_reset = 0;
500 unsigned int first_ctrl = pinfo->first_ctrl;
501 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
502 __maybe_unused int retval;
503 __maybe_unused bool goodspd = false;
504 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500505
506 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
507 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun1d71efb2014-08-01 15:51:00 -0700508 if (pinfo->board_need_mem_reset)
509 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500510
511 /* data bus width capacity adjust shift amount */
York Sun51370d52016-12-28 08:43:45 -0800512 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500513
York Sun1d71efb2014-08-01 15:51:00 -0700514 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500515 dbw_capacity_adjust[i] = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500516
517 debug("starting at step %u (%s)\n",
518 start_step, step_to_string(start_step));
519
520 switch (start_step) {
521 case STEP_GET_SPD:
York Sun1b3e3c42011-06-07 09:42:16 +0800522#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500523 /* STEP 1: Gather all DIMM SPD data */
York Sun1d71efb2014-08-01 15:51:00 -0700524 for (i = first_ctrl; i <= last_ctrl; i++) {
525 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
526 dimm_slots_per_ctrl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500527 }
528
529 case STEP_COMPUTE_DIMM_PARMS:
530 /* STEP 2: Compute DIMM parameters from SPD data */
531
York Sun1d71efb2014-08-01 15:51:00 -0700532 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500533 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500534 generic_spd_eeprom_t *spd =
535 &(pinfo->spd_installed_dimms[i][j]);
536 dimm_params_t *pdimm =
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200537 &(pinfo->dimm_params[i][j]);
York Sun03e664d2015-01-06 13:18:50 -0800538 retval = compute_dimm_parameters(
539 i, spd, pdimm, j);
York Sunf2d264b2011-06-07 09:42:17 +0800540#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun66869f92015-03-19 09:30:26 -0700541 if (!j && retval) {
York Suna4c66502012-08-17 08:22:39 +0000542 printf("SPD error on controller %d! "
543 "Trying fallback to raw timing "
544 "calculation\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700545 retval = fsl_ddr_get_dimm_params(pdimm,
546 i, j);
York Sunf2d264b2011-06-07 09:42:17 +0800547 }
548#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500549 if (retval == 2) {
550 printf("Error: compute_dimm_parameters"
551 " non-zero returned FATAL value "
552 "for memctl=%u dimm=%u\n", i, j);
553 return 0;
554 }
York Sunf2d264b2011-06-07 09:42:17 +0800555#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500556 if (retval) {
557 debug("Warning: compute_dimm_parameters"
558 " non-zero return value for memctl=%u "
559 "dimm=%u\n", i, j);
York Sun1d71efb2014-08-01 15:51:00 -0700560 } else {
561 goodspd = true;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500562 }
563 }
564 }
York Sun1d71efb2014-08-01 15:51:00 -0700565 if (!goodspd) {
566 /*
567 * No valid SPD found
568 * Throw an error if this is for main memory, i.e.
569 * first_ctrl == 0. Otherwise, siliently return 0
570 * as the memory size.
571 */
572 if (first_ctrl == 0)
573 printf("Error: No valid SPD detected.\n");
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500574
York Sun1d71efb2014-08-01 15:51:00 -0700575 return 0;
576 }
Shaohui Xie98de3692012-06-28 23:36:38 +0000577#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sun1b3e3c42011-06-07 09:42:16 +0800578 case STEP_COMPUTE_DIMM_PARMS:
York Sun1d71efb2014-08-01 15:51:00 -0700579 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun1b3e3c42011-06-07 09:42:16 +0800580 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
581 dimm_params_t *pdimm =
582 &(pinfo->dimm_params[i][j]);
583 fsl_ddr_get_dimm_params(pdimm, i, j);
584 }
585 }
586 debug("Filling dimm parameters from board specific file\n");
587#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500588 case STEP_COMPUTE_COMMON_PARMS:
589 /*
590 * STEP 3: Compute a common set of timing parameters
591 * suitable for all of the DIMMs on each memory controller
592 */
York Sun1d71efb2014-08-01 15:51:00 -0700593 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500594 debug("Computing lowest common DIMM"
595 " parameters for memctl=%u\n", i);
York Sun03e664d2015-01-06 13:18:50 -0800596 compute_lowest_common_dimm_parameters
597 (i,
598 pinfo->dimm_params[i],
599 &timing_params[i],
600 CONFIG_DIMM_SLOTS_PER_CTLR);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500601 }
602
603 case STEP_GATHER_OPTS:
604 /* STEP 4: Gather configuration requirements from user */
York Sun1d71efb2014-08-01 15:51:00 -0700605 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500606 debug("Reloading memory controller "
607 "configuration options for memctl=%u\n", i);
608 /*
609 * This "reloads" the memory controller options
610 * to defaults. If the user "edits" an option,
611 * next_step points to the step after this,
612 * which is currently STEP_ASSIGN_ADDRESSES.
613 */
614 populate_memctl_options(
York Sun56848422015-07-23 14:04:48 -0700615 &timing_params[i],
Haiying Wangdfb49102008-10-03 12:36:55 -0400616 &pinfo->memctl_opts[i],
617 pinfo->dimm_params[i], i);
York Sunc63e1372013-06-25 11:37:48 -0700618 /*
619 * For RDIMMs, JEDEC spec requires clocks to be stable
620 * before reset signal is deasserted. For the boards
621 * using fixed parameters, this function should be
622 * be called from board init file.
623 */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530624 if (timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700625 assert_reset = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500626 }
York Sun1d71efb2014-08-01 15:51:00 -0700627 if (assert_reset && !size_only) {
628 if (pinfo->board_mem_reset) {
629 debug("Asserting mem reset\n");
630 pinfo->board_mem_reset();
631 } else {
632 debug("Asserting mem reset missing\n");
633 }
York Sunc63e1372013-06-25 11:37:48 -0700634 }
635
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500636 case STEP_ASSIGN_ADDRESSES:
637 /* STEP 5: Assign addresses to chip selects */
York Suna4c66502012-08-17 08:22:39 +0000638 check_interleaving_options(pinfo);
639 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sun349689b2014-04-01 14:20:49 -0700640 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500641
642 case STEP_COMPUTE_REGS:
643 /* STEP 6: compute controller register values */
York Suna4c66502012-08-17 08:22:39 +0000644 debug("FSL Memory ctrl register computation\n");
York Sun1d71efb2014-08-01 15:51:00 -0700645 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500646 if (timing_params[i].ndimms_present == 0) {
647 memset(&ddr_reg[i], 0,
648 sizeof(fsl_ddr_cfg_regs_t));
649 continue;
650 }
651
York Sun03e664d2015-01-06 13:18:50 -0800652 compute_fsl_memctl_config_regs
653 (i,
654 &pinfo->memctl_opts[i],
655 &ddr_reg[i], &timing_params[i],
656 pinfo->dimm_params[i],
657 dbw_capacity_adjust[i],
658 size_only);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500659 }
660
661 default:
662 break;
663 }
664
York Suna4c66502012-08-17 08:22:39 +0000665 {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500666 /*
667 * Compute the amount of memory available just by
668 * looking for the highest valid CSn_BNDS value.
669 * This allows us to also experiment with using
670 * only CS0 when using dual-rank DIMMs.
671 */
672 unsigned int max_end = 0;
673
York Sun1d71efb2014-08-01 15:51:00 -0700674 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500675 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
676 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
677 if (reg->cs[j].config & 0x80000000) {
678 unsigned int end;
York Sund8556db2013-06-25 11:37:45 -0700679 /*
680 * 0xfffffff is a special value we put
681 * for unused bnds
682 */
683 if (reg->cs[j].bnds == 0xffffffff)
684 continue;
685 end = reg->cs[j].bnds & 0xffff;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500686 if (end > max_end) {
687 max_end = end;
688 }
689 }
690 }
691 }
692
York Sun00ec3fd2013-10-28 16:36:02 -0700693 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun1d71efb2014-08-01 15:51:00 -0700694 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500695 }
696
697 return total_mem;
698}
699
York Sun1d71efb2014-08-01 15:51:00 -0700700phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500701{
York Sun1d71efb2014-08-01 15:51:00 -0700702 unsigned int i, first_ctrl, last_ctrl;
York Sun9ac4ffb2013-09-30 14:20:51 -0700703#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000704 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun9ac4ffb2013-09-30 14:20:51 -0700705#endif
Kumar Galae7563af2009-06-11 23:42:35 -0500706 unsigned long long total_memory;
York Sun1d71efb2014-08-01 15:51:00 -0700707 int deassert_reset = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500708
York Sun1d71efb2014-08-01 15:51:00 -0700709 first_ctrl = pinfo->first_ctrl;
710 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500711
712 /* Compute it once normally. */
York Sun6f5e1dc2011-09-16 13:21:35 -0700713#ifdef CONFIG_FSL_DDR_INTERACTIVE
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200714 if (tstc() && (getchar() == 'd')) { /* we got a key press of 'd' */
York Sun1d71efb2014-08-01 15:51:00 -0700715 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yange8ba6c52013-01-07 14:01:03 +0000716 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun1d71efb2014-08-01 15:51:00 -0700717 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sune750cfa2013-01-04 08:13:59 +0000718 } else
York Sun6f5e1dc2011-09-16 13:21:35 -0700719#endif
York Sun1d71efb2014-08-01 15:51:00 -0700720 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500721
York Sunf31cfd12012-10-08 07:44:24 +0000722 /* setup 3-way interleaving before enabling DDRC */
York Sun1d71efb2014-08-01 15:51:00 -0700723 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
724 case FSL_DDR_3WAY_1KB_INTERLEAVING:
725 case FSL_DDR_3WAY_4KB_INTERLEAVING:
726 case FSL_DDR_3WAY_8KB_INTERLEAVING:
727 fsl_ddr_set_intl3r(
728 pinfo->memctl_opts[first_ctrl].
729 memctl_interleaving_mode);
730 break;
731 default:
732 break;
York Sunf31cfd12012-10-08 07:44:24 +0000733 }
734
York Sunc63e1372013-06-25 11:37:48 -0700735 /*
736 * Program configuration registers.
737 * JEDEC specs requires clocks to be stable before deasserting reset
738 * for RDIMMs. Clocks start after chip select is enabled and clock
739 * control register is set. During step 1, all controllers have their
740 * registers set but not enabled. Step 2 proceeds after deasserting
741 * reset through board FPGA or GPIO.
742 * For non-registered DIMMs, initialization can go through but it is
743 * also OK to follow the same flow.
744 */
York Sun1d71efb2014-08-01 15:51:00 -0700745 if (pinfo->board_need_mem_reset)
746 deassert_reset = pinfo->board_need_mem_reset();
747 for (i = first_ctrl; i <= last_ctrl; i++) {
748 if (pinfo->common_timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700749 deassert_reset = 1;
750 }
York Sun1d71efb2014-08-01 15:51:00 -0700751 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500752 debug("Programming controller %u\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700753 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500754 debug("No dimms present on controller %u; "
755 "skipping programming\n", i);
756 continue;
757 }
York Sunc63e1372013-06-25 11:37:48 -0700758 /*
759 * The following call with step = 1 returns before enabling
760 * the controller. It has to finish with step = 2 later.
761 */
York Sun1d71efb2014-08-01 15:51:00 -0700762 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sunc63e1372013-06-25 11:37:48 -0700763 deassert_reset ? 1 : 0);
764 }
765 if (deassert_reset) {
766 /* Use board FPGA or GPIO to deassert reset signal */
York Sun1d71efb2014-08-01 15:51:00 -0700767 if (pinfo->board_mem_de_reset) {
768 debug("Deasserting mem reset\n");
769 pinfo->board_mem_de_reset();
770 } else {
771 debug("Deasserting mem reset missing\n");
772 }
773 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sunc63e1372013-06-25 11:37:48 -0700774 /* Call with step = 2 to continue initialization */
York Sun1d71efb2014-08-01 15:51:00 -0700775 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sunc63e1372013-06-25 11:37:48 -0700776 i, 2);
777 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500778 }
779
York Sune32d59a2015-01-06 13:18:55 -0800780#ifdef CONFIG_FSL_DDR_SYNC_REFRESH
781 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
782#endif
783
York Sun9ac4ffb2013-09-30 14:20:51 -0700784#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000785 /* program LAWs */
York Sun1d71efb2014-08-01 15:51:00 -0700786 for (i = first_ctrl; i <= last_ctrl; i++) {
787 if (pinfo->memctl_opts[i].memctl_interleaving) {
788 switch (pinfo->memctl_opts[i].
789 memctl_interleaving_mode) {
York Suna4c66502012-08-17 08:22:39 +0000790 case FSL_DDR_CACHE_LINE_INTERLEAVING:
791 case FSL_DDR_PAGE_INTERLEAVING:
792 case FSL_DDR_BANK_INTERLEAVING:
793 case FSL_DDR_SUPERBANK_INTERLEAVING:
York Sun1d71efb2014-08-01 15:51:00 -0700794 if (i % 2)
795 break;
York Suna4c66502012-08-17 08:22:39 +0000796 if (i == 0) {
797 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun1d71efb2014-08-01 15:51:00 -0700798 fsl_ddr_set_lawbar(
799 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000800 law_memctl, i);
801 }
York Sun51370d52016-12-28 08:43:45 -0800802#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun1d71efb2014-08-01 15:51:00 -0700803 else if (i == 2) {
804 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
805 fsl_ddr_set_lawbar(
806 &pinfo->common_timing_params[i],
807 law_memctl, i);
808 }
809#endif
York Suna4c66502012-08-17 08:22:39 +0000810 break;
811 case FSL_DDR_3WAY_1KB_INTERLEAVING:
812 case FSL_DDR_3WAY_4KB_INTERLEAVING:
813 case FSL_DDR_3WAY_8KB_INTERLEAVING:
814 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
815 if (i == 0) {
York Sun1d71efb2014-08-01 15:51:00 -0700816 fsl_ddr_set_lawbar(
817 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000818 law_memctl, i);
819 }
820 break;
821 case FSL_DDR_4WAY_1KB_INTERLEAVING:
822 case FSL_DDR_4WAY_4KB_INTERLEAVING:
823 case FSL_DDR_4WAY_8KB_INTERLEAVING:
824 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
825 if (i == 0)
York Sun1d71efb2014-08-01 15:51:00 -0700826 fsl_ddr_set_lawbar(
827 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000828 law_memctl, i);
829 /* place holder for future 4-way interleaving */
830 break;
831 default:
832 break;
833 }
834 } else {
835 switch (i) {
836 case 0:
837 law_memctl = LAW_TRGT_IF_DDR_1;
838 break;
839 case 1:
840 law_memctl = LAW_TRGT_IF_DDR_2;
841 break;
842 case 2:
843 law_memctl = LAW_TRGT_IF_DDR_3;
844 break;
845 case 3:
846 law_memctl = LAW_TRGT_IF_DDR_4;
847 break;
848 default:
849 break;
850 }
York Sun1d71efb2014-08-01 15:51:00 -0700851 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
852 law_memctl, i);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500853 }
854 }
York Sun9ac4ffb2013-09-30 14:20:51 -0700855#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500856
York Suna4c66502012-08-17 08:22:39 +0000857 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Galae7563af2009-06-11 23:42:35 -0500858
859#if !defined(CONFIG_PHYS_64BIT)
Pali Rohár27219972022-09-09 17:32:44 +0200860 /*
861 * Show warning about big DDR moodules. But avoid warning for 4 GB DDR
862 * modules when U-Boot supports RAM of maximal size 4 GB - 1 byte.
863 */
864 if ((first_ctrl == 0) && (total_memory - 1 > (phys_size_t)~0ULL)) {
Shruti Kanetkar2f848f92013-08-15 11:25:37 -0500865 puts("Detected ");
866 print_size(total_memory, " of memory\n");
Pali Rohárcf4eba42022-09-09 17:32:42 +0200867#ifndef CONFIG_SPL_BUILD
868 puts(" "); /* re-align to match init_dram print */
869#endif
870 puts("This U-Boot only supports <= ");
Pali Rohár27219972022-09-09 17:32:44 +0200871 print_size((unsigned long long)((phys_size_t)~0ULL)+1, " of DDR\n");
Pali Rohárcf4eba42022-09-09 17:32:42 +0200872#ifndef CONFIG_SPL_BUILD
873 puts(" "); /* re-align to match init_dram print */
874#endif
875 puts("You could rebuild it with CONFIG_PHYS_64BIT\n");
876#ifndef CONFIG_SPL_BUILD
877 puts(" "); /* re-align to match init_dram print */
878#endif
Kumar Galae7563af2009-06-11 23:42:35 -0500879 }
880#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500881
Pali Rohár27219972022-09-09 17:32:44 +0200882 /* Ensure that total_memory does not overflow on return */
883 if (total_memory > (phys_size_t)~0ULL)
884 total_memory = (phys_size_t)~0ULL;
885
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500886 return total_memory;
887}
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500888
889/*
York Sun1d71efb2014-08-01 15:51:00 -0700890 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassf1683aa2017-04-06 12:47:05 -0600891 * called by dram_init() in the board file.
York Sun1d71efb2014-08-01 15:51:00 -0700892 *
893 * It returns amount of memory configured in bytes.
894 */
895phys_size_t fsl_ddr_sdram(void)
896{
897 fsl_ddr_info_t info;
898
899 /* Reset info structure. */
900 memset(&info, 0, sizeof(fsl_ddr_info_t));
Tom Rini6cc04542022-10-28 20:27:13 -0400901 info.mem_base = CFG_SYS_FSL_DDR_SDRAM_BASE_PHY;
York Sun1d71efb2014-08-01 15:51:00 -0700902 info.first_ctrl = 0;
903 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
904 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
905 info.board_need_mem_reset = board_need_mem_reset;
906 info.board_mem_reset = board_assert_mem_reset;
907 info.board_mem_de_reset = board_deassert_mem_reset;
York Sun61bd2f72015-11-04 09:53:10 -0800908 remove_unused_controllers(&info);
York Sun1d71efb2014-08-01 15:51:00 -0700909
910 return __fsl_ddr_sdram(&info);
911}
912
913#ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
914phys_size_t fsl_other_ddr_sdram(unsigned long long base,
915 unsigned int first_ctrl,
916 unsigned int num_ctrls,
917 unsigned int dimm_slots_per_ctrl,
918 int (*board_need_reset)(void),
919 void (*board_reset)(void),
920 void (*board_de_reset)(void))
921{
922 fsl_ddr_info_t info;
923
924 /* Reset info structure. */
925 memset(&info, 0, sizeof(fsl_ddr_info_t));
926 info.mem_base = base;
927 info.first_ctrl = first_ctrl;
928 info.num_ctrls = num_ctrls;
929 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
930 info.board_need_mem_reset = board_need_reset;
931 info.board_mem_reset = board_reset;
932 info.board_mem_de_reset = board_de_reset;
933
934 return __fsl_ddr_sdram(&info);
935}
936#endif
937
938/*
939 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
940 * size of the total memory without setting ddr control registers.
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500941 */
942phys_size_t
943fsl_ddr_sdram_size(void)
944{
945 fsl_ddr_info_t info;
946 unsigned long long total_memory = 0;
947
948 memset(&info, 0 , sizeof(fsl_ddr_info_t));
Tom Rini6cc04542022-10-28 20:27:13 -0400949 info.mem_base = CFG_SYS_FSL_DDR_SDRAM_BASE_PHY;
York Sun1d71efb2014-08-01 15:51:00 -0700950 info.first_ctrl = 0;
951 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
952 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
953 info.board_need_mem_reset = NULL;
Ed Swarthout81dfdee2016-01-14 12:28:04 -0600954 remove_unused_controllers(&info);
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500955
956 /* Compute it once normally. */
957 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
958
Pali Rohár922460b2022-09-09 17:32:43 +0200959 /* Ensure that total_memory does not overflow on return */
960 if (total_memory > (phys_size_t)~0ULL)
961 total_memory = (phys_size_t)~0ULL;
962
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500963 return total_memory;
964}