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