blob: f389e5ef952417e55011e0edcffbd0f1f5d8265d [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>
Heiko Schocher09aa70f2019-08-26 17:28:34 +020014#include <dm.h>
Kumar Galac39f44d2011-01-31 22:18:47 -060015#include <i2c.h>
York Sun5614e712013-09-30 09:22:09 -070016#include <fsl_ddr_sdram.h>
York Sun5614e712013-09-30 09:22:09 -070017#include <fsl_ddr.h>
Simon Glass691d7192020-05-10 11:40:02 -060018#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <log.h>
Simon Glasscd93d622020-05-10 11:40:13 -060020#include <asm/bitops.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050021
York Sun6b9e3092014-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 Six133ec602019-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 Sun6b9e3092014-02-10 13:59:43 -080034#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
35#endif
Mario Six133ec602019-01-21 09:18:16 +010036#endif
York Sun6b9e3092014-02-10 13:59:43 -080037
York Sun9ac4ffb2013-09-30 14:20:51 -070038#ifdef CONFIG_PPC
39#include <asm/fsl_law.h>
40
York Suna4c66502012-08-17 08:22:39 +000041void fsl_ddr_set_lawbar(
Kumar Gala58e5e9a2008-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 Sun9ac4ffb2013-09-30 14:20:51 -070045#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -050046
York Sun9ac4ffb2013-09-30 14:20:51 -070047void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac39f44d2011-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 Sun51370d52016-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 Galac39f44d2011-01-31 22:18:47 -060053 [0][0] = SPD_EEPROM_ADDRESS,
54};
York Sun51370d52016-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] = {
York Sun639f3302011-08-26 11:32:41 -070057 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
58 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
59};
York Sun51370d52016-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] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060062 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
63 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
64};
York Sun51370d52016-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 Galac39f44d2011-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 Sun51370d52016-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 Suna4c66502012-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 Sun51370d52016-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 Suna4c66502012-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 Galac39f44d2011-01-31 22:18:47 -060088#endif
89
Igor Opaniuk2147a162021-02-09 13:52:45 +020090#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-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 Sun34e026f2014-03-27 17:54:47 -0700102#define SPD_SPA0_ADDRESS 0x36
103#define SPD_SPA1_ADDRESS 0x37
104
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200105static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
106 int alen, uint8_t *buf, int len)
107{
108 int ret;
109
Igor Opaniuk2147a162021-02-09 13:52:45 +0200110#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-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
119#ifdef CONFIG_SYS_FSL_DDR4
120static int ddr_i2c_dummy_write(unsigned int chip_addr)
121{
122 uint8_t buf = 0;
123
Igor Opaniuk2147a162021-02-09 13:52:45 +0200124#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-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);
139#endif
140
141 return 0;
142}
143#endif
144
Kumar Galac39f44d2011-01-31 22:18:47 -0600145static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
146{
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200147 int ret;
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200148 DEV_TYPE *dev;
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200149
Igor Opaniuk2147a162021-02-09 13:52:45 +0200150#if CONFIG_IS_ENABLED(DM_I2C)
Heiko Schocher09aa70f2019-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
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200164 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800165#endif
166
York Sun34e026f2014-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 Schocher09aa70f2019-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 Sun34e026f2014-03-27 17:54:47 -0700176 if (!ret) {
Heiko Schocher09aa70f2019-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 Sun34e026f2014-03-27 17:54:47 -0700182 }
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800183
184#else
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200185 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
186 sizeof(generic_spd_eeprom_t));
York Sun34e026f2014-03-27 17:54:47 -0700187#endif
Kumar Galac39f44d2011-01-31 22:18:47 -0600188
189 if (ret) {
York Sun82968a72012-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 Galac39f44d2011-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 Sunb92557c2015-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 Galac39f44d2011-01-31 22:18:47 -0600217void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun1d71efb2014-08-01 15:51:00 -0700218 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600219{
220 unsigned int i;
221 unsigned int i2c_address = 0;
222
York Sun51370d52016-12-28 08:43:45 -0800223 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600224 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
225 return;
226 }
227
York Sun1d71efb2014-08-01 15:51:00 -0700228 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600229 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb92557c2015-05-28 14:54:08 +0530230 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac39f44d2011-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 Sun1d71efb2014-08-01 15:51:00 -0700236 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600237{
238}
239#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala58e5e9a2008-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 Denkf12e4542008-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 Gala58e5e9a2008-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 Denkf12e4542008-09-13 02:23:05 +0200266 * | per memory controller supported
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500267 * --------------------------------|-----------------------------------------
Wolfgang Denkf12e4542008-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 Gala58e5e9a2008-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 Denkf12e4542008-09-13 02:23:05 +0200280 * Name of field | documentation name | this code
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500281 * -----------------------------|-----------------------|------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200282 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
283 * | interleaving
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500284 */
285
Kumar Gala58e5e9a2008-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 Singh71b255b2021-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 Sun349689b2014-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 Gala58e5e9a2008-08-26 15:01:29 -0500313 return step_string_tbl[s];
314}
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500315
York Sunef002272013-03-25 07:39:35 +0000316static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Suna4c66502012-08-17 08:22:39 +0000317 unsigned int dbw_cap_adj[])
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500318{
York Sun1d71efb2014-08-01 15:51:00 -0700319 unsigned int i, j;
York Suna4c66502012-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 Sun1d71efb2014-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 Gala58e5e9a2008-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 Sun1d71efb2014-08-01 15:51:00 -0700331 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-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 Sun51d498f2011-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 Gala58e5e9a2008-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 Suna4c66502012-08-17 08:22:39 +0000382 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500383 }
384
York Sun1d71efb2014-08-01 15:51:00 -0700385 current_mem_base = pinfo->mem_base;
York Suna4c66502012-08-17 08:22:39 +0000386 total_mem = 0;
York Sun1d71efb2014-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 Suna4c66502012-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 Sun1d71efb2014-08-01 15:51:00 -0700406 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000407 if (pinfo->memctl_opts[i].memctl_interleaving) {
408 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sun6b1e1252014-02-10 13:59:44 -0800409 case FSL_DDR_256B_INTERLEAVING:
York Suna4c66502012-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 Gala58e5e9a2008-08-26 15:01:29 -0500455 }
456 }
457 } else {
458 /*
459 * Simple linear assignment if memory
460 * controllers are not interleaved.
461 */
York Sun1d71efb2014-08-01 15:51:00 -0700462 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000463 total_ctlr_mem = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500464 pinfo->common_timing_params[i].base_address =
York Suna4c66502012-08-17 08:22:39 +0000465 current_mem_base;
Kumar Gala58e5e9a2008-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 Suna4c66502012-08-17 08:22:39 +0000469 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500470 pinfo->dimm_params[i][j].base_address =
York Suna4c66502012-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 Gala58e5e9a2008-08-26 15:01:29 -0500475 }
York Suna4c66502012-08-17 08:22:39 +0000476 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500477 pinfo->common_timing_params[i].total_mem =
York Suna4c66502012-08-17 08:22:39 +0000478 total_ctlr_mem;
479 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500480 }
481 }
York Suna4c66502012-08-17 08:22:39 +0000482 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500483
York Suna4c66502012-08-17 08:22:39 +0000484 return total_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500485}
486
York Sunef002272013-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 Galae7563af2009-06-11 23:42:35 -0500492unsigned long long
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500493fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
494 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500495{
496 unsigned int i, j;
Kumar Galae7563af2009-06-11 23:42:35 -0500497 unsigned long long total_mem = 0;
York Sun1d71efb2014-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 Gala58e5e9a2008-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 Sun1d71efb2014-08-01 15:51:00 -0700507 if (pinfo->board_need_mem_reset)
508 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500509
510 /* data bus width capacity adjust shift amount */
York Sun51370d52016-12-28 08:43:45 -0800511 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500512
York Sun1d71efb2014-08-01 15:51:00 -0700513 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500514 dbw_capacity_adjust[i] = 0;
Kumar Gala58e5e9a2008-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 Sun1b3e3c42011-06-07 09:42:16 +0800521#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500522 /* STEP 1: Gather all DIMM SPD data */
York Sun1d71efb2014-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 Gala58e5e9a2008-08-26 15:01:29 -0500526 }
527
528 case STEP_COMPUTE_DIMM_PARMS:
529 /* STEP 2: Compute DIMM parameters from SPD data */
530
York Sun1d71efb2014-08-01 15:51:00 -0700531 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500532 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500533 generic_spd_eeprom_t *spd =
534 &(pinfo->spd_installed_dimms[i][j]);
535 dimm_params_t *pdimm =
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200536 &(pinfo->dimm_params[i][j]);
York Sun03e664d2015-01-06 13:18:50 -0800537 retval = compute_dimm_parameters(
538 i, spd, pdimm, j);
York Sunf2d264b2011-06-07 09:42:17 +0800539#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun66869f92015-03-19 09:30:26 -0700540 if (!j && retval) {
York Suna4c66502012-08-17 08:22:39 +0000541 printf("SPD error on controller %d! "
542 "Trying fallback to raw timing "
543 "calculation\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700544 retval = fsl_ddr_get_dimm_params(pdimm,
545 i, j);
York Sunf2d264b2011-06-07 09:42:17 +0800546 }
547#else
Kumar Gala58e5e9a2008-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 Sunf2d264b2011-06-07 09:42:17 +0800554#endif
Kumar Gala58e5e9a2008-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 Sun1d71efb2014-08-01 15:51:00 -0700559 } else {
560 goodspd = true;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500561 }
562 }
563 }
York Sun1d71efb2014-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 Gala58e5e9a2008-08-26 15:01:29 -0500573
York Sun1d71efb2014-08-01 15:51:00 -0700574 return 0;
575 }
Shaohui Xie98de3692012-06-28 23:36:38 +0000576#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sun1b3e3c42011-06-07 09:42:16 +0800577 case STEP_COMPUTE_DIMM_PARMS:
York Sun1d71efb2014-08-01 15:51:00 -0700578 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun1b3e3c42011-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 Gala58e5e9a2008-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 Sun1d71efb2014-08-01 15:51:00 -0700592 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500593 debug("Computing lowest common DIMM"
594 " parameters for memctl=%u\n", i);
York Sun03e664d2015-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 Gala58e5e9a2008-08-26 15:01:29 -0500600 }
601
602 case STEP_GATHER_OPTS:
603 /* STEP 4: Gather configuration requirements from user */
York Sun1d71efb2014-08-01 15:51:00 -0700604 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-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 Sun56848422015-07-23 14:04:48 -0700614 &timing_params[i],
Haiying Wangdfb49102008-10-03 12:36:55 -0400615 &pinfo->memctl_opts[i],
616 pinfo->dimm_params[i], i);
York Sunc63e1372013-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 Jain0dd38a32013-09-25 10:41:19 +0530623 if (timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700624 assert_reset = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500625 }
York Sun1d71efb2014-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 }
York Sunc63e1372013-06-25 11:37:48 -0700633 }
634
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500635 case STEP_ASSIGN_ADDRESSES:
636 /* STEP 5: Assign addresses to chip selects */
York Suna4c66502012-08-17 08:22:39 +0000637 check_interleaving_options(pinfo);
638 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sun349689b2014-04-01 14:20:49 -0700639 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500640
641 case STEP_COMPUTE_REGS:
642 /* STEP 6: compute controller register values */
York Suna4c66502012-08-17 08:22:39 +0000643 debug("FSL Memory ctrl register computation\n");
York Sun1d71efb2014-08-01 15:51:00 -0700644 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-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 Sun03e664d2015-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 Gala58e5e9a2008-08-26 15:01:29 -0500658 }
659
660 default:
661 break;
662 }
663
York Suna4c66502012-08-17 08:22:39 +0000664 {
Kumar Gala58e5e9a2008-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 Sun1d71efb2014-08-01 15:51:00 -0700673 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-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 Sund8556db2013-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 Gala58e5e9a2008-08-26 15:01:29 -0500685 if (end > max_end) {
686 max_end = end;
687 }
688 }
689 }
690 }
691
York Sun00ec3fd2013-10-28 16:36:02 -0700692 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun1d71efb2014-08-01 15:51:00 -0700693 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500694 }
695
696 return total_mem;
697}
698
York Sun1d71efb2014-08-01 15:51:00 -0700699phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500700{
York Sun1d71efb2014-08-01 15:51:00 -0700701 unsigned int i, first_ctrl, last_ctrl;
York Sun9ac4ffb2013-09-30 14:20:51 -0700702#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000703 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun9ac4ffb2013-09-30 14:20:51 -0700704#endif
Kumar Galae7563af2009-06-11 23:42:35 -0500705 unsigned long long total_memory;
York Sun1d71efb2014-08-01 15:51:00 -0700706 int deassert_reset = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500707
York Sun1d71efb2014-08-01 15:51:00 -0700708 first_ctrl = pinfo->first_ctrl;
709 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500710
711 /* Compute it once normally. */
York Sun6f5e1dc2011-09-16 13:21:35 -0700712#ifdef CONFIG_FSL_DDR_INTERACTIVE
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200713 if (tstc() && (getchar() == 'd')) { /* we got a key press of 'd' */
York Sun1d71efb2014-08-01 15:51:00 -0700714 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yange8ba6c52013-01-07 14:01:03 +0000715 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun1d71efb2014-08-01 15:51:00 -0700716 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sune750cfa2013-01-04 08:13:59 +0000717 } else
York Sun6f5e1dc2011-09-16 13:21:35 -0700718#endif
York Sun1d71efb2014-08-01 15:51:00 -0700719 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500720
York Sunf31cfd12012-10-08 07:44:24 +0000721 /* setup 3-way interleaving before enabling DDRC */
York Sun1d71efb2014-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 Sunf31cfd12012-10-08 07:44:24 +0000732 }
733
York Sunc63e1372013-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 Sun1d71efb2014-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 Sunc63e1372013-06-25 11:37:48 -0700748 deassert_reset = 1;
749 }
York Sun1d71efb2014-08-01 15:51:00 -0700750 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500751 debug("Programming controller %u\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700752 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500753 debug("No dimms present on controller %u; "
754 "skipping programming\n", i);
755 continue;
756 }
York Sunc63e1372013-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 Sun1d71efb2014-08-01 15:51:00 -0700761 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sunc63e1372013-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 Sun1d71efb2014-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 Sunc63e1372013-06-25 11:37:48 -0700773 /* Call with step = 2 to continue initialization */
York Sun1d71efb2014-08-01 15:51:00 -0700774 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sunc63e1372013-06-25 11:37:48 -0700775 i, 2);
776 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500777 }
778
York Sune32d59a2015-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 Sun9ac4ffb2013-09-30 14:20:51 -0700783#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000784 /* program LAWs */
York Sun1d71efb2014-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 Suna4c66502012-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 Sun1d71efb2014-08-01 15:51:00 -0700793 if (i % 2)
794 break;
York Suna4c66502012-08-17 08:22:39 +0000795 if (i == 0) {
796 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun1d71efb2014-08-01 15:51:00 -0700797 fsl_ddr_set_lawbar(
798 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000799 law_memctl, i);
800 }
York Sun51370d52016-12-28 08:43:45 -0800801#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun1d71efb2014-08-01 15:51:00 -0700802 else if (i == 2) {
803 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
804 fsl_ddr_set_lawbar(
805 &pinfo->common_timing_params[i],
806 law_memctl, i);
807 }
808#endif
York Suna4c66502012-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 Sun1d71efb2014-08-01 15:51:00 -0700815 fsl_ddr_set_lawbar(
816 &pinfo->common_timing_params[i],
York Suna4c66502012-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 Sun1d71efb2014-08-01 15:51:00 -0700825 fsl_ddr_set_lawbar(
826 &pinfo->common_timing_params[i],
York Suna4c66502012-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 Sun1d71efb2014-08-01 15:51:00 -0700850 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
851 law_memctl, i);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500852 }
853 }
York Sun9ac4ffb2013-09-30 14:20:51 -0700854#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500855
York Suna4c66502012-08-17 08:22:39 +0000856 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Galae7563af2009-06-11 23:42:35 -0500857
858#if !defined(CONFIG_PHYS_64BIT)
859 /* Check for 4G or more. Bad. */
York Sun1d71efb2014-08-01 15:51:00 -0700860 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
Shruti Kanetkar2f848f92013-08-15 11:25:37 -0500861 puts("Detected ");
862 print_size(total_memory, " of memory\n");
Becky Bruce7ea38712010-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 Glassf1683aa2017-04-06 12:47:05 -0600865 printf(" "); /* re-align to match init_dram print */
Kumar Galae7563af2009-06-11 23:42:35 -0500866 total_memory = CONFIG_MAX_MEM_MAPPED;
867 }
868#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500869
870 return total_memory;
871}
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500872
873/*
York Sun1d71efb2014-08-01 15:51:00 -0700874 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassf1683aa2017-04-06 12:47:05 -0600875 * called by dram_init() in the board file.
York Sun1d71efb2014-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 Sun61bd2f72015-11-04 09:53:10 -0800892 remove_unused_controllers(&info);
York Sun1d71efb2014-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 Wangfc0c2b62010-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 Sun1d71efb2014-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 Swarthout81dfdee2016-01-14 12:28:04 -0600938 remove_unused_controllers(&info);
Haiying Wangfc0c2b62010-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}