Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011-2013 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * @file ddr3_util.h |
| 22 | * |
| 23 | * \brief Utilities for decoding DDR3 SPDs |
| 24 | */ |
| 25 | |
| 26 | #include <console/console.h> |
| 27 | #include <device/device.h> |
| 28 | #include <device/dram/ddr3.h> |
| 29 | |
Alexandru Gagniuc | 78706fd | 2013-06-03 13:58:10 -0500 | [diff] [blame] | 30 | /*============================================================================== |
| 31 | * = DDR3 SPD decoding helpers |
| 32 | *----------------------------------------------------------------------------*/ |
| 33 | |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 34 | /** |
| 35 | * \brief Checks if the DIMM is Registered based on byte[3] of the SPD |
| 36 | * |
| 37 | * Tells if the DIMM type is registered or not. |
| 38 | * |
| 39 | * @param type DIMM type. This is byte[3] of the SPD. |
| 40 | */ |
| 41 | int dimm_is_registered(enum spd_dimm_type type) |
| 42 | { |
| 43 | if ((type == SPD_DIMM_TYPE_RDIMM) |
| 44 | | (type == SPD_DIMM_TYPE_MINI_RDIMM) |
| 45 | | (type == SPD_DIMM_TYPE_72B_SO_RDIMM)) |
| 46 | return 1; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /** |
Alexandru Gagniuc | 4c37e58 | 2013-12-17 13:08:01 -0500 | [diff] [blame] | 52 | * \brief Calculate the CRC of a DDR3 SPD |
| 53 | * |
| 54 | * @param spd pointer to raw SPD data |
| 55 | * @param len length of data in SPD |
| 56 | * |
| 57 | * @return the CRC of the SPD data, or 0 when spd data is truncated. |
| 58 | */ |
| 59 | u16 spd_ddr3_calc_crc(u8 *spd, int len) |
| 60 | { |
| 61 | int n_crc, i; |
| 62 | u8 *ptr; |
| 63 | u16 crc; |
| 64 | |
| 65 | /* Find the number of bytes covered by CRC */ |
| 66 | if (spd[0] & 0x80) { |
| 67 | n_crc = 117; |
| 68 | } else { |
| 69 | n_crc = 126; |
| 70 | } |
| 71 | |
| 72 | if (len < n_crc) |
| 73 | /* Not enough bytes available to get the CRC */ |
| 74 | return 0; |
| 75 | |
| 76 | /* Compute the CRC */ |
| 77 | crc = 0; |
| 78 | ptr = spd; |
| 79 | while (--n_crc >= 0) { |
| 80 | crc = crc ^ (int)*ptr++ << 8; |
| 81 | for (i = 0; i < 8; ++i) |
| 82 | if (crc & 0x8000) { |
| 83 | crc = crc << 1 ^ 0x1021; |
| 84 | } else { |
| 85 | crc = crc << 1; |
| 86 | } |
| 87 | } |
| 88 | return crc; |
| 89 | } |
| 90 | |
| 91 | /** |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 92 | * \brief Decode the raw SPD data |
| 93 | * |
| 94 | * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a |
| 95 | * @ref dimm_attr structure. The SPD data must first be read in a contiguous |
| 96 | * array, and passed to this function. |
| 97 | * |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 98 | * @param dimm pointer to @ref dimm_attr structure where the decoded data is to |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 99 | * be stored |
| 100 | * @param spd array of raw data previously read from the SPD. |
| 101 | * |
| 102 | * @return @ref spd_status enumerator |
| 103 | * SPD_STATUS_OK -- decoding was successful |
| 104 | * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD |
| 105 | * SPD_STATUS_CRC_ERROR -- CRC did not verify |
| 106 | * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was |
| 107 | * detected. |
| 108 | */ |
| 109 | int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd) |
| 110 | { |
Alexandru Gagniuc | 4c37e58 | 2013-12-17 13:08:01 -0500 | [diff] [blame] | 111 | int ret; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 112 | u16 crc, spd_crc; |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 113 | u8 ftb_divisor, ftb_dividend, capacity_shift, bus_width; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 114 | u8 reg8; |
| 115 | u32 mtb; /* medium time base */ |
| 116 | unsigned int val, param; |
| 117 | |
| 118 | ret = SPD_STATUS_OK; |
| 119 | |
| 120 | /* Don't assume we memset 0 dimm struct. Clear all our flags */ |
| 121 | dimm->flags.raw = 0; |
| 122 | /* Make sure that the SPD dump is indeed from a DDR3 module */ |
| 123 | if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) { |
| 124 | printram("Not a DDR3 SPD!\n"); |
| 125 | dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED; |
| 126 | return SPD_STATUS_INVALID; |
| 127 | } |
| 128 | dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3; |
| 129 | |
Edward O'Callaghan | 31cef1f | 2014-11-08 09:35:31 +1100 | [diff] [blame] | 130 | crc = spd_ddr3_calc_crc(spd, sizeof(*spd)); |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 131 | /* Compare with the CRC in the SPD */ |
| 132 | spd_crc = (spd[127] << 8) + spd[126]; |
| 133 | /* Verify the CRC is correct */ |
| 134 | if (crc != spd_crc) { |
| 135 | printram("ERROR: SPD CRC failed!!!"); |
| 136 | ret = SPD_STATUS_CRC_ERROR; |
| 137 | }; |
| 138 | |
| 139 | printram(" Revision: %x\n", spd[1]); |
| 140 | printram(" Type : %x\n", spd[2]); |
| 141 | printram(" Key : %x\n", spd[3]); |
| 142 | |
| 143 | reg8 = spd[4]; |
| 144 | /* Number of memory banks */ |
| 145 | val = (reg8 >> 4) & 0x07; |
| 146 | if (val > 0x03) { |
| 147 | printram(" Invalid number of memory banks\n"); |
| 148 | ret = SPD_STATUS_INVALID_FIELD; |
| 149 | } |
| 150 | param = 1 << (val + 3); |
| 151 | printram(" Banks : %u\n", param); |
| 152 | /* SDRAM capacity */ |
| 153 | capacity_shift = reg8 & 0x0f; |
| 154 | if (capacity_shift > 0x06) { |
| 155 | printram(" Invalid module capacity\n"); |
| 156 | ret = SPD_STATUS_INVALID_FIELD; |
| 157 | } |
| 158 | if (capacity_shift < 0x02) { |
| 159 | printram(" Capacity: %u Mb\n", 256 << capacity_shift); |
| 160 | } else { |
| 161 | printram(" Capacity: %u Gb\n", 1 << (capacity_shift - 2)); |
| 162 | } |
| 163 | |
| 164 | reg8 = spd[5]; |
| 165 | /* Row address bits */ |
| 166 | val = (reg8 >> 3) & 0x07; |
| 167 | if (val > 0x04) { |
| 168 | printram(" Invalid row address bits\n"); |
| 169 | ret = SPD_STATUS_INVALID_FIELD; |
| 170 | } |
| 171 | dimm->row_bits = val + 12; |
| 172 | /* Column address bits */ |
| 173 | val = reg8 & 0x07; |
| 174 | if (val > 0x03) { |
| 175 | printram(" Invalid column address bits\n"); |
| 176 | ret = SPD_STATUS_INVALID_FIELD; |
| 177 | } |
| 178 | dimm->col_bits = val + 9; |
| 179 | |
| 180 | /* Module nominal voltage */ |
| 181 | reg8 = spd[6]; |
| 182 | printram(" Supported voltages:"); |
| 183 | if (reg8 & (1 << 2)) { |
| 184 | dimm->flags.operable_1_25V = 1; |
| 185 | printram(" 1.25V"); |
| 186 | } |
| 187 | if (reg8 & (1 << 1)) { |
| 188 | dimm->flags.operable_1_35V = 1; |
| 189 | printram(" 1.35V"); |
| 190 | } |
| 191 | if (!(reg8 & (1 << 0))) { |
| 192 | dimm->flags.operable_1_50V = 1; |
| 193 | printram(" 1.5V"); |
| 194 | } |
| 195 | printram("\n"); |
| 196 | |
| 197 | /* Module organization */ |
| 198 | reg8 = spd[7]; |
| 199 | /* Number of ranks */ |
| 200 | val = (reg8 >> 3) & 0x07; |
| 201 | if (val > 3) { |
| 202 | printram(" Invalid number of ranks\n"); |
| 203 | ret = SPD_STATUS_INVALID_FIELD; |
| 204 | } |
| 205 | dimm->ranks = val + 1; |
| 206 | /* SDRAM device width */ |
| 207 | val = (reg8 & 0x07); |
| 208 | if (val > 3) { |
| 209 | printram(" Invalid SDRAM width\n"); |
| 210 | ret = SPD_STATUS_INVALID_FIELD; |
| 211 | } |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 212 | dimm->width = (4 << val); |
| 213 | printram(" SDRAM width : %u\n", dimm->width); |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 214 | |
| 215 | /* Memory bus width */ |
| 216 | reg8 = spd[8]; |
| 217 | /* Bus extension */ |
| 218 | val = (reg8 >> 3) & 0x03; |
| 219 | if (val > 1) { |
| 220 | printram(" Invalid bus extension\n"); |
| 221 | ret = SPD_STATUS_INVALID_FIELD; |
| 222 | } |
| 223 | dimm->flags.is_ecc = val ? 1 : 0; |
| 224 | printram(" Bus extension : %u bits\n", val ? 8 : 0); |
| 225 | /* Bus width */ |
| 226 | val = reg8 & 0x07; |
| 227 | if (val > 3) { |
| 228 | printram(" Invalid bus width\n"); |
| 229 | ret = SPD_STATUS_INVALID_FIELD; |
| 230 | } |
| 231 | bus_width = 8 << val; |
| 232 | printram(" Bus width : %u\n", bus_width); |
| 233 | |
| 234 | /* We have all the info we need to compute the dimm size */ |
| 235 | /* Capacity is 256Mbit multiplied by the power of 2 specified in |
| 236 | * capacity_shift |
| 237 | * The rest is the JEDEC formula */ |
| 238 | dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 239 | * dimm->ranks) / dimm->width; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 240 | |
| 241 | /* Fine Timebase (FTB) Dividend/Divisor */ |
| 242 | /* Dividend */ |
| 243 | ftb_dividend = (spd[9] >> 4) & 0x0f; |
| 244 | /* Divisor */ |
| 245 | ftb_divisor = spd[9] & 0x0f; |
| 246 | |
| 247 | /* Medium Timebase = |
| 248 | * Medium Timebase (MTB) Dividend / |
| 249 | * Medium Timebase (MTB) Divisor */ |
| 250 | mtb = (((u32) spd[10]) << 8) / spd[11]; |
| 251 | |
| 252 | /* SDRAM Minimum Cycle Time (tCKmin) */ |
| 253 | dimm->tCK = spd[12] * mtb; |
| 254 | /* CAS Latencies Supported */ |
| 255 | dimm->cas_supported = (spd[15] << 8) + spd[14]; |
| 256 | /* Minimum CAS Latency Time (tAAmin) */ |
| 257 | dimm->tAA = spd[16] * mtb; |
| 258 | /* Minimum Write Recovery Time (tWRmin) */ |
| 259 | dimm->tWR = spd[17] * mtb; |
| 260 | /* Minimum RAS# to CAS# Delay Time (tRCDmin) */ |
| 261 | dimm->tRCD = spd[18] * mtb; |
| 262 | /* Minimum Row Active to Row Active Delay Time (tRRDmin) */ |
| 263 | dimm->tRRD = spd[19] * mtb; |
| 264 | /* Minimum Row Precharge Delay Time (tRPmin) */ |
| 265 | dimm->tRP = spd[20] * mtb; |
| 266 | /* Minimum Active to Precharge Delay Time (tRASmin) */ |
| 267 | dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb; |
| 268 | /* Minimum Active to Active/Refresh Delay Time (tRCmin) */ |
| 269 | dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb; |
| 270 | /* Minimum Refresh Recovery Delay Time (tRFCmin) */ |
| 271 | dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb; |
| 272 | /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */ |
| 273 | dimm->tWTR = spd[26] * mtb; |
| 274 | /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */ |
| 275 | dimm->tRTP = spd[27] * mtb; |
| 276 | /* Minimum Four Activate Window Delay Time (tFAWmin) */ |
| 277 | dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb; |
| 278 | |
| 279 | /* SDRAM Optional Features */ |
| 280 | reg8 = spd[30]; |
| 281 | printram(" Optional features :"); |
| 282 | if (reg8 & 0x80) { |
| 283 | dimm->flags.dll_off_mode = 1; |
| 284 | printram(" DLL-Off_mode"); |
| 285 | } |
| 286 | if (reg8 & 0x02) { |
| 287 | dimm->flags.rzq7_supported = 1; |
| 288 | printram(" RZQ/7"); |
| 289 | } |
| 290 | if (reg8 & 0x01) { |
| 291 | dimm->flags.rzq6_supported = 1; |
| 292 | printram(" RZQ/6"); |
| 293 | } |
| 294 | printram("\n"); |
| 295 | |
| 296 | /* SDRAM Thermal and Refresh Options */ |
| 297 | reg8 = spd[31]; |
| 298 | printram(" Thermal features :"); |
| 299 | if (reg8 & 0x80) { |
| 300 | dimm->flags.pasr = 1; |
| 301 | printram(" PASR"); |
| 302 | } |
| 303 | if (reg8 & 0x08) { |
| 304 | dimm->flags.odts = 1; |
| 305 | printram(" ODTS"); |
| 306 | } |
| 307 | if (reg8 & 0x04) { |
| 308 | dimm->flags.asr = 1; |
| 309 | printram(" ASR"); |
| 310 | } |
| 311 | if (reg8 & 0x02) { |
| 312 | dimm->flags.ext_temp_range = 1; |
| 313 | printram(" ext_temp_refresh"); |
| 314 | } |
| 315 | if (reg8 & 0x01) { |
| 316 | dimm->flags.ext_temp_refresh = 1; |
| 317 | printram(" ext_temp_range"); |
| 318 | } |
| 319 | printram("\n"); |
| 320 | |
| 321 | /* Module Thermal Sensor */ |
| 322 | reg8 = spd[32]; |
| 323 | if (reg8 & 0x80) |
| 324 | dimm->flags.therm_sensor = 1; |
| 325 | printram(" Thermal sensor : %s\n", |
| 326 | dimm->flags.therm_sensor ? "yes" : "no"); |
| 327 | |
| 328 | /* SDRAM Device Type */ |
| 329 | reg8 = spd[33]; |
| 330 | printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes"); |
| 331 | |
| 332 | if (spd[63] & 0x01) { |
| 333 | dimm->flags.pins_mirrored = 1; |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 334 | printram(" DIMM Rank1 Address bits mirrored!!!\n"); |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 335 | } |
| 336 | |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 337 | dimm->reference_card = spd[62] & 0x1f; |
| 338 | printram(" DIMM Reference card %c\n", 'A' + dimm->reference_card); |
| 339 | |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * The information printed below has a more informational character, and is not |
| 345 | * necessarily tied in to RAM init debugging. Hence, we stop using printram(), |
| 346 | * and use the standard printk()'s below. |
| 347 | */ |
| 348 | |
| 349 | static void print_ns(const char *msg, u32 val) |
| 350 | { |
| 351 | u32 mant, fp; |
| 352 | mant = val / 256; |
| 353 | fp = (val % 256) * 1000 / 256; |
| 354 | |
| 355 | printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * \brief Print the info in DIMM |
| 360 | * |
| 361 | * Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is |
| 362 | * selected, or for a purely informative output. |
| 363 | * |
Martin Roth | 63373ed | 2013-07-08 16:24:19 -0600 | [diff] [blame] | 364 | * @param dimm pointer to already decoded @ref dimm_attr structure |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 365 | */ |
| 366 | void dram_print_spd_ddr3(const dimm_attr * dimm) |
| 367 | { |
| 368 | u16 val16; |
| 369 | int i; |
| 370 | |
| 371 | printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits); |
| 372 | printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits); |
| 373 | printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks); |
| 374 | printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb); |
| 375 | |
| 376 | /* CAS Latencies Supported */ |
| 377 | val16 = dimm->cas_supported; |
| 378 | printk(BIOS_INFO, " CAS latencies :"); |
| 379 | i = 0; |
| 380 | do { |
| 381 | if (val16 & 1) |
| 382 | printk(BIOS_INFO, " %u", i + 4); |
| 383 | i++; |
| 384 | val16 >>= 1; |
| 385 | } while (val16); |
| 386 | printk(BIOS_INFO, "\n"); |
| 387 | |
| 388 | print_ns(" tCKmin : ", dimm->tCK); |
| 389 | print_ns(" tAAmin : ", dimm->tAA); |
| 390 | print_ns(" tWRmin : ", dimm->tWR); |
| 391 | print_ns(" tRCDmin : ", dimm->tRCD); |
| 392 | print_ns(" tRRDmin : ", dimm->tRRD); |
| 393 | print_ns(" tRPmin : ", dimm->tRP); |
| 394 | print_ns(" tRASmin : ", dimm->tRAS); |
| 395 | print_ns(" tRCmin : ", dimm->tRC); |
| 396 | print_ns(" tRFCmin : ", dimm->tRFC); |
| 397 | print_ns(" tWTRmin : ", dimm->tWTR); |
| 398 | print_ns(" tRTPmin : ", dimm->tRTP); |
| 399 | print_ns(" tFAWmin : ", dimm->tFAW); |
Alexandru Gagniuc | 78706fd | 2013-06-03 13:58:10 -0500 | [diff] [blame] | 400 | } |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 401 | |
Alexandru Gagniuc | 78706fd | 2013-06-03 13:58:10 -0500 | [diff] [blame] | 402 | /*============================================================================== |
| 403 | *= DDR3 MRS helpers |
| 404 | *----------------------------------------------------------------------------*/ |
| 405 | |
| 406 | /* |
| 407 | * MRS command structure: |
| 408 | * cmd[15:0] = Address pins MA[15:0] |
| 409 | * cmd[18:16] = Bank address BA[2:0] |
| 410 | */ |
| 411 | |
| 412 | /* Map tWR value to a bitmask of the MR0 cycle */ |
| 413 | static u16 ddr3_twr_to_mr0_map(u8 twr) |
| 414 | { |
| 415 | if ((twr >= 5) && (twr <= 8)) |
| 416 | return (twr - 4) << 9; |
| 417 | |
| 418 | /* |
| 419 | * From 8T onwards, we can only use even values. Round up if we are |
| 420 | * given an odd value. |
| 421 | */ |
| 422 | if ((twr >= 9) && (twr <= 14)) |
| 423 | return ((twr + 1) >> 1) << 9; |
| 424 | |
| 425 | /* tWR == 16T is [000] */ |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* Map the CAS latency to a bitmask for the MR0 cycle */ |
| 430 | static u16 ddr3_cas_to_mr0_map(u8 cas) |
| 431 | { |
| 432 | u16 mask = 0; |
| 433 | /* A[6:4] are bits [2:0] of (CAS - 4) */ |
| 434 | mask = ((cas - 4) & 0x07) << 4; |
| 435 | |
| 436 | /* A2 is the MSB of (CAS - 4) */ |
| 437 | if ((cas - 4) & (1 << 3)) |
| 438 | mask |= (1 << 2); |
| 439 | |
| 440 | return mask; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * \brief Get command address for a DDR3 MR0 command |
| 445 | * |
| 446 | * The DDR3 specification only covers odd write_recovery up to 7T. If an odd |
| 447 | * write_recovery greater than 7 is specified, it will be rounded up. If a tWR |
| 448 | * greater than 8 is specified, it is recommended to explicitly round it up or |
| 449 | * down before calling this function. |
| 450 | * |
| 451 | * write_recovery and cas are given in clock cycles. For example, a CAS of 7T |
| 452 | * should be given as 7. |
| 453 | * |
| 454 | * @param write_recovery Write recovery latency, tWR in clock cycles. |
| 455 | * @param cas CAS latency in clock cycles. |
| 456 | */ |
| 457 | mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd, |
| 458 | u8 write_recovery, |
| 459 | enum ddr3_mr0_dll_reset dll_reset, |
| 460 | enum ddr3_mr0_mode mode, |
| 461 | u8 cas, |
| 462 | enum ddr3_mr0_burst_type burst_type, |
| 463 | enum ddr3_mr0_burst_length burst_length) |
| 464 | { |
| 465 | mrs_cmd_t cmd = 0 << 16; |
| 466 | |
| 467 | if (precharge_pd == DDR3_MR0_PRECHARGE_FAST) |
| 468 | cmd |= (1 << 12); |
| 469 | |
| 470 | cmd |= ddr3_twr_to_mr0_map(write_recovery); |
| 471 | |
| 472 | if (dll_reset == DDR3_MR0_DLL_RESET_YES) |
| 473 | cmd |= (1 << 8); |
| 474 | |
| 475 | if (mode == DDR3_MR0_MODE_TEST) |
| 476 | cmd |= (1 << 7); |
| 477 | |
| 478 | cmd |= ddr3_cas_to_mr0_map(cas); |
| 479 | |
| 480 | if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED) |
| 481 | cmd |= (1 << 3); |
| 482 | |
| 483 | cmd |= (burst_length & 0x03) << 0; |
| 484 | |
| 485 | return cmd; |
| 486 | } |
| 487 | |
| 488 | static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom) |
| 489 | { |
| 490 | u16 mask = 0; |
| 491 | /* A9 <-> rtt_nom[2] */ |
| 492 | if (rtt_nom & (1 << 2)) |
| 493 | mask |= (1 << 9); |
| 494 | /* A6 <-> rtt_nom[1] */ |
| 495 | if (rtt_nom & (1 << 1)) |
| 496 | mask |= (1 << 6); |
| 497 | /* A2 <-> rtt_nom[0] */ |
| 498 | if (rtt_nom & (1 << 0)) |
| 499 | mask |= (1 << 2); |
| 500 | |
| 501 | return mask; |
| 502 | } |
| 503 | |
| 504 | static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods) |
| 505 | { |
| 506 | u16 mask = 0; |
| 507 | /* A5 <-> ods[1] */ |
| 508 | if (ods & (1 << 1)) |
| 509 | mask |= (1 << 5); |
| 510 | /* A1 <-> ods[0] */ |
| 511 | if (ods & (1 << 0)) |
| 512 | mask |= (1 << 1); |
| 513 | |
| 514 | return mask; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * \brief Get command address for a DDR3 MR1 command |
| 519 | */ |
| 520 | mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff, |
| 521 | enum ddr3_mr1_tqds tqds, |
| 522 | enum ddr3_mr1_rtt_nom rtt_nom, |
| 523 | enum ddr3_mr1_write_leveling write_leveling, |
| 524 | enum ddr3_mr1_ods ods, |
| 525 | enum ddr3_mr1_additive_latency additive_latency, |
| 526 | enum ddr3_mr1_dll dll_disable) |
| 527 | { |
| 528 | mrs_cmd_t cmd = 1 << 16; |
| 529 | |
| 530 | if (qoff == DDR3_MR1_QOFF_DISABLE) |
| 531 | cmd |= (1 << 12); |
| 532 | |
| 533 | if (tqds == DDR3_MR1_TQDS_ENABLE) |
| 534 | cmd |= (1 << 11); |
| 535 | |
| 536 | cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom); |
| 537 | |
| 538 | if (write_leveling == DDR3_MR1_WRLVL_ENABLE) |
| 539 | cmd |= (1 << 7); |
| 540 | |
| 541 | cmd |= ddr3_ods_to_mr1_map(ods); |
| 542 | |
| 543 | cmd |= (additive_latency & 0x03) << 3; |
| 544 | |
| 545 | if (dll_disable == DDR3_MR1_DLL_DISABLE) |
| 546 | cmd |= (1 << 0); |
| 547 | |
| 548 | return cmd; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * \brief Get command address for a DDR3 MR2 command |
| 553 | * |
| 554 | * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be |
| 555 | * given as 7. |
| 556 | * |
| 557 | * @param cas_cwl CAS write latency in clock cycles. |
| 558 | */ |
| 559 | mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr, |
| 560 | enum ddr3_mr2_srt_range extended_temp, |
| 561 | enum ddr3_mr2_asr self_refresh, u8 cas_cwl) |
| 562 | { |
| 563 | mrs_cmd_t cmd = 2 << 16; |
| 564 | |
| 565 | cmd |= (rtt_wr & 0x03) << 9; |
| 566 | |
| 567 | if (extended_temp == DDR3_MR2_SRT_EXTENDED) |
| 568 | cmd |= (1 << 7); |
| 569 | |
| 570 | if (self_refresh == DDR3_MR2_ASR_AUTO) |
| 571 | cmd |= (1 << 6); |
| 572 | |
| 573 | cmd |= ((cas_cwl - 5) & 0x07) << 3; |
| 574 | |
| 575 | return cmd; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * \brief Get command address for a DDR3 MR3 command |
| 580 | * |
| 581 | * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read |
| 582 | * leveling mode. Zero for normal operation. |
| 583 | */ |
| 584 | mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr) |
| 585 | { |
| 586 | mrs_cmd_t cmd = 3 << 16; |
| 587 | |
| 588 | if (dataflow_from_mpr) |
| 589 | cmd |= (1 << 2); |
| 590 | |
| 591 | return cmd; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * \brief Mirror the address bits for this MRS command |
| 596 | * |
| 597 | * Swap the following bits in the MRS command: |
| 598 | * - MA3 <-> MA4 |
| 599 | * - MA5 <-> MA6 |
| 600 | * - MA7 <-> MA8 |
| 601 | * - BA0 <-> BA1 |
| 602 | */ |
| 603 | mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd) |
| 604 | { |
| 605 | u32 downshift, upshift; |
| 606 | /* High bits= A4 | A6 | A8 | BA1 */ |
| 607 | /* Low bits = A3 | A5 | A7 | BA0 */ |
| 608 | u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16); |
| 609 | downshift = (cmd & (lowbits << 1)); |
| 610 | upshift = (cmd & lowbits); |
| 611 | cmd &= ~(lowbits | (lowbits << 1)); |
| 612 | cmd |= (downshift >> 1) | (upshift << 1); |
| 613 | return cmd; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 614 | } |