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