Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1 | /* |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 2 | * Copyright 2008-2010 Freescale Semiconductor, Inc. |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 3 | * |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Generic driver for Freescale DDR/DDR2/DDR3 memory controller. |
| 12 | * Based on code from spd_sdram.c |
| 13 | * Author: James Yang [at freescale.com] |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #include <asm/fsl_ddr_sdram.h> |
| 18 | |
| 19 | #include "ddr.h" |
| 20 | |
| 21 | extern unsigned int picos_to_mclk(unsigned int picos); |
| 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 | |
| 59 | #if defined(CONFIG_FSL_DDR1) |
| 60 | rtt = 0; |
| 61 | #elif defined(CONFIG_FSL_DDR2) |
| 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 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 70 | /* |
| 71 | * compute the CAS write latency according to DDR3 spec |
| 72 | * CWL = 5 if tCK >= 2.5ns |
| 73 | * 6 if 2.5ns > tCK >= 1.875ns |
| 74 | * 7 if 1.875ns > tCK >= 1.5ns |
| 75 | * 8 if 1.5ns > tCK >= 1.25ns |
| 76 | */ |
| 77 | static inline unsigned int compute_cas_write_latency(void) |
| 78 | { |
| 79 | unsigned int cwl; |
| 80 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 81 | |
| 82 | if (mclk_ps >= 2500) |
| 83 | cwl = 5; |
| 84 | else if (mclk_ps >= 1875) |
| 85 | cwl = 6; |
| 86 | else if (mclk_ps >= 1500) |
| 87 | cwl = 7; |
| 88 | else if (mclk_ps >= 1250) |
| 89 | cwl = 8; |
| 90 | else |
| 91 | cwl = 8; |
| 92 | return cwl; |
| 93 | } |
| 94 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 95 | /* Chip Select Configuration (CSn_CONFIG) */ |
| 96 | static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr, |
| 97 | const memctl_options_t *popts, |
| 98 | const dimm_params_t *dimm_params) |
| 99 | { |
| 100 | unsigned int cs_n_en = 0; /* Chip Select enable */ |
| 101 | unsigned int intlv_en = 0; /* Memory controller interleave enable */ |
| 102 | unsigned int intlv_ctl = 0; /* Interleaving control */ |
| 103 | unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */ |
| 104 | unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */ |
| 105 | unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */ |
| 106 | unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */ |
| 107 | unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */ |
| 108 | unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */ |
| 109 | |
| 110 | /* Compute CS_CONFIG only for existing ranks of each DIMM. */ |
| 111 | if ((((i&1) == 0) |
| 112 | && (dimm_params[i/2].n_ranks == 1)) |
| 113 | || (dimm_params[i/2].n_ranks == 2)) { |
| 114 | unsigned int n_banks_per_sdram_device; |
| 115 | cs_n_en = 1; |
| 116 | if (i == 0) { |
| 117 | /* These fields only available in CS0_CONFIG */ |
| 118 | intlv_en = popts->memctl_interleaving; |
| 119 | intlv_ctl = popts->memctl_interleaving_mode; |
| 120 | } |
| 121 | ap_n_en = popts->cs_local_opts[i].auto_precharge; |
| 122 | odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg; |
| 123 | odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg; |
| 124 | n_banks_per_sdram_device |
| 125 | = dimm_params[i/2].n_banks_per_sdram_device; |
| 126 | ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2; |
| 127 | row_bits_cs_n = dimm_params[i/2].n_row_addr - 12; |
| 128 | col_bits_cs_n = dimm_params[i/2].n_col_addr - 8; |
| 129 | } |
| 130 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 131 | ddr->cs[i].config = (0 |
| 132 | | ((cs_n_en & 0x1) << 31) |
| 133 | | ((intlv_en & 0x3) << 29) |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 134 | | ((intlv_ctl & 0xf) << 24) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 135 | | ((ap_n_en & 0x1) << 23) |
| 136 | |
| 137 | /* XXX: some implementation only have 1 bit starting at left */ |
| 138 | | ((odt_rd_cfg & 0x7) << 20) |
| 139 | |
| 140 | /* XXX: Some implementation only have 1 bit starting at left */ |
| 141 | | ((odt_wr_cfg & 0x7) << 16) |
| 142 | |
| 143 | | ((ba_bits_cs_n & 0x3) << 14) |
| 144 | | ((row_bits_cs_n & 0x7) << 8) |
| 145 | | ((col_bits_cs_n & 0x7) << 0) |
| 146 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 147 | 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] | 148 | } |
| 149 | |
| 150 | /* Chip Select Configuration 2 (CSn_CONFIG_2) */ |
| 151 | /* FIXME: 8572 */ |
| 152 | static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr) |
| 153 | { |
| 154 | unsigned int pasr_cfg = 0; /* Partial array self refresh config */ |
| 155 | |
| 156 | ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 157 | 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] | 158 | } |
| 159 | |
| 160 | /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */ |
| 161 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 162 | #if !defined(CONFIG_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 163 | /* |
| 164 | * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0) |
| 165 | * |
| 166 | * Avoid writing for DDR I. The new PQ38 DDR controller |
| 167 | * dreams up non-zero default values to be backwards compatible. |
| 168 | */ |
| 169 | static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr) |
| 170 | { |
| 171 | unsigned char trwt_mclk = 0; /* Read-to-write turnaround */ |
| 172 | unsigned char twrt_mclk = 0; /* Write-to-read turnaround */ |
| 173 | /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */ |
| 174 | unsigned char trrt_mclk = 0; /* Read-to-read turnaround */ |
| 175 | unsigned char twwt_mclk = 0; /* Write-to-write turnaround */ |
| 176 | |
| 177 | /* Active powerdown exit timing (tXARD and tXARDS). */ |
| 178 | unsigned char act_pd_exit_mclk; |
| 179 | /* Precharge powerdown exit timing (tXP). */ |
| 180 | unsigned char pre_pd_exit_mclk; |
| 181 | /* Precharge powerdown exit timing (tAXPD). */ |
| 182 | unsigned char taxpd_mclk; |
| 183 | /* Mode register set cycle time (tMRD). */ |
| 184 | unsigned char tmrd_mclk; |
| 185 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 186 | #if defined(CONFIG_FSL_DDR3) |
| 187 | /* |
| 188 | * (tXARD and tXARDS). Empirical? |
| 189 | * The DDR3 spec has not tXARD, |
| 190 | * we use the tXP instead of it. |
| 191 | * tXP=max(3nCK, 7.5ns) for DDR3. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 192 | * spec has not the tAXPD, we use |
| 193 | * tAXPD=8, need design to confirm. |
| 194 | */ |
Dave Liu | c7d983a | 2009-12-16 10:24:36 -0600 | [diff] [blame] | 195 | int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */ |
| 196 | act_pd_exit_mclk = picos_to_mclk(tXP); |
| 197 | /* Mode register MR0[A12] is '1' - fast exit */ |
| 198 | pre_pd_exit_mclk = act_pd_exit_mclk; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 199 | taxpd_mclk = 8; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 200 | tmrd_mclk = 4; |
Dave Liu | 8107926 | 2009-12-08 11:56:48 +0800 | [diff] [blame] | 201 | /* set the turnaround time */ |
| 202 | trwt_mclk = 1; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 203 | #else /* CONFIG_FSL_DDR2 */ |
| 204 | /* |
| 205 | * (tXARD and tXARDS). Empirical? |
| 206 | * tXARD = 2 for DDR2 |
| 207 | * tXP=2 |
| 208 | * tAXPD=8 |
| 209 | */ |
| 210 | act_pd_exit_mclk = 2; |
| 211 | pre_pd_exit_mclk = 2; |
| 212 | taxpd_mclk = 8; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 213 | tmrd_mclk = 2; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 214 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 215 | |
| 216 | ddr->timing_cfg_0 = (0 |
| 217 | | ((trwt_mclk & 0x3) << 30) /* RWT */ |
| 218 | | ((twrt_mclk & 0x3) << 28) /* WRT */ |
| 219 | | ((trrt_mclk & 0x3) << 26) /* RRT */ |
| 220 | | ((twwt_mclk & 0x3) << 24) /* WWT */ |
| 221 | | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */ |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 222 | | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 223 | | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */ |
| 224 | | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */ |
| 225 | ); |
| 226 | debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0); |
| 227 | } |
| 228 | #endif /* defined(CONFIG_FSL_DDR2) */ |
| 229 | |
| 230 | /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */ |
| 231 | static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr, |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 232 | const common_timing_params_t *common_dimm, |
| 233 | unsigned int cas_latency) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 234 | { |
| 235 | /* Extended Activate to precharge interval (tRAS) */ |
| 236 | unsigned int ext_acttopre = 0; |
| 237 | unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */ |
| 238 | unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */ |
| 239 | unsigned int cntl_adj = 0; /* Control Adjust */ |
| 240 | |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 241 | /* If the tRAS > 19 MCLK, we use the ext mode */ |
| 242 | if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13) |
| 243 | ext_acttopre = 1; |
| 244 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 245 | ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 246 | |
| 247 | /* If the CAS latency more than 8, use the ext mode */ |
| 248 | if (cas_latency > 8) |
| 249 | ext_caslat = 1; |
| 250 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 251 | ddr->timing_cfg_3 = (0 |
| 252 | | ((ext_acttopre & 0x1) << 24) |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 253 | | ((ext_refrec & 0xF) << 16) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 254 | | ((ext_caslat & 0x1) << 12) |
| 255 | | ((cntl_adj & 0x7) << 0) |
| 256 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 257 | debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */ |
| 261 | static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr, |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 262 | const memctl_options_t *popts, |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 263 | const common_timing_params_t *common_dimm, |
| 264 | unsigned int cas_latency) |
| 265 | { |
| 266 | /* Precharge-to-activate interval (tRP) */ |
| 267 | unsigned char pretoact_mclk; |
| 268 | /* Activate to precharge interval (tRAS) */ |
| 269 | unsigned char acttopre_mclk; |
| 270 | /* Activate to read/write interval (tRCD) */ |
| 271 | unsigned char acttorw_mclk; |
| 272 | /* CASLAT */ |
| 273 | unsigned char caslat_ctrl; |
| 274 | /* Refresh recovery time (tRFC) ; trfc_low */ |
| 275 | unsigned char refrec_ctrl; |
| 276 | /* Last data to precharge minimum interval (tWR) */ |
| 277 | unsigned char wrrec_mclk; |
| 278 | /* Activate-to-activate interval (tRRD) */ |
| 279 | unsigned char acttoact_mclk; |
| 280 | /* Last write data pair to read command issue interval (tWTR) */ |
| 281 | unsigned char wrtord_mclk; |
| 282 | |
| 283 | pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps); |
| 284 | acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps); |
| 285 | acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps); |
| 286 | |
| 287 | /* |
| 288 | * Translate CAS Latency to a DDR controller field value: |
| 289 | * |
| 290 | * CAS Lat DDR I DDR II Ctrl |
| 291 | * Clocks SPD Bit SPD Bit Value |
| 292 | * ------- ------- ------- ----- |
| 293 | * 1.0 0 0001 |
| 294 | * 1.5 1 0010 |
| 295 | * 2.0 2 2 0011 |
| 296 | * 2.5 3 0100 |
| 297 | * 3.0 4 3 0101 |
| 298 | * 3.5 5 0110 |
| 299 | * 4.0 4 0111 |
| 300 | * 4.5 1000 |
| 301 | * 5.0 5 1001 |
| 302 | */ |
| 303 | #if defined(CONFIG_FSL_DDR1) |
| 304 | caslat_ctrl = (cas_latency + 1) & 0x07; |
| 305 | #elif defined(CONFIG_FSL_DDR2) |
| 306 | caslat_ctrl = 2 * cas_latency - 1; |
| 307 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 308 | /* |
| 309 | * if the CAS latency more than 8 cycle, |
| 310 | * we need set extend bit for it at |
| 311 | * TIMING_CFG_3[EXT_CASLAT] |
| 312 | */ |
| 313 | if (cas_latency > 8) |
| 314 | cas_latency -= 8; |
| 315 | caslat_ctrl = 2 * cas_latency - 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 316 | #endif |
| 317 | |
| 318 | refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8; |
| 319 | wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 320 | if (popts->OTF_burst_chop_en) |
| 321 | wrrec_mclk += 2; |
| 322 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 323 | acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 324 | /* |
| 325 | * JEDEC has min requirement for tRRD |
| 326 | */ |
| 327 | #if defined(CONFIG_FSL_DDR3) |
| 328 | if (acttoact_mclk < 4) |
| 329 | acttoact_mclk = 4; |
| 330 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 331 | wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 332 | /* |
| 333 | * JEDEC has some min requirements for tWTR |
| 334 | */ |
| 335 | #if defined(CONFIG_FSL_DDR2) |
| 336 | if (wrtord_mclk < 2) |
| 337 | wrtord_mclk = 2; |
| 338 | #elif defined(CONFIG_FSL_DDR3) |
| 339 | if (wrtord_mclk < 4) |
| 340 | wrtord_mclk = 4; |
| 341 | #endif |
| 342 | if (popts->OTF_burst_chop_en) |
| 343 | wrtord_mclk += 2; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 344 | |
| 345 | ddr->timing_cfg_1 = (0 |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 346 | | ((pretoact_mclk & 0x0F) << 28) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 347 | | ((acttopre_mclk & 0x0F) << 24) |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 348 | | ((acttorw_mclk & 0xF) << 20) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 349 | | ((caslat_ctrl & 0xF) << 16) |
| 350 | | ((refrec_ctrl & 0xF) << 12) |
Dave Liu | 5c1bb51 | 2008-11-21 16:31:22 +0800 | [diff] [blame] | 351 | | ((wrrec_mclk & 0x0F) << 8) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 352 | | ((acttoact_mclk & 0x07) << 4) |
| 353 | | ((wrtord_mclk & 0x07) << 0) |
| 354 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 355 | debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */ |
| 359 | static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr, |
| 360 | const memctl_options_t *popts, |
| 361 | const common_timing_params_t *common_dimm, |
| 362 | unsigned int cas_latency, |
| 363 | unsigned int additive_latency) |
| 364 | { |
| 365 | /* Additive latency */ |
| 366 | unsigned char add_lat_mclk; |
| 367 | /* CAS-to-preamble override */ |
| 368 | unsigned short cpo; |
| 369 | /* Write latency */ |
| 370 | unsigned char wr_lat; |
| 371 | /* Read to precharge (tRTP) */ |
| 372 | unsigned char rd_to_pre; |
| 373 | /* Write command to write data strobe timing adjustment */ |
| 374 | unsigned char wr_data_delay; |
| 375 | /* Minimum CKE pulse width (tCKE) */ |
| 376 | unsigned char cke_pls; |
| 377 | /* Window for four activates (tFAW) */ |
| 378 | unsigned short four_act; |
| 379 | |
| 380 | /* FIXME add check that this must be less than acttorw_mclk */ |
| 381 | add_lat_mclk = additive_latency; |
| 382 | cpo = popts->cpo_override; |
| 383 | |
| 384 | #if defined(CONFIG_FSL_DDR1) |
| 385 | /* |
| 386 | * This is a lie. It should really be 1, but if it is |
| 387 | * set to 1, bits overlap into the old controller's |
| 388 | * otherwise unused ACSM field. If we leave it 0, then |
| 389 | * the HW will magically treat it as 1 for DDR 1. Oh Yea. |
| 390 | */ |
| 391 | wr_lat = 0; |
| 392 | #elif defined(CONFIG_FSL_DDR2) |
Dave Liu | 82aa953 | 2009-03-14 12:48:19 +0800 | [diff] [blame] | 393 | wr_lat = cas_latency - 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 394 | #else |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 395 | wr_lat = compute_cas_write_latency(); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 396 | #endif |
| 397 | |
| 398 | rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps); |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 399 | /* |
| 400 | * JEDEC has some min requirements for tRTP |
| 401 | */ |
Dave Liu | 82aa953 | 2009-03-14 12:48:19 +0800 | [diff] [blame] | 402 | #if defined(CONFIG_FSL_DDR2) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 403 | if (rd_to_pre < 2) |
| 404 | rd_to_pre = 2; |
| 405 | #elif defined(CONFIG_FSL_DDR3) |
| 406 | if (rd_to_pre < 4) |
| 407 | rd_to_pre = 4; |
Dave Liu | 82aa953 | 2009-03-14 12:48:19 +0800 | [diff] [blame] | 408 | #endif |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 409 | if (additive_latency) |
| 410 | rd_to_pre += additive_latency; |
| 411 | if (popts->OTF_burst_chop_en) |
| 412 | rd_to_pre += 2; /* according to UM */ |
| 413 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 414 | wr_data_delay = popts->write_data_delay; |
| 415 | cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps); |
| 416 | four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps); |
| 417 | |
| 418 | ddr->timing_cfg_2 = (0 |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 419 | | ((add_lat_mclk & 0xf) << 28) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 420 | | ((cpo & 0x1f) << 23) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 421 | | ((wr_lat & 0xf) << 19) |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 422 | | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT) |
| 423 | | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 424 | | ((cke_pls & 0x7) << 6) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 425 | | ((four_act & 0x3f) << 0) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 426 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 427 | debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */ |
| 431 | static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr, |
| 432 | const memctl_options_t *popts, |
| 433 | const common_timing_params_t *common_dimm) |
| 434 | { |
| 435 | unsigned int mem_en; /* DDR SDRAM interface logic enable */ |
| 436 | unsigned int sren; /* Self refresh enable (during sleep) */ |
| 437 | unsigned int ecc_en; /* ECC enable. */ |
| 438 | unsigned int rd_en; /* Registered DIMM enable */ |
| 439 | unsigned int sdram_type; /* Type of SDRAM */ |
| 440 | unsigned int dyn_pwr; /* Dynamic power management mode */ |
| 441 | unsigned int dbw; /* DRAM dta bus width */ |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 442 | unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 443 | unsigned int ncap = 0; /* Non-concurrent auto-precharge */ |
| 444 | unsigned int threeT_en; /* Enable 3T timing */ |
| 445 | unsigned int twoT_en; /* Enable 2T timing */ |
| 446 | unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */ |
| 447 | unsigned int x32_en = 0; /* x32 enable */ |
| 448 | unsigned int pchb8 = 0; /* precharge bit 8 enable */ |
| 449 | unsigned int hse; /* Global half strength override */ |
| 450 | unsigned int mem_halt = 0; /* memory controller halt */ |
| 451 | unsigned int bi = 0; /* Bypass initialization */ |
| 452 | |
| 453 | mem_en = 1; |
| 454 | sren = popts->self_refresh_in_sleep; |
| 455 | if (common_dimm->all_DIMMs_ECC_capable) { |
| 456 | /* Allow setting of ECC only if all DIMMs are ECC. */ |
| 457 | ecc_en = popts->ECC_mode; |
| 458 | } else { |
| 459 | ecc_en = 0; |
| 460 | } |
| 461 | |
| 462 | rd_en = (common_dimm->all_DIMMs_registered |
| 463 | && !common_dimm->all_DIMMs_unbuffered); |
| 464 | |
| 465 | sdram_type = CONFIG_FSL_SDRAM_TYPE; |
| 466 | |
| 467 | dyn_pwr = popts->dynamic_power; |
| 468 | dbw = popts->data_bus_width; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 469 | /* 8-beat burst enable DDR-III case |
| 470 | * we must clear it when use the on-the-fly mode, |
| 471 | * must set it when use the 32-bits bus mode. |
| 472 | */ |
| 473 | if (sdram_type == SDRAM_TYPE_DDR3) { |
| 474 | if (popts->burst_length == DDR_BL8) |
| 475 | eight_be = 1; |
| 476 | if (popts->burst_length == DDR_OTF) |
| 477 | eight_be = 0; |
| 478 | if (dbw == 0x1) |
| 479 | eight_be = 1; |
| 480 | } |
| 481 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 482 | threeT_en = popts->threeT_en; |
| 483 | twoT_en = popts->twoT_en; |
| 484 | ba_intlv_ctl = popts->ba_intlv_ctl; |
| 485 | hse = popts->half_strength_driver_enable; |
| 486 | |
| 487 | ddr->ddr_sdram_cfg = (0 |
| 488 | | ((mem_en & 0x1) << 31) |
| 489 | | ((sren & 0x1) << 30) |
| 490 | | ((ecc_en & 0x1) << 29) |
| 491 | | ((rd_en & 0x1) << 28) |
| 492 | | ((sdram_type & 0x7) << 24) |
| 493 | | ((dyn_pwr & 0x1) << 21) |
| 494 | | ((dbw & 0x3) << 19) |
| 495 | | ((eight_be & 0x1) << 18) |
| 496 | | ((ncap & 0x1) << 17) |
| 497 | | ((threeT_en & 0x1) << 16) |
| 498 | | ((twoT_en & 0x1) << 15) |
| 499 | | ((ba_intlv_ctl & 0x7F) << 8) |
| 500 | | ((x32_en & 0x1) << 5) |
| 501 | | ((pchb8 & 0x1) << 4) |
| 502 | | ((hse & 0x1) << 3) |
| 503 | | ((mem_halt & 0x1) << 1) |
| 504 | | ((bi & 0x1) << 0) |
| 505 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 506 | debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */ |
| 510 | static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr, |
| 511 | const memctl_options_t *popts) |
| 512 | { |
| 513 | unsigned int frc_sr = 0; /* Force self refresh */ |
| 514 | unsigned int sr_ie = 0; /* Self-refresh interrupt enable */ |
| 515 | unsigned int dll_rst_dis; /* DLL reset disable */ |
| 516 | unsigned int dqs_cfg; /* DQS configuration */ |
| 517 | unsigned int odt_cfg; /* ODT configuration */ |
| 518 | unsigned int num_pr; /* Number of posted refreshes */ |
| 519 | unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */ |
| 520 | unsigned int ap_en; /* Address Parity Enable */ |
| 521 | unsigned int d_init; /* DRAM data initialization */ |
| 522 | unsigned int rcw_en = 0; /* Register Control Word Enable */ |
| 523 | unsigned int md_en = 0; /* Mirrored DIMM Enable */ |
| 524 | |
| 525 | dll_rst_dis = 1; /* Make this configurable */ |
| 526 | dqs_cfg = popts->DQS_config; |
| 527 | if (popts->cs_local_opts[0].odt_rd_cfg |
| 528 | || popts->cs_local_opts[0].odt_wr_cfg) { |
| 529 | /* FIXME */ |
| 530 | odt_cfg = 2; |
| 531 | } else { |
| 532 | odt_cfg = 0; |
| 533 | } |
| 534 | |
| 535 | num_pr = 1; /* Make this configurable */ |
| 536 | |
| 537 | /* |
| 538 | * 8572 manual says |
| 539 | * {TIMING_CFG_1[PRETOACT] |
| 540 | * + [DDR_SDRAM_CFG_2[NUM_PR] |
| 541 | * * ({EXT_REFREC || REFREC} + 8 + 2)]} |
| 542 | * << DDR_SDRAM_INTERVAL[REFINT] |
| 543 | */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 544 | #if defined(CONFIG_FSL_DDR3) |
| 545 | obc_cfg = popts->OTF_burst_chop_en; |
| 546 | #else |
| 547 | obc_cfg = 0; |
| 548 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 549 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 550 | ap_en = 0; /* Make this configurable? */ |
| 551 | |
| 552 | #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER) |
| 553 | /* Use the DDR controller to auto initialize memory. */ |
| 554 | d_init = 1; |
| 555 | ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE; |
| 556 | debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init); |
| 557 | #else |
| 558 | /* Memory will be initialized via DMA, or not at all. */ |
| 559 | d_init = 0; |
| 560 | #endif |
| 561 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 562 | #if defined(CONFIG_FSL_DDR3) |
| 563 | md_en = popts->mirrored_dimm; |
| 564 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 565 | ddr->ddr_sdram_cfg_2 = (0 |
| 566 | | ((frc_sr & 0x1) << 31) |
| 567 | | ((sr_ie & 0x1) << 30) |
| 568 | | ((dll_rst_dis & 0x1) << 29) |
| 569 | | ((dqs_cfg & 0x3) << 26) |
| 570 | | ((odt_cfg & 0x3) << 21) |
| 571 | | ((num_pr & 0xf) << 12) |
| 572 | | ((obc_cfg & 0x1) << 6) |
| 573 | | ((ap_en & 0x1) << 5) |
| 574 | | ((d_init & 0x1) << 4) |
| 575 | | ((rcw_en & 0x1) << 2) |
| 576 | | ((md_en & 0x1) << 0) |
| 577 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 578 | 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] | 579 | } |
| 580 | |
| 581 | /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */ |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 582 | static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr, |
| 583 | const memctl_options_t *popts) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 584 | { |
| 585 | unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */ |
| 586 | unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */ |
| 587 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 588 | #if defined(CONFIG_FSL_DDR3) |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 589 | unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 590 | unsigned int srt = 0; /* self-refresh temerature, normal range */ |
| 591 | unsigned int asr = 0; /* auto self-refresh disable */ |
| 592 | unsigned int cwl = compute_cas_write_latency() - 5; |
| 593 | unsigned int pasr = 0; /* partial array self refresh disable */ |
| 594 | |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 595 | if (popts->rtt_override) |
| 596 | rtt_wr = popts->rtt_wr_override_value; |
| 597 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 598 | esdmode2 = (0 |
| 599 | | ((rtt_wr & 0x3) << 9) |
| 600 | | ((srt & 0x1) << 7) |
| 601 | | ((asr & 0x1) << 6) |
| 602 | | ((cwl & 0x7) << 3) |
| 603 | | ((pasr & 0x7) << 0)); |
| 604 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 605 | ddr->ddr_sdram_mode_2 = (0 |
| 606 | | ((esdmode2 & 0xFFFF) << 16) |
| 607 | | ((esdmode3 & 0xFFFF) << 0) |
| 608 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 609 | debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */ |
| 613 | static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr, |
| 614 | const memctl_options_t *popts, |
| 615 | const common_timing_params_t *common_dimm) |
| 616 | { |
| 617 | unsigned int refint; /* Refresh interval */ |
| 618 | unsigned int bstopre; /* Precharge interval */ |
| 619 | |
| 620 | refint = picos_to_mclk(common_dimm->refresh_rate_ps); |
| 621 | |
| 622 | bstopre = popts->bstopre; |
| 623 | |
| 624 | /* refint field used 0x3FFF in earlier controllers */ |
| 625 | ddr->ddr_sdram_interval = (0 |
| 626 | | ((refint & 0xFFFF) << 16) |
| 627 | | ((bstopre & 0x3FFF) << 0) |
| 628 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 629 | debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 630 | } |
| 631 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 632 | #if defined(CONFIG_FSL_DDR3) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 633 | /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */ |
| 634 | static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr, |
| 635 | const memctl_options_t *popts, |
| 636 | const common_timing_params_t *common_dimm, |
| 637 | unsigned int cas_latency, |
| 638 | unsigned int additive_latency) |
| 639 | { |
| 640 | unsigned short esdmode; /* Extended SDRAM mode */ |
| 641 | unsigned short sdmode; /* SDRAM mode */ |
| 642 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 643 | /* Mode Register - MR1 */ |
| 644 | unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */ |
| 645 | unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */ |
| 646 | unsigned int rtt; |
| 647 | unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */ |
| 648 | unsigned int al = 0; /* Posted CAS# additive latency (AL) */ |
| 649 | unsigned int dic = 1; /* Output driver impedance, 34ohm */ |
| 650 | unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal), |
| 651 | 1=Disable (Test/Debug) */ |
| 652 | |
| 653 | /* Mode Register - MR0 */ |
| 654 | unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */ |
| 655 | unsigned int wr; /* Write Recovery */ |
| 656 | unsigned int dll_rst; /* DLL Reset */ |
| 657 | unsigned int mode; /* Normal=0 or Test=1 */ |
| 658 | unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */ |
| 659 | /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */ |
| 660 | unsigned int bt; |
| 661 | unsigned int bl; /* BL: Burst Length */ |
| 662 | |
| 663 | unsigned int wr_mclk; |
| 664 | |
| 665 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 666 | |
| 667 | rtt = fsl_ddr_get_rtt(); |
| 668 | if (popts->rtt_override) |
| 669 | rtt = popts->rtt_override_value; |
| 670 | |
| 671 | if (additive_latency == (cas_latency - 1)) |
| 672 | al = 1; |
| 673 | if (additive_latency == (cas_latency - 2)) |
| 674 | al = 2; |
| 675 | |
| 676 | /* |
| 677 | * The esdmode value will also be used for writing |
| 678 | * MR1 during write leveling for DDR3, although the |
| 679 | * bits specifically related to the write leveling |
| 680 | * scheme will be handled automatically by the DDR |
| 681 | * controller. so we set the wrlvl_en = 0 here. |
| 682 | */ |
| 683 | esdmode = (0 |
| 684 | | ((qoff & 0x1) << 12) |
| 685 | | ((tdqs_en & 0x1) << 11) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 686 | | ((rtt & 0x4) << 7) /* rtt field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 687 | | ((wrlvl_en & 0x1) << 7) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 688 | | ((rtt & 0x2) << 5) /* rtt field is split */ |
| 689 | | ((dic & 0x2) << 4) /* DIC field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 690 | | ((al & 0x3) << 3) |
Kumar Gala | 14f2eb1 | 2009-09-10 14:54:55 -0500 | [diff] [blame] | 691 | | ((rtt & 0x1) << 2) /* rtt field is split */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 692 | | ((dic & 0x1) << 1) /* DIC field is split */ |
| 693 | | ((dll_en & 0x1) << 0) |
| 694 | ); |
| 695 | |
| 696 | /* |
| 697 | * DLL control for precharge PD |
| 698 | * 0=slow exit DLL off (tXPDLL) |
| 699 | * 1=fast exit DLL on (tXP) |
| 700 | */ |
| 701 | dll_on = 1; |
| 702 | wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps; |
| 703 | if (wr_mclk >= 12) |
| 704 | wr = 6; |
| 705 | else if (wr_mclk >= 9) |
| 706 | wr = 5; |
| 707 | else |
| 708 | wr = wr_mclk - 4; |
| 709 | dll_rst = 0; /* dll no reset */ |
| 710 | mode = 0; /* normal mode */ |
| 711 | |
| 712 | /* look up table to get the cas latency bits */ |
| 713 | if (cas_latency >= 5 && cas_latency <= 11) { |
| 714 | unsigned char cas_latency_table[7] = { |
| 715 | 0x2, /* 5 clocks */ |
| 716 | 0x4, /* 6 clocks */ |
| 717 | 0x6, /* 7 clocks */ |
| 718 | 0x8, /* 8 clocks */ |
| 719 | 0xa, /* 9 clocks */ |
| 720 | 0xc, /* 10 clocks */ |
| 721 | 0xe /* 11 clocks */ |
| 722 | }; |
| 723 | caslat = cas_latency_table[cas_latency - 5]; |
| 724 | } |
| 725 | bt = 0; /* Nibble sequential */ |
| 726 | |
| 727 | switch (popts->burst_length) { |
| 728 | case DDR_BL8: |
| 729 | bl = 0; |
| 730 | break; |
| 731 | case DDR_OTF: |
| 732 | bl = 1; |
| 733 | break; |
| 734 | case DDR_BC4: |
| 735 | bl = 2; |
| 736 | break; |
| 737 | default: |
| 738 | printf("Error: invalid burst length of %u specified. " |
| 739 | " Defaulting to on-the-fly BC4 or BL8 beats.\n", |
| 740 | popts->burst_length); |
| 741 | bl = 1; |
| 742 | break; |
| 743 | } |
| 744 | |
| 745 | sdmode = (0 |
| 746 | | ((dll_on & 0x1) << 12) |
| 747 | | ((wr & 0x7) << 9) |
| 748 | | ((dll_rst & 0x1) << 8) |
| 749 | | ((mode & 0x1) << 7) |
| 750 | | (((caslat >> 1) & 0x7) << 4) |
| 751 | | ((bt & 0x1) << 3) |
| 752 | | ((bl & 0x3) << 0) |
| 753 | ); |
| 754 | |
| 755 | ddr->ddr_sdram_mode = (0 |
| 756 | | ((esdmode & 0xFFFF) << 16) |
| 757 | | ((sdmode & 0xFFFF) << 0) |
| 758 | ); |
| 759 | |
| 760 | debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode); |
| 761 | } |
| 762 | |
| 763 | #else /* !CONFIG_FSL_DDR3 */ |
| 764 | |
| 765 | /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */ |
| 766 | static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr, |
| 767 | const memctl_options_t *popts, |
| 768 | const common_timing_params_t *common_dimm, |
| 769 | unsigned int cas_latency, |
| 770 | unsigned int additive_latency) |
| 771 | { |
| 772 | unsigned short esdmode; /* Extended SDRAM mode */ |
| 773 | unsigned short sdmode; /* SDRAM mode */ |
| 774 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 775 | /* |
| 776 | * FIXME: This ought to be pre-calculated in a |
| 777 | * technology-specific routine, |
| 778 | * e.g. compute_DDR2_mode_register(), and then the |
| 779 | * sdmode and esdmode passed in as part of common_dimm. |
| 780 | */ |
| 781 | |
| 782 | /* Extended Mode Register */ |
| 783 | unsigned int mrs = 0; /* Mode Register Set */ |
| 784 | unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */ |
| 785 | unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */ |
| 786 | unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */ |
| 787 | unsigned int ocd = 0; /* 0x0=OCD not supported, |
| 788 | 0x7=OCD default state */ |
| 789 | unsigned int rtt; |
| 790 | unsigned int al; /* Posted CAS# additive latency (AL) */ |
| 791 | unsigned int ods = 0; /* Output Drive Strength: |
| 792 | 0 = Full strength (18ohm) |
| 793 | 1 = Reduced strength (4ohm) */ |
| 794 | unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal), |
| 795 | 1=Disable (Test/Debug) */ |
| 796 | |
| 797 | /* Mode Register (MR) */ |
| 798 | unsigned int mr; /* Mode Register Definition */ |
| 799 | unsigned int pd; /* Power-Down Mode */ |
| 800 | unsigned int wr; /* Write Recovery */ |
| 801 | unsigned int dll_res; /* DLL Reset */ |
| 802 | unsigned int mode; /* Normal=0 or Test=1 */ |
Kumar Gala | 35ad58d | 2008-09-05 14:40:29 -0500 | [diff] [blame] | 803 | unsigned int caslat = 0;/* CAS# latency */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 804 | /* BT: Burst Type (0=Sequential, 1=Interleaved) */ |
| 805 | unsigned int bt; |
| 806 | unsigned int bl; /* BL: Burst Length */ |
| 807 | |
| 808 | #if defined(CONFIG_FSL_DDR2) |
| 809 | const unsigned int mclk_ps = get_memory_clk_period_ps(); |
| 810 | #endif |
| 811 | |
| 812 | rtt = fsl_ddr_get_rtt(); |
| 813 | |
| 814 | al = additive_latency; |
| 815 | |
| 816 | esdmode = (0 |
| 817 | | ((mrs & 0x3) << 14) |
| 818 | | ((outputs & 0x1) << 12) |
| 819 | | ((rdqs_en & 0x1) << 11) |
| 820 | | ((dqs_en & 0x1) << 10) |
| 821 | | ((ocd & 0x7) << 7) |
| 822 | | ((rtt & 0x2) << 5) /* rtt field is split */ |
| 823 | | ((al & 0x7) << 3) |
| 824 | | ((rtt & 0x1) << 2) /* rtt field is split */ |
| 825 | | ((ods & 0x1) << 1) |
| 826 | | ((dll_en & 0x1) << 0) |
| 827 | ); |
| 828 | |
| 829 | mr = 0; /* FIXME: CHECKME */ |
| 830 | |
| 831 | /* |
| 832 | * 0 = Fast Exit (Normal) |
| 833 | * 1 = Slow Exit (Low Power) |
| 834 | */ |
| 835 | pd = 0; |
| 836 | |
| 837 | #if defined(CONFIG_FSL_DDR1) |
| 838 | wr = 0; /* Historical */ |
| 839 | #elif defined(CONFIG_FSL_DDR2) |
| 840 | wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 841 | #endif |
| 842 | dll_res = 0; |
| 843 | mode = 0; |
| 844 | |
| 845 | #if defined(CONFIG_FSL_DDR1) |
| 846 | if (1 <= cas_latency && cas_latency <= 4) { |
| 847 | unsigned char mode_caslat_table[4] = { |
| 848 | 0x5, /* 1.5 clocks */ |
| 849 | 0x2, /* 2.0 clocks */ |
| 850 | 0x6, /* 2.5 clocks */ |
| 851 | 0x3 /* 3.0 clocks */ |
| 852 | }; |
Kumar Gala | 35ad58d | 2008-09-05 14:40:29 -0500 | [diff] [blame] | 853 | caslat = mode_caslat_table[cas_latency - 1]; |
| 854 | } else { |
| 855 | printf("Warning: unknown cas_latency %d\n", cas_latency); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 856 | } |
| 857 | #elif defined(CONFIG_FSL_DDR2) |
| 858 | caslat = cas_latency; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 859 | #endif |
| 860 | bt = 0; |
| 861 | |
| 862 | switch (popts->burst_length) { |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 863 | case DDR_BL4: |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 864 | bl = 2; |
| 865 | break; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 866 | case DDR_BL8: |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 867 | bl = 3; |
| 868 | break; |
| 869 | default: |
| 870 | printf("Error: invalid burst length of %u specified. " |
| 871 | " Defaulting to 4 beats.\n", |
| 872 | popts->burst_length); |
| 873 | bl = 2; |
| 874 | break; |
| 875 | } |
| 876 | |
| 877 | sdmode = (0 |
| 878 | | ((mr & 0x3) << 14) |
| 879 | | ((pd & 0x1) << 12) |
| 880 | | ((wr & 0x7) << 9) |
| 881 | | ((dll_res & 0x1) << 8) |
| 882 | | ((mode & 0x1) << 7) |
| 883 | | ((caslat & 0x7) << 4) |
| 884 | | ((bt & 0x1) << 3) |
| 885 | | ((bl & 0x7) << 0) |
| 886 | ); |
| 887 | |
| 888 | ddr->ddr_sdram_mode = (0 |
| 889 | | ((esdmode & 0xFFFF) << 16) |
| 890 | | ((sdmode & 0xFFFF) << 0) |
| 891 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 892 | debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 893 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 894 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 895 | |
| 896 | /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */ |
| 897 | static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr) |
| 898 | { |
| 899 | unsigned int init_value; /* Initialization value */ |
| 900 | |
| 901 | init_value = 0xDEADBEEF; |
| 902 | ddr->ddr_data_init = init_value; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL) |
| 907 | * The old controller on the 8540/60 doesn't have this register. |
| 908 | * Hope it's OK to set it (to 0) anyway. |
| 909 | */ |
| 910 | static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr, |
| 911 | const memctl_options_t *popts) |
| 912 | { |
| 913 | unsigned int clk_adjust; /* Clock adjust */ |
| 914 | |
| 915 | clk_adjust = popts->clk_adjust; |
| 916 | ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23; |
| 917 | } |
| 918 | |
| 919 | /* DDR Initialization Address (DDR_INIT_ADDR) */ |
| 920 | static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr) |
| 921 | { |
| 922 | unsigned int init_addr = 0; /* Initialization address */ |
| 923 | |
| 924 | ddr->ddr_init_addr = init_addr; |
| 925 | } |
| 926 | |
| 927 | /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */ |
| 928 | static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr) |
| 929 | { |
| 930 | unsigned int uia = 0; /* Use initialization address */ |
| 931 | unsigned int init_ext_addr = 0; /* Initialization address */ |
| 932 | |
| 933 | ddr->ddr_init_ext_addr = (0 |
| 934 | | ((uia & 0x1) << 31) |
| 935 | | (init_ext_addr & 0xF) |
| 936 | ); |
| 937 | } |
| 938 | |
| 939 | /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */ |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 940 | static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr, |
| 941 | const memctl_options_t *popts) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 942 | { |
| 943 | unsigned int rwt = 0; /* Read-to-write turnaround for same CS */ |
| 944 | unsigned int wrt = 0; /* Write-to-read turnaround for same CS */ |
| 945 | unsigned int rrt = 0; /* Read-to-read turnaround for same CS */ |
| 946 | unsigned int wwt = 0; /* Write-to-write turnaround for same CS */ |
| 947 | unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */ |
| 948 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 949 | #if defined(CONFIG_FSL_DDR3) |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 950 | if (popts->burst_length == DDR_BL8) { |
| 951 | /* We set BL/2 for fixed BL8 */ |
| 952 | rrt = 0; /* BL/2 clocks */ |
| 953 | wwt = 0; /* BL/2 clocks */ |
| 954 | } else { |
| 955 | /* We need to set BL/2 + 2 to BC4 and OTF */ |
| 956 | rrt = 2; /* BL/2 + 2 clocks */ |
| 957 | wwt = 2; /* BL/2 + 2 clocks */ |
| 958 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 959 | dll_lock = 1; /* tDLLK = 512 clocks from spec */ |
| 960 | #endif |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 961 | ddr->timing_cfg_4 = (0 |
| 962 | | ((rwt & 0xf) << 28) |
| 963 | | ((wrt & 0xf) << 24) |
| 964 | | ((rrt & 0xf) << 20) |
| 965 | | ((wwt & 0xf) << 16) |
| 966 | | (dll_lock & 0x3) |
| 967 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 968 | debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */ |
| 972 | static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr) |
| 973 | { |
| 974 | unsigned int rodt_on = 0; /* Read to ODT on */ |
| 975 | unsigned int rodt_off = 0; /* Read to ODT off */ |
| 976 | unsigned int wodt_on = 0; /* Write to ODT on */ |
| 977 | unsigned int wodt_off = 0; /* Write to ODT off */ |
| 978 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 979 | #if defined(CONFIG_FSL_DDR3) |
| 980 | rodt_on = 3; /* 2 clocks */ |
| 981 | rodt_off = 4; /* 4 clocks */ |
| 982 | wodt_on = 2; /* 1 clocks */ |
| 983 | wodt_off = 4; /* 4 clocks */ |
| 984 | #endif |
| 985 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 986 | ddr->timing_cfg_5 = (0 |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 987 | | ((rodt_on & 0x1f) << 24) |
| 988 | | ((rodt_off & 0x7) << 20) |
| 989 | | ((wodt_on & 0x1f) << 12) |
| 990 | | ((wodt_off & 0x7) << 8) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 991 | ); |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 992 | debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 996 | 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] | 997 | { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 998 | unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */ |
| 999 | /* Normal Operation Full Calibration Time (tZQoper) */ |
| 1000 | unsigned int zqoper = 0; |
| 1001 | /* Normal Operation Short Calibration Time (tZQCS) */ |
| 1002 | unsigned int zqcs = 0; |
| 1003 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1004 | if (zq_en) { |
| 1005 | zqinit = 9; /* 512 clocks */ |
| 1006 | zqoper = 8; /* 256 clocks */ |
| 1007 | zqcs = 6; /* 64 clocks */ |
| 1008 | } |
| 1009 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1010 | ddr->ddr_zq_cntl = (0 |
| 1011 | | ((zq_en & 0x1) << 31) |
| 1012 | | ((zqinit & 0xF) << 24) |
| 1013 | | ((zqoper & 0xF) << 16) |
| 1014 | | ((zqcs & 0xF) << 8) |
| 1015 | ); |
| 1016 | } |
| 1017 | |
| 1018 | /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */ |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 1019 | static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en, |
| 1020 | const memctl_options_t *popts) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1021 | { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1022 | /* |
| 1023 | * First DQS pulse rising edge after margining mode |
| 1024 | * is programmed (tWL_MRD) |
| 1025 | */ |
| 1026 | unsigned int wrlvl_mrd = 0; |
| 1027 | /* ODT delay after margining mode is programmed (tWL_ODTEN) */ |
| 1028 | unsigned int wrlvl_odten = 0; |
| 1029 | /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */ |
| 1030 | unsigned int wrlvl_dqsen = 0; |
| 1031 | /* WRLVL_SMPL: Write leveling sample time */ |
| 1032 | unsigned int wrlvl_smpl = 0; |
| 1033 | /* WRLVL_WLR: Write leveling repeition time */ |
| 1034 | unsigned int wrlvl_wlr = 0; |
| 1035 | /* WRLVL_START: Write leveling start time */ |
| 1036 | unsigned int wrlvl_start = 0; |
| 1037 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1038 | /* suggest enable write leveling for DDR3 due to fly-by topology */ |
| 1039 | if (wrlvl_en) { |
| 1040 | /* tWL_MRD min = 40 nCK, we set it 64 */ |
| 1041 | wrlvl_mrd = 0x6; |
| 1042 | /* tWL_ODTEN 128 */ |
| 1043 | wrlvl_odten = 0x7; |
| 1044 | /* tWL_DQSEN min = 25 nCK, we set it 32 */ |
| 1045 | wrlvl_dqsen = 0x5; |
| 1046 | /* |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 1047 | * Write leveling sample time at least need 6 clocks |
| 1048 | * higher than tWLO to allow enough time for progagation |
| 1049 | * delay and sampling the prime data bits. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1050 | */ |
| 1051 | wrlvl_smpl = 0xf; |
| 1052 | /* |
| 1053 | * Write leveling repetition time |
| 1054 | * at least tWLO + 6 clocks clocks |
| 1055 | * we set it 32 |
| 1056 | */ |
| 1057 | wrlvl_wlr = 0x5; |
| 1058 | /* |
| 1059 | * Write leveling start time |
| 1060 | * The value use for the DQS_ADJUST for the first sample |
| 1061 | * when write leveling is enabled. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1062 | */ |
| 1063 | wrlvl_start = 0x8; |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 1064 | /* |
| 1065 | * Override the write leveling sample and start time |
| 1066 | * according to specific board |
| 1067 | */ |
| 1068 | if (popts->wrlvl_override) { |
| 1069 | wrlvl_smpl = popts->wrlvl_sample; |
| 1070 | wrlvl_start = popts->wrlvl_start; |
| 1071 | } |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1074 | ddr->ddr_wrlvl_cntl = (0 |
| 1075 | | ((wrlvl_en & 0x1) << 31) |
| 1076 | | ((wrlvl_mrd & 0x7) << 24) |
| 1077 | | ((wrlvl_odten & 0x7) << 20) |
| 1078 | | ((wrlvl_dqsen & 0x7) << 16) |
| 1079 | | ((wrlvl_smpl & 0xf) << 12) |
| 1080 | | ((wrlvl_wlr & 0x7) << 8) |
Dave Liu | 4758d53 | 2008-11-21 16:31:29 +0800 | [diff] [blame] | 1081 | | ((wrlvl_start & 0x1F) << 0) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1082 | ); |
| 1083 | } |
| 1084 | |
| 1085 | /* DDR Self Refresh Counter (DDR_SR_CNTR) */ |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1086 | 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] | 1087 | { |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1088 | /* Self Refresh Idle Threshold */ |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1089 | ddr->ddr_sr_cntr = (sr_it & 0xF) << 16; |
| 1090 | } |
| 1091 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1092 | /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */ |
| 1093 | static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr) |
| 1094 | { |
| 1095 | unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */ |
| 1096 | unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */ |
| 1097 | unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */ |
| 1098 | unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */ |
| 1099 | unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */ |
| 1100 | unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */ |
| 1101 | unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */ |
| 1102 | unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */ |
| 1103 | |
| 1104 | ddr->ddr_sdram_rcw_1 = (0 |
| 1105 | | ((rcw0 & 0xF) << 28) |
| 1106 | | ((rcw1 & 0xF) << 24) |
| 1107 | | ((rcw2 & 0xF) << 20) |
| 1108 | | ((rcw3 & 0xF) << 16) |
| 1109 | | ((rcw4 & 0xF) << 12) |
| 1110 | | ((rcw5 & 0xF) << 8) |
| 1111 | | ((rcw6 & 0xF) << 4) |
| 1112 | | ((rcw7 & 0xF) << 0) |
| 1113 | ); |
| 1114 | } |
| 1115 | |
| 1116 | /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */ |
| 1117 | static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr) |
| 1118 | { |
| 1119 | unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */ |
| 1120 | unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */ |
| 1121 | unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */ |
| 1122 | unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */ |
| 1123 | unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */ |
| 1124 | unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */ |
| 1125 | unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */ |
| 1126 | unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */ |
| 1127 | |
| 1128 | ddr->ddr_sdram_rcw_2 = (0 |
| 1129 | | ((rcw8 & 0xF) << 28) |
| 1130 | | ((rcw9 & 0xF) << 24) |
| 1131 | | ((rcw10 & 0xF) << 20) |
| 1132 | | ((rcw11 & 0xF) << 16) |
| 1133 | | ((rcw12 & 0xF) << 12) |
| 1134 | | ((rcw13 & 0xF) << 8) |
| 1135 | | ((rcw14 & 0xF) << 4) |
| 1136 | | ((rcw15 & 0xF) << 0) |
| 1137 | ); |
| 1138 | } |
| 1139 | |
| 1140 | unsigned int |
| 1141 | check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr) |
| 1142 | { |
| 1143 | unsigned int res = 0; |
| 1144 | |
| 1145 | /* |
| 1146 | * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are |
| 1147 | * not set at the same time. |
| 1148 | */ |
| 1149 | if (ddr->ddr_sdram_cfg & 0x10000000 |
| 1150 | && ddr->ddr_sdram_cfg & 0x00008000) { |
| 1151 | printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] " |
| 1152 | " should not be set at the same time.\n"); |
| 1153 | res++; |
| 1154 | } |
| 1155 | |
| 1156 | return res; |
| 1157 | } |
| 1158 | |
| 1159 | unsigned int |
| 1160 | compute_fsl_memctl_config_regs(const memctl_options_t *popts, |
| 1161 | fsl_ddr_cfg_regs_t *ddr, |
| 1162 | const common_timing_params_t *common_dimm, |
| 1163 | const dimm_params_t *dimm_params, |
| 1164 | unsigned int dbw_cap_adj) |
| 1165 | { |
| 1166 | unsigned int i; |
| 1167 | unsigned int cas_latency; |
| 1168 | unsigned int additive_latency; |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1169 | unsigned int sr_it; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1170 | unsigned int zq_en; |
| 1171 | unsigned int wrlvl_en; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1172 | |
| 1173 | memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t)); |
| 1174 | |
| 1175 | if (common_dimm == NULL) { |
| 1176 | printf("Error: subset DIMM params struct null pointer\n"); |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Process overrides first. |
| 1182 | * |
| 1183 | * FIXME: somehow add dereated caslat to this |
| 1184 | */ |
| 1185 | cas_latency = (popts->cas_latency_override) |
| 1186 | ? popts->cas_latency_override_value |
| 1187 | : common_dimm->lowest_common_SPD_caslat; |
| 1188 | |
| 1189 | additive_latency = (popts->additive_latency_override) |
| 1190 | ? popts->additive_latency_override_value |
| 1191 | : common_dimm->additive_latency; |
| 1192 | |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1193 | sr_it = (popts->auto_self_refresh_en) |
| 1194 | ? popts->sr_it |
| 1195 | : 0; |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1196 | /* ZQ calibration */ |
| 1197 | zq_en = (popts->zq_en) ? 1 : 0; |
| 1198 | /* write leveling */ |
| 1199 | wrlvl_en = (popts->wrlvl_en) ? 1 : 0; |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1200 | |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1201 | /* Chip Select Memory Bounds (CSn_BNDS) */ |
| 1202 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
Kumar Gala | 68ef4bd | 2009-06-11 23:42:35 -0500 | [diff] [blame] | 1203 | unsigned long long ea = 0, sa = 0; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1204 | unsigned int cs_per_dimm |
| 1205 | = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR; |
| 1206 | unsigned int dimm_number |
| 1207 | = i / cs_per_dimm; |
| 1208 | unsigned long long rank_density |
| 1209 | = dimm_params[dimm_number].rank_density; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1210 | |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1211 | if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) || |
| 1212 | ((i == 2) && (popts->ba_intlv_ctl & 0x04)) || |
| 1213 | ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) { |
| 1214 | /* |
| 1215 | * Don't set up boundaries for unused CS |
| 1216 | * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3 |
| 1217 | * cs2 for cs0_cs1_cs2_cs3 |
| 1218 | * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3 |
Dave Liu | 625b268 | 2009-12-16 10:24:39 -0600 | [diff] [blame] | 1219 | * But we need to set the ODT_RD_CFG and |
| 1220 | * ODT_WR_CFG for CS1_CONFIG here. |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1221 | */ |
Dave Liu | 625b268 | 2009-12-16 10:24:39 -0600 | [diff] [blame] | 1222 | set_csn_config(i, ddr, popts, dimm_params); |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1223 | continue; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1224 | } |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1225 | if (dimm_params[dimm_number].n_ranks == 0) { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1226 | debug("Skipping setup of CS%u " |
| 1227 | "because n_ranks on DIMM %u is 0\n", i, i/2); |
| 1228 | continue; |
| 1229 | } |
| 1230 | if (popts->memctl_interleaving && popts->ba_intlv_ctl) { |
| 1231 | /* |
| 1232 | * This works superbank 2CS |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1233 | * There are 2 or more memory controllers configured |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1234 | * identically, memory is interleaved between them, |
| 1235 | * and each controller uses rank interleaving within |
| 1236 | * itself. Therefore the starting and ending address |
| 1237 | * on each controller is twice the amount present on |
| 1238 | * each controller. |
| 1239 | */ |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1240 | unsigned long long ctlr_density = 0; |
| 1241 | switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) { |
| 1242 | case FSL_DDR_CS0_CS1: |
| 1243 | case FSL_DDR_CS0_CS1_AND_CS2_CS3: |
| 1244 | ctlr_density = dimm_params[0].rank_density * 2; |
| 1245 | break; |
| 1246 | case FSL_DDR_CS2_CS3: |
| 1247 | ctlr_density = dimm_params[0].rank_density; |
| 1248 | break; |
| 1249 | case FSL_DDR_CS0_CS1_CS2_CS3: |
| 1250 | /* |
| 1251 | * The four CS interleaving should have been verified by |
| 1252 | * populate_memctl_options() |
| 1253 | */ |
| 1254 | ctlr_density = dimm_params[0].rank_density * 4; |
| 1255 | break; |
| 1256 | default: |
| 1257 | break; |
| 1258 | } |
| 1259 | ea = (CONFIG_NUM_DDR_CONTROLLERS * |
| 1260 | (ctlr_density >> dbw_cap_adj)) - 1; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1261 | } |
| 1262 | else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) { |
| 1263 | /* |
| 1264 | * If memory interleaving between controllers is NOT |
| 1265 | * enabled, the starting address for each memory |
| 1266 | * controller is distinct. However, because rank |
| 1267 | * interleaving is enabled, the starting and ending |
| 1268 | * addresses of the total memory on that memory |
| 1269 | * controller needs to be programmed into its |
| 1270 | * respective CS0_BNDS. |
| 1271 | */ |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1272 | switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) { |
| 1273 | case FSL_DDR_CS0_CS1_CS2_CS3: |
| 1274 | /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS |
| 1275 | * needs to be set. |
| 1276 | */ |
| 1277 | sa = common_dimm->base_address; |
| 1278 | ea = sa + (4 * (rank_density >> dbw_cap_adj))-1; |
| 1279 | break; |
| 1280 | case FSL_DDR_CS0_CS1_AND_CS2_CS3: |
| 1281 | /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS |
| 1282 | * and CS2_CNDS need to be set. |
| 1283 | */ |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1284 | if ((i == 2) && (dimm_number == 0)) { |
| 1285 | sa = dimm_params[dimm_number].base_address + |
| 1286 | 2 * (rank_density >> dbw_cap_adj); |
| 1287 | ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1; |
| 1288 | } else { |
| 1289 | sa = dimm_params[dimm_number].base_address; |
| 1290 | ea = sa + (2 * (rank_density >> |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1291 | dbw_cap_adj)) - 1; |
| 1292 | } |
| 1293 | break; |
| 1294 | case FSL_DDR_CS0_CS1: |
| 1295 | /* CS0+CS1 interleaving, CS0_CNDS needs |
| 1296 | * to be set |
| 1297 | */ |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1298 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 1299 | sa = dimm_params[dimm_number].base_address; |
| 1300 | ea = sa + (rank_density >> dbw_cap_adj) - 1; |
| 1301 | sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1302 | ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1303 | } else { |
| 1304 | sa = 0; |
| 1305 | ea = 0; |
| 1306 | } |
| 1307 | if (i == 0) |
| 1308 | ea += (rank_density >> dbw_cap_adj); |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1309 | break; |
| 1310 | case FSL_DDR_CS2_CS3: |
| 1311 | /* CS2+CS3 interleaving*/ |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1312 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 1313 | sa = dimm_params[dimm_number].base_address; |
| 1314 | ea = sa + (rank_density >> dbw_cap_adj) - 1; |
| 1315 | sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1316 | ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1317 | } else { |
| 1318 | sa = 0; |
| 1319 | ea = 0; |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1320 | } |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1321 | if (i == 2) |
| 1322 | ea += (rank_density >> dbw_cap_adj); |
Haiying Wang | 272b596 | 2008-10-03 12:36:39 -0400 | [diff] [blame] | 1323 | break; |
| 1324 | default: /* No bank(chip-select) interleaving */ |
| 1325 | break; |
| 1326 | } |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1327 | } |
| 1328 | else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) { |
| 1329 | /* |
| 1330 | * Only the rank on CS0 of each memory controller may |
| 1331 | * be used if memory controller interleaving is used |
| 1332 | * without rank interleaving within each memory |
| 1333 | * controller. However, the ending address programmed |
| 1334 | * into each CS0 must be the sum of the amount of |
| 1335 | * memory in the two CS0 ranks. |
| 1336 | */ |
| 1337 | if (i == 0) { |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1338 | ea = (2 * (rank_density >> dbw_cap_adj)) - 1; |
| 1339 | } |
| 1340 | |
| 1341 | } |
| 1342 | else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) { |
| 1343 | /* |
| 1344 | * No rank interleaving and no memory controller |
| 1345 | * interleaving. |
| 1346 | */ |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1347 | sa = dimm_params[dimm_number].base_address; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1348 | ea = sa + (rank_density >> dbw_cap_adj) - 1; |
york | 93799ca | 2010-07-02 22:25:52 +0000 | [diff] [blame^] | 1349 | if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) { |
| 1350 | sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1351 | ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj); |
| 1352 | } else { |
| 1353 | sa = 0; |
| 1354 | ea = 0; |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | sa >>= 24; |
| 1359 | ea >>= 24; |
| 1360 | |
| 1361 | ddr->cs[i].bnds = (0 |
| 1362 | | ((sa & 0xFFF) << 16) /* starting address MSB */ |
| 1363 | | ((ea & 0xFFF) << 0) /* ending address MSB */ |
| 1364 | ); |
| 1365 | |
Haiying Wang | d90e040 | 2008-10-03 12:37:26 -0400 | [diff] [blame] | 1366 | debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1367 | set_csn_config(i, ddr, popts, dimm_params); |
| 1368 | set_csn_config_2(i, ddr); |
| 1369 | } |
| 1370 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1371 | #if !defined(CONFIG_FSL_DDR1) |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1372 | set_timing_cfg_0(ddr); |
| 1373 | #endif |
| 1374 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1375 | set_timing_cfg_3(ddr, common_dimm, cas_latency); |
| 1376 | set_timing_cfg_1(ddr, popts, common_dimm, cas_latency); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1377 | set_timing_cfg_2(ddr, popts, common_dimm, |
| 1378 | cas_latency, additive_latency); |
| 1379 | |
| 1380 | set_ddr_sdram_cfg(ddr, popts, common_dimm); |
| 1381 | |
| 1382 | set_ddr_sdram_cfg_2(ddr, popts); |
| 1383 | set_ddr_sdram_mode(ddr, popts, common_dimm, |
| 1384 | cas_latency, additive_latency); |
Dave Liu | 2d0f125 | 2009-12-16 10:24:38 -0600 | [diff] [blame] | 1385 | set_ddr_sdram_mode_2(ddr, popts); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1386 | set_ddr_sdram_interval(ddr, popts, common_dimm); |
| 1387 | set_ddr_data_init(ddr); |
| 1388 | set_ddr_sdram_clk_cntl(ddr, popts); |
| 1389 | set_ddr_init_addr(ddr); |
| 1390 | set_ddr_init_ext_addr(ddr); |
Dave Liu | 3525e1a | 2010-03-05 12:22:00 +0800 | [diff] [blame] | 1391 | set_timing_cfg_4(ddr, popts); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1392 | set_timing_cfg_5(ddr); |
| 1393 | |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 1394 | set_ddr_zq_cntl(ddr, zq_en); |
Dave Liu | 64ee7df | 2009-12-16 10:24:37 -0600 | [diff] [blame] | 1395 | set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1396 | |
Dave Liu | 2aad0ae | 2008-11-21 16:31:35 +0800 | [diff] [blame] | 1397 | set_ddr_sr_cntr(ddr, sr_it); |
Kumar Gala | 124b082 | 2008-08-26 15:01:29 -0500 | [diff] [blame] | 1398 | |
| 1399 | set_ddr_sdram_rcw_1(ddr); |
| 1400 | set_ddr_sdram_rcw_2(ddr); |
| 1401 | |
| 1402 | return check_fsl_memctl_config_regs(ddr); |
| 1403 | } |