blob: ae6bca9f3c6c02c531a3c8fe9c73e7cf06d66a6a [file] [log] [blame]
davidhendricks@gmail.comffdc0932012-02-27 23:46:44 +00001/* Copyright 2012, Google Inc.
2 * All rights reserved.
dhendrix@google.com7d320d22011-02-08 22:21:06 +00003 *
davidhendricks@gmail.comffdc0932012-02-27 23:46:44 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
dhendrix@google.com7d320d22011-02-08 22:21:06 +00007 *
davidhendricks@gmail.comffdc0932012-02-27 23:46:44 +00008 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
dhendrix@google.com7d320d22011-02-08 22:21:06 +000017 *
davidhendricks@gmail.comffdc0932012-02-27 23:46:44 +000018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dhendrix@google.com7d320d22011-02-08 22:21:06 +000029 *
30 * DDR3 field access for DDR3 SPDs.
31 */
32
33#include <ctype.h>
34#include <stdint.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38
dhendrix@google.comcc628f52011-09-28 01:20:37 +000039#include <valstr.h>
40
dhendrix@google.com7d320d22011-02-08 22:21:06 +000041#include "mosys/platform.h"
42#include "mosys/kv_pair.h"
David Hendricks7d51ea82014-04-01 17:22:58 -070043#include "mosys/log.h"
dhendrix@google.com7d320d22011-02-08 22:21:06 +000044
45#include "lib/string.h"
dhendrix@google.com7d320d22011-02-08 22:21:06 +000046
47#include "lib/ddr3.h"
dhendrix@google.com7d320d22011-02-08 22:21:06 +000048#include "lib/spd.h"
49
dhendrix@google.com1f8bd822011-02-08 22:57:20 +000050#include "jedec_id.h"
51
David Hendricks7d51ea82014-04-01 17:22:58 -070052
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000053static const struct valstr ddr3_module_type_lut[] = {
54 { 0x00, "Undefined" },
55 { 0x01, "RDIMM" },
56 { 0x02, "UDIMM" },
57 { 0x03, "SO-DIMM" },
58 { 0x04, "MICRO-DIMM" },
59 { 0x05, "MINI-RDIMM" },
60 { 0x06, "MINI-UDIMM" },
61 { 0x07, "MINI-CDIMM" },
62 { 0x08, "72b-SO-UDIMM" },
63 { 0x09, "72b-SO-RDIMM" },
64 { 0x0a, "72b-SO-CDIMM" },
65 { 0x0b, "LRDIMM" },
66};
67
dhendrix@google.com7d320d22011-02-08 22:21:06 +000068/*
69 * spd_print_field_ddr3 - add common DDR SPD fields into key=value pair
70 *
71 * @intf: platform interface
72 * @kv: key=value pair
73 * @data: raw spd data
74 * @type: type of field to retrieve
75 *
76 * returns 1 to indicate data added to key=value pair
77 * returns 0 to indicate no data added
78 * returns <0 to indicate error
79 *
80 */
81int spd_print_field_ddr3(struct platform_intf *intf, struct kv_pair *kv,
82 const void *data, enum spd_field_type type)
83{
84 int ret;
85 const uint8_t *byte = data;
86
87 ret = 0;
88 switch (type) {
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000089 case SPD_GET_DRAM_TYPE:
dhendrix@google.com597d3032011-02-12 00:05:53 +000090 kv_pair_add(kv, "dram", "DDR3");
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000091 ret = 1;
92 break;
93 case SPD_GET_MODULE_TYPE:
dhendrix@google.com597d3032011-02-12 00:05:53 +000094 kv_pair_add(kv, "module",
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000095 val2str(byte[DDR3_SPD_REG_MODULE_TYPE],
96 ddr3_module_type_lut));
97 ret = 1;
98 break;
dhendrix@google.com7d320d22011-02-08 22:21:06 +000099 case SPD_GET_MFG_ID:
100 {
101 uint8_t manuf_lsb;
102 uint8_t manuf_msb;
103 const char *tstr;
104
105 manuf_lsb = byte[DDR3_SPD_REG_MODULE_MANUF_JEDEC_ID_LSB] & 0x7f;
dhendrix@google.com2f6879c2011-02-09 04:29:50 +0000106 manuf_msb = byte[DDR3_SPD_REG_MODULE_MANUF_JEDEC_ID_MSB] & 0x7f;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000107
108 tstr = jedec_manufacturer(manuf_lsb, manuf_msb);
109
110 if (tstr != NULL) {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000111 kv_pair_fmt(kv, "module_mfg", "%u-%u: %s", manuf_lsb + 1,
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000112 manuf_msb, tstr);
113 } else {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000114 kv_pair_fmt(kv, "module_mfg", "%u-%u", manuf_lsb + 1,
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000115 manuf_msb);
116 }
117 ret = 1;
118 break;
119 }
120
121 case SPD_GET_MFG_ID_DRAM:
122 {
123 uint8_t manuf_lsb;
124 uint8_t manuf_msb;
125 const char *tstr;
126
127 manuf_lsb = byte[DDR3_SPD_REG_DRAM_MANUF_JEDEC_ID_LSB] & 0x7f;
dhendrix@google.com2f6879c2011-02-09 04:29:50 +0000128 manuf_msb = byte[DDR3_SPD_REG_DRAM_MANUF_JEDEC_ID_MSB] & 0x7f;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000129
130 tstr = jedec_manufacturer(manuf_lsb, manuf_msb);
131
132 if (tstr != NULL) {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000133 kv_pair_fmt(kv, "dram_mfg", "%u-%u: %s",
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000134 manuf_lsb + 1, manuf_msb, tstr);
135 } else {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000136 kv_pair_fmt(kv, "dram_mfg", "%u-%u",
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000137 manuf_lsb + 1, manuf_msb);
138 }
139 ret = 1;
140 break;
141 }
142
143 case SPD_GET_MFG_LOC:
144 {
145 kv_pair_fmt(kv, "mfg_loc", "0x%02x",
146 byte[DDR3_SPD_REG_MODULE_MANUF_LOC]);
147 ret = 1;
148 break;
149 }
150
151 case SPD_GET_MFG_DATE: /* manufacturing date (BCD values) */
152 {
153 uint8_t year;
154 uint8_t week;
155
156 year = byte[DDR3_SPD_REG_MODULE_MANUF_DATE_YEAR];
157 week = byte[DDR3_SPD_REG_MODULE_MANUF_DATE_YEAR];
158 kv_pair_fmt(kv, "mfg_date", "20%02x-wk%02x", week, year);
159 ret = 1;
160 break;
161 }
162
163 case SPD_GET_SERIAL_NUMBER:
164 {
dhendrix@google.comdac5b422011-02-09 02:43:16 +0000165 kv_pair_fmt(kv, "serial_number", "%02x%02x%02x%02x",
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000166 byte[DDR3_SPD_REG_MODULE_MANUF_SERIAL_0],
167 byte[DDR3_SPD_REG_MODULE_MANUF_SERIAL_1],
168 byte[DDR3_SPD_REG_MODULE_MANUF_SERIAL_2],
169 byte[DDR3_SPD_REG_MODULE_MANUF_SERIAL_3]);
170 ret = 1;
171 break;
172 }
173
174 case SPD_GET_PART_NUMBER:
175 {
176 char part[19];
177
178 memcpy(part, &byte[DDR3_SPD_REG_MODULE_PART_NUM_0], 18);
179 part[18] = '\0';
180 kv_pair_fmt(kv, "part_number", "%s", part);
181
182 ret = 1;
183 break;
184 }
185
186 case SPD_GET_REVISION_CODE:
187 {
188 kv_pair_fmt(kv, "revision_code", "0x%02x%02x",
189 byte[DDR3_SPD_REG_MODULE_REVISION_0],
190 byte[DDR3_SPD_REG_MODULE_REVISION_1]);
191 ret = 1;
192 break;
193 }
194
195 case SPD_GET_SIZE:
196 {
197 /* See "Calculating Module Capacity" section in DDR3 SPD
198 * specification for details. */
David Hendricksae2d5422012-07-18 14:52:41 -0700199 unsigned int size;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000200
201 /* calculate the total size in MB */
202 size = 256 << (byte[DDR3_SPD_REG_DENSITY_BANKS] & 0xf);
203 size >>= 3; /* in terms of bytes instead of bits. */
204 size *= 8 << (byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] & 0x7);
205 size /= 4 << (byte[DDR3_SPD_REG_MODULE_ORG] & 0x7);
206 size *= 1 + ((byte[DDR3_SPD_REG_MODULE_ORG] >> 3) & 0x7);
207
David Hendricksae2d5422012-07-18 14:52:41 -0700208 kv_pair_fmt(kv, "size_mb", "%u", size);
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000209 ret = 1;
210 break;
211 }
212
213 case SPD_GET_ECC:
214 {
215 uint8_t bus_ext_width = byte[DDR3_SPD_REG_MODULE_BUS_WIDTH];
216 bus_ext_width >>= 3;
217 bus_ext_width &= 0x7;
218 kv_pair_add_bool(kv, "ecc", bus_ext_width);
219 ret = 1;
220 break;
221 }
222
223 case SPD_GET_RANKS:
224 {
225 kv_pair_fmt(kv, "ranks", "%d",
226 1 + ((byte[DDR3_SPD_REG_MODULE_ORG] >> 3) & 0x7));
227 ret = 1;
228 break;
229 }
230
231 case SPD_GET_WIDTH:
232 {
233 /* Total width including ECC. */
234 uint8_t width;
235 width = 8 << (byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] & 0x7);
236 width += 8 * ((byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] >> 3) & 0x7);
237 kv_pair_fmt(kv, "width", "%d", width);
238 ret = 1;
239 break;
240 }
241
242 case SPD_GET_CHECKSUM:
243 {
244 kv_pair_fmt(kv, "checksum", "0x%02x%02x",
245 byte[DDR3_SPD_REG_CRC_1],
246 byte[DDR3_SPD_REG_CRC_0]);
247 ret = 1;
248 break;
249 }
250
251 case SPD_GET_SPEEDS:
252 {
David Hendricks7d51ea82014-04-01 17:22:58 -0700253 int i, mhz, first_entry;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000254 char speeds[128];
255 const struct valstr possible_mhz[] = {
David Hendricks4f5033a2013-04-03 14:10:19 -0700256 { 400, "DDR3-800" },
257 { 533, "DDR3-1066" },
258 { 667, "DDR3-1333" },
259 { 800, "DDR3-1600" },
260 { 933, "DDR3-1866" },
261 { 1067, "DDR3-2133" },
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000262 { 0 }
263 };
David Hendricks7d51ea82014-04-01 17:22:58 -0700264 int tck_mtb = byte[DDR3_SPD_REG_TCK_MIN];
265 int mtb_dividend = byte[DDR3_SPD_REG_MTB_DIVIDEND];
266 int mtb_divisor = byte[DDR3_SPD_REG_MTB_DIVISOR];
267 int ftb_dividend = byte[DDR3_SPD_REG_FTB_DIVIDEND_DIVSOR] >> 4;
268 int ftb_divisor = byte[DDR3_SPD_REG_FTB_DIVIDEND_DIVSOR] & 0xf;
269 double tck_ns, mtb = 0.0, ftb_ns = 0.0;
270 /* fine offset is encoded in 2's complement format */
271 int8_t ftb_offset = byte[DDR3_SPD_REG_FINE_OFFSET_TCK_MIN];
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000272
David Hendricks7d51ea82014-04-01 17:22:58 -0700273 /* Sanity check that MTB and FTB values are >=1 (as per spec) */
274 ret = -1;
275 if (!mtb_dividend)
276 lprintf(LOG_ERR, "Invalid MTB dividend from SPD\n");
277 else if (!mtb_divisor)
278 lprintf(LOG_ERR, "Invalid MTB divisor from SPD\n");
279 else if (!ftb_dividend)
280 lprintf(LOG_ERR, "Invalid FTB dividend from SPD\n");
281 else if (!ftb_divisor)
282 lprintf(LOG_ERR, "Invalid FTB divisor from SPD\n");
283 else
284 ret = 0;
285 if (ret)
286 break;
287
288 mtb = (double)mtb_dividend / mtb_divisor;
289 ftb_ns = ((double)(ftb_dividend) / ftb_divisor) / 1000;
290 tck_ns = tck_mtb * mtb + (ftb_offset * ftb_ns);
291 mhz = (int)((double)1000/tck_ns);
292
293 lprintf(LOG_DEBUG, "%s: %d * %.03fns + %d * %.03fns = %.02fns,"
294 " mhz = %d\n", __func__,
295 tck_mtb, mtb, ftb_offset, ftb_ns, tck_ns, mhz);
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000296
297 memset(speeds, 0, sizeof(speeds));
David Hendricks7d51ea82014-04-01 17:22:58 -0700298 first_entry = 1;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000299 for (i = 0; possible_mhz[i].val != 0; i++) {
dhendrix@google.com04cdd4f2011-09-21 22:20:32 +0000300 double min = possible_mhz[i].val * 0.99;
301
302 if (min <= mhz) {
David Hendricks7d51ea82014-04-01 17:22:58 -0700303 if (!first_entry)
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000304 strcat(speeds, ", ");
David Hendricks7d51ea82014-04-01 17:22:58 -0700305 first_entry = 0;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000306 strcat(speeds, possible_mhz[i].str);
307 }
308 }
309
310 kv_pair_add(kv, "speeds", speeds);
311 ret = 1;
312 break;
313 }
314
315 default:
316 {
317 ret = 0; /* force "we don't handle this here */
318 break;
319 }
320 }
321
322 return ret;
323}