1 /* This file is part of the coreboot project. */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
7 * \brief Utilities for decoding DDR2 SPDs
10 #include <console/console.h>
11 #include <device/device.h>
12 #include <device/dram/ddr2.h>
17 /*==============================================================================
18 * = DDR2 SPD decoding helpers
19 *----------------------------------------------------------------------------*/
22 * \brief Checks if the DIMM is Registered based on byte[20] of the SPD
24 * Tells if the DIMM type is registered or not.
26 * @param type DIMM type. This is byte[20] of the SPD.
28 int spd_dimm_is_registered_ddr2(enum spd_dimm_type_ddr2 type
)
30 if ((type
== SPD_DDR2_DIMM_TYPE_RDIMM
)
31 || (type
== SPD_DDR2_DIMM_TYPE_72B_SO_RDIMM
)
32 || (type
== SPD_DDR2_DIMM_TYPE_MINI_RDIMM
))
39 * \brief Calculate the checksum of a DDR2 SPD unique identifier
41 * @param spd pointer to raw SPD data
42 * @param len length of data in SPD
44 * @return the checksum of SPD data bytes 63, or 0 when spd data is truncated.
46 u8
spd_ddr2_calc_checksum(u8
*spd
, int len
)
52 /* Not enough bytes available to get the checksum */
55 for (i
= 0; i
< 63; i
++)
62 * \brief Calculate the CRC of a DDR2 SPD unique identifier
64 * @param spd pointer to raw SPD data
65 * @param len length of data in SPD
67 * @return the CRC of SPD data bytes 64..72 and 93..98, or 0
68 * when spd data is truncated.
70 u16
spd_ddr2_calc_unique_crc(const u8
*spd
, int len
)
75 /* Not enough bytes available to get the CRC */
77 for (i
= 64; i
<= 72; i
++)
78 id_bytes
[j
++] = spd
[i
];
79 for (i
= 93; i
<= 98; i
++)
80 id_bytes
[j
++] = spd
[i
];
82 return ddr_crc16(id_bytes
, 15);
86 * \brief Return size of SPD.
88 * Returns size of SPD. Usually 128 Byte.
90 u32
spd_decode_spd_size_ddr2(u8 byte0
)
92 return MIN(byte0
, SPD_SIZE_MAX_DDR2
);
96 * \brief Return size of eeprom.
98 * Returns size of eeprom. Usually 256 Byte.
100 u32
spd_decode_eeprom_size_ddr2(u8 byte1
)
112 * \brief Return index of MSB set
114 * Returns the index of MSB set.
116 u8
spd_get_msbs(u8 c
)
122 * \brief Decode SPD tck cycle time
124 * Decodes a raw SPD data from a DDR2 DIMM.
125 * Returns cycle time in 1/256th ns.
127 static int spd_decode_tck_time(u32
*tck
, u8 c
)
148 printk(BIOS_WARNING
, "Invalid tck setting. "
149 "lower nibble is 0x%x\n", c
& 0xf);
152 low
= (c
& 0xf) * 10;
155 *tck
= ((high
* 100 + low
) << 8) / 100;
160 * \brief Decode SPD bcd style timings
162 * Decodes a raw SPD data from a DDR2 DIMM.
163 * Returns cycle time in 1/256th ns.
165 static int spd_decode_bcd_time(u32
*bcd
, u8 c
)
171 if (high
>= 10 || low
>= 10)
174 *bcd
= ((high
* 10 + low
) << 8) / 100;
179 * \brief Decode SPD tRP, tRRP cycle time
181 * Decodes a raw SPD data from a DDR2 DIMM.
182 * Returns cycle time in 1/256th ns.
184 static u32
spd_decode_quarter_time(u8 c
)
189 low
= 25 * (c
& 0x3);
191 return ((high
* 100 + low
) << 8) / 100;
195 * \brief Decode SPD tRR time
197 * Decodes a raw SPD data from a DDR2 DIMM.
198 * Returns cycle time in 1/256th us.
200 static int spd_decode_tRR_time(u32
*tRR
, u8 c
)
204 printk(BIOS_WARNING
, "Invalid tRR value 0x%x\n", c
);
229 * \brief Decode SPD tRC,tRFC time
231 * Decodes a raw SPD data from a DDR2 DIMM.
232 * Returns cycle time in 1/256th us.
234 static void spd_decode_tRCtRFC_time(u8
*spd_40_41_42
, u32
*tRC
, u32
*tRFC
)
238 b40
= spd_40_41_42
[0];
239 b41
= spd_40_41_42
[1];
240 b42
= spd_40_41_42
[2];
248 switch ((b40
>> 1) & 0x07) {
268 switch ((b40
>> 4) & 0x07) {
288 /* Convert to 1/256th us */
289 *tRC
= (*tRC
<< 8) / 100;
290 *tRFC
= (*tRFC
<< 8) / 100;
294 * \brief Decode the raw SPD data
296 * Decodes a raw SPD data from a DDR2 DIMM, and organizes it into a
297 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
298 * array, and passed to this function.
300 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
302 * @param spd array of raw data previously read from the SPD.
304 * @return @ref spd_status enumerator
305 * SPD_STATUS_OK -- decoding was successful
306 * SPD_STATUS_INVALID -- invalid SPD or not a DDR2 SPD
307 * SPD_STATUS_CRC_ERROR -- CRC did not verify
308 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
311 int spd_decode_ddr2(struct dimm_attr_ddr2_st
*dimm
, u8 spd
[SPD_SIZE_MAX_DDR2
])
313 u8 spd_size
, cl
, reg8
;
315 int ret
= SPD_STATUS_OK
;
317 memset(dimm
, 0, sizeof(*dimm
));
319 spd_size
= spd_decode_spd_size_ddr2(spd
[0]);
320 eeprom_size
= spd_decode_eeprom_size_ddr2(spd
[1]);
322 printram("EEPROM with 0x%04x bytes\n", eeprom_size
);
323 printram("SPD contains 0x%02x bytes\n", spd_size
);
325 if (spd_size
< 64 || eeprom_size
< 64) {
326 printk(BIOS_WARNING
, "ERROR: SPD to small\n");
327 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
328 return SPD_STATUS_INVALID
;
331 if (spd_ddr2_calc_checksum(spd
, spd_size
) != spd
[63]) {
332 printk(BIOS_WARNING
, "ERROR: SPD checksum error\n");
333 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
334 return SPD_STATUS_CRC_ERROR
;
336 dimm
->checksum
= spd
[63];
339 if ((reg8
& 0xf0) != 0x10) {
341 "ERROR: Unsupported SPD revision %01x.%01x\n",
342 reg8
>> 4, reg8
& 0xf);
343 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
344 return SPD_STATUS_INVALID
;
347 printram(" Revision : %01x.%01x\n", dimm
->rev
>> 4, dimm
->rev
& 0xf);
350 printram(" Type : 0x%02x\n", reg8
);
352 printk(BIOS_WARNING
, "ERROR: Unsupported SPD type %x\n", reg8
);
353 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
354 return SPD_STATUS_INVALID
;
356 dimm
->dram_type
= SPD_MEMORY_TYPE_SDRAM_DDR2
;
358 dimm
->row_bits
= spd
[3];
359 printram(" Rows : %u\n", dimm
->row_bits
);
360 if ((dimm
->row_bits
> 31) ||
361 ((dimm
->row_bits
> 15) && (dimm
->rev
< 0x13))) {
363 "SPD decode: invalid number of memory rows\n");
364 ret
= SPD_STATUS_INVALID_FIELD
;
367 dimm
->col_bits
= spd
[4];
368 printram(" Columns : %u\n", dimm
->col_bits
);
369 if (dimm
->col_bits
> 15) {
371 "SPD decode: invalid number of memory columns\n");
372 ret
= SPD_STATUS_INVALID_FIELD
;
375 dimm
->ranks
= (spd
[5] & 0x7) + 1;
376 printram(" Ranks : %u\n", dimm
->ranks
);
378 dimm
->mod_width
= spd
[6];
379 printram(" Module data width : x%u\n", dimm
->mod_width
);
380 if (!dimm
->mod_width
) {
381 printk(BIOS_WARNING
, "SPD decode: invalid module data width\n");
382 ret
= SPD_STATUS_INVALID_FIELD
;
385 dimm
->width
= spd
[13];
386 printram(" SDRAM width : x%u\n", dimm
->width
);
388 printk(BIOS_WARNING
, "SPD decode: invalid SDRAM width\n");
389 ret
= SPD_STATUS_INVALID_FIELD
;
392 dimm
->banks
= spd
[17];
393 printram(" Banks : %u\n", dimm
->banks
);
396 "SPD decode: invalid module banks count\n");
397 ret
= SPD_STATUS_INVALID_FIELD
;
402 dimm
->flags
.operable_5_00V
= 1;
403 printram(" Voltage : 5.0V\n");
406 dimm
->flags
.operable_3_33V
= 1;
407 printram(" Voltage : 3.3V\n");
410 dimm
->flags
.operable_1_50V
= 1;
411 printram(" Voltage : 1.5V\n");
414 dimm
->flags
.operable_3_33V
= 1;
415 printram(" Voltage : 3.3V\n");
418 dimm
->flags
.operable_2_50V
= 1;
419 printram(" Voltage : 2.5V\n");
422 dimm
->flags
.operable_1_80V
= 1;
423 printram(" Voltage : 1.8V\n");
426 printk(BIOS_WARNING
, "SPD decode: unknown voltage level.\n");
427 ret
= SPD_STATUS_INVALID_FIELD
;
430 dimm
->cas_supported
= spd
[18];
431 if ((dimm
->cas_supported
& 0x3) || !dimm
->cas_supported
) {
433 "SPD decode: invalid CAS support advertised.\n");
434 ret
= SPD_STATUS_INVALID_FIELD
;
436 printram(" Supported CAS mask : 0x%x\n", dimm
->cas_supported
);
438 if ((dimm
->rev
< 0x13) && (dimm
->cas_supported
& 0x80)) {
440 "SPD decode: invalid CAS support advertised.\n");
441 ret
= SPD_STATUS_INVALID_FIELD
;
443 if ((dimm
->rev
< 0x12) && (dimm
->cas_supported
& 0x40)) {
445 "SPD decode: invalid CAS support advertised.\n");
446 ret
= SPD_STATUS_INVALID_FIELD
;
450 cl
= spd_get_msbs(dimm
->cas_supported
);
452 /* SDRAM Cycle time at Maximum Supported CAS Latency (CL), CL=X */
453 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
], spd
[9]) != CB_SUCCESS
) {
455 "SPD decode: invalid min tCL for CAS%d\n", cl
);
456 ret
= SPD_STATUS_INVALID_FIELD
;
458 /* SDRAM Access from Clock */
459 if (spd_decode_bcd_time(&dimm
->access_time
[cl
], spd
[10])
462 "SPD decode: invalid min tAC for CAS%d\n", cl
);
463 ret
= SPD_STATUS_INVALID_FIELD
;
466 if (dimm
->cas_supported
& (1 << (cl
- 1))) {
467 /* Minimum Clock Cycle at CLX-1 */
468 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
- 1], spd
[23])
471 "SPD decode: invalid min tCL for CAS%d\n",
473 ret
= SPD_STATUS_INVALID_FIELD
;
475 /* Maximum Data Access Time (tAC) from Clock at CLX-1 */
476 if (spd_decode_bcd_time(&dimm
->access_time
[cl
- 1], spd
[24])
479 "SPD decode: invalid min tAC for CAS%d\n",
481 ret
= SPD_STATUS_INVALID_FIELD
;
484 if (dimm
->cas_supported
& (1 << (cl
- 2))) {
485 /* Minimum Clock Cycle at CLX-2 */
486 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
- 2], spd
[25])
489 "SPD decode: invalid min tCL for CAS%d\n",
491 ret
= SPD_STATUS_INVALID_FIELD
;
493 /* Maximum Data Access Time (tAC) from Clock at CLX-2 */
494 if (spd_decode_bcd_time(&dimm
->access_time
[cl
- 2], spd
[26])
497 "SPD decode: invalid min tAC for CAS%d\n",
499 ret
= SPD_STATUS_INVALID_FIELD
;
503 reg8
= (spd
[31] >> 5) | (spd
[31] << 3);
506 "SPD decode: invalid rank density.\n");
507 ret
= SPD_STATUS_INVALID_FIELD
;
511 dimm
->ranksize_mb
= 128 * reg8
;
513 dimm
->size_mb
= dimm
->ranksize_mb
* dimm
->ranks
;
514 if (dimm
->size_mb
< 1024)
515 printram(" Capacity : %u MB\n", dimm
->size_mb
);
517 printram(" Capacity : %u GB\n", dimm
->size_mb
>> 10);
519 /* SDRAM Maximum Cycle Time (tCKmax) */
520 if (spd_decode_bcd_time(&dimm
->tCK
, spd
[43]) != CB_SUCCESS
) {
521 printk(BIOS_WARNING
, "SPD decode: invalid Max tCK\n");
522 ret
= SPD_STATUS_INVALID_FIELD
;
524 /* Minimum Write Recovery Time (tWRmin) */
525 dimm
->tWR
= spd_decode_quarter_time(spd
[36]);
526 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
527 dimm
->tRCD
= spd_decode_quarter_time(spd
[29]);
528 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
529 dimm
->tRRD
= spd_decode_quarter_time(spd
[28]);
530 /* Minimum Row Precharge Delay Time (tRPmin) */
531 dimm
->tRP
= spd_decode_quarter_time(spd
[27]);
532 /* Minimum Active to Precharge Delay Time (tRASmin) */
533 dimm
->tRAS
= spd
[30] << 8;
534 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
535 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
536 spd_decode_tRCtRFC_time(&spd
[40], &dimm
->tRC
, &dimm
->tRFC
);
537 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
538 dimm
->tWTR
= spd_decode_quarter_time(spd
[37]);
539 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
540 dimm
->tRTP
= spd_decode_quarter_time(spd
[38]);
541 /* Data Input Setup Time Before Strobe */
542 if (spd_decode_bcd_time(&dimm
->tDS
, spd
[34]) != CB_SUCCESS
) {
543 printk(BIOS_WARNING
, "SPD decode: invalid tDS\n");
544 ret
= SPD_STATUS_INVALID_FIELD
;
546 /* Data Input Hold Time After Strobe */
547 if (spd_decode_bcd_time(&dimm
->tDH
, spd
[35]) != CB_SUCCESS
) {
548 printk(BIOS_WARNING
, "SPD decode: invalid tDH\n");
549 ret
= SPD_STATUS_INVALID_FIELD
;
551 /* SDRAM Device DQS-DQ Skew for DQS and associated DQ signals */
552 dimm
->tDQSQ
= (spd
[44] << 8) / 100;
553 /* SDRAM Device Maximum Read Data Hold Skew Factor */
554 dimm
->tQHS
= (spd
[45] << 8) / 100;
555 /* PLL Relock Time in us */
556 dimm
->tPLL
= spd
[46] << 8;
557 /* Refresh rate in us */
558 if (spd_decode_tRR_time(&dimm
->tRR
, spd
[12]) != CB_SUCCESS
)
559 ret
= SPD_STATUS_INVALID_FIELD
;
560 dimm
->flags
.self_refresh
= (spd
[12] >> 7) & 1;
561 printram("The assembly supports self refresh: %s\n",
562 dimm
->flags
.self_refresh
? "true" : "false");
564 /* Number of PLLs on DIMM */
565 if (dimm
->rev
>= 0x11)
566 dimm
->plls
= (spd
[21] >> 2) & 0x3;
568 /* SDRAM Thermal and Refresh Options */
569 printram(" General features :");
570 if ((dimm
->rev
>= 0x12) && (spd
[22] & 0x04)) {
571 dimm
->flags
.pasr
= 1;
574 if ((dimm
->rev
>= 0x12) && (spd
[22] & 0x02)) {
575 dimm
->flags
.terminate_50ohms
= 1;
578 if (spd
[22] & 0x01) {
579 dimm
->flags
.weak_driver
= 1;
580 printram(" WEAK_DRIVER");
584 /* SDRAM Supported Burst length */
585 printram(" Burst length :");
586 if (spd
[16] & 0x08) {
590 if (spd
[16] & 0x04) {
596 dimm
->dimm_type
= spd
[20] & SPD_DDR2_DIMM_TYPE_MASK
;
597 printram(" Dimm type : %x\n", dimm
->dimm_type
);
599 dimm
->flags
.is_ecc
= !!(spd
[11] & 0x3);
600 printram(" ECC support : %x\n", dimm
->flags
.is_ecc
);
602 dimm
->flags
.stacked
= !!(spd
[5] & 0x10);
603 printram(" Package : %s\n",
604 dimm
->flags
.stacked
? "stack" : "planar");
607 memcpy(&dimm
->manufacturer_id
, &spd
[64], 4);
608 printram(" Manufacturer ID : %x\n", dimm
->manufacturer_id
);
612 dimm
->part_number
[16] = 0;
613 memcpy(dimm
->part_number
, &spd
[73], 16);
614 printram(" Part number : %s\n", dimm
->part_number
);
618 dimm
->year
= spd
[93] + 2000;
619 dimm
->weeks
= spd
[94];
620 printram(" Date : %d week %d\n", dimm
->year
, dimm
->weeks
);
624 memcpy(&dimm
->serial
, &spd
[95], 4);
625 printram(" Serial number : 0x%08x\n", dimm
->serial
);
631 * The information printed below has a more informational character, and is not
632 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
633 * and use the standard printk()'s below.
636 static void print_ns(const char *msg
, u32 val
)
640 fp
= (val
% 256) * 1000 / 256;
642 printk(BIOS_INFO
, "%s%3u.%.3u ns\n", msg
, mant
, fp
);
645 static void print_us(const char *msg
, u32 val
)
649 fp
= (val
% 256) * 1000 / 256;
651 printk(BIOS_INFO
, "%s%3u.%.3u us\n", msg
, mant
, fp
);
655 * \brief Print the info in DIMM
657 * Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
658 * selected, or for a purely informative output.
660 * @param dimm pointer to already decoded @ref dimm_attr structure
662 void dram_print_spd_ddr2(const struct dimm_attr_ddr2_st
*dimm
)
667 printk(BIOS_INFO
, " Row addr bits : %u\n", dimm
->row_bits
);
668 printk(BIOS_INFO
, " Column addr bits : %u\n", dimm
->col_bits
);
669 printk(BIOS_INFO
, " Number of ranks : %u\n", dimm
->ranks
);
670 printk(BIOS_INFO
, " DIMM Capacity : %u MB\n", dimm
->size_mb
);
671 printk(BIOS_INFO
, " Width : x%u\n", dimm
->width
);
672 printk(BIOS_INFO
, " Banks : %u\n", dimm
->banks
);
674 /* CAS Latencies Supported */
675 printk(BIOS_INFO
, " CAS latencies :");
676 for (i
= 2; i
< 8; i
++) {
677 if (dimm
->cas_supported
& (1 << i
))
678 printk(BIOS_INFO
, " %u", i
);
680 printk(BIOS_INFO
, "\n");
682 for (i
= 2; i
< 8; i
++) {
683 if (!(dimm
->cas_supported
& (1 << i
)))
686 strcpy(buf
, " tCK at CLx : ");
687 /* Simple snprintf replacement */
689 print_ns(buf
, dimm
->cycle_time
[i
]);
691 strcpy(buf
, " tAC at CLx : ");
692 /* Simple snprintf replacement */
694 print_ns(buf
, dimm
->access_time
[i
]);
696 print_ns(" tCKmax : ", dimm
->tCK
);
697 print_ns(" tWRmin : ", dimm
->tWR
);
698 print_ns(" tRCDmin : ", dimm
->tRCD
);
699 print_ns(" tRRDmin : ", dimm
->tRRD
);
700 print_ns(" tRPmin : ", dimm
->tRP
);
701 print_ns(" tRASmin : ", dimm
->tRAS
);
702 print_ns(" tRCmin : ", dimm
->tRC
);
703 print_ns(" tRFCmin : ", dimm
->tRFC
);
704 print_ns(" tWTRmin : ", dimm
->tWTR
);
705 print_ns(" tRTPmin : ", dimm
->tRTP
);
706 print_ns(" tDS : ", dimm
->tDS
);
707 print_ns(" tDH : ", dimm
->tDH
);
708 print_ns(" tDQSQmax : ", dimm
->tDQSQ
);
709 print_ns(" tQHSmax : ", dimm
->tQHS
);
710 print_us(" tPLL : ", dimm
->tPLL
);
711 print_us(" tRR : ", dimm
->tRR
);
714 void normalize_tck(u32
*tclk
)
716 if (*tclk
<= TCK_800MHZ
) {
718 } else if (*tclk
<= TCK_666MHZ
) {
720 } else if (*tclk
<= TCK_533MHZ
) {
722 } else if (*tclk
<= TCK_400MHZ
) {
724 } else if (*tclk
<= TCK_333MHZ
) {
726 } else if (*tclk
<= TCK_266MHZ
) {
728 } else if (*tclk
<= TCK_200MHZ
) {
732 printk(BIOS_ERR
, "Too slow common tCLK found\n");