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