1 /* SPDX-License-Identifier: GPL-2.0-or-later */
6 * \brief Utilities for decoding DDR2 SPDs
9 #include <console/console.h>
10 #include <device/device.h>
11 #include <device/dram/ddr2.h>
16 /*==============================================================================
17 * = DDR2 SPD decoding helpers
18 *----------------------------------------------------------------------------*/
21 * \brief Checks if the DIMM is Registered based on byte[20] of the SPD
23 * Tells if the DIMM type is registered or not.
25 * @param type DIMM type. This is byte[20] of the SPD.
27 bool spd_dimm_is_registered_ddr2(enum spd_dimm_type_ddr2 type
)
29 if ((type
== SPD_DDR2_DIMM_TYPE_RDIMM
) || (type
== SPD_DDR2_DIMM_TYPE_72B_SO_RDIMM
) ||
30 (type
== SPD_DDR2_DIMM_TYPE_MINI_RDIMM
))
37 * \brief Calculate the checksum of a DDR2 SPD unique identifier
39 * @param spd pointer to raw SPD data
40 * @param len length of data in SPD
42 * @return the checksum of SPD data bytes 63, or 0 when spd data is truncated.
44 u8
spd_ddr2_calc_checksum(u8
*spd
, int len
)
50 /* Not enough bytes available to get the checksum */
53 for (i
= 0; i
< 63; i
++)
60 * \brief Calculate the CRC of a DDR2 SPD unique identifier
62 * @param spd pointer to raw SPD data
63 * @param len length of data in SPD
65 * @return the CRC of SPD data bytes 64..72 and 93..98, or 0
66 * when spd data is truncated.
68 u16
spd_ddr2_calc_unique_crc(const u8
*spd
, int len
)
73 /* Not enough bytes available to get the CRC */
75 for (i
= 64; i
<= 72; i
++)
76 id_bytes
[j
++] = spd
[i
];
77 for (i
= 93; i
<= 98; i
++)
78 id_bytes
[j
++] = spd
[i
];
80 return ddr_crc16(id_bytes
, 15);
84 * \brief Return size of SPD.
86 * Returns size of SPD. Usually 128 Byte.
88 u32
spd_decode_spd_size_ddr2(u8 byte0
)
90 return MIN(byte0
, SPD_SIZE_MAX_DDR2
);
94 * \brief Return size of eeprom.
96 * Returns size of eeprom. Usually 256 Byte.
98 u32
spd_decode_eeprom_size_ddr2(u8 byte1
)
110 * \brief Return index of MSB set
112 * Returns the index of MSB set.
114 u8
spd_get_msbs(u8 c
)
120 * \brief Decode SPD tck cycle time
122 * Decodes a raw SPD data from a DDR2 DIMM.
123 * Returns cycle time in 1/256th ns.
125 static enum cb_err
spd_decode_tck_time(u32
*tck
, u8 c
)
146 printk(BIOS_WARNING
, "Invalid tck setting. lower nibble is 0x%x\n", c
& 0xf);
149 low
= (c
& 0xf) * 10;
152 *tck
= ((high
* 100 + low
) << 8) / 100;
157 * \brief Decode SPD bcd style timings
159 * Decodes a raw SPD data from a DDR2 DIMM.
160 * Returns cycle time in 1/256th ns.
162 static enum cb_err
spd_decode_bcd_time(u32
*bcd
, u8 c
)
168 if (high
>= 10 || low
>= 10)
171 *bcd
= ((high
* 10 + low
) << 8) / 100;
176 * \brief Decode SPD tRP, tRRP cycle time
178 * Decodes a raw SPD data from a DDR2 DIMM.
179 * Returns cycle time in 1/256th ns.
181 static u32
spd_decode_quarter_time(u8 c
)
186 low
= 25 * (c
& 0x3);
188 return ((high
* 100 + low
) << 8) / 100;
192 * \brief Decode SPD tRR time
194 * Decodes a raw SPD data from a DDR2 DIMM.
195 * Returns cycle time in 1/256th us.
197 static enum cb_err
spd_decode_tRR_time(u32
*tRR
, u8 c
)
201 printk(BIOS_WARNING
, "Invalid tRR value 0x%x\n", c
);
226 * \brief Decode SPD tRC,tRFC time
228 * Decodes a raw SPD data from a DDR2 DIMM.
229 * Returns cycle time in 1/256th us.
231 static void spd_decode_tRCtRFC_time(u8
*spd_40_41_42
, u32
*tRC
, u32
*tRFC
)
235 b40
= spd_40_41_42
[0];
236 b41
= spd_40_41_42
[1];
237 b42
= spd_40_41_42
[2];
245 switch ((b40
>> 1) & 0x07) {
265 switch ((b40
>> 4) & 0x07) {
285 /* Convert to 1/256th us */
286 *tRC
= (*tRC
<< 8) / 100;
287 *tRFC
= (*tRFC
<< 8) / 100;
291 * \brief Decode the raw SPD data
293 * Decodes a raw SPD data from a DDR2 DIMM, and organizes it into a
294 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
295 * array, and passed to this function.
297 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
299 * @param spd array of raw data previously read from the SPD.
301 * @return @ref spd_status enumerator
302 * SPD_STATUS_OK -- decoding was successful
303 * SPD_STATUS_INVALID -- invalid SPD or not a DDR2 SPD
304 * SPD_STATUS_CRC_ERROR -- CRC did not verify
305 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
308 int spd_decode_ddr2(struct dimm_attr_ddr2_st
*dimm
, u8 spd
[SPD_SIZE_MAX_DDR2
])
310 u8 spd_size
, cl
, reg8
;
312 int ret
= SPD_STATUS_OK
;
314 memset(dimm
, 0, sizeof(*dimm
));
316 spd_size
= spd_decode_spd_size_ddr2(spd
[0]);
317 eeprom_size
= spd_decode_eeprom_size_ddr2(spd
[1]);
319 printram("EEPROM with 0x%04x bytes\n", eeprom_size
);
320 printram("SPD contains 0x%02x bytes\n", spd_size
);
322 if (spd_size
< 64 || eeprom_size
< 64) {
323 printk(BIOS_ERR
, "SPD too small\n");
324 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
325 return SPD_STATUS_INVALID
;
328 if (spd_ddr2_calc_checksum(spd
, spd_size
) != spd
[63]) {
329 printk(BIOS_ERR
, "SPD checksum error\n");
330 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
331 return SPD_STATUS_CRC_ERROR
;
333 dimm
->checksum
= spd
[63];
336 if ((reg8
& 0xf0) != 0x10) {
337 printk(BIOS_ERR
, "Unsupported SPD revision %01x.%01x\n", reg8
>> 4, reg8
& 0xf);
338 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
339 return SPD_STATUS_INVALID
;
342 printram(" Revision : %01x.%01x\n", dimm
->rev
>> 4, dimm
->rev
& 0xf);
345 printram(" Type : 0x%02x\n", reg8
);
347 printk(BIOS_ERR
, "Unsupported SPD type %x\n", reg8
);
348 dimm
->dram_type
= SPD_MEMORY_TYPE_UNDEFINED
;
349 return SPD_STATUS_INVALID
;
351 dimm
->dram_type
= SPD_MEMORY_TYPE_SDRAM_DDR2
;
353 dimm
->row_bits
= spd
[3];
354 printram(" Rows : %u\n", dimm
->row_bits
);
355 if ((dimm
->row_bits
> 31) || ((dimm
->row_bits
> 15) && (dimm
->rev
< 0x13))) {
356 printk(BIOS_WARNING
, "SPD decode: invalid number of memory rows\n");
357 ret
= SPD_STATUS_INVALID_FIELD
;
360 dimm
->col_bits
= spd
[4];
361 printram(" Columns : %u\n", dimm
->col_bits
);
362 if (dimm
->col_bits
> 15) {
363 printk(BIOS_WARNING
, "SPD decode: invalid number of memory columns\n");
364 ret
= SPD_STATUS_INVALID_FIELD
;
367 dimm
->ranks
= (spd
[5] & 0x7) + 1;
368 printram(" Ranks : %u\n", dimm
->ranks
);
370 dimm
->mod_width
= spd
[6];
371 printram(" Module data width : x%u\n", dimm
->mod_width
);
372 if (!dimm
->mod_width
) {
373 printk(BIOS_WARNING
, "SPD decode: invalid module data width\n");
374 ret
= SPD_STATUS_INVALID_FIELD
;
377 dimm
->width
= spd
[13];
378 printram(" SDRAM width : x%u\n", dimm
->width
);
380 printk(BIOS_WARNING
, "SPD decode: invalid SDRAM width\n");
381 ret
= SPD_STATUS_INVALID_FIELD
;
384 dimm
->banks
= spd
[17];
385 printram(" Banks : %u\n", dimm
->banks
);
387 printk(BIOS_WARNING
, "SPD decode: invalid module banks count\n");
388 ret
= SPD_STATUS_INVALID_FIELD
;
393 dimm
->flags
.operable_5_00V
= 1;
394 printram(" Voltage : 5.0V\n");
397 dimm
->flags
.operable_3_33V
= 1;
398 printram(" Voltage : 3.3V\n");
401 dimm
->flags
.operable_1_50V
= 1;
402 printram(" Voltage : 1.5V\n");
405 dimm
->flags
.operable_3_33V
= 1;
406 printram(" Voltage : 3.3V\n");
409 dimm
->flags
.operable_2_50V
= 1;
410 printram(" Voltage : 2.5V\n");
413 dimm
->flags
.operable_1_80V
= 1;
414 printram(" Voltage : 1.8V\n");
417 printk(BIOS_WARNING
, "SPD decode: unknown voltage level.\n");
418 ret
= SPD_STATUS_INVALID_FIELD
;
421 dimm
->cas_supported
= spd
[18];
422 if ((dimm
->cas_supported
& 0x3) || !dimm
->cas_supported
) {
423 printk(BIOS_WARNING
, "SPD decode: invalid CAS support advertised.\n");
424 ret
= SPD_STATUS_INVALID_FIELD
;
426 printram(" Supported CAS mask : 0x%x\n", dimm
->cas_supported
);
428 if ((dimm
->rev
< 0x13) && (dimm
->cas_supported
& 0x80)) {
429 printk(BIOS_WARNING
, "SPD decode: invalid CAS support advertised.\n");
430 ret
= SPD_STATUS_INVALID_FIELD
;
432 if ((dimm
->rev
< 0x12) && (dimm
->cas_supported
& 0x40)) {
433 printk(BIOS_WARNING
, "SPD decode: invalid CAS support advertised.\n");
434 ret
= SPD_STATUS_INVALID_FIELD
;
438 cl
= spd_get_msbs(dimm
->cas_supported
);
440 /* SDRAM Cycle time at Maximum Supported CAS Latency (CL), CL=X */
441 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
], spd
[9]) != CB_SUCCESS
) {
442 printk(BIOS_WARNING
, "SPD decode: invalid min tCL for CAS%d\n", cl
);
443 ret
= SPD_STATUS_INVALID_FIELD
;
445 /* SDRAM Access from Clock */
446 if (spd_decode_bcd_time(&dimm
->access_time
[cl
], spd
[10]) != CB_SUCCESS
) {
447 printk(BIOS_WARNING
, "SPD decode: invalid min tAC for CAS%d\n", cl
);
448 ret
= SPD_STATUS_INVALID_FIELD
;
451 if (dimm
->cas_supported
& (1 << (cl
- 1))) {
452 /* Minimum Clock Cycle at CLX-1 */
453 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
- 1], spd
[23]) != CB_SUCCESS
) {
454 printk(BIOS_WARNING
, "SPD decode: invalid min tCL for CAS%d\n", cl
- 1);
455 ret
= SPD_STATUS_INVALID_FIELD
;
457 /* Maximum Data Access Time (tAC) from Clock at CLX-1 */
458 if (spd_decode_bcd_time(&dimm
->access_time
[cl
- 1], spd
[24]) != CB_SUCCESS
) {
459 printk(BIOS_WARNING
, "SPD decode: invalid min tAC for CAS%d\n", cl
- 1);
460 ret
= SPD_STATUS_INVALID_FIELD
;
463 if (dimm
->cas_supported
& (1 << (cl
- 2))) {
464 /* Minimum Clock Cycle at CLX-2 */
465 if (spd_decode_tck_time(&dimm
->cycle_time
[cl
- 2], spd
[25]) != CB_SUCCESS
) {
466 printk(BIOS_WARNING
, "SPD decode: invalid min tCL for CAS%d\n", cl
- 2);
467 ret
= SPD_STATUS_INVALID_FIELD
;
469 /* Maximum Data Access Time (tAC) from Clock at CLX-2 */
470 if (spd_decode_bcd_time(&dimm
->access_time
[cl
- 2], spd
[26]) != CB_SUCCESS
) {
471 printk(BIOS_WARNING
, "SPD decode: invalid min tAC for CAS%d\n", cl
- 2);
472 ret
= SPD_STATUS_INVALID_FIELD
;
476 reg8
= (spd
[31] >> 5) | (spd
[31] << 3);
478 printk(BIOS_WARNING
, "SPD decode: invalid rank density.\n");
479 ret
= SPD_STATUS_INVALID_FIELD
;
483 dimm
->ranksize_mb
= 128 * reg8
;
485 dimm
->size_mb
= dimm
->ranksize_mb
* dimm
->ranks
;
486 if (dimm
->size_mb
< 1024)
487 printram(" Capacity : %u MB\n", dimm
->size_mb
);
489 printram(" Capacity : %u GB\n", dimm
->size_mb
>> 10);
491 /* SDRAM Maximum Cycle Time (tCKmax) */
492 if (spd_decode_bcd_time(&dimm
->tCK
, spd
[43]) != CB_SUCCESS
) {
493 printk(BIOS_WARNING
, "SPD decode: invalid Max tCK\n");
494 ret
= SPD_STATUS_INVALID_FIELD
;
496 /* Minimum Write Recovery Time (tWRmin) */
497 dimm
->tWR
= spd_decode_quarter_time(spd
[36]);
498 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
499 dimm
->tRCD
= spd_decode_quarter_time(spd
[29]);
500 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
501 dimm
->tRRD
= spd_decode_quarter_time(spd
[28]);
502 /* Minimum Row Precharge Delay Time (tRPmin) */
503 dimm
->tRP
= spd_decode_quarter_time(spd
[27]);
504 /* Minimum Active to Precharge Delay Time (tRASmin) */
505 dimm
->tRAS
= spd
[30] << 8;
506 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
507 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
508 spd_decode_tRCtRFC_time(&spd
[40], &dimm
->tRC
, &dimm
->tRFC
);
509 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
510 dimm
->tWTR
= spd_decode_quarter_time(spd
[37]);
511 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
512 dimm
->tRTP
= spd_decode_quarter_time(spd
[38]);
513 /* Data Input Setup Time Before Strobe */
514 if (spd_decode_bcd_time(&dimm
->tDS
, spd
[34]) != CB_SUCCESS
) {
515 printk(BIOS_WARNING
, "SPD decode: invalid tDS\n");
516 ret
= SPD_STATUS_INVALID_FIELD
;
518 /* Data Input Hold Time After Strobe */
519 if (spd_decode_bcd_time(&dimm
->tDH
, spd
[35]) != CB_SUCCESS
) {
520 printk(BIOS_WARNING
, "SPD decode: invalid tDH\n");
521 ret
= SPD_STATUS_INVALID_FIELD
;
523 /* SDRAM Device DQS-DQ Skew for DQS and associated DQ signals */
524 dimm
->tDQSQ
= (spd
[44] << 8) / 100;
525 /* SDRAM Device Maximum Read Data Hold Skew Factor */
526 dimm
->tQHS
= (spd
[45] << 8) / 100;
527 /* PLL Relock Time in us */
528 dimm
->tPLL
= spd
[46] << 8;
529 /* Refresh rate in us */
530 if (spd_decode_tRR_time(&dimm
->tRR
, spd
[12]) != CB_SUCCESS
)
531 ret
= SPD_STATUS_INVALID_FIELD
;
532 dimm
->flags
.self_refresh
= (spd
[12] >> 7) & 1;
533 printram("The assembly supports self refresh: %s\n",
534 dimm
->flags
.self_refresh
? "true" : "false");
536 /* Number of PLLs on DIMM */
537 if (dimm
->rev
>= 0x11)
538 dimm
->plls
= (spd
[21] >> 2) & 0x3;
540 /* SDRAM Thermal and Refresh Options */
541 printram(" General features :");
542 if ((dimm
->rev
>= 0x12) && (spd
[22] & 0x04)) {
543 dimm
->flags
.pasr
= 1;
546 if ((dimm
->rev
>= 0x12) && (spd
[22] & 0x02)) {
547 dimm
->flags
.terminate_50ohms
= 1;
550 if (spd
[22] & 0x01) {
551 dimm
->flags
.weak_driver
= 1;
552 printram(" WEAK_DRIVER");
556 /* SDRAM Supported Burst length */
557 printram(" Burst length :");
558 if (spd
[16] & 0x08) {
562 if (spd
[16] & 0x04) {
568 dimm
->dimm_type
= spd
[20] & SPD_DDR2_DIMM_TYPE_MASK
;
569 printram(" Dimm type : %x\n", dimm
->dimm_type
);
571 dimm
->flags
.is_ecc
= !!(spd
[11] & 0x3);
572 printram(" ECC support : %x\n", dimm
->flags
.is_ecc
);
574 dimm
->flags
.stacked
= !!(spd
[5] & 0x10);
575 printram(" Package : %s\n", dimm
->flags
.stacked
? "stack" : "planar");
578 memcpy(&dimm
->manufacturer_id
, &spd
[64], 4);
579 printram(" Manufacturer ID : %x\n", dimm
->manufacturer_id
);
583 dimm
->part_number
[16] = 0;
584 memcpy(dimm
->part_number
, &spd
[73], 16);
585 printram(" Part number : %s\n", dimm
->part_number
);
589 dimm
->year
= spd
[93] + 2000;
590 dimm
->weeks
= spd
[94];
591 printram(" Date : %d week %d\n", dimm
->year
, dimm
->weeks
);
595 memcpy(&dimm
->serial
, &spd
[95], 4);
596 printram(" Serial number : 0x%08x\n", dimm
->serial
);
602 * The information printed below has a more informational character, and is not
603 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
604 * and use the standard printk()'s below.
607 static void print_ns(const char *msg
, u32 val
)
611 fp
= (val
% 256) * 1000 / 256;
613 printk(BIOS_INFO
, "%s%3u.%.3u ns\n", msg
, mant
, fp
);
616 static void print_us(const char *msg
, u32 val
)
620 fp
= (val
% 256) * 1000 / 256;
622 printk(BIOS_INFO
, "%s%3u.%.3u us\n", msg
, mant
, fp
);
626 * \brief Print the info in DIMM
628 * Print info about the DIMM. Useful to use when CONFIG(DEBUG_RAM_SETUP) is
629 * selected, or for a purely informative output.
631 * @param dimm pointer to already decoded @ref dimm_attr structure
633 void dram_print_spd_ddr2(const struct dimm_attr_ddr2_st
*dimm
)
638 printk(BIOS_INFO
, " Row addr bits : %u\n", dimm
->row_bits
);
639 printk(BIOS_INFO
, " Column addr bits : %u\n", dimm
->col_bits
);
640 printk(BIOS_INFO
, " Number of ranks : %u\n", dimm
->ranks
);
641 printk(BIOS_INFO
, " DIMM Capacity : %u MB\n", dimm
->size_mb
);
642 printk(BIOS_INFO
, " Width : x%u\n", dimm
->width
);
643 printk(BIOS_INFO
, " Banks : %u\n", dimm
->banks
);
645 /* CAS Latencies Supported */
646 printk(BIOS_INFO
, " CAS latencies :");
647 for (i
= 2; i
< 8; i
++) {
648 if (dimm
->cas_supported
& (1 << i
))
649 printk(BIOS_INFO
, " %u", i
);
651 printk(BIOS_INFO
, "\n");
653 for (i
= 2; i
< 8; i
++) {
654 if (!(dimm
->cas_supported
& (1 << i
)))
657 strcpy(buf
, " tCK at CLx : ");
658 /* Simple snprintf replacement */
660 print_ns(buf
, dimm
->cycle_time
[i
]);
662 strcpy(buf
, " tAC at CLx : ");
663 /* Simple snprintf replacement */
665 print_ns(buf
, dimm
->access_time
[i
]);
667 print_ns(" tCKmax : ", dimm
->tCK
);
668 print_ns(" tWRmin : ", dimm
->tWR
);
669 print_ns(" tRCDmin : ", dimm
->tRCD
);
670 print_ns(" tRRDmin : ", dimm
->tRRD
);
671 print_ns(" tRPmin : ", dimm
->tRP
);
672 print_ns(" tRASmin : ", dimm
->tRAS
);
673 print_ns(" tRCmin : ", dimm
->tRC
);
674 print_ns(" tRFCmin : ", dimm
->tRFC
);
675 print_ns(" tWTRmin : ", dimm
->tWTR
);
676 print_ns(" tRTPmin : ", dimm
->tRTP
);
677 print_ns(" tDS : ", dimm
->tDS
);
678 print_ns(" tDH : ", dimm
->tDH
);
679 print_ns(" tDQSQmax : ", dimm
->tDQSQ
);
680 print_ns(" tQHSmax : ", dimm
->tQHS
);
681 print_us(" tPLL : ", dimm
->tPLL
);
682 print_us(" tRR : ", dimm
->tRR
);
685 void normalize_tck(u32
*tclk
)
687 if (*tclk
<= TCK_800MHZ
) {
689 } else if (*tclk
<= TCK_666MHZ
) {
691 } else if (*tclk
<= TCK_533MHZ
) {
693 } else if (*tclk
<= TCK_400MHZ
) {
695 } else if (*tclk
<= TCK_333MHZ
) {
697 } else if (*tclk
<= TCK_266MHZ
) {
699 } else if (*tclk
<= TCK_200MHZ
) {
703 printk(BIOS_ERR
, "Too slow common tCLK found\n");