blob: 0aa6aedae7e16c6bf145c12bcc55b4defd77a5eb [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.
Kumar Gala58e5e9a2008-08-26 15:01:29 -05004 */
5
6/*
7 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
8 * Based on code from spd_sdram.c
9 * Author: James Yang [at freescale.com]
10 */
11
12#include <common.h>
Heiko Schocher09aa70f2019-08-26 17:28:34 +020013#include <dm.h>
Kumar Galac39f44d2011-01-31 22:18:47 -060014#include <i2c.h>
York Sun5614e712013-09-30 09:22:09 -070015#include <fsl_ddr_sdram.h>
York Sun5614e712013-09-30 09:22:09 -070016#include <fsl_ddr.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050017
York Sun6b9e3092014-02-10 13:59:43 -080018/*
19 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
20 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
21 * all Power SoCs. But it could be different for ARM SoCs. For example,
22 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
23 * 0x00_8000_0000 ~ 0x00_ffff_ffff
24 * 0x80_8000_0000 ~ 0xff_ffff_ffff
25 */
26#ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
Mario Six133ec602019-01-21 09:18:16 +010027#ifdef CONFIG_MPC83xx
28#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
29#else
York Sun6b9e3092014-02-10 13:59:43 -080030#define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
31#endif
Mario Six133ec602019-01-21 09:18:16 +010032#endif
York Sun6b9e3092014-02-10 13:59:43 -080033
York Sun9ac4ffb2013-09-30 14:20:51 -070034#ifdef CONFIG_PPC
35#include <asm/fsl_law.h>
36
York Suna4c66502012-08-17 08:22:39 +000037void fsl_ddr_set_lawbar(
Kumar Gala58e5e9a2008-08-26 15:01:29 -050038 const common_timing_params_t *memctl_common_params,
39 unsigned int memctl_interleaved,
40 unsigned int ctrl_num);
York Sun9ac4ffb2013-09-30 14:20:51 -070041#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -050042
York Sun9ac4ffb2013-09-30 14:20:51 -070043void fsl_ddr_set_intl3r(const unsigned int granule_size);
Kumar Galac39f44d2011-01-31 22:18:47 -060044#if defined(SPD_EEPROM_ADDRESS) || \
45 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
46 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
York Sun51370d52016-12-28 08:43:45 -080047#if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
48u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060049 [0][0] = SPD_EEPROM_ADDRESS,
50};
York Sun51370d52016-12-28 08:43:45 -080051#elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
52u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Sun639f3302011-08-26 11:32:41 -070053 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
54 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
55};
York Sun51370d52016-12-28 08:43:45 -080056#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
57u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
Kumar Galac39f44d2011-01-31 22:18:47 -060058 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
59 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
60};
York Sun51370d52016-12-28 08:43:45 -080061#elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
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 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
65 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
66 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
67};
York Sun51370d52016-12-28 08:43:45 -080068#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
69u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Suna4c66502012-08-17 08:22:39 +000070 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
71 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
72 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
73};
York Sun51370d52016-12-28 08:43:45 -080074#elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
75u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
York Suna4c66502012-08-17 08:22:39 +000076 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
77 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
78 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
79 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
80 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
81 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
82};
83
Kumar Galac39f44d2011-01-31 22:18:47 -060084#endif
85
Heiko Schocher09aa70f2019-08-26 17:28:34 +020086#if defined(CONFIG_DM_I2C)
87#define DEV_TYPE struct udevice
88#else
89/* Local udevice */
90struct ludevice {
91 u8 chip;
92};
93
94#define DEV_TYPE struct ludevice
95
96#endif
97
York Sun34e026f2014-03-27 17:54:47 -070098#define SPD_SPA0_ADDRESS 0x36
99#define SPD_SPA1_ADDRESS 0x37
100
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200101static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
102 int alen, uint8_t *buf, int len)
103{
104 int ret;
105
106#ifdef CONFIG_DM_I2C
107 ret = dm_i2c_read(dev, 0, buf, len);
108#else
109 ret = i2c_read(dev->chip, addr, alen, buf, len);
110#endif
111
112 return ret;
113}
114
115#ifdef CONFIG_SYS_FSL_DDR4
116static int ddr_i2c_dummy_write(unsigned int chip_addr)
117{
118 uint8_t buf = 0;
119
120#ifdef CONFIG_DM_I2C
121 struct udevice *dev;
122 int ret;
123
124 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
125 1, &dev);
126 if (ret) {
127 printf("%s: Cannot find udev for a bus %d\n", __func__,
128 CONFIG_SYS_SPD_BUS_NUM);
129 return ret;
130 }
131
132 return dm_i2c_write(dev, 0, &buf, 1);
133#else
134 return i2c_write(chip_addr, 0, 1, &buf, 1);
135#endif
136
137 return 0;
138}
139#endif
140
Kumar Galac39f44d2011-01-31 22:18:47 -0600141static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
142{
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200143 int ret;
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200144 DEV_TYPE *dev;
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200145
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200146#if defined(CONFIG_DM_I2C)
147 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
148 1, &dev);
149 if (ret) {
150 printf("%s: Cannot find udev for a bus %d\n", __func__,
151 CONFIG_SYS_SPD_BUS_NUM);
152 return;
153 }
154#else /* Non DM I2C support - will be removed */
155 struct ludevice ldev = {
156 .chip = i2c_address,
157 };
158 dev = &ldev;
159
Valentin Longchamp0778bbe2013-10-18 11:47:19 +0200160 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800161#endif
162
York Sun34e026f2014-03-27 17:54:47 -0700163#ifdef CONFIG_SYS_FSL_DDR4
164 /*
165 * DDR4 SPD has 384 to 512 bytes
166 * To access the lower 256 bytes, we need to set EE page address to 0
167 * To access the upper 256 bytes, we need to set EE page address to 1
168 * See Jedec standar No. 21-C for detail
169 */
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200170 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
171 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
York Sun34e026f2014-03-27 17:54:47 -0700172 if (!ret) {
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200173 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
174 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
175 min(256,
176 (int)sizeof(generic_spd_eeprom_t)
177 - 256));
York Sun34e026f2014-03-27 17:54:47 -0700178 }
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800179
180#else
Heiko Schocher09aa70f2019-08-26 17:28:34 +0200181 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
182 sizeof(generic_spd_eeprom_t));
York Sun34e026f2014-03-27 17:54:47 -0700183#endif
Kumar Galac39f44d2011-01-31 22:18:47 -0600184
185 if (ret) {
York Sun82968a72012-10-08 07:44:28 +0000186 if (i2c_address ==
187#ifdef SPD_EEPROM_ADDRESS
188 SPD_EEPROM_ADDRESS
189#elif defined(SPD_EEPROM_ADDRESS1)
190 SPD_EEPROM_ADDRESS1
191#endif
192 ) {
193 printf("DDR: failed to read SPD from address %u\n",
194 i2c_address);
195 } else {
196 debug("DDR: failed to read SPD from address %u\n",
197 i2c_address);
198 }
Kumar Galac39f44d2011-01-31 22:18:47 -0600199 memset(spd, 0, sizeof(generic_spd_eeprom_t));
200 }
201}
202
203__attribute__((weak, alias("__get_spd")))
204void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
205
York Sunb92557c2015-05-28 14:54:08 +0530206/* This function allows boards to update SPD address */
207__weak void update_spd_address(unsigned int ctrl_num,
208 unsigned int slot,
209 unsigned int *addr)
210{
211}
212
Kumar Galac39f44d2011-01-31 22:18:47 -0600213void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun1d71efb2014-08-01 15:51:00 -0700214 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600215{
216 unsigned int i;
217 unsigned int i2c_address = 0;
218
York Sun51370d52016-12-28 08:43:45 -0800219 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600220 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
221 return;
222 }
223
York Sun1d71efb2014-08-01 15:51:00 -0700224 for (i = 0; i < dimm_slots_per_ctrl; i++) {
Kumar Galac39f44d2011-01-31 22:18:47 -0600225 i2c_address = spd_i2c_addr[ctrl_num][i];
York Sunb92557c2015-05-28 14:54:08 +0530226 update_spd_address(ctrl_num, i, &i2c_address);
Kumar Galac39f44d2011-01-31 22:18:47 -0600227 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
228 }
229}
230#else
231void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
York Sun1d71efb2014-08-01 15:51:00 -0700232 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
Kumar Galac39f44d2011-01-31 22:18:47 -0600233{
234}
235#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500236
237/*
238 * ASSUMPTIONS:
239 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
240 * - Same memory data bus width on all controllers
241 *
242 * NOTES:
243 *
244 * The memory controller and associated documentation use confusing
245 * terminology when referring to the orgranization of DRAM.
246 *
247 * Here is a terminology translation table:
248 *
249 * memory controller/documention |industry |this code |signals
250 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200251 * physical bank/bank |rank |rank |chip select (CS)
252 * logical bank/sub-bank |bank |bank |bank address (BA)
253 * page/row |row |page |row address
254 * ??? |column |column |column address
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500255 *
256 * The naming confusion is further exacerbated by the descriptions of the
257 * memory controller interleaving feature, where accesses are interleaved
258 * _BETWEEN_ two seperate memory controllers. This is configured only in
259 * CS0_CONFIG[INTLV_CTL] of each memory controller.
260 *
261 * memory controller documentation | number of chip selects
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200262 * | per memory controller supported
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500263 * --------------------------------|-----------------------------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200264 * cache line interleaving | 1 (CS0 only)
265 * page interleaving | 1 (CS0 only)
266 * bank interleaving | 1 (CS0 only)
267 * superbank interleraving | depends on bank (chip select)
268 * | interleraving [rank interleaving]
269 * | mode used on every memory controller
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500270 *
271 * Even further confusing is the existence of the interleaving feature
272 * _WITHIN_ each memory controller. The feature is referred to in
273 * documentation as chip select interleaving or bank interleaving,
274 * although it is configured in the DDR_SDRAM_CFG field.
275 *
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200276 * Name of field | documentation name | this code
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500277 * -----------------------------|-----------------------|------------------
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200278 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
279 * | interleaving
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500280 */
281
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500282const char *step_string_tbl[] = {
283 "STEP_GET_SPD",
284 "STEP_COMPUTE_DIMM_PARMS",
285 "STEP_COMPUTE_COMMON_PARMS",
286 "STEP_GATHER_OPTS",
287 "STEP_ASSIGN_ADDRESSES",
288 "STEP_COMPUTE_REGS",
289 "STEP_PROGRAM_REGS",
290 "STEP_ALL"
291};
292
293const char * step_to_string(unsigned int step) {
294
295 unsigned int s = __ilog2(step);
296
297 if ((1 << s) != step)
298 return step_string_tbl[7];
299
York Sun349689b2014-04-01 14:20:49 -0700300 if (s >= ARRAY_SIZE(step_string_tbl)) {
301 printf("Error for the step in %s\n", __func__);
302 s = 0;
303 }
304
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500305 return step_string_tbl[s];
306}
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500307
York Sunef002272013-03-25 07:39:35 +0000308static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
York Suna4c66502012-08-17 08:22:39 +0000309 unsigned int dbw_cap_adj[])
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500310{
York Sun1d71efb2014-08-01 15:51:00 -0700311 unsigned int i, j;
York Suna4c66502012-08-17 08:22:39 +0000312 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
313 unsigned long long rank_density, ctlr_density = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700314 unsigned int first_ctrl = pinfo->first_ctrl;
315 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500316
317 /*
318 * If a reduced data width is requested, but the SPD
319 * specifies a physically wider device, adjust the
320 * computed dimm capacities accordingly before
321 * assigning addresses.
322 */
York Sun1d71efb2014-08-01 15:51:00 -0700323 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500324 unsigned int found = 0;
325
326 switch (pinfo->memctl_opts[i].data_bus_width) {
327 case 2:
328 /* 16-bit */
York Sun51d498f2011-05-27 07:25:51 +0800329 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
330 unsigned int dw;
331 if (!pinfo->dimm_params[i][j].n_ranks)
332 continue;
333 dw = pinfo->dimm_params[i][j].primary_sdram_width;
334 if ((dw == 72 || dw == 64)) {
335 dbw_cap_adj[i] = 2;
336 break;
337 } else if ((dw == 40 || dw == 32)) {
338 dbw_cap_adj[i] = 1;
339 break;
340 }
341 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500342 break;
343
344 case 1:
345 /* 32-bit */
346 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
347 unsigned int dw;
348 dw = pinfo->dimm_params[i][j].data_width;
349 if (pinfo->dimm_params[i][j].n_ranks
350 && (dw == 72 || dw == 64)) {
351 /*
352 * FIXME: can't really do it
353 * like this because this just
354 * further reduces the memory
355 */
356 found = 1;
357 break;
358 }
359 }
360 if (found) {
361 dbw_cap_adj[i] = 1;
362 }
363 break;
364
365 case 0:
366 /* 64-bit */
367 break;
368
369 default:
370 printf("unexpected data bus width "
371 "specified controller %u\n", i);
372 return 1;
373 }
York Suna4c66502012-08-17 08:22:39 +0000374 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500375 }
376
York Sun1d71efb2014-08-01 15:51:00 -0700377 current_mem_base = pinfo->mem_base;
York Suna4c66502012-08-17 08:22:39 +0000378 total_mem = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700379 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
380 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
381 dbw_cap_adj[first_ctrl];
382 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
York Suna4c66502012-08-17 08:22:39 +0000383 FSL_DDR_CS0_CS1_CS2_CS3) {
384 case FSL_DDR_CS0_CS1_CS2_CS3:
385 ctlr_density = 4 * rank_density;
386 break;
387 case FSL_DDR_CS0_CS1:
388 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
389 ctlr_density = 2 * rank_density;
390 break;
391 case FSL_DDR_CS2_CS3:
392 default:
393 ctlr_density = rank_density;
394 break;
395 }
396 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
397 rank_density, ctlr_density);
York Sun1d71efb2014-08-01 15:51:00 -0700398 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000399 if (pinfo->memctl_opts[i].memctl_interleaving) {
400 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
York Sun6b1e1252014-02-10 13:59:44 -0800401 case FSL_DDR_256B_INTERLEAVING:
York Suna4c66502012-08-17 08:22:39 +0000402 case FSL_DDR_CACHE_LINE_INTERLEAVING:
403 case FSL_DDR_PAGE_INTERLEAVING:
404 case FSL_DDR_BANK_INTERLEAVING:
405 case FSL_DDR_SUPERBANK_INTERLEAVING:
406 total_ctlr_mem = 2 * ctlr_density;
407 break;
408 case FSL_DDR_3WAY_1KB_INTERLEAVING:
409 case FSL_DDR_3WAY_4KB_INTERLEAVING:
410 case FSL_DDR_3WAY_8KB_INTERLEAVING:
411 total_ctlr_mem = 3 * ctlr_density;
412 break;
413 case FSL_DDR_4WAY_1KB_INTERLEAVING:
414 case FSL_DDR_4WAY_4KB_INTERLEAVING:
415 case FSL_DDR_4WAY_8KB_INTERLEAVING:
416 total_ctlr_mem = 4 * ctlr_density;
417 break;
418 default:
419 panic("Unknown interleaving mode");
420 }
421 pinfo->common_timing_params[i].base_address =
422 current_mem_base;
423 pinfo->common_timing_params[i].total_mem =
424 total_ctlr_mem;
425 total_mem = current_mem_base + total_ctlr_mem;
426 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
427 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
428 } else {
429 /* when 3rd controller not interleaved */
430 current_mem_base = total_mem;
431 total_ctlr_mem = 0;
432 pinfo->common_timing_params[i].base_address =
433 current_mem_base;
434 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
435 unsigned long long cap =
436 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
437 pinfo->dimm_params[i][j].base_address =
438 current_mem_base;
439 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
440 current_mem_base += cap;
441 total_ctlr_mem += cap;
442 }
443 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
444 pinfo->common_timing_params[i].total_mem =
445 total_ctlr_mem;
446 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500447 }
448 }
449 } else {
450 /*
451 * Simple linear assignment if memory
452 * controllers are not interleaved.
453 */
York Sun1d71efb2014-08-01 15:51:00 -0700454 for (i = first_ctrl; i <= last_ctrl; i++) {
York Suna4c66502012-08-17 08:22:39 +0000455 total_ctlr_mem = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500456 pinfo->common_timing_params[i].base_address =
York Suna4c66502012-08-17 08:22:39 +0000457 current_mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500458 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
459 /* Compute DIMM base addresses. */
460 unsigned long long cap =
York Suna4c66502012-08-17 08:22:39 +0000461 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500462 pinfo->dimm_params[i][j].base_address =
York Suna4c66502012-08-17 08:22:39 +0000463 current_mem_base;
464 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
465 current_mem_base += cap;
466 total_ctlr_mem += cap;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500467 }
York Suna4c66502012-08-17 08:22:39 +0000468 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500469 pinfo->common_timing_params[i].total_mem =
York Suna4c66502012-08-17 08:22:39 +0000470 total_ctlr_mem;
471 total_mem += total_ctlr_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500472 }
473 }
York Suna4c66502012-08-17 08:22:39 +0000474 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500475
York Suna4c66502012-08-17 08:22:39 +0000476 return total_mem;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500477}
478
York Sunef002272013-03-25 07:39:35 +0000479/* Use weak function to allow board file to override the address assignment */
480__attribute__((weak, alias("__step_assign_addresses")))
481unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
482 unsigned int dbw_cap_adj[]);
483
Kumar Galae7563af2009-06-11 23:42:35 -0500484unsigned long long
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500485fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
486 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500487{
488 unsigned int i, j;
Kumar Galae7563af2009-06-11 23:42:35 -0500489 unsigned long long total_mem = 0;
York Sun1d71efb2014-08-01 15:51:00 -0700490 int assert_reset = 0;
491 unsigned int first_ctrl = pinfo->first_ctrl;
492 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
493 __maybe_unused int retval;
494 __maybe_unused bool goodspd = false;
495 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500496
497 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
498 common_timing_params_t *timing_params = pinfo->common_timing_params;
York Sun1d71efb2014-08-01 15:51:00 -0700499 if (pinfo->board_need_mem_reset)
500 assert_reset = pinfo->board_need_mem_reset();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500501
502 /* data bus width capacity adjust shift amount */
York Sun51370d52016-12-28 08:43:45 -0800503 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500504
York Sun1d71efb2014-08-01 15:51:00 -0700505 for (i = first_ctrl; i <= last_ctrl; i++)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500506 dbw_capacity_adjust[i] = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500507
508 debug("starting at step %u (%s)\n",
509 start_step, step_to_string(start_step));
510
511 switch (start_step) {
512 case STEP_GET_SPD:
York Sun1b3e3c42011-06-07 09:42:16 +0800513#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500514 /* STEP 1: Gather all DIMM SPD data */
York Sun1d71efb2014-08-01 15:51:00 -0700515 for (i = first_ctrl; i <= last_ctrl; i++) {
516 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
517 dimm_slots_per_ctrl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500518 }
519
520 case STEP_COMPUTE_DIMM_PARMS:
521 /* STEP 2: Compute DIMM parameters from SPD data */
522
York Sun1d71efb2014-08-01 15:51:00 -0700523 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500524 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500525 generic_spd_eeprom_t *spd =
526 &(pinfo->spd_installed_dimms[i][j]);
527 dimm_params_t *pdimm =
Wolfgang Denkf12e4542008-09-13 02:23:05 +0200528 &(pinfo->dimm_params[i][j]);
York Sun03e664d2015-01-06 13:18:50 -0800529 retval = compute_dimm_parameters(
530 i, spd, pdimm, j);
York Sunf2d264b2011-06-07 09:42:17 +0800531#ifdef CONFIG_SYS_DDR_RAW_TIMING
York Sun66869f92015-03-19 09:30:26 -0700532 if (!j && retval) {
York Suna4c66502012-08-17 08:22:39 +0000533 printf("SPD error on controller %d! "
534 "Trying fallback to raw timing "
535 "calculation\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700536 retval = fsl_ddr_get_dimm_params(pdimm,
537 i, j);
York Sunf2d264b2011-06-07 09:42:17 +0800538 }
539#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500540 if (retval == 2) {
541 printf("Error: compute_dimm_parameters"
542 " non-zero returned FATAL value "
543 "for memctl=%u dimm=%u\n", i, j);
544 return 0;
545 }
York Sunf2d264b2011-06-07 09:42:17 +0800546#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500547 if (retval) {
548 debug("Warning: compute_dimm_parameters"
549 " non-zero return value for memctl=%u "
550 "dimm=%u\n", i, j);
York Sun1d71efb2014-08-01 15:51:00 -0700551 } else {
552 goodspd = true;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500553 }
554 }
555 }
York Sun1d71efb2014-08-01 15:51:00 -0700556 if (!goodspd) {
557 /*
558 * No valid SPD found
559 * Throw an error if this is for main memory, i.e.
560 * first_ctrl == 0. Otherwise, siliently return 0
561 * as the memory size.
562 */
563 if (first_ctrl == 0)
564 printf("Error: No valid SPD detected.\n");
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500565
York Sun1d71efb2014-08-01 15:51:00 -0700566 return 0;
567 }
Shaohui Xie98de3692012-06-28 23:36:38 +0000568#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sun1b3e3c42011-06-07 09:42:16 +0800569 case STEP_COMPUTE_DIMM_PARMS:
York Sun1d71efb2014-08-01 15:51:00 -0700570 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sun1b3e3c42011-06-07 09:42:16 +0800571 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
572 dimm_params_t *pdimm =
573 &(pinfo->dimm_params[i][j]);
574 fsl_ddr_get_dimm_params(pdimm, i, j);
575 }
576 }
577 debug("Filling dimm parameters from board specific file\n");
578#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500579 case STEP_COMPUTE_COMMON_PARMS:
580 /*
581 * STEP 3: Compute a common set of timing parameters
582 * suitable for all of the DIMMs on each memory controller
583 */
York Sun1d71efb2014-08-01 15:51:00 -0700584 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500585 debug("Computing lowest common DIMM"
586 " parameters for memctl=%u\n", i);
York Sun03e664d2015-01-06 13:18:50 -0800587 compute_lowest_common_dimm_parameters
588 (i,
589 pinfo->dimm_params[i],
590 &timing_params[i],
591 CONFIG_DIMM_SLOTS_PER_CTLR);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500592 }
593
594 case STEP_GATHER_OPTS:
595 /* STEP 4: Gather configuration requirements from user */
York Sun1d71efb2014-08-01 15:51:00 -0700596 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500597 debug("Reloading memory controller "
598 "configuration options for memctl=%u\n", i);
599 /*
600 * This "reloads" the memory controller options
601 * to defaults. If the user "edits" an option,
602 * next_step points to the step after this,
603 * which is currently STEP_ASSIGN_ADDRESSES.
604 */
605 populate_memctl_options(
York Sun56848422015-07-23 14:04:48 -0700606 &timing_params[i],
Haiying Wangdfb49102008-10-03 12:36:55 -0400607 &pinfo->memctl_opts[i],
608 pinfo->dimm_params[i], i);
York Sunc63e1372013-06-25 11:37:48 -0700609 /*
610 * For RDIMMs, JEDEC spec requires clocks to be stable
611 * before reset signal is deasserted. For the boards
612 * using fixed parameters, this function should be
613 * be called from board init file.
614 */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530615 if (timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700616 assert_reset = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500617 }
York Sun1d71efb2014-08-01 15:51:00 -0700618 if (assert_reset && !size_only) {
619 if (pinfo->board_mem_reset) {
620 debug("Asserting mem reset\n");
621 pinfo->board_mem_reset();
622 } else {
623 debug("Asserting mem reset missing\n");
624 }
York Sunc63e1372013-06-25 11:37:48 -0700625 }
626
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500627 case STEP_ASSIGN_ADDRESSES:
628 /* STEP 5: Assign addresses to chip selects */
York Suna4c66502012-08-17 08:22:39 +0000629 check_interleaving_options(pinfo);
630 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
York Sun349689b2014-04-01 14:20:49 -0700631 debug("Total mem %llu assigned\n", total_mem);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500632
633 case STEP_COMPUTE_REGS:
634 /* STEP 6: compute controller register values */
York Suna4c66502012-08-17 08:22:39 +0000635 debug("FSL Memory ctrl register computation\n");
York Sun1d71efb2014-08-01 15:51:00 -0700636 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500637 if (timing_params[i].ndimms_present == 0) {
638 memset(&ddr_reg[i], 0,
639 sizeof(fsl_ddr_cfg_regs_t));
640 continue;
641 }
642
York Sun03e664d2015-01-06 13:18:50 -0800643 compute_fsl_memctl_config_regs
644 (i,
645 &pinfo->memctl_opts[i],
646 &ddr_reg[i], &timing_params[i],
647 pinfo->dimm_params[i],
648 dbw_capacity_adjust[i],
649 size_only);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500650 }
651
652 default:
653 break;
654 }
655
York Suna4c66502012-08-17 08:22:39 +0000656 {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500657 /*
658 * Compute the amount of memory available just by
659 * looking for the highest valid CSn_BNDS value.
660 * This allows us to also experiment with using
661 * only CS0 when using dual-rank DIMMs.
662 */
663 unsigned int max_end = 0;
664
York Sun1d71efb2014-08-01 15:51:00 -0700665 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500666 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
667 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
668 if (reg->cs[j].config & 0x80000000) {
669 unsigned int end;
York Sund8556db2013-06-25 11:37:45 -0700670 /*
671 * 0xfffffff is a special value we put
672 * for unused bnds
673 */
674 if (reg->cs[j].bnds == 0xffffffff)
675 continue;
676 end = reg->cs[j].bnds & 0xffff;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500677 if (end > max_end) {
678 max_end = end;
679 }
680 }
681 }
682 }
683
York Sun00ec3fd2013-10-28 16:36:02 -0700684 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
York Sun1d71efb2014-08-01 15:51:00 -0700685 0xFFFFFFULL) - pinfo->mem_base;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500686 }
687
688 return total_mem;
689}
690
York Sun1d71efb2014-08-01 15:51:00 -0700691phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500692{
York Sun1d71efb2014-08-01 15:51:00 -0700693 unsigned int i, first_ctrl, last_ctrl;
York Sun9ac4ffb2013-09-30 14:20:51 -0700694#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000695 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
York Sun9ac4ffb2013-09-30 14:20:51 -0700696#endif
Kumar Galae7563af2009-06-11 23:42:35 -0500697 unsigned long long total_memory;
York Sun1d71efb2014-08-01 15:51:00 -0700698 int deassert_reset = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500699
York Sun1d71efb2014-08-01 15:51:00 -0700700 first_ctrl = pinfo->first_ctrl;
701 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500702
703 /* Compute it once normally. */
York Sun6f5e1dc2011-09-16 13:21:35 -0700704#ifdef CONFIG_FSL_DDR_INTERACTIVE
James Yange8ba6c52013-01-07 14:01:03 +0000705 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
York Sun1d71efb2014-08-01 15:51:00 -0700706 total_memory = fsl_ddr_interactive(pinfo, 0);
James Yange8ba6c52013-01-07 14:01:03 +0000707 } else if (fsl_ddr_interactive_env_var_exists()) {
York Sun1d71efb2014-08-01 15:51:00 -0700708 total_memory = fsl_ddr_interactive(pinfo, 1);
York Sune750cfa2013-01-04 08:13:59 +0000709 } else
York Sun6f5e1dc2011-09-16 13:21:35 -0700710#endif
York Sun1d71efb2014-08-01 15:51:00 -0700711 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500712
York Sunf31cfd12012-10-08 07:44:24 +0000713 /* setup 3-way interleaving before enabling DDRC */
York Sun1d71efb2014-08-01 15:51:00 -0700714 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
715 case FSL_DDR_3WAY_1KB_INTERLEAVING:
716 case FSL_DDR_3WAY_4KB_INTERLEAVING:
717 case FSL_DDR_3WAY_8KB_INTERLEAVING:
718 fsl_ddr_set_intl3r(
719 pinfo->memctl_opts[first_ctrl].
720 memctl_interleaving_mode);
721 break;
722 default:
723 break;
York Sunf31cfd12012-10-08 07:44:24 +0000724 }
725
York Sunc63e1372013-06-25 11:37:48 -0700726 /*
727 * Program configuration registers.
728 * JEDEC specs requires clocks to be stable before deasserting reset
729 * for RDIMMs. Clocks start after chip select is enabled and clock
730 * control register is set. During step 1, all controllers have their
731 * registers set but not enabled. Step 2 proceeds after deasserting
732 * reset through board FPGA or GPIO.
733 * For non-registered DIMMs, initialization can go through but it is
734 * also OK to follow the same flow.
735 */
York Sun1d71efb2014-08-01 15:51:00 -0700736 if (pinfo->board_need_mem_reset)
737 deassert_reset = pinfo->board_need_mem_reset();
738 for (i = first_ctrl; i <= last_ctrl; i++) {
739 if (pinfo->common_timing_params[i].all_dimms_registered)
York Sunc63e1372013-06-25 11:37:48 -0700740 deassert_reset = 1;
741 }
York Sun1d71efb2014-08-01 15:51:00 -0700742 for (i = first_ctrl; i <= last_ctrl; i++) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500743 debug("Programming controller %u\n", i);
York Sun1d71efb2014-08-01 15:51:00 -0700744 if (pinfo->common_timing_params[i].ndimms_present == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500745 debug("No dimms present on controller %u; "
746 "skipping programming\n", i);
747 continue;
748 }
York Sunc63e1372013-06-25 11:37:48 -0700749 /*
750 * The following call with step = 1 returns before enabling
751 * the controller. It has to finish with step = 2 later.
752 */
York Sun1d71efb2014-08-01 15:51:00 -0700753 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
York Sunc63e1372013-06-25 11:37:48 -0700754 deassert_reset ? 1 : 0);
755 }
756 if (deassert_reset) {
757 /* Use board FPGA or GPIO to deassert reset signal */
York Sun1d71efb2014-08-01 15:51:00 -0700758 if (pinfo->board_mem_de_reset) {
759 debug("Deasserting mem reset\n");
760 pinfo->board_mem_de_reset();
761 } else {
762 debug("Deasserting mem reset missing\n");
763 }
764 for (i = first_ctrl; i <= last_ctrl; i++) {
York Sunc63e1372013-06-25 11:37:48 -0700765 /* Call with step = 2 to continue initialization */
York Sun1d71efb2014-08-01 15:51:00 -0700766 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
York Sunc63e1372013-06-25 11:37:48 -0700767 i, 2);
768 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500769 }
770
York Sune32d59a2015-01-06 13:18:55 -0800771#ifdef CONFIG_FSL_DDR_SYNC_REFRESH
772 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
773#endif
774
York Sun9ac4ffb2013-09-30 14:20:51 -0700775#ifdef CONFIG_PPC
York Suna4c66502012-08-17 08:22:39 +0000776 /* program LAWs */
York Sun1d71efb2014-08-01 15:51:00 -0700777 for (i = first_ctrl; i <= last_ctrl; i++) {
778 if (pinfo->memctl_opts[i].memctl_interleaving) {
779 switch (pinfo->memctl_opts[i].
780 memctl_interleaving_mode) {
York Suna4c66502012-08-17 08:22:39 +0000781 case FSL_DDR_CACHE_LINE_INTERLEAVING:
782 case FSL_DDR_PAGE_INTERLEAVING:
783 case FSL_DDR_BANK_INTERLEAVING:
784 case FSL_DDR_SUPERBANK_INTERLEAVING:
York Sun1d71efb2014-08-01 15:51:00 -0700785 if (i % 2)
786 break;
York Suna4c66502012-08-17 08:22:39 +0000787 if (i == 0) {
788 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
York Sun1d71efb2014-08-01 15:51:00 -0700789 fsl_ddr_set_lawbar(
790 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000791 law_memctl, i);
792 }
York Sun51370d52016-12-28 08:43:45 -0800793#if CONFIG_SYS_NUM_DDR_CTLRS > 3
York Sun1d71efb2014-08-01 15:51:00 -0700794 else if (i == 2) {
795 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
796 fsl_ddr_set_lawbar(
797 &pinfo->common_timing_params[i],
798 law_memctl, i);
799 }
800#endif
York Suna4c66502012-08-17 08:22:39 +0000801 break;
802 case FSL_DDR_3WAY_1KB_INTERLEAVING:
803 case FSL_DDR_3WAY_4KB_INTERLEAVING:
804 case FSL_DDR_3WAY_8KB_INTERLEAVING:
805 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
806 if (i == 0) {
York Sun1d71efb2014-08-01 15:51:00 -0700807 fsl_ddr_set_lawbar(
808 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000809 law_memctl, i);
810 }
811 break;
812 case FSL_DDR_4WAY_1KB_INTERLEAVING:
813 case FSL_DDR_4WAY_4KB_INTERLEAVING:
814 case FSL_DDR_4WAY_8KB_INTERLEAVING:
815 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
816 if (i == 0)
York Sun1d71efb2014-08-01 15:51:00 -0700817 fsl_ddr_set_lawbar(
818 &pinfo->common_timing_params[i],
York Suna4c66502012-08-17 08:22:39 +0000819 law_memctl, i);
820 /* place holder for future 4-way interleaving */
821 break;
822 default:
823 break;
824 }
825 } else {
826 switch (i) {
827 case 0:
828 law_memctl = LAW_TRGT_IF_DDR_1;
829 break;
830 case 1:
831 law_memctl = LAW_TRGT_IF_DDR_2;
832 break;
833 case 2:
834 law_memctl = LAW_TRGT_IF_DDR_3;
835 break;
836 case 3:
837 law_memctl = LAW_TRGT_IF_DDR_4;
838 break;
839 default:
840 break;
841 }
York Sun1d71efb2014-08-01 15:51:00 -0700842 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
843 law_memctl, i);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500844 }
845 }
York Sun9ac4ffb2013-09-30 14:20:51 -0700846#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500847
York Suna4c66502012-08-17 08:22:39 +0000848 debug("total_memory by %s = %llu\n", __func__, total_memory);
Kumar Galae7563af2009-06-11 23:42:35 -0500849
850#if !defined(CONFIG_PHYS_64BIT)
851 /* Check for 4G or more. Bad. */
York Sun1d71efb2014-08-01 15:51:00 -0700852 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
Shruti Kanetkar2f848f92013-08-15 11:25:37 -0500853 puts("Detected ");
854 print_size(total_memory, " of memory\n");
Becky Bruce7ea38712010-12-17 17:17:59 -0600855 printf(" This U-Boot only supports < 4G of DDR\n");
856 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
Simon Glassf1683aa2017-04-06 12:47:05 -0600857 printf(" "); /* re-align to match init_dram print */
Kumar Galae7563af2009-06-11 23:42:35 -0500858 total_memory = CONFIG_MAX_MEM_MAPPED;
859 }
860#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500861
862 return total_memory;
863}
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500864
865/*
York Sun1d71efb2014-08-01 15:51:00 -0700866 * fsl_ddr_sdram(void) -- this is the main function to be
Simon Glassf1683aa2017-04-06 12:47:05 -0600867 * called by dram_init() in the board file.
York Sun1d71efb2014-08-01 15:51:00 -0700868 *
869 * It returns amount of memory configured in bytes.
870 */
871phys_size_t fsl_ddr_sdram(void)
872{
873 fsl_ddr_info_t info;
874
875 /* Reset info structure. */
876 memset(&info, 0, sizeof(fsl_ddr_info_t));
877 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
878 info.first_ctrl = 0;
879 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
880 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
881 info.board_need_mem_reset = board_need_mem_reset;
882 info.board_mem_reset = board_assert_mem_reset;
883 info.board_mem_de_reset = board_deassert_mem_reset;
York Sun61bd2f72015-11-04 09:53:10 -0800884 remove_unused_controllers(&info);
York Sun1d71efb2014-08-01 15:51:00 -0700885
886 return __fsl_ddr_sdram(&info);
887}
888
889#ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
890phys_size_t fsl_other_ddr_sdram(unsigned long long base,
891 unsigned int first_ctrl,
892 unsigned int num_ctrls,
893 unsigned int dimm_slots_per_ctrl,
894 int (*board_need_reset)(void),
895 void (*board_reset)(void),
896 void (*board_de_reset)(void))
897{
898 fsl_ddr_info_t info;
899
900 /* Reset info structure. */
901 memset(&info, 0, sizeof(fsl_ddr_info_t));
902 info.mem_base = base;
903 info.first_ctrl = first_ctrl;
904 info.num_ctrls = num_ctrls;
905 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
906 info.board_need_mem_reset = board_need_reset;
907 info.board_mem_reset = board_reset;
908 info.board_mem_de_reset = board_de_reset;
909
910 return __fsl_ddr_sdram(&info);
911}
912#endif
913
914/*
915 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
916 * size of the total memory without setting ddr control registers.
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500917 */
918phys_size_t
919fsl_ddr_sdram_size(void)
920{
921 fsl_ddr_info_t info;
922 unsigned long long total_memory = 0;
923
924 memset(&info, 0 , sizeof(fsl_ddr_info_t));
York Sun1d71efb2014-08-01 15:51:00 -0700925 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
926 info.first_ctrl = 0;
927 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
928 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
929 info.board_need_mem_reset = NULL;
Ed Swarthout81dfdee2016-01-14 12:28:04 -0600930 remove_unused_controllers(&info);
Haiying Wangfc0c2b62010-12-01 10:35:31 -0500931
932 /* Compute it once normally. */
933 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
934
935 return total_memory;
936}