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