2 * This file is part of the coreboot project.
4 * Copyright (C) 2011-2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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.
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.
20 * \brief Utilities for decoding DDR3 SPDs
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/dram/ddr3.h>
27 /*==============================================================================
28 * = DDR3 SPD decoding helpers
29 *----------------------------------------------------------------------------*/
32 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
34 * Tells if the DIMM type is registered or not.
36 * @param type DIMM type. This is byte[3] of the SPD.
38 int dimm_is_registered(enum spd_dimm_type type
)
40 if ((type
== SPD_DIMM_TYPE_RDIMM
)
41 | (type
== SPD_DIMM_TYPE_MINI_RDIMM
)
42 | (type
== SPD_DIMM_TYPE_72B_SO_RDIMM
))
49 * \brief Calculate the CRC of a DDR3 SPD
51 * @param spd pointer to raw SPD data
52 * @param len length of data in SPD
54 * @return the CRC of the SPD data, or 0 when spd data is truncated.
56 u16
spd_ddr3_calc_crc(u8
*spd
, int len
)
62 /* Find the number of bytes covered by CRC */
70 /* Not enough bytes available to get the CRC */
76 while (--n_crc
>= 0) {
77 crc
= crc
^ (int)*ptr
++ << 8;
78 for (i
= 0; i
< 8; ++i
)
80 crc
= crc
<< 1 ^ 0x1021;
89 * \brief Decode the raw SPD data
91 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
92 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
93 * array, and passed to this function.
95 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
97 * @param spd array of raw data previously read from the SPD.
99 * @return @ref spd_status enumerator
100 * SPD_STATUS_OK -- decoding was successful
101 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
102 * SPD_STATUS_CRC_ERROR -- CRC did not verify
103 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
106 int spd_decode_ddr3(dimm_attr
* dimm
, spd_raw_data spd
)
110 u8 ftb_divisor
, ftb_dividend
, capacity_shift
, bus_width
;
112 u32 mtb
; /* medium time base */
113 unsigned int val
, param
;
117 /* Don't assume we memset 0 dimm struct. Clear all our flags */
119 /* Make sure that the SPD dump is indeed from a DDR3 module */
120 if (spd
[2] != SPD_MEMORY_TYPE_SDRAM_DDR3
) {
121 printram("Not a DDR3 SPD!\n");
122 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
123 return SPD_STATUS_INVALID
;
125 dimm
->dram_type
= SPD_MEMORY_TYPE_SDRAM_DDR3
;
126 dimm
->dimm_type
= spd
[3] & 0xf;
128 crc
= spd_ddr3_calc_crc(spd
, sizeof(spd_raw_data
));
129 /* Compare with the CRC in the SPD */
130 spd_crc
= (spd
[127] << 8) + spd
[126];
131 /* Verify the CRC is correct */
132 if (crc
!= spd_crc
) {
133 printram("ERROR: SPD CRC failed!!!\n");
134 ret
= SPD_STATUS_CRC_ERROR
;
137 printram(" Revision: %x\n", spd
[1]);
138 printram(" Type : %x\n", spd
[2]);
139 printram(" Key : %x\n", spd
[3]);
142 /* Number of memory banks */
143 val
= (reg8
>> 4) & 0x07;
145 printram(" Invalid number of memory banks\n");
146 ret
= SPD_STATUS_INVALID_FIELD
;
148 param
= 1 << (val
+ 3);
149 printram(" Banks : %u\n", param
);
151 capacity_shift
= reg8
& 0x0f;
152 if (capacity_shift
> 0x06) {
153 printram(" Invalid module capacity\n");
154 ret
= SPD_STATUS_INVALID_FIELD
;
156 if (capacity_shift
< 0x02) {
157 printram(" Capacity: %u Mb\n", 256 << capacity_shift
);
159 printram(" Capacity: %u Gb\n", 1 << (capacity_shift
- 2));
163 /* Row address bits */
164 val
= (reg8
>> 3) & 0x07;
166 printram(" Invalid row address bits\n");
167 ret
= SPD_STATUS_INVALID_FIELD
;
169 dimm
->row_bits
= val
+ 12;
170 /* Column address bits */
173 printram(" Invalid column address bits\n");
174 ret
= SPD_STATUS_INVALID_FIELD
;
176 dimm
->col_bits
= val
+ 9;
178 /* Module nominal voltage */
180 printram(" Supported voltages:");
181 if (reg8
& (1 << 2)) {
182 dimm
->flags
.operable_1_25V
= 1;
185 if (reg8
& (1 << 1)) {
186 dimm
->flags
.operable_1_35V
= 1;
189 if (!(reg8
& (1 << 0))) {
190 dimm
->flags
.operable_1_50V
= 1;
195 /* Module organization */
197 /* Number of ranks */
198 val
= (reg8
>> 3) & 0x07;
200 printram(" Invalid number of ranks\n");
201 ret
= SPD_STATUS_INVALID_FIELD
;
203 dimm
->ranks
= val
+ 1;
204 /* SDRAM device width */
207 printram(" Invalid SDRAM width\n");
208 ret
= SPD_STATUS_INVALID_FIELD
;
210 dimm
->width
= (4 << val
);
211 printram(" SDRAM width : %u\n", dimm
->width
);
213 /* Memory bus width */
216 val
= (reg8
>> 3) & 0x03;
218 printram(" Invalid bus extension\n");
219 ret
= SPD_STATUS_INVALID_FIELD
;
221 dimm
->flags
.is_ecc
= val
? 1 : 0;
222 printram(" Bus extension : %u bits\n", val
? 8 : 0);
226 printram(" Invalid bus width\n");
227 ret
= SPD_STATUS_INVALID_FIELD
;
229 bus_width
= 8 << val
;
230 printram(" Bus width : %u\n", bus_width
);
232 /* We have all the info we need to compute the dimm size */
233 /* Capacity is 256Mbit multiplied by the power of 2 specified in
235 * The rest is the JEDEC formula */
236 dimm
->size_mb
= ((1 << (capacity_shift
+ (25 - 20))) * bus_width
237 * dimm
->ranks
) / dimm
->width
;
239 /* Fine Timebase (FTB) Dividend/Divisor */
241 ftb_dividend
= (spd
[9] >> 4) & 0x0f;
243 ftb_divisor
= spd
[9] & 0x0f;
246 * Medium Timebase (MTB) Dividend /
247 * Medium Timebase (MTB) Divisor */
248 mtb
= (((u32
) spd
[10]) << 8) / spd
[11];
250 /* SDRAM Minimum Cycle Time (tCKmin) */
251 dimm
->tCK
= spd
[12] * mtb
;
252 /* CAS Latencies Supported */
253 dimm
->cas_supported
= (spd
[15] << 8) + spd
[14];
254 /* Minimum CAS Latency Time (tAAmin) */
255 dimm
->tAA
= spd
[16] * mtb
;
256 /* Minimum Write Recovery Time (tWRmin) */
257 dimm
->tWR
= spd
[17] * mtb
;
258 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
259 dimm
->tRCD
= spd
[18] * mtb
;
260 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
261 dimm
->tRRD
= spd
[19] * mtb
;
262 /* Minimum Row Precharge Delay Time (tRPmin) */
263 dimm
->tRP
= spd
[20] * mtb
;
264 /* Minimum Active to Precharge Delay Time (tRASmin) */
265 dimm
->tRAS
= (((spd
[21] & 0x0f) << 8) + spd
[22]) * mtb
;
266 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
267 dimm
->tRC
= (((spd
[21] & 0xf0) << 4) + spd
[23]) * mtb
;
268 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
269 dimm
->tRFC
= ((spd
[25] << 8) + spd
[24]) * mtb
;
270 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
271 dimm
->tWTR
= spd
[26] * mtb
;
272 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
273 dimm
->tRTP
= spd
[27] * mtb
;
274 /* Minimum Four Activate Window Delay Time (tFAWmin) */
275 dimm
->tFAW
= (((spd
[28] & 0x0f) << 8) + spd
[29]) * mtb
;
277 /* SDRAM Optional Features */
279 printram(" Optional features :");
281 dimm
->flags
.dll_off_mode
= 1;
282 printram(" DLL-Off_mode");
285 dimm
->flags
.rzq7_supported
= 1;
289 dimm
->flags
.rzq6_supported
= 1;
294 /* SDRAM Thermal and Refresh Options */
296 printram(" Thermal features :");
298 dimm
->flags
.pasr
= 1;
302 dimm
->flags
.odts
= 1;
310 dimm
->flags
.ext_temp_range
= 1;
311 printram(" ext_temp_refresh");
314 dimm
->flags
.ext_temp_refresh
= 1;
315 printram(" ext_temp_range");
319 /* Module Thermal Sensor */
322 dimm
->flags
.therm_sensor
= 1;
323 printram(" Thermal sensor : %s\n",
324 dimm
->flags
.therm_sensor
? "yes" : "no");
326 /* SDRAM Device Type */
328 printram(" Standard SDRAM : %s\n", (reg8
& 0x80) ? "no" : "yes");
330 if (spd
[63] & 0x01) {
331 dimm
->flags
.pins_mirrored
= 1;
332 printram(" DIMM Rank1 Address bits mirrored!!!\n");
335 dimm
->reference_card
= spd
[62] & 0x1f;
336 printram(" DIMM Reference card %c\n", 'A' + dimm
->reference_card
);
342 * The information printed below has a more informational character, and is not
343 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
344 * and use the standard printk()'s below.
347 static void print_ns(const char *msg
, u32 val
)
351 fp
= (val
% 256) * 1000 / 256;
353 printk(BIOS_INFO
, "%s%3u.%.3u ns\n", msg
, mant
, fp
);
357 * \brief Print the info in DIMM
359 * Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
360 * selected, or for a purely informative output.
362 * @param dimm pointer to already decoded @ref dimm_attr structure
364 void dram_print_spd_ddr3(const dimm_attr
* dimm
)
369 printk(BIOS_INFO
, " Row addr bits : %u\n", dimm
->row_bits
);
370 printk(BIOS_INFO
, " Column addr bits : %u\n", dimm
->col_bits
);
371 printk(BIOS_INFO
, " Number of ranks : %u\n", dimm
->ranks
);
372 printk(BIOS_INFO
, " DIMM Capacity : %u MB\n", dimm
->size_mb
);
374 /* CAS Latencies Supported */
375 val16
= dimm
->cas_supported
;
376 printk(BIOS_INFO
, " CAS latencies :");
380 printk(BIOS_INFO
, " %u", i
+ 4);
384 printk(BIOS_INFO
, "\n");
386 print_ns(" tCKmin : ", dimm
->tCK
);
387 print_ns(" tAAmin : ", dimm
->tAA
);
388 print_ns(" tWRmin : ", dimm
->tWR
);
389 print_ns(" tRCDmin : ", dimm
->tRCD
);
390 print_ns(" tRRDmin : ", dimm
->tRRD
);
391 print_ns(" tRPmin : ", dimm
->tRP
);
392 print_ns(" tRASmin : ", dimm
->tRAS
);
393 print_ns(" tRCmin : ", dimm
->tRC
);
394 print_ns(" tRFCmin : ", dimm
->tRFC
);
395 print_ns(" tWTRmin : ", dimm
->tWTR
);
396 print_ns(" tRTPmin : ", dimm
->tRTP
);
397 print_ns(" tFAWmin : ", dimm
->tFAW
);
400 /*==============================================================================
402 *----------------------------------------------------------------------------*/
405 * MRS command structure:
406 * cmd[15:0] = Address pins MA[15:0]
407 * cmd[18:16] = Bank address BA[2:0]
410 /* Map tWR value to a bitmask of the MR0 cycle */
411 static u16
ddr3_twr_to_mr0_map(u8 twr
)
413 if ((twr
>= 5) && (twr
<= 8))
414 return (twr
- 4) << 9;
417 * From 8T onwards, we can only use even values. Round up if we are
418 * given an odd value.
420 if ((twr
>= 9) && (twr
<= 14))
421 return ((twr
+ 1) >> 1) << 9;
423 /* tWR == 16T is [000] */
427 /* Map the CAS latency to a bitmask for the MR0 cycle */
428 static u16
ddr3_cas_to_mr0_map(u8 cas
)
431 /* A[6:4] are bits [2:0] of (CAS - 4) */
432 mask
= ((cas
- 4) & 0x07) << 4;
434 /* A2 is the MSB of (CAS - 4) */
435 if ((cas
- 4) & (1 << 3))
442 * \brief Get command address for a DDR3 MR0 command
444 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
445 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
446 * greater than 8 is specified, it is recommended to explicitly round it up or
447 * down before calling this function.
449 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
450 * should be given as 7.
452 * @param precharge_pd
453 * @param write_recovery Write recovery latency, tWR in clock cycles.
456 * @param cas CAS latency in clock cycles.
458 * @param burst_length
460 mrs_cmd_t
ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd
,
462 enum ddr3_mr0_dll_reset dll_reset
,
463 enum ddr3_mr0_mode mode
,
465 enum ddr3_mr0_burst_type burst_type
,
466 enum ddr3_mr0_burst_length burst_length
)
468 mrs_cmd_t cmd
= 0 << 16;
470 if (precharge_pd
== DDR3_MR0_PRECHARGE_FAST
)
473 cmd
|= ddr3_twr_to_mr0_map(write_recovery
);
475 if (dll_reset
== DDR3_MR0_DLL_RESET_YES
)
478 if (mode
== DDR3_MR0_MODE_TEST
)
481 cmd
|= ddr3_cas_to_mr0_map(cas
);
483 if (burst_type
== DDR3_MR0_BURST_TYPE_INTERLEAVED
)
486 cmd
|= (burst_length
& 0x03) << 0;
491 static u16
ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom
)
494 /* A9 <-> rtt_nom[2] */
495 if (rtt_nom
& (1 << 2))
497 /* A6 <-> rtt_nom[1] */
498 if (rtt_nom
& (1 << 1))
500 /* A2 <-> rtt_nom[0] */
501 if (rtt_nom
& (1 << 0))
507 static u16
ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods
)
521 * \brief Get command address for a DDR3 MR1 command
523 mrs_cmd_t
ddr3_get_mr1(enum ddr3_mr1_qoff qoff
,
524 enum ddr3_mr1_tqds tqds
,
525 enum ddr3_mr1_rtt_nom rtt_nom
,
526 enum ddr3_mr1_write_leveling write_leveling
,
527 enum ddr3_mr1_ods ods
,
528 enum ddr3_mr1_additive_latency additive_latency
,
529 enum ddr3_mr1_dll dll_disable
)
531 mrs_cmd_t cmd
= 1 << 16;
533 if (qoff
== DDR3_MR1_QOFF_DISABLE
)
536 if (tqds
== DDR3_MR1_TQDS_ENABLE
)
539 cmd
|= ddr3_rtt_nom_to_mr1_map(rtt_nom
);
541 if (write_leveling
== DDR3_MR1_WRLVL_ENABLE
)
544 cmd
|= ddr3_ods_to_mr1_map(ods
);
546 cmd
|= (additive_latency
& 0x03) << 3;
548 if (dll_disable
== DDR3_MR1_DLL_DISABLE
)
555 * \brief Get command address for a DDR3 MR2 command
557 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
561 * @param extended_temp
562 * @param self_refresh
563 * @param cas_cwl CAS write latency in clock cycles.
566 mrs_cmd_t
ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr
,
567 enum ddr3_mr2_srt_range extended_temp
,
568 enum ddr3_mr2_asr self_refresh
, u8 cas_cwl
)
570 mrs_cmd_t cmd
= 2 << 16;
572 cmd
|= (rtt_wr
& 0x03) << 9;
574 if (extended_temp
== DDR3_MR2_SRT_EXTENDED
)
577 if (self_refresh
== DDR3_MR2_ASR_AUTO
)
580 cmd
|= ((cas_cwl
- 5) & 0x07) << 3;
586 * \brief Get command address for a DDR3 MR3 command
588 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
589 * leveling mode. Zero for normal operation.
591 mrs_cmd_t
ddr3_get_mr3(char dataflow_from_mpr
)
593 mrs_cmd_t cmd
= 3 << 16;
595 if (dataflow_from_mpr
)
602 * \brief Mirror the address bits for this MRS command
604 * Swap the following bits in the MRS command:
610 mrs_cmd_t
ddr3_mrs_mirror_pins(mrs_cmd_t cmd
)
612 u32 downshift
, upshift
;
613 /* High bits= A4 | A6 | A8 | BA1 */
614 /* Low bits = A3 | A5 | A7 | BA0 */
615 u32 lowbits
= (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
616 downshift
= (cmd
& (lowbits
<< 1));
617 upshift
= (cmd
& lowbits
);
618 cmd
&= ~(lowbits
| (lowbits
<< 1));
619 cmd
|= (downshift
>> 1) | (upshift
<< 1);