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