Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * Version 2 as published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Generic driver for Freescale DDR/DDR2/DDR3 memory controller. |
| 11 | * Based on code from spd_sdram.c |
| 12 | * Author: James Yang [at freescale.com] |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <asm/fsl_ddr_sdram.h> |
| 17 | |
| 18 | #include "ddr.h" |
| 19 | |
| 20 | extern void fsl_ddr_set_lawbar( |
| 21 | const common_timing_params_t *memctl_common_params, |
| 22 | unsigned int memctl_interleaved, |
| 23 | unsigned int ctrl_num); |
| 24 | |
| 25 | /* processor specific function */ |
| 26 | extern void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs, |
| 27 | unsigned int ctrl_num); |
| 28 | |
| 29 | /* Board-specific functions defined in each board's ddr.c */ |
| 30 | extern void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd, |
| 31 | unsigned int ctrl_num); |
| 32 | |
| 33 | /* |
| 34 | * ASSUMPTIONS: |
| 35 | * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller |
| 36 | * - Same memory data bus width on all controllers |
| 37 | * |
| 38 | * NOTES: |
| 39 | * |
| 40 | * The memory controller and associated documentation use confusing |
| 41 | * terminology when referring to the orgranization of DRAM. |
| 42 | * |
| 43 | * Here is a terminology translation table: |
| 44 | * |
| 45 | * memory controller/documention |industry |this code |signals |
| 46 | * -------------------------------|-----------|-----------|----------------- |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 47 | * physical bank/bank |rank |rank |chip select (CS) |
| 48 | * logical bank/sub-bank |bank |bank |bank address (BA) |
| 49 | * page/row |row |page |row address |
| 50 | * ??? |column |column |column address |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 51 | * |
| 52 | * The naming confusion is further exacerbated by the descriptions of the |
| 53 | * memory controller interleaving feature, where accesses are interleaved |
| 54 | * _BETWEEN_ two seperate memory controllers. This is configured only in |
| 55 | * CS0_CONFIG[INTLV_CTL] of each memory controller. |
| 56 | * |
| 57 | * memory controller documentation | number of chip selects |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 58 | * | per memory controller supported |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 59 | * --------------------------------|----------------------------------------- |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 60 | * cache line interleaving | 1 (CS0 only) |
| 61 | * page interleaving | 1 (CS0 only) |
| 62 | * bank interleaving | 1 (CS0 only) |
| 63 | * superbank interleraving | depends on bank (chip select) |
| 64 | * | interleraving [rank interleaving] |
| 65 | * | mode used on every memory controller |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 66 | * |
| 67 | * Even further confusing is the existence of the interleaving feature |
| 68 | * _WITHIN_ each memory controller. The feature is referred to in |
| 69 | * documentation as chip select interleaving or bank interleaving, |
| 70 | * although it is configured in the DDR_SDRAM_CFG field. |
| 71 | * |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 72 | * Name of field | documentation name | this code |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 73 | * -----------------------------|-----------------------|------------------ |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 74 | * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving |
| 75 | * | interleaving |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | #ifdef DEBUG |
| 79 | const char *step_string_tbl[] = { |
| 80 | "STEP_GET_SPD", |
| 81 | "STEP_COMPUTE_DIMM_PARMS", |
| 82 | "STEP_COMPUTE_COMMON_PARMS", |
| 83 | "STEP_GATHER_OPTS", |
| 84 | "STEP_ASSIGN_ADDRESSES", |
| 85 | "STEP_COMPUTE_REGS", |
| 86 | "STEP_PROGRAM_REGS", |
| 87 | "STEP_ALL" |
| 88 | }; |
| 89 | |
| 90 | const char * step_to_string(unsigned int step) { |
| 91 | |
| 92 | unsigned int s = __ilog2(step); |
| 93 | |
| 94 | if ((1 << s) != step) |
| 95 | return step_string_tbl[7]; |
| 96 | |
| 97 | return step_string_tbl[s]; |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | int step_assign_addresses(fsl_ddr_info_t *pinfo, |
| 102 | unsigned int dbw_cap_adj[], |
| 103 | unsigned int *memctl_interleaving, |
| 104 | unsigned int *rank_interleaving) |
| 105 | { |
| 106 | int i, j; |
| 107 | |
| 108 | /* |
| 109 | * If a reduced data width is requested, but the SPD |
| 110 | * specifies a physically wider device, adjust the |
| 111 | * computed dimm capacities accordingly before |
| 112 | * assigning addresses. |
| 113 | */ |
| 114 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 115 | unsigned int found = 0; |
| 116 | |
| 117 | switch (pinfo->memctl_opts[i].data_bus_width) { |
| 118 | case 2: |
| 119 | /* 16-bit */ |
| 120 | printf("can't handle 16-bit mode yet\n"); |
| 121 | break; |
| 122 | |
| 123 | case 1: |
| 124 | /* 32-bit */ |
| 125 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 126 | unsigned int dw; |
| 127 | dw = pinfo->dimm_params[i][j].data_width; |
| 128 | if (pinfo->dimm_params[i][j].n_ranks |
| 129 | && (dw == 72 || dw == 64)) { |
| 130 | /* |
| 131 | * FIXME: can't really do it |
| 132 | * like this because this just |
| 133 | * further reduces the memory |
| 134 | */ |
| 135 | found = 1; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | if (found) { |
| 140 | dbw_cap_adj[i] = 1; |
| 141 | } |
| 142 | break; |
| 143 | |
| 144 | case 0: |
| 145 | /* 64-bit */ |
| 146 | break; |
| 147 | |
| 148 | default: |
| 149 | printf("unexpected data bus width " |
| 150 | "specified controller %u\n", i); |
| 151 | return 1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Check if all controllers are configured for memory |
| 157 | * controller interleaving. |
| 158 | */ |
| 159 | j = 0; |
| 160 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 161 | if (pinfo->memctl_opts[i].memctl_interleaving) { |
| 162 | j++; |
| 163 | } |
| 164 | } |
| 165 | if (j == 2) { |
| 166 | *memctl_interleaving = 1; |
| 167 | } |
| 168 | |
| 169 | /* Check that all controllers are rank interleaving. */ |
| 170 | j = 0; |
| 171 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 172 | if (pinfo->memctl_opts[i].ba_intlv_ctl) { |
| 173 | j++; |
| 174 | } |
| 175 | } |
| 176 | if (j == 2) { |
| 177 | *rank_interleaving = 1; |
| 178 | } |
| 179 | |
| 180 | if (*memctl_interleaving) { |
| 181 | phys_addr_t addr; |
Haiying Wang | dbbbb3a | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 182 | phys_size_t total_mem_per_ctlr = 0; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * If interleaving between memory controllers, |
| 186 | * make each controller start at a base address |
| 187 | * of 0. |
| 188 | * |
| 189 | * Also, if bank interleaving (chip select |
| 190 | * interleaving) is enabled on each memory |
| 191 | * controller, CS0 needs to be programmed to |
| 192 | * cover the entire memory range on that memory |
| 193 | * controller |
| 194 | * |
| 195 | * Bank interleaving also implies that each |
| 196 | * addressed chip select is identical in size. |
| 197 | */ |
| 198 | |
| 199 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 200 | addr = 0; |
Haiying Wang | dbbbb3a | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 201 | pinfo->common_timing_params[i].base_address = |
| 202 | (phys_addr_t)addr; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 203 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 204 | unsigned long long cap |
| 205 | = pinfo->dimm_params[i][j].capacity; |
| 206 | |
| 207 | pinfo->dimm_params[i][j].base_address = addr; |
| 208 | addr += (phys_addr_t)(cap >> dbw_cap_adj[i]); |
Haiying Wang | dbbbb3a | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 209 | total_mem_per_ctlr += cap >> dbw_cap_adj[i]; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 210 | } |
| 211 | } |
Haiying Wang | dbbbb3a | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 212 | pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr; |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 213 | } else { |
| 214 | /* |
| 215 | * Simple linear assignment if memory |
| 216 | * controllers are not interleaved. |
| 217 | */ |
| 218 | phys_size_t cur_memsize = 0; |
| 219 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 220 | phys_size_t total_mem_per_ctlr = 0; |
| 221 | pinfo->common_timing_params[i].base_address = |
| 222 | (phys_addr_t)cur_memsize; |
| 223 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 224 | /* Compute DIMM base addresses. */ |
| 225 | unsigned long long cap = |
| 226 | pinfo->dimm_params[i][j].capacity; |
| 227 | |
| 228 | pinfo->dimm_params[i][j].base_address = |
| 229 | (phys_addr_t)cur_memsize; |
| 230 | cur_memsize += cap >> dbw_cap_adj[i]; |
| 231 | total_mem_per_ctlr += cap >> dbw_cap_adj[i]; |
| 232 | } |
| 233 | pinfo->common_timing_params[i].total_mem = |
| 234 | total_mem_per_ctlr; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | phys_size_t |
| 242 | fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step) |
| 243 | { |
| 244 | unsigned int i, j; |
| 245 | unsigned int all_controllers_memctl_interleaving = 0; |
| 246 | unsigned int all_controllers_rank_interleaving = 0; |
| 247 | phys_size_t total_mem = 0; |
| 248 | |
| 249 | fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg; |
| 250 | common_timing_params_t *timing_params = pinfo->common_timing_params; |
| 251 | |
| 252 | /* data bus width capacity adjust shift amount */ |
| 253 | unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS]; |
| 254 | |
| 255 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 256 | dbw_capacity_adjust[i] = 0; |
| 257 | } |
| 258 | |
| 259 | debug("starting at step %u (%s)\n", |
| 260 | start_step, step_to_string(start_step)); |
| 261 | |
| 262 | switch (start_step) { |
| 263 | case STEP_GET_SPD: |
| 264 | /* STEP 1: Gather all DIMM SPD data */ |
| 265 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 266 | fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i); |
| 267 | } |
| 268 | |
| 269 | case STEP_COMPUTE_DIMM_PARMS: |
| 270 | /* STEP 2: Compute DIMM parameters from SPD data */ |
| 271 | |
| 272 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 273 | for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) { |
| 274 | unsigned int retval; |
| 275 | generic_spd_eeprom_t *spd = |
| 276 | &(pinfo->spd_installed_dimms[i][j]); |
| 277 | dimm_params_t *pdimm = |
Wolfgang Denk | f12e454 | 2008-09-13 02:23:05 +0200 | [diff] [blame] | 278 | &(pinfo->dimm_params[i][j]); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 279 | |
| 280 | retval = compute_dimm_parameters(spd, pdimm, i); |
| 281 | if (retval == 2) { |
| 282 | printf("Error: compute_dimm_parameters" |
| 283 | " non-zero returned FATAL value " |
| 284 | "for memctl=%u dimm=%u\n", i, j); |
| 285 | return 0; |
| 286 | } |
| 287 | if (retval) { |
| 288 | debug("Warning: compute_dimm_parameters" |
| 289 | " non-zero return value for memctl=%u " |
| 290 | "dimm=%u\n", i, j); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | case STEP_COMPUTE_COMMON_PARMS: |
| 296 | /* |
| 297 | * STEP 3: Compute a common set of timing parameters |
| 298 | * suitable for all of the DIMMs on each memory controller |
| 299 | */ |
| 300 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 301 | debug("Computing lowest common DIMM" |
| 302 | " parameters for memctl=%u\n", i); |
| 303 | compute_lowest_common_dimm_parameters( |
| 304 | pinfo->dimm_params[i], |
| 305 | &timing_params[i], |
| 306 | CONFIG_DIMM_SLOTS_PER_CTLR); |
| 307 | } |
| 308 | |
| 309 | case STEP_GATHER_OPTS: |
| 310 | /* STEP 4: Gather configuration requirements from user */ |
| 311 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 312 | debug("Reloading memory controller " |
| 313 | "configuration options for memctl=%u\n", i); |
| 314 | /* |
| 315 | * This "reloads" the memory controller options |
| 316 | * to defaults. If the user "edits" an option, |
| 317 | * next_step points to the step after this, |
| 318 | * which is currently STEP_ASSIGN_ADDRESSES. |
| 319 | */ |
| 320 | populate_memctl_options( |
| 321 | timing_params[i].all_DIMMs_registered, |
Haiying Wang | dfb4910 | 2008-10-03 12:36:55 -0400 | [diff] [blame^] | 322 | &pinfo->memctl_opts[i], |
| 323 | pinfo->dimm_params[i], i); |
Kumar Gala | 58e5e9a | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | case STEP_ASSIGN_ADDRESSES: |
| 327 | /* STEP 5: Assign addresses to chip selects */ |
| 328 | step_assign_addresses(pinfo, |
| 329 | dbw_capacity_adjust, |
| 330 | &all_controllers_memctl_interleaving, |
| 331 | &all_controllers_rank_interleaving); |
| 332 | |
| 333 | case STEP_COMPUTE_REGS: |
| 334 | /* STEP 6: compute controller register values */ |
| 335 | debug("FSL Memory ctrl cg register computation\n"); |
| 336 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 337 | if (timing_params[i].ndimms_present == 0) { |
| 338 | memset(&ddr_reg[i], 0, |
| 339 | sizeof(fsl_ddr_cfg_regs_t)); |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | compute_fsl_memctl_config_regs( |
| 344 | &pinfo->memctl_opts[i], |
| 345 | &ddr_reg[i], &timing_params[i], |
| 346 | pinfo->dimm_params[i], |
| 347 | dbw_capacity_adjust[i]); |
| 348 | } |
| 349 | |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | /* Compute the total amount of memory. */ |
| 355 | |
| 356 | /* |
| 357 | * If bank interleaving but NOT memory controller interleaving |
| 358 | * CS_BNDS describe the quantity of memory on each memory |
| 359 | * controller, so the total is the sum across. |
| 360 | */ |
| 361 | if (!all_controllers_memctl_interleaving |
| 362 | && all_controllers_rank_interleaving) { |
| 363 | total_mem = 0; |
| 364 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 365 | total_mem += timing_params[i].total_mem; |
| 366 | } |
| 367 | |
| 368 | } else { |
| 369 | /* |
| 370 | * Compute the amount of memory available just by |
| 371 | * looking for the highest valid CSn_BNDS value. |
| 372 | * This allows us to also experiment with using |
| 373 | * only CS0 when using dual-rank DIMMs. |
| 374 | */ |
| 375 | unsigned int max_end = 0; |
| 376 | |
| 377 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 378 | for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) { |
| 379 | fsl_ddr_cfg_regs_t *reg = &ddr_reg[i]; |
| 380 | if (reg->cs[j].config & 0x80000000) { |
| 381 | unsigned int end; |
| 382 | end = reg->cs[j].bnds & 0xFFF; |
| 383 | if (end > max_end) { |
| 384 | max_end = end; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | #if !defined(CONFIG_PHYS_64BIT) |
| 391 | /* Check for 4G or more with a 32-bit phys_addr_t. Bad. */ |
| 392 | if (max_end >= 0xff) { |
| 393 | printf("This U-Boot only supports < 4G of DDR\n"); |
| 394 | printf("You could rebuild it with CONFIG_PHYS_64BIT\n"); |
| 395 | return 0; /* Ensure DDR setup failure. */ |
| 396 | } |
| 397 | #endif |
| 398 | |
| 399 | total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
| 400 | | 0xFFFFFFULL); |
| 401 | } |
| 402 | |
| 403 | return total_mem; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * fsl_ddr_sdram() -- this is the main function to be called by |
| 408 | * initdram() in the board file. |
| 409 | * |
| 410 | * It returns amount of memory configured in bytes. |
| 411 | */ |
| 412 | phys_size_t fsl_ddr_sdram(void) |
| 413 | { |
| 414 | unsigned int i; |
| 415 | unsigned int memctl_interleaved; |
| 416 | phys_size_t total_memory; |
| 417 | fsl_ddr_info_t info; |
| 418 | |
| 419 | /* Reset info structure. */ |
| 420 | memset(&info, 0, sizeof(fsl_ddr_info_t)); |
| 421 | |
| 422 | /* Compute it once normally. */ |
| 423 | total_memory = fsl_ddr_compute(&info, STEP_GET_SPD); |
| 424 | |
| 425 | /* Check for memory controller interleaving. */ |
| 426 | memctl_interleaved = 0; |
| 427 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 428 | memctl_interleaved += |
| 429 | info.memctl_opts[i].memctl_interleaving; |
| 430 | } |
| 431 | |
| 432 | if (memctl_interleaved) { |
| 433 | if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) { |
| 434 | debug("memctl interleaving\n"); |
| 435 | /* |
| 436 | * Change the meaning of memctl_interleaved |
| 437 | * to be "boolean". |
| 438 | */ |
| 439 | memctl_interleaved = 1; |
| 440 | } else { |
| 441 | printf("Error: memctl interleaving not " |
| 442 | "properly configured on all controllers\n"); |
| 443 | while (1); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /* Program configuration registers. */ |
| 448 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 449 | debug("Programming controller %u\n", i); |
| 450 | if (info.common_timing_params[i].ndimms_present == 0) { |
| 451 | debug("No dimms present on controller %u; " |
| 452 | "skipping programming\n", i); |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i); |
| 457 | } |
| 458 | |
| 459 | if (memctl_interleaved) { |
| 460 | const unsigned int ctrl_num = 0; |
| 461 | |
| 462 | /* Only set LAWBAR1 if memory controller interleaving is on. */ |
| 463 | fsl_ddr_set_lawbar(&info.common_timing_params[0], |
| 464 | memctl_interleaved, ctrl_num); |
| 465 | } else { |
| 466 | /* |
| 467 | * Memory controller interleaving is NOT on; |
| 468 | * set each lawbar individually. |
| 469 | */ |
| 470 | for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) { |
| 471 | fsl_ddr_set_lawbar(&info.common_timing_params[i], |
| 472 | 0, i); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | debug("total_memory = %llu\n", (u64)total_memory); |
| 477 | |
| 478 | return total_memory; |
| 479 | } |