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) 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. |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 15 | */ |
| 16 | |
Elyes HAOUAS | aa8e7e7 | 2016-06-19 12:38:47 +0200 | [diff] [blame] | 17 | /* |
| 18 | * JEDEC Standard No. 21-C |
| 19 | * Annex K: Serial Presence Detect (SPD) for DDR3 SDRAM Modules 2014 |
| 20 | * http://www.jedec.org/sites/default/files/docs/4_01_02_11R24.pdf |
| 21 | */ |
| 22 | |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 23 | #ifndef DEVICE_DRAM_DDR3L_H |
| 24 | #define DEVICE_DRAM_DDR3L_H |
| 25 | |
| 26 | /** |
| 27 | * @file ddr3.h |
| 28 | * |
| 29 | * \brief Utilities for decoding DDR3 SPDs |
| 30 | */ |
| 31 | |
| 32 | #include <stdint.h> |
| 33 | #include <spd.h> |
| 34 | |
| 35 | /** |
| 36 | * \brief Convenience definitions for TCK values |
| 37 | * |
| 38 | * Different values for tCK, representing standard DDR3 frequencies. |
| 39 | * These values are in 1/256 ns units. |
| 40 | * @{ |
| 41 | */ |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 42 | #define TCK_1333MHZ 192 |
| 43 | #define TCK_1200MHZ 212 |
Patrick Rudolph | 577aad6 | 2016-06-14 18:48:17 +0200 | [diff] [blame] | 44 | #define TCK_1100MHZ 232 |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 45 | #define TCK_1066MHZ 240 |
Patrick Rudolph | 577aad6 | 2016-06-14 18:48:17 +0200 | [diff] [blame] | 46 | #define TCK_1000MHZ 256 |
Patrick Rudolph | 98b5f90 | 2016-02-06 13:23:31 +0100 | [diff] [blame] | 47 | #define TCK_933MHZ 274 |
Patrick Rudolph | 577aad6 | 2016-06-14 18:48:17 +0200 | [diff] [blame] | 48 | #define TCK_900MHZ 284 |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 49 | #define TCK_800MHZ 320 |
Patrick Rudolph | 577aad6 | 2016-06-14 18:48:17 +0200 | [diff] [blame] | 50 | #define TCK_700MHZ 365 |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 51 | #define TCK_666MHZ 384 |
| 52 | #define TCK_533MHZ 480 |
| 53 | #define TCK_400MHZ 640 |
| 54 | #define TCK_333MHZ 768 |
| 55 | #define TCK_266MHZ 960 |
| 56 | #define TCK_200MHZ 1280 |
| 57 | /** @} */ |
| 58 | |
| 59 | /** |
| 60 | * \brief Convenience macro for enabling printk with CONFIG_DEBUG_RAM_SETUP |
| 61 | * |
| 62 | * Use this macro instead of printk(); for verbose RAM initialization messages. |
| 63 | * When CONFIG_DEBUG_RAM_SETUP is not selected, these messages are automatically |
| 64 | * disabled. |
| 65 | * @{ |
| 66 | */ |
Martin Roth | c4e49f6 | 2015-07-11 13:42:54 -0600 | [diff] [blame] | 67 | #if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP) |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 68 | #define printram(x, ...) printk(BIOS_DEBUG, x, ##__VA_ARGS__) |
| 69 | #else |
| 70 | #define printram(x, ...) |
| 71 | #endif |
| 72 | /** @} */ |
| 73 | |
| 74 | /* |
| 75 | * Module type (byte 3, bits 3:0) of SPD |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 76 | * This definition is specific to DDR3. DDR2 SPDs have a different structure. |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 77 | */ |
| 78 | enum spd_dimm_type { |
| 79 | SPD_DIMM_TYPE_UNDEFINED = 0x00, |
| 80 | SPD_DIMM_TYPE_RDIMM = 0x01, |
| 81 | SPD_DIMM_TYPE_UDIMM = 0x02, |
| 82 | SPD_DIMM_TYPE_SO_DIMM = 0x03, |
| 83 | SPD_DIMM_TYPE_MICRO_DIMM = 0x04, |
| 84 | SPD_DIMM_TYPE_MINI_RDIMM = 0x05, |
| 85 | SPD_DIMM_TYPE_MINI_UDIMM = 0x06, |
| 86 | SPD_DIMM_TYPE_MINI_CDIMM = 0x07, |
| 87 | SPD_DIMM_TYPE_72B_SO_UDIMM = 0x08, |
| 88 | SPD_DIMM_TYPE_72B_SO_RDIMM = 0x09, |
| 89 | SPD_DIMM_TYPE_72B_SO_CDIMM = 0x0a, |
| 90 | SPD_DIMM_TYPE_LRDIMM = 0x0b, |
Elyes HAOUAS | aa8e7e7 | 2016-06-19 12:38:47 +0200 | [diff] [blame] | 91 | SPD_DIMM_TYPE_16B_SO_DIMM = 0x0c, |
| 92 | SPD_DIMM_TYPE_32B_SO_DIMM = 0x0d, |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 93 | /* Masks to bits 3:0 to give the dimm type */ |
| 94 | SPD_DIMM_TYPE_MASK = 0x0f, |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * \brief DIMM flags |
| 99 | * |
| 100 | * Characteristic flags for the DIMM, as presented by the SPD |
| 101 | */ |
| 102 | typedef union dimm_flags_st { |
| 103 | /* The whole point of the union/struct construct is to allow us to clear |
| 104 | * all the bits with one line: flags.raw = 0. |
| 105 | * We do not care how these bits are ordered */ |
| 106 | struct { |
| 107 | /* Indicates if rank 1 of DIMM uses a mirrored pin mapping. See: |
| 108 | * Annex K: Serial Presence Detect (SPD) for DDR3 SDRAM */ |
| 109 | unsigned pins_mirrored:1; |
| 110 | /* Module can work at 1.50V - All DIMMS must be 1.5V operable */ |
| 111 | unsigned operable_1_50V:1; |
| 112 | /* Module can work at 1.35V */ |
| 113 | unsigned operable_1_35V:1; |
| 114 | /* Module can work at 1.20V */ |
| 115 | unsigned operable_1_25V:1; |
| 116 | /* Has an 8-bit bus extension, meaning the DIMM supports ECC */ |
| 117 | unsigned is_ecc:1; |
| 118 | /* DLL-Off Mode Support */ |
| 119 | unsigned dll_off_mode:1; |
| 120 | /* Indicates a drive strength of RZQ/6 (40 Ohm) is supported */ |
| 121 | unsigned rzq6_supported:1; |
| 122 | /* Indicates a drive strength of RZQ/7 (35 Ohm) is supported */ |
| 123 | unsigned rzq7_supported:1; |
| 124 | /* Partial Array Self Refresh */ |
| 125 | unsigned pasr:1; |
| 126 | /* On-die Thermal Sensor Readout */ |
| 127 | unsigned odts:1; |
| 128 | /* Auto Self Refresh */ |
| 129 | unsigned asr:1; |
| 130 | /* Extended temperature range supported */ |
| 131 | unsigned ext_temp_range:1; |
| 132 | /* Operating at extended temperature requires 2X refresh rate */ |
| 133 | unsigned ext_temp_refresh:1; |
| 134 | /* Thermal sensor incorporated */ |
| 135 | unsigned therm_sensor:1; |
| 136 | }; |
| 137 | unsigned raw; |
| 138 | } dimm_flags_t; |
| 139 | |
| 140 | /** |
| 141 | * \brief DIMM characteristics |
| 142 | * |
| 143 | * The characteristics of each DIMM, as presented by the SPD |
| 144 | */ |
| 145 | typedef struct dimm_attr_st { |
| 146 | enum spd_memory_type dram_type; |
Vladimir Serbinenko | 0e675f7 | 2014-12-07 13:56:48 +0100 | [diff] [blame] | 147 | enum spd_dimm_type dimm_type; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 148 | u16 cas_supported; |
| 149 | /* Flags extracted from SPD */ |
| 150 | dimm_flags_t flags; |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 151 | /* SDRAM width */ |
| 152 | u8 width; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 153 | /* Number of ranks */ |
| 154 | u8 ranks; |
| 155 | /* Number or row address bits */ |
| 156 | u8 row_bits; |
| 157 | /* Number or column address bits */ |
| 158 | u8 col_bits; |
| 159 | /* Size of module in MiB */ |
| 160 | u32 size_mb; |
| 161 | /* Latencies are in units of 1/256 ns */ |
| 162 | u32 tCK; |
| 163 | u32 tAA; |
| 164 | u32 tWR; |
| 165 | u32 tRCD; |
| 166 | u32 tRRD; |
| 167 | u32 tRP; |
| 168 | u32 tRAS; |
| 169 | u32 tRC; |
| 170 | u32 tRFC; |
| 171 | u32 tWTR; |
| 172 | u32 tRTP; |
| 173 | u32 tFAW; |
Vladimir Serbinenko | 7686a56 | 2014-05-18 11:05:56 +0200 | [diff] [blame] | 174 | |
| 175 | u8 reference_card; |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 176 | /* XMP: Module voltage in mV */ |
| 177 | u16 voltage; |
| 178 | /* XMP: max DIMMs per channel supported (1-4) */ |
| 179 | u8 dimms_per_channel; |
Patrick Rudolph | 0769159 | 2016-02-29 18:21:00 +0100 | [diff] [blame] | 180 | /* Manufacturer ID */ |
| 181 | u16 manufacturer_id; |
| 182 | /* ASCII part number - NULL terminated */ |
| 183 | u8 part_number[17]; |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 184 | } dimm_attr; |
| 185 | |
| 186 | /** Result of the SPD decoding process */ |
| 187 | enum spd_status { |
| 188 | SPD_STATUS_OK = 0, |
| 189 | SPD_STATUS_INVALID, |
| 190 | SPD_STATUS_CRC_ERROR, |
| 191 | SPD_STATUS_INVALID_FIELD, |
| 192 | }; |
| 193 | |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 194 | enum ddr3_xmp_profile { |
| 195 | DDR3_XMP_PROFILE_1 = 0, |
| 196 | DDR3_XMP_PROFILE_2 = 1, |
| 197 | }; |
| 198 | |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 199 | typedef u8 spd_raw_data[256]; |
| 200 | |
Alexandru Gagniuc | 4c37e58 | 2013-12-17 13:08:01 -0500 | [diff] [blame] | 201 | u16 spd_ddr3_calc_crc(u8 *spd, int len); |
Kyösti Mälkki | 7dc4b84 | 2016-11-18 18:41:17 +0200 | [diff] [blame] | 202 | u16 spd_ddr3_calc_unique_crc(u8 *spd, int len); |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 203 | int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd_data); |
| 204 | int dimm_is_registered(enum spd_dimm_type type); |
| 205 | void dram_print_spd_ddr3(const dimm_attr * dimm); |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 206 | int spd_xmp_decode_ddr3(dimm_attr * dimm, |
| 207 | spd_raw_data spd, |
| 208 | enum ddr3_xmp_profile profile); |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * \brief Read double word from specified address |
| 212 | * |
| 213 | * Should be useful when doing an MRS to the DIMM |
| 214 | */ |
Stefan Reinauer | 1e2500e | 2015-06-19 14:59:06 -0700 | [diff] [blame] | 215 | static inline u32 volatile_read(volatile uintptr_t addr) |
Alexandru Gagniuc | f97ff3f | 2013-05-21 14:43:45 -0500 | [diff] [blame] | 216 | { |
| 217 | volatile u32 result; |
| 218 | result = *(volatile u32 *)addr; |
| 219 | return result; |
| 220 | } |
| 221 | |
Alexandru Gagniuc | 78706fd | 2013-06-03 13:58:10 -0500 | [diff] [blame] | 222 | /** |
| 223 | * \brief Representation of an MRS command |
| 224 | * |
| 225 | * This represents an MRS command as seen by the DIMM. This is not a memory |
| 226 | * address that can be read to generate an MRS command. The mapping of CPU |
| 227 | * to memory pins is hardware-dependent. |
| 228 | * \n |
| 229 | * The idea is to generalize the MRS code, and only need a hardware-specific |
| 230 | * function to map the MRS bits to CPU address bits. An MRS command can be |
| 231 | * sent like: |
| 232 | * @code{.c} |
| 233 | * u32 addr; |
| 234 | * mrs_cmd_t mrs; |
| 235 | * chipset_enable_mrs_command_mode(); |
| 236 | * mrs = ddr3_get_mr2(rtt_wr, srt, asr, cwl) |
| 237 | * if (rank_has_mirrorred_pins) |
| 238 | * mrs = ddr3_mrs_mirror_pins(mrs); |
| 239 | * addr = chipset_specific_get_mrs_addr(mrs); |
| 240 | * volatile_read(addr); |
| 241 | * @endcode |
| 242 | * |
| 243 | * The MRS representation has the following structure: |
| 244 | * - cmd[15:0] = Address pins MA[15:0] |
| 245 | * - cmd[18:16] = Bank address BA[2:0] |
| 246 | */ |
| 247 | typedef u32 mrs_cmd_t; |
| 248 | |
| 249 | enum ddr3_mr0_precharge { |
| 250 | DDR3_MR0_PRECHARGE_SLOW = 0, |
| 251 | DDR3_MR0_PRECHARGE_FAST = 1, |
| 252 | }; |
| 253 | enum ddr3_mr0_mode { |
| 254 | DDR3_MR0_MODE_NORMAL = 0, |
| 255 | DDR3_MR0_MODE_TEST = 1, |
| 256 | }; |
| 257 | enum ddr3_mr0_dll_reset { |
| 258 | DDR3_MR0_DLL_RESET_NO = 0, |
| 259 | DDR3_MR0_DLL_RESET_YES = 1, |
| 260 | }; |
| 261 | enum ddr3_mr0_burst_type { |
| 262 | DDR3_MR0_BURST_TYPE_SEQUENTIAL = 0, |
| 263 | DDR3_MR0_BURST_TYPE_INTERLEAVED = 1, |
| 264 | }; |
| 265 | enum ddr3_mr0_burst_length { |
| 266 | DDR3_MR0_BURST_LENGTH_8 = 0, |
| 267 | DDR3_MR0_BURST_LENGTH_CHOP = 1, |
| 268 | DDR3_MR0_BURST_LENGTH_4 = 2, |
| 269 | }; |
| 270 | mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd, |
| 271 | u8 write_recovery, |
| 272 | enum ddr3_mr0_dll_reset dll_reset, |
| 273 | enum ddr3_mr0_mode mode, |
| 274 | u8 cas, |
| 275 | enum ddr3_mr0_burst_type interleaved_burst, |
| 276 | enum ddr3_mr0_burst_length burst_length); |
| 277 | |
| 278 | enum ddr3_mr1_qoff { |
| 279 | DDR3_MR1_QOFF_ENABLE = 0, |
| 280 | DDR3_MR1_QOFF_DISABLE = 1, |
| 281 | }; |
| 282 | enum ddr3_mr1_tqds { |
| 283 | DDR3_MR1_TQDS_DISABLE = 0, |
| 284 | DDR3_MR1_TQDS_ENABLE = 1, |
| 285 | }; |
| 286 | enum ddr3_mr1_write_leveling { |
| 287 | DDR3_MR1_WRLVL_DISABLE = 0, |
| 288 | DDR3_MR1_WRLVL_ENABLE = 1, |
| 289 | }; |
| 290 | enum ddr3_mr1_rtt_nom { |
| 291 | DDR3_MR1_RTT_NOM_OFF = 0, |
| 292 | DDR3_MR1_RTT_NOM_RZQ4 = 1, |
| 293 | DDR3_MR1_RTT_NOM_RZQ2 = 2, |
| 294 | DDR3_MR1_RTT_NOM_RZQ6 = 3, |
| 295 | DDR3_MR1_RTT_NOM_RZQ12 = 4, |
| 296 | DDR3_MR1_RTT_NOM_RZQ8 = 5, |
| 297 | }; |
| 298 | enum ddr3_mr1_additive_latency { |
| 299 | DDR3_MR1_AL_DISABLE = 0, |
| 300 | DDR3_MR1_AL_CL_MINUS_1 = 1, |
| 301 | DDR3_MR1_AL_CL_MINUS_2 = 2, |
| 302 | }; |
| 303 | enum ddr3_mr1_ods { |
| 304 | DDR3_MR1_ODS_RZQ6 = 0, |
| 305 | DDR3_MR1_ODS_RZQ7 = 1, |
| 306 | }; |
| 307 | enum ddr3_mr1_dll { |
| 308 | DDR3_MR1_DLL_ENABLE = 0, |
| 309 | DDR3_MR1_DLL_DISABLE = 1, |
| 310 | }; |
| 311 | |
| 312 | mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff, |
| 313 | enum ddr3_mr1_tqds tqds, |
| 314 | enum ddr3_mr1_rtt_nom rtt_nom, |
| 315 | enum ddr3_mr1_write_leveling write_leveling, |
| 316 | enum ddr3_mr1_ods output_drive_strenght, |
| 317 | enum ddr3_mr1_additive_latency additive_latency, |
| 318 | enum ddr3_mr1_dll dll_disable); |
| 319 | |
| 320 | enum ddr3_mr2_rttwr { |
| 321 | DDR3_MR2_RTTWR_OFF = 0, |
| 322 | DDR3_MR2_RTTWR_RZQ4 = 1, |
| 323 | DDR3_MR2_RTTWR_RZQ2 = 2, |
| 324 | }; |
| 325 | enum ddr3_mr2_srt_range { |
| 326 | DDR3_MR2_SRT_NORMAL = 0, |
| 327 | DDR3_MR2_SRT_EXTENDED = 1, |
| 328 | }; |
| 329 | enum ddr3_mr2_asr { |
| 330 | DDR3_MR2_ASR_MANUAL = 0, |
| 331 | DDR3_MR2_ASR_AUTO = 1, |
| 332 | }; |
| 333 | |
| 334 | mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr, |
| 335 | enum ddr3_mr2_srt_range extended_temp, |
| 336 | enum ddr3_mr2_asr self_refresh, u8 cas_cwl); |
| 337 | |
| 338 | mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr); |
| 339 | mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd); |
| 340 | |
Martin Roth | fd277d8 | 2016-01-11 12:47:30 -0700 | [diff] [blame] | 341 | #endif /* DEVICE_DRAM_DDR3L_H */ |