blob: 58dc9b3781b7ca93eb2e5d51908a440dfc01c817 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
James Yang0f2cbe32008-08-26 15:01:27 -05002/*
York Sun34e026f2014-03-27 17:54:47 -07003 * Copyright 2008-2014 Freescale Semiconductor, Inc.
James Yang0f2cbe32008-08-26 15:01:27 -05004 */
5
6#include <common.h>
7#include <ddr_spd.h>
8
9/* used for ddr1 and ddr2 spd */
10static int
11spd_check(const u8 *buf, u8 spd_rev, u8 spd_cksum)
12{
13 unsigned int cksum = 0;
14 unsigned int i;
15
16 /*
17 * Check SPD revision supported
Kyle Moffettc7fd27c2011-03-28 11:35:48 -040018 * Rev 1.X or less supported by this code
James Yang0f2cbe32008-08-26 15:01:27 -050019 */
York Sunbcb6c2b2010-05-07 09:12:01 -050020 if (spd_rev >= 0x20) {
James Yang0f2cbe32008-08-26 15:01:27 -050021 printf("SPD revision %02X not supported by this code\n",
22 spd_rev);
23 return 1;
24 }
York Sunbcb6c2b2010-05-07 09:12:01 -050025 if (spd_rev > 0x13) {
26 printf("SPD revision %02X not verified by this code\n",
27 spd_rev);
28 }
James Yang0f2cbe32008-08-26 15:01:27 -050029
30 /*
31 * Calculate checksum
32 */
33 for (i = 0; i < 63; i++) {
34 cksum += *buf++;
35 }
36 cksum &= 0xFF;
37
38 if (cksum != spd_cksum) {
39 printf("SPD checksum unexpected. "
40 "Checksum in SPD = %02X, computed SPD = %02X\n",
41 spd_cksum, cksum);
42 return 1;
43 }
44
45 return 0;
46}
47
48unsigned int
49ddr1_spd_check(const ddr1_spd_eeprom_t *spd)
50{
51 const u8 *p = (const u8 *)spd;
52
53 return spd_check(p, spd->spd_rev, spd->cksum);
54}
55
56unsigned int
57ddr2_spd_check(const ddr2_spd_eeprom_t *spd)
58{
59 const u8 *p = (const u8 *)spd;
60
61 return spd_check(p, spd->spd_rev, spd->cksum);
62}
Dave Liuc360cea2009-03-14 12:48:30 +080063
64/*
65 * CRC16 compute for DDR3 SPD
66 * Copied from DDR3 SPD spec.
67 */
68static int
69crc16(char *ptr, int count)
70{
71 int crc, i;
72
73 crc = 0;
74 while (--count >= 0) {
75 crc = crc ^ (int)*ptr++ << 8;
76 for (i = 0; i < 8; ++i)
77 if (crc & 0x8000)
78 crc = crc << 1 ^ 0x1021;
79 else
80 crc = crc << 1;
81 }
82 return crc & 0xffff;
83}
84
85unsigned int
86ddr3_spd_check(const ddr3_spd_eeprom_t *spd)
87{
88 char *p = (char *)spd;
89 int csum16;
90 int len;
91 char crc_lsb; /* byte 126 */
92 char crc_msb; /* byte 127 */
93
94 /*
95 * SPD byte0[7] - CRC coverage
96 * 0 = CRC covers bytes 0~125
97 * 1 = CRC covers bytes 0~116
98 */
99
100 len = !(spd->info_size_crc & 0x80) ? 126 : 117;
101 csum16 = crc16(p, len);
102
103 crc_lsb = (char) (csum16 & 0xff);
104 crc_msb = (char) (csum16 >> 8);
105
106 if (spd->crc[0] == crc_lsb && spd->crc[1] == crc_msb) {
107 return 0;
108 } else {
109 printf("SPD checksum unexpected.\n"
110 "Checksum lsb in SPD = %02X, computed SPD = %02X\n"
111 "Checksum msb in SPD = %02X, computed SPD = %02X\n",
112 spd->crc[0], crc_lsb, spd->crc[1], crc_msb);
113 return 1;
114 }
115}
York Sun34e026f2014-03-27 17:54:47 -0700116
117unsigned int ddr4_spd_check(const struct ddr4_spd_eeprom_s *spd)
118{
119 char *p = (char *)spd;
120 int csum16;
121 int len;
122 char crc_lsb; /* byte 126 */
123 char crc_msb; /* byte 127 */
124
125 len = 126;
126 csum16 = crc16(p, len);
127
128 crc_lsb = (char) (csum16 & 0xff);
129 crc_msb = (char) (csum16 >> 8);
130
131 if (spd->crc[0] != crc_lsb || spd->crc[1] != crc_msb) {
132 printf("SPD checksum unexpected.\n"
133 "Checksum lsb in SPD = %02X, computed SPD = %02X\n"
134 "Checksum msb in SPD = %02X, computed SPD = %02X\n",
135 spd->crc[0], crc_lsb, spd->crc[1], crc_msb);
136 return 1;
137 }
138
139 p = (char *)((ulong)spd + 128);
140 len = 126;
141 csum16 = crc16(p, len);
142
143 crc_lsb = (char) (csum16 & 0xff);
144 crc_msb = (char) (csum16 >> 8);
145
146 if (spd->mod_section.uc[126] != crc_lsb ||
147 spd->mod_section.uc[127] != crc_msb) {
148 printf("SPD checksum unexpected.\n"
149 "Checksum lsb in SPD = %02X, computed SPD = %02X\n"
150 "Checksum msb in SPD = %02X, computed SPD = %02X\n",
151 spd->mod_section.uc[126],
152 crc_lsb, spd->mod_section.uc[127],
153 crc_msb);
154 return 1;
155 }
156
157 return 0;
158}