blob: fd93f4678a4e6c27d29b6b41678c3cad03cdd0b0 [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
dhendrix@google.com7d320d22011-02-08 22:21:06 +000052/*
53 * spd_print_field_ddr3 - add common DDR SPD fields into key=value pair
54 *
55 * @intf: platform interface
56 * @kv: key=value pair
57 * @data: raw spd data
58 * @type: type of field to retrieve
59 *
60 * returns 1 to indicate data added to key=value pair
61 * returns 0 to indicate no data added
62 * returns <0 to indicate error
63 *
64 */
65int spd_print_field_ddr3(struct platform_intf *intf, struct kv_pair *kv,
66 const void *data, enum spd_field_type type)
67{
68 int ret;
69 const uint8_t *byte = data;
70
71 ret = 0;
72 switch (type) {
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000073 case SPD_GET_DRAM_TYPE:
Duncan Laurie8f9820a2016-02-08 14:45:55 -080074 kv_pair_add(kv, "dram",
75 (byte[DDR3_SPD_REG_DEVICE_TYPE] ==
76 SPD_DRAM_TYPE_LPDDR3) ? "LPDDR3" : "DDR3");
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000077 ret = 1;
78 break;
79 case SPD_GET_MODULE_TYPE:
dhendrix@google.com597d3032011-02-12 00:05:53 +000080 kv_pair_add(kv, "module",
dhendrix@google.com1b1f1d12011-02-12 00:01:04 +000081 val2str(byte[DDR3_SPD_REG_MODULE_TYPE],
82 ddr3_module_type_lut));
83 ret = 1;
84 break;
dhendrix@google.com7d320d22011-02-08 22:21:06 +000085 case SPD_GET_MFG_ID:
86 {
87 uint8_t manuf_lsb;
88 uint8_t manuf_msb;
89 const char *tstr;
90
91 manuf_lsb = byte[DDR3_SPD_REG_MODULE_MANUF_JEDEC_ID_LSB] & 0x7f;
dhendrix@google.com2f6879c2011-02-09 04:29:50 +000092 manuf_msb = byte[DDR3_SPD_REG_MODULE_MANUF_JEDEC_ID_MSB] & 0x7f;
dhendrix@google.com7d320d22011-02-08 22:21:06 +000093
94 tstr = jedec_manufacturer(manuf_lsb, manuf_msb);
95
96 if (tstr != NULL) {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +000097 kv_pair_fmt(kv, "module_mfg", "%u-%u: %s", manuf_lsb + 1,
dhendrix@google.com7d320d22011-02-08 22:21:06 +000098 manuf_msb, tstr);
99 } else {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000100 kv_pair_fmt(kv, "module_mfg", "%u-%u", manuf_lsb + 1,
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000101 manuf_msb);
102 }
103 ret = 1;
104 break;
105 }
106
107 case SPD_GET_MFG_ID_DRAM:
108 {
109 uint8_t manuf_lsb;
110 uint8_t manuf_msb;
111 const char *tstr;
112
113 manuf_lsb = byte[DDR3_SPD_REG_DRAM_MANUF_JEDEC_ID_LSB] & 0x7f;
dhendrix@google.com2f6879c2011-02-09 04:29:50 +0000114 manuf_msb = byte[DDR3_SPD_REG_DRAM_MANUF_JEDEC_ID_MSB] & 0x7f;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000115
116 tstr = jedec_manufacturer(manuf_lsb, manuf_msb);
117
118 if (tstr != NULL) {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000119 kv_pair_fmt(kv, "dram_mfg", "%u-%u: %s",
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000120 manuf_lsb + 1, manuf_msb, tstr);
121 } else {
dhendrix@google.com00d74fc2011-02-09 03:02:11 +0000122 kv_pair_fmt(kv, "dram_mfg", "%u-%u",
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000123 manuf_lsb + 1, manuf_msb);
124 }
125 ret = 1;
126 break;
127 }
128
129 case SPD_GET_MFG_LOC:
130 {
131 kv_pair_fmt(kv, "mfg_loc", "0x%02x",
132 byte[DDR3_SPD_REG_MODULE_MANUF_LOC]);
133 ret = 1;
134 break;
135 }
136
137 case SPD_GET_MFG_DATE: /* manufacturing date (BCD values) */
138 {
139 uint8_t year;
140 uint8_t week;
141
142 year = byte[DDR3_SPD_REG_MODULE_MANUF_DATE_YEAR];
143 week = byte[DDR3_SPD_REG_MODULE_MANUF_DATE_YEAR];
144 kv_pair_fmt(kv, "mfg_date", "20%02x-wk%02x", week, year);
145 ret = 1;
146 break;
147 }
148
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000149 case SPD_GET_PART_NUMBER:
150 {
151 char part[19];
152
153 memcpy(part, &byte[DDR3_SPD_REG_MODULE_PART_NUM_0], 18);
154 part[18] = '\0';
155 kv_pair_fmt(kv, "part_number", "%s", part);
156
157 ret = 1;
158 break;
159 }
160
161 case SPD_GET_REVISION_CODE:
162 {
163 kv_pair_fmt(kv, "revision_code", "0x%02x%02x",
164 byte[DDR3_SPD_REG_MODULE_REVISION_0],
165 byte[DDR3_SPD_REG_MODULE_REVISION_1]);
166 ret = 1;
167 break;
168 }
169
170 case SPD_GET_SIZE:
171 {
172 /* See "Calculating Module Capacity" section in DDR3 SPD
173 * specification for details. */
David Hendricksae2d5422012-07-18 14:52:41 -0700174 unsigned int size;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000175
176 /* calculate the total size in MB */
177 size = 256 << (byte[DDR3_SPD_REG_DENSITY_BANKS] & 0xf);
178 size >>= 3; /* in terms of bytes instead of bits. */
179 size *= 8 << (byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] & 0x7);
180 size /= 4 << (byte[DDR3_SPD_REG_MODULE_ORG] & 0x7);
181 size *= 1 + ((byte[DDR3_SPD_REG_MODULE_ORG] >> 3) & 0x7);
182
David Hendricksae2d5422012-07-18 14:52:41 -0700183 kv_pair_fmt(kv, "size_mb", "%u", size);
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000184 ret = 1;
185 break;
186 }
187
188 case SPD_GET_ECC:
189 {
190 uint8_t bus_ext_width = byte[DDR3_SPD_REG_MODULE_BUS_WIDTH];
191 bus_ext_width >>= 3;
192 bus_ext_width &= 0x7;
193 kv_pair_add_bool(kv, "ecc", bus_ext_width);
194 ret = 1;
195 break;
196 }
197
198 case SPD_GET_RANKS:
199 {
200 kv_pair_fmt(kv, "ranks", "%d",
201 1 + ((byte[DDR3_SPD_REG_MODULE_ORG] >> 3) & 0x7));
202 ret = 1;
203 break;
204 }
205
206 case SPD_GET_WIDTH:
207 {
208 /* Total width including ECC. */
209 uint8_t width;
210 width = 8 << (byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] & 0x7);
211 width += 8 * ((byte[DDR3_SPD_REG_MODULE_BUS_WIDTH] >> 3) & 0x7);
212 kv_pair_fmt(kv, "width", "%d", width);
213 ret = 1;
214 break;
215 }
216
217 case SPD_GET_CHECKSUM:
218 {
219 kv_pair_fmt(kv, "checksum", "0x%02x%02x",
220 byte[DDR3_SPD_REG_CRC_1],
221 byte[DDR3_SPD_REG_CRC_0]);
222 ret = 1;
223 break;
224 }
225
226 case SPD_GET_SPEEDS:
227 {
David Hendricks7d51ea82014-04-01 17:22:58 -0700228 int i, mhz, first_entry;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000229 char speeds[128];
230 const struct valstr possible_mhz[] = {
David Hendricks4f5033a2013-04-03 14:10:19 -0700231 { 400, "DDR3-800" },
232 { 533, "DDR3-1066" },
233 { 667, "DDR3-1333" },
234 { 800, "DDR3-1600" },
235 { 933, "DDR3-1866" },
236 { 1067, "DDR3-2133" },
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000237 { 0 }
238 };
David Hendricks7d51ea82014-04-01 17:22:58 -0700239 int tck_mtb = byte[DDR3_SPD_REG_TCK_MIN];
240 int mtb_dividend = byte[DDR3_SPD_REG_MTB_DIVIDEND];
241 int mtb_divisor = byte[DDR3_SPD_REG_MTB_DIVISOR];
242 int ftb_dividend = byte[DDR3_SPD_REG_FTB_DIVIDEND_DIVSOR] >> 4;
243 int ftb_divisor = byte[DDR3_SPD_REG_FTB_DIVIDEND_DIVSOR] & 0xf;
244 double tck_ns, mtb = 0.0, ftb_ns = 0.0;
245 /* fine offset is encoded in 2's complement format */
246 int8_t ftb_offset = byte[DDR3_SPD_REG_FINE_OFFSET_TCK_MIN];
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000247
David Hendricks7d51ea82014-04-01 17:22:58 -0700248 /* Sanity check that MTB and FTB values are >=1 (as per spec) */
249 ret = -1;
250 if (!mtb_dividend)
251 lprintf(LOG_ERR, "Invalid MTB dividend from SPD\n");
252 else if (!mtb_divisor)
253 lprintf(LOG_ERR, "Invalid MTB divisor from SPD\n");
254 else if (!ftb_dividend)
255 lprintf(LOG_ERR, "Invalid FTB dividend from SPD\n");
256 else if (!ftb_divisor)
257 lprintf(LOG_ERR, "Invalid FTB divisor from SPD\n");
258 else
259 ret = 0;
260 if (ret)
261 break;
262
263 mtb = (double)mtb_dividend / mtb_divisor;
264 ftb_ns = ((double)(ftb_dividend) / ftb_divisor) / 1000;
265 tck_ns = tck_mtb * mtb + (ftb_offset * ftb_ns);
266 mhz = (int)((double)1000/tck_ns);
267
268 lprintf(LOG_DEBUG, "%s: %d * %.03fns + %d * %.03fns = %.02fns,"
269 " mhz = %d\n", __func__,
270 tck_mtb, mtb, ftb_offset, ftb_ns, tck_ns, mhz);
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000271
272 memset(speeds, 0, sizeof(speeds));
David Hendricks7d51ea82014-04-01 17:22:58 -0700273 first_entry = 1;
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000274 for (i = 0; possible_mhz[i].val != 0; i++) {
dhendrix@google.com04cdd4f2011-09-21 22:20:32 +0000275 double min = possible_mhz[i].val * 0.99;
276
277 if (min <= mhz) {
David Hendricks7d51ea82014-04-01 17:22:58 -0700278 if (!first_entry)
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000279 strcat(speeds, ", ");
David Hendricks7d51ea82014-04-01 17:22:58 -0700280 first_entry = 0;
Duncan Laurie8f9820a2016-02-08 14:45:55 -0800281 if (byte[DDR3_SPD_REG_DEVICE_TYPE] ==
282 SPD_DRAM_TYPE_LPDDR3)
283 strcat(speeds, "LP");
dhendrix@google.com7d320d22011-02-08 22:21:06 +0000284 strcat(speeds, possible_mhz[i].str);
285 }
286 }
287
288 kv_pair_add(kv, "speeds", speeds);
289 ret = 1;
290 break;
291 }
292
293 default:
294 {
295 ret = 0; /* force "we don't handle this here */
296 break;
297 }
298 }
299
300 return ret;
301}