Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2 | /* |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 3 | * Copyright 2008-2014 Freescale Semiconductor, Inc. |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 4 | */ |
| 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> |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 13 | #include <i2c.h> |
York Sun | 5614e71 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 14 | #include <fsl_ddr_sdram.h> |
York Sun | 5614e71 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 15 | #include <fsl_ddr.h> |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 16 | |
York Sun | 6b9e309 | 2014-02-10 13:59:43 -0800 | [diff] [blame] | 17 | /* |
| 18 | * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view |
| 19 | * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for |
| 20 | * all Power SoCs. But it could be different for ARM SoCs. For example, |
| 21 | * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of |
| 22 | * 0x00_8000_0000 ~ 0x00_ffff_ffff |
| 23 | * 0x80_8000_0000 ~ 0xff_ffff_ffff |
| 24 | */ |
| 25 | #ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY |
Mario Six | 133ec60 | 2019-01-21 09:18:16 +0100 | [diff] [blame] | 26 | #ifdef CONFIG_MPC83xx |
| 27 | #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE |
| 28 | #else |
York Sun | 6b9e309 | 2014-02-10 13:59:43 -0800 | [diff] [blame] | 29 | #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE |
| 30 | #endif |
Mario Six | 133ec60 | 2019-01-21 09:18:16 +0100 | [diff] [blame] | 31 | #endif |
York Sun | 6b9e309 | 2014-02-10 13:59:43 -0800 | [diff] [blame] | 32 | |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 33 | #ifdef CONFIG_PPC |
| 34 | #include <asm/fsl_law.h> |
| 35 | |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 36 | void fsl_ddr_set_lawbar( |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 37 | const common_timing_params_t *memctl_common_params, |
| 38 | unsigned int memctl_interleaved, |
| 39 | unsigned int ctrl_num); |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 40 | #endif |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 41 | |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 42 | void fsl_ddr_set_intl3r(const unsigned int granule_size); |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 43 | #if defined(SPD_EEPROM_ADDRESS) || \ |
| 44 | defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \ |
| 45 | defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4) |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 46 | #if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1) |
| 47 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 48 | [0][0] = SPD_EEPROM_ADDRESS, |
| 49 | }; |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 50 | #elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 51 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
York Sun | 639f330 | 2011-08-26 11:32:41 -0700 | [diff] [blame] | 52 | [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */ |
| 53 | [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */ |
| 54 | }; |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 55 | #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1) |
| 56 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 57 | [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */ |
| 58 | [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */ |
| 59 | }; |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 60 | #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 61 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 62 | [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */ |
| 63 | [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */ |
| 64 | [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */ |
| 65 | [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */ |
| 66 | }; |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 67 | #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1) |
| 68 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 69 | [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */ |
| 70 | [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */ |
| 71 | [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */ |
| 72 | }; |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 73 | #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 74 | u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 75 | [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */ |
| 76 | [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */ |
| 77 | [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */ |
| 78 | [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */ |
| 79 | [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */ |
| 80 | [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */ |
| 81 | }; |
| 82 | |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 83 | #endif |
| 84 | |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 85 | #define SPD_SPA0_ADDRESS 0x36 |
| 86 | #define SPD_SPA1_ADDRESS 0x37 |
| 87 | |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 88 | static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address) |
| 89 | { |
Valentin Longchamp | 0778bbe | 2013-10-18 11:47:19 +0200 | [diff] [blame] | 90 | int ret; |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 92 | uint8_t dummy = 0; |
| 93 | #endif |
Valentin Longchamp | 0778bbe | 2013-10-18 11:47:19 +0200 | [diff] [blame] | 94 | |
| 95 | i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM); |
| 96 | |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 97 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 98 | /* |
| 99 | * DDR4 SPD has 384 to 512 bytes |
| 100 | * To access the lower 256 bytes, we need to set EE page address to 0 |
| 101 | * To access the upper 256 bytes, we need to set EE page address to 1 |
| 102 | * See Jedec standar No. 21-C for detail |
| 103 | */ |
| 104 | i2c_write(SPD_SPA0_ADDRESS, 0, 1, &dummy, 1); |
| 105 | ret = i2c_read(i2c_address, 0, 1, (uchar *)spd, 256); |
| 106 | if (!ret) { |
| 107 | i2c_write(SPD_SPA1_ADDRESS, 0, 1, &dummy, 1); |
| 108 | ret = i2c_read(i2c_address, 0, 1, |
| 109 | (uchar *)((ulong)spd + 256), |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 110 | min(256, |
| 111 | (int)sizeof(generic_spd_eeprom_t) - 256)); |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 112 | } |
| 113 | #else |
Valentin Longchamp | 0778bbe | 2013-10-18 11:47:19 +0200 | [diff] [blame] | 114 | ret = i2c_read(i2c_address, 0, 1, (uchar *)spd, |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 115 | sizeof(generic_spd_eeprom_t)); |
York Sun | 34e026f | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 116 | #endif |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 117 | |
| 118 | if (ret) { |
York Sun | 82968a7 | 2012-10-08 07:44:28 +0000 | [diff] [blame] | 119 | if (i2c_address == |
| 120 | #ifdef SPD_EEPROM_ADDRESS |
| 121 | SPD_EEPROM_ADDRESS |
| 122 | #elif defined(SPD_EEPROM_ADDRESS1) |
| 123 | SPD_EEPROM_ADDRESS1 |
| 124 | #endif |
| 125 | ) { |
| 126 | printf("DDR: failed to read SPD from address %u\n", |
| 127 | i2c_address); |
| 128 | } else { |
| 129 | debug("DDR: failed to read SPD from address %u\n", |
| 130 | i2c_address); |
| 131 | } |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 132 | memset(spd, 0, sizeof(generic_spd_eeprom_t)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | __attribute__((weak, alias("__get_spd"))) |
| 137 | void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address); |
| 138 | |
York Sun | b92557c | 2015-05-28 14:54:08 +0530 | [diff] [blame] | 139 | /* This function allows boards to update SPD address */ |
| 140 | __weak void update_spd_address(unsigned int ctrl_num, |
| 141 | unsigned int slot, |
| 142 | unsigned int *addr) |
| 143 | { |
| 144 | } |
| 145 | |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 146 | void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd, |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 147 | unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl) |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 148 | { |
| 149 | unsigned int i; |
| 150 | unsigned int i2c_address = 0; |
| 151 | |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 152 | if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) { |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 153 | printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num); |
| 154 | return; |
| 155 | } |
| 156 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 157 | for (i = 0; i < dimm_slots_per_ctrl; i++) { |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 158 | i2c_address = spd_i2c_addr[ctrl_num][i]; |
York Sun | b92557c | 2015-05-28 14:54:08 +0530 | [diff] [blame] | 159 | update_spd_address(ctrl_num, i, &i2c_address); |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 160 | get_spd(&(ctrl_dimms_spd[i]), i2c_address); |
| 161 | } |
| 162 | } |
| 163 | #else |
| 164 | void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd, |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 165 | unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl) |
Kumar Gala | c39f44d | 2011-01-31 22:18:47 -0600 | [diff] [blame] | 166 | { |
| 167 | } |
| 168 | #endif /* SPD_EEPROM_ADDRESSx */ |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * ASSUMPTIONS: |
| 172 | * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller |
| 173 | * - Same memory data bus width on all controllers |
| 174 | * |
| 175 | * NOTES: |
| 176 | * |
| 177 | * The memory controller and associated documentation use confusing |
| 178 | * terminology when referring to the orgranization of DRAM. |
| 179 | * |
| 180 | * Here is a terminology translation table: |
| 181 | * |
| 182 | * memory controller/documention |industry |this code |signals |
| 183 | * -------------------------------|-----------|-----------|----------------- |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 184 | * physical bank/bank |rank |rank |chip select (CS) |
| 185 | * logical bank/sub-bank |bank |bank |bank address (BA) |
| 186 | * page/row |row |page |row address |
| 187 | * ??? |column |column |column address |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 188 | * |
| 189 | * The naming confusion is further exacerbated by the descriptions of the |
| 190 | * memory controller interleaving feature, where accesses are interleaved |
| 191 | * _BETWEEN_ two seperate memory controllers. This is configured only in |
| 192 | * CS0_CONFIG[INTLV_CTL] of each memory controller. |
| 193 | * |
| 194 | * memory controller documentation | number of chip selects |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 195 | * | per memory controller supported |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 196 | * --------------------------------|----------------------------------------- |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 197 | * cache line interleaving | 1 (CS0 only) |
| 198 | * page interleaving | 1 (CS0 only) |
| 199 | * bank interleaving | 1 (CS0 only) |
| 200 | * superbank interleraving | depends on bank (chip select) |
| 201 | * | interleraving [rank interleaving] |
| 202 | * | mode used on every memory controller |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 203 | * |
| 204 | * Even further confusing is the existence of the interleaving feature |
| 205 | * _WITHIN_ each memory controller. The feature is referred to in |
| 206 | * documentation as chip select interleaving or bank interleaving, |
| 207 | * although it is configured in the DDR_SDRAM_CFG field. |
| 208 | * |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 209 | * Name of field | documentation name | this code |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 210 | * -----------------------------|-----------------------|------------------ |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 211 | * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving |
| 212 | * | interleaving |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 213 | */ |
| 214 | |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 215 | const char *step_string_tbl[] = { |
| 216 | "STEP_GET_SPD", |
| 217 | "STEP_COMPUTE_DIMM_PARMS", |
| 218 | "STEP_COMPUTE_COMMON_PARMS", |
| 219 | "STEP_GATHER_OPTS", |
| 220 | "STEP_ASSIGN_ADDRESSES", |
| 221 | "STEP_COMPUTE_REGS", |
| 222 | "STEP_PROGRAM_REGS", |
| 223 | "STEP_ALL" |
| 224 | }; |
| 225 | |
| 226 | const char * step_to_string(unsigned int step) { |
| 227 | |
| 228 | unsigned int s = __ilog2(step); |
| 229 | |
| 230 | if ((1 << s) != step) |
| 231 | return step_string_tbl[7]; |
| 232 | |
York Sun | 349689b | 2014-04-01 14:20:49 -0700 | [diff] [blame] | 233 | if (s >= ARRAY_SIZE(step_string_tbl)) { |
| 234 | printf("Error for the step in %s\n", __func__); |
| 235 | s = 0; |
| 236 | } |
| 237 | |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 238 | return step_string_tbl[s]; |
| 239 | } |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 240 | |
York Sun | ef00227 | 2013-03-25 07:39:35 +0000 | [diff] [blame] | 241 | static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo, |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 242 | unsigned int dbw_cap_adj[]) |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 243 | { |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 244 | unsigned int i, j; |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 245 | unsigned long long total_mem, current_mem_base, total_ctlr_mem; |
| 246 | unsigned long long rank_density, ctlr_density = 0; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 247 | unsigned int first_ctrl = pinfo->first_ctrl; |
| 248 | unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * If a reduced data width is requested, but the SPD |
| 252 | * specifies a physically wider device, adjust the |
| 253 | * computed dimm capacities accordingly before |
| 254 | * assigning addresses. |
| 255 | */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 256 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 257 | unsigned int found = 0; |
| 258 | |
| 259 | switch (pinfo->memctl_opts[i].data_bus_width) { |
| 260 | case 2: |
| 261 | /* 16-bit */ |
York Sun | 51d498f | 2011-05-27 07:25:51 +0800 | [diff] [blame] | 262 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 263 | unsigned int dw; |
| 264 | if (!pinfo->dimm_params[i][j].n_ranks) |
| 265 | continue; |
| 266 | dw = pinfo->dimm_params[i][j].primary_sdram_width; |
| 267 | if ((dw == 72 || dw == 64)) { |
| 268 | dbw_cap_adj[i] = 2; |
| 269 | break; |
| 270 | } else if ((dw == 40 || dw == 32)) { |
| 271 | dbw_cap_adj[i] = 1; |
| 272 | break; |
| 273 | } |
| 274 | } |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | case 1: |
| 278 | /* 32-bit */ |
| 279 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 280 | unsigned int dw; |
| 281 | dw = pinfo->dimm_params[i][j].data_width; |
| 282 | if (pinfo->dimm_params[i][j].n_ranks |
| 283 | && (dw == 72 || dw == 64)) { |
| 284 | /* |
| 285 | * FIXME: can't really do it |
| 286 | * like this because this just |
| 287 | * further reduces the memory |
| 288 | */ |
| 289 | found = 1; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | if (found) { |
| 294 | dbw_cap_adj[i] = 1; |
| 295 | } |
| 296 | break; |
| 297 | |
| 298 | case 0: |
| 299 | /* 64-bit */ |
| 300 | break; |
| 301 | |
| 302 | default: |
| 303 | printf("unexpected data bus width " |
| 304 | "specified controller %u\n", i); |
| 305 | return 1; |
| 306 | } |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 307 | debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 308 | } |
| 309 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 310 | current_mem_base = pinfo->mem_base; |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 311 | total_mem = 0; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 312 | if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) { |
| 313 | rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >> |
| 314 | dbw_cap_adj[first_ctrl]; |
| 315 | switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl & |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 316 | FSL_DDR_CS0_CS1_CS2_CS3) { |
| 317 | case FSL_DDR_CS0_CS1_CS2_CS3: |
| 318 | ctlr_density = 4 * rank_density; |
| 319 | break; |
| 320 | case FSL_DDR_CS0_CS1: |
| 321 | case FSL_DDR_CS0_CS1_AND_CS2_CS3: |
| 322 | ctlr_density = 2 * rank_density; |
| 323 | break; |
| 324 | case FSL_DDR_CS2_CS3: |
| 325 | default: |
| 326 | ctlr_density = rank_density; |
| 327 | break; |
| 328 | } |
| 329 | debug("rank density is 0x%llx, ctlr density is 0x%llx\n", |
| 330 | rank_density, ctlr_density); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 331 | for (i = first_ctrl; i <= last_ctrl; i++) { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 332 | if (pinfo->memctl_opts[i].memctl_interleaving) { |
| 333 | switch (pinfo->memctl_opts[i].memctl_interleaving_mode) { |
York Sun | 6b1e125 | 2014-02-10 13:59:44 -0800 | [diff] [blame] | 334 | case FSL_DDR_256B_INTERLEAVING: |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 335 | case FSL_DDR_CACHE_LINE_INTERLEAVING: |
| 336 | case FSL_DDR_PAGE_INTERLEAVING: |
| 337 | case FSL_DDR_BANK_INTERLEAVING: |
| 338 | case FSL_DDR_SUPERBANK_INTERLEAVING: |
| 339 | total_ctlr_mem = 2 * ctlr_density; |
| 340 | break; |
| 341 | case FSL_DDR_3WAY_1KB_INTERLEAVING: |
| 342 | case FSL_DDR_3WAY_4KB_INTERLEAVING: |
| 343 | case FSL_DDR_3WAY_8KB_INTERLEAVING: |
| 344 | total_ctlr_mem = 3 * ctlr_density; |
| 345 | break; |
| 346 | case FSL_DDR_4WAY_1KB_INTERLEAVING: |
| 347 | case FSL_DDR_4WAY_4KB_INTERLEAVING: |
| 348 | case FSL_DDR_4WAY_8KB_INTERLEAVING: |
| 349 | total_ctlr_mem = 4 * ctlr_density; |
| 350 | break; |
| 351 | default: |
| 352 | panic("Unknown interleaving mode"); |
| 353 | } |
| 354 | pinfo->common_timing_params[i].base_address = |
| 355 | current_mem_base; |
| 356 | pinfo->common_timing_params[i].total_mem = |
| 357 | total_ctlr_mem; |
| 358 | total_mem = current_mem_base + total_ctlr_mem; |
| 359 | debug("ctrl %d base 0x%llx\n", i, current_mem_base); |
| 360 | debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem); |
| 361 | } else { |
| 362 | /* when 3rd controller not interleaved */ |
| 363 | current_mem_base = total_mem; |
| 364 | total_ctlr_mem = 0; |
| 365 | pinfo->common_timing_params[i].base_address = |
| 366 | current_mem_base; |
| 367 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 368 | unsigned long long cap = |
| 369 | pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i]; |
| 370 | pinfo->dimm_params[i][j].base_address = |
| 371 | current_mem_base; |
| 372 | debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base); |
| 373 | current_mem_base += cap; |
| 374 | total_ctlr_mem += cap; |
| 375 | } |
| 376 | debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem); |
| 377 | pinfo->common_timing_params[i].total_mem = |
| 378 | total_ctlr_mem; |
| 379 | total_mem += total_ctlr_mem; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | /* |
| 384 | * Simple linear assignment if memory |
| 385 | * controllers are not interleaved. |
| 386 | */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 387 | for (i = first_ctrl; i <= last_ctrl; i++) { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 388 | total_ctlr_mem = 0; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 389 | pinfo->common_timing_params[i].base_address = |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 390 | current_mem_base; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 391 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 392 | /* Compute DIMM base addresses. */ |
| 393 | unsigned long long cap = |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 394 | pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i]; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 395 | pinfo->dimm_params[i][j].base_address = |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 396 | current_mem_base; |
| 397 | debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base); |
| 398 | current_mem_base += cap; |
| 399 | total_ctlr_mem += cap; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 400 | } |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 401 | debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 402 | pinfo->common_timing_params[i].total_mem = |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 403 | total_ctlr_mem; |
| 404 | total_mem += total_ctlr_mem; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 405 | } |
| 406 | } |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 407 | debug("Total mem by %s is 0x%llx\n", __func__, total_mem); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 408 | |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 409 | return total_mem; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 410 | } |
| 411 | |
York Sun | ef00227 | 2013-03-25 07:39:35 +0000 | [diff] [blame] | 412 | /* Use weak function to allow board file to override the address assignment */ |
| 413 | __attribute__((weak, alias("__step_assign_addresses"))) |
| 414 | unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo, |
| 415 | unsigned int dbw_cap_adj[]); |
| 416 | |
Kumar Gala | e7563af | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 417 | unsigned long long |
Haiying Wang | fc0c2b6 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 418 | fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step, |
| 419 | unsigned int size_only) |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 420 | { |
| 421 | unsigned int i, j; |
Kumar Gala | e7563af | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 422 | unsigned long long total_mem = 0; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 423 | int assert_reset = 0; |
| 424 | unsigned int first_ctrl = pinfo->first_ctrl; |
| 425 | unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1; |
| 426 | __maybe_unused int retval; |
| 427 | __maybe_unused bool goodspd = false; |
| 428 | __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 429 | |
| 430 | fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg; |
| 431 | common_timing_params_t *timing_params = pinfo->common_timing_params; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 432 | if (pinfo->board_need_mem_reset) |
| 433 | assert_reset = pinfo->board_need_mem_reset(); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 434 | |
| 435 | /* data bus width capacity adjust shift amount */ |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 436 | unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS]; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 437 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 438 | for (i = first_ctrl; i <= last_ctrl; i++) |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 439 | dbw_capacity_adjust[i] = 0; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 440 | |
| 441 | debug("starting at step %u (%s)\n", |
| 442 | start_step, step_to_string(start_step)); |
| 443 | |
| 444 | switch (start_step) { |
| 445 | case STEP_GET_SPD: |
York Sun | 1b3e3c4 | 2011-06-07 09:42:16 +0800 | [diff] [blame] | 446 | #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM) |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 447 | /* STEP 1: Gather all DIMM SPD data */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 448 | for (i = first_ctrl; i <= last_ctrl; i++) { |
| 449 | fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i, |
| 450 | dimm_slots_per_ctrl); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | case STEP_COMPUTE_DIMM_PARMS: |
| 454 | /* STEP 2: Compute DIMM parameters from SPD data */ |
| 455 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 456 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 457 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 458 | generic_spd_eeprom_t *spd = |
| 459 | &(pinfo->spd_installed_dimms[i][j]); |
| 460 | dimm_params_t *pdimm = |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 461 | &(pinfo->dimm_params[i][j]); |
York Sun | 03e664d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 462 | retval = compute_dimm_parameters( |
| 463 | i, spd, pdimm, j); |
York Sun | f2d264b | 2011-06-07 09:42:17 +0800 | [diff] [blame] | 464 | #ifdef CONFIG_SYS_DDR_RAW_TIMING |
York Sun | 66869f9 | 2015-03-19 09:30:26 -0700 | [diff] [blame] | 465 | if (!j && retval) { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 466 | printf("SPD error on controller %d! " |
| 467 | "Trying fallback to raw timing " |
| 468 | "calculation\n", i); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 469 | retval = fsl_ddr_get_dimm_params(pdimm, |
| 470 | i, j); |
York Sun | f2d264b | 2011-06-07 09:42:17 +0800 | [diff] [blame] | 471 | } |
| 472 | #else |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 473 | if (retval == 2) { |
| 474 | printf("Error: compute_dimm_parameters" |
| 475 | " non-zero returned FATAL value " |
| 476 | "for memctl=%u dimm=%u\n", i, j); |
| 477 | return 0; |
| 478 | } |
York Sun | f2d264b | 2011-06-07 09:42:17 +0800 | [diff] [blame] | 479 | #endif |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 480 | if (retval) { |
| 481 | debug("Warning: compute_dimm_parameters" |
| 482 | " non-zero return value for memctl=%u " |
| 483 | "dimm=%u\n", i, j); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 484 | } else { |
| 485 | goodspd = true; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 489 | if (!goodspd) { |
| 490 | /* |
| 491 | * No valid SPD found |
| 492 | * Throw an error if this is for main memory, i.e. |
| 493 | * first_ctrl == 0. Otherwise, siliently return 0 |
| 494 | * as the memory size. |
| 495 | */ |
| 496 | if (first_ctrl == 0) |
| 497 | printf("Error: No valid SPD detected.\n"); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 498 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 499 | return 0; |
| 500 | } |
Shaohui Xie | 98de369 | 2012-06-28 23:36:38 +0000 | [diff] [blame] | 501 | #elif defined(CONFIG_SYS_DDR_RAW_TIMING) |
York Sun | 1b3e3c4 | 2011-06-07 09:42:16 +0800 | [diff] [blame] | 502 | case STEP_COMPUTE_DIMM_PARMS: |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 503 | for (i = first_ctrl; i <= last_ctrl; i++) { |
York Sun | 1b3e3c4 | 2011-06-07 09:42:16 +0800 | [diff] [blame] | 504 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 505 | dimm_params_t *pdimm = |
| 506 | &(pinfo->dimm_params[i][j]); |
| 507 | fsl_ddr_get_dimm_params(pdimm, i, j); |
| 508 | } |
| 509 | } |
| 510 | debug("Filling dimm parameters from board specific file\n"); |
| 511 | #endif |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 512 | case STEP_COMPUTE_COMMON_PARMS: |
| 513 | /* |
| 514 | * STEP 3: Compute a common set of timing parameters |
| 515 | * suitable for all of the DIMMs on each memory controller |
| 516 | */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 517 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 518 | debug("Computing lowest common DIMM" |
| 519 | " parameters for memctl=%u\n", i); |
York Sun | 03e664d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 520 | compute_lowest_common_dimm_parameters |
| 521 | (i, |
| 522 | pinfo->dimm_params[i], |
| 523 | &timing_params[i], |
| 524 | CONFIG_DIMM_SLOTS_PER_CTLR); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | case STEP_GATHER_OPTS: |
| 528 | /* STEP 4: Gather configuration requirements from user */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 529 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 530 | debug("Reloading memory controller " |
| 531 | "configuration options for memctl=%u\n", i); |
| 532 | /* |
| 533 | * This "reloads" the memory controller options |
| 534 | * to defaults. If the user "edits" an option, |
| 535 | * next_step points to the step after this, |
| 536 | * which is currently STEP_ASSIGN_ADDRESSES. |
| 537 | */ |
| 538 | populate_memctl_options( |
York Sun | 5684842 | 2015-07-23 14:04:48 -0700 | [diff] [blame] | 539 | &timing_params[i], |
Haiying Wang | dfb4910 | 2008-10-03 12:36:55 -0400 | [diff] [blame] | 540 | &pinfo->memctl_opts[i], |
| 541 | pinfo->dimm_params[i], i); |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 542 | /* |
| 543 | * For RDIMMs, JEDEC spec requires clocks to be stable |
| 544 | * before reset signal is deasserted. For the boards |
| 545 | * using fixed parameters, this function should be |
| 546 | * be called from board init file. |
| 547 | */ |
Priyanka Jain | 0dd38a3 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 548 | if (timing_params[i].all_dimms_registered) |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 549 | assert_reset = 1; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 550 | } |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 551 | if (assert_reset && !size_only) { |
| 552 | if (pinfo->board_mem_reset) { |
| 553 | debug("Asserting mem reset\n"); |
| 554 | pinfo->board_mem_reset(); |
| 555 | } else { |
| 556 | debug("Asserting mem reset missing\n"); |
| 557 | } |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 560 | case STEP_ASSIGN_ADDRESSES: |
| 561 | /* STEP 5: Assign addresses to chip selects */ |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 562 | check_interleaving_options(pinfo); |
| 563 | total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust); |
York Sun | 349689b | 2014-04-01 14:20:49 -0700 | [diff] [blame] | 564 | debug("Total mem %llu assigned\n", total_mem); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 565 | |
| 566 | case STEP_COMPUTE_REGS: |
| 567 | /* STEP 6: compute controller register values */ |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 568 | debug("FSL Memory ctrl register computation\n"); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 569 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 570 | if (timing_params[i].ndimms_present == 0) { |
| 571 | memset(&ddr_reg[i], 0, |
| 572 | sizeof(fsl_ddr_cfg_regs_t)); |
| 573 | continue; |
| 574 | } |
| 575 | |
York Sun | 03e664d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 576 | compute_fsl_memctl_config_regs |
| 577 | (i, |
| 578 | &pinfo->memctl_opts[i], |
| 579 | &ddr_reg[i], &timing_params[i], |
| 580 | pinfo->dimm_params[i], |
| 581 | dbw_capacity_adjust[i], |
| 582 | size_only); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | default: |
| 586 | break; |
| 587 | } |
| 588 | |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 589 | { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 590 | /* |
| 591 | * Compute the amount of memory available just by |
| 592 | * looking for the highest valid CSn_BNDS value. |
| 593 | * This allows us to also experiment with using |
| 594 | * only CS0 when using dual-rank DIMMs. |
| 595 | */ |
| 596 | unsigned int max_end = 0; |
| 597 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 598 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 599 | for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) { |
| 600 | fsl_ddr_cfg_regs_t *reg = &ddr_reg[i]; |
| 601 | if (reg->cs[j].config & 0x80000000) { |
| 602 | unsigned int end; |
York Sun | d8556db | 2013-06-25 11:37:45 -0700 | [diff] [blame] | 603 | /* |
| 604 | * 0xfffffff is a special value we put |
| 605 | * for unused bnds |
| 606 | */ |
| 607 | if (reg->cs[j].bnds == 0xffffffff) |
| 608 | continue; |
| 609 | end = reg->cs[j].bnds & 0xffff; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 610 | if (end > max_end) { |
| 611 | max_end = end; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
York Sun | 00ec3fd | 2013-10-28 16:36:02 -0700 | [diff] [blame] | 617 | total_mem = 1 + (((unsigned long long)max_end << 24ULL) | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 618 | 0xFFFFFFULL) - pinfo->mem_base; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return total_mem; |
| 622 | } |
| 623 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 624 | phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo) |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 625 | { |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 626 | unsigned int i, first_ctrl, last_ctrl; |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 627 | #ifdef CONFIG_PPC |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 628 | unsigned int law_memctl = LAW_TRGT_IF_DDR_1; |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 629 | #endif |
Kumar Gala | e7563af | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 630 | unsigned long long total_memory; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 631 | int deassert_reset = 0; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 632 | |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 633 | first_ctrl = pinfo->first_ctrl; |
| 634 | last_ctrl = first_ctrl + pinfo->num_ctrls - 1; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 635 | |
| 636 | /* Compute it once normally. */ |
York Sun | 6f5e1dc | 2011-09-16 13:21:35 -0700 | [diff] [blame] | 637 | #ifdef CONFIG_FSL_DDR_INTERACTIVE |
James Yang | e8ba6c5 | 2013-01-07 14:01:03 +0000 | [diff] [blame] | 638 | if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 639 | total_memory = fsl_ddr_interactive(pinfo, 0); |
James Yang | e8ba6c5 | 2013-01-07 14:01:03 +0000 | [diff] [blame] | 640 | } else if (fsl_ddr_interactive_env_var_exists()) { |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 641 | total_memory = fsl_ddr_interactive(pinfo, 1); |
York Sun | e750cfa | 2013-01-04 08:13:59 +0000 | [diff] [blame] | 642 | } else |
York Sun | 6f5e1dc | 2011-09-16 13:21:35 -0700 | [diff] [blame] | 643 | #endif |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 644 | total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 645 | |
York Sun | f31cfd1 | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 646 | /* setup 3-way interleaving before enabling DDRC */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 647 | switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) { |
| 648 | case FSL_DDR_3WAY_1KB_INTERLEAVING: |
| 649 | case FSL_DDR_3WAY_4KB_INTERLEAVING: |
| 650 | case FSL_DDR_3WAY_8KB_INTERLEAVING: |
| 651 | fsl_ddr_set_intl3r( |
| 652 | pinfo->memctl_opts[first_ctrl]. |
| 653 | memctl_interleaving_mode); |
| 654 | break; |
| 655 | default: |
| 656 | break; |
York Sun | f31cfd1 | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 657 | } |
| 658 | |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Program configuration registers. |
| 661 | * JEDEC specs requires clocks to be stable before deasserting reset |
| 662 | * for RDIMMs. Clocks start after chip select is enabled and clock |
| 663 | * control register is set. During step 1, all controllers have their |
| 664 | * registers set but not enabled. Step 2 proceeds after deasserting |
| 665 | * reset through board FPGA or GPIO. |
| 666 | * For non-registered DIMMs, initialization can go through but it is |
| 667 | * also OK to follow the same flow. |
| 668 | */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 669 | if (pinfo->board_need_mem_reset) |
| 670 | deassert_reset = pinfo->board_need_mem_reset(); |
| 671 | for (i = first_ctrl; i <= last_ctrl; i++) { |
| 672 | if (pinfo->common_timing_params[i].all_dimms_registered) |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 673 | deassert_reset = 1; |
| 674 | } |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 675 | for (i = first_ctrl; i <= last_ctrl; i++) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 676 | debug("Programming controller %u\n", i); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 677 | if (pinfo->common_timing_params[i].ndimms_present == 0) { |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 678 | debug("No dimms present on controller %u; " |
| 679 | "skipping programming\n", i); |
| 680 | continue; |
| 681 | } |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 682 | /* |
| 683 | * The following call with step = 1 returns before enabling |
| 684 | * the controller. It has to finish with step = 2 later. |
| 685 | */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 686 | fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i, |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 687 | deassert_reset ? 1 : 0); |
| 688 | } |
| 689 | if (deassert_reset) { |
| 690 | /* Use board FPGA or GPIO to deassert reset signal */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 691 | if (pinfo->board_mem_de_reset) { |
| 692 | debug("Deasserting mem reset\n"); |
| 693 | pinfo->board_mem_de_reset(); |
| 694 | } else { |
| 695 | debug("Deasserting mem reset missing\n"); |
| 696 | } |
| 697 | for (i = first_ctrl; i <= last_ctrl; i++) { |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 698 | /* Call with step = 2 to continue initialization */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 699 | fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), |
York Sun | c63e137 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 700 | i, 2); |
| 701 | } |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 702 | } |
| 703 | |
York Sun | e32d59a | 2015-01-06 13:18:55 -0800 | [diff] [blame] | 704 | #ifdef CONFIG_FSL_DDR_SYNC_REFRESH |
| 705 | fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl); |
| 706 | #endif |
| 707 | |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 708 | #ifdef CONFIG_PPC |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 709 | /* program LAWs */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 710 | for (i = first_ctrl; i <= last_ctrl; i++) { |
| 711 | if (pinfo->memctl_opts[i].memctl_interleaving) { |
| 712 | switch (pinfo->memctl_opts[i]. |
| 713 | memctl_interleaving_mode) { |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 714 | case FSL_DDR_CACHE_LINE_INTERLEAVING: |
| 715 | case FSL_DDR_PAGE_INTERLEAVING: |
| 716 | case FSL_DDR_BANK_INTERLEAVING: |
| 717 | case FSL_DDR_SUPERBANK_INTERLEAVING: |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 718 | if (i % 2) |
| 719 | break; |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 720 | if (i == 0) { |
| 721 | law_memctl = LAW_TRGT_IF_DDR_INTRLV; |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 722 | fsl_ddr_set_lawbar( |
| 723 | &pinfo->common_timing_params[i], |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 724 | law_memctl, i); |
| 725 | } |
York Sun | 51370d5 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 726 | #if CONFIG_SYS_NUM_DDR_CTLRS > 3 |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 727 | else if (i == 2) { |
| 728 | law_memctl = LAW_TRGT_IF_DDR_INTLV_34; |
| 729 | fsl_ddr_set_lawbar( |
| 730 | &pinfo->common_timing_params[i], |
| 731 | law_memctl, i); |
| 732 | } |
| 733 | #endif |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 734 | break; |
| 735 | case FSL_DDR_3WAY_1KB_INTERLEAVING: |
| 736 | case FSL_DDR_3WAY_4KB_INTERLEAVING: |
| 737 | case FSL_DDR_3WAY_8KB_INTERLEAVING: |
| 738 | law_memctl = LAW_TRGT_IF_DDR_INTLV_123; |
| 739 | if (i == 0) { |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 740 | fsl_ddr_set_lawbar( |
| 741 | &pinfo->common_timing_params[i], |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 742 | law_memctl, i); |
| 743 | } |
| 744 | break; |
| 745 | case FSL_DDR_4WAY_1KB_INTERLEAVING: |
| 746 | case FSL_DDR_4WAY_4KB_INTERLEAVING: |
| 747 | case FSL_DDR_4WAY_8KB_INTERLEAVING: |
| 748 | law_memctl = LAW_TRGT_IF_DDR_INTLV_1234; |
| 749 | if (i == 0) |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 750 | fsl_ddr_set_lawbar( |
| 751 | &pinfo->common_timing_params[i], |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 752 | law_memctl, i); |
| 753 | /* place holder for future 4-way interleaving */ |
| 754 | break; |
| 755 | default: |
| 756 | break; |
| 757 | } |
| 758 | } else { |
| 759 | switch (i) { |
| 760 | case 0: |
| 761 | law_memctl = LAW_TRGT_IF_DDR_1; |
| 762 | break; |
| 763 | case 1: |
| 764 | law_memctl = LAW_TRGT_IF_DDR_2; |
| 765 | break; |
| 766 | case 2: |
| 767 | law_memctl = LAW_TRGT_IF_DDR_3; |
| 768 | break; |
| 769 | case 3: |
| 770 | law_memctl = LAW_TRGT_IF_DDR_4; |
| 771 | break; |
| 772 | default: |
| 773 | break; |
| 774 | } |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 775 | fsl_ddr_set_lawbar(&pinfo->common_timing_params[i], |
| 776 | law_memctl, i); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 777 | } |
| 778 | } |
York Sun | 9ac4ffb | 2013-09-30 14:20:51 -0700 | [diff] [blame] | 779 | #endif |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 780 | |
York Sun | a4c6650 | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 781 | debug("total_memory by %s = %llu\n", __func__, total_memory); |
Kumar Gala | e7563af | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 782 | |
| 783 | #if !defined(CONFIG_PHYS_64BIT) |
| 784 | /* Check for 4G or more. Bad. */ |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 785 | if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) { |
Shruti Kanetkar | 2f848f9 | 2013-08-15 11:25:37 -0500 | [diff] [blame] | 786 | puts("Detected "); |
| 787 | print_size(total_memory, " of memory\n"); |
Becky Bruce | 7ea3871 | 2010-12-17 17:17:59 -0600 | [diff] [blame] | 788 | printf(" This U-Boot only supports < 4G of DDR\n"); |
| 789 | printf(" You could rebuild it with CONFIG_PHYS_64BIT\n"); |
Simon Glass | f1683aa | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 790 | printf(" "); /* re-align to match init_dram print */ |
Kumar Gala | e7563af | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 791 | total_memory = CONFIG_MAX_MEM_MAPPED; |
| 792 | } |
| 793 | #endif |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 794 | |
| 795 | return total_memory; |
| 796 | } |
Haiying Wang | fc0c2b6 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 797 | |
| 798 | /* |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 799 | * fsl_ddr_sdram(void) -- this is the main function to be |
Simon Glass | f1683aa | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 800 | * called by dram_init() in the board file. |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 801 | * |
| 802 | * It returns amount of memory configured in bytes. |
| 803 | */ |
| 804 | phys_size_t fsl_ddr_sdram(void) |
| 805 | { |
| 806 | fsl_ddr_info_t info; |
| 807 | |
| 808 | /* Reset info structure. */ |
| 809 | memset(&info, 0, sizeof(fsl_ddr_info_t)); |
| 810 | info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY; |
| 811 | info.first_ctrl = 0; |
| 812 | info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS; |
| 813 | info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR; |
| 814 | info.board_need_mem_reset = board_need_mem_reset; |
| 815 | info.board_mem_reset = board_assert_mem_reset; |
| 816 | info.board_mem_de_reset = board_deassert_mem_reset; |
York Sun | 61bd2f7 | 2015-11-04 09:53:10 -0800 | [diff] [blame] | 817 | remove_unused_controllers(&info); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 818 | |
| 819 | return __fsl_ddr_sdram(&info); |
| 820 | } |
| 821 | |
| 822 | #ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS |
| 823 | phys_size_t fsl_other_ddr_sdram(unsigned long long base, |
| 824 | unsigned int first_ctrl, |
| 825 | unsigned int num_ctrls, |
| 826 | unsigned int dimm_slots_per_ctrl, |
| 827 | int (*board_need_reset)(void), |
| 828 | void (*board_reset)(void), |
| 829 | void (*board_de_reset)(void)) |
| 830 | { |
| 831 | fsl_ddr_info_t info; |
| 832 | |
| 833 | /* Reset info structure. */ |
| 834 | memset(&info, 0, sizeof(fsl_ddr_info_t)); |
| 835 | info.mem_base = base; |
| 836 | info.first_ctrl = first_ctrl; |
| 837 | info.num_ctrls = num_ctrls; |
| 838 | info.dimm_slots_per_ctrl = dimm_slots_per_ctrl; |
| 839 | info.board_need_mem_reset = board_need_reset; |
| 840 | info.board_mem_reset = board_reset; |
| 841 | info.board_mem_de_reset = board_de_reset; |
| 842 | |
| 843 | return __fsl_ddr_sdram(&info); |
| 844 | } |
| 845 | #endif |
| 846 | |
| 847 | /* |
| 848 | * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the |
| 849 | * size of the total memory without setting ddr control registers. |
Haiying Wang | fc0c2b6 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 850 | */ |
| 851 | phys_size_t |
| 852 | fsl_ddr_sdram_size(void) |
| 853 | { |
| 854 | fsl_ddr_info_t info; |
| 855 | unsigned long long total_memory = 0; |
| 856 | |
| 857 | memset(&info, 0 , sizeof(fsl_ddr_info_t)); |
York Sun | 1d71efb | 2014-08-01 15:51:00 -0700 | [diff] [blame] | 858 | info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY; |
| 859 | info.first_ctrl = 0; |
| 860 | info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS; |
| 861 | info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR; |
| 862 | info.board_need_mem_reset = NULL; |
Ed Swarthout | 81dfdee | 2016-01-14 12:28:04 -0600 | [diff] [blame] | 863 | remove_unused_controllers(&info); |
Haiying Wang | fc0c2b6 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 864 | |
| 865 | /* Compute it once normally. */ |
| 866 | total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1); |
| 867 | |
| 868 | return total_memory; |
| 869 | } |