Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1 | /* |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 2 | * Copyright 2008-2014 Freescale Semiconductor, Inc. |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Generic driver for Freescale DDR/DDR2/DDR3 memory controller. |
| 9 | * Based on code from spd_sdram.c |
| 10 | * Author: James Yang [at freescale.com] |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 14 | #include <fsl_ddr_sdram.h> |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 15 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 16 | #include <fsl_ddr.h> |
York Sun | a21803d | 2013-11-18 10:29:32 -0800 | [diff] [blame] | 17 | #include <fsl_immap.h> |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 19 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 20 | unsigned int picos_to_mclk(unsigned int picos); |
| 21 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 22 | /* |
| 23 | * Determine Rtt value. |
| 24 | * |
| 25 | * This should likely be either board or controller specific. |
| 26 | * |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 27 | * Rtt(nominal) - DDR2: |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 28 | * 0 = Rtt disabled |
| 29 | * 1 = 75 ohm |
| 30 | * 2 = 150 ohm |
| 31 | * 3 = 50 ohm |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 32 | * Rtt(nominal) - DDR3: |
| 33 | * 0 = Rtt disabled |
| 34 | * 1 = 60 ohm |
| 35 | * 2 = 120 ohm |
| 36 | * 3 = 40 ohm |
| 37 | * 4 = 20 ohm |
| 38 | * 5 = 30 ohm |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 39 | * |
| 40 | * FIXME: Apparently 8641 needs a value of 2 |
| 41 | * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572 |
| 42 | * |
| 43 | * FIXME: There was some effort down this line earlier: |
| 44 | * |
| 45 | * unsigned int i; |
| 46 | * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) { |
| 47 | * if (popts->dimmslot[i].num_valid_cs |
| 48 | * && (popts->cs_local_opts[2*i].odt_rd_cfg |
| 49 | * || popts->cs_local_opts[2*i].odt_wr_cfg)) { |
| 50 | * rtt = 2; |
| 51 | * break; |
| 52 | * } |
| 53 | * } |
| 54 | */ |
| 55 | static inline int fsl_ddr_get_rtt(void) |
| 56 | { |
| 57 | int rtt; |
| 58 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 59 | #if defined(CONFIG_SYS_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 60 | rtt = 0; |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 61 | #elif defined(CONFIG_SYS_FSL_DDR2) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 62 | rtt = 3; |
| 63 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 64 | rtt = 0; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | return rtt; |
| 68 | } |
| 69 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 71 | /* |
| 72 | * compute CAS write latency according to DDR4 spec |
| 73 | * CWL = 9 for <= 1600MT/s |
| 74 | * 10 for <= 1866MT/s |
| 75 | * 11 for <= 2133MT/s |
| 76 | * 12 for <= 2400MT/s |
| 77 | * 14 for <= 2667MT/s |
| 78 | * 16 for <= 2933MT/s |
| 79 | * 18 for higher |
| 80 | */ |
| 81 | static inline unsigned int compute_cas_write_latency(void) |
| 82 | { |
| 83 | unsigned int cwl; |
| 84 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 85 | if (mclk_ps >= 1250) |
| 86 | cwl = 9; |
| 87 | else if (mclk_ps >= 1070) |
| 88 | cwl = 10; |
| 89 | else if (mclk_ps >= 935) |
| 90 | cwl = 11; |
| 91 | else if (mclk_ps >= 833) |
| 92 | cwl = 12; |
| 93 | else if (mclk_ps >= 750) |
| 94 | cwl = 14; |
| 95 | else if (mclk_ps >= 681) |
| 96 | cwl = 16; |
| 97 | else |
| 98 | cwl = 18; |
| 99 | |
| 100 | return cwl; |
| 101 | } |
| 102 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 103 | /* |
| 104 | * compute the CAS write latency according to DDR3 spec |
| 105 | * CWL = 5 if tCK >= 2.5ns |
| 106 | * 6 if 2.5ns > tCK >= 1.875ns |
| 107 | * 7 if 1.875ns > tCK >= 1.5ns |
| 108 | * 8 if 1.5ns > tCK >= 1.25ns |
York Sun | 7a16d64 | 2011-08-24 09:40:25 -0700 | [diff] [blame] | 109 | * 9 if 1.25ns > tCK >= 1.07ns |
| 110 | * 10 if 1.07ns > tCK >= 0.935ns |
| 111 | * 11 if 0.935ns > tCK >= 0.833ns |
| 112 | * 12 if 0.833ns > tCK >= 0.75ns |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 113 | */ |
| 114 | static inline unsigned int compute_cas_write_latency(void) |
| 115 | { |
| 116 | unsigned int cwl; |
| 117 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 118 | |
| 119 | if (mclk_ps >= 2500) |
| 120 | cwl = 5; |
| 121 | else if (mclk_ps >= 1875) |
| 122 | cwl = 6; |
| 123 | else if (mclk_ps >= 1500) |
| 124 | cwl = 7; |
| 125 | else if (mclk_ps >= 1250) |
| 126 | cwl = 8; |
York Sun | 7a16d64 | 2011-08-24 09:40:25 -0700 | [diff] [blame] | 127 | else if (mclk_ps >= 1070) |
| 128 | cwl = 9; |
| 129 | else if (mclk_ps >= 935) |
| 130 | cwl = 10; |
| 131 | else if (mclk_ps >= 833) |
| 132 | cwl = 11; |
| 133 | else if (mclk_ps >= 750) |
| 134 | cwl = 12; |
| 135 | else { |
| 136 | cwl = 12; |
| 137 | printf("Warning: CWL is out of range\n"); |
| 138 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 139 | return cwl; |
| 140 | } |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 141 | #endif |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 142 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 143 | /* Chip Select Configuration (CSn_CONFIG) */ |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 144 | static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr, |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 145 | const memctl_options_t *popts, |
| 146 | const dimm_params_t *dimm_params) |
| 147 | { |
| 148 | unsigned int cs_n_en = 0; /* Chip Select enable */ |
| 149 | unsigned int intlv_en = 0; /* Memory controller interleave enable */ |
| 150 | unsigned int intlv_ctl = 0; /* Interleaving control */ |
| 151 | unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */ |
| 152 | unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */ |
| 153 | unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */ |
| 154 | unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */ |
| 155 | unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */ |
| 156 | unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */ |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 157 | int go_config = 0; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 158 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 159 | unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */ |
| 160 | #else |
| 161 | unsigned int n_banks_per_sdram_device; |
| 162 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 163 | |
| 164 | /* Compute CS_CONFIG only for existing ranks of each DIMM. */ |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 165 | switch (i) { |
| 166 | case 0: |
| 167 | if (dimm_params[dimm_number].n_ranks > 0) { |
| 168 | go_config = 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 169 | /* These fields only available in CS0_CONFIG */ |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 170 | if (!popts->memctl_interleaving) |
| 171 | break; |
| 172 | switch (popts->memctl_interleaving_mode) { |
York Sun | c459ae6 | 2014-02-10 13:59:44 -0800 | [diff] [blame] | 173 | case FSL_DDR_256B_INTERLEAVING: |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 174 | case FSL_DDR_CACHE_LINE_INTERLEAVING: |
| 175 | case FSL_DDR_PAGE_INTERLEAVING: |
| 176 | case FSL_DDR_BANK_INTERLEAVING: |
| 177 | case FSL_DDR_SUPERBANK_INTERLEAVING: |
| 178 | intlv_en = popts->memctl_interleaving; |
| 179 | intlv_ctl = popts->memctl_interleaving_mode; |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 184 | } |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 185 | break; |
| 186 | case 1: |
| 187 | if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \ |
| 188 | (dimm_number == 1 && dimm_params[1].n_ranks > 0)) |
| 189 | go_config = 1; |
| 190 | break; |
| 191 | case 2: |
| 192 | if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \ |
York Sun | 15f874a | 2011-08-26 11:32:40 -0700 | [diff] [blame] | 193 | (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0)) |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 194 | go_config = 1; |
| 195 | break; |
| 196 | case 3: |
| 197 | if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \ |
| 198 | (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \ |
| 199 | (dimm_number == 3 && dimm_params[3].n_ranks > 0)) |
| 200 | go_config = 1; |
| 201 | break; |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | if (go_config) { |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 206 | cs_n_en = 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 207 | ap_n_en = popts->cs_local_opts[i].auto_precharge; |
| 208 | odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg; |
| 209 | odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 210 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 211 | ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits; |
| 212 | bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits; |
| 213 | #else |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 214 | n_banks_per_sdram_device |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 215 | = dimm_params[dimm_number].n_banks_per_sdram_device; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 216 | ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 217 | #endif |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 218 | row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12; |
| 219 | col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 220 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 221 | ddr->cs[i].config = (0 |
| 222 | | ((cs_n_en & 0x1) << 31) |
| 223 | | ((intlv_en & 0x3) << 29) |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 224 | | ((intlv_ctl & 0xf) << 24) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 225 | | ((ap_n_en & 0x1) << 23) |
| 226 | |
| 227 | /* XXX: some implementation only have 1 bit starting at left */ |
| 228 | | ((odt_rd_cfg & 0x7) << 20) |
| 229 | |
| 230 | /* XXX: Some implementation only have 1 bit starting at left */ |
| 231 | | ((odt_wr_cfg & 0x7) << 16) |
| 232 | |
| 233 | | ((ba_bits_cs_n & 0x3) << 14) |
| 234 | | ((row_bits_cs_n & 0x7) << 8) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 235 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 236 | | ((bg_bits_cs_n & 0x3) << 4) |
| 237 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 238 | | ((col_bits_cs_n & 0x7) << 0) |
| 239 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 240 | debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Chip Select Configuration 2 (CSn_CONFIG_2) */ |
| 244 | /* FIXME: 8572 */ |
| 245 | static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr) |
| 246 | { |
| 247 | unsigned int pasr_cfg = 0; /* Partial array self refresh config */ |
| 248 | |
| 249 | ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 250 | debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */ |
| 254 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 255 | #if !defined(CONFIG_SYS_FSL_DDR1) |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 256 | static inline int avoid_odt_overlap(const dimm_params_t *dimm_params) |
| 257 | { |
| 258 | #if CONFIG_DIMM_SLOTS_PER_CTLR == 1 |
| 259 | if (dimm_params[0].n_ranks == 4) |
| 260 | return 1; |
| 261 | #endif |
| 262 | |
| 263 | #if CONFIG_DIMM_SLOTS_PER_CTLR == 2 |
| 264 | if ((dimm_params[0].n_ranks == 2) && |
| 265 | (dimm_params[1].n_ranks == 2)) |
| 266 | return 1; |
| 267 | |
| 268 | #ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE |
| 269 | if (dimm_params[0].n_ranks == 4) |
| 270 | return 1; |
| 271 | #endif |
| 272 | #endif |
| 273 | return 0; |
| 274 | } |
| 275 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 276 | /* |
| 277 | * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0) |
| 278 | * |
| 279 | * Avoid writing for DDR I. The new PQ38 DDR controller |
| 280 | * dreams up non-zero default values to be backwards compatible. |
| 281 | */ |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 282 | static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr, |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 283 | const memctl_options_t *popts, |
| 284 | const dimm_params_t *dimm_params) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 285 | { |
| 286 | unsigned char trwt_mclk = 0; /* Read-to-write turnaround */ |
| 287 | unsigned char twrt_mclk = 0; /* Write-to-read turnaround */ |
| 288 | /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */ |
| 289 | unsigned char trrt_mclk = 0; /* Read-to-read turnaround */ |
| 290 | unsigned char twwt_mclk = 0; /* Write-to-write turnaround */ |
| 291 | |
| 292 | /* Active powerdown exit timing (tXARD and tXARDS). */ |
| 293 | unsigned char act_pd_exit_mclk; |
| 294 | /* Precharge powerdown exit timing (tXP). */ |
| 295 | unsigned char pre_pd_exit_mclk; |
york | 1714e49 | 2010-07-02 22:25:56 +0000 | [diff] [blame] | 296 | /* ODT powerdown exit timing (tAXPD). */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 297 | unsigned char taxpd_mclk = 0; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 298 | /* Mode register set cycle time (tMRD). */ |
| 299 | unsigned char tmrd_mclk; |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 300 | #if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3) |
| 301 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 302 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 303 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 304 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 305 | /* tXP=max(4nCK, 6ns) */ |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 306 | int txp = max(mclk_ps * 4, 6000); /* unit=ps */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 307 | trwt_mclk = 2; |
| 308 | twrt_mclk = 1; |
| 309 | act_pd_exit_mclk = picos_to_mclk(txp); |
| 310 | pre_pd_exit_mclk = act_pd_exit_mclk; |
| 311 | /* |
| 312 | * MRS_CYC = max(tMRD, tMOD) |
| 313 | * tMRD = 8nCK, tMOD = max(24nCK, 15ns) |
| 314 | */ |
| 315 | tmrd_mclk = max(24, picos_to_mclk(15000)); |
| 316 | #elif defined(CONFIG_SYS_FSL_DDR3) |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 317 | unsigned int data_rate = get_ddr_freq(0); |
| 318 | int txp; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 319 | /* |
| 320 | * (tXARD and tXARDS). Empirical? |
| 321 | * The DDR3 spec has not tXARD, |
| 322 | * we use the tXP instead of it. |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 323 | * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066 |
| 324 | * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133 |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 325 | * spec has not the tAXPD, we use |
york | 1714e49 | 2010-07-02 22:25:56 +0000 | [diff] [blame] | 326 | * tAXPD=1, need design to confirm. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 327 | */ |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 328 | txp = max(mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000)); |
| 329 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 330 | tmrd_mclk = 4; |
Dave Liu | 8107926 | 2009-12-08 11:56:48 +0800 | [diff] [blame] | 331 | /* set the turnaround time */ |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * for single quad-rank DIMM and two dual-rank DIMMs |
| 335 | * to avoid ODT overlap |
| 336 | */ |
| 337 | if (avoid_odt_overlap(dimm_params)) { |
| 338 | twwt_mclk = 2; |
| 339 | trrt_mclk = 1; |
| 340 | } |
| 341 | /* for faster clock, need more time for data setup */ |
| 342 | trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1; |
| 343 | |
York Sun | 27f83be | 2011-02-10 10:13:10 -0800 | [diff] [blame] | 344 | if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving)) |
| 345 | twrt_mclk = 1; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 346 | |
| 347 | if (popts->dynamic_power == 0) { /* powerdown is not used */ |
| 348 | act_pd_exit_mclk = 1; |
| 349 | pre_pd_exit_mclk = 1; |
| 350 | taxpd_mclk = 1; |
| 351 | } else { |
| 352 | /* act_pd_exit_mclk = tXARD, see above */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 353 | act_pd_exit_mclk = picos_to_mclk(txp); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 354 | /* Mode register MR0[A12] is '1' - fast exit */ |
| 355 | pre_pd_exit_mclk = act_pd_exit_mclk; |
| 356 | taxpd_mclk = 1; |
| 357 | } |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 358 | #else /* CONFIG_SYS_FSL_DDR2 */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 359 | /* |
| 360 | * (tXARD and tXARDS). Empirical? |
| 361 | * tXARD = 2 for DDR2 |
| 362 | * tXP=2 |
| 363 | * tAXPD=8 |
| 364 | */ |
| 365 | act_pd_exit_mclk = 2; |
| 366 | pre_pd_exit_mclk = 2; |
| 367 | taxpd_mclk = 8; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 368 | tmrd_mclk = 2; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 369 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 370 | |
York Sun | f8691fc | 2011-05-27 13:44:28 +0800 | [diff] [blame] | 371 | if (popts->trwt_override) |
| 372 | trwt_mclk = popts->trwt; |
| 373 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 374 | ddr->timing_cfg_0 = (0 |
| 375 | | ((trwt_mclk & 0x3) << 30) /* RWT */ |
| 376 | | ((twrt_mclk & 0x3) << 28) /* WRT */ |
| 377 | | ((trrt_mclk & 0x3) << 26) /* RRT */ |
| 378 | | ((twwt_mclk & 0x3) << 24) /* WWT */ |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 379 | | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */ |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 380 | | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 381 | | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */ |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 382 | | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 383 | ); |
| 384 | debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0); |
| 385 | } |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 386 | #endif /* defined(CONFIG_SYS_FSL_DDR2) */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 387 | |
| 388 | /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */ |
| 389 | static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr, |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 390 | const memctl_options_t *popts, |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 391 | const common_timing_params_t *common_dimm, |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 392 | unsigned int cas_latency, |
| 393 | unsigned int additive_latency) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 394 | { |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 395 | /* Extended precharge to activate interval (tRP) */ |
| 396 | unsigned int ext_pretoact = 0; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 397 | /* Extended Activate to precharge interval (tRAS) */ |
| 398 | unsigned int ext_acttopre = 0; |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 399 | /* Extended activate to read/write interval (tRCD) */ |
| 400 | unsigned int ext_acttorw = 0; |
| 401 | /* Extended refresh recovery time (tRFC) */ |
| 402 | unsigned int ext_refrec; |
| 403 | /* Extended MCAS latency from READ cmd */ |
| 404 | unsigned int ext_caslat = 0; |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 405 | /* Extended additive latency */ |
| 406 | unsigned int ext_add_lat = 0; |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 407 | /* Extended last data to precharge interval (tWR) */ |
| 408 | unsigned int ext_wrrec = 0; |
| 409 | /* Control Adjust */ |
| 410 | unsigned int cntl_adj = 0; |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 411 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 412 | ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4; |
| 413 | ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4; |
| 414 | ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4; |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 415 | ext_caslat = (2 * cas_latency - 1) >> 4; |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 416 | ext_add_lat = additive_latency >> 4; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 417 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 418 | ext_refrec = (picos_to_mclk(common_dimm->trfc1_ps) - 8) >> 4; |
| 419 | #else |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 420 | ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4; |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 421 | /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 422 | #endif |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 423 | ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) + |
| 424 | (popts->otf_burst_chop_en ? 2 : 0)) >> 4; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 425 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 426 | ddr->timing_cfg_3 = (0 |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 427 | | ((ext_pretoact & 0x1) << 28) |
James Yang | 26681f5 | 2013-07-22 09:35:26 -0700 | [diff] [blame] | 428 | | ((ext_acttopre & 0x3) << 24) |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 429 | | ((ext_acttorw & 0x1) << 22) |
| 430 | | ((ext_refrec & 0x1F) << 16) |
| 431 | | ((ext_caslat & 0x3) << 12) |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 432 | | ((ext_add_lat & 0x1) << 10) |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 433 | | ((ext_wrrec & 0x1) << 8) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 434 | | ((cntl_adj & 0x7) << 0) |
| 435 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 436 | debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */ |
| 440 | static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr, |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 441 | const memctl_options_t *popts, |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 442 | const common_timing_params_t *common_dimm, |
| 443 | unsigned int cas_latency) |
| 444 | { |
| 445 | /* Precharge-to-activate interval (tRP) */ |
| 446 | unsigned char pretoact_mclk; |
| 447 | /* Activate to precharge interval (tRAS) */ |
| 448 | unsigned char acttopre_mclk; |
| 449 | /* Activate to read/write interval (tRCD) */ |
| 450 | unsigned char acttorw_mclk; |
| 451 | /* CASLAT */ |
| 452 | unsigned char caslat_ctrl; |
| 453 | /* Refresh recovery time (tRFC) ; trfc_low */ |
| 454 | unsigned char refrec_ctrl; |
| 455 | /* Last data to precharge minimum interval (tWR) */ |
| 456 | unsigned char wrrec_mclk; |
| 457 | /* Activate-to-activate interval (tRRD) */ |
| 458 | unsigned char acttoact_mclk; |
| 459 | /* Last write data pair to read command issue interval (tWTR) */ |
| 460 | unsigned char wrtord_mclk; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 461 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 462 | /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */ |
| 463 | static const u8 wrrec_table[] = { |
| 464 | 10, 10, 10, 10, 10, |
| 465 | 10, 10, 10, 10, 10, |
| 466 | 12, 12, 14, 14, 16, |
| 467 | 16, 18, 18, 20, 20, |
| 468 | 24, 24, 24, 24}; |
| 469 | #else |
York Sun | 3673f2c | 2011-03-02 14:24:11 -0800 | [diff] [blame] | 470 | /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */ |
| 471 | static const u8 wrrec_table[] = { |
| 472 | 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0}; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 473 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 474 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 475 | pretoact_mclk = picos_to_mclk(common_dimm->trp_ps); |
| 476 | acttopre_mclk = picos_to_mclk(common_dimm->tras_ps); |
| 477 | acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Translate CAS Latency to a DDR controller field value: |
| 481 | * |
| 482 | * CAS Lat DDR I DDR II Ctrl |
| 483 | * Clocks SPD Bit SPD Bit Value |
| 484 | * ------- ------- ------- ----- |
| 485 | * 1.0 0 0001 |
| 486 | * 1.5 1 0010 |
| 487 | * 2.0 2 2 0011 |
| 488 | * 2.5 3 0100 |
| 489 | * 3.0 4 3 0101 |
| 490 | * 3.5 5 0110 |
| 491 | * 4.0 4 0111 |
| 492 | * 4.5 1000 |
| 493 | * 5.0 5 1001 |
| 494 | */ |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 495 | #if defined(CONFIG_SYS_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 496 | caslat_ctrl = (cas_latency + 1) & 0x07; |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 497 | #elif defined(CONFIG_SYS_FSL_DDR2) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 498 | caslat_ctrl = 2 * cas_latency - 1; |
| 499 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 500 | /* |
| 501 | * if the CAS latency more than 8 cycle, |
| 502 | * we need set extend bit for it at |
| 503 | * TIMING_CFG_3[EXT_CASLAT] |
| 504 | */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 505 | if (fsl_ddr_get_version() <= 0x40400) |
| 506 | caslat_ctrl = 2 * cas_latency - 1; |
| 507 | else |
| 508 | caslat_ctrl = (cas_latency - 1) << 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 509 | #endif |
| 510 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 511 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 512 | refrec_ctrl = picos_to_mclk(common_dimm->trfc1_ps) - 8; |
| 513 | wrrec_mclk = picos_to_mclk(common_dimm->twr_ps); |
| 514 | acttoact_mclk = max(picos_to_mclk(common_dimm->trrds_ps), 4); |
| 515 | wrtord_mclk = max(2, picos_to_mclk(2500)); |
York Sun | edbeee1 | 2014-04-01 14:20:49 -0700 | [diff] [blame] | 516 | if ((wrrec_mclk < 1) || (wrrec_mclk > 24)) |
| 517 | printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 518 | else |
| 519 | wrrec_mclk = wrrec_table[wrrec_mclk - 1]; |
| 520 | #else |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 521 | refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8; |
| 522 | wrrec_mclk = picos_to_mclk(common_dimm->twr_ps); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 523 | acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps); |
| 524 | wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps); |
York Sun | edbeee1 | 2014-04-01 14:20:49 -0700 | [diff] [blame] | 525 | if ((wrrec_mclk < 1) || (wrrec_mclk > 16)) |
| 526 | printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk); |
York Sun | cd077cf | 2012-08-17 08:22:40 +0000 | [diff] [blame] | 527 | else |
| 528 | wrrec_mclk = wrrec_table[wrrec_mclk - 1]; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 529 | #endif |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 530 | if (popts->otf_burst_chop_en) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 531 | wrrec_mclk += 2; |
| 532 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 533 | /* |
| 534 | * JEDEC has min requirement for tRRD |
| 535 | */ |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 536 | #if defined(CONFIG_SYS_FSL_DDR3) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 537 | if (acttoact_mclk < 4) |
| 538 | acttoact_mclk = 4; |
| 539 | #endif |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 540 | /* |
| 541 | * JEDEC has some min requirements for tWTR |
| 542 | */ |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 543 | #if defined(CONFIG_SYS_FSL_DDR2) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 544 | if (wrtord_mclk < 2) |
| 545 | wrtord_mclk = 2; |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 546 | #elif defined(CONFIG_SYS_FSL_DDR3) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 547 | if (wrtord_mclk < 4) |
| 548 | wrtord_mclk = 4; |
| 549 | #endif |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 550 | if (popts->otf_burst_chop_en) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 551 | wrtord_mclk += 2; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 552 | |
| 553 | ddr->timing_cfg_1 = (0 |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 554 | | ((pretoact_mclk & 0x0F) << 28) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 555 | | ((acttopre_mclk & 0x0F) << 24) |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 556 | | ((acttorw_mclk & 0xF) << 20) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 557 | | ((caslat_ctrl & 0xF) << 16) |
| 558 | | ((refrec_ctrl & 0xF) << 12) |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 559 | | ((wrrec_mclk & 0x0F) << 8) |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 560 | | ((acttoact_mclk & 0x0F) << 4) |
| 561 | | ((wrtord_mclk & 0x0F) << 0) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 562 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 563 | debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */ |
| 567 | static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr, |
| 568 | const memctl_options_t *popts, |
| 569 | const common_timing_params_t *common_dimm, |
| 570 | unsigned int cas_latency, |
| 571 | unsigned int additive_latency) |
| 572 | { |
| 573 | /* Additive latency */ |
| 574 | unsigned char add_lat_mclk; |
| 575 | /* CAS-to-preamble override */ |
| 576 | unsigned short cpo; |
| 577 | /* Write latency */ |
| 578 | unsigned char wr_lat; |
| 579 | /* Read to precharge (tRTP) */ |
| 580 | unsigned char rd_to_pre; |
| 581 | /* Write command to write data strobe timing adjustment */ |
| 582 | unsigned char wr_data_delay; |
| 583 | /* Minimum CKE pulse width (tCKE) */ |
| 584 | unsigned char cke_pls; |
| 585 | /* Window for four activates (tFAW) */ |
| 586 | unsigned short four_act; |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 587 | #ifdef CONFIG_SYS_FSL_DDR3 |
| 588 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 589 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 590 | |
| 591 | /* FIXME add check that this must be less than acttorw_mclk */ |
| 592 | add_lat_mclk = additive_latency; |
| 593 | cpo = popts->cpo_override; |
| 594 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 595 | #if defined(CONFIG_SYS_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 596 | /* |
| 597 | * This is a lie. It should really be 1, but if it is |
| 598 | * set to 1, bits overlap into the old controller's |
| 599 | * otherwise unused ACSM field. If we leave it 0, then |
| 600 | * the HW will magically treat it as 1 for DDR 1. Oh Yea. |
| 601 | */ |
| 602 | wr_lat = 0; |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 603 | #elif defined(CONFIG_SYS_FSL_DDR2) |
Dave Liu | 82aa953 | 2009-03-14 12:48:19 +0800 | [diff] [blame] | 604 | wr_lat = cas_latency - 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 605 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 606 | wr_lat = compute_cas_write_latency(); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 607 | #endif |
| 608 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 609 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 610 | rd_to_pre = picos_to_mclk(7500); |
| 611 | #else |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 612 | rd_to_pre = picos_to_mclk(common_dimm->trtp_ps); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 613 | #endif |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 614 | /* |
| 615 | * JEDEC has some min requirements for tRTP |
| 616 | */ |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 617 | #if defined(CONFIG_SYS_FSL_DDR2) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 618 | if (rd_to_pre < 2) |
| 619 | rd_to_pre = 2; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 620 | #elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 621 | if (rd_to_pre < 4) |
| 622 | rd_to_pre = 4; |
Dave Liu | 82aa953 | 2009-03-14 12:48:19 +0800 | [diff] [blame] | 623 | #endif |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 624 | if (popts->otf_burst_chop_en) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 625 | rd_to_pre += 2; /* according to UM */ |
| 626 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 627 | wr_data_delay = popts->write_data_delay; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 628 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 629 | cpo = 0; |
| 630 | cke_pls = max(3, picos_to_mclk(5000)); |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 631 | #elif defined(CONFIG_SYS_FSL_DDR3) |
| 632 | /* |
| 633 | * cke pulse = max(3nCK, 7.5ns) for DDR3-800 |
| 634 | * max(3nCK, 5.625ns) for DDR3-1066, 1333 |
| 635 | * max(3nCK, 5ns) for DDR3-1600, 1866, 2133 |
| 636 | */ |
| 637 | cke_pls = max(3, picos_to_mclk(mclk_ps > 1870 ? 7500 : |
| 638 | (mclk_ps > 1245 ? 5625 : 5000))); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 639 | #else |
York Sun | c1bf24f | 2014-08-21 16:13:22 -0700 | [diff] [blame] | 640 | cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 641 | #endif |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 642 | four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 643 | |
| 644 | ddr->timing_cfg_2 = (0 |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 645 | | ((add_lat_mclk & 0xf) << 28) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 646 | | ((cpo & 0x1f) << 23) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 647 | | ((wr_lat & 0xf) << 19) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 648 | | ((wr_lat & 0x10) << 14) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 649 | | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT) |
| 650 | | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 651 | | ((cke_pls & 0x7) << 6) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 652 | | ((four_act & 0x3f) << 0) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 653 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 654 | debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 655 | } |
| 656 | |
york | de87932 | 2010-07-02 22:25:55 +0000 | [diff] [blame] | 657 | /* DDR SDRAM Register Control Word */ |
| 658 | static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 659 | const memctl_options_t *popts, |
york | de87932 | 2010-07-02 22:25:55 +0000 | [diff] [blame] | 660 | const common_timing_params_t *common_dimm) |
| 661 | { |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 662 | if (common_dimm->all_dimms_registered && |
| 663 | !common_dimm->all_dimms_unbuffered) { |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 664 | if (popts->rcw_override) { |
| 665 | ddr->ddr_sdram_rcw_1 = popts->rcw_1; |
| 666 | ddr->ddr_sdram_rcw_2 = popts->rcw_2; |
| 667 | } else { |
| 668 | ddr->ddr_sdram_rcw_1 = |
| 669 | common_dimm->rcw[0] << 28 | \ |
| 670 | common_dimm->rcw[1] << 24 | \ |
| 671 | common_dimm->rcw[2] << 20 | \ |
| 672 | common_dimm->rcw[3] << 16 | \ |
| 673 | common_dimm->rcw[4] << 12 | \ |
| 674 | common_dimm->rcw[5] << 8 | \ |
| 675 | common_dimm->rcw[6] << 4 | \ |
| 676 | common_dimm->rcw[7]; |
| 677 | ddr->ddr_sdram_rcw_2 = |
| 678 | common_dimm->rcw[8] << 28 | \ |
| 679 | common_dimm->rcw[9] << 24 | \ |
| 680 | common_dimm->rcw[10] << 20 | \ |
| 681 | common_dimm->rcw[11] << 16 | \ |
| 682 | common_dimm->rcw[12] << 12 | \ |
| 683 | common_dimm->rcw[13] << 8 | \ |
| 684 | common_dimm->rcw[14] << 4 | \ |
| 685 | common_dimm->rcw[15]; |
| 686 | } |
york | de87932 | 2010-07-02 22:25:55 +0000 | [diff] [blame] | 687 | debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1); |
| 688 | debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2); |
| 689 | } |
| 690 | } |
| 691 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 692 | /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */ |
| 693 | static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr, |
| 694 | const memctl_options_t *popts, |
| 695 | const common_timing_params_t *common_dimm) |
| 696 | { |
| 697 | unsigned int mem_en; /* DDR SDRAM interface logic enable */ |
| 698 | unsigned int sren; /* Self refresh enable (during sleep) */ |
| 699 | unsigned int ecc_en; /* ECC enable. */ |
| 700 | unsigned int rd_en; /* Registered DIMM enable */ |
| 701 | unsigned int sdram_type; /* Type of SDRAM */ |
| 702 | unsigned int dyn_pwr; /* Dynamic power management mode */ |
| 703 | unsigned int dbw; /* DRAM dta bus width */ |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 704 | unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 705 | unsigned int ncap = 0; /* Non-concurrent auto-precharge */ |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 706 | unsigned int threet_en; /* Enable 3T timing */ |
| 707 | unsigned int twot_en; /* Enable 2T timing */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 708 | unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */ |
| 709 | unsigned int x32_en = 0; /* x32 enable */ |
| 710 | unsigned int pchb8 = 0; /* precharge bit 8 enable */ |
| 711 | unsigned int hse; /* Global half strength override */ |
York Sun | 5d6c626 | 2014-09-05 13:52:41 +0800 | [diff] [blame] | 712 | unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 713 | unsigned int mem_halt = 0; /* memory controller halt */ |
| 714 | unsigned int bi = 0; /* Bypass initialization */ |
| 715 | |
| 716 | mem_en = 1; |
| 717 | sren = popts->self_refresh_in_sleep; |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 718 | if (common_dimm->all_dimms_ecc_capable) { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 719 | /* Allow setting of ECC only if all DIMMs are ECC. */ |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 720 | ecc_en = popts->ecc_mode; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 721 | } else { |
| 722 | ecc_en = 0; |
| 723 | } |
| 724 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 725 | if (common_dimm->all_dimms_registered && |
| 726 | !common_dimm->all_dimms_unbuffered) { |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 727 | rd_en = 1; |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 728 | twot_en = 0; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 729 | } else { |
| 730 | rd_en = 0; |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 731 | twot_en = popts->twot_en; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 732 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 733 | |
| 734 | sdram_type = CONFIG_FSL_SDRAM_TYPE; |
| 735 | |
| 736 | dyn_pwr = popts->dynamic_power; |
| 737 | dbw = popts->data_bus_width; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 738 | /* 8-beat burst enable DDR-III case |
| 739 | * we must clear it when use the on-the-fly mode, |
| 740 | * must set it when use the 32-bits bus mode. |
| 741 | */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 742 | if ((sdram_type == SDRAM_TYPE_DDR3) || |
| 743 | (sdram_type == SDRAM_TYPE_DDR4)) { |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 744 | if (popts->burst_length == DDR_BL8) |
| 745 | eight_be = 1; |
| 746 | if (popts->burst_length == DDR_OTF) |
| 747 | eight_be = 0; |
| 748 | if (dbw == 0x1) |
| 749 | eight_be = 1; |
| 750 | } |
| 751 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 752 | threet_en = popts->threet_en; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 753 | ba_intlv_ctl = popts->ba_intlv_ctl; |
| 754 | hse = popts->half_strength_driver_enable; |
| 755 | |
York Sun | 5d6c626 | 2014-09-05 13:52:41 +0800 | [diff] [blame] | 756 | /* set when ddr bus width < 64 */ |
| 757 | acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0; |
| 758 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 759 | ddr->ddr_sdram_cfg = (0 |
| 760 | | ((mem_en & 0x1) << 31) |
| 761 | | ((sren & 0x1) << 30) |
| 762 | | ((ecc_en & 0x1) << 29) |
| 763 | | ((rd_en & 0x1) << 28) |
| 764 | | ((sdram_type & 0x7) << 24) |
| 765 | | ((dyn_pwr & 0x1) << 21) |
| 766 | | ((dbw & 0x3) << 19) |
| 767 | | ((eight_be & 0x1) << 18) |
| 768 | | ((ncap & 0x1) << 17) |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 769 | | ((threet_en & 0x1) << 16) |
| 770 | | ((twot_en & 0x1) << 15) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 771 | | ((ba_intlv_ctl & 0x7F) << 8) |
| 772 | | ((x32_en & 0x1) << 5) |
| 773 | | ((pchb8 & 0x1) << 4) |
| 774 | | ((hse & 0x1) << 3) |
York Sun | 5d6c626 | 2014-09-05 13:52:41 +0800 | [diff] [blame] | 775 | | ((acc_ecc_en & 0x1) << 2) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 776 | | ((mem_halt & 0x1) << 1) |
| 777 | | ((bi & 0x1) << 0) |
| 778 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 779 | debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */ |
| 783 | static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 784 | const memctl_options_t *popts, |
| 785 | const unsigned int unq_mrs_en) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 786 | { |
| 787 | unsigned int frc_sr = 0; /* Force self refresh */ |
| 788 | unsigned int sr_ie = 0; /* Self-refresh interrupt enable */ |
York Sun | 15f874a | 2011-08-26 11:32:40 -0700 | [diff] [blame] | 789 | unsigned int odt_cfg = 0; /* ODT configuration */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 790 | unsigned int num_pr; /* Number of posted refreshes */ |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 791 | unsigned int slow = 0; /* DDR will be run less than 1250 */ |
York Sun | 4889c98 | 2013-06-25 11:37:47 -0700 | [diff] [blame] | 792 | unsigned int x4_en = 0; /* x4 DRAM enable */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 793 | unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */ |
| 794 | unsigned int ap_en; /* Address Parity Enable */ |
| 795 | unsigned int d_init; /* DRAM data initialization */ |
| 796 | unsigned int rcw_en = 0; /* Register Control Word Enable */ |
| 797 | unsigned int md_en = 0; /* Mirrored DIMM Enable */ |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 798 | unsigned int qd_en = 0; /* quad-rank DIMM Enable */ |
York Sun | 15f874a | 2011-08-26 11:32:40 -0700 | [diff] [blame] | 799 | int i; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 800 | #ifndef CONFIG_SYS_FSL_DDR4 |
| 801 | unsigned int dll_rst_dis = 1; /* DLL reset disable */ |
| 802 | unsigned int dqs_cfg; /* DQS configuration */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 803 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 804 | dqs_cfg = popts->dqs_config; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 805 | #endif |
York Sun | 15f874a | 2011-08-26 11:32:40 -0700 | [diff] [blame] | 806 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 807 | if (popts->cs_local_opts[i].odt_rd_cfg |
| 808 | || popts->cs_local_opts[i].odt_wr_cfg) { |
| 809 | odt_cfg = SDRAM_CFG2_ODT_ONLY_READ; |
| 810 | break; |
| 811 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | num_pr = 1; /* Make this configurable */ |
| 815 | |
| 816 | /* |
| 817 | * 8572 manual says |
| 818 | * {TIMING_CFG_1[PRETOACT] |
| 819 | * + [DDR_SDRAM_CFG_2[NUM_PR] |
| 820 | * * ({EXT_REFREC || REFREC} + 8 + 2)]} |
| 821 | * << DDR_SDRAM_INTERVAL[REFINT] |
| 822 | */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 823 | #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4) |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 824 | obc_cfg = popts->otf_burst_chop_en; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 825 | #else |
| 826 | obc_cfg = 0; |
| 827 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 828 | |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 829 | #if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7) |
| 830 | slow = get_ddr_freq(0) < 1249000000; |
| 831 | #endif |
| 832 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 833 | if (popts->registered_dimm_en) { |
| 834 | rcw_en = 1; |
| 835 | ap_en = popts->ap_en; |
| 836 | } else { |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 837 | ap_en = 0; |
| 838 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 839 | |
York Sun | 4889c98 | 2013-06-25 11:37:47 -0700 | [diff] [blame] | 840 | x4_en = popts->x4_en ? 1 : 0; |
| 841 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 842 | #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER) |
| 843 | /* Use the DDR controller to auto initialize memory. */ |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 844 | d_init = popts->ecc_init_using_memctl; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 845 | ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE; |
| 846 | debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init); |
| 847 | #else |
| 848 | /* Memory will be initialized via DMA, or not at all. */ |
| 849 | d_init = 0; |
| 850 | #endif |
| 851 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 852 | #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 853 | md_en = popts->mirrored_dimm; |
| 854 | #endif |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 855 | qd_en = popts->quad_rank_present ? 1 : 0; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 856 | ddr->ddr_sdram_cfg_2 = (0 |
| 857 | | ((frc_sr & 0x1) << 31) |
| 858 | | ((sr_ie & 0x1) << 30) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 859 | #ifndef CONFIG_SYS_FSL_DDR4 |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 860 | | ((dll_rst_dis & 0x1) << 29) |
| 861 | | ((dqs_cfg & 0x3) << 26) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 862 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 863 | | ((odt_cfg & 0x3) << 21) |
| 864 | | ((num_pr & 0xf) << 12) |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 865 | | ((slow & 1) << 11) |
York Sun | 4889c98 | 2013-06-25 11:37:47 -0700 | [diff] [blame] | 866 | | (x4_en << 10) |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 867 | | (qd_en << 9) |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 868 | | (unq_mrs_en << 8) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 869 | | ((obc_cfg & 0x1) << 6) |
| 870 | | ((ap_en & 0x1) << 5) |
| 871 | | ((d_init & 0x1) << 4) |
| 872 | | ((rcw_en & 0x1) << 2) |
| 873 | | ((md_en & 0x1) << 0) |
| 874 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 875 | debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 876 | } |
| 877 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 878 | #ifdef CONFIG_SYS_FSL_DDR4 |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 879 | /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */ |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 880 | static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 881 | const memctl_options_t *popts, |
Valentin Longchamp | 0b81093 | 2013-10-18 11:47:20 +0200 | [diff] [blame] | 882 | const common_timing_params_t *common_dimm, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 883 | const unsigned int unq_mrs_en) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 884 | { |
| 885 | unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */ |
| 886 | unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 887 | int i; |
| 888 | unsigned int wr_crc = 0; /* Disable */ |
| 889 | unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */ |
| 890 | unsigned int srt = 0; /* self-refresh temerature, normal range */ |
| 891 | unsigned int cwl = compute_cas_write_latency() - 9; |
| 892 | unsigned int mpr = 0; /* serial */ |
| 893 | unsigned int wc_lat; |
| 894 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 895 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 896 | if (popts->rtt_override) |
| 897 | rtt_wr = popts->rtt_wr_override_value; |
| 898 | else |
| 899 | rtt_wr = popts->cs_local_opts[0].odt_rtt_wr; |
| 900 | |
| 901 | if (common_dimm->extended_op_srt) |
| 902 | srt = common_dimm->extended_op_srt; |
| 903 | |
| 904 | esdmode2 = (0 |
| 905 | | ((wr_crc & 0x1) << 12) |
| 906 | | ((rtt_wr & 0x3) << 9) |
| 907 | | ((srt & 0x3) << 6) |
| 908 | | ((cwl & 0x7) << 3)); |
| 909 | |
| 910 | if (mclk_ps >= 1250) |
| 911 | wc_lat = 0; |
| 912 | else if (mclk_ps >= 833) |
| 913 | wc_lat = 1; |
| 914 | else |
| 915 | wc_lat = 2; |
| 916 | |
| 917 | esdmode3 = (0 |
| 918 | | ((mpr & 0x3) << 11) |
| 919 | | ((wc_lat & 0x3) << 9)); |
| 920 | |
| 921 | ddr->ddr_sdram_mode_2 = (0 |
| 922 | | ((esdmode2 & 0xFFFF) << 16) |
| 923 | | ((esdmode3 & 0xFFFF) << 0) |
| 924 | ); |
| 925 | debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2); |
| 926 | |
| 927 | if (unq_mrs_en) { /* unique mode registers are supported */ |
| 928 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 929 | if (popts->rtt_override) |
| 930 | rtt_wr = popts->rtt_wr_override_value; |
| 931 | else |
| 932 | rtt_wr = popts->cs_local_opts[i].odt_rtt_wr; |
| 933 | |
| 934 | esdmode2 &= 0xF9FF; /* clear bit 10, 9 */ |
| 935 | esdmode2 |= (rtt_wr & 0x3) << 9; |
| 936 | switch (i) { |
| 937 | case 1: |
| 938 | ddr->ddr_sdram_mode_4 = (0 |
| 939 | | ((esdmode2 & 0xFFFF) << 16) |
| 940 | | ((esdmode3 & 0xFFFF) << 0) |
| 941 | ); |
| 942 | break; |
| 943 | case 2: |
| 944 | ddr->ddr_sdram_mode_6 = (0 |
| 945 | | ((esdmode2 & 0xFFFF) << 16) |
| 946 | | ((esdmode3 & 0xFFFF) << 0) |
| 947 | ); |
| 948 | break; |
| 949 | case 3: |
| 950 | ddr->ddr_sdram_mode_8 = (0 |
| 951 | | ((esdmode2 & 0xFFFF) << 16) |
| 952 | | ((esdmode3 & 0xFFFF) << 0) |
| 953 | ); |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n", |
| 958 | ddr->ddr_sdram_mode_4); |
| 959 | debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n", |
| 960 | ddr->ddr_sdram_mode_6); |
| 961 | debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n", |
| 962 | ddr->ddr_sdram_mode_8); |
| 963 | } |
| 964 | } |
| 965 | #elif defined(CONFIG_SYS_FSL_DDR3) |
| 966 | /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */ |
| 967 | static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr, |
| 968 | const memctl_options_t *popts, |
| 969 | const common_timing_params_t *common_dimm, |
| 970 | const unsigned int unq_mrs_en) |
| 971 | { |
| 972 | unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */ |
| 973 | unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */ |
Kumar Gala | 65b5be2 | 2011-01-20 01:53:15 -0600 | [diff] [blame] | 974 | int i; |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 975 | unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 976 | unsigned int srt = 0; /* self-refresh temerature, normal range */ |
| 977 | unsigned int asr = 0; /* auto self-refresh disable */ |
| 978 | unsigned int cwl = compute_cas_write_latency() - 5; |
| 979 | unsigned int pasr = 0; /* partial array self refresh disable */ |
| 980 | |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 981 | if (popts->rtt_override) |
| 982 | rtt_wr = popts->rtt_wr_override_value; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 983 | else |
| 984 | rtt_wr = popts->cs_local_opts[0].odt_rtt_wr; |
Valentin Longchamp | 0b81093 | 2013-10-18 11:47:20 +0200 | [diff] [blame] | 985 | |
| 986 | if (common_dimm->extended_op_srt) |
| 987 | srt = common_dimm->extended_op_srt; |
| 988 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 989 | esdmode2 = (0 |
| 990 | | ((rtt_wr & 0x3) << 9) |
| 991 | | ((srt & 0x1) << 7) |
| 992 | | ((asr & 0x1) << 6) |
| 993 | | ((cwl & 0x7) << 3) |
| 994 | | ((pasr & 0x7) << 0)); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 995 | ddr->ddr_sdram_mode_2 = (0 |
| 996 | | ((esdmode2 & 0xFFFF) << 16) |
| 997 | | ((esdmode3 & 0xFFFF) << 0) |
| 998 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 999 | debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1000 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1001 | if (unq_mrs_en) { /* unique mode registers are supported */ |
Kumar Gala | d5bbe66 | 2011-11-09 10:05:10 -0600 | [diff] [blame] | 1002 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1003 | if (popts->rtt_override) |
| 1004 | rtt_wr = popts->rtt_wr_override_value; |
| 1005 | else |
| 1006 | rtt_wr = popts->cs_local_opts[i].odt_rtt_wr; |
| 1007 | |
| 1008 | esdmode2 &= 0xF9FF; /* clear bit 10, 9 */ |
| 1009 | esdmode2 |= (rtt_wr & 0x3) << 9; |
| 1010 | switch (i) { |
| 1011 | case 1: |
| 1012 | ddr->ddr_sdram_mode_4 = (0 |
| 1013 | | ((esdmode2 & 0xFFFF) << 16) |
| 1014 | | ((esdmode3 & 0xFFFF) << 0) |
| 1015 | ); |
| 1016 | break; |
| 1017 | case 2: |
| 1018 | ddr->ddr_sdram_mode_6 = (0 |
| 1019 | | ((esdmode2 & 0xFFFF) << 16) |
| 1020 | | ((esdmode3 & 0xFFFF) << 0) |
| 1021 | ); |
| 1022 | break; |
| 1023 | case 3: |
| 1024 | ddr->ddr_sdram_mode_8 = (0 |
| 1025 | | ((esdmode2 & 0xFFFF) << 16) |
| 1026 | | ((esdmode3 & 0xFFFF) << 0) |
| 1027 | ); |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
| 1031 | debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n", |
| 1032 | ddr->ddr_sdram_mode_4); |
| 1033 | debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n", |
| 1034 | ddr->ddr_sdram_mode_6); |
| 1035 | debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n", |
| 1036 | ddr->ddr_sdram_mode_8); |
| 1037 | } |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | #else /* for DDR2 and DDR1 */ |
| 1041 | /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */ |
| 1042 | static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr, |
| 1043 | const memctl_options_t *popts, |
| 1044 | const common_timing_params_t *common_dimm, |
| 1045 | const unsigned int unq_mrs_en) |
| 1046 | { |
| 1047 | unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */ |
| 1048 | unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */ |
| 1049 | |
| 1050 | ddr->ddr_sdram_mode_2 = (0 |
| 1051 | | ((esdmode2 & 0xFFFF) << 16) |
| 1052 | | ((esdmode3 & 0xFFFF) << 0) |
| 1053 | ); |
| 1054 | debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2); |
| 1055 | } |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1056 | #endif |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1057 | |
| 1058 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1059 | /* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */ |
| 1060 | static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr, |
| 1061 | const memctl_options_t *popts, |
| 1062 | const common_timing_params_t *common_dimm, |
| 1063 | const unsigned int unq_mrs_en) |
| 1064 | { |
| 1065 | int i; |
| 1066 | unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */ |
| 1067 | unsigned short esdmode5; /* Extended SDRAM mode 5 */ |
| 1068 | |
| 1069 | esdmode5 = 0x00000400; /* Data mask enabled */ |
| 1070 | |
| 1071 | ddr->ddr_sdram_mode_9 = (0 |
| 1072 | | ((esdmode4 & 0xffff) << 16) |
| 1073 | | ((esdmode5 & 0xffff) << 0) |
| 1074 | ); |
| 1075 | debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9); |
| 1076 | if (unq_mrs_en) { /* unique mode registers are supported */ |
| 1077 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 1078 | switch (i) { |
| 1079 | case 1: |
| 1080 | ddr->ddr_sdram_mode_11 = (0 |
| 1081 | | ((esdmode4 & 0xFFFF) << 16) |
| 1082 | | ((esdmode5 & 0xFFFF) << 0) |
| 1083 | ); |
| 1084 | break; |
| 1085 | case 2: |
| 1086 | ddr->ddr_sdram_mode_13 = (0 |
| 1087 | | ((esdmode4 & 0xFFFF) << 16) |
| 1088 | | ((esdmode5 & 0xFFFF) << 0) |
| 1089 | ); |
| 1090 | break; |
| 1091 | case 3: |
| 1092 | ddr->ddr_sdram_mode_15 = (0 |
| 1093 | | ((esdmode4 & 0xFFFF) << 16) |
| 1094 | | ((esdmode5 & 0xFFFF) << 0) |
| 1095 | ); |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n", |
| 1100 | ddr->ddr_sdram_mode_11); |
| 1101 | debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n", |
| 1102 | ddr->ddr_sdram_mode_13); |
| 1103 | debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n", |
| 1104 | ddr->ddr_sdram_mode_15); |
| 1105 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1106 | } |
| 1107 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1108 | /* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */ |
| 1109 | static void set_ddr_sdram_mode_10(fsl_ddr_cfg_regs_t *ddr, |
| 1110 | const memctl_options_t *popts, |
| 1111 | const common_timing_params_t *common_dimm, |
| 1112 | const unsigned int unq_mrs_en) |
| 1113 | { |
| 1114 | int i; |
| 1115 | unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */ |
| 1116 | unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */ |
| 1117 | unsigned int tccdl_min = picos_to_mclk(common_dimm->tccdl_ps); |
| 1118 | |
| 1119 | esdmode6 = ((tccdl_min - 4) & 0x7) << 10; |
| 1120 | |
| 1121 | ddr->ddr_sdram_mode_10 = (0 |
| 1122 | | ((esdmode6 & 0xffff) << 16) |
| 1123 | | ((esdmode7 & 0xffff) << 0) |
| 1124 | ); |
| 1125 | debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10); |
| 1126 | if (unq_mrs_en) { /* unique mode registers are supported */ |
| 1127 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 1128 | switch (i) { |
| 1129 | case 1: |
| 1130 | ddr->ddr_sdram_mode_12 = (0 |
| 1131 | | ((esdmode6 & 0xFFFF) << 16) |
| 1132 | | ((esdmode7 & 0xFFFF) << 0) |
| 1133 | ); |
| 1134 | break; |
| 1135 | case 2: |
| 1136 | ddr->ddr_sdram_mode_14 = (0 |
| 1137 | | ((esdmode6 & 0xFFFF) << 16) |
| 1138 | | ((esdmode7 & 0xFFFF) << 0) |
| 1139 | ); |
| 1140 | break; |
| 1141 | case 3: |
| 1142 | ddr->ddr_sdram_mode_16 = (0 |
| 1143 | | ((esdmode6 & 0xFFFF) << 16) |
| 1144 | | ((esdmode7 & 0xFFFF) << 0) |
| 1145 | ); |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n", |
| 1150 | ddr->ddr_sdram_mode_12); |
| 1151 | debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n", |
| 1152 | ddr->ddr_sdram_mode_14); |
| 1153 | debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n", |
| 1154 | ddr->ddr_sdram_mode_16); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | #endif |
| 1159 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1160 | /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */ |
| 1161 | static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr, |
| 1162 | const memctl_options_t *popts, |
| 1163 | const common_timing_params_t *common_dimm) |
| 1164 | { |
| 1165 | unsigned int refint; /* Refresh interval */ |
| 1166 | unsigned int bstopre; /* Precharge interval */ |
| 1167 | |
| 1168 | refint = picos_to_mclk(common_dimm->refresh_rate_ps); |
| 1169 | |
| 1170 | bstopre = popts->bstopre; |
| 1171 | |
| 1172 | /* refint field used 0x3FFF in earlier controllers */ |
| 1173 | ddr->ddr_sdram_interval = (0 |
| 1174 | | ((refint & 0xFFFF) << 16) |
| 1175 | | ((bstopre & 0x3FFF) << 0) |
| 1176 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 1177 | debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1180 | #ifdef CONFIG_SYS_FSL_DDR4 |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1181 | /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */ |
| 1182 | static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr, |
| 1183 | const memctl_options_t *popts, |
| 1184 | const common_timing_params_t *common_dimm, |
| 1185 | unsigned int cas_latency, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1186 | unsigned int additive_latency, |
| 1187 | const unsigned int unq_mrs_en) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1188 | { |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1189 | int i; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1190 | unsigned short esdmode; /* Extended SDRAM mode */ |
| 1191 | unsigned short sdmode; /* SDRAM mode */ |
| 1192 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1193 | /* Mode Register - MR1 */ |
| 1194 | unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */ |
| 1195 | unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */ |
| 1196 | unsigned int rtt; |
| 1197 | unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */ |
| 1198 | unsigned int al = 0; /* Posted CAS# additive latency (AL) */ |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1199 | unsigned int dic = 0; /* Output driver impedance, 40ohm */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1200 | unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal), |
| 1201 | 0=Disable (Test/Debug) */ |
| 1202 | |
| 1203 | /* Mode Register - MR0 */ |
| 1204 | unsigned int wr = 0; /* Write Recovery */ |
| 1205 | unsigned int dll_rst; /* DLL Reset */ |
| 1206 | unsigned int mode; /* Normal=0 or Test=1 */ |
| 1207 | unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */ |
| 1208 | /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */ |
| 1209 | unsigned int bt; |
| 1210 | unsigned int bl; /* BL: Burst Length */ |
| 1211 | |
| 1212 | unsigned int wr_mclk; |
| 1213 | /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */ |
| 1214 | static const u8 wr_table[] = { |
| 1215 | 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6}; |
| 1216 | /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */ |
| 1217 | static const u8 cas_latency_table[] = { |
| 1218 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, |
| 1219 | 9, 9, 10, 10, 11, 11}; |
| 1220 | |
| 1221 | if (popts->rtt_override) |
| 1222 | rtt = popts->rtt_override_value; |
| 1223 | else |
| 1224 | rtt = popts->cs_local_opts[0].odt_rtt_norm; |
| 1225 | |
| 1226 | if (additive_latency == (cas_latency - 1)) |
| 1227 | al = 1; |
| 1228 | if (additive_latency == (cas_latency - 2)) |
| 1229 | al = 2; |
| 1230 | |
| 1231 | if (popts->quad_rank_present) |
| 1232 | dic = 1; /* output driver impedance 240/7 ohm */ |
| 1233 | |
| 1234 | /* |
| 1235 | * The esdmode value will also be used for writing |
| 1236 | * MR1 during write leveling for DDR3, although the |
| 1237 | * bits specifically related to the write leveling |
| 1238 | * scheme will be handled automatically by the DDR |
| 1239 | * controller. so we set the wrlvl_en = 0 here. |
| 1240 | */ |
| 1241 | esdmode = (0 |
| 1242 | | ((qoff & 0x1) << 12) |
| 1243 | | ((tdqs_en & 0x1) << 11) |
| 1244 | | ((rtt & 0x7) << 8) |
| 1245 | | ((wrlvl_en & 0x1) << 7) |
| 1246 | | ((al & 0x3) << 3) |
| 1247 | | ((dic & 0x3) << 1) /* DIC field is split */ |
| 1248 | | ((dll_en & 0x1) << 0) |
| 1249 | ); |
| 1250 | |
| 1251 | /* |
| 1252 | * DLL control for precharge PD |
| 1253 | * 0=slow exit DLL off (tXPDLL) |
| 1254 | * 1=fast exit DLL on (tXP) |
| 1255 | */ |
| 1256 | |
| 1257 | wr_mclk = picos_to_mclk(common_dimm->twr_ps); |
| 1258 | if (wr_mclk <= 24) { |
| 1259 | wr = wr_table[wr_mclk - 10]; |
| 1260 | } else { |
| 1261 | printf("Error: unsupported write recovery for mode register wr_mclk = %d\n", |
| 1262 | wr_mclk); |
| 1263 | } |
| 1264 | |
| 1265 | dll_rst = 0; /* dll no reset */ |
| 1266 | mode = 0; /* normal mode */ |
| 1267 | |
| 1268 | /* look up table to get the cas latency bits */ |
| 1269 | if (cas_latency >= 9 && cas_latency <= 24) |
| 1270 | caslat = cas_latency_table[cas_latency - 9]; |
| 1271 | else |
| 1272 | printf("Error: unsupported cas latency for mode register\n"); |
| 1273 | |
| 1274 | bt = 0; /* Nibble sequential */ |
| 1275 | |
| 1276 | switch (popts->burst_length) { |
| 1277 | case DDR_BL8: |
| 1278 | bl = 0; |
| 1279 | break; |
| 1280 | case DDR_OTF: |
| 1281 | bl = 1; |
| 1282 | break; |
| 1283 | case DDR_BC4: |
| 1284 | bl = 2; |
| 1285 | break; |
| 1286 | default: |
| 1287 | printf("Error: invalid burst length of %u specified. ", |
| 1288 | popts->burst_length); |
| 1289 | puts("Defaulting to on-the-fly BC4 or BL8 beats.\n"); |
| 1290 | bl = 1; |
| 1291 | break; |
| 1292 | } |
| 1293 | |
| 1294 | sdmode = (0 |
| 1295 | | ((wr & 0x7) << 9) |
| 1296 | | ((dll_rst & 0x1) << 8) |
| 1297 | | ((mode & 0x1) << 7) |
| 1298 | | (((caslat >> 1) & 0x7) << 4) |
| 1299 | | ((bt & 0x1) << 3) |
| 1300 | | ((caslat & 1) << 2) |
| 1301 | | ((bl & 0x3) << 0) |
| 1302 | ); |
| 1303 | |
| 1304 | ddr->ddr_sdram_mode = (0 |
| 1305 | | ((esdmode & 0xFFFF) << 16) |
| 1306 | | ((sdmode & 0xFFFF) << 0) |
| 1307 | ); |
| 1308 | |
| 1309 | debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode); |
| 1310 | |
| 1311 | if (unq_mrs_en) { /* unique mode registers are supported */ |
| 1312 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 1313 | if (popts->rtt_override) |
| 1314 | rtt = popts->rtt_override_value; |
| 1315 | else |
| 1316 | rtt = popts->cs_local_opts[i].odt_rtt_norm; |
| 1317 | |
| 1318 | esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */ |
| 1319 | esdmode |= (rtt & 0x7) << 8; |
| 1320 | switch (i) { |
| 1321 | case 1: |
| 1322 | ddr->ddr_sdram_mode_3 = (0 |
| 1323 | | ((esdmode & 0xFFFF) << 16) |
| 1324 | | ((sdmode & 0xFFFF) << 0) |
| 1325 | ); |
| 1326 | break; |
| 1327 | case 2: |
| 1328 | ddr->ddr_sdram_mode_5 = (0 |
| 1329 | | ((esdmode & 0xFFFF) << 16) |
| 1330 | | ((sdmode & 0xFFFF) << 0) |
| 1331 | ); |
| 1332 | break; |
| 1333 | case 3: |
| 1334 | ddr->ddr_sdram_mode_7 = (0 |
| 1335 | | ((esdmode & 0xFFFF) << 16) |
| 1336 | | ((sdmode & 0xFFFF) << 0) |
| 1337 | ); |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n", |
| 1342 | ddr->ddr_sdram_mode_3); |
| 1343 | debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n", |
| 1344 | ddr->ddr_sdram_mode_5); |
| 1345 | debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n", |
| 1346 | ddr->ddr_sdram_mode_5); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | #elif defined(CONFIG_SYS_FSL_DDR3) |
| 1351 | /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */ |
| 1352 | static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr, |
| 1353 | const memctl_options_t *popts, |
| 1354 | const common_timing_params_t *common_dimm, |
| 1355 | unsigned int cas_latency, |
| 1356 | unsigned int additive_latency, |
| 1357 | const unsigned int unq_mrs_en) |
| 1358 | { |
| 1359 | int i; |
| 1360 | unsigned short esdmode; /* Extended SDRAM mode */ |
| 1361 | unsigned short sdmode; /* SDRAM mode */ |
| 1362 | |
| 1363 | /* Mode Register - MR1 */ |
| 1364 | unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */ |
| 1365 | unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */ |
| 1366 | unsigned int rtt; |
| 1367 | unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */ |
| 1368 | unsigned int al = 0; /* Posted CAS# additive latency (AL) */ |
| 1369 | unsigned int dic = 0; /* Output driver impedance, 40ohm */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1370 | unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal), |
| 1371 | 1=Disable (Test/Debug) */ |
| 1372 | |
| 1373 | /* Mode Register - MR0 */ |
| 1374 | unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */ |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1375 | unsigned int wr = 0; /* Write Recovery */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1376 | unsigned int dll_rst; /* DLL Reset */ |
| 1377 | unsigned int mode; /* Normal=0 or Test=1 */ |
| 1378 | unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */ |
| 1379 | /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */ |
| 1380 | unsigned int bt; |
| 1381 | unsigned int bl; /* BL: Burst Length */ |
| 1382 | |
| 1383 | unsigned int wr_mclk; |
York Sun | 3673f2c | 2011-03-02 14:24:11 -0800 | [diff] [blame] | 1384 | /* |
| 1385 | * DDR_SDRAM_MODE doesn't support 9,11,13,15 |
| 1386 | * Please refer JEDEC Standard No. 79-3E for Mode Register MR0 |
| 1387 | * for this table |
| 1388 | */ |
| 1389 | static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0}; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1390 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1391 | if (popts->rtt_override) |
| 1392 | rtt = popts->rtt_override_value; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1393 | else |
| 1394 | rtt = popts->cs_local_opts[0].odt_rtt_norm; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1395 | |
| 1396 | if (additive_latency == (cas_latency - 1)) |
| 1397 | al = 1; |
| 1398 | if (additive_latency == (cas_latency - 2)) |
| 1399 | al = 2; |
| 1400 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1401 | if (popts->quad_rank_present) |
| 1402 | dic = 1; /* output driver impedance 240/7 ohm */ |
| 1403 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1404 | /* |
| 1405 | * The esdmode value will also be used for writing |
| 1406 | * MR1 during write leveling for DDR3, although the |
| 1407 | * bits specifically related to the write leveling |
| 1408 | * scheme will be handled automatically by the DDR |
| 1409 | * controller. so we set the wrlvl_en = 0 here. |
| 1410 | */ |
| 1411 | esdmode = (0 |
| 1412 | | ((qoff & 0x1) << 12) |
| 1413 | | ((tdqs_en & 0x1) << 11) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 1414 | | ((rtt & 0x4) << 7) /* rtt field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1415 | | ((wrlvl_en & 0x1) << 7) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 1416 | | ((rtt & 0x2) << 5) /* rtt field is split */ |
| 1417 | | ((dic & 0x2) << 4) /* DIC field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1418 | | ((al & 0x3) << 3) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 1419 | | ((rtt & 0x1) << 2) /* rtt field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1420 | | ((dic & 0x1) << 1) /* DIC field is split */ |
| 1421 | | ((dll_en & 0x1) << 0) |
| 1422 | ); |
| 1423 | |
| 1424 | /* |
| 1425 | * DLL control for precharge PD |
| 1426 | * 0=slow exit DLL off (tXPDLL) |
| 1427 | * 1=fast exit DLL on (tXP) |
| 1428 | */ |
| 1429 | dll_on = 1; |
York Sun | 3673f2c | 2011-03-02 14:24:11 -0800 | [diff] [blame] | 1430 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1431 | wr_mclk = picos_to_mclk(common_dimm->twr_ps); |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1432 | if (wr_mclk <= 16) { |
| 1433 | wr = wr_table[wr_mclk - 5]; |
| 1434 | } else { |
| 1435 | printf("Error: unsupported write recovery for mode register " |
| 1436 | "wr_mclk = %d\n", wr_mclk); |
| 1437 | } |
York Sun | 3673f2c | 2011-03-02 14:24:11 -0800 | [diff] [blame] | 1438 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1439 | dll_rst = 0; /* dll no reset */ |
| 1440 | mode = 0; /* normal mode */ |
| 1441 | |
| 1442 | /* look up table to get the cas latency bits */ |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1443 | if (cas_latency >= 5 && cas_latency <= 16) { |
| 1444 | unsigned char cas_latency_table[] = { |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1445 | 0x2, /* 5 clocks */ |
| 1446 | 0x4, /* 6 clocks */ |
| 1447 | 0x6, /* 7 clocks */ |
| 1448 | 0x8, /* 8 clocks */ |
| 1449 | 0xa, /* 9 clocks */ |
| 1450 | 0xc, /* 10 clocks */ |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1451 | 0xe, /* 11 clocks */ |
| 1452 | 0x1, /* 12 clocks */ |
| 1453 | 0x3, /* 13 clocks */ |
| 1454 | 0x5, /* 14 clocks */ |
| 1455 | 0x7, /* 15 clocks */ |
| 1456 | 0x9, /* 16 clocks */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1457 | }; |
| 1458 | caslat = cas_latency_table[cas_latency - 5]; |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1459 | } else { |
| 1460 | printf("Error: unsupported cas latency for mode register\n"); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1461 | } |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1462 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1463 | bt = 0; /* Nibble sequential */ |
| 1464 | |
| 1465 | switch (popts->burst_length) { |
| 1466 | case DDR_BL8: |
| 1467 | bl = 0; |
| 1468 | break; |
| 1469 | case DDR_OTF: |
| 1470 | bl = 1; |
| 1471 | break; |
| 1472 | case DDR_BC4: |
| 1473 | bl = 2; |
| 1474 | break; |
| 1475 | default: |
| 1476 | printf("Error: invalid burst length of %u specified. " |
| 1477 | " Defaulting to on-the-fly BC4 or BL8 beats.\n", |
| 1478 | popts->burst_length); |
| 1479 | bl = 1; |
| 1480 | break; |
| 1481 | } |
| 1482 | |
| 1483 | sdmode = (0 |
| 1484 | | ((dll_on & 0x1) << 12) |
| 1485 | | ((wr & 0x7) << 9) |
| 1486 | | ((dll_rst & 0x1) << 8) |
| 1487 | | ((mode & 0x1) << 7) |
| 1488 | | (((caslat >> 1) & 0x7) << 4) |
| 1489 | | ((bt & 0x1) << 3) |
York Sun | bad8209 | 2012-08-17 08:22:38 +0000 | [diff] [blame] | 1490 | | ((caslat & 1) << 2) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1491 | | ((bl & 0x3) << 0) |
| 1492 | ); |
| 1493 | |
| 1494 | ddr->ddr_sdram_mode = (0 |
| 1495 | | ((esdmode & 0xFFFF) << 16) |
| 1496 | | ((sdmode & 0xFFFF) << 0) |
| 1497 | ); |
| 1498 | |
| 1499 | debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1500 | |
| 1501 | if (unq_mrs_en) { /* unique mode registers are supported */ |
Kumar Gala | d5bbe66 | 2011-11-09 10:05:10 -0600 | [diff] [blame] | 1502 | for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1503 | if (popts->rtt_override) |
| 1504 | rtt = popts->rtt_override_value; |
| 1505 | else |
| 1506 | rtt = popts->cs_local_opts[i].odt_rtt_norm; |
| 1507 | |
| 1508 | esdmode &= 0xFDBB; /* clear bit 9,6,2 */ |
| 1509 | esdmode |= (0 |
| 1510 | | ((rtt & 0x4) << 7) /* rtt field is split */ |
| 1511 | | ((rtt & 0x2) << 5) /* rtt field is split */ |
| 1512 | | ((rtt & 0x1) << 2) /* rtt field is split */ |
| 1513 | ); |
| 1514 | switch (i) { |
| 1515 | case 1: |
| 1516 | ddr->ddr_sdram_mode_3 = (0 |
| 1517 | | ((esdmode & 0xFFFF) << 16) |
| 1518 | | ((sdmode & 0xFFFF) << 0) |
| 1519 | ); |
| 1520 | break; |
| 1521 | case 2: |
| 1522 | ddr->ddr_sdram_mode_5 = (0 |
| 1523 | | ((esdmode & 0xFFFF) << 16) |
| 1524 | | ((sdmode & 0xFFFF) << 0) |
| 1525 | ); |
| 1526 | break; |
| 1527 | case 3: |
| 1528 | ddr->ddr_sdram_mode_7 = (0 |
| 1529 | | ((esdmode & 0xFFFF) << 16) |
| 1530 | | ((sdmode & 0xFFFF) << 0) |
| 1531 | ); |
| 1532 | break; |
| 1533 | } |
| 1534 | } |
| 1535 | debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n", |
| 1536 | ddr->ddr_sdram_mode_3); |
| 1537 | debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n", |
| 1538 | ddr->ddr_sdram_mode_5); |
| 1539 | debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n", |
| 1540 | ddr->ddr_sdram_mode_5); |
| 1541 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1542 | } |
| 1543 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 1544 | #else /* !CONFIG_SYS_FSL_DDR3 */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1545 | |
| 1546 | /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */ |
| 1547 | static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr, |
| 1548 | const memctl_options_t *popts, |
| 1549 | const common_timing_params_t *common_dimm, |
| 1550 | unsigned int cas_latency, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1551 | unsigned int additive_latency, |
| 1552 | const unsigned int unq_mrs_en) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1553 | { |
| 1554 | unsigned short esdmode; /* Extended SDRAM mode */ |
| 1555 | unsigned short sdmode; /* SDRAM mode */ |
| 1556 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1557 | /* |
| 1558 | * FIXME: This ought to be pre-calculated in a |
| 1559 | * technology-specific routine, |
| 1560 | * e.g. compute_DDR2_mode_register(), and then the |
| 1561 | * sdmode and esdmode passed in as part of common_dimm. |
| 1562 | */ |
| 1563 | |
| 1564 | /* Extended Mode Register */ |
| 1565 | unsigned int mrs = 0; /* Mode Register Set */ |
| 1566 | unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */ |
| 1567 | unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */ |
| 1568 | unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */ |
| 1569 | unsigned int ocd = 0; /* 0x0=OCD not supported, |
| 1570 | 0x7=OCD default state */ |
| 1571 | unsigned int rtt; |
| 1572 | unsigned int al; /* Posted CAS# additive latency (AL) */ |
| 1573 | unsigned int ods = 0; /* Output Drive Strength: |
| 1574 | 0 = Full strength (18ohm) |
| 1575 | 1 = Reduced strength (4ohm) */ |
| 1576 | unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal), |
| 1577 | 1=Disable (Test/Debug) */ |
| 1578 | |
| 1579 | /* Mode Register (MR) */ |
| 1580 | unsigned int mr; /* Mode Register Definition */ |
| 1581 | unsigned int pd; /* Power-Down Mode */ |
| 1582 | unsigned int wr; /* Write Recovery */ |
| 1583 | unsigned int dll_res; /* DLL Reset */ |
| 1584 | unsigned int mode; /* Normal=0 or Test=1 */ |
Kumar Gala | 35ad58d | 2008-09-05 14:40:29 -0500 | [diff] [blame] | 1585 | unsigned int caslat = 0;/* CAS# latency */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1586 | /* BT: Burst Type (0=Sequential, 1=Interleaved) */ |
| 1587 | unsigned int bt; |
| 1588 | unsigned int bl; /* BL: Burst Length */ |
| 1589 | |
Priyanka Jain | 4a71741 | 2013-09-25 10:41:19 +0530 | [diff] [blame] | 1590 | dqs_en = !popts->dqs_config; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1591 | rtt = fsl_ddr_get_rtt(); |
| 1592 | |
| 1593 | al = additive_latency; |
| 1594 | |
| 1595 | esdmode = (0 |
| 1596 | | ((mrs & 0x3) << 14) |
| 1597 | | ((outputs & 0x1) << 12) |
| 1598 | | ((rdqs_en & 0x1) << 11) |
| 1599 | | ((dqs_en & 0x1) << 10) |
| 1600 | | ((ocd & 0x7) << 7) |
| 1601 | | ((rtt & 0x2) << 5) /* rtt field is split */ |
| 1602 | | ((al & 0x7) << 3) |
| 1603 | | ((rtt & 0x1) << 2) /* rtt field is split */ |
| 1604 | | ((ods & 0x1) << 1) |
| 1605 | | ((dll_en & 0x1) << 0) |
| 1606 | ); |
| 1607 | |
| 1608 | mr = 0; /* FIXME: CHECKME */ |
| 1609 | |
| 1610 | /* |
| 1611 | * 0 = Fast Exit (Normal) |
| 1612 | * 1 = Slow Exit (Low Power) |
| 1613 | */ |
| 1614 | pd = 0; |
| 1615 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 1616 | #if defined(CONFIG_SYS_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1617 | wr = 0; /* Historical */ |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 1618 | #elif defined(CONFIG_SYS_FSL_DDR2) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1619 | wr = picos_to_mclk(common_dimm->twr_ps); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1620 | #endif |
| 1621 | dll_res = 0; |
| 1622 | mode = 0; |
| 1623 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 1624 | #if defined(CONFIG_SYS_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1625 | if (1 <= cas_latency && cas_latency <= 4) { |
| 1626 | unsigned char mode_caslat_table[4] = { |
| 1627 | 0x5, /* 1.5 clocks */ |
| 1628 | 0x2, /* 2.0 clocks */ |
| 1629 | 0x6, /* 2.5 clocks */ |
| 1630 | 0x3 /* 3.0 clocks */ |
| 1631 | }; |
Kumar Gala | 35ad58d | 2008-09-05 14:40:29 -0500 | [diff] [blame] | 1632 | caslat = mode_caslat_table[cas_latency - 1]; |
| 1633 | } else { |
| 1634 | printf("Warning: unknown cas_latency %d\n", cas_latency); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1635 | } |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 1636 | #elif defined(CONFIG_SYS_FSL_DDR2) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1637 | caslat = cas_latency; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1638 | #endif |
| 1639 | bt = 0; |
| 1640 | |
| 1641 | switch (popts->burst_length) { |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1642 | case DDR_BL4: |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1643 | bl = 2; |
| 1644 | break; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1645 | case DDR_BL8: |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1646 | bl = 3; |
| 1647 | break; |
| 1648 | default: |
| 1649 | printf("Error: invalid burst length of %u specified. " |
| 1650 | " Defaulting to 4 beats.\n", |
| 1651 | popts->burst_length); |
| 1652 | bl = 2; |
| 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | sdmode = (0 |
| 1657 | | ((mr & 0x3) << 14) |
| 1658 | | ((pd & 0x1) << 12) |
| 1659 | | ((wr & 0x7) << 9) |
| 1660 | | ((dll_res & 0x1) << 8) |
| 1661 | | ((mode & 0x1) << 7) |
| 1662 | | ((caslat & 0x7) << 4) |
| 1663 | | ((bt & 0x1) << 3) |
| 1664 | | ((bl & 0x7) << 0) |
| 1665 | ); |
| 1666 | |
| 1667 | ddr->ddr_sdram_mode = (0 |
| 1668 | | ((esdmode & 0xFFFF) << 16) |
| 1669 | | ((sdmode & 0xFFFF) << 0) |
| 1670 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 1671 | debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1672 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1673 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1674 | |
| 1675 | /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */ |
| 1676 | static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr) |
| 1677 | { |
| 1678 | unsigned int init_value; /* Initialization value */ |
| 1679 | |
Anatolij Gustschin | 021b7ae | 2013-01-21 23:50:27 +0000 | [diff] [blame] | 1680 | #ifdef CONFIG_MEM_INIT_VALUE |
| 1681 | init_value = CONFIG_MEM_INIT_VALUE; |
| 1682 | #else |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1683 | init_value = 0xDEADBEEF; |
Anatolij Gustschin | 021b7ae | 2013-01-21 23:50:27 +0000 | [diff] [blame] | 1684 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1685 | ddr->ddr_data_init = init_value; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL) |
| 1690 | * The old controller on the 8540/60 doesn't have this register. |
| 1691 | * Hope it's OK to set it (to 0) anyway. |
| 1692 | */ |
| 1693 | static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr, |
| 1694 | const memctl_options_t *popts) |
| 1695 | { |
| 1696 | unsigned int clk_adjust; /* Clock adjust */ |
| 1697 | |
| 1698 | clk_adjust = popts->clk_adjust; |
| 1699 | ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23; |
york | de87932 | 2010-07-02 22:25:55 +0000 | [diff] [blame] | 1700 | debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* DDR Initialization Address (DDR_INIT_ADDR) */ |
| 1704 | static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr) |
| 1705 | { |
| 1706 | unsigned int init_addr = 0; /* Initialization address */ |
| 1707 | |
| 1708 | ddr->ddr_init_addr = init_addr; |
| 1709 | } |
| 1710 | |
| 1711 | /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */ |
| 1712 | static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr) |
| 1713 | { |
| 1714 | unsigned int uia = 0; /* Use initialization address */ |
| 1715 | unsigned int init_ext_addr = 0; /* Initialization address */ |
| 1716 | |
| 1717 | ddr->ddr_init_ext_addr = (0 |
| 1718 | | ((uia & 0x1) << 31) |
| 1719 | | (init_ext_addr & 0xF) |
| 1720 | ); |
| 1721 | } |
| 1722 | |
| 1723 | /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */ |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 1724 | static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr, |
| 1725 | const memctl_options_t *popts) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1726 | { |
| 1727 | unsigned int rwt = 0; /* Read-to-write turnaround for same CS */ |
| 1728 | unsigned int wrt = 0; /* Write-to-read turnaround for same CS */ |
| 1729 | unsigned int rrt = 0; /* Read-to-read turnaround for same CS */ |
| 1730 | unsigned int wwt = 0; /* Write-to-write turnaround for same CS */ |
| 1731 | unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */ |
| 1732 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1733 | #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4) |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 1734 | if (popts->burst_length == DDR_BL8) { |
| 1735 | /* We set BL/2 for fixed BL8 */ |
| 1736 | rrt = 0; /* BL/2 clocks */ |
| 1737 | wwt = 0; /* BL/2 clocks */ |
| 1738 | } else { |
| 1739 | /* We need to set BL/2 + 2 to BC4 and OTF */ |
| 1740 | rrt = 2; /* BL/2 + 2 clocks */ |
| 1741 | wwt = 2; /* BL/2 + 2 clocks */ |
| 1742 | } |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1743 | #endif |
| 1744 | |
| 1745 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1746 | dll_lock = 2; /* tDLLK = 1024 clocks */ |
| 1747 | #elif defined(CONFIG_SYS_FSL_DDR3) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1748 | dll_lock = 1; /* tDLLK = 512 clocks from spec */ |
| 1749 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1750 | ddr->timing_cfg_4 = (0 |
| 1751 | | ((rwt & 0xf) << 28) |
| 1752 | | ((wrt & 0xf) << 24) |
| 1753 | | ((rrt & 0xf) << 20) |
| 1754 | | ((wwt & 0xf) << 16) |
| 1755 | | (dll_lock & 0x3) |
| 1756 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 1757 | debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */ |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1761 | static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1762 | { |
| 1763 | unsigned int rodt_on = 0; /* Read to ODT on */ |
| 1764 | unsigned int rodt_off = 0; /* Read to ODT off */ |
| 1765 | unsigned int wodt_on = 0; /* Write to ODT on */ |
| 1766 | unsigned int wodt_off = 0; /* Write to ODT off */ |
| 1767 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1768 | #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4) |
| 1769 | unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) + |
| 1770 | ((ddr->timing_cfg_2 & 0x00040000) >> 14); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1771 | /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1772 | if (cas_latency >= wr_lat) |
| 1773 | rodt_on = cas_latency - wr_lat + 1; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1774 | rodt_off = 4; /* 4 clocks */ |
york | 1714e49 | 2010-07-02 22:25:56 +0000 | [diff] [blame] | 1775 | wodt_on = 1; /* 1 clocks */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1776 | wodt_off = 4; /* 4 clocks */ |
| 1777 | #endif |
| 1778 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1779 | ddr->timing_cfg_5 = (0 |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 1780 | | ((rodt_on & 0x1f) << 24) |
| 1781 | | ((rodt_off & 0x7) << 20) |
| 1782 | | ((wodt_on & 0x1f) << 12) |
| 1783 | | ((wodt_off & 0x7) << 8) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1784 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 1785 | debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1786 | } |
| 1787 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1788 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1789 | static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr) |
| 1790 | { |
| 1791 | unsigned int hs_caslat = 0; |
| 1792 | unsigned int hs_wrlat = 0; |
| 1793 | unsigned int hs_wrrec = 0; |
| 1794 | unsigned int hs_clkadj = 0; |
| 1795 | unsigned int hs_wrlvl_start = 0; |
| 1796 | |
| 1797 | ddr->timing_cfg_6 = (0 |
| 1798 | | ((hs_caslat & 0x1f) << 24) |
| 1799 | | ((hs_wrlat & 0x1f) << 19) |
| 1800 | | ((hs_wrrec & 0x1f) << 12) |
| 1801 | | ((hs_clkadj & 0x1f) << 6) |
| 1802 | | ((hs_wrlvl_start & 0x1f) << 0) |
| 1803 | ); |
| 1804 | debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6); |
| 1805 | } |
| 1806 | |
| 1807 | static void set_timing_cfg_7(fsl_ddr_cfg_regs_t *ddr, |
| 1808 | const common_timing_params_t *common_dimm) |
| 1809 | { |
| 1810 | unsigned int txpr, tcksre, tcksrx; |
| 1811 | unsigned int cke_rst, cksre, cksrx, par_lat, cs_to_cmd; |
| 1812 | |
| 1813 | txpr = max(5, picos_to_mclk(common_dimm->trfc1_ps + 10000)); |
| 1814 | tcksre = max(5, picos_to_mclk(10000)); |
| 1815 | tcksrx = max(5, picos_to_mclk(10000)); |
| 1816 | par_lat = 0; |
| 1817 | cs_to_cmd = 0; |
| 1818 | |
| 1819 | if (txpr <= 200) |
| 1820 | cke_rst = 0; |
| 1821 | else if (txpr <= 256) |
| 1822 | cke_rst = 1; |
| 1823 | else if (txpr <= 512) |
| 1824 | cke_rst = 2; |
| 1825 | else |
| 1826 | cke_rst = 3; |
| 1827 | |
| 1828 | if (tcksre <= 19) |
| 1829 | cksre = tcksre - 5; |
| 1830 | else |
| 1831 | cksre = 15; |
| 1832 | |
| 1833 | if (tcksrx <= 19) |
| 1834 | cksrx = tcksrx - 5; |
| 1835 | else |
| 1836 | cksrx = 15; |
| 1837 | |
| 1838 | ddr->timing_cfg_7 = (0 |
| 1839 | | ((cke_rst & 0x3) << 28) |
| 1840 | | ((cksre & 0xf) << 24) |
| 1841 | | ((cksrx & 0xf) << 20) |
| 1842 | | ((par_lat & 0xf) << 16) |
| 1843 | | ((cs_to_cmd & 0xf) << 4) |
| 1844 | ); |
| 1845 | debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7); |
| 1846 | } |
| 1847 | |
| 1848 | static void set_timing_cfg_8(fsl_ddr_cfg_regs_t *ddr, |
| 1849 | const memctl_options_t *popts, |
| 1850 | const common_timing_params_t *common_dimm, |
| 1851 | unsigned int cas_latency) |
| 1852 | { |
| 1853 | unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg; |
| 1854 | unsigned int acttoact_bg, wrtord_bg, pre_all_rec; |
| 1855 | unsigned int tccdl = picos_to_mclk(common_dimm->tccdl_ps); |
| 1856 | unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) + |
| 1857 | ((ddr->timing_cfg_2 & 0x00040000) >> 14); |
| 1858 | |
| 1859 | rwt_bg = cas_latency + 2 + 4 - wr_lat; |
| 1860 | if (rwt_bg < tccdl) |
| 1861 | rwt_bg = tccdl - rwt_bg; |
| 1862 | else |
| 1863 | rwt_bg = 0; |
| 1864 | |
| 1865 | wrt_bg = wr_lat + 4 + 1 - cas_latency; |
| 1866 | if (wrt_bg < tccdl) |
| 1867 | wrt_bg = tccdl - wrt_bg; |
| 1868 | else |
| 1869 | wrt_bg = 0; |
| 1870 | |
| 1871 | if (popts->burst_length == DDR_BL8) { |
| 1872 | rrt_bg = tccdl - 4; |
| 1873 | wwt_bg = tccdl - 4; |
| 1874 | } else { |
| 1875 | rrt_bg = tccdl - 2; |
| 1876 | wwt_bg = tccdl - 4; |
| 1877 | } |
| 1878 | |
| 1879 | acttoact_bg = picos_to_mclk(common_dimm->trrdl_ps); |
| 1880 | wrtord_bg = max(4, picos_to_mclk(7500)); |
York Sun | f0e4f6d | 2014-06-26 11:14:44 -0700 | [diff] [blame] | 1881 | if (popts->otf_burst_chop_en) |
| 1882 | wrtord_bg += 2; |
| 1883 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1884 | pre_all_rec = 0; |
| 1885 | |
| 1886 | ddr->timing_cfg_8 = (0 |
| 1887 | | ((rwt_bg & 0xf) << 28) |
| 1888 | | ((wrt_bg & 0xf) << 24) |
| 1889 | | ((rrt_bg & 0xf) << 20) |
| 1890 | | ((wwt_bg & 0xf) << 16) |
| 1891 | | ((acttoact_bg & 0xf) << 12) |
| 1892 | | ((wrtord_bg & 0xf) << 8) |
| 1893 | | ((pre_all_rec & 0x1f) << 0) |
| 1894 | ); |
| 1895 | |
| 1896 | debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8); |
| 1897 | } |
| 1898 | |
| 1899 | static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr) |
| 1900 | { |
| 1901 | ddr->timing_cfg_9 = 0; |
| 1902 | debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9); |
| 1903 | } |
| 1904 | |
York Sun | a8b3d52 | 2014-09-11 13:32:06 -0700 | [diff] [blame] | 1905 | /* This function needs to be called after set_ddr_sdram_cfg() is called */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1906 | static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr, |
| 1907 | const dimm_params_t *dimm_params) |
| 1908 | { |
York Sun | a8b3d52 | 2014-09-11 13:32:06 -0700 | [diff] [blame] | 1909 | unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1; |
| 1910 | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1911 | ddr->dq_map_0 = ((dimm_params->dq_mapping[0] & 0x3F) << 26) | |
| 1912 | ((dimm_params->dq_mapping[1] & 0x3F) << 20) | |
| 1913 | ((dimm_params->dq_mapping[2] & 0x3F) << 14) | |
| 1914 | ((dimm_params->dq_mapping[3] & 0x3F) << 8) | |
| 1915 | ((dimm_params->dq_mapping[4] & 0x3F) << 2); |
| 1916 | |
| 1917 | ddr->dq_map_1 = ((dimm_params->dq_mapping[5] & 0x3F) << 26) | |
| 1918 | ((dimm_params->dq_mapping[6] & 0x3F) << 20) | |
| 1919 | ((dimm_params->dq_mapping[7] & 0x3F) << 14) | |
| 1920 | ((dimm_params->dq_mapping[10] & 0x3F) << 8) | |
| 1921 | ((dimm_params->dq_mapping[11] & 0x3F) << 2); |
| 1922 | |
| 1923 | ddr->dq_map_2 = ((dimm_params->dq_mapping[12] & 0x3F) << 26) | |
| 1924 | ((dimm_params->dq_mapping[13] & 0x3F) << 20) | |
| 1925 | ((dimm_params->dq_mapping[14] & 0x3F) << 14) | |
| 1926 | ((dimm_params->dq_mapping[15] & 0x3F) << 8) | |
| 1927 | ((dimm_params->dq_mapping[16] & 0x3F) << 2); |
| 1928 | |
York Sun | a8b3d52 | 2014-09-11 13:32:06 -0700 | [diff] [blame] | 1929 | /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1930 | ddr->dq_map_3 = ((dimm_params->dq_mapping[17] & 0x3F) << 26) | |
| 1931 | ((dimm_params->dq_mapping[8] & 0x3F) << 20) | |
York Sun | a8b3d52 | 2014-09-11 13:32:06 -0700 | [diff] [blame] | 1932 | (acc_ecc_en ? 0 : |
| 1933 | (dimm_params->dq_mapping[9] & 0x3F) << 14) | |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1934 | dimm_params->dq_mapping_ors; |
| 1935 | |
| 1936 | debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0); |
| 1937 | debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1); |
| 1938 | debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2); |
| 1939 | debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3); |
| 1940 | } |
| 1941 | static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr, |
| 1942 | const memctl_options_t *popts) |
| 1943 | { |
| 1944 | int rd_pre; |
| 1945 | |
| 1946 | rd_pre = popts->quad_rank_present ? 1 : 0; |
| 1947 | |
| 1948 | ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16; |
| 1949 | |
| 1950 | debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3); |
| 1951 | } |
| 1952 | #endif /* CONFIG_SYS_FSL_DDR4 */ |
| 1953 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1954 | /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1955 | static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1956 | { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1957 | unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */ |
| 1958 | /* Normal Operation Full Calibration Time (tZQoper) */ |
| 1959 | unsigned int zqoper = 0; |
| 1960 | /* Normal Operation Short Calibration Time (tZQCS) */ |
| 1961 | unsigned int zqcs = 0; |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1962 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1963 | unsigned int zqcs_init; |
| 1964 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1965 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1966 | if (zq_en) { |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1967 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1968 | zqinit = 10; /* 1024 clocks */ |
| 1969 | zqoper = 9; /* 512 clocks */ |
| 1970 | zqcs = 7; /* 128 clocks */ |
| 1971 | zqcs_init = 5; /* 1024 refresh sequences */ |
| 1972 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1973 | zqinit = 9; /* 512 clocks */ |
| 1974 | zqoper = 8; /* 256 clocks */ |
| 1975 | zqcs = 6; /* 64 clocks */ |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1976 | #endif |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1977 | } |
| 1978 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1979 | ddr->ddr_zq_cntl = (0 |
| 1980 | | ((zq_en & 0x1) << 31) |
| 1981 | | ((zqinit & 0xF) << 24) |
| 1982 | | ((zqoper & 0xF) << 16) |
| 1983 | | ((zqcs & 0xF) << 8) |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 1984 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 1985 | | ((zqcs_init & 0xF) << 0) |
| 1986 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1987 | ); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 1988 | debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */ |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 1992 | static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en, |
| 1993 | const memctl_options_t *popts) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1994 | { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1995 | /* |
| 1996 | * First DQS pulse rising edge after margining mode |
| 1997 | * is programmed (tWL_MRD) |
| 1998 | */ |
| 1999 | unsigned int wrlvl_mrd = 0; |
| 2000 | /* ODT delay after margining mode is programmed (tWL_ODTEN) */ |
| 2001 | unsigned int wrlvl_odten = 0; |
| 2002 | /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */ |
| 2003 | unsigned int wrlvl_dqsen = 0; |
| 2004 | /* WRLVL_SMPL: Write leveling sample time */ |
| 2005 | unsigned int wrlvl_smpl = 0; |
| 2006 | /* WRLVL_WLR: Write leveling repeition time */ |
| 2007 | unsigned int wrlvl_wlr = 0; |
| 2008 | /* WRLVL_START: Write leveling start time */ |
| 2009 | unsigned int wrlvl_start = 0; |
| 2010 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2011 | /* suggest enable write leveling for DDR3 due to fly-by topology */ |
| 2012 | if (wrlvl_en) { |
| 2013 | /* tWL_MRD min = 40 nCK, we set it 64 */ |
| 2014 | wrlvl_mrd = 0x6; |
| 2015 | /* tWL_ODTEN 128 */ |
| 2016 | wrlvl_odten = 0x7; |
| 2017 | /* tWL_DQSEN min = 25 nCK, we set it 32 */ |
| 2018 | wrlvl_dqsen = 0x5; |
| 2019 | /* |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 2020 | * Write leveling sample time at least need 6 clocks |
| 2021 | * higher than tWLO to allow enough time for progagation |
| 2022 | * delay and sampling the prime data bits. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2023 | */ |
| 2024 | wrlvl_smpl = 0xf; |
| 2025 | /* |
| 2026 | * Write leveling repetition time |
| 2027 | * at least tWLO + 6 clocks clocks |
york | 1714e49 | 2010-07-02 22:25:56 +0000 | [diff] [blame] | 2028 | * we set it 64 |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2029 | */ |
york | 1714e49 | 2010-07-02 22:25:56 +0000 | [diff] [blame] | 2030 | wrlvl_wlr = 0x6; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2031 | /* |
| 2032 | * Write leveling start time |
| 2033 | * The value use for the DQS_ADJUST for the first sample |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2034 | * when write leveling is enabled. It probably needs to be |
| 2035 | * overriden per platform. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2036 | */ |
| 2037 | wrlvl_start = 0x8; |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 2038 | /* |
| 2039 | * Override the write leveling sample and start time |
| 2040 | * according to specific board |
| 2041 | */ |
| 2042 | if (popts->wrlvl_override) { |
| 2043 | wrlvl_smpl = popts->wrlvl_sample; |
| 2044 | wrlvl_start = popts->wrlvl_start; |
| 2045 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2046 | } |
| 2047 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2048 | ddr->ddr_wrlvl_cntl = (0 |
| 2049 | | ((wrlvl_en & 0x1) << 31) |
| 2050 | | ((wrlvl_mrd & 0x7) << 24) |
| 2051 | | ((wrlvl_odten & 0x7) << 20) |
| 2052 | | ((wrlvl_dqsen & 0x7) << 16) |
| 2053 | | ((wrlvl_smpl & 0xf) << 12) |
| 2054 | | ((wrlvl_wlr & 0x7) << 8) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 2055 | | ((wrlvl_start & 0x1F) << 0) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2056 | ); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2057 | debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl); |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 2058 | ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2; |
| 2059 | debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2); |
| 2060 | ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3; |
| 2061 | debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3); |
| 2062 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | /* DDR Self Refresh Counter (DDR_SR_CNTR) */ |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2066 | static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2067 | { |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2068 | /* Self Refresh Idle Threshold */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2069 | ddr->ddr_sr_cntr = (sr_it & 0xF) << 16; |
| 2070 | } |
| 2071 | |
york | 4260372 | 2010-07-02 22:25:54 +0000 | [diff] [blame] | 2072 | static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts) |
| 2073 | { |
| 2074 | if (popts->addr_hash) { |
| 2075 | ddr->ddr_eor = 0x40000000; /* address hash enable */ |
Kumar Gala | 4513d76 | 2011-03-18 11:53:06 -0500 | [diff] [blame] | 2076 | puts("Address hashing enabled.\n"); |
york | 4260372 | 2010-07-02 22:25:54 +0000 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2080 | static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts) |
| 2081 | { |
| 2082 | ddr->ddr_cdr1 = popts->ddr_cdr1; |
| 2083 | debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1); |
| 2084 | } |
| 2085 | |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 2086 | static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts) |
| 2087 | { |
| 2088 | ddr->ddr_cdr2 = popts->ddr_cdr2; |
| 2089 | debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2); |
| 2090 | } |
| 2091 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2092 | unsigned int |
| 2093 | check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr) |
| 2094 | { |
| 2095 | unsigned int res = 0; |
| 2096 | |
| 2097 | /* |
| 2098 | * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are |
| 2099 | * not set at the same time. |
| 2100 | */ |
| 2101 | if (ddr->ddr_sdram_cfg & 0x10000000 |
| 2102 | && ddr->ddr_sdram_cfg & 0x00008000) { |
| 2103 | printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] " |
| 2104 | " should not be set at the same time.\n"); |
| 2105 | res++; |
| 2106 | } |
| 2107 | |
| 2108 | return res; |
| 2109 | } |
| 2110 | |
| 2111 | unsigned int |
| 2112 | compute_fsl_memctl_config_regs(const memctl_options_t *popts, |
| 2113 | fsl_ddr_cfg_regs_t *ddr, |
| 2114 | const common_timing_params_t *common_dimm, |
| 2115 | const dimm_params_t *dimm_params, |
Haiying Wang | 80ad401 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 2116 | unsigned int dbw_cap_adj, |
| 2117 | unsigned int size_only) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2118 | { |
| 2119 | unsigned int i; |
| 2120 | unsigned int cas_latency; |
| 2121 | unsigned int additive_latency; |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2122 | unsigned int sr_it; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2123 | unsigned int zq_en; |
| 2124 | unsigned int wrlvl_en; |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2125 | unsigned int ip_rev = 0; |
| 2126 | unsigned int unq_mrs_en = 0; |
York Sun | 2927c5e | 2010-10-18 13:46:50 -0700 | [diff] [blame] | 2127 | int cs_en = 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2128 | |
| 2129 | memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t)); |
| 2130 | |
| 2131 | if (common_dimm == NULL) { |
| 2132 | printf("Error: subset DIMM params struct null pointer\n"); |
| 2133 | return 1; |
| 2134 | } |
| 2135 | |
| 2136 | /* |
| 2137 | * Process overrides first. |
| 2138 | * |
| 2139 | * FIXME: somehow add dereated caslat to this |
| 2140 | */ |
| 2141 | cas_latency = (popts->cas_latency_override) |
| 2142 | ? popts->cas_latency_override_value |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 2143 | : common_dimm->lowest_common_spd_caslat; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2144 | |
| 2145 | additive_latency = (popts->additive_latency_override) |
| 2146 | ? popts->additive_latency_override_value |
| 2147 | : common_dimm->additive_latency; |
| 2148 | |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2149 | sr_it = (popts->auto_self_refresh_en) |
| 2150 | ? popts->sr_it |
| 2151 | : 0; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2152 | /* ZQ calibration */ |
| 2153 | zq_en = (popts->zq_en) ? 1 : 0; |
| 2154 | /* write leveling */ |
| 2155 | wrlvl_en = (popts->wrlvl_en) ? 1 : 0; |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2156 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2157 | /* Chip Select Memory Bounds (CSn_BNDS) */ |
| 2158 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2159 | unsigned long long ea, sa; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2160 | unsigned int cs_per_dimm |
| 2161 | = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR; |
| 2162 | unsigned int dimm_number |
| 2163 | = i / cs_per_dimm; |
| 2164 | unsigned long long rank_density |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2165 | = dimm_params[dimm_number].rank_density >> dbw_cap_adj; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2166 | |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2167 | if (dimm_params[dimm_number].n_ranks == 0) { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2168 | debug("Skipping setup of CS%u " |
york | f4f93c6 | 2010-07-02 22:25:53 +0000 | [diff] [blame] | 2169 | "because n_ranks on DIMM %u is 0\n", i, dimm_number); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2170 | continue; |
| 2171 | } |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2172 | if (popts->memctl_interleaving) { |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2173 | switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) { |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2174 | case FSL_DDR_CS0_CS1_CS2_CS3: |
| 2175 | break; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2176 | case FSL_DDR_CS0_CS1: |
| 2177 | case FSL_DDR_CS0_CS1_AND_CS2_CS3: |
York Sun | 2927c5e | 2010-10-18 13:46:50 -0700 | [diff] [blame] | 2178 | if (i > 1) |
| 2179 | cs_en = 0; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | case FSL_DDR_CS2_CS3: |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2182 | default: |
York Sun | 2927c5e | 2010-10-18 13:46:50 -0700 | [diff] [blame] | 2183 | if (i > 0) |
| 2184 | cs_en = 0; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2185 | break; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2186 | } |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2187 | sa = common_dimm->base_address; |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2188 | ea = sa + common_dimm->total_mem - 1; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2189 | } else if (!popts->memctl_interleaving) { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2190 | /* |
| 2191 | * If memory interleaving between controllers is NOT |
| 2192 | * enabled, the starting address for each memory |
| 2193 | * controller is distinct. However, because rank |
| 2194 | * interleaving is enabled, the starting and ending |
| 2195 | * addresses of the total memory on that memory |
| 2196 | * controller needs to be programmed into its |
| 2197 | * respective CS0_BNDS. |
| 2198 | */ |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2199 | switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) { |
| 2200 | case FSL_DDR_CS0_CS1_CS2_CS3: |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2201 | sa = common_dimm->base_address; |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2202 | ea = sa + common_dimm->total_mem - 1; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2203 | break; |
| 2204 | case FSL_DDR_CS0_CS1_AND_CS2_CS3: |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2205 | if ((i >= 2) && (dimm_number == 0)) { |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2206 | sa = dimm_params[dimm_number].base_address + |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2207 | 2 * rank_density; |
| 2208 | ea = sa + 2 * rank_density - 1; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2209 | } else { |
| 2210 | sa = dimm_params[dimm_number].base_address; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2211 | ea = sa + 2 * rank_density - 1; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2212 | } |
| 2213 | break; |
| 2214 | case FSL_DDR_CS0_CS1: |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2215 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 2216 | sa = dimm_params[dimm_number].base_address; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2217 | ea = sa + rank_density - 1; |
| 2218 | if (i != 1) |
| 2219 | sa += (i % cs_per_dimm) * rank_density; |
| 2220 | ea += (i % cs_per_dimm) * rank_density; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2221 | } else { |
| 2222 | sa = 0; |
| 2223 | ea = 0; |
| 2224 | } |
| 2225 | if (i == 0) |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2226 | ea += rank_density; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2227 | break; |
| 2228 | case FSL_DDR_CS2_CS3: |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2229 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 2230 | sa = dimm_params[dimm_number].base_address; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2231 | ea = sa + rank_density - 1; |
| 2232 | if (i != 3) |
| 2233 | sa += (i % cs_per_dimm) * rank_density; |
| 2234 | ea += (i % cs_per_dimm) * rank_density; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2235 | } else { |
| 2236 | sa = 0; |
| 2237 | ea = 0; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2238 | } |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame] | 2239 | if (i == 2) |
| 2240 | ea += (rank_density >> dbw_cap_adj); |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2241 | break; |
| 2242 | default: /* No bank(chip-select) interleaving */ |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 2243 | sa = dimm_params[dimm_number].base_address; |
| 2244 | ea = sa + rank_density - 1; |
| 2245 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 2246 | sa += (i % cs_per_dimm) * rank_density; |
| 2247 | ea += (i % cs_per_dimm) * rank_density; |
| 2248 | } else { |
| 2249 | sa = 0; |
| 2250 | ea = 0; |
| 2251 | } |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 2252 | break; |
| 2253 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2254 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2255 | |
| 2256 | sa >>= 24; |
| 2257 | ea >>= 24; |
| 2258 | |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2259 | if (cs_en) { |
| 2260 | ddr->cs[i].bnds = (0 |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 2261 | | ((sa & 0xffff) << 16) /* starting address */ |
| 2262 | | ((ea & 0xffff) << 0) /* ending address */ |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2263 | ); |
| 2264 | } else { |
York Sun | c21a739 | 2013-06-25 11:37:45 -0700 | [diff] [blame] | 2265 | /* setting bnds to 0xffffffff for inactive CS */ |
| 2266 | ddr->cs[i].bnds = 0xffffffff; |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2267 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2268 | |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 2269 | debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds); |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2270 | set_csn_config(dimm_number, i, ddr, popts, dimm_params); |
| 2271 | set_csn_config_2(i, ddr); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2272 | } |
| 2273 | |
Haiying Wang | 80ad401 | 2010-12-01 10:35:31 -0500 | [diff] [blame] | 2274 | /* |
| 2275 | * In the case we only need to compute the ddr sdram size, we only need |
| 2276 | * to set csn registers, so return from here. |
| 2277 | */ |
| 2278 | if (size_only) |
| 2279 | return 0; |
| 2280 | |
york | 4260372 | 2010-07-02 22:25:54 +0000 | [diff] [blame] | 2281 | set_ddr_eor(ddr, popts); |
| 2282 | |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 2283 | #if !defined(CONFIG_SYS_FSL_DDR1) |
York Sun | 98df4d1 | 2012-10-08 07:44:23 +0000 | [diff] [blame] | 2284 | set_timing_cfg_0(ddr, popts, dimm_params); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2285 | #endif |
| 2286 | |
York Sun | 63c91cd | 2013-06-03 12:39:06 -0700 | [diff] [blame] | 2287 | set_timing_cfg_3(ddr, popts, common_dimm, cas_latency, |
| 2288 | additive_latency); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2289 | set_timing_cfg_1(ddr, popts, common_dimm, cas_latency); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2290 | set_timing_cfg_2(ddr, popts, common_dimm, |
| 2291 | cas_latency, additive_latency); |
| 2292 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2293 | set_ddr_cdr1(ddr, popts); |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 2294 | set_ddr_cdr2(ddr, popts); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2295 | set_ddr_sdram_cfg(ddr, popts, common_dimm); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2296 | ip_rev = fsl_ddr_get_version(); |
| 2297 | if (ip_rev > 0x40400) |
| 2298 | unq_mrs_en = 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2299 | |
York Sun | a8b3d52 | 2014-09-11 13:32:06 -0700 | [diff] [blame] | 2300 | if ((ip_rev > 0x40700) && (popts->cswl_override != 0)) |
York Sun | e0f6046 | 2014-09-05 13:52:43 +0800 | [diff] [blame] | 2301 | ddr->debug[18] = popts->cswl_override; |
| 2302 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2303 | set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2304 | set_ddr_sdram_mode(ddr, popts, common_dimm, |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2305 | cas_latency, additive_latency, unq_mrs_en); |
Valentin Longchamp | 0b81093 | 2013-10-18 11:47:20 +0200 | [diff] [blame] | 2306 | set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 2307 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 2308 | set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en); |
| 2309 | set_ddr_sdram_mode_10(ddr, popts, common_dimm, unq_mrs_en); |
| 2310 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2311 | set_ddr_sdram_interval(ddr, popts, common_dimm); |
| 2312 | set_ddr_data_init(ddr); |
| 2313 | set_ddr_sdram_clk_cntl(ddr, popts); |
| 2314 | set_ddr_init_addr(ddr); |
| 2315 | set_ddr_init_ext_addr(ddr); |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 2316 | set_timing_cfg_4(ddr, popts); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2317 | set_timing_cfg_5(ddr, cas_latency); |
York Sun | 2896cb7 | 2014-03-27 17:54:47 -0700 | [diff] [blame] | 2318 | #ifdef CONFIG_SYS_FSL_DDR4 |
| 2319 | set_ddr_sdram_cfg_3(ddr, popts); |
| 2320 | set_timing_cfg_6(ddr); |
| 2321 | set_timing_cfg_7(ddr, common_dimm); |
| 2322 | set_timing_cfg_8(ddr, popts, common_dimm, cas_latency); |
| 2323 | set_timing_cfg_9(ddr); |
| 2324 | set_ddr_dq_mapping(ddr, dimm_params); |
| 2325 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2326 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 2327 | set_ddr_zq_cntl(ddr, zq_en); |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 2328 | set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2329 | |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 2330 | set_ddr_sr_cntr(ddr, sr_it); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2331 | |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 2332 | set_ddr_sdram_rcw(ddr, popts, common_dimm); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2333 | |
York Sun | 972cc40 | 2013-06-25 11:37:41 -0700 | [diff] [blame] | 2334 | #ifdef CONFIG_SYS_FSL_DDR_EMU |
| 2335 | /* disble DDR training for emulator */ |
| 2336 | ddr->debug[2] = 0x00000400; |
| 2337 | ddr->debug[4] = 0xff800000; |
| 2338 | #endif |
York Sun | 9982579 | 2014-05-23 13:15:00 -0700 | [diff] [blame] | 2339 | #ifdef CONFIG_SYS_FSL_ERRATUM_A004508 |
| 2340 | if ((ip_rev >= 0x40000) && (ip_rev < 0x40400)) |
| 2341 | ddr->debug[2] |= 0x00000200; /* set bit 22 */ |
| 2342 | #endif |
| 2343 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 2344 | return check_fsl_memctl_config_regs(ddr); |
| 2345 | } |